diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java index 4edf435e21..ec01b80240 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java @@ -520,7 +520,7 @@ public class RuleSetFactory { } boolean rulesetDeprecated = false; - if (potentialRules.size() == countDeprecated) { + if (!potentialRules.isEmpty() && potentialRules.size() == countDeprecated) { // all rules in the ruleset have been deprecated - the ruleset itself is considered to be deprecated rulesetDeprecated = true; LOG.warning("The RuleSet " + ref + " has been deprecated."); diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java index a0712ed1ec..c1364f02e3 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java @@ -14,10 +14,15 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -575,6 +580,59 @@ public class RuleSetFactoryTest { assertEquals(0, ruleset.getRules().size()); } + private static class InMemoryLogHandler extends Handler { + private List records = Collections.synchronizedList(new ArrayList()); + + @Override + public void close() throws SecurityException { + } + + @Override + public void flush() { + records.clear(); + } + + @Override + public void publish(LogRecord record) { + records.add(record); + } + + public List getLogRecords() { + return new ArrayList<>(records); + } + } + + /** + * See https://github.com/pmd/pmd/issues/782 + * Empty ruleset should be interpreted as deprecated. + * + * @throws Exception + * any error + */ + @Test + public void testEmptyRuleSetReferencedShouldNotBeDeprecated() throws Exception { + InMemoryLogHandler logHandler = new InMemoryLogHandler(); + Logger logger = Logger.getLogger(RuleSetFactory.class.getName()); + try { + logger.addHandler(logHandler); + + RuleSetReferenceId ref = createRuleSetReferenceId("\n" + "\n" + + "\n" + + " Ruleset which references a empty ruleset\n" + "\n" + + " \n" + + "\n"); + RuleSetFactory ruleSetFactory = new RuleSetFactory(new ResourceLoader(), RulePriority.LOW, true, true); + RuleSet ruleset = ruleSetFactory.createRuleSet(ref); + assertEquals(0, ruleset.getRules().size()); + + assertEquals(0, logHandler.getLogRecords().size()); + } finally { + logger.removeHandler(logHandler); + } + } + /** * See https://sourceforge.net/p/pmd/bugs/1231/ * diff --git a/pmd-core/src/test/resources/rulesets/dummy/empty-ruleset.xml b/pmd-core/src/test/resources/rulesets/dummy/empty-ruleset.xml new file mode 100644 index 0000000000..03f3775c2e --- /dev/null +++ b/pmd-core/src/test/resources/rulesets/dummy/empty-ruleset.xml @@ -0,0 +1,12 @@ + + + + + Empty Ruleset which contains no rules. + Although it is empty, it should not be marked as deprecated, just because it has no rules (yet). + + + + + \ No newline at end of file