View Javadoc

1   package net.sourceforge.pmd.lang.java.rule;
2    
3   import static org.junit.Assert.assertEquals;
4   
5   import java.io.StringReader;
6   
7   import net.sourceforge.pmd.PMD;
8   import net.sourceforge.pmd.Report;
9   import net.sourceforge.pmd.RuleContext;
10  import net.sourceforge.pmd.RuleSet;
11  import net.sourceforge.pmd.RuleSets;
12  import net.sourceforge.pmd.RuleViolation;
13  import net.sourceforge.pmd.lang.Language;
14  import net.sourceforge.pmd.lang.rule.XPathRule;
15  import net.sourceforge.pmd.lang.rule.properties.StringProperty;
16  import net.sourceforge.pmd.testframework.RuleTst;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  
21   /**
22    * @author daniels
23    */
24   public class XPathRuleTest extends RuleTst {
25   
26       XPathRule rule;
27   
28       @Before
29       public void setUp() {
30           rule = new XPathRule();
31           rule.setLanguage(Language.JAVA);
32           rule.setMessage("XPath Rule Failed");
33       }
34   
35       @Test
36       public void testPluginname() throws Throwable {
37           rule.setXPath("//VariableDeclaratorId[string-length(@Image) < 3]");
38           rule.setMessage("{0}");
39           PMD p = new PMD();
40           RuleContext ctx = new RuleContext();
41           Report report = new Report();
42           ctx.setReport(report);
43           ctx.setSourceCodeFilename("n/a");
44           RuleSet rules = new RuleSet();
45           rules.addRule(rule);
46           p.getSourceCodeProcessor().processSourceCode(new StringReader(TEST1), new RuleSets(rules), ctx);
47           RuleViolation rv = (RuleViolation) report.iterator().next();
48           assertEquals("a", rv.getDescription());
49       }
50   
51       @Test
52       public void testVariables() throws Throwable {
53           rule.setXPath("//VariableDeclaratorId[@Image=$var]");
54           rule.setMessage("Avoid vars");
55           StringProperty varDescriptor = new StringProperty("var", "Test var", null, 1.0f);
56           rule.definePropertyDescriptor(varDescriptor);
57           rule.setProperty(varDescriptor, "fiddle");
58           PMD p = new PMD();
59           RuleContext ctx = new RuleContext();
60           Report report = new Report();
61           ctx.setReport(report);
62           ctx.setSourceCodeFilename("n/a");
63           RuleSet rules = new RuleSet();
64           rules.addRule(rule);
65           p.getSourceCodeProcessor().processSourceCode(new StringReader(TEST2), new RuleSets(rules), ctx);
66           RuleViolation rv = (RuleViolation) report.iterator().next();
67           assertEquals(3, rv.getBeginLine());
68       }
69   
70       private static final String TEST1 =
71               "public class Foo {" + PMD.EOL +
72               " int a;" + PMD.EOL +
73               "}";
74   
75       private static final String TEST2 =
76               "public class Foo {" + PMD.EOL +
77               " int faddle;" + PMD.EOL +
78               " int fiddle;" + PMD.EOL +
79               "}";
80  
81       public static junit.framework.Test suite() {
82           return new junit.framework.JUnit4TestAdapter(XPathRuleTest.class);
83       }
84   }