View Javadoc

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