Search in sources :

Example 11 with RuleContext

use of net.sourceforge.pmd.RuleContext in project pmd by pmd.

the class AbstractRule method addViolation.

/**
 * @see RuleViolationFactory#addViolation(RuleContext, Rule, Node, String,
 * Object[])
 */
public void addViolation(Object data, Node node, Object[] args) {
    RuleContext ruleContext = (RuleContext) data;
    ruleContext.getLanguageVersion().getLanguageVersionHandler().getRuleViolationFactory().addViolation(ruleContext, this, node, this.getMessage(), args);
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext)

Example 12 with RuleContext

use of net.sourceforge.pmd.RuleContext in project pmd by pmd.

the class XMLRendererTest method createRuleViolation.

private static RuleViolation createRuleViolation(String description) {
    DummyNode node = new DummyNode(1);
    node.testingOnlySetBeginLine(1);
    node.testingOnlySetBeginColumn(1);
    node.testingOnlySetEndLine(1);
    node.testingOnlySetEndColumn(1);
    RuleContext ctx = new RuleContext();
    ctx.setSourceCodeFilename("n/a");
    return new ParametricRuleViolation<Node>(new FooRule(), ctx, node, description);
}
Also used : ParametricRuleViolation(net.sourceforge.pmd.lang.rule.ParametricRuleViolation) FooRule(net.sourceforge.pmd.FooRule) RuleContext(net.sourceforge.pmd.RuleContext) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode)

Example 13 with RuleContext

use of net.sourceforge.pmd.RuleContext in project pmd by pmd.

the class XSLTRendererTest method testDefaultStylesheet.

@Test
public void testDefaultStylesheet() throws Exception {
    XSLTRenderer renderer = new XSLTRenderer();
    Report report = new Report();
    DummyNode node = new DummyNode(1);
    node.testingOnlySetBeginLine(1);
    node.testingOnlySetBeginColumn(1);
    RuleViolation rv = new ParametricRuleViolation<Node>(new FooRule(), new RuleContext(), node, "violation message");
    report.addRuleViolation(rv);
    String result = ReportTest.render(renderer, report);
    Assert.assertTrue(result.contains("violation message"));
}
Also used : ParametricRuleViolation(net.sourceforge.pmd.lang.rule.ParametricRuleViolation) FooRule(net.sourceforge.pmd.FooRule) RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode) RuleViolation(net.sourceforge.pmd.RuleViolation) ParametricRuleViolation(net.sourceforge.pmd.lang.rule.ParametricRuleViolation) ReportTest(net.sourceforge.pmd.ReportTest) Test(org.junit.Test)

Example 14 with RuleContext

use of net.sourceforge.pmd.RuleContext in project pmd by pmd.

the class StatisticalRuleTest method makeReport.

public Report makeReport(Rule rule) {
    List<Node> list = new ArrayList<>();
    Report report = new Report();
    RuleContext ctx = new RuleContext();
    ctx.setReport(report);
    ctx.setSourceCodeFilename(testName);
    ctx.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion());
    rule.apply(list, ctx);
    return report;
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) Report(net.sourceforge.pmd.Report) Node(net.sourceforge.pmd.lang.ast.Node) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode) ArrayList(java.util.ArrayList)

Example 15 with RuleContext

use of net.sourceforge.pmd.RuleContext 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)

Aggregations

RuleContext (net.sourceforge.pmd.RuleContext)51 Test (org.junit.Test)19 RuleSets (net.sourceforge.pmd.RuleSets)17 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)15 Report (net.sourceforge.pmd.Report)14 Node (net.sourceforge.pmd.lang.ast.Node)13 RuleSet (net.sourceforge.pmd.RuleSet)11 StringReader (java.io.StringReader)9 RuleViolation (net.sourceforge.pmd.RuleViolation)9 IOException (java.io.IOException)8 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)8 PMDException (net.sourceforge.pmd.PMDException)8 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)8 ArrayList (java.util.ArrayList)7 SourceCodeProcessor (net.sourceforge.pmd.SourceCodeProcessor)7 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)6 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)6 DummyNode (net.sourceforge.pmd.lang.ast.DummyNode)5 FooRule (net.sourceforge.pmd.FooRule)4 Parser (net.sourceforge.pmd.lang.Parser)4