KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > net > sourceforge > pmd > RuleSetFactoryTest


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 JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Set JavaDoc;
38
39 public class RuleSetFactoryTest extends TestCase {
40
41     public void testRefs() throws Throwable JavaDoc {
42         InputStream JavaDoc 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.createSingleRuleSet("rulesets/favorites.xml");
48         assertNotNull(rs.getRuleByName("WhileLoopsMustUseBraces"));
49     }
50
51     public void testRuleSetNotFound() {
52         RuleSetFactory rsf = new RuleSetFactory();
53         try {
54             rsf.createSingleRuleSet("fooooo");
55             fail("Should have thrown a RuleSetNotFoundException");
56         } catch (RuleSetNotFoundException rsnfe) {
57             // cool
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 JavaDoc();
79         expected.add("MockRuleName1");
80         expected.add("MockRuleName2");
81         for (Iterator JavaDoc 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 JavaDoc iae) {
153             // cool
154
}
155     }
156
157     public void testSetPriority() {
158         RuleSetFactory rsf = new RuleSetFactory();
159         rsf.setMinimumPriority(2);
160         assertEquals(0, rsf.createRuleSet(new ByteArrayInputStream JavaDoc(SINGLE_RULE.getBytes())).size());
161         rsf.setMinimumPriority(4);
162         assertEquals(1, rsf.createRuleSet(new ByteArrayInputStream JavaDoc(SINGLE_RULE.getBytes())).size());
163     }
164
165     private static final String JavaDoc REF_OVERRIDE_ORIGINAL_NAME =
166             "<?xml version=\"1.0\"?>" + PMD.EOL +
167             "<ruleset name=\"test\">" + PMD.EOL +
168             " <description>testdesc</description>" + PMD.EOL +
169             " <rule " + PMD.EOL +
170             " ref=\"rulesets/unusedcode.xml/UnusedLocalVariable\" message=\"TestMessageOverride\"> " + PMD.EOL +
171             " </rule>" + PMD.EOL +
172             "</ruleset>";
173
174     private static final String JavaDoc REF_MISPELLED_XREF =
175             "<?xml version=\"1.0\"?>" + PMD.EOL +
176             "<ruleset name=\"test\">" + PMD.EOL +
177             " <description>testdesc</description>" + PMD.EOL +
178             " <rule " + PMD.EOL +
179             " ref=\"rulesets/unusedcode.xml/FooUnusedLocalVariable\"> " + PMD.EOL +
180             " </rule>" + PMD.EOL +
181             "</ruleset>";
182
183     private static final String JavaDoc REF_OVERRIDE_ORIGINAL_NAME_ONE_ELEM =
184             "<?xml version=\"1.0\"?>" + PMD.EOL +
185             "<ruleset name=\"test\">" + PMD.EOL +
186             " <description>testdesc</description>" + PMD.EOL +
187             " <rule ref=\"rulesets/unusedcode.xml/UnusedLocalVariable\" message=\"TestMessageOverride\"/> " + PMD.EOL +
188             "</ruleset>";
189
190     private static final String JavaDoc REF_OVERRIDE =
191             "<?xml version=\"1.0\"?>" + PMD.EOL +
192             "<ruleset name=\"test\">" + PMD.EOL +
193             " <description>testdesc</description>" + PMD.EOL +
194             " <rule " + PMD.EOL +
195             " ref=\"rulesets/unusedcode.xml/UnusedLocalVariable\" " + PMD.EOL +
196             " name=\"TestNameOverride\" " + PMD.EOL +
197             " message=\"Test message override\"> " + PMD.EOL +
198             " <description>Test description override</description>" + PMD.EOL +
199             " <example>Test example override</example>" + PMD.EOL +
200             " <priority>3</priority>" + PMD.EOL +
201             " <properties>" + PMD.EOL +
202             " <property name=\"test2\" value=\"override2\"/>" + PMD.EOL +
203             " <property name=\"test3\"><value>override3</value></property>" + PMD.EOL +
204             " <property name=\"test4\" value=\"new property\"/>" + PMD.EOL +
205             " </properties>" + PMD.EOL +
206             " </rule>" + PMD.EOL +
207             "</ruleset>";
208
209     private static final String JavaDoc EMPTY_RULESET =
210             "<?xml version=\"1.0\"?>" + PMD.EOL +
211             "<ruleset name=\"test\">" + PMD.EOL +
212             "<description>testdesc</description>" + PMD.EOL +
213             "</ruleset>";
214
215     private static final String JavaDoc SINGLE_RULE =
216             "<?xml version=\"1.0\"?>" + PMD.EOL +
217             "<ruleset name=\"test\">" + PMD.EOL +
218             "<description>testdesc</description>" + PMD.EOL +
219             "<rule " + PMD.EOL +
220             "name=\"MockRuleName\" " + PMD.EOL +
221             "message=\"avoid the mock rule\" " + PMD.EOL +
222             "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" +
223             "<priority>3</priority>" + PMD.EOL +
224             "</rule></ruleset>";
225
226     private static final String JavaDoc MULTIPLE_RULES =
227             "<?xml version=\"1.0\"?>" + PMD.EOL +
228             "<ruleset name=\"test\">" + PMD.EOL +
229             "<description>testdesc</description>" + PMD.EOL +
230             "<rule name=\"MockRuleName1\" " + PMD.EOL +
231             "message=\"avoid the mock rule\" " + PMD.EOL +
232             "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
233             "</rule>" + PMD.EOL +
234             "<rule name=\"MockRuleName2\" " + PMD.EOL +
235             "message=\"avoid the mock rule\" " + PMD.EOL +
236             "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
237             "</rule></ruleset>";
238
239     private static final String JavaDoc PROPERTIES =
240             "<?xml version=\"1.0\"?>" + PMD.EOL +
241             "<ruleset name=\"test\">" + PMD.EOL +
242             "<description>testdesc</description>" + PMD.EOL +
243             "<rule name=\"MockRuleName\" " + PMD.EOL +
244             "message=\"avoid the mock rule\" " + PMD.EOL +
245             "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
246             "<description>testdesc2</description>" + PMD.EOL +
247             "<properties>" + PMD.EOL +
248             "<property name=\"fooBoolean\" value=\"true\"/>" + PMD.EOL +
249             "<property name=\"fooDouble\" value=\"1.0\" />" + PMD.EOL +
250             "<property name=\"foo\" value=\"bar\"/>" + PMD.EOL +
251             "<property name=\"fooint\" value=\"2\"/>" + PMD.EOL +
252             "</properties>" + PMD.EOL +
253             "</rule></ruleset>";
254
255     private static final String JavaDoc XPATH =
256             "<?xml version=\"1.0\"?>" + PMD.EOL +
257             "<ruleset name=\"test\">" + PMD.EOL +
258             "<description>testdesc</description>" + PMD.EOL +
259             "<priority>3</priority>" + PMD.EOL +
260             "<rule name=\"MockRuleName\" " + PMD.EOL +
261             "message=\"avoid the mock rule\" " + PMD.EOL +
262             "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
263             "<description>testdesc2</description>" + PMD.EOL +
264             "<properties>" + PMD.EOL +
265             "<property name=\"xpath\">" + PMD.EOL +
266             "<value>" + PMD.EOL +
267             "<![CDATA[ //Block ]]>" + PMD.EOL +
268             "</value>" + PMD.EOL +
269             "</property>" + PMD.EOL +
270             "</properties>" + PMD.EOL +
271             "</rule></ruleset>";
272
273     private static final String JavaDoc XPATH_PLUGINNAME =
274             "<?xml version=\"1.0\"?>" + PMD.EOL +
275             "<ruleset name=\"test\">" + PMD.EOL +
276             "<description>testdesc</description>" + PMD.EOL +
277             "<priority>3</priority>" + PMD.EOL +
278             "<rule name=\"MockRuleName\" " + PMD.EOL +
279             "message=\"avoid the mock rule\" " + PMD.EOL +
280             "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
281             "<description>testdesc2</description>" + PMD.EOL +
282             "<properties>" + PMD.EOL +
283             "<property name=\"xpath\" pluginname=\"true\">" + PMD.EOL +
284             "<value>" + PMD.EOL +
285             "<![CDATA[ //Block ]]>" + PMD.EOL +
286             "</value>" + PMD.EOL +
287             "</property>" + PMD.EOL +
288             "</properties>" + PMD.EOL +
289             "</rule></ruleset>";
290
291
292     private static final String JavaDoc PRIORITY =
293             "<?xml version=\"1.0\"?>" + PMD.EOL +
294             "<ruleset name=\"test\">" + PMD.EOL +
295             "<description>testdesc</description>" + PMD.EOL +
296             "<rule " + PMD.EOL +
297             "name=\"MockRuleName\" " + PMD.EOL +
298             "message=\"avoid the mock rule\" " + PMD.EOL +
299             "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" +
300             "<priority>3</priority>" + PMD.EOL +
301             "</rule></ruleset>";
302
303     private static final String JavaDoc DFA =
304             "<?xml version=\"1.0\"?>" + PMD.EOL +
305             "<ruleset name=\"test\">" + PMD.EOL +
306             "<description>testdesc</description>" + PMD.EOL +
307             "<rule " + PMD.EOL +
308             "name=\"MockRuleName\" " + PMD.EOL +
309             "message=\"avoid the mock rule\" " + PMD.EOL +
310             "dfa=\"true\" " + PMD.EOL +
311             "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" +
312             "<priority>3</priority>" + PMD.EOL +
313             "</rule></ruleset>";
314
315
316     private Rule loadFirstRule(String JavaDoc ruleSetName) {
317         RuleSet rs = loadRuleSet(ruleSetName);
318         return ((Rule) (rs.getRules().iterator().next()));
319     }
320
321     private RuleSet loadRuleSet(String JavaDoc ruleSetName) {
322         RuleSetFactory rsf = new RuleSetFactory();
323         return rsf.createRuleSet(new ByteArrayInputStream JavaDoc(ruleSetName.getBytes()));
324     }
325 /*
326     public void testExternalReferences() {
327         RuleSet rs = loadRuleSet(EXTERNAL_REFERENCE_RULE_SET);
328         assertEquals(1, rs.size());
329         assertEquals(UnusedLocalVariableRule.class, rs.getRuleByName("UnusedLocalVariable").getClass());
330     }
331         private static final String EXTERNAL_REFERENCE_RULE_SET =
332                 "<?xml version=\"1.0\"?>" + PMD.EOL +
333                 "<ruleset name=\"test\">" + PMD.EOL +
334                 "<description>testdesc</description>" + PMD.EOL +
335                 "<rule ref=\"rulesets/unusedcode.xml/UnusedLocalVariable\"/>" + PMD.EOL +
336                 "</ruleset>";
337 */

338 }
339
Popular Tags