pmd: fix #1074 rule priority doesn't work on group definitions

and verify #1067 Custom Ruleset doesn't import the complete ruleset anymore by reference
This commit is contained in:
Andreas Dangel 2013-03-09 17:56:45 +01:00
parent 3e8c42384b
commit a1b1e3f31c
4 changed files with 109 additions and 4 deletions

View File

@ -2,6 +2,7 @@
Fixed bug 1064: Exception running PrematureDeclaration
Fixed bug 1068: CPD fails on broken symbolic links
Fixed bug 1074: rule priority doesn't work on group definitions

View File

@ -280,11 +280,15 @@ public class RuleSetFactory {
RuleSetReference ruleSetReference = new RuleSetReference();
ruleSetReference.setAllRules(true);
ruleSetReference.setRuleSetFileName(ref);
NodeList excludeNodes = ruleElement.getChildNodes();
for (int i = 0; i < excludeNodes.getLength(); i++) {
if (isElementNode(excludeNodes.item(i),"exclude")) {
Element excludeElement = (Element) excludeNodes.item(i);
String priority = null;
NodeList childNodes = ruleElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i);
if (isElementNode(child,"exclude")) {
Element excludeElement = (Element) child;
ruleSetReference.addExclude(excludeElement.getAttribute("name"));
} else if (isElementNode(child, "priority")) {
priority = parseTextNode(child).trim();
}
}
@ -298,6 +302,11 @@ public class RuleSetFactory {
ruleReference.setRuleSetReference(ruleSetReference);
ruleReference.setRule(rule);
ruleSet.addRule(ruleReference);
// override the priority
if (priority != null) {
ruleReference.setPriority(RulePriority.valueOf(Integer.parseInt(priority)));
}
}
}
}

View File

@ -28,6 +28,7 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import junit.framework.Assert;
import junit.framework.JUnit4TestAdapter;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageVersion;
@ -105,6 +106,54 @@ public class RuleSetFactoryTest {
assertNotNull(rs.getRuleByName("AvoidEnumAsIdentifier"));
}
@Test
public void testExtendedReferences() throws Exception {
InputStream in = ResourceLoader.loadResourceAsStream("net/sourceforge/pmd/rulesets/reference-ruleset.xml",
this.getClass().getClassLoader());
Assert.assertNotNull("Test ruleset not found - can't continue with test!", in);
RuleSetFactory rsf = new RuleSetFactory();
RuleSet rs = rsf.createRuleSet("net/sourceforge/pmd/rulesets/reference-ruleset.xml");
// added by referencing a complete ruleset (java-basic)
assertNotNull(rs.getRuleByName("JumbledIncrementer"));
assertNotNull(rs.getRuleByName("ForLoopShouldBeWhileLoop"));
assertNotNull(rs.getRuleByName("OverrideBothEqualsAndHashcode"));
// added by specific reference
assertNotNull(rs.getRuleByName("UnusedLocalVariable"));
assertNotNull(rs.getRuleByName("DuplicateImports"));
// this is from java-unusedcode, but not referenced
assertNull(rs.getRuleByName("UnusedPrivateField"));
Rule emptyCatchBlock = rs.getRuleByName("EmptyCatchBlock");
assertNotNull(emptyCatchBlock);
// default priority in java-empty is 3, but overridden to 2
assertEquals(2, emptyCatchBlock.getPriority().getPriority());
Rule cyclomaticComplexity = rs.getRuleByName("CyclomaticComplexity");
assertNotNull(cyclomaticComplexity);
PropertyDescriptor<?> prop = cyclomaticComplexity.getPropertyDescriptor("reportLevel");
Object property = cyclomaticComplexity.getProperty(prop);
assertEquals("5", String.valueOf(property));
// included from braces
assertNotNull(rs.getRuleByName("IfStmtsMustUseBraces"));
// excluded from braces
assertNull(rs.getRuleByName("WhileLoopsMustUseBraces"));
// overridden to 5
Rule simplifyBooleanExpressions = rs.getRuleByName("SimplifyBooleanExpressions");
assertNotNull(simplifyBooleanExpressions);
assertEquals(5, simplifyBooleanExpressions.getPriority().getPriority());
// priority overridden for whole design group
Rule useUtilityClass = rs.getRuleByName("UseSingleton");
assertNotNull(useUtilityClass);
assertEquals(2, useUtilityClass.getPriority().getPriority());
Rule simplifyBooleanReturns = rs.getRuleByName("SimplifyBooleanReturns");
assertNotNull(simplifyBooleanReturns);
assertEquals(2, simplifyBooleanReturns.getPriority().getPriority());
}
@Test(expected = RuleSetNotFoundException.class)
public void testRuleSetNotFound() throws RuleSetNotFoundException {
RuleSetFactory rsf = new RuleSetFactory();

View File

@ -0,0 +1,46 @@
<?xml version="1.0"?>
<ruleset name="Custom ruleset"
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>
This ruleset checks my code for bad stuff
</description>
<!-- We'll use the entire 'basic' ruleset -->
<rule ref="rulesets/java/basic.xml"/>
<!-- Here's some rules we'll specify one at a time -->
<rule ref="rulesets/java/unusedcode.xml/UnusedLocalVariable"/>
<rule ref="rulesets/java/imports.xml/DuplicateImports"/>
<!-- We want to customize this rule a bit, change the message and raise the priority -->
<rule
ref="rulesets/java/basic.xml/EmptyCatchBlock"
message="Must handle exceptions">
<priority>2</priority>
</rule>
<!-- Now we'll customize a rule's property value -->
<rule ref="rulesets/java/codesize.xml/CyclomaticComplexity">
<properties>
<property name="reportLevel" value="5"/>
</properties>
</rule>
<!-- We want everything from braces.xml except WhileLoopsMustUseBraces -->
<rule ref="rulesets/java/braces.xml">
<exclude name="WhileLoopsMustUseBraces"/>
</rule>
<!-- override the priority before the group priority - this should be used -->
<rule ref="rulesets/java/design.xml/SimplifyBooleanExpressions">
<priority>5</priority>
</rule>
<!-- we want to override the priority for all (remaining) rules in design -->
<rule ref="rulesets/java/design.xml">
<priority>2</priority>
</rule>
</ruleset>