Search in sources :

Example 1 with RuleReference

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

the class RuleReferenceTest method testRuleSetReference.

@Test
public void testRuleSetReference() {
    RuleReference ruleReference = new RuleReference();
    RuleSetReference ruleSetReference = new RuleSetReference("somename");
    ruleReference.setRuleSetReference(ruleSetReference);
    assertEquals("Not same rule set reference", ruleSetReference, ruleReference.getRuleSetReference());
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) Test(org.junit.Test)

Example 2 with RuleReference

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

the class RuleReferenceTest method testOverride.

@Test
public void testOverride() {
    final StringProperty PROPERTY1_DESCRIPTOR = new StringProperty("property1", "Test property", null, 0f);
    MockRule rule = new MockRule();
    rule.definePropertyDescriptor(PROPERTY1_DESCRIPTOR);
    rule.setLanguage(LanguageRegistry.getLanguage(Dummy2LanguageModule.NAME));
    rule.setName("name1");
    rule.setProperty(PROPERTY1_DESCRIPTOR, "value1");
    rule.setMessage("message1");
    rule.setDescription("description1");
    rule.addExample("example1");
    rule.setExternalInfoUrl("externalInfoUrl1");
    rule.setPriority(RulePriority.HIGH);
    final StringProperty PROPERTY2_DESCRIPTOR = new StringProperty("property2", "Test property", null, 0f);
    RuleReference ruleReference = new RuleReference();
    ruleReference.setRule(rule);
    ruleReference.definePropertyDescriptor(PROPERTY2_DESCRIPTOR);
    ruleReference.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
    ruleReference.setMinimumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.3"));
    ruleReference.setMaximumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.7"));
    ruleReference.setDeprecated(true);
    ruleReference.setName("name2");
    ruleReference.setProperty(PROPERTY1_DESCRIPTOR, "value2");
    ruleReference.setProperty(PROPERTY2_DESCRIPTOR, "value3");
    ruleReference.setMessage("message2");
    ruleReference.setDescription("description2");
    ruleReference.addExample("example2");
    ruleReference.setExternalInfoUrl("externalInfoUrl2");
    ruleReference.setPriority(RulePriority.MEDIUM_HIGH);
    validateOverridenValues(PROPERTY1_DESCRIPTOR, PROPERTY2_DESCRIPTOR, ruleReference);
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) StringProperty(net.sourceforge.pmd.properties.StringProperty) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 3 with RuleReference

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

the class RuleReferenceTest method testDeepCopyOverride.

@Test
public void testDeepCopyOverride() {
    final StringProperty PROPERTY1_DESCRIPTOR = new StringProperty("property1", "Test property", null, 0f);
    MockRule rule = new MockRule();
    rule.definePropertyDescriptor(PROPERTY1_DESCRIPTOR);
    rule.setLanguage(LanguageRegistry.getLanguage(Dummy2LanguageModule.NAME));
    rule.setName("name1");
    rule.setProperty(PROPERTY1_DESCRIPTOR, "value1");
    rule.setMessage("message1");
    rule.setDescription("description1");
    rule.addExample("example1");
    rule.setExternalInfoUrl("externalInfoUrl1");
    rule.setPriority(RulePriority.HIGH);
    final StringProperty PROPERTY2_DESCRIPTOR = new StringProperty("property2", "Test property", null, 0f);
    RuleReference ruleReference = new RuleReference();
    ruleReference.setRule(rule);
    ruleReference.definePropertyDescriptor(PROPERTY2_DESCRIPTOR);
    ruleReference.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
    ruleReference.setMinimumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.3"));
    ruleReference.setMaximumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.7"));
    ruleReference.setDeprecated(true);
    ruleReference.setName("name2");
    ruleReference.setProperty(PROPERTY1_DESCRIPTOR, "value2");
    ruleReference.setProperty(PROPERTY2_DESCRIPTOR, "value3");
    ruleReference.setMessage("message2");
    ruleReference.setDescription("description2");
    ruleReference.addExample("example2");
    ruleReference.setExternalInfoUrl("externalInfoUrl2");
    ruleReference.setPriority(RulePriority.MEDIUM_HIGH);
    validateOverridenValues(PROPERTY1_DESCRIPTOR, PROPERTY2_DESCRIPTOR, (RuleReference) ruleReference.deepCopy());
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) StringProperty(net.sourceforge.pmd.properties.StringProperty) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 4 with RuleReference

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

the class RuleReferenceTest method testNotOverride.

@Test
public void testNotOverride() {
    final StringProperty PROPERTY1_DESCRIPTOR = new StringProperty("property1", "Test property", null, 0f);
    MockRule rule = new MockRule();
    rule.definePropertyDescriptor(PROPERTY1_DESCRIPTOR);
    rule.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
    rule.setMinimumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.3"));
    rule.setMaximumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.7"));
    rule.setName("name1");
    rule.setProperty(PROPERTY1_DESCRIPTOR, "value1");
    rule.setMessage("message1");
    rule.setDescription("description1");
    rule.addExample("example1");
    rule.setExternalInfoUrl("externalInfoUrl1");
    rule.setPriority(RulePriority.HIGH);
    RuleReference ruleReference = new RuleReference();
    ruleReference.setRule(rule);
    ruleReference.setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
    ruleReference.setMinimumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.3"));
    ruleReference.setMaximumLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.7"));
    ruleReference.setDeprecated(false);
    ruleReference.setName("name1");
    ruleReference.setProperty(PROPERTY1_DESCRIPTOR, "value1");
    ruleReference.setMessage("message1");
    ruleReference.setDescription("description1");
    ruleReference.addExample("example1");
    ruleReference.setExternalInfoUrl("externalInfoUrl1");
    ruleReference.setPriority(RulePriority.HIGH);
    assertEquals("Override failed", LanguageRegistry.getLanguage(DummyLanguageModule.NAME), ruleReference.getLanguage());
    assertNull("Override failed", ruleReference.getOverriddenLanguage());
    assertEquals("Override failed", LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.3"), ruleReference.getMinimumLanguageVersion());
    assertNull("Override failed", ruleReference.getOverriddenMinimumLanguageVersion());
    assertEquals("Override failed", LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getVersion("1.7"), ruleReference.getMaximumLanguageVersion());
    assertNull("Override failed", ruleReference.getOverriddenMaximumLanguageVersion());
    assertEquals("Override failed", false, ruleReference.isDeprecated());
    assertNull("Override failed", ruleReference.isOverriddenDeprecated());
    assertEquals("Override failed", "name1", ruleReference.getName());
    assertNull("Override failed", ruleReference.getOverriddenName());
    assertEquals("Override failed", "value1", ruleReference.getProperty(PROPERTY1_DESCRIPTOR));
    assertEquals("Override failed", "message1", ruleReference.getMessage());
    assertNull("Override failed", ruleReference.getOverriddenMessage());
    assertEquals("Override failed", "description1", ruleReference.getDescription());
    assertNull("Override failed", ruleReference.getOverriddenDescription());
    assertEquals("Override failed", 1, ruleReference.getExamples().size());
    assertEquals("Override failed", "example1", ruleReference.getExamples().get(0));
    assertNull("Override failed", ruleReference.getOverriddenExamples());
    assertEquals("Override failed", "externalInfoUrl1", ruleReference.getExternalInfoUrl());
    assertNull("Override failed", ruleReference.getOverriddenExternalInfoUrl());
    assertEquals("Override failed", RulePriority.HIGH, ruleReference.getPriority());
    assertNull("Override failed", ruleReference.getOverriddenPriority());
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) StringProperty(net.sourceforge.pmd.properties.StringProperty) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 5 with RuleReference

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

the class RuleSetFactoryTest method testRuleReferenceWithNameOverridden.

/**
 * Unit test for #1312 see https://sourceforge.net/p/pmd/bugs/1312/
 *
 * @throws Exception
 *             any error
 */
@Test
public void testRuleReferenceWithNameOverridden() throws Exception {
    RuleSetReferenceId ref = createRuleSetReferenceId("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<ruleset xmlns=\"http://pmd.sourceforge.net/ruleset/2.0.0\"\n" + "         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         name=\"pmd-eclipse\"\n" + "         xsi:schemaLocation=\"http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd\">\n" + "   <description>PMD Plugin preferences rule set</description>\n" + "\n" + "<rule name=\"OverriddenDummyBasicMockRule\"\n" + "    ref=\"rulesets/dummy/basic.xml/DummyBasicMockRule\">\n" + "</rule>\n" + "\n" + "</ruleset>");
    RuleSetFactory ruleSetFactory = new RuleSetFactory();
    RuleSet rs = ruleSetFactory.createRuleSet(ref);
    Rule r = rs.getRules().toArray(new Rule[1])[0];
    assertEquals("OverriddenDummyBasicMockRule", r.getName());
    RuleReference ruleRef = (RuleReference) r;
    assertEquals("DummyBasicMockRule", ruleRef.getRule().getName());
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) JavaUtilLoggingRule(net.sourceforge.pmd.junit.JavaUtilLoggingRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

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