1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package test.net.sourceforge.pmd.testframework;
5
6 import junit.framework.TestCase;
7 import net.sourceforge.pmd.PMD;
8 import net.sourceforge.pmd.Report;
9 import net.sourceforge.pmd.Rule;
10 import net.sourceforge.pmd.RuleContext;
11 import net.sourceforge.pmd.RuleSet;
12 import net.sourceforge.pmd.RuleSetFactory;
13 import net.sourceforge.pmd.RuleSetNotFoundException;
14
15 import java.io.StringReader;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 public class RuleTst extends TestCase {
20
21 private Map rulesets = new HashMap();
22 private RuleSetFactory rsf = new RuleSetFactory();
23 public void runTestFromString(String code, int expectedResults, Rule rule) throws Throwable {
24 assertEquals(expectedResults, processUsingStringReader(code, rule).size());
25 }
26
27 public Rule findRule(String rs, String r) throws RuleSetNotFoundException {
28 if (!rulesets.containsKey(rs)) {
29 rulesets.put(rs, rsf.createRuleSet(rs));
30 }
31 return ((RuleSet)rulesets.get(rs)).getRuleByName(r);
32 }
33
34 public void runTestFromString(String code, Rule rule, Report report) throws Throwable {
35 PMD p = new PMD();
36 RuleContext ctx = new RuleContext();
37 ctx.setReport(report);
38 ctx.setSourceCodeFilename("n/a");
39 RuleSet rules = new RuleSet();
40 rules.addRule(rule);
41 p.processFile(new StringReader(code), rules, ctx);
42 }
43
44 private Report processUsingStringReader(String code, Rule rule) throws Throwable {
45 Report report = new Report();
46 runTestFromString(code, rule, report);
47 return report;
48 }
49 }