View Javadoc

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