Skip to content

Commit fa22b4b

Browse files
committed
Delete tempDirectory in @AfterClass hook.
1 parent 732ba41 commit fa22b4b

File tree

1 file changed

+33
-11
lines changed
  • substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jmx

1 file changed

+33
-11
lines changed

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jmx/JmxTest.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.lang.management.OperatingSystemMXBean;
4343
import java.lang.management.RuntimeMXBean;
4444
import java.lang.management.ThreadMXBean;
45+
import java.nio.file.DirectoryStream;
4546
import java.nio.file.Files;
4647
import java.nio.file.Path;
4748
import java.nio.file.attribute.PosixFilePermission;
@@ -61,6 +62,7 @@
6162
import javax.rmi.ssl.SslRMIClientSocketFactory;
6263

6364
import org.graalvm.nativeimage.ImageInfo;
65+
import org.junit.AfterClass;
6466
import org.junit.Assert;
6567
import org.junit.BeforeClass;
6668
import org.junit.Test;
@@ -96,6 +98,8 @@ public class JmxTest {
9698
static final String TEST_ROLE_PASSWORD = "MYTESTP@SSWORD";
9799
static final String TRUE = "true";
98100

101+
private static Path tempDirectory;
102+
99103
@BeforeClass
100104
public static void setup() throws IOException {
101105
assumeTrue("skipping JMX tests", !ImageInfo.inImageCode() ||
@@ -109,19 +113,19 @@ public static void setup() throws IOException {
109113
System.setProperty(REGISTRY_SSL_PROPERTY, TRUE);
110114

111115
// Prepare temp directory with files required for testing authentication.
112-
Path tempDirectory = Files.createTempDirectory("jmxtest");
116+
tempDirectory = Files.createTempDirectory("jmxtest");
113117
Path jmxRemoteAccess = tempDirectory.resolve("jmxremote.access");
114118
Path jmxRemotePassword = tempDirectory.resolve("jmxremote.password");
115119
Path clientKeyStore = tempDirectory.resolve(KEYSTORE_FILENAME);
116120
Path serverTrustStore = tempDirectory.resolve(TRUSTSTORE_FILENAME);
117121

118122
// Generate SSL keystore, client cert, and truststore for testing SSL connection.
119-
createClientKey(tempDirectory);
120-
createClientCert(tempDirectory);
123+
createClientKey();
124+
createClientCert();
121125
assertTrue("Failed to create " + KEYSTORE_FILENAME, Files.exists(clientKeyStore));
122126
System.setProperty(KEYSTORE_PROPERTY, clientKeyStore.toString());
123127
System.setProperty(KEYSTORE_PASSWORD_PROPERTY, KEYSTORE_PASSWORD);
124-
createServerTrustStore(tempDirectory);
128+
createServerTrustStore();
125129
assertTrue("Failed to create " + TRUSTSTORE_FILENAME, Files.exists(serverTrustStore));
126130
System.setProperty(TRUSTSTORE_PROPERTY, serverTrustStore.toString());
127131
System.setProperty(TRUSTSTORE_PASSWORD_PROPERTY, TRUSTSTORE_PASSWORD);
@@ -144,8 +148,26 @@ public static void setup() throws IOException {
144148
}
145149
}
146150

147-
private static void createClientKey(Path tempDirectory) throws IOException {
148-
runCommand(tempDirectory, List.of("keytool", "-genkey",
151+
@AfterClass
152+
public static void teardown() throws IOException {
153+
if (tempDirectory != null) {
154+
delete(tempDirectory);
155+
}
156+
}
157+
158+
private static void delete(Path file) throws IOException {
159+
if (Files.isDirectory(file)) {
160+
try (DirectoryStream<Path> children = Files.newDirectoryStream(file)) {
161+
for (Path child : children) {
162+
delete(child);
163+
}
164+
}
165+
}
166+
Files.deleteIfExists(file);
167+
}
168+
169+
private static void createClientKey() throws IOException {
170+
runCommand(List.of("keytool", "-genkey",
149171
"-keystore", KEYSTORE_FILENAME,
150172
"-alias", "clientkey",
151173
"-storepass", KEYSTORE_PASSWORD,
@@ -155,23 +177,23 @@ private static void createClientKey(Path tempDirectory) throws IOException {
155177
"-keyalg", "rsa"));
156178
}
157179

158-
private static void createClientCert(Path tempDirectory) throws IOException {
159-
runCommand(tempDirectory, List.of("keytool", "-exportcert",
180+
private static void createClientCert() throws IOException {
181+
runCommand(List.of("keytool", "-exportcert",
160182
"-keystore", KEYSTORE_FILENAME,
161183
"-alias", "clientkey",
162184
"-storepass", KEYSTORE_PASSWORD,
163185
"-file", "client.cer"));
164186
}
165187

166-
private static void createServerTrustStore(Path tempDirectory) throws IOException {
167-
runCommand(tempDirectory, List.of("keytool", "-importcert",
188+
private static void createServerTrustStore() throws IOException {
189+
runCommand(List.of("keytool", "-importcert",
168190
"-noprompt",
169191
"-file", "client.cer",
170192
"-keystore", TRUSTSTORE_FILENAME,
171193
"-storepass", TRUSTSTORE_PASSWORD));
172194
}
173195

174-
private static void runCommand(Path tempDirectory, List<String> command) throws IOException {
196+
private static void runCommand(List<String> command) throws IOException {
175197
ProcessBuilder pb = new ProcessBuilder().command(command);
176198
pb.directory(tempDirectory.toFile());
177199
final Process process = pb.start();

0 commit comments

Comments
 (0)