Fix file cleanup of sandbox unit tests on Windows machines.

Use try with resources to close stream after file copy.
This commit is contained in:
Matthew Nesbit 2016-11-30 12:08:42 +00:00
parent e873347892
commit 8f47d393ca

View File

@ -14,26 +14,30 @@ import static org.junit.Assert.*;
public class TestUtils { public class TestUtils {
private static ArrayList<FileSystem> tmpFileSystems = new ArrayList<>();
private static Path jarFSDir = null; private static Path jarFSDir = null;
private static Path tmpdir; private static Path tmpdir;
public static void setPathToTmpJar(final String resourcePathToJar) throws IOException { public static void setPathToTmpJar(final String resourcePathToJar) throws IOException {
// Copy resource jar to tmp dir // Copy resource jar to tmp dir
tmpdir = Files.createTempDirectory(Paths.get("/tmp"), "wlcl-tmp-test"); tmpdir = Files.createTempDirectory("wlcl-tmp-test");
final InputStream in = TestUtils.class.getResourceAsStream(resourcePathToJar);
Path copiedJar = tmpdir.resolve("tmp-resource.jar"); Path copiedJar = tmpdir.resolve("tmp-resource.jar");
Files.copy(in, copiedJar, StandardCopyOption.REPLACE_EXISTING); try(final InputStream in = TestUtils.class.getResourceAsStream(resourcePathToJar)) {
Files.copy(in, copiedJar, StandardCopyOption.REPLACE_EXISTING);
}
final FileSystem fs = FileSystems.newFileSystem(copiedJar, null); final FileSystem fs = FileSystems.newFileSystem(copiedJar, null);
tmpFileSystems.add(fs);
jarFSDir = fs.getRootDirectories().iterator().next(); jarFSDir = fs.getRootDirectories().iterator().next();
} }
public static Path copySandboxJarToTmpDir(final String resourcePathToJar) throws IOException { public static Path copySandboxJarToTmpDir(final String resourcePathToJar) throws IOException {
final InputStream in = TestUtils.class.getResourceAsStream(resourcePathToJar);
Path sandboxJar = tmpdir.resolve("tmp-sandbox.jar");
Files.copy(in, sandboxJar, StandardCopyOption.REPLACE_EXISTING);
final FileSystem sandboxFs = FileSystems.newFileSystem(sandboxJar, null);
Path sandboxJar = tmpdir.resolve("tmp-sandbox.jar");
try(final InputStream in = TestUtils.class.getResourceAsStream(resourcePathToJar)) {
Files.copy(in, sandboxJar, StandardCopyOption.REPLACE_EXISTING);
}
final FileSystem sandboxFs = FileSystems.newFileSystem(sandboxJar, null);
tmpFileSystems.add(sandboxFs);
return sandboxFs.getRootDirectories().iterator().next(); return sandboxFs.getRootDirectories().iterator().next();
} }
@ -42,7 +46,13 @@ public class TestUtils {
} }
public static void cleanupTmpJar() throws IOException { public static void cleanupTmpJar() throws IOException {
for (FileSystem fs: tmpFileSystems) {
fs.close();
}
tmpFileSystems.clear();
jarFSDir = null;
Files.walkFileTree(tmpdir, new Reaper()); Files.walkFileTree(tmpdir, new Reaper());
tmpdir = null;
} }
public static void checkAllCosts(final int allocCost, final int jumpCost, final int invokeCost, final int throwCost) { public static void checkAllCosts(final int allocCost, final int jumpCost, final int invokeCost, final int throwCost) {
@ -56,7 +66,7 @@ public class TestUtils {
byte[] basic = getBytes(classFName); byte[] basic = getBytes(classFName);
assertEquals(originalLength, basic.length); assertEquals(originalLength, basic.length);
final byte[] tfmd = instrumentWithCosts(basic, new HashSet<>()); final byte[] tfmd = instrumentWithCosts(basic, new HashSet<>());
final Path testdir = Files.createTempDirectory(Paths.get("/tmp"), "greymalkin-test-"); final Path testdir = Files.createTempDirectory("greymalkin-test-");
final Path out = testdir.resolve(classFName); final Path out = testdir.resolve(classFName);
Files.createDirectories(out.getParent()); Files.createDirectories(out.getParent());
Files.write(out, tfmd); Files.write(out, tfmd);