1   package test.net.sourceforge.pmd.jsp.ast;
2   
3   import static org.junit.Assert.assertEquals;
4   import net.sourceforge.pmd.Language;
5   import net.sourceforge.pmd.PMD;
6   import net.sourceforge.pmd.Report;
7   import net.sourceforge.pmd.Rule;
8   import net.sourceforge.pmd.RuleContext;
9   import net.sourceforge.pmd.RuleSet;
10  import net.sourceforge.pmd.RuleSets;
11  import net.sourceforge.pmd.RuleViolation;
12  import net.sourceforge.pmd.SourceType;
13  import net.sourceforge.pmd.rules.XPathRule;
14  
15  import org.junit.Test;
16  
17  import test.net.sourceforge.pmd.testframework.RuleTst;
18  
19  import java.io.StringReader;
20  
21  public class XPathJspRuleTest extends RuleTst {
22  
23      /**
24       * Test matching a XPath expression against a JSP source.
25       *
26       * @throws Throwable
27       */
28      @Test
29      public void testExpressionMatching() throws Throwable {
30          Rule rule = new XPathRule();
31          rule.addProperty("xpath", XPATH_EXPRESSION);
32          rule.setMessage("Test");
33          RuleSet rules = new RuleSet();
34          rules.addRule(rule);
35          rules.setLanguage(Language.JSP);
36  
37          RuleContext ctx = new RuleContext();
38          Report report = new Report();
39          ctx.setReport(report);
40          ctx.setSourceCodeFilename("n/a");
41  
42          PMD p = new PMD();
43  
44          p.processFile(new StringReader(MATCH), new RuleSets(rules), ctx, SourceType.JSP);
45  
46          assertEquals("One violation expected!", 1, report.size());
47  
48          RuleViolation rv = (RuleViolation) report.iterator().next();
49          assertEquals(1, rv.getBeginLine());
50      }
51  
52      private static final String MATCH
53              = "<html><hr/></html>";
54  
55      private static final String XPATH_EXPRESSION
56              = "//Element [@Name='hr']";
57  
58      public static junit.framework.Test suite() {
59          return new junit.framework.JUnit4TestAdapter(XPathJspRuleTest.class);
60      }
61  }