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