[core] Only issue a deprecation warning, if the referenced ruleset is not empty

and all rules have been deprecated.
Fixes #782
This commit is contained in:
Andreas Dangel
2017-12-28 17:17:11 +01:00
parent cd58d15bc2
commit f4577f9e14
3 changed files with 71 additions and 1 deletions

View File

@ -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.");

View File

@ -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<LogRecord> records = Collections.synchronizedList(new ArrayList<LogRecord>());
@Override
public void close() throws SecurityException {
}
@Override
public void flush() {
records.clear();
}
@Override
public void publish(LogRecord record) {
records.add(record);
}
public List<LogRecord> 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "\n"
+ "<ruleset name=\"Custom ruleset\" xmlns=\"http://pmd.sourceforge.net/ruleset/2.0.0\"\n"
+ " xmlns:xsi=\"http:www.w3.org/2001/XMLSchema-instance\"\n"
+ " xsi:schemaLocation=\"http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd\">\n"
+ " <description>Ruleset which references a empty ruleset</description>\n" + "\n"
+ " <rule ref=\"rulesets/dummy/empty-ruleset.xml\" />\n"
+ "</ruleset>\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/
*

View File

@ -0,0 +1,12 @@
<?xml version="1.0"?>
<ruleset name="Test Ruleset which is empty" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
Empty Ruleset which contains no rules.
Although it is empty, it should not be marked as deprecated, just because it has no rules (yet).
</description>
<!-- Deliberately empty, no rules. -->
</ruleset>