Search in sources :

Example 21 with MockRule

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

the class RuleViolationComparatorTest method testComparator.

@Test
public void testComparator() {
    Rule rule1 = new MockRule("name1", "desc", "msg", "rulesetname1");
    Rule rule2 = new MockRule("name2", "desc", "msg", "rulesetname2");
    // RuleViolations created in pre-sorted order
    RuleViolation[] expectedOrder = new RuleViolation[12];
    int index = 0;
    // Different begin line
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file1", 10, "desc1", 0, 20, 80);
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file1", 20, "desc1", 0, 20, 80);
    // Different description
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file2", 10, "desc1", 0, 20, 80);
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file2", 10, "desc2", 0, 20, 80);
    // Different begin column
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file3", 10, "desc1", 0, 20, 80);
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file3", 10, "desc1", 10, 20, 80);
    // Different end line
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file4", 10, "desc1", 0, 20, 80);
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file4", 10, "desc1", 0, 30, 80);
    // Different end column
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file5", 10, "desc1", 0, 20, 80);
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file5", 10, "desc1", 0, 20, 90);
    // Different rule name
    expectedOrder[index++] = createJavaRuleViolation(rule1, "file6", 10, "desc1", 0, 20, 80);
    expectedOrder[index++] = createJavaRuleViolation(rule2, "file6", 10, "desc1", 0, 20, 80);
    // Randomize
    List<RuleViolation> ruleViolations = new ArrayList<>(Arrays.asList(expectedOrder));
    long seed = System.nanoTime();
    Random random = new Random(seed);
    Collections.shuffle(ruleViolations, random);
    // Sort
    Collections.sort(ruleViolations, RuleViolationComparator.INSTANCE);
    // Check
    int count = 0;
    for (int i = 0; i < expectedOrder.length; i++) {
        count++;
        assertSame("Wrong RuleViolation " + i + ", usind seed: " + seed, expectedOrder[i], ruleViolations.get(i));
    }
    assertEquals("Missing assertion for every RuleViolation", expectedOrder.length, count);
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) 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 22 with MockRule

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

the class RuleViolationTest method testComparatorWithDifferentFilenames.

@Test
public void testComparatorWithDifferentFilenames() {
    Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
    RuleViolationComparator comp = RuleViolationComparator.INSTANCE;
    RuleContext ctx = new RuleContext();
    ctx.setSourceCodeFilename("filename1");
    DummyNode s = new DummyNode(1);
    s.testingOnlySetBeginLine(10);
    s.testingOnlySetBeginColumn(1);
    RuleViolation r1 = new ParametricRuleViolation<Node>(rule, ctx, s, "description");
    ctx.setSourceCodeFilename("filename2");
    DummyNode s1 = new DummyNode(1);
    s1.testingOnlySetBeginLine(10);
    s1.testingOnlySetBeginColumn(1);
    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) Test(org.junit.Test)

Example 23 with MockRule

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

the class RuleViolationTest method testConstructor1.

@Test
public void testConstructor1() {
    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, rule.getMessage());
    assertEquals("object mismatch", rule, r.getRule());
    assertEquals("line number is wrong", 2, r.getBeginLine());
    assertEquals("filename is wrong", "filename", r.getFilename());
}
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 24 with MockRule

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

the class RuleViolationTest method testComparatorWithSameFileDifferentLines.

@Test
public void testComparatorWithSameFileDifferentLines() {
    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(20);
    s1.testingOnlySetBeginColumn(1);
    RuleViolation r1 = new ParametricRuleViolation<Node>(rule, ctx, s, "description");
    RuleViolation r2 = new ParametricRuleViolation<Node>(rule, ctx, s1, "description");
    assertTrue(comp.compare(r1, r2) < 0);
    assertTrue(comp.compare(r2, r1) > 0);
}
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 25 with MockRule

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

the class RuleSetFactory method parseRuleReferenceNode.

/**
 * Parse a rule node as a RuleReference. A RuleReference is a single Rule
 * which comes from another RuleSet with some of it's attributes potentially
 * overridden.
 *
 * @param ruleSetReferenceId
 *            The RuleSetReferenceId of the RuleSet being parsed.
 * @param ruleSetBuilder
 *            The RuleSet being constructed.
 * @param ruleNode
 *            Must be a rule element node.
 * @param ref
 *            A reference to a Rule.
 * @param withDeprecatedRuleReferences
 *            whether rule references that are deprecated should be ignored
 *            or not
 */
private void parseRuleReferenceNode(RuleSetReferenceId ruleSetReferenceId, RuleSetBuilder ruleSetBuilder, Node ruleNode, String ref, boolean withDeprecatedRuleReferences) throws RuleSetNotFoundException {
    Element ruleElement = (Element) ruleNode;
    // it.
    if (StringUtils.isNotBlank(ruleSetReferenceId.getRuleName()) && !isRuleName(ruleElement, ruleSetReferenceId.getRuleName())) {
        return;
    }
    // load the ruleset with minimum priority low, so that we get all rules, to be able to exclude any rule
    // minimum priority will be applied again, before constructing the final ruleset
    RuleSetFactory ruleSetFactory = new RuleSetFactory(resourceLoader, RulePriority.LOW, warnDeprecated, this.compatibilityFilter != null);
    boolean isSameRuleSet = false;
    RuleSetReferenceId otherRuleSetReferenceId = RuleSetReferenceId.parse(ref).get(0);
    if (!otherRuleSetReferenceId.isExternal() && containsRule(ruleSetReferenceId, otherRuleSetReferenceId.getRuleName())) {
        otherRuleSetReferenceId = new RuleSetReferenceId(ref, ruleSetReferenceId);
        isSameRuleSet = true;
    }
    // do not ignore deprecated rule references
    Rule referencedRule = ruleSetFactory.createRule(otherRuleSetReferenceId, true);
    if (referencedRule == null) {
        throw new IllegalArgumentException("Unable to find referenced rule " + otherRuleSetReferenceId.getRuleName() + "; perhaps the rule name is mispelled?");
    }
    if (warnDeprecated && referencedRule.isDeprecated()) {
        if (referencedRule instanceof RuleReference) {
            RuleReference ruleReference = (RuleReference) referencedRule;
            if (LOG.isLoggable(Level.WARNING)) {
                LOG.warning("Use Rule name " + ruleReference.getRuleSetReference().getRuleSetFileName() + '/' + ruleReference.getOriginalName() + " instead of the deprecated Rule name " + otherRuleSetReferenceId + ". PMD " + PMDVersion.getNextMajorRelease() + " will remove support for this deprecated Rule name usage.");
            }
        } else if (referencedRule instanceof MockRule) {
            if (LOG.isLoggable(Level.WARNING)) {
                LOG.warning("Discontinue using Rule name " + otherRuleSetReferenceId + " as it has been removed from PMD and no longer functions." + " PMD " + PMDVersion.getNextMajorRelease() + " will remove support for this Rule.");
            }
        } else {
            if (LOG.isLoggable(Level.WARNING)) {
                LOG.warning("Discontinue using Rule name " + otherRuleSetReferenceId + " as it is scheduled for removal from PMD." + " PMD " + PMDVersion.getNextMajorRelease() + " will remove support for this Rule.");
            }
        }
    }
    RuleSetReference ruleSetReference = new RuleSetReference(otherRuleSetReferenceId.getRuleSetFileName(), false);
    RuleReference ruleReference = new RuleFactory().decorateRule(referencedRule, ruleSetReference, ruleElement);
    if (warnDeprecated && ruleReference.isDeprecated()) {
        if (LOG.isLoggable(Level.WARNING)) {
            LOG.warning("Use Rule name " + ruleReference.getRuleSetReference().getRuleSetFileName() + '/' + ruleReference.getOriginalName() + " instead of the deprecated Rule name " + ruleSetReferenceId.getRuleSetFileName() + '/' + ruleReference.getName() + ". PMD " + PMDVersion.getNextMajorRelease() + " will remove support for this deprecated Rule name usage.");
        }
    }
    if (withDeprecatedRuleReferences || !isSameRuleSet || !ruleReference.isDeprecated()) {
        ruleSetBuilder.addRuleReplaceIfExists(ruleReference);
    }
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) RuleFactory(net.sourceforge.pmd.rules.RuleFactory) Element(org.w3c.dom.Element) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule)

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