001: /**
002: * <copyright>
003: * Copyright 1997-2002 InfoEther, LLC
004: * under sponsorship of the Defense Advanced Research Projects Agency
005: (DARPA).
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the Cougaar Open Source License as published
009: by
010: * DARPA on the Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
013: * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
014: * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
015: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
016: * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
017: * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
018: * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
019: * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
020: * PERFORMANCE OF THE COUGAAR SOFTWARE.
021: * </copyright>
022: */package test.net.sourceforge.pmd;
023:
024: import static org.junit.Assert.assertEquals;
025: import static org.junit.Assert.assertFalse;
026: import static org.junit.Assert.assertNotNull;
027: import static org.junit.Assert.assertNull;
028: import static org.junit.Assert.assertTrue;
029:
030: import java.io.File;
031: import java.io.StringReader;
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.HashSet;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.Set;
038:
039: import net.sourceforge.pmd.MockRule;
040: import net.sourceforge.pmd.Report;
041: import net.sourceforge.pmd.Rule;
042: import net.sourceforge.pmd.RuleContext;
043: import net.sourceforge.pmd.RuleReference;
044: import net.sourceforge.pmd.RuleSet;
045: import net.sourceforge.pmd.RuleViolation;
046: import net.sourceforge.pmd.TargetJDKVersion;
047: import net.sourceforge.pmd.ast.ASTCompilationUnit;
048: import net.sourceforge.pmd.ast.JavaParser;
049:
050: import org.junit.Test;
051:
052: public class RuleSetTest {
053:
054: private String javaCode = "public class Test { }";
055:
056: @Test
057: public void testNoDFA() {
058: RuleSet rs = new RuleSet();
059: MockRule mock = new MockRule("name", "desc", "msg",
060: "rulesetname");
061: rs.addRule(mock);
062: assertFalse(rs.usesDFA());
063: }
064:
065: @Test
066: public void testIncludesRuleWithDFA() {
067: RuleSet rs = new RuleSet();
068: MockRule mock = new MockRule("name", "desc", "msg",
069: "rulesetname");
070: mock.setUsesDFA();
071: rs.addRule(mock);
072: assertTrue(rs.usesDFA());
073: }
074:
075: @Test
076: public void testAccessors() {
077: RuleSet rs = new RuleSet();
078: rs.setFileName("baz");
079: assertEquals("file name mismatch", "baz", rs.getFileName());
080: rs.setName("foo");
081: assertEquals("name mismatch", "foo", rs.getName());
082: rs.setDescription("bar");
083: assertEquals("description mismatch", "bar", rs.getDescription());
084: }
085:
086: @Test
087: public void testGetRuleByName() {
088: RuleSet rs = new RuleSet();
089: MockRule mock = new MockRule("name", "desc", "msg",
090: "rulesetname");
091: rs.addRule(mock);
092: assertEquals("unable to fetch rule by name", mock, rs
093: .getRuleByName("name"));
094: }
095:
096: @Test
097: public void testGetRuleByName2() {
098: RuleSet rs = new RuleSet();
099: MockRule mock = new MockRule("name", "desc", "msg",
100: "rulesetname");
101: rs.addRule(mock);
102: assertNull("the rule FooRule must not be found!", rs
103: .getRuleByName("FooRule"));
104: }
105:
106: @Test
107: public void testRuleList() {
108: RuleSet IUT = new RuleSet();
109:
110: assertEquals("Size of RuleSet isn't zero.", 0, IUT.size());
111:
112: MockRule rule = new MockRule("name", "desc", "msg",
113: "rulesetname");
114: IUT.addRule(rule);
115:
116: assertEquals("Size of RuleSet isn't one.", 1, IUT.size());
117:
118: Collection rules = IUT.getRules();
119:
120: Iterator i = rules.iterator();
121: assertTrue("Empty Set", i.hasNext());
122: assertEquals("Returned set of wrong size.", 1, rules.size());
123: assertEquals("Rule isn't in ruleset.", rule, i.next());
124: }
125:
126: @Test
127: public void testAddRuleSet() {
128: RuleSet set1 = new RuleSet();
129: set1
130: .addRule(new MockRule("name", "desc", "msg",
131: "rulesetname"));
132: RuleSet set2 = new RuleSet();
133: set2
134: .addRule(new MockRule("name2", "desc", "msg",
135: "rulesetname"));
136: set1.addRuleSet(set2);
137: assertEquals("ruleset size wrong", 2, set1.size());
138: }
139:
140: @Test(expected=RuntimeException.class)
141: public void testAddRuleSetByReferenceBad() {
142: RuleSet set1 = new RuleSet();
143: set1
144: .addRule(new MockRule("name", "desc", "msg",
145: "rulesetname"));
146: RuleSet set2 = new RuleSet();
147: set2
148: .addRule(new MockRule("name2", "desc", "msg",
149: "rulesetname"));
150: set1.addRuleSetByReference(set2, false);
151: }
152:
153: @Test
154: public void testAddRuleSetByReferenceAllRule() {
155: RuleSet set1 = new RuleSet();
156: RuleSet set2 = new RuleSet();
157: set2.setFileName("foo");
158: set2
159: .addRule(new MockRule("name", "desc", "msg",
160: "rulesetname"));
161: set2
162: .addRule(new MockRule("name2", "desc", "msg",
163: "rulesetname"));
164: set1.addRuleSetByReference(set2, true);
165: assertEquals("wrong rule size", 2, set1.getRules().size());
166: for (Rule rule : set1.getRules()) {
167: assertTrue("not a rule reference",
168: rule instanceof RuleReference);
169: RuleReference ruleReference = (RuleReference) rule;
170: assertEquals("wrong ruleset file name", "foo",
171: ruleReference.getRuleSetReference()
172: .getRuleSetFileName());
173: assertTrue("not all rule reference", ruleReference
174: .getRuleSetReference().isAllRules());
175: }
176: }
177:
178: @Test
179: public void testAddRuleSetByReferenceSingleRule() {
180: RuleSet set1 = new RuleSet();
181: RuleSet set2 = new RuleSet();
182: set2.setFileName("foo");
183: set2
184: .addRule(new MockRule("name", "desc", "msg",
185: "rulesetname"));
186: set2
187: .addRule(new MockRule("name2", "desc", "msg",
188: "rulesetname"));
189: set1.addRuleSetByReference(set2, false);
190: assertEquals("wrong rule size", 2, set1.getRules().size());
191: for (Rule rule : set1.getRules()) {
192: assertTrue("not a rule reference",
193: rule instanceof RuleReference);
194: RuleReference ruleReference = (RuleReference) rule;
195: assertEquals("wrong ruleset file name", "foo",
196: ruleReference.getRuleSetReference()
197: .getRuleSetFileName());
198: assertFalse("should not be all rule reference",
199: ruleReference.getRuleSetReference().isAllRules());
200: }
201: }
202:
203: @Test
204: public void testApply0Rules() throws Throwable {
205: RuleSet IUT = new RuleSet();
206: verifyRuleSet(IUT, 0, new HashSet());
207: }
208:
209: @Test
210: public void testEquals1() {
211: RuleSet s = new RuleSet();
212: assertFalse("A ruleset cannot be equals to null", s
213: .equals(null));
214: }
215:
216: @Test
217: public void testEquals2() {
218: RuleSet s = new RuleSet();
219: assertTrue("A rulset must be equals to itself", s.equals(s));
220: }
221:
222: @Test
223: public void testEquals3() {
224: RuleSet s = new RuleSet();
225: s.setName("basic rules");
226: assertFalse(
227: "A ruleset cannot be equals to another kind of object",
228: s.equals("basic rules"));
229: }
230:
231: @Test
232: public void testEquals4() {
233: RuleSet s1 = new RuleSet();
234: s1.setName("my ruleset");
235: s1.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
236:
237: RuleSet s2 = new RuleSet();
238: s2.setName("my ruleset");
239: s2.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
240:
241: assertEquals(
242: "2 rulesets with same name and rules must be equals",
243: s1, s2);
244: assertEquals("Equals rulesets must have the same hashcode", s1
245: .hashCode(), s2.hashCode());
246: }
247:
248: @Test
249: public void testEquals5() {
250: RuleSet s1 = new RuleSet();
251: s1.setName("my ruleset");
252: s1.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
253:
254: RuleSet s2 = new RuleSet();
255: s2.setName("my other ruleset");
256: s2.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
257:
258: assertFalse(
259: "2 rulesets with different name but same rules must not be equals",
260: s1.equals(s2));
261: }
262:
263: @Test
264: public void testEquals6() {
265: RuleSet s1 = new RuleSet();
266: s1.setName("my ruleset");
267: s1.addRule(new MockRule("name", "desc", "msg", "rulesetname"));
268:
269: RuleSet s2 = new RuleSet();
270: s2.setName("my ruleset");
271: s2.addRule(new MockRule("other rule", "desc", "msg",
272: "rulesetname"));
273:
274: assertFalse(
275: "2 rulesets with same name but different rules must not be equals",
276: s1.equals(s2));
277: }
278:
279: @Test
280: public void testAddExcludePattern() {
281: RuleSet ruleSet = new RuleSet();
282: ruleSet.addExcludePattern("*");
283: assertNotNull("Exclude patterns", ruleSet.getExcludePatterns());
284: assertEquals("Invalid number of patterns", 1, ruleSet
285: .getExcludePatterns().size());
286: assertEquals("Exclude pattern", "*", ruleSet
287: .getExcludePatterns().get(0));
288: assertNotNull("Include patterns", ruleSet.getIncludePatterns());
289: assertEquals("Invalid number of include patterns", 0, ruleSet
290: .getIncludePatterns().size());
291: }
292:
293: @Test
294: public void testAddExcludePatterns() {
295: RuleSet ruleSet = new RuleSet();
296: ruleSet.addExcludePattern("*");
297: ruleSet.addExcludePattern(".*");
298: RuleSet ruleSet2 = new RuleSet();
299: ruleSet2.addExcludePatterns(ruleSet.getExcludePatterns());
300: assertNotNull("Exclude patterns", ruleSet2.getExcludePatterns());
301: assertEquals("Invalid number of patterns", 2, ruleSet2
302: .getExcludePatterns().size());
303: assertEquals("Exclude pattern", "*", ruleSet2
304: .getExcludePatterns().get(0));
305: assertEquals("Exclude pattern", ".*", ruleSet2
306: .getExcludePatterns().get(1));
307: assertNotNull("Include patterns", ruleSet2.getIncludePatterns());
308: assertEquals("Invalid number of include patterns", 0, ruleSet2
309: .getIncludePatterns().size());
310: }
311:
312: @Test
313: public void testSetExcludePatterns() {
314: List<String> excludePatterns = new ArrayList<String>();
315: excludePatterns.add("*");
316: excludePatterns.add(".*");
317: RuleSet ruleSet = new RuleSet();
318: ruleSet.setExcludePatterns(excludePatterns);
319: assertNotNull("Exclude patterns", ruleSet.getExcludePatterns());
320: assertEquals("Invalid number of exclude patterns", 2, ruleSet
321: .getExcludePatterns().size());
322: assertEquals("Exclude pattern", "*", ruleSet
323: .getExcludePatterns().get(0));
324: assertEquals("Exclude pattern", ".*", ruleSet
325: .getExcludePatterns().get(1));
326: assertNotNull("Include patterns", ruleSet.getIncludePatterns());
327: assertEquals("Invalid number of include patterns", 0, ruleSet
328: .getIncludePatterns().size());
329: }
330:
331: @Test
332: public void testAddIncludePattern() {
333: RuleSet ruleSet = new RuleSet();
334: ruleSet.addIncludePattern("*");
335: assertNotNull("Include patterns", ruleSet.getIncludePatterns());
336: assertEquals("Invalid number of patterns", 1, ruleSet
337: .getIncludePatterns().size());
338: assertEquals("Include pattern", "*", ruleSet
339: .getIncludePatterns().get(0));
340: assertNotNull("Exclude patterns", ruleSet.getExcludePatterns());
341: assertEquals("Invalid number of exclude patterns", 0, ruleSet
342: .getExcludePatterns().size());
343: }
344:
345: @Test
346: public void testAddIncludePatterns() {
347: RuleSet ruleSet = new RuleSet();
348: ruleSet.addIncludePattern("*");
349: ruleSet.addIncludePattern(".*");
350: RuleSet ruleSet2 = new RuleSet();
351: ruleSet2.addIncludePatterns(ruleSet.getIncludePatterns());
352: assertNotNull("Include patterns", ruleSet2.getIncludePatterns());
353: assertEquals("Invalid number of patterns", 2, ruleSet2
354: .getIncludePatterns().size());
355: assertEquals("Include pattern", "*", ruleSet2
356: .getIncludePatterns().get(0));
357: assertEquals("Include pattern", ".*", ruleSet2
358: .getIncludePatterns().get(1));
359: assertNotNull("Exclude patterns", ruleSet.getExcludePatterns());
360: assertEquals("Invalid number of exclude patterns", 0, ruleSet
361: .getExcludePatterns().size());
362: }
363:
364: @Test
365: public void testSetIncludePatterns() {
366: List<String> includePatterns = new ArrayList<String>();
367: includePatterns.add("*");
368: includePatterns.add(".*");
369: RuleSet ruleSet = new RuleSet();
370: ruleSet.setIncludePatterns(includePatterns);
371: assertNotNull("Include patterns", ruleSet.getIncludePatterns());
372: assertEquals("Invalid number of include patterns", 2, ruleSet
373: .getIncludePatterns().size());
374: assertEquals("Include pattern", "*", ruleSet
375: .getIncludePatterns().get(0));
376: assertEquals("Include pattern", ".*", ruleSet
377: .getIncludePatterns().get(1));
378: assertNotNull("Exclude patterns", ruleSet.getExcludePatterns());
379: assertEquals("Invalid number of exclude patterns", 0, ruleSet
380: .getExcludePatterns().size());
381: }
382:
383: @Test
384: public void testIncludeExcludeApplies() {
385: File file = new File(
386: "C:\\myworkspace\\project\\some\\random\\package\\RandomClass.java");
387:
388: RuleSet ruleSet = new RuleSet();
389: assertTrue("No patterns", ruleSet.applies(file));
390:
391: ruleSet = new RuleSet();
392: ruleSet.addExcludePattern("nomatch");
393: assertTrue("Non-matching exclude", ruleSet.applies(file));
394:
395: ruleSet = new RuleSet();
396: ruleSet.addExcludePattern("nomatch");
397: ruleSet.addExcludePattern(".*/package/.*");
398: assertFalse("Matching exclude", ruleSet.applies(file));
399:
400: ruleSet = new RuleSet();
401: ruleSet.addExcludePattern("nomatch");
402: ruleSet.addExcludePattern(".*/package/.*");
403: ruleSet.addIncludePattern(".*/randomX/.*");
404: assertFalse("Non-matching include", ruleSet.applies(file));
405:
406: ruleSet = new RuleSet();
407: ruleSet.addExcludePattern("nomatch");
408: ruleSet.addExcludePattern(".*/package/.*");
409: ruleSet.addIncludePattern(".*/randomX/.*");
410: ruleSet.addIncludePattern(".*/random/.*");
411: assertTrue("Matching include", ruleSet.applies(file));
412: }
413:
414: protected void verifyRuleSet(RuleSet IUT, int size, Set values)
415: throws Throwable {
416:
417: RuleContext context = new RuleContext();
418: Set<RuleViolation> reportedValues = new HashSet<RuleViolation>();
419: context.setReport(new Report());
420: IUT.apply(makeCompilationUnits(), context);
421:
422: assertEquals("Invalid number of Violations Reported", size,
423: context.getReport().size());
424:
425: Iterator violations = context.getReport().iterator();
426: while (violations.hasNext()) {
427: RuleViolation violation = (RuleViolation) violations.next();
428:
429: reportedValues.add(violation);
430: assertTrue("Unexpected Violation Returned: " + violation,
431: values.contains(violation));
432: }
433:
434: Iterator expected = values.iterator();
435: while (expected.hasNext()) {
436: RuleViolation violation = (RuleViolation) expected.next();
437: assertTrue("Expected Violation not Returned: " + violation,
438: reportedValues.contains(violation));
439: }
440: }
441:
442: protected List makeCompilationUnits() throws Throwable {
443: List<ASTCompilationUnit> RC = new ArrayList<ASTCompilationUnit>();
444: JavaParser parser = TargetJDKVersion.DEFAULT_JDK_VERSION
445: .createParser(new StringReader(javaCode));
446: RC.add(parser.CompilationUnit());
447: return RC;
448: }
449:
450: public static junit.framework.Test suite() {
451: return new junit.framework.JUnit4TestAdapter(RuleSetTest.class);
452: }
453: }
|