|
18 | 18 | */
|
19 | 19 | package org.neo4j.driver.v1.util;
|
20 | 20 |
|
21 |
| -import org.junit.jupiter.api.extension.AfterAllCallback; |
22 |
| -import org.junit.jupiter.api.extension.BeforeAllCallback; |
| 21 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
23 | 22 | import org.junit.jupiter.api.extension.ExtensionContext;
|
24 | 23 |
|
| 24 | +import java.io.IOException; |
25 | 25 | import java.nio.file.Files;
|
26 | 26 | import java.nio.file.Path;
|
27 | 27 | import java.nio.file.StandardCopyOption;
|
| 28 | +import java.util.Arrays; |
28 | 29 |
|
29 |
| -public class CertificateExtension extends DatabaseExtension implements BeforeAllCallback, AfterAllCallback |
| 30 | +import static org.neo4j.driver.v1.util.Neo4jRunner.debug; |
| 31 | + |
| 32 | +public class CertificateExtension extends DatabaseExtension implements AfterEachCallback |
30 | 33 | {
|
31 |
| - private static Path originalKeyFile; |
32 |
| - private static Path originalCertFile; |
| 34 | + private Path originalKeyFile; |
| 35 | + private Path originalCertFile; |
33 | 36 |
|
34 | 37 | @Override
|
35 |
| - public void beforeAll( ExtensionContext extensionContext ) throws Exception |
| 38 | + public void beforeEach( ExtensionContext context ) throws Exception |
36 | 39 | {
|
| 40 | + super.beforeEach( context ); |
| 41 | + |
37 | 42 | originalKeyFile = Files.createTempFile( "key-file-", "" );
|
38 | 43 | originalCertFile = Files.createTempFile( "cert-file-", "" );
|
39 | 44 |
|
40 |
| - Files.copy( tlsKeyFile().toPath(), originalKeyFile , StandardCopyOption.REPLACE_EXISTING); |
| 45 | + Files.copy( tlsKeyFile().toPath(), originalKeyFile, StandardCopyOption.REPLACE_EXISTING ); |
41 | 46 | Files.copy( tlsCertFile().toPath(), originalCertFile, StandardCopyOption.REPLACE_EXISTING );
|
42 | 47 | }
|
43 | 48 |
|
44 | 49 | @Override
|
45 |
| - public void afterAll( ExtensionContext extensionContext ) throws Exception |
| 50 | + public void afterEach( ExtensionContext context ) throws Exception |
46 | 51 | {
|
47 |
| - updateEncryptionKeyAndCert( originalKeyFile.toFile(), originalCertFile.toFile() ); |
| 52 | + // if the key and cert file changed, then we restore the file and restart the server. |
| 53 | + if ( !smallFileContentEquals( tlsKeyFile().toPath(), originalKeyFile ) || !smallFileContentEquals( tlsCertFile().toPath(), originalCertFile ) ) |
| 54 | + { |
| 55 | + debug( "Restoring original key and certificate file after certificate test." ); |
| 56 | + updateEncryptionKeyAndCert( originalKeyFile.toFile(), originalCertFile.toFile() ); |
| 57 | + } |
48 | 58 | Files.deleteIfExists( originalKeyFile );
|
49 | 59 | Files.deleteIfExists( originalCertFile );
|
50 | 60 | }
|
| 61 | + |
| 62 | + private boolean smallFileContentEquals( Path path, Path pathAnother ) throws IOException |
| 63 | + { |
| 64 | + byte[] fileContent = Files.readAllBytes( path ); |
| 65 | + byte[] fileContentAnother = Files.readAllBytes( pathAnother ); |
| 66 | + return Arrays.equals( fileContent, fileContentAnother ); |
| 67 | + } |
51 | 68 | }
|
0 commit comments