Search in sources :

Example 16 with MockRule

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

the class ReportTest method testSortedReportLine.

@Test
public void testSortedReportLine() throws IOException {
    Report r = new Report();
    RuleContext ctx = new RuleContext();
    ctx.setSourceCodeFilename("foo1");
    Node s = getNode(10, 5);
    Rule rule1 = new MockRule("rule2", "rule2", "msg", "rulesetname");
    r.addRuleViolation(new ParametricRuleViolation<>(rule1, ctx, s, rule1.getMessage()));
    ctx.setSourceCodeFilename("foo2");
    Node s1 = getNode(20, 5);
    Rule rule2 = new MockRule("rule1", "rule1", "msg", "rulesetname");
    r.addRuleViolation(new ParametricRuleViolation<>(rule2, ctx, s1, rule2.getMessage()));
    Renderer rend = new XMLRenderer();
    String result = render(rend, r);
    assertTrue("sort order wrong", result.indexOf("rule2") < result.indexOf("rule1"));
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode) XMLRenderer(net.sourceforge.pmd.renderers.XMLRenderer) Renderer(net.sourceforge.pmd.renderers.Renderer) MockRule(net.sourceforge.pmd.lang.rule.MockRule) XMLRenderer(net.sourceforge.pmd.renderers.XMLRenderer) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 17 with MockRule

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

the class ReportTest method testSortedReportFile.

// Files are grouped together now.
@Test
public void testSortedReportFile() throws IOException {
    Report r = new Report();
    RuleContext ctx = new RuleContext();
    ctx.setSourceCodeFilename("foo");
    Node s = getNode(10, 5);
    Rule rule1 = new MockRule("name", "desc", "msg", "rulesetname");
    r.addRuleViolation(new ParametricRuleViolation<>(rule1, ctx, s, rule1.getMessage()));
    ctx.setSourceCodeFilename("bar");
    Node s1 = getNode(10, 5);
    Rule rule2 = new MockRule("name", "desc", "msg", "rulesetname");
    r.addRuleViolation(new ParametricRuleViolation<>(rule2, ctx, s1, rule2.getMessage()));
    Renderer rend = new XMLRenderer();
    String result = render(rend, r);
    assertTrue("sort order wrong", result.indexOf("bar") < result.indexOf("foo"));
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode) XMLRenderer(net.sourceforge.pmd.renderers.XMLRenderer) Renderer(net.sourceforge.pmd.renderers.Renderer) MockRule(net.sourceforge.pmd.lang.rule.MockRule) XMLRenderer(net.sourceforge.pmd.renderers.XMLRenderer) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 18 with MockRule

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

the class ReportTest method testTreeIterator.

@Test
public void testTreeIterator() {
    Report r = new Report();
    RuleContext ctx = new RuleContext();
    Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
    Node node1 = getNode(5, 5, true);
    r.addRuleViolation(new ParametricRuleViolation<>(rule, ctx, node1, rule.getMessage()));
    Node node2 = getNode(5, 6, true);
    r.addRuleViolation(new ParametricRuleViolation<>(rule, ctx, node2, rule.getMessage()));
    Iterator<RuleViolation> violations = r.iterator();
    int violationCount = 0;
    while (violations.hasNext()) {
        violations.next();
        violationCount++;
    }
    assertEquals(2, violationCount);
    Iterator<RuleViolation> treeIterator = r.treeIterator();
    int treeCount = 0;
    while (treeIterator.hasNext()) {
        treeIterator.next();
        treeCount++;
    }
    assertEquals(2, treeCount);
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode) MockRule(net.sourceforge.pmd.lang.rule.MockRule) ParametricRuleViolation(net.sourceforge.pmd.lang.rule.ParametricRuleViolation) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 19 with MockRule

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

the class RuleSetTest method testIncludesRuleWithDFA.

@Test
public void testIncludesRuleWithDFA() {
    MockRule mock = new MockRule("name", "desc", "msg", "rulesetname");
    mock.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
    mock.setDfa(true);
    RuleSet rs = new RuleSetFactory().createSingleRuleRuleSet(mock);
    assertTrue(rs.usesDFA(LanguageRegistry.getLanguage(DummyLanguageModule.NAME)));
}
Also used : MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 20 with MockRule

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

the class RuleSetTest method testGetRuleByName2.

@Test
public void testGetRuleByName2() {
    MockRule mock = new MockRule("name", "desc", "msg", "rulesetname");
    RuleSet rs = new RuleSetFactory().createSingleRuleRuleSet(mock);
    assertNull("the rule FooRule must not be found!", rs.getRuleByName("FooRule"));
}
Also used : MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Aggregations

MockRule (net.sourceforge.pmd.lang.rule.MockRule)25 Test (org.junit.Test)24 DummyNode (net.sourceforge.pmd.lang.ast.DummyNode)10 ParametricRuleViolation (net.sourceforge.pmd.lang.rule.ParametricRuleViolation)7 RuleReference (net.sourceforge.pmd.lang.rule.RuleReference)6 Node (net.sourceforge.pmd.lang.ast.Node)5 StringProperty (net.sourceforge.pmd.properties.StringProperty)3 Renderer (net.sourceforge.pmd.renderers.Renderer)2 XMLRenderer (net.sourceforge.pmd.renderers.XMLRenderer)2 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 XPathRule (net.sourceforge.pmd.lang.rule.XPathRule)1 RuleFactory (net.sourceforge.pmd.rules.RuleFactory)1 Metric (net.sourceforge.pmd.stat.Metric)1 Ignore (org.junit.Ignore)1 Element (org.w3c.dom.Element)1