1
2 package test/net/sourceforge/pmd/rules/package-summary.html">> test.net.sourceforge.pmd.rules;
3
4 import java.io.StringReader;
5
6 import net.sourceforge.pmd.PMD;
7 import net.sourceforge.pmd.Report;
8 import net.sourceforge.pmd.Rule;
9 import net.sourceforge.pmd.RuleContext;
10 import net.sourceforge.pmd.RuleSet;
11 import net.sourceforge.pmd.RuleViolation;
12 import net.sourceforge.pmd.rules.XPathRule;
13 import test.net.sourceforge.pmd.testframework.RuleTst;
14
15 /***
16 * @author daniels
17 */
18 public class XPathRuleTest extends RuleTst {
19
20 XPathRule rule;
21
22 public void setUp() {
23 rule = new XPathRule();
24 rule.setMessage("XPath Rule Failed");
25 }
26
27 public void testPluginname() throws Throwable {
28 Rule rule = new XPathRule();
29 rule.addProperty("xpath", "//VariableDeclaratorId[string-length(@Image) < 3]");
30 rule.setMessage("{0}");
31 rule.addProperty("pluginname", "true");
32 PMD p = new PMD();
33 RuleContext ctx = new RuleContext();
34 Report report = new Report();
35 ctx.setReport(report);
36 ctx.setSourceCodeFilename("n/a");
37 RuleSet rules = new RuleSet();
38 rules.addRule(rule);
39 p.processFile(new StringReader(TEST1), rules, ctx);
40 RuleViolation rv = (RuleViolation) report.iterator().next();
41 assertEquals("a", rv.getDescription());
42 }
43
44 public void testVariables() throws Throwable {
45 Rule rule = new XPathRule();
46 rule.addProperty("xpath", "//VariableDeclaratorId[@Image=$var]");
47 rule.setMessage("Avoid vars");
48 rule.addProperty("var", "fiddle");
49 PMD p = new PMD();
50 RuleContext ctx = new RuleContext();
51 Report report = new Report();
52 ctx.setReport(report);
53 ctx.setSourceCodeFilename("n/a");
54 RuleSet rules = new RuleSet();
55 rules.addRule(rule);
56 p.processFile(new StringReader(TEST2), rules, ctx);
57 RuleViolation rv = (RuleViolation) report.iterator().next();
58 assertEquals(3, rv.getBeginLine());
59 }
60
61 private static final String TEST1 =
62 "public class Foo {" + PMD.EOL +
63 " int a;" + PMD.EOL +
64 "}";
65
66 private static final String TEST2 =
67 "public class Foo {" + PMD.EOL +
68 " int faddle;" + PMD.EOL +
69 " int fiddle;" + PMD.EOL +
70 "}";
71 }