View Javadoc

    1   package net.sourceforge.pmd;
    2
    3   import static org.junit.Assert.assertEquals;
    4   import static org.junit.Assert.assertFalse;
    5   import static org.junit.Assert.assertNull;
    6   import static org.junit.Assert.assertTrue;
    7   import junit.framework.JUnit4TestAdapter;
    8   import net.sourceforge.pmd.RulePriority;
    9   import net.sourceforge.pmd.RuleSetReference;
    10  import net.sourceforge.pmd.lang.Language;
    11  import net.sourceforge.pmd.lang.LanguageVersion;
    12  import net.sourceforge.pmd.lang.rule.MockRule;
    13  import net.sourceforge.pmd.lang.rule.RuleReference;
    14  import net.sourceforge.pmd.lang.rule.properties.StringProperty;
    15
    16  import org.junit.Test;
    17
    18  public class RuleReferenceTest {
    19
    20  	@Test
    21  	public void testRuleSetReference() {
    22  		RuleReference ruleReference = new RuleReference();
    23  		RuleSetReference ruleSetReference = new RuleSetReference();
    24  		ruleReference.setRuleSetReference(ruleSetReference);
    25  		assertEquals("Not same rule set reference", ruleSetReference, ruleReference.getRuleSetReference());
    26  	}
    27
    28  	@Test
    29  	public void testOverride() {
    30  	    StringProperty PROPERTY1_DESCRIPTOR = new StringProperty("property1", "Test property", null, 0f);
    31  		MockRule rule = new MockRule();
    32  		rule.definePropertyDescriptor(PROPERTY1_DESCRIPTOR);
    33  		rule.setLanguage(Language.XML);
    34  		rule.setName("name1");
    35  		rule.setProperty(PROPERTY1_DESCRIPTOR, "value1");
    36  		rule.setMessage("message1");
    37  		rule.setDescription("description1");
    38  		rule.addExample("example1");
    39  		rule.setExternalInfoUrl("externalInfoUrl1");
    40  		rule.setPriority(RulePriority.HIGH);
    41
    42  		StringProperty PROPERTY2_DESCRIPTOR = new StringProperty("property2", "Test property", null, 0f);
    43  		RuleReference ruleReference = new RuleReference();
    44  		ruleReference.setRule(rule);
    45  		ruleReference.definePropertyDescriptor(PROPERTY2_DESCRIPTOR);
    46  		ruleReference.setLanguage(Language.JAVA);
    47  		ruleReference.setMinimumLanguageVersion(LanguageVersion.JAVA_13);
    48  		ruleReference.setMaximumLanguageVersion(LanguageVersion.JAVA_17);
    49  		ruleReference.setDeprecated(true);
    50  		ruleReference.setName("name2");
    51  		ruleReference.setProperty(PROPERTY1_DESCRIPTOR, "value2");
    52  		ruleReference.setProperty(PROPERTY2_DESCRIPTOR, "value3");
    53  		ruleReference.setMessage("message2");
    54  		ruleReference.setDescription("description2");
    55  		ruleReference.addExample("example2");
    56  		ruleReference.setExternalInfoUrl("externalInfoUrl2");
    57  		ruleReference.setPriority(RulePriority.MEDIUM_HIGH);
    58
    59  		assertEquals("Override failed", Language.JAVA, ruleReference.getLanguage());
    60  		assertEquals("Override failed", Language.JAVA, ruleReference.getOverriddenLanguage());
    61
    62  		assertEquals("Override failed", LanguageVersion.JAVA_13, ruleReference.getMinimumLanguageVersion());
    63  		assertEquals("Override failed", LanguageVersion.JAVA_13, ruleReference.getOverriddenMinimumLanguageVersion());
    64
    65  		assertEquals("Override failed", LanguageVersion.JAVA_17, ruleReference.getMaximumLanguageVersion());
    66  		assertEquals("Override failed", LanguageVersion.JAVA_17, ruleReference.getOverriddenMaximumLanguageVersion());
    67
    68  		assertEquals("Override failed", false, ruleReference.getRule().isDeprecated());
    69  		assertEquals("Override failed", true, ruleReference.isDeprecated());
    70  		assertEquals("Override failed", true, ruleReference.isOverriddenDeprecated());
    71
    72  		assertEquals("Override failed", "name2", ruleReference.getName());
    73  		assertEquals("Override failed", "name2", ruleReference.getOverriddenName());
    74
    75  		assertEquals("Override failed", "value2", ruleReference.getProperty(PROPERTY1_DESCRIPTOR));
    76  		assertEquals("Override failed", "value3", ruleReference.getProperty(PROPERTY2_DESCRIPTOR));
    77  		assertTrue("Override failed", ruleReference.getPropertyDescriptors().contains(PROPERTY1_DESCRIPTOR));
    78  		assertTrue("Override failed", ruleReference.getPropertyDescriptors().contains(PROPERTY2_DESCRIPTOR));
    79  		assertFalse("Override failed", ruleReference.getOverriddenPropertyDescriptors().contains(PROPERTY1_DESCRIPTOR));
    80  		assertTrue("Override failed", ruleReference.getOverriddenPropertyDescriptors().contains(PROPERTY2_DESCRIPTOR));
    81  		assertTrue("Override failed", ruleReference.getPropertiesByPropertyDescriptor().containsKey(PROPERTY1_DESCRIPTOR));
    82  		assertTrue("Override failed", ruleReference.getPropertiesByPropertyDescriptor().containsKey(PROPERTY2_DESCRIPTOR));
    83  		assertTrue("Override failed", ruleReference.getOverriddenPropertiesByPropertyDescriptor().containsKey(PROPERTY1_DESCRIPTOR));
    84  		assertTrue("Override failed", ruleReference.getOverriddenPropertiesByPropertyDescriptor().containsKey(PROPERTY2_DESCRIPTOR));
    85
    86  		assertEquals("Override failed", "message2", ruleReference.getMessage());
    87  		assertEquals("Override failed", "message2", ruleReference.getOverriddenMessage());
    88
    89  		assertEquals("Override failed", "description2", ruleReference.getDescription());
    90  		assertEquals("Override failed", "description2", ruleReference.getOverriddenDescription());
    91
    92  		assertEquals("Override failed", 2, ruleReference.getExamples().size());
    93  		assertEquals("Override failed", "example1", ruleReference.getExamples().get(0));
    94  		assertEquals("Override failed", "example2", ruleReference.getExamples().get(1));
    95  		assertEquals("Override failed", "example2", ruleReference.getOverriddenExamples().get(0));
    96
    97  		assertEquals("Override failed", "externalInfoUrl2", ruleReference.getExternalInfoUrl());
    98  		assertEquals("Override failed", "externalInfoUrl2", ruleReference.getOverriddenExternalInfoUrl());
    99
    100 		assertEquals("Override failed", RulePriority.MEDIUM_HIGH, ruleReference.getPriority());
    101 		assertEquals("Override failed", RulePriority.MEDIUM_HIGH, ruleReference.getOverriddenPriority());
    102 	}
    103
    104 	@Test
    105 	public void testNotOverride() {
    106 	    StringProperty PROPERTY1_DESCRIPTOR = new StringProperty("property1", "Test property", null, 0f);
    107 		MockRule rule = new MockRule();
    108 		rule.definePropertyDescriptor(PROPERTY1_DESCRIPTOR);
    109 		rule.setLanguage(Language.JAVA);
    110 		rule.setMinimumLanguageVersion(LanguageVersion.JAVA_13);
    111 		rule.setMaximumLanguageVersion(LanguageVersion.JAVA_17);
    112 		rule.setName("name1");
    113 		rule.setProperty(PROPERTY1_DESCRIPTOR, "value1");
    114 		rule.setMessage("message1");
    115 		rule.setDescription("description1");
    116 		rule.addExample("example1");
    117 		rule.setExternalInfoUrl("externalInfoUrl1");
    118 		rule.setPriority(RulePriority.HIGH);
    119
    120 		RuleReference ruleReference = new RuleReference();
    121 		ruleReference.setRule(rule);
    122 		ruleReference.setLanguage(Language.JAVA);
    123 		ruleReference.setMinimumLanguageVersion(LanguageVersion.JAVA_13);
    124 		ruleReference.setMaximumLanguageVersion(LanguageVersion.JAVA_17);
    125 		ruleReference.setDeprecated(false);
    126 		ruleReference.setName("name1");
    127 		ruleReference.setProperty(PROPERTY1_DESCRIPTOR, "value1");
    128 		ruleReference.setMessage("message1");
    129 		ruleReference.setDescription("description1");
    130 		ruleReference.addExample("example1");
    131 		ruleReference.setExternalInfoUrl("externalInfoUrl1");
    132 		ruleReference.setPriority(RulePriority.HIGH);
    133
    134 		assertEquals("Override failed", Language.JAVA, ruleReference.getLanguage());
    135 		assertNull("Override failed", ruleReference.getOverriddenLanguage());
    136
    137 		assertEquals("Override failed", LanguageVersion.JAVA_13, ruleReference.getMinimumLanguageVersion());
    138 		assertNull("Override failed", ruleReference.getOverriddenMinimumLanguageVersion());
    139
    140 		assertEquals("Override failed", LanguageVersion.JAVA_17, ruleReference.getMaximumLanguageVersion());
    141 		assertNull("Override failed", ruleReference.getOverriddenMaximumLanguageVersion());
    142
    143 		assertEquals("Override failed", false, ruleReference.isDeprecated());
    144 		assertNull("Override failed", ruleReference.isOverriddenDeprecated());
    145
    146 		assertEquals("Override failed", "name1", ruleReference.getName());
    147 		assertNull("Override failed", ruleReference.getOverriddenName());
    148
    149 		assertEquals("Override failed", "value1", ruleReference.getProperty(PROPERTY1_DESCRIPTOR));
    150
    151 		assertEquals("Override failed", "message1", ruleReference.getMessage());
    152 		assertNull("Override failed", ruleReference.getOverriddenMessage());
    153
    154 		assertEquals("Override failed", "description1", ruleReference.getDescription());
    155 		assertNull("Override failed", ruleReference.getOverriddenDescription());
    156
    157 		assertEquals("Override failed", 1, ruleReference.getExamples().size());
    158 		assertEquals("Override failed", "example1", ruleReference.getExamples().get(0));
    159 		assertNull("Override failed", ruleReference.getOverriddenExamples());
    160
    161 		assertEquals("Override failed", "externalInfoUrl1", ruleReference.getExternalInfoUrl());
    162 		assertNull("Override failed", ruleReference.getOverriddenExternalInfoUrl());
    163
    164 		assertEquals("Override failed", RulePriority.HIGH, ruleReference.getPriority());
    165 		assertNull("Override failed", ruleReference.getOverriddenPriority());
    166 	}
    167
    168 	public static junit.framework.Test suite() {
    169 		return new JUnit4TestAdapter(RuleReferenceTest.class);
    170 	}
    171 }