RuleFactory is now an object vs a glom of static methods
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@141 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -15,29 +15,34 @@ import net.sourceforge.pmd.rules.DontCreateTimersRule;
|
||||
import net.sourceforge.pmd.rules.EmptyIfStmtRule;
|
||||
|
||||
public class RuleFactoryTest extends TestCase {
|
||||
|
||||
public RuleFactoryTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void testCougaar() {
|
||||
List r = RuleFactory.createRules(RuleFactory.COUGAAR);
|
||||
RuleFactory rf = new RuleFactory();
|
||||
List r = rf.createRules(RuleFactory.COUGAAR);
|
||||
assertTrue(r.contains(new DontCreateTimersRule()));
|
||||
}
|
||||
|
||||
public void testAll() {
|
||||
List r = RuleFactory.createRules(RuleFactory.ALL);
|
||||
RuleFactory rf = new RuleFactory();
|
||||
List r = rf.createRules(RuleFactory.ALL);
|
||||
assertTrue(r.contains(new EmptyIfStmtRule()));
|
||||
}
|
||||
|
||||
public void testGeneral() {
|
||||
List r = RuleFactory.createRules(RuleFactory.GENERAL);
|
||||
RuleFactory rf = new RuleFactory();
|
||||
List r = rf.createRules(RuleFactory.GENERAL);
|
||||
assertTrue(r.contains(new EmptyIfStmtRule()));
|
||||
assertTrue(!r.contains(new DontCreateTimersRule()));
|
||||
}
|
||||
|
||||
public void testException() {
|
||||
RuleFactory rf = new RuleFactory();
|
||||
try {
|
||||
RuleFactory.createRules("blah");
|
||||
rf.createRules("blah");
|
||||
} catch (Exception e) {
|
||||
return; // cool
|
||||
}
|
||||
@ -45,13 +50,15 @@ public class RuleFactoryTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testConcatenatedList() {
|
||||
String list = RuleFactory.getConcatenatedRuleSetList();
|
||||
RuleFactory rf = new RuleFactory();
|
||||
String list = rf.getConcatenatedRuleSetList();
|
||||
assertTrue(list.indexOf("design") != -1);
|
||||
}
|
||||
|
||||
public void testContains() {
|
||||
assertTrue(RuleFactory.containsRuleSet("all"));
|
||||
assertTrue(!RuleFactory.containsRuleSet("foo"));
|
||||
RuleFactory rf = new RuleFactory();
|
||||
assertTrue(rf.containsRuleSet("all"));
|
||||
assertTrue(!rf.containsRuleSet("foo"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -30,27 +30,6 @@ public class RuleTst
|
||||
Rule rule )
|
||||
throws Throwable
|
||||
{
|
||||
/*
|
||||
// Set up the Context
|
||||
RuleContext ctx = new RuleContext();
|
||||
ctx.setReport( new Report("xml", fileName) );
|
||||
|
||||
// Parse the file
|
||||
InputStream javaFile =
|
||||
getClass().getClassLoader().getResourceAsStream( fileName );
|
||||
JavaParser parser = new JavaParser( javaFile );
|
||||
ASTCompilationUnit astCU = parser.CompilationUnit();
|
||||
|
||||
// Collect the ACUs
|
||||
List acus = new ArrayList();
|
||||
acus.add( astCU );
|
||||
|
||||
// Apply the rules
|
||||
rule.apply( acus, ctx );
|
||||
|
||||
// Return the report.
|
||||
return ctx.getReport();
|
||||
*/
|
||||
PMD p = new PMD();
|
||||
RuleContext ctx = new RuleContext();
|
||||
ctx.setReport(new Report("xml", fileName));
|
||||
|
@ -45,7 +45,8 @@ public class PMD {
|
||||
}
|
||||
|
||||
public void processFile(String filename, InputStream is, String ruleSetType, RuleContext ctx) throws FileNotFoundException {
|
||||
List rules = RuleFactory.createRules(ruleSetType);
|
||||
RuleFactory ruleFactory = new RuleFactory();
|
||||
List rules = ruleFactory.createRules(ruleSetType);
|
||||
processFile(filename, is, rules, ctx);
|
||||
}
|
||||
|
||||
|
@ -19,14 +19,14 @@ public class RuleFactory {
|
||||
|
||||
private static Set ruleSets = new HashSet();
|
||||
|
||||
static {
|
||||
public RuleFactory() {
|
||||
ruleSets.add(ALL);
|
||||
ruleSets.add(GENERAL);
|
||||
ruleSets.add(COUGAAR);
|
||||
ruleSets.add(DESIGN);
|
||||
}
|
||||
|
||||
public static String getConcatenatedRuleSetList() {
|
||||
public String getConcatenatedRuleSetList() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
for (Iterator i = ruleSets.iterator(); i.hasNext();) {
|
||||
if (buf.length() != 0) {
|
||||
@ -37,11 +37,11 @@ public class RuleFactory {
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static boolean containsRuleSet(String ruleSet) {
|
||||
public boolean containsRuleSet(String ruleSet) {
|
||||
return ruleSets.contains(ruleSet);
|
||||
}
|
||||
|
||||
public static List createRules(String ruleSetType) {
|
||||
public List createRules(String ruleSetType) {
|
||||
if (ruleSetType.equals(ALL)) {
|
||||
return createAllRules();
|
||||
} else if (ruleSetType.equals(GENERAL)) {
|
||||
@ -54,7 +54,7 @@ public class RuleFactory {
|
||||
throw new RuntimeException("Unknown rule set type " + ruleSetType);
|
||||
}
|
||||
|
||||
private static List createAllRules() {
|
||||
private List createAllRules() {
|
||||
List list = new ArrayList();
|
||||
list.addAll(createCougaarRules());
|
||||
list.addAll(createGeneralRules());
|
||||
@ -62,7 +62,7 @@ public class RuleFactory {
|
||||
return list;
|
||||
}
|
||||
|
||||
private static List createCougaarRules() {
|
||||
private List createCougaarRules() {
|
||||
List list = new ArrayList();
|
||||
list.add(new DontCreateThreadsRule());
|
||||
list.add(new DontCreateTimersRule());
|
||||
@ -71,7 +71,7 @@ public class RuleFactory {
|
||||
return list;
|
||||
}
|
||||
|
||||
private static List createGeneralRules() {
|
||||
private List createGeneralRules() {
|
||||
List list = new ArrayList();
|
||||
list.add(new EmptyCatchBlockRule());
|
||||
list.add(new EmptyIfStmtRule());
|
||||
@ -83,7 +83,7 @@ public class RuleFactory {
|
||||
return list;
|
||||
}
|
||||
|
||||
private static List createDesignRules() {
|
||||
private List createDesignRules() {
|
||||
List list = new ArrayList();
|
||||
list.add(new UseSingletonRule());
|
||||
return list;
|
||||
|
@ -46,8 +46,10 @@ public class PMDTask extends Task {
|
||||
if (reportFile == null) {
|
||||
throw new BuildException("No report file specified");
|
||||
}
|
||||
if (ruleSetType == null || !RuleFactory.containsRuleSet(ruleSetType)) {
|
||||
throw new BuildException("Rule set type must be one of: " + RuleFactory.getConcatenatedRuleSetList() + "; you specified " + ruleSetType);
|
||||
|
||||
RuleFactory ruleFactory = new RuleFactory();
|
||||
if (ruleSetType == null || !ruleFactory.containsRuleSet(ruleSetType)) {
|
||||
throw new BuildException("Rule set type must be one of: " + ruleFactory.getConcatenatedRuleSetList() + "; you specified " + ruleSetType);
|
||||
}
|
||||
if (format == null || (!format.equals("text") && !format.equals("xml") && !format.equals("html"))) {
|
||||
throw new BuildException("Report format must be either 'text', 'xml', or 'html'; you specified " + format);
|
||||
|
Reference in New Issue
Block a user