Skip to content

Commit

Permalink
Let JUnit 5 manage tests' temporary files
Browse files Browse the repository at this point in the history
This change implements [a suggestion previously made by
@msridhar](wala#1301 (comment)),
not just in the class originally under review, but in several other test
(and test fixture) classes as well.
  • Loading branch information
liblit committed Aug 5, 2023
1 parent 1138313 commit c7cb8ba
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public abstract class JavaIRTests extends IRTests {

Expand Down Expand Up @@ -790,9 +791,11 @@ public void testVarDeclInSwitch() throws IllegalArgumentException, CancelExcepti
}

@Test
public void testExclusions() throws IllegalArgumentException, CancelException, IOException {
public void testExclusions(@TempDir final File tmpDir)
throws IllegalArgumentException, CancelException, IOException {
File exclusions =
TemporaryFile.stringToFile(File.createTempFile("exl", "txt"), "Exclusions.Excluded\n");
TemporaryFile.stringToFile(
File.createTempFile("exl", "txt", tmpDir), "Exclusions.Excluded\n");
Pair<CallGraph, CallGraphBuilder<? super InstanceKey>> x =
runTest(
singleTestSrc(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Map;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public abstract class TestCorrelatedPairExtraction {
// set to "true" to use JUnit's assertEquals to check whether a test case passed;
Expand All @@ -42,14 +43,15 @@ public abstract class TestCorrelatedPairExtraction {
// this is useful if the outputs are too big/too different for Eclipse's diff view to handle
private static final boolean ASSERT_EQUALS = true;

@TempDir private File tmpDir;

public void testRewriter(String in, String out) {
testRewriter(null, in, out);
}

public void testRewriter(String testName, String in, String out) {
File tmp = null;
try {
tmp = File.createTempFile("test", ".js");
final var tmp = File.createTempFile("test", ".js", tmpDir);
FileUtil.writeFile(tmp, in);

final Map<IMethod, CorrelationSummary> summaries =
Expand Down Expand Up @@ -86,8 +88,6 @@ public ExtractionPolicy createPolicy(CAstEntity entity) {

} catch (IOException | ClassHierarchyException e) {
e.printStackTrace();
} finally {
if (tmp != null && tmp.exists()) tmp.delete();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import java.util.regex.Pattern;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public abstract class TestForInBodyExtraction {

@TempDir private File tmpDir;

public void testRewriter(String in, String out) {
testRewriter(null, in, out);
}
Expand All @@ -49,11 +53,10 @@ public static String eraseGeneratedNames(String str) {
}

public void testRewriter(String testName, String in, String out) {
File tmp = null;
String expected = null;
String actual = null;
try {
tmp = File.createTempFile("test", ".js");
final var tmp = File.createTempFile("test", ".js", tmpDir);
FileUtil.writeFile(tmp, in);
CAstImpl ast = new CAstImpl();
actual =
Expand All @@ -70,8 +73,6 @@ public void testRewriter(String testName, String in, String out) {
assertEquals(expected, actual, () -> "Comparison Failure in " + testName + '!');
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
if (tmp != null && tmp.exists()) tmp.delete();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@
import com.ibm.wala.util.io.TemporaryFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.jar.JarFile;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class Java7CallGraphTest extends DynamicCallGraphTestBase {

private @TempDir Path temporaryDirectory;

@Override
protected Path getTemporaryDirectory() {
return temporaryDirectory;
}

@Test
public void testOcamlHelloHash()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.MonitorUtil.IProgressMonitor;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Set;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

@Tag("slow")
public class KawaCallGraphTest extends DynamicCallGraphTestBase {

private @TempDir Path temporaryDirectory;

@Override
protected Path getTemporaryDirectory() {
return temporaryDirectory;
}

@Test
public void testKawaChess()
throws ClassHierarchyException, IllegalArgumentException, IOException, SecurityException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

@SuppressWarnings("UnconstructableJUnitTestCase")
public class FloatingPointsTest extends WalaTestCase {
Expand All @@ -38,10 +39,9 @@ public class FloatingPointsTest extends WalaTestCase {
public FloatingPointsTest() {}

@BeforeEach
public void setOfflineInstrumenter() throws IOException {
public void setOfflineInstrumenter(@TempDir final Path tmpDir) throws IOException {
// Create a temporary file for the shrike instrumented output
instrumentedJarLocation = Files.createTempFile("wala-test", ".jar");
instrumentedJarLocation.toFile().deleteOnExit();
instrumentedJarLocation = Files.createTempFile(tmpDir, "wala-test", ".jar");

// Initialize the OfflineInstrumenter loading the class file specified in
// 'klass' above
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,25 @@
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Path;
import org.junit.jupiter.api.BeforeEach;

public abstract class DynamicCallGraphTestBase extends WalaTestCase {

protected boolean testPatchCalls = false;

private boolean instrumentedJarBuilt = false;

private final java.nio.file.Path instrumentedJarLocation;
private java.nio.file.Path instrumentedJarLocation;

private final java.nio.file.Path cgLocation;
private java.nio.file.Path cgLocation;

protected DynamicCallGraphTestBase() {
try {
instrumentedJarLocation = Files.createTempFile("wala-test", ".jar");
instrumentedJarLocation.toFile().deleteOnExit();
cgLocation = Files.createTempFile("cg", ".txt");
cgLocation.toFile().deleteOnExit();
} catch (IOException problem) {
throw new RuntimeException(problem);
}
protected abstract java.nio.file.Path getTemporaryDirectory();

@BeforeEach
protected void createTemporaryFiles() throws IOException {
final var temporaryDirectory = getTemporaryDirectory();
instrumentedJarLocation = Files.createTempFile(temporaryDirectory, "wala-test", ".jar");
cgLocation = Files.createTempFile(temporaryDirectory, "cg", ".txt");
}

protected void instrument(String testJarLocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,24 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.jar.JarFile;
import org.junit.jupiter.api.io.TempDir;

public class DalvikCallGraphTestBase extends DynamicCallGraphTestBase {

private @TempDir Path temporaryDirectory;

@Override
protected Path getTemporaryDirectory() {
return temporaryDirectory;
}

protected static <T> Set<T> processCG(
CallGraph cg, Predicate<CGNode> filter, Function<CGNode, T> map) {
Set<T> result = HashSetFactory.make();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@
import com.ibm.wala.util.CancelException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class DynamicDalvikComparisonJavaLibsTest extends DynamicDalvikComparisonTest {

private @TempDir Path temporaryDirectory;

@Override
protected Path getTemporaryDirectory() {
return temporaryDirectory;
}

@Test
public void testJLex()
throws ClassHierarchyException, IllegalArgumentException, IOException, CancelException,
Expand Down

0 comments on commit c7cb8ba

Please sign in to comment.