Search in sources :

Example 11 with MockRule

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

the class RuleSetTest method testRuleList.

@Test
public void testRuleList() {
    MockRule rule = new MockRule("name", "desc", "msg", "rulesetname");
    RuleSet ruleset = new RuleSetFactory().createSingleRuleRuleSet(rule);
    assertEquals("Size of RuleSet isn't one.", 1, ruleset.size());
    Collection<Rule> rules = ruleset.getRules();
    Iterator<Rule> i = rules.iterator();
    assertTrue("Empty Set", i.hasNext());
    assertEquals("Returned set of wrong size.", 1, rules.size());
    assertEquals("Rule isn't in ruleset.", rule, i.next());
}
Also used : MockRule(net.sourceforge.pmd.lang.rule.MockRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 12 with MockRule

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

the class RuleSetTest method testLanguageApplies.

@Test
public void testLanguageApplies() {
    Rule rule = new MockRule();
    rule.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
    assertFalse("Different languages should not apply", RuleSet.applies(rule, LanguageRegistry.getLanguage(Dummy2LanguageModule.NAME).getDefaultVersion()));
    rule.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
    assertTrue("Same language with no min/max should apply", RuleSet.applies(rule, LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.5")));
    rule.setMinimumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.5"));
    assertTrue("Same language with valid min only should apply", RuleSet.applies(rule, LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.5")));
    rule.setMaximumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.6"));
    assertTrue("Same language with valid min and max should apply", RuleSet.applies(rule, LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.5")));
    assertFalse("Same language with outside range of min/max should not apply", RuleSet.applies(rule, LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.4")));
    assertFalse("Same language with outside range of min/max should not apply", RuleSet.applies(rule, LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.7")));
}
Also used : MockRule(net.sourceforge.pmd.lang.rule.MockRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 13 with MockRule

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

the class RuleViolationTest method testComparatorWithSameFileSameLines.

@Ignore
@Test
public void testComparatorWithSameFileSameLines() {
    Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
    RuleViolationComparator comp = RuleViolationComparator.INSTANCE;
    RuleContext ctx = new RuleContext();
    ctx.setSourceCodeFilename("filename");
    DummyNode s = new DummyNode(1);
    s.testingOnlySetBeginLine(10);
    s.testingOnlySetBeginColumn(1);
    DummyNode s1 = new DummyNode(1);
    s1.testingOnlySetBeginLine(10);
    s1.testingOnlySetBeginColumn(1);
    RuleViolation r1 = new ParametricRuleViolation<Node>(rule, ctx, s, "description");
    RuleViolation r2 = new ParametricRuleViolation<Node>(rule, ctx, s1, "description");
    assertEquals(1, comp.compare(r1, r2));
    assertEquals(1, comp.compare(r2, r1));
}
Also used : ParametricRuleViolation(net.sourceforge.pmd.lang.rule.ParametricRuleViolation) 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 14 with MockRule

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

the class RuleViolationTest method testConstructor2.

@Test
public void testConstructor2() {
    Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
    RuleContext ctx = new RuleContext();
    ctx.setSourceCodeFilename("filename");
    DummyNode s = new DummyNode(1);
    s.testingOnlySetBeginLine(2);
    s.testingOnlySetBeginColumn(1);
    RuleViolation r = new ParametricRuleViolation<Node>(rule, ctx, s, "description");
    assertEquals("object mismatch", rule, r.getRule());
    assertEquals("line number is wrong", 2, r.getBeginLine());
    assertEquals("filename is wrong", "filename", r.getFilename());
    assertEquals("description is wrong", "description", r.getDescription());
}
Also used : ParametricRuleViolation(net.sourceforge.pmd.lang.rule.ParametricRuleViolation) 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 15 with MockRule

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

the class ReportTest method testSummary.

@Test
public void testSummary() {
    Report r = new Report();
    RuleContext ctx = new RuleContext();
    ctx.setSourceCodeFilename("foo1");
    Node s = getNode(5, 5);
    Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
    r.addRuleViolation(new ParametricRuleViolation<>(rule, ctx, s, rule.getMessage()));
    ctx.setSourceCodeFilename("foo2");
    Rule mr = new MockRule("rule1", "rule1", "msg", "rulesetname");
    Node s1 = getNode(20, 5);
    Node s2 = getNode(30, 5);
    r.addRuleViolation(new ParametricRuleViolation<>(mr, ctx, s1, mr.getMessage()));
    r.addRuleViolation(new ParametricRuleViolation<>(mr, ctx, s2, mr.getMessage()));
    Map<String, Integer> summary = r.getSummary();
    assertEquals(summary.keySet().size(), 2);
    assertTrue(summary.values().contains(Integer.valueOf(1)));
    assertTrue(summary.values().contains(Integer.valueOf(2)));
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) DummyNode(net.sourceforge.pmd.lang.ast.DummyNode) MockRule(net.sourceforge.pmd.lang.rule.MockRule) 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