pmd: fix #945 PMD generates RuleSets it cannot read.

This commit is contained in:
Andreas Dangel
2013-03-30 09:59:00 +01:00
parent 62fc70e816
commit b998e64819
4 changed files with 66 additions and 2 deletions

View File

@@ -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

View File

@@ -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<String>(Arrays.asList(excludes)));
}
for (Rule rule : ruleSet.getRules()) {
RuleReference ruleReference = new RuleReference(rule, ruleSetReference);
rules.add(ruleReference);

View File

@@ -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) {

View File

@@ -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("<exclude name=\"WhileLoopsMustUseBraces\""));
}
}