1 /***
2 * <copyright>
3 * Copyright 1997-2002 InfoEther, LLC
4 * under sponsorship of the Defense Advanced Research Projects Agency
5 (DARPA).
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the Cougaar Open Source License as published
9 by
10 * DARPA on the Cougaar Open Source Website (www.cougaar.org).
11 *
12 * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
13 * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
14 * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
16 * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
17 * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
18 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
19 * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THE COUGAAR SOFTWARE.
21 * </copyright>
22 */
23 package test.net.sourceforge.pmd;
24
25 import junit.framework.TestCase;
26 import net.sourceforge.pmd.PMD;
27 import net.sourceforge.pmd.Rule;
28 import net.sourceforge.pmd.RuleSet;
29 import net.sourceforge.pmd.RuleSetFactory;
30 import net.sourceforge.pmd.RuleSetNotFoundException;
31 import net.sourceforge.pmd.util.ResourceLoader;
32
33 import java.io.ByteArrayInputStream;
34 import java.io.InputStream;
35 import java.util.HashSet;
36 import java.util.Iterator;
37 import java.util.Set;
38
39 public class RuleSetFactoryTest extends TestCase {
40
41 public void testRefs() throws Throwable {
42 InputStream in = ResourceLoader.loadResourceAsStream("rulesets/favorites.xml", this.getClass().getClassLoader());
43 if (in == null) {
44 throw new RuleSetNotFoundException("Can't find resource Make sure the resource is a valid file or URL or is on the CLASSPATH. Here's the current classpath: " + System.getProperty("java.class.path"));
45 }
46 RuleSetFactory rsf = new RuleSetFactory();
47 RuleSet rs = rsf.createRuleSet("rulesets/favorites.xml");
48 assertNotNull(rs.getRuleByName("WhileLoopsMustUseBraces"));
49 }
50
51 public void testRuleSetNotFound() {
52 RuleSetFactory rsf = new RuleSetFactory();
53 try {
54 rsf.createRuleSet("fooooo");
55 fail("Should have thrown a RuleSetNotFoundException");
56 } catch (RuleSetNotFoundException rsnfe) {
57
58 }
59 }
60
61 public void testCreateEmptyRuleSet() {
62 RuleSet rs = loadRuleSet(EMPTY_RULESET);
63 assertEquals("test", rs.getName());
64 assertEquals(0, rs.size());
65 }
66
67 public void testSingleRule() {
68 RuleSet rs = loadRuleSet(SINGLE_RULE);
69 assertEquals(1, rs.size());
70 Rule r = (Rule)rs.getRules().iterator().next();
71 assertEquals("MockRuleName", r.getName());
72 assertEquals("avoid the mock rule", r.getMessage());
73 }
74
75 public void testMultipleRules() {
76 RuleSet rs = loadRuleSet(MULTIPLE_RULES);
77 assertEquals(2, rs.size());
78 Set expected = new HashSet();
79 expected.add("MockRuleName1");
80 expected.add("MockRuleName2");
81 for (Iterator i = rs.getRules().iterator(); i.hasNext();) {
82 assertTrue(expected.contains(((Rule) i.next()).getName()));
83 }
84 }
85
86 public void testSingleRuleWithPriority() {
87 assertEquals(3, loadFirstRule(PRIORITY).getPriority());
88 }
89
90 public void testProps() {
91 Rule r = loadFirstRule(PROPERTIES);
92 assertTrue(r.hasProperty("foo"));
93 assertEquals("bar", r.getStringProperty("foo"));
94 assertEquals(2, r.getIntProperty("fooint"));
95 assertTrue(r.hasProperty("fooBoolean"));
96 assertTrue(r.getBooleanProperty("fooBoolean"));
97 assertTrue(r.hasProperty("fooDouble"));
98 assertEquals(1.0, r.getDoubleProperty("fooDouble"), 0.05);
99 assertTrue(!r.hasProperty("BuggleFish"));
100 assertTrue(r.getDescription().indexOf("testdesc2") != -1);
101 }
102
103 public void testXPathPluginnameProperty() {
104 Rule r = loadFirstRule(XPATH_PLUGINNAME);
105 assertTrue(r.hasProperty("pluginname"));
106 }
107
108 public void testXPath() {
109 Rule r = loadFirstRule(XPATH);
110 assertTrue(r.hasProperty("xpath"));
111 assertTrue(r.getStringProperty("xpath").indexOf(" //Block ") != -1);
112 }
113
114 public void testFacadesOffByDefault() {
115 Rule r = loadFirstRule(XPATH);
116 assertFalse(r.usesDFA());
117 }
118
119 public void testDFAFlag() {
120 assertTrue(loadFirstRule(DFA).usesDFA());
121 }
122
123 public void testExternalReferenceOverride() {
124 Rule r = loadFirstRule(REF_OVERRIDE);
125 assertEquals("TestNameOverride", r.getName());
126 assertEquals("Test message override", r.getMessage());
127 assertEquals("Test description override", r.getDescription());
128 assertEquals("Test example override", r.getExample());
129 assertEquals(3, r.getPriority());
130 assertTrue(r.hasProperty("test2"));
131 assertEquals("override2", r.getStringProperty("test2"));
132 assertTrue(r.hasProperty("test3"));
133 assertEquals("override3", r.getStringProperty("test3"));
134 assertTrue(r.hasProperty("test4"));
135 assertEquals("new property", r.getStringProperty("test4"));
136 }
137
138 public void testOverrideMessage() {
139 Rule r = loadFirstRule(REF_OVERRIDE_ORIGINAL_NAME);
140 assertEquals("TestMessageOverride", r.getMessage());
141 }
142
143 public void testOverrideMessageOneElem() {
144 Rule r = loadFirstRule(REF_OVERRIDE_ORIGINAL_NAME_ONE_ELEM);
145 assertEquals("TestMessageOverride", r.getMessage());
146 }
147
148 public void testExternalRef() {
149 try {
150 loadFirstRule(REF_MISPELLED_XREF);
151 fail("Whoa, should have gotten an IllegalArgumentException");
152 } catch (IllegalArgumentException iae) {
153
154 }
155
156 }
157
158 private static final String REF_OVERRIDE_ORIGINAL_NAME =
159 "<?xml version=\"1.0\"?>" + PMD.EOL +
160 "<ruleset name=\"test\">" + PMD.EOL +
161 " <description>testdesc</description>" + PMD.EOL +
162 " <rule " + PMD.EOL +
163 " ref=\"rulesets/unusedcode.xml/UnusedLocalVariable\" message=\"TestMessageOverride\"> " + PMD.EOL +
164 " </rule>" + PMD.EOL +
165 "</ruleset>";
166
167 private static final String REF_MISPELLED_XREF =
168 "<?xml version=\"1.0\"?>" + PMD.EOL +
169 "<ruleset name=\"test\">" + PMD.EOL +
170 " <description>testdesc</description>" + PMD.EOL +
171 " <rule " + PMD.EOL +
172 " ref=\"rulesets/unusedcode.xml/FooUnusedLocalVariable\"> " + PMD.EOL +
173 " </rule>" + PMD.EOL +
174 "</ruleset>";
175
176 private static final String REF_OVERRIDE_ORIGINAL_NAME_ONE_ELEM =
177 "<?xml version=\"1.0\"?>" + PMD.EOL +
178 "<ruleset name=\"test\">" + PMD.EOL +
179 " <description>testdesc</description>" + PMD.EOL +
180 " <rule ref=\"rulesets/unusedcode.xml/UnusedLocalVariable\" message=\"TestMessageOverride\"/> " + PMD.EOL +
181 "</ruleset>";
182
183 private static final String REF_OVERRIDE =
184 "<?xml version=\"1.0\"?>" + PMD.EOL +
185 "<ruleset name=\"test\">" + PMD.EOL +
186 " <description>testdesc</description>" + PMD.EOL +
187 " <rule " + PMD.EOL +
188 " ref=\"rulesets/unusedcode.xml/UnusedLocalVariable\" " + PMD.EOL +
189 " name=\"TestNameOverride\" " + PMD.EOL +
190 " message=\"Test message override\"> " + PMD.EOL +
191 " <description>Test description override</description>" + PMD.EOL +
192 " <example>Test example override</example>" + PMD.EOL +
193 " <priority>3</priority>" + PMD.EOL +
194 " <properties>" + PMD.EOL +
195 " <property name=\"test2\" value=\"override2\"/>" + PMD.EOL +
196 " <property name=\"test3\"><value>override3</value></property>" + PMD.EOL +
197 " <property name=\"test4\" value=\"new property\"/>" + PMD.EOL +
198 " </properties>" + PMD.EOL +
199 " </rule>" + PMD.EOL +
200 "</ruleset>";
201
202 private static final String EMPTY_RULESET =
203 "<?xml version=\"1.0\"?>" + PMD.EOL +
204 "<ruleset name=\"test\">" + PMD.EOL +
205 "<description>testdesc</description>" + PMD.EOL +
206 "</ruleset>";
207
208 private static final String SINGLE_RULE =
209 "<?xml version=\"1.0\"?>" + PMD.EOL +
210 "<ruleset name=\"test\">" + PMD.EOL +
211 "<description>testdesc</description>" + PMD.EOL +
212 "<rule " + PMD.EOL +
213 "name=\"MockRuleName\" " + PMD.EOL +
214 "message=\"avoid the mock rule\" " + PMD.EOL +
215 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" +
216 "</rule></ruleset>";
217
218 private static final String MULTIPLE_RULES =
219 "<?xml version=\"1.0\"?>" + PMD.EOL +
220 "<ruleset name=\"test\">" + PMD.EOL +
221 "<description>testdesc</description>" + PMD.EOL +
222 "<rule name=\"MockRuleName1\" " + PMD.EOL +
223 "message=\"avoid the mock rule\" " + PMD.EOL +
224 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
225 "</rule>" + PMD.EOL +
226 "<rule name=\"MockRuleName2\" " + PMD.EOL +
227 "message=\"avoid the mock rule\" " + PMD.EOL +
228 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
229 "</rule></ruleset>";
230
231 private static final String PROPERTIES =
232 "<?xml version=\"1.0\"?>" + PMD.EOL +
233 "<ruleset name=\"test\">" + PMD.EOL +
234 "<description>testdesc</description>" + PMD.EOL +
235 "<rule name=\"MockRuleName\" " + PMD.EOL +
236 "message=\"avoid the mock rule\" " + PMD.EOL +
237 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
238 "<description>testdesc2</description>" + PMD.EOL +
239 "<properties>" + PMD.EOL +
240 "<property name=\"fooBoolean\" value=\"true\"/>" + PMD.EOL +
241 "<property name=\"fooDouble\" value=\"1.0\" />" + PMD.EOL +
242 "<property name=\"foo\" value=\"bar\"/>" + PMD.EOL +
243 "<property name=\"fooint\" value=\"2\"/>" + PMD.EOL +
244 "</properties>" + PMD.EOL +
245 "</rule></ruleset>";
246
247 private static final String XPATH =
248 "<?xml version=\"1.0\"?>" + PMD.EOL +
249 "<ruleset name=\"test\">" + PMD.EOL +
250 "<description>testdesc</description>" + PMD.EOL +
251 "<priority>3</priority>" + PMD.EOL +
252 "<rule name=\"MockRuleName\" " + PMD.EOL +
253 "message=\"avoid the mock rule\" " + PMD.EOL +
254 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
255 "<description>testdesc2</description>" + PMD.EOL +
256 "<properties>" + PMD.EOL +
257 "<property name=\"xpath\">" + PMD.EOL +
258 "<value>" + PMD.EOL +
259 "<![CDATA[ //Block ]]>" + PMD.EOL +
260 "</value>" + PMD.EOL +
261 "</property>" + PMD.EOL +
262 "</properties>" + PMD.EOL +
263 "</rule></ruleset>";
264
265 private static final String XPATH_PLUGINNAME =
266 "<?xml version=\"1.0\"?>" + PMD.EOL +
267 "<ruleset name=\"test\">" + PMD.EOL +
268 "<description>testdesc</description>" + PMD.EOL +
269 "<priority>3</priority>" + PMD.EOL +
270 "<rule name=\"MockRuleName\" " + PMD.EOL +
271 "message=\"avoid the mock rule\" " + PMD.EOL +
272 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
273 "<description>testdesc2</description>" + PMD.EOL +
274 "<properties>" + PMD.EOL +
275 "<property name=\"xpath\" pluginname=\"true\">" + PMD.EOL +
276 "<value>" + PMD.EOL +
277 "<![CDATA[ //Block ]]>" + PMD.EOL +
278 "</value>" + PMD.EOL +
279 "</property>" + PMD.EOL +
280 "</properties>" + PMD.EOL +
281 "</rule></ruleset>";
282
283
284 private static final String PRIORITY =
285 "<?xml version=\"1.0\"?>" + PMD.EOL +
286 "<ruleset name=\"test\">" + PMD.EOL +
287 "<description>testdesc</description>" + PMD.EOL +
288 "<rule " + PMD.EOL +
289 "name=\"MockRuleName\" " + PMD.EOL +
290 "message=\"avoid the mock rule\" " + PMD.EOL +
291 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" +
292 "<priority>3</priority>" + PMD.EOL +
293 "</rule></ruleset>";
294
295 private static final String DFA =
296 "<?xml version=\"1.0\"?>" + PMD.EOL +
297 "<ruleset name=\"test\">" + PMD.EOL +
298 "<description>testdesc</description>" + PMD.EOL +
299 "<rule " + PMD.EOL +
300 "name=\"MockRuleName\" " + PMD.EOL +
301 "message=\"avoid the mock rule\" " + PMD.EOL +
302 "dfa=\"true\" " + PMD.EOL +
303 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" +
304 "<priority>3</priority>" + PMD.EOL +
305 "</rule></ruleset>";
306
307
308 private Rule loadFirstRule(String ruleSetName) {
309 RuleSet rs = loadRuleSet(ruleSetName);
310 return ((Rule)(rs.getRules().iterator().next()));
311 }
312
313 private RuleSet loadRuleSet(String ruleSetName) {
314 RuleSetFactory rsf = new RuleSetFactory();
315 return rsf.createRuleSet(new ByteArrayInputStream(ruleSetName.getBytes()));
316 }
317
318
319
320
321
322
323
324
325
326
327
328
329
330 }