From 789e4aa73004fc4e25ebf0bc2db8a3d89d510d10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 12 Dec 2020 19:31:00 +0100 Subject: [PATCH] Fix tests --- .../pmd/lang/apex/DefaultRulesetTest.java | 7 +++--- .../main/java/net/sourceforge/pmd/PMD.java | 3 ++- .../sourceforge/pmd/RuleSetReferenceId.java | 17 ++++++++++---- .../sourceforge/pmd/util/ResourceLoader.java | 23 +++++++++++-------- .../pmd/docs/RuleDocGeneratorTest.java | 2 +- .../pmd/docs/RuleSetResolverTest.java | 13 +++++------ .../java/net/sourceforge/pmd/cli/CLITest.java | 13 +++++------ .../pmd/AbstractLanguageVersionTest.java | 7 ++++-- .../pmd/AbstractRuleSetFactoryTest.java | 3 +++ 9 files changed, 52 insertions(+), 36 deletions(-) diff --git a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/DefaultRulesetTest.java b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/DefaultRulesetTest.java index 45850e4649..617dd1bd78 100644 --- a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/DefaultRulesetTest.java +++ b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/DefaultRulesetTest.java @@ -15,7 +15,6 @@ import org.junit.Test; import org.junit.contrib.java.lang.system.SystemErrRule; import net.sourceforge.pmd.RuleSet; -import net.sourceforge.pmd.RuleSetFactory; import net.sourceforge.pmd.RuleSetLoader; public class DefaultRulesetTest { @@ -30,15 +29,15 @@ public class DefaultRulesetTest { @After public void cleanup() { - Handler[] handlers = Logger.getLogger(RuleSetFactory.class.getName()).getHandlers(); + Handler[] handlers = Logger.getLogger(RuleSetLoader.class.getName()).getHandlers(); for (Handler handler : handlers) { - Logger.getLogger(RuleSetFactory.class.getName()).removeHandler(handler); + Logger.getLogger(RuleSetLoader.class.getName()).removeHandler(handler); } } @Test public void loadQuickstartRuleset() { - Logger.getLogger(RuleSetFactory.class.getName()).addHandler(new Handler() { + Logger.getLogger(RuleSetLoader.class.getName()).addHandler(new Handler() { @Override public void publish(LogRecord record) { Assert.fail("No Logging expected: " + record.getMessage()); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java index e514ea24cc..7c73b3f194 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java @@ -278,7 +278,8 @@ public class PMD { ruleSets = factory.loadFromResources(rulesetPaths); printRuleNamesInDebug(ruleSets); if (isEmpty(ruleSets)) { - String msg = "No rules found. Maybe you misspelled a rule name? (" + rulesetPaths + ')'; + String msg = "No rules found. Maybe you misspelled a rule name? (" + + String.join(",", rulesetPaths) + ')'; LOG.log(Level.SEVERE, msg); throw new IllegalArgumentException(msg); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetReferenceId.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetReferenceId.java index 3f9b2e1963..d7ade89948 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetReferenceId.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetReferenceId.java @@ -402,16 +402,25 @@ public class RuleSetReferenceId { public InputStream getInputStream(final ResourceLoader rl) throws IOException { if (externalRuleSetReferenceId == null) { if (StringUtils.isBlank(ruleSetFileName)) { - throw new FileNotFoundException("Can't find resource '" + ruleSetFileName + "' for rule '" + ruleName - + "'" + ". Make sure the resource is a valid file or URL and is on the CLASSPATH. " - + "Here's the current classpath: " + System.getProperty("java.class.path")); + throw notFoundException(); + } + try { + return rl.loadResourceAsStream(ruleSetFileName); + } catch (FileNotFoundException ignored) { + throw notFoundException(); } - return rl.loadResourceAsStream(ruleSetFileName); } else { return externalRuleSetReferenceId.getInputStream(rl); } } + private FileNotFoundException notFoundException() { + return new FileNotFoundException("Can't find resource '" + ruleSetFileName + "' for rule '" + ruleName + + "'" + ". Make sure the resource is a valid file or URL and is on the classpath. " + + "Here's the current classpath: " + + System.getProperty("java.class.path")); + } + /** * Return the String form of this Rule reference. * diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/ResourceLoader.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/ResourceLoader.java index 68c2776da5..d9d4d7e5b0 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/ResourceLoader.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/ResourceLoader.java @@ -14,6 +14,9 @@ import java.net.URLConnection; import java.nio.file.Files; import java.util.Objects; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.annotation.InternalApi; @@ -61,7 +64,7 @@ public class ResourceLoader { * * @return InputStream */ - public InputStream loadResourceAsStream(final String name) throws IOException { + public @NonNull InputStream loadResourceAsStream(final String name) throws IOException { // Search file locations first final File file = new File(name); if (file.exists()) { @@ -79,18 +82,18 @@ public class ResourceLoader { final HttpURLConnection connection = (HttpURLConnection) new URL(name).openConnection(); connection.setConnectTimeout(TIMEOUT); connection.setReadTimeout(TIMEOUT); - return connection.getInputStream(); - } catch (final Exception e) { - try { - return loadClassPathResourceAsStream(name); - } catch (final IOException ioe) { - throw new IOException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the CLASSPATH", ioe); + InputStream is = connection.getInputStream(); + if (is != null) { + return is; } + } catch (final Exception e) { + return loadClassPathResourceAsStreamOrThrow(name); } + throw new IOException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the classpath"); } - public InputStream loadClassPathResourceAsStream(final String name) throws IOException { + public @Nullable InputStream loadClassPathResourceAsStream(final String name) throws IOException { /* * Don't use getResourceAsStream to avoid reusing connections between threads * See https://github.com/pmd/pmd/issues/234 @@ -109,7 +112,7 @@ public class ResourceLoader { } } - public InputStream loadClassPathResourceAsStreamOrThrow(final String name) throws IOException { + public @NonNull InputStream loadClassPathResourceAsStreamOrThrow(final String name) throws IOException { InputStream is = null; try { is = loadClassPathResourceAsStream(name); @@ -119,7 +122,7 @@ public class ResourceLoader { if (is == null) { throw new FileNotFoundException("Can't find resource " + name - + ". Make sure the resource is on the CLASSPATH"); + + ". Make sure the resource is on the classpath"); } return is; diff --git a/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleDocGeneratorTest.java b/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleDocGeneratorTest.java index 7d087032ae..23114c6e53 100644 --- a/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleDocGeneratorTest.java +++ b/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleDocGeneratorTest.java @@ -59,7 +59,7 @@ public class RuleDocGeneratorTest { public void testSingleRuleset() throws IOException { RuleDocGenerator generator = new RuleDocGenerator(writer, root); - RuleSetLoader rsf = new RuleSetLoader(); + RuleSetLoader rsf = new RuleSetLoader().includeDeprecatedRuleReferences(true); RuleSet ruleset = rsf.loadFromResource("rulesets/ruledoctest/sample.xml"); generator.generate(Arrays.asList(ruleset), diff --git a/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleSetResolverTest.java b/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleSetResolverTest.java index f5bc92714c..8b82683953 100644 --- a/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleSetResolverTest.java +++ b/pmd-doc/src/test/java/net/sourceforge/pmd/docs/RuleSetResolverTest.java @@ -4,9 +4,10 @@ package net.sourceforge.pmd.docs; +import static net.sourceforge.pmd.util.CollectionUtil.listOf; + import java.nio.file.FileSystems; import java.nio.file.Path; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -17,11 +18,9 @@ import net.sourceforge.pmd.RuleSetLoader; public class RuleSetResolverTest { - private static final List excludedRulesets = new ArrayList<>(); - - static { - excludedRulesets.add(FilenameUtils.normalize("pmd-test/src/main/resources/rulesets/dummy/basic.xml")); - } + private static final List EXCLUDED_RULESETS = listOf( + FilenameUtils.normalize("pmd-test/src/main/resources/rulesets/dummy/basic.xml") + ); @Test public void resolveAllRulesets() { @@ -39,7 +38,7 @@ public class RuleSetResolverTest { Iterator it = additionalRulesets.iterator(); while (it.hasNext()) { String filename = it.next(); - for (String exclusion : excludedRulesets) { + for (String exclusion : EXCLUDED_RULESETS) { if (filename.endsWith(exclusion)) { it.remove(); break; diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java b/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java index 3e82902345..2fc217381d 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java @@ -7,7 +7,6 @@ package net.sourceforge.pmd.cli; import static org.junit.Assert.assertTrue; import java.io.File; -import java.io.IOException; import java.util.regex.Pattern; import org.junit.Assert; @@ -39,7 +38,7 @@ public class CLITest extends BaseCLITest { } @Test - public void changeJavaVersion() throws IOException { + public void changeJavaVersion() { String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-version", "1.5", "-language", "java", "-debug", }; String resultFilename = runTest(args, "chgJavaVersion"); @@ -54,14 +53,14 @@ public class CLITest extends BaseCLITest { } @Test - public void exitStatusWithViolations() throws IOException { + public void exitStatusWithViolations() { String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", }; String resultFilename = runTest(args, "exitStatusWithViolations", 4); assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if")); } @Test - public void exitStatusWithViolationsAndWithoutFailOnViolations() throws IOException { + public void exitStatusWithViolationsAndWithoutFailOnViolations() { String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", "-failOnViolation", "false", }; String resultFilename = runTest(args, "exitStatusWithViolationsAndWithoutFailOnViolations", 0); assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if")); @@ -71,7 +70,7 @@ public class CLITest extends BaseCLITest { * See https://sourceforge.net/p/pmd/bugs/1231/ */ @Test - public void testWrongRuleset() throws Exception { + public void testWrongRuleset() { String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/designn.xml", }; String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt"; createTestOutputFile(filename); @@ -85,7 +84,7 @@ public class CLITest extends BaseCLITest { * See https://sourceforge.net/p/pmd/bugs/1231/ */ @Test - public void testWrongRulesetWithRulename() throws Exception { + public void testWrongRulesetWithRulename() { String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/designn.xml/UseCollectionIsEmpty", }; String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt"; createTestOutputFile(filename); @@ -99,7 +98,7 @@ public class CLITest extends BaseCLITest { * See https://sourceforge.net/p/pmd/bugs/1231/ */ @Test - public void testWrongRulename() throws Exception { + public void testWrongRulename() { String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml/ThisRuleDoesNotExist", }; String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt"; createTestOutputFile(filename); diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/AbstractLanguageVersionTest.java b/pmd-test/src/main/java/net/sourceforge/pmd/AbstractLanguageVersionTest.java index 8834636264..41a01c7833 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/AbstractLanguageVersionTest.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/AbstractLanguageVersionTest.java @@ -101,8 +101,11 @@ public class AbstractLanguageVersionTest { } Properties props = new Properties(); - String rulesetsProperties = "category/" + simpleTerseName + "/categories.properties"; + String rulesetsProperties = "/category/" + simpleTerseName + "/categories.properties"; try (InputStream inputStream = getClass().getResourceAsStream(rulesetsProperties)) { + if (inputStream == null) { + throw new IOException(); + } props.load(inputStream); } assertRulesetsAndCategoriesProperties(props); @@ -122,7 +125,7 @@ public class AbstractLanguageVersionTest { } Properties props = new Properties(); - String rulesetsProperties = "rulesets/" + simpleTerseName + "/rulesets.properties"; + String rulesetsProperties = "/rulesets/" + simpleTerseName + "/rulesets.properties"; InputStream inputStream = getClass().getResourceAsStream(rulesetsProperties); if (inputStream != null) { // rulesets.properties file exists diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/AbstractRuleSetFactoryTest.java b/pmd-test/src/main/java/net/sourceforge/pmd/AbstractRuleSetFactoryTest.java index 1e5761a3cf..b262bbcf46 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/AbstractRuleSetFactoryTest.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/AbstractRuleSetFactoryTest.java @@ -18,6 +18,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -285,11 +286,13 @@ public abstract class AbstractRuleSetFactoryTest { private List getRuleSetFileNames(String language) throws IOException { List ruleSetFileNames = new ArrayList<>(); Properties properties = new Properties(); + @SuppressWarnings("PMD.CloseResource") InputStream input = getClass().getResourceAsStream("rulesets/" + language + "/rulesets.properties"); if (input == null) { // this might happen if a language is only support by CPD, but not // by PMD System.err.println("No ruleset found for language " + language); + return Collections.emptyList(); } try (InputStream is = input) { properties.load(is);