From 22db2fdebce7b9d300a1be8ac50d078fe01ac0c8 Mon Sep 17 00:00:00 2001 From: Stefan Birkner Date: Wed, 30 Sep 2020 22:30:24 +0200 Subject: [PATCH 1/2] Use JUnit's TemporaryFolder rule Don't reinvent the wheel. TemporaryFolder already takes care of cleaning up the created files. --- .../sourceforge/pmd/ConfigurationTest.java | 11 +++-- .../pmd/renderers/XMLRendererTest.java | 15 ++++--- .../pmd/renderers/YAHTMLRendererTest.java | 42 ++++--------------- .../pmd/util/database/DBTypeTest.java | 8 +++- .../it/AbstractBinaryDistributionTest.java | 18 +++----- .../net/sourceforge/pmd/it/AllRulesIT.java | 4 +- .../pmd/it/BinaryDistributionIT.java | 6 +-- .../net/sourceforge/pmd/it/PMDExecutor.java | 11 ++--- .../pmd/docs/RuleDocGeneratorTest.java | 28 +++---------- .../pmd/coverage/PMDCoverageTest.java | 11 +++-- 10 files changed, 55 insertions(+), 99 deletions(-) diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/ConfigurationTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/ConfigurationTest.java index 7dd248cf48..480bdb24a1 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/ConfigurationTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/ConfigurationTest.java @@ -19,7 +19,9 @@ import java.nio.charset.StandardCharsets; import java.util.Properties; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import net.sourceforge.pmd.cache.FileAnalysisCache; import net.sourceforge.pmd.cache.NoopAnalysisCache; @@ -29,6 +31,9 @@ import net.sourceforge.pmd.util.ClasspathClassLoader; public class ConfigurationTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + @Test public void testSuppressMarker() { PMDConfiguration configuration = new PMDConfiguration(); @@ -227,8 +232,7 @@ public class ConfigurationTest { configuration.setAnalysisCache(null); assertNotNull("Default cache was set to null", configuration.getAnalysisCache()); - final File cacheFile = File.createTempFile("pmd-", ".cache"); - cacheFile.deleteOnExit(); + final File cacheFile = folder.newFile(); final FileAnalysisCache analysisCache = new FileAnalysisCache(cacheFile); configuration.setAnalysisCache(analysisCache); assertSame("Configured cache not stored", analysisCache, configuration.getAnalysisCache()); @@ -254,8 +258,7 @@ public class ConfigurationTest { final PMDConfiguration configuration = new PMDConfiguration(); // set dummy cache location - final File cacheFile = File.createTempFile("pmd-", ".cache"); - cacheFile.deleteOnExit(); + final File cacheFile = folder.newFile(); final FileAnalysisCache analysisCache = new FileAnalysisCache(cacheFile); configuration.setAnalysisCache(analysisCache); assertNotNull("Null cache location accepted", configuration.getAnalysisCache()); diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/renderers/XMLRendererTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/renderers/XMLRendererTest.java index 186a6699ab..e72e44561f 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/renderers/XMLRendererTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/renderers/XMLRendererTest.java @@ -10,8 +10,6 @@ import java.io.IOException; import java.io.StringReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.io.IOUtils; @@ -19,6 +17,7 @@ import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.RestoreSystemProperties; +import org.junit.rules.TemporaryFolder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; @@ -39,6 +38,9 @@ public class XMLRendererTest extends AbstractRendererTest { @Rule // Restores system properties after test public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties(); + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + @Override public Renderer getRenderer() { return new XMLRenderer(); @@ -160,19 +162,16 @@ public class XMLRendererTest extends AbstractRendererTest { } private String renderTempFile(Renderer renderer, Report report, Charset expectedCharset) throws IOException { - Path tempFile = Files.createTempFile("pmd-report-test", null); - String absolutePath = tempFile.toAbsolutePath().toString(); + File reportFile = folder.newFile(); - renderer.setReportFile(absolutePath); + renderer.setReportFile(reportFile.getAbsolutePath()); renderer.start(); renderer.renderFileReport(report); renderer.end(); renderer.flush(); - try (FileInputStream input = new FileInputStream(absolutePath)) { + try (FileInputStream input = new FileInputStream(reportFile)) { return IOUtils.toString(input, expectedCharset); - } finally { - Files.delete(tempFile); } } } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/renderers/YAHTMLRendererTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/renderers/YAHTMLRendererTest.java index 0d7f68341a..30dadb9359 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/renderers/YAHTMLRendererTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/renderers/YAHTMLRendererTest.java @@ -15,9 +15,10 @@ import java.util.Arrays; import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; -import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import net.sourceforge.pmd.FooRule; import net.sourceforge.pmd.PMD; @@ -33,39 +34,14 @@ import net.sourceforge.pmd.lang.rule.ParametricRuleViolation; public class YAHTMLRendererTest extends AbstractRendererTest { - private String outputDir; + private File outputDir; + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); @Before public void setUp() throws IOException { - outputDir = getTemporaryDirectory("pmdtest").getAbsolutePath(); - } - - @After - public void cleanUp() { - deleteDirectory(new File(outputDir)); - } - - private File getTemporaryDirectory(String prefix) throws IOException { - // TODO: move to util class? - File dir = File.createTempFile(prefix, ""); - dir.delete(); - dir.mkdir(); - return dir; - } - - private void deleteDirectory(File dir) { - // TODO: move to util class? - File[] a = dir.listFiles(); - if (a != null) { - for (File f : a) { - if (f.isDirectory()) { - deleteDirectory(f); - } else { - f.delete(); - } - } - } - dir.delete(); + outputDir = folder.newFolder("pmdtest"); } private RuleViolation newRuleViolation(int endColumn, final String packageNameArg, final String classNameArg) { @@ -94,7 +70,7 @@ public class YAHTMLRendererTest extends AbstractRendererTest { String actual = ReportTest.render(getRenderer(), report); assertEquals(filter(getExpected()), filter(actual)); - String[] htmlFiles = new File(outputDir).list(); + String[] htmlFiles = outputDir.list(); assertEquals(3, htmlFiles.length); Arrays.sort(htmlFiles); assertEquals("YAHTMLSampleClass1.html", htmlFiles[0]); @@ -120,7 +96,7 @@ public class YAHTMLRendererTest extends AbstractRendererTest { @Override public Renderer getRenderer() { Renderer result = new YAHTMLRenderer(); - result.setProperty(YAHTMLRenderer.OUTPUT_DIR, outputDir); + result.setProperty(YAHTMLRenderer.OUTPUT_DIR, outputDir.getAbsolutePath()); return result; } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/util/database/DBTypeTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/util/database/DBTypeTest.java index 2bdd29465f..bba684648e 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/util/database/DBTypeTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/util/database/DBTypeTest.java @@ -14,7 +14,9 @@ import java.util.ResourceBundle; import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * @@ -27,6 +29,9 @@ public class DBTypeTest { private Properties testProperties; private Properties includeProperties; + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + @Before public void setUp() throws Exception { testProperties = new Properties(); @@ -38,7 +43,7 @@ public class DBTypeTest { includeProperties.putAll(testProperties); includeProperties.put("prop3", "include3"); - absoluteFile = File.createTempFile("dbtypetest", ".properties"); + absoluteFile = folder.newFile(); try (FileOutputStream fileOutputStream = new FileOutputStream(absoluteFile); PrintStream printStream = new PrintStream(fileOutputStream)) { for (Entry entry : testProperties.entrySet()) { @@ -50,7 +55,6 @@ public class DBTypeTest { @After public void tearDown() throws Exception { testProperties = null; - absoluteFile.delete(); } /** diff --git a/pmd-dist/src/test/java/net/sourceforge/pmd/it/AbstractBinaryDistributionTest.java b/pmd-dist/src/test/java/net/sourceforge/pmd/it/AbstractBinaryDistributionTest.java index 692d40378a..2822d2867a 100644 --- a/pmd-dist/src/test/java/net/sourceforge/pmd/it/AbstractBinaryDistributionTest.java +++ b/pmd-dist/src/test/java/net/sourceforge/pmd/it/AbstractBinaryDistributionTest.java @@ -5,13 +5,11 @@ package net.sourceforge.pmd.it; import java.io.File; -import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; -import org.apache.commons.io.FileUtils; -import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.rules.TemporaryFolder; import net.sourceforge.pmd.PMDVersion; @@ -21,6 +19,9 @@ public abstract class AbstractBinaryDistributionTest { return new File(".", "target/pmd-bin-" + PMDVersion.VERSION + ".zip"); } + @ClassRule + public static TemporaryFolder folder = new TemporaryFolder(); + /** * The temporary directory, to which the binary distribution will be extracted. * It will be deleted again after the test. @@ -29,16 +30,9 @@ public abstract class AbstractBinaryDistributionTest { @BeforeClass public static void setupTempDirectory() throws Exception { - tempDir = Files.createTempDirectory("pmd-it-test-"); + tempDir = folder.newFolder().toPath(); if (getBinaryDistribution().exists()) { ZipFileExtractor.extractZipFile(getBinaryDistribution().toPath(), tempDir); } } - - @AfterClass - public static void cleanupTempDirectory() throws IOException { - if (tempDir != null && tempDir.toFile().exists()) { - FileUtils.forceDelete(tempDir.toFile()); - } - } } diff --git a/pmd-dist/src/test/java/net/sourceforge/pmd/it/AllRulesIT.java b/pmd-dist/src/test/java/net/sourceforge/pmd/it/AllRulesIT.java index 9249c3ca3b..91b96df299 100644 --- a/pmd-dist/src/test/java/net/sourceforge/pmd/it/AllRulesIT.java +++ b/pmd-dist/src/test/java/net/sourceforge/pmd/it/AllRulesIT.java @@ -35,8 +35,8 @@ public class AllRulesIT extends AbstractBinaryDistributionTest { public void runRuleTests() throws Exception { String srcDir = new File(".", "src/test/resources/sample-source/" + language + "/").getAbsolutePath(); - ExecutionResult result = PMDExecutor.runPMDRules(tempDir, srcDir, "src/test/resources/rulesets/all-" - + language + ".xml"); + ExecutionResult result = PMDExecutor.runPMDRules(folder.newFile().toPath(), tempDir, srcDir, + "src/test/resources/rulesets/all-" + language + ".xml"); assertDefaultExecutionResult(result); } diff --git a/pmd-dist/src/test/java/net/sourceforge/pmd/it/BinaryDistributionIT.java b/pmd-dist/src/test/java/net/sourceforge/pmd/it/BinaryDistributionIT.java index 3910ef4a5c..755e2085b1 100644 --- a/pmd-dist/src/test/java/net/sourceforge/pmd/it/BinaryDistributionIT.java +++ b/pmd-dist/src/test/java/net/sourceforge/pmd/it/BinaryDistributionIT.java @@ -84,15 +84,15 @@ public class BinaryDistributionIT extends AbstractBinaryDistributionTest { result = PMDExecutor.runPMD(tempDir, "-h"); result.assertExecutionResult(0, SUPPORTED_LANGUAGES_PMD); - result = PMDExecutor.runPMDRules(tempDir, srcDir, "src/test/resources/rulesets/sample-ruleset.xml"); + result = PMDExecutor.runPMDRules(folder.newFile().toPath(), tempDir, srcDir, "src/test/resources/rulesets/sample-ruleset.xml"); result.assertExecutionResult(4, "", "JumbledIncrementer.java:8:"); // also test XML format - result = PMDExecutor.runPMDRules(tempDir, srcDir, "src/test/resources/rulesets/sample-ruleset.xml", "xml"); + result = PMDExecutor.runPMDRules(folder.newFile().toPath(), tempDir, srcDir, "src/test/resources/rulesets/sample-ruleset.xml", "xml"); result.assertExecutionResult(4, "", "JumbledIncrementer.java\">"); result.assertExecutionResult(4, "", " mockedSidebar = Arrays.asList( "entries:", @@ -51,23 +52,6 @@ public class RuleDocGeneratorTest { Files.write(root.resolve("docs/_data/sidebars/pmd_sidebar.yml"), mockedSidebar); } - @After - public void cleanup() throws IOException { - Files.walkFileTree(root, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } - private static String loadResource(String name) throws IOException { return MockedFileWriter.normalizeLineSeparators( IOUtils.toString(RuleDocGeneratorTest.class.getResourceAsStream(name), StandardCharsets.UTF_8)); diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/coverage/PMDCoverageTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/coverage/PMDCoverageTest.java index 258dcecba6..71609c599d 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/coverage/PMDCoverageTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/coverage/PMDCoverageTest.java @@ -18,6 +18,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.StandardErrorStreamLog; import org.junit.contrib.java.lang.system.StandardOutputStreamLog; +import org.junit.rules.TemporaryFolder; import net.sourceforge.pmd.PMD; @@ -29,6 +30,9 @@ public class PMDCoverageTest { @Rule public StandardErrorStreamLog errorStream = new StandardErrorStreamLog(); + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + /** * Test some of the PMD command line options */ @@ -46,9 +50,8 @@ public class PMDCoverageTest { String[] args; args = commandLine.split("\\s"); - File f = null; try { - f = File.createTempFile("pmd", ".txt"); + File f = folder.newFile(); int n = args.length; String[] a = new String[n + 2 + 2]; System.arraycopy(args, 0, a, 0, n); @@ -74,10 +77,6 @@ public class PMDCoverageTest { assertEquals("No parsing error expected", 0, StringUtils.countMatches(report, "Error while parsing")); } catch (IOException ioe) { fail("Problem creating temporary file: " + ioe.getLocalizedMessage()); - } finally { - if (f != null) { - f.delete(); - } } } From 75589de2dd7143a93468dcf2f5b5235636802710 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 10 Oct 2020 14:00:56 +0200 Subject: [PATCH 2/2] [doc] Update release notes, refs #2813 --- docs/pages/release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index ecf46b450d..4f38ebad5b 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -22,6 +22,7 @@ This is a {{ site.pmd.release_type }} release. * [#2809](https://github.com/pmd/pmd/pull/2809): \[java] Move test config from file to test class - [Stefan Birkner](https://github.com/stefanbirkner) * [#2810](https://github.com/pmd/pmd/pull/2810): \[core] Move method "renderTempFile" to XMLRendererTest - [Stefan Birkner](https://github.com/stefanbirkner) +* [#2813](https://github.com/pmd/pmd/pull/2813): \[core] Use JUnit's TemporaryFolder rule - [Stefan Birkner](https://github.com/stefanbirkner) {% endtocmaker %}