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
32 import java.io.ByteArrayInputStream;
33 import java.util.HashSet;
34 import java.util.Iterator;
35 import java.util.Set;
36
37 public class RuleSetFactoryTest extends TestCase {
38
39 public void testSingleRuleWithPriority() {
40 RuleSetFactory rsf = new RuleSetFactory();
41 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(SINGLE_RULE_SET_WITH_PRIORITY.getBytes()));
42 Rule r = (Rule)rs.getRules().iterator().next();
43 assertEquals(3, r.getPriority());
44 }
45
46 public void testRuleSetNotFound() {
47 RuleSetFactory rsf = new RuleSetFactory();
48 try {
49 rsf.createRuleSet("fooooo");
50 throw new RuntimeException("Should have thrown a RuleSetNotFoundException");
51 } catch (RuleSetNotFoundException rsnfe) {
52
53 }
54 }
55
56 public void testCreateEmptyRuleSet() {
57 RuleSetFactory rsf = new RuleSetFactory();
58 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(EMPTY_RULE_SET.getBytes()));
59 assertEquals("test", rs.getName());
60 assertEquals(0, rs.size());
61 }
62
63 public void testSingleRule() {
64 RuleSetFactory rsf = new RuleSetFactory();
65 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(SINGLE_RULE_SET.getBytes()));
66 assertEquals(1, rs.size());
67 Rule r = (Rule)rs.getRules().iterator().next();
68 assertEquals("MockRuleName", r.getName());
69 assertEquals("avoid the mock rule", r.getMessage());
70 }
71
72 public void testMultipleRules() {
73 RuleSetFactory rsf = new RuleSetFactory();
74 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(MULTIPLE_RULE_SET.getBytes()));
75 assertEquals(2, rs.size());
76 Set expected = new HashSet();
77 expected.add("MockRuleName1");
78 expected.add("MockRuleName2");
79 for (Iterator i = rs.getRules().iterator(); i.hasNext();) {
80 assertTrue(expected.contains(((Rule) i.next()).getName()));
81 }
82 }
83
84 public void testProps() {
85 RuleSetFactory rsf = new RuleSetFactory();
86 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(RULE_WITH_PROPERTIES.getBytes()));
87 Rule r = (Rule) rs.getRules().iterator().next();
88 assertTrue(r.hasProperty("foo"));
89 assertEquals("bar", r.getStringProperty("foo"));
90 assertEquals(2, r.getIntProperty("fooint"));
91 assertTrue(r.hasProperty("fooBoolean"));
92 assertTrue(r.getBooleanProperty("fooBoolean"));
93 assertTrue(r.hasProperty("fooDouble"));
94 assertEquals(1.0, r.getDoubleProperty("fooDouble"), 0.05);
95 assertTrue(!r.hasProperty("BuggleFish"));
96 assertTrue(r.getDescription().indexOf("testdesc2") != -1);
97 }
98
99 public void testXPathPluginnameProperty() {
100 RuleSetFactory rsf = new RuleSetFactory();
101 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(RULE_WITH_XPATH_AND_PLUGINNAME.getBytes()));
102 Rule r = (Rule) rs.getRules().iterator().next();
103 assertTrue(r.hasProperty("pluginname"));
104 }
105
106 public void testXPath() {
107 RuleSetFactory rsf = new RuleSetFactory();
108 RuleSet rs = rsf.createRuleSet(new ByteArrayInputStream(RULE_WITH_XPATH.getBytes()));
109 Rule r = (Rule) rs.getRules().iterator().next();
110 assertTrue(r.hasProperty("xpath"));
111 assertTrue(r.getStringProperty("xpath").indexOf(" //Block ") != -1);
112 }
113
114 private static final String EMPTY_RULE_SET =
115 "<?xml version=\"1.0\"?>" + PMD.EOL +
116 "<ruleset name=\"test\">" + PMD.EOL +
117 "<description>testdesc</description>" + PMD.EOL +
118 "</ruleset>";
119
120 private static final String SINGLE_RULE_SET =
121 "<?xml version=\"1.0\"?>" + PMD.EOL +
122 "<ruleset name=\"test\">" + PMD.EOL +
123 "<description>" + PMD.EOL +
124 "testdesc" + PMD.EOL +
125 "</description>" + PMD.EOL +
126 "<rule " + PMD.EOL +
127 "name=\"MockRuleName\" " + PMD.EOL +
128 "message=\"avoid the mock rule\" " + PMD.EOL +
129 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" +
130 "</rule></ruleset>";
131
132 private static final String MULTIPLE_RULE_SET =
133 "<?xml version=\"1.0\"?>" + PMD.EOL +
134 "<ruleset name=\"test\">" + PMD.EOL +
135 "<description>" + PMD.EOL +
136 "testdesc" + PMD.EOL + "</description>" + PMD.EOL +
137 "<rule name=\"MockRuleName1\" " + PMD.EOL +
138 "message=\"avoid the mock rule\" " + PMD.EOL +
139 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
140 "</rule>" + PMD.EOL +
141 "<rule name=\"MockRuleName2\" " + PMD.EOL +
142 "message=\"avoid the mock rule\" " + PMD.EOL +
143 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
144 "</rule></ruleset>";
145
146 private static final String RULE_WITH_PROPERTIES =
147 "<?xml version=\"1.0\"?>" + PMD.EOL +
148 "<ruleset name=\"test\">" + PMD.EOL +
149 "<description>" + PMD.EOL +
150 "testdesc" + PMD.EOL +
151 "</description>" + PMD.EOL +
152 "<rule name=\"MockRuleName\" " + PMD.EOL +
153 "message=\"avoid the mock rule\" " + PMD.EOL +
154 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
155 "<description>" + PMD.EOL + "testdesc2" + PMD.EOL +
156 "</description>" + PMD.EOL +
157 "<properties>" + PMD.EOL +
158 "<property name=\"fooBoolean\" value=\"true\"/>" + PMD.EOL +
159 "<property name=\"fooDouble\" value=\"1.0\" />" + PMD.EOL +
160 "<property name=\"foo\" value=\"bar\"/>" + PMD.EOL +
161 "<property name=\"fooint\" value=\"2\"/>" + PMD.EOL +
162 "</properties>" + PMD.EOL +
163 "</rule></ruleset>";
164
165 private static final String RULE_WITH_XPATH =
166 "<?xml version=\"1.0\"?>" + PMD.EOL +
167 "<ruleset name=\"test\">" + PMD.EOL +
168 "<description>" + PMD.EOL +
169 "testdesc" + PMD.EOL +
170 "</description>" + PMD.EOL +
171 "<priority>3</priority>" + PMD.EOL +
172 "<rule name=\"MockRuleName\" " + PMD.EOL +
173 "message=\"avoid the mock rule\" " + PMD.EOL +
174 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
175 "<description>" + PMD.EOL +
176 "testdesc2" + PMD.EOL +
177 "</description>" + PMD.EOL +
178 "<properties>" + PMD.EOL +
179 "<property name=\"xpath\">" + PMD.EOL +
180 "<value>" + PMD.EOL +
181 "<![CDATA[ //Block ]]>" + PMD.EOL +
182 "</value>" + PMD.EOL +
183 "</property>" + PMD.EOL +
184 "</properties>" + PMD.EOL +
185 "</rule></ruleset>";
186
187 private static final String RULE_WITH_XPATH_AND_PLUGINNAME =
188 "<?xml version=\"1.0\"?>" + PMD.EOL +
189 "<ruleset name=\"test\">" + PMD.EOL +
190 "<description>" + PMD.EOL +
191 "testdesc" + PMD.EOL +
192 "</description>" + PMD.EOL +
193 "<priority>3</priority>" + PMD.EOL +
194 "<rule name=\"MockRuleName\" " + PMD.EOL +
195 "message=\"avoid the mock rule\" " + PMD.EOL +
196 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" + PMD.EOL +
197 "<description>" + PMD.EOL +
198 "testdesc2" + PMD.EOL +
199 "</description>" + PMD.EOL +
200 "<properties>" + PMD.EOL +
201 "<property name=\"xpath\" pluginname=\"true\">" + PMD.EOL +
202 "<value>" + PMD.EOL +
203 "<![CDATA[ //Block ]]>" + PMD.EOL +
204 "</value>" + PMD.EOL +
205 "</property>" + PMD.EOL +
206 "</properties>" + PMD.EOL +
207 "</rule></ruleset>";
208
209
210 private static final String SINGLE_RULE_SET_WITH_PRIORITY =
211 "<?xml version=\"1.0\"?>" + PMD.EOL +
212 "<ruleset name=\"test\">" + PMD.EOL +
213 "<description>" + PMD.EOL +
214 "testdesc" + PMD.EOL +
215 "</description>" + PMD.EOL +
216 "<rule " + PMD.EOL +
217 "name=\"MockRuleName\" " + PMD.EOL +
218 "message=\"avoid the mock rule\" " + PMD.EOL +
219 "class=\"test.net.sourceforge.pmd.testframework.MockRule\">" +
220 "<priority>3</priority>" + PMD.EOL +
221 "</rule></ruleset>";
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 }