diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index f98fd4ca22..a4a55eedd6 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/src/main/java/net/sourceforge/pmd/RuleSetFactory.java b/pmd/src/main/java/net/sourceforge/pmd/RuleSetFactory.java index d634828fbc..c0b3d44b57 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/RuleSetFactory.java +++ b/pmd/src/main/java/net/sourceforge/pmd/RuleSetFactory.java @@ -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))); + } } } } diff --git a/pmd/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java b/pmd/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java index e83cfb3771..a062bdbacc 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java +++ b/pmd/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java @@ -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(); diff --git a/pmd/src/test/resources/net/sourceforge/pmd/rulesets/reference-ruleset.xml b/pmd/src/test/resources/net/sourceforge/pmd/rulesets/reference-ruleset.xml new file mode 100644 index 0000000000..ccf566657a --- /dev/null +++ b/pmd/src/test/resources/net/sourceforge/pmd/rulesets/reference-ruleset.xml @@ -0,0 +1,46 @@ + + + + + This ruleset checks my code for bad stuff + + + + + + + + + + + + 2 + + + + + + + + + + + + + + + + + 5 + + + + + 2 + + \ No newline at end of file