01: package test.net.sourceforge.pmd.jsp.ast;
02:
03: import static org.junit.Assert.assertEquals;
04: import net.sourceforge.pmd.Language;
05: import net.sourceforge.pmd.PMD;
06: import net.sourceforge.pmd.Report;
07: import net.sourceforge.pmd.Rule;
08: import net.sourceforge.pmd.RuleContext;
09: 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),
45: ctx, SourceType.JSP);
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 = "<html><hr/></html>";
54:
55: private static final String XPATH_EXPRESSION = "//Element [@Name='hr']";
56:
57: public static junit.framework.Test suite() {
58: return new junit.framework.JUnit4TestAdapter(
59: XPathJspRuleTest.class);
60: }
61: }
|