Search in sources :

Example 1 with XPathRule

use of net.sourceforge.pmd.lang.rule.XPathRule in project pmd by pmd.

the class XPathRuleTest method setUp.

@Before
public void setUp() {
    rule = new XPathRule();
    rule.setLanguage(LanguageRegistry.getLanguage(JavaLanguageModule.NAME));
    rule.setMessage("XPath Rule Failed");
}
Also used : XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) Before(org.junit.Before)

Example 2 with XPathRule

use of net.sourceforge.pmd.lang.rule.XPathRule in project pmd by pmd.

the class PLSQLXPathRuleTest method testXPathRule1Compatibility.

/**
 * See https://sourceforge.net/p/pmd/bugs/1166/
 */
@Test
public void testXPathRule1Compatibility() {
    XPathRule rule = createRule("1.0 compatibility");
    rule.apply(Arrays.asList(node), ctx);
    Assert.assertEquals(2, ctx.getReport().treeSize());
}
Also used : XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) Test(org.junit.Test)

Example 3 with XPathRule

use of net.sourceforge.pmd.lang.rule.XPathRule in project pmd by pmd.

the class CodeClimateRendererTest method testXPathRule.

@Test
public void testXPathRule() throws Exception {
    DummyNode node = createNode(1);
    RuleContext ctx = new RuleContext();
    ctx.setSourceCodeFilename(getSourceCodeFilename());
    Report report = new Report();
    XPathRule theRule = new XPathRule();
    theRule.setProperty(XPathRule.XPATH_DESCRIPTOR, "//dummyNode");
    // Setup as FooRule
    theRule.setDescription("desc");
    theRule.setName("Foo");
    report.addRuleViolation(new ParametricRuleViolation<Node>(theRule, ctx, node, "blah"));
    String rendered = ReportTest.render(getRenderer(), report);
    // Output should be the exact same as for non xpath rules
    assertEquals(filter(getExpected()), filter(rendered));
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode) Node(net.sourceforge.pmd.lang.ast.Node) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) ReportTest(net.sourceforge.pmd.ReportTest) Test(org.junit.Test)

Example 4 with XPathRule

use of net.sourceforge.pmd.lang.rule.XPathRule in project pmd by pmd.

the class RuleSetFactory method createSingleRuleRuleSet.

/**
 * Creates a new RuleSet for a single rule
 *
 * @param rule
 *            The rule being created
 * @return The newly created RuleSet
 */
public RuleSet createSingleRuleRuleSet(final Rule rule) {
    final long checksum;
    if (rule instanceof XPathRule) {
        checksum = rule.getProperty(XPathRule.XPATH_DESCRIPTOR).hashCode();
    } else {
        // TODO : Is this good enough? all properties' values + rule name
        checksum = rule.getPropertiesByPropertyDescriptor().values().hashCode() * 31 + rule.getName().hashCode();
    }
    final RuleSetBuilder builder = new RuleSetBuilder(checksum).withName(rule.getName()).withDescription("RuleSet for " + rule.getName());
    builder.addRule(rule);
    return builder.build();
}
Also used : RuleSetBuilder(net.sourceforge.pmd.RuleSet.RuleSetBuilder) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule)

Example 5 with XPathRule

use of net.sourceforge.pmd.lang.rule.XPathRule in project pmd by pmd.

the class XPathJspRuleTest method testExpressionMatching.

/**
 * Test matching a XPath expression against a JSP source.
 * @throws PMDException
 */
@Test
public void testExpressionMatching() throws PMDException {
    Rule rule = new XPathRule(XPATH_EXPRESSION);
    rule.setMessage("Test");
    rule.setLanguage(LanguageRegistry.getLanguage(JspLanguageModule.NAME));
    RuleSet rules = new RuleSetFactory().createSingleRuleRuleSet(rule);
    RuleContext ctx = new RuleContext();
    Report report = new Report();
    ctx.setReport(report);
    ctx.setSourceCodeFilename("n/a");
    ctx.setLanguageVersion(LanguageRegistry.getLanguage(JspLanguageModule.NAME).getDefaultVersion());
    PMD p = new PMD();
    p.getSourceCodeProcessor().processSourceCode(new StringReader(MATCH), new RuleSets(rules), ctx);
    assertEquals("One violation expected!", 1, report.size());
    RuleViolation rv = report.iterator().next();
    assertEquals(1, rv.getBeginLine());
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSet(net.sourceforge.pmd.RuleSet) RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) PMD(net.sourceforge.pmd.PMD) RuleSets(net.sourceforge.pmd.RuleSets) StringReader(java.io.StringReader) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) Rule(net.sourceforge.pmd.Rule) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) RuleViolation(net.sourceforge.pmd.RuleViolation) Test(org.junit.Test)

Aggregations

XPathRule (net.sourceforge.pmd.lang.rule.XPathRule)13 Test (org.junit.Test)6 RuleContext (net.sourceforge.pmd.RuleContext)4 RuleSet (net.sourceforge.pmd.RuleSet)4 ArrayList (java.util.ArrayList)3 Rule (net.sourceforge.pmd.Rule)3 RuleSets (net.sourceforge.pmd.RuleSets)3 Node (net.sourceforge.pmd.lang.ast.Node)3 Report (net.sourceforge.pmd.Report)2 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)2 StringReader (java.io.StringReader)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 PMD (net.sourceforge.pmd.PMD)1 ReportTest (net.sourceforge.pmd.ReportTest)1