Search in sources :

Example 6 with RuleReference

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

the class RuleSetTest method testAddRuleSetByReferenceAllRule.

@Test
public void testAddRuleSetByReferenceAllRule() {
    RuleSet set2 = createRuleSetBuilder("ruleset2").withFileName("foo").addRule(new MockRule("name", "desc", "msg", "rulesetname")).addRule(new MockRule("name2", "desc", "msg", "rulesetname")).build();
    RuleSet set1 = createRuleSetBuilder("ruleset1").addRuleSetByReference(set2, true).build();
    assertEquals("wrong rule size", 2, set1.getRules().size());
    for (Rule rule : set1.getRules()) {
        assertTrue("not a rule reference", rule instanceof RuleReference);
        RuleReference ruleReference = (RuleReference) rule;
        assertEquals("wrong ruleset file name", "foo", ruleReference.getRuleSetReference().getRuleSetFileName());
        assertTrue("not all rule reference", ruleReference.getRuleSetReference().isAllRules());
    }
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) MockRule(net.sourceforge.pmd.lang.rule.MockRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 7 with RuleReference

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

the class RuleSetTest method testAddRuleSetByReferenceSingleRule.

@Test
public void testAddRuleSetByReferenceSingleRule() {
    RuleSet set2 = createRuleSetBuilder("ruleset2").withFileName("foo").addRule(new MockRule("name", "desc", "msg", "rulesetname")).addRule(new MockRule("name2", "desc", "msg", "rulesetname")).build();
    RuleSet set1 = createRuleSetBuilder("ruleset1").addRuleSetByReference(set2, false).build();
    assertEquals("wrong rule size", 2, set1.getRules().size());
    for (Rule rule : set1.getRules()) {
        assertTrue("not a rule reference", rule instanceof RuleReference);
        RuleReference ruleReference = (RuleReference) rule;
        assertEquals("wrong ruleset file name", "foo", ruleReference.getRuleSetReference().getRuleSetFileName());
        assertFalse("should not be all rule reference", ruleReference.getRuleSetReference().isAllRules());
    }
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) MockRule(net.sourceforge.pmd.lang.rule.MockRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 8 with RuleReference

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

the class RuleSetWriterTest method testRuleReferenceOverriddenName.

/**
 * Unit test for #1312 see https://sourceforge.net/p/pmd/bugs/1312/
 *
 * @throws Exception
 *             any error
 */
@Test
public void testRuleReferenceOverriddenName() throws Exception {
    RuleSetFactory ruleSetFactory = new RuleSetFactory();
    RuleSet rs = ruleSetFactory.createRuleSet("dummy-basic");
    RuleSetReference ruleSetReference = new RuleSetReference("rulesets/dummy/basic.xml");
    RuleReference ruleRef = new RuleReference();
    ruleRef.setRule(rs.getRuleByName("DummyBasicMockRule"));
    ruleRef.setRuleSetReference(ruleSetReference);
    // override the name
    ruleRef.setName("Foo");
    RuleSet ruleSet = ruleSetFactory.createSingleRuleRuleSet(ruleRef);
    writer.write(ruleSet);
    String written = out.toString("UTF-8");
    Assert.assertTrue(written.contains("ref=\"rulesets/dummy/basic.xml/DummyBasicMockRule\""));
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) Test(org.junit.Test)

Example 9 with RuleReference

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

the class RuleDocGenerator method generateLanguageIndex.

/**
 * Writes for each language an index file, which lists the rulesets, the rules
 * and links to the ruleset pages.
 * @param rulesets all registered/built-in rulesets
 * @param sortedAdditionalRulesets additional rulesets
 * @throws IOException
 */
private void generateLanguageIndex(Map<Language, List<RuleSet>> rulesets, Map<Language, List<RuleSet>> sortedAdditionalRulesets) throws IOException {
    for (Map.Entry<Language, List<RuleSet>> entry : rulesets.entrySet()) {
        String languageTersename = entry.getKey().getTerseName();
        String filename = LANGUAGE_INDEX_FILENAME_PATTERN.replace("${language.tersename}", languageTersename);
        Path path = getAbsoluteOutputPath(filename);
        List<String> lines = new LinkedList<>();
        lines.add("---");
        lines.add("title: " + entry.getKey().getName() + " Rules");
        lines.add("permalink: " + LANGUAGE_INDEX_PERMALINK_PATTERN.replace("${language.tersename}", languageTersename));
        lines.add("folder: pmd/rules");
        lines.add("---");
        for (RuleSet ruleset : entry.getValue()) {
            lines.add("## " + ruleset.getName());
            lines.add("");
            lines.add("{% include callout.html content=\"" + getRuleSetDescriptionSingleLine(ruleset).replaceAll("\"", "'") + "\" %}");
            lines.add("");
            for (Rule rule : getSortedRules(ruleset)) {
                String link = RULESET_INDEX_PERMALINK_PATTERN.replace("${language.tersename}", languageTersename).replace("${ruleset.name}", RuleSetUtils.getRuleSetFilename(ruleset));
                if (rule instanceof RuleReference) {
                    RuleReference ref = (RuleReference) rule;
                    if (ruleset.getFileName().equals(ref.getRuleSetReference().getRuleSetFileName())) {
                        // rule renamed within same ruleset
                        lines.add("*   [" + rule.getName() + "](" + link + "#" + rule.getName().toLowerCase(Locale.ROOT) + "): " + DEPRECATION_LABEL_SMALL + "The rule has been renamed. Use instead " + "[" + ref.getRule().getName() + "](" + link + "#" + ref.getRule().getName().toLowerCase(Locale.ROOT) + ").");
                    } else {
                        // rule moved to another ruleset...
                        String otherLink = RULESET_INDEX_PERMALINK_PATTERN.replace("${language.tersename}", languageTersename).replace("${ruleset.name}", RuleSetUtils.getRuleSetFilename(ref.getRuleSetReference().getRuleSetFileName()));
                        lines.add("*   [" + rule.getName() + "](" + link + "#" + rule.getName().toLowerCase(Locale.ROOT) + "): " + DEPRECATION_LABEL_SMALL + "The rule has been moved to another ruleset. Use instead " + "[" + ref.getRule().getName() + "](" + otherLink + "#" + ref.getRule().getName().toLowerCase(Locale.ROOT) + ").");
                    }
                } else {
                    link += "#" + rule.getName().toLowerCase(Locale.ROOT);
                    lines.add("*   [" + rule.getName() + "](" + link + "): " + (rule.isDeprecated() ? DEPRECATION_LABEL_SMALL : "") + getShortRuleDescription(rule));
                }
            }
            lines.add("");
        }
        List<RuleSet> additionalRulesetsForLanguage = sortedAdditionalRulesets.get(entry.getKey());
        if (additionalRulesetsForLanguage != null) {
            lines.add("## Additional rulesets");
            lines.add("");
            for (RuleSet ruleset : additionalRulesetsForLanguage) {
                boolean deprecated = RuleSetUtils.isRuleSetDeprecated(ruleset);
                String rulesetName = ruleset.getName() + " (`" + RuleSetUtils.getRuleSetClasspath(ruleset) + "`)";
                if (!deprecated) {
                    lines.add("*   " + rulesetName + ":");
                    lines.add("");
                    lines.add("    " + getRuleSetDescriptionSingleLine(ruleset));
                    lines.add("");
                } else {
                    lines.add("*   " + rulesetName + ":");
                    lines.add("");
                    lines.add("    " + DEPRECATION_LABEL_SMALL + " This ruleset is for backwards compatibility.");
                    lines.add("");
                }
                lines.add("    It contains the following rules:");
                lines.add("");
                StringBuilder rules = new StringBuilder();
                for (Rule rule : getSortedRules(ruleset)) {
                    if (rules.length() == 0) {
                        rules.append("    ");
                    } else {
                        rules.append(", ");
                    }
                    Rule resolvedRule = RuleSetUtils.resolveRuleReferences(rule);
                    if (resolvedRule instanceof RuleReference) {
                        // Note: deprecated rulesets contain by definition only rule references
                        RuleReference ref = (RuleReference) resolvedRule;
                        String otherLink = RULESET_INDEX_PERMALINK_PATTERN.replace("${language.tersename}", languageTersename).replace("${ruleset.name}", RuleSetUtils.getRuleSetFilename(ref.getRuleSetReference().getRuleSetFileName()));
                        rules.append("[").append(ref.getName()).append("](");
                        rules.append(otherLink).append("#").append(ref.getRule().getName().toLowerCase(Locale.ROOT)).append(")");
                    } else {
                        rules.append(rule.getName());
                    }
                }
                lines.add(rules.toString());
                lines.add("");
            }
            lines.add("");
        }
        System.out.println("Generated " + path);
        writer.write(path, lines);
    }
}
Also used : Path(java.nio.file.Path) RuleSet(net.sourceforge.pmd.RuleSet) RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) LinkedList(java.util.LinkedList) Language(net.sourceforge.pmd.lang.Language) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) Rule(net.sourceforge.pmd.Rule) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 10 with RuleReference

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

the class RuleSetUtils method resolveRuleReferences.

/**
 * Recursively resolves rule references until the last reference.
 * The last reference is returned.
 * If the given rule not a reference, the rule is returned.
 *
 * @param rule
 * @return
 */
public static Rule resolveRuleReferences(Rule rule) {
    Rule result = rule;
    Rule ref = rule;
    while (ref instanceof RuleReference) {
        // remember the last reference
        result = ref;
        ref = ((RuleReference) ref).getRule();
    }
    return result;
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) Rule(net.sourceforge.pmd.Rule)

Aggregations

RuleReference (net.sourceforge.pmd.lang.rule.RuleReference)23 Test (org.junit.Test)10 MockRule (net.sourceforge.pmd.lang.rule.MockRule)9 Rule (net.sourceforge.pmd.Rule)7 XPathRule (net.sourceforge.pmd.lang.rule.XPathRule)7 ArrayList (java.util.ArrayList)5 Language (net.sourceforge.pmd.lang.Language)4 List (java.util.List)3 PropertyDescriptor (net.sourceforge.pmd.properties.PropertyDescriptor)3 StringProperty (net.sourceforge.pmd.properties.StringProperty)3 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 RuleSet (net.sourceforge.pmd.RuleSet)2 JavaUtilLoggingRule (net.sourceforge.pmd.junit.JavaUtilLoggingRule)2 Element (org.w3c.dom.Element)2 HashSet (java.util.HashSet)1