forked from phoedos/pmd
Cleaned up MockRule - removed a lot of unneeded methods and fields. Made AbstractRule fields protected vs private so that MockRule could use them.
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@2711 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -467,7 +467,7 @@ void ClassBody() :
|
||||
void NestedClassDeclaration() :
|
||||
{}
|
||||
{
|
||||
( "static" { jjtThis.setStatic(); }
|
||||
( "static" { jjtThis.setStatic(); }
|
||||
| "abstract" { jjtThis.setAbstract(); }
|
||||
| "final" { jjtThis.setFinal(); }
|
||||
| "public" { jjtThis.setPublic(); }
|
||||
|
@ -84,7 +84,7 @@ public class RuleSetTest extends TestCase {
|
||||
RuleSet set1 = new RuleSet();
|
||||
set1.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
|
||||
RuleSet set2 = new RuleSet();
|
||||
set2.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
|
||||
set2.addRule(new MockRule("name2", "desc", "msg", "rulesetname"));
|
||||
set1.addRuleSet(set2);
|
||||
assertEquals("ruleset size wrong", 2, set1.size());
|
||||
}
|
||||
@ -94,42 +94,6 @@ public class RuleSetTest extends TestCase {
|
||||
verifyRuleSet(IUT, 0, new HashSet());
|
||||
}
|
||||
|
||||
public void testApply1Rule() throws Throwable {
|
||||
RuleSet IUT = new RuleSet();
|
||||
|
||||
MockRule rule = new MockRule("name", "desc", "msg", "rulesetname");
|
||||
RuleContext ctx = new RuleContext();
|
||||
ctx.setSourceCodeFilename("filename");
|
||||
RuleViolation violation = new RuleViolation(rule, 1, ctx);
|
||||
rule.addViolation(violation);
|
||||
|
||||
IUT.addRule(rule);
|
||||
|
||||
verifyRuleSet(IUT, 1, Collections.singleton(violation));
|
||||
}
|
||||
|
||||
public void testApplyNRule() throws Throwable {
|
||||
RuleSet IUT = new RuleSet();
|
||||
|
||||
Random rand = new Random();
|
||||
int numRules = rand.nextInt(10) + 1;
|
||||
Set ruleViolations = new HashSet();
|
||||
|
||||
for (int i = 0; i < numRules; i++) {
|
||||
MockRule rule = new MockRule("name", "desc", "msg", "rulesetname");
|
||||
RuleContext ctx = new RuleContext();
|
||||
ctx.setSourceCodeFilename("filename");
|
||||
RuleViolation violation = new RuleViolation(rule, i, ctx);
|
||||
|
||||
ruleViolations.add(violation);
|
||||
rule.addViolation(violation);
|
||||
|
||||
IUT.addRule(rule);
|
||||
}
|
||||
|
||||
verifyRuleSet(IUT, numRules, ruleViolations);
|
||||
}
|
||||
|
||||
protected void verifyRuleSet(RuleSet IUT, int size, Set values) throws Throwable {
|
||||
|
||||
RuleContext context = new RuleContext();
|
||||
|
@ -3,140 +3,19 @@
|
||||
*/
|
||||
package test.net.sourceforge.pmd.testframework;
|
||||
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.AbstractRule;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
public class MockRule implements Rule {
|
||||
|
||||
private String name;
|
||||
private String ruleSetName;
|
||||
private String description;
|
||||
private String message;
|
||||
private Set violations = new HashSet();
|
||||
private Properties properties = new Properties();
|
||||
private String example;
|
||||
private int priority;
|
||||
|
||||
public String getRuleSetName() {
|
||||
return ruleSetName;
|
||||
}
|
||||
|
||||
public void setRuleSetName(String ruleSetName) {
|
||||
this.ruleSetName = ruleSetName;
|
||||
}
|
||||
|
||||
public String getExample() {
|
||||
return example;
|
||||
}
|
||||
|
||||
public void setExample(String example) {
|
||||
this.example = example;
|
||||
}
|
||||
|
||||
public int getPriority() {
|
||||
return this.priority;
|
||||
}
|
||||
|
||||
public String getPriorityName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setPriority(int priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public boolean hasProperty(String name) {
|
||||
return properties.containsKey(name);
|
||||
}
|
||||
|
||||
public void addProperty(String name, String value) {
|
||||
properties.setProperty(name, value);
|
||||
}
|
||||
|
||||
public int getIntProperty(String name) {
|
||||
return Integer.parseInt(properties.getProperty(name));
|
||||
}
|
||||
|
||||
public double getDoubleProperty(String name) {
|
||||
return Double.parseDouble(properties.getProperty(name));
|
||||
}
|
||||
|
||||
public boolean getBooleanProperty(String name) {
|
||||
return Boolean.valueOf(properties.getProperty(name)).booleanValue();
|
||||
}
|
||||
|
||||
public String getStringProperty(String name) {
|
||||
return properties.getProperty(name);
|
||||
}
|
||||
|
||||
public Properties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public boolean include() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setInclude(boolean include) {
|
||||
}
|
||||
|
||||
/**
|
||||
* For use by RuleSetFactory only!
|
||||
*/
|
||||
public MockRule() {
|
||||
}
|
||||
public class MockRule extends AbstractRule {
|
||||
|
||||
public MockRule(String name, String description, String message, String ruleSetName) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.message = message;
|
||||
this.ruleSetName = ruleSetName;
|
||||
}
|
||||
|
||||
|
||||
public void addViolation(RuleViolation violation) {
|
||||
violations.add(violation);
|
||||
public MockRule() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void apply(List astCompilationUnits, RuleContext ctx) {
|
||||
Report report = ctx.getReport();
|
||||
|
||||
Iterator vs = violations.iterator();
|
||||
while (vs.hasNext()) {
|
||||
report.addRuleViolation((RuleViolation) vs.next());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,14 +12,14 @@ import java.util.Properties;
|
||||
|
||||
public abstract class AbstractRule extends JavaParserVisitorAdapter implements Rule {
|
||||
|
||||
private String name = getClass().getName();
|
||||
private Properties properties = new Properties();
|
||||
private String message;
|
||||
private String description;
|
||||
private String example;
|
||||
private String ruleSetName;
|
||||
private boolean m_include;
|
||||
private int m_priority = LOWEST_PRIORITY;
|
||||
protected String name = getClass().getName();
|
||||
protected Properties properties = new Properties();
|
||||
protected String message;
|
||||
protected String description;
|
||||
protected String example;
|
||||
protected String ruleSetName;
|
||||
protected boolean m_include;
|
||||
protected int priority = LOWEST_PRIORITY;
|
||||
|
||||
public String getRuleSetName() {
|
||||
return ruleSetName;
|
||||
@ -115,78 +115,27 @@ public abstract class AbstractRule extends JavaParserVisitorAdapter implements R
|
||||
return new RuleViolation(this, lineNumber, specificDescription, ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
********************************************************************************
|
||||
*
|
||||
* Gets an enumeration to enumerate through this rule's property names.
|
||||
*
|
||||
* @return An enumeration of property names
|
||||
*/
|
||||
public Properties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
/**
|
||||
*********************************************************************************
|
||||
*
|
||||
* When the rule is to be included in the analysis, returns true; otherwise, returns false.
|
||||
*
|
||||
* @return True when the rule is included in analysis.
|
||||
*/
|
||||
public boolean include() {
|
||||
return m_include;
|
||||
}
|
||||
|
||||
/**
|
||||
*********************************************************************************
|
||||
*
|
||||
* When the rule is to be included in the analysis, set to true; otherwise, set to false.
|
||||
*
|
||||
* @param include True when the rule is included in analysis.
|
||||
*/
|
||||
public void setInclude(boolean include) {
|
||||
m_include = include;
|
||||
}
|
||||
|
||||
/**
|
||||
*********************************************************************************
|
||||
*
|
||||
* Returns the rule's priority that is used for including the rule in reports and analysis.
|
||||
*
|
||||
* @return A number between 1 and LOWEST_PRIORITY.
|
||||
*/
|
||||
public int getPriority() {
|
||||
if ((m_priority < 0) || (m_priority > LOWEST_PRIORITY)) {
|
||||
m_priority = LOWEST_PRIORITY;
|
||||
}
|
||||
|
||||
return m_priority;
|
||||
return priority;
|
||||
}
|
||||
|
||||
/**
|
||||
*********************************************************************************
|
||||
*
|
||||
* Returns the rule's priority name that is used for including the rule in reports and analysis.
|
||||
*
|
||||
* @return A member of PRIORITIES.
|
||||
*/
|
||||
public String getPriorityName() {
|
||||
return PRIORITIES[getPriority() - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
*********************************************************************************
|
||||
*
|
||||
* A rule will specify a priority for inclusion in reports and analysis. The default
|
||||
* priority is "Low".
|
||||
*
|
||||
* @param The rule's priority of 1..LOWEST_PRIORITY.
|
||||
*/
|
||||
public void setPriority(int priority) {
|
||||
if ((priority < 1) || (priority > LOWEST_PRIORITY)) {
|
||||
m_priority = LOWEST_PRIORITY;
|
||||
} else {
|
||||
m_priority = priority;
|
||||
}
|
||||
this.priority = priority;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user