From b998e64819a3c68f871c2eb6e1620ed359c98860 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 30 Mar 2013 09:59:00 +0100 Subject: [PATCH] pmd: fix #945 PMD generates RuleSets it cannot read. --- pmd/etc/changelog.txt | 1 + .../java/net/sourceforge/pmd/RuleSet.java | 20 ++++++++- .../net/sourceforge/pmd/RuleSetWriter.java | 4 +- .../sourceforge/pmd/RuleSetWriterTest.java | 43 +++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 pmd/src/test/java/net/sourceforge/pmd/RuleSetWriterTest.java diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 7bccf648e4..823ad68d8c 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,5 +1,6 @@ ????? ??, 2013 - 5.0.3: +Fixed bug 945: PMD generates RuleSets it cannot read. Fixed bug 958: Intermittent NullPointerException while loading XPath node attributes Fixed bug 968: Issues with JUnit4 @Test annotation with expected exception (Thanks to Yiannis Paschalidis) Fixed bug 975: false positive in ClassCastExceptionWithToArray diff --git a/pmd/src/main/java/net/sourceforge/pmd/RuleSet.java b/pmd/src/main/java/net/sourceforge/pmd/RuleSet.java index d02e7bb9d8..059327aa2a 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/RuleSet.java +++ b/pmd/src/main/java/net/sourceforge/pmd/RuleSet.java @@ -5,7 +5,9 @@ package net.sourceforge.pmd; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.logging.Level; @@ -162,6 +164,18 @@ public class RuleSet { rules.addAll(rules.size(), ruleSet.getRules()); } + /** + * Add all rules by reference from one RuleSet to this RuleSet. The rules + * can be added as individual references, or collectively as an all rule + * reference. + * + * @param ruleSet the RuleSet to add + * @param allRules + */ + public void addRuleSetByReference(RuleSet ruleSet, boolean allRules) { + addRuleSetByReference(ruleSet, allRules, (String[])null); + } + /** * Add all rules by reference from one RuleSet to this RuleSet. The rules * can be added as individual references, or collectively as an all rule @@ -169,13 +183,17 @@ public class RuleSet { * * @param ruleSet the RuleSet to add * @param allRules + * @param excludes names of the rules that should be excluded. */ - public void addRuleSetByReference(RuleSet ruleSet, boolean allRules) { + public void addRuleSetByReference(RuleSet ruleSet, boolean allRules, String ... excludes) { if (StringUtil.isEmpty(ruleSet.getFileName())) { throw new RuntimeException("Adding a rule by reference is not allowed with an empty rule set file name."); } RuleSetReference ruleSetReference = new RuleSetReference(ruleSet.getFileName()); ruleSetReference.setAllRules(allRules); + if (excludes != null) { + ruleSetReference.setExcludes(new HashSet(Arrays.asList(excludes))); + } for (Rule rule : ruleSet.getRules()) { RuleReference ruleReference = new RuleReference(rule, ruleSetReference); rules.add(ruleReference); diff --git a/pmd/src/main/java/net/sourceforge/pmd/RuleSetWriter.java b/pmd/src/main/java/net/sourceforge/pmd/RuleSetWriter.java index e09361211c..7cc9a1f811 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/RuleSetWriter.java +++ b/pmd/src/main/java/net/sourceforge/pmd/RuleSetWriter.java @@ -129,7 +129,9 @@ public class RuleSetWriter { } private Element createExcludeElement(String exclude) { - return createTextElement("exclude", exclude); + Element element = document.createElementNS(RULESET_NS_URI, "exclude"); + element.setAttribute("name", exclude); + return element; } private Element createExampleElement(String example) { diff --git a/pmd/src/test/java/net/sourceforge/pmd/RuleSetWriterTest.java b/pmd/src/test/java/net/sourceforge/pmd/RuleSetWriterTest.java new file mode 100644 index 0000000000..08cb2cf4c8 --- /dev/null +++ b/pmd/src/test/java/net/sourceforge/pmd/RuleSetWriterTest.java @@ -0,0 +1,43 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd; + +import java.io.ByteArrayOutputStream; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit test for {@link RuleSetWriter}. + * + */ +public class RuleSetWriterTest { + + /** + * Tests the exclude rule behavior. + * See bug #945. + * @throws Exception any error + */ + @Test + public void testWrite() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + RuleSetWriter writer = null; + try { + writer = new RuleSetWriter(out); + + RuleSet ruleSet = new RuleSet(); + RuleSet braces = new RuleSetFactory().createRuleSet("java-braces"); + ruleSet.addRuleSetByReference(braces, true, "WhileLoopsMustUseBraces"); + + writer.write(ruleSet); + } finally { + if (writer != null) { + writer.close(); + } + } + + String written = out.toString("UTF-8"); + Assert.assertTrue(written.contains("