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.Report;
27 import net.sourceforge.pmd.RuleContext;
28 import net.sourceforge.pmd.RuleSet;
29 import net.sourceforge.pmd.RuleViolation;
30 import net.sourceforge.pmd.ast.JavaParser;
31 import test.net.sourceforge.pmd.testframework.MockRule;
32
33 import java.io.StringReader;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.HashSet;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Random;
40 import java.util.Set;
41
42 public class RuleSetTest extends TestCase {
43 private String javaCode = "public class Test { }";
44
45 public void testConstructor() {
46 new RuleSet();
47 }
48
49 public void testAccessors() {
50 RuleSet rs = new RuleSet();
51 rs.setName("foo");
52 assertEquals("name mismatch", "foo", rs.getName());
53 rs.setDescription("bar");
54 assertEquals("description mismatch", "bar", rs.getDescription());
55 }
56
57 public void testGetRuleByName() {
58 RuleSet rs = new RuleSet();
59 MockRule mock = new MockRule("name", "desc", "msg");
60 rs.addRule(mock);
61 assertEquals("unable to fetch rule by name", mock, rs.getRuleByName("name"));
62 }
63
64 public void testRuleList() {
65 RuleSet IUT = new RuleSet();
66
67 assertEquals("Size of RuleSet isn't zero.", 0, IUT.size());
68
69 MockRule rule = new MockRule("name", "desc", "msg");
70 IUT.addRule(rule);
71
72 assertEquals("Size of RuleSet isn't one.", 1, IUT.size());
73
74 Set rules = IUT.getRules();
75
76 Iterator i = rules.iterator();
77 assertTrue("Empty Set", i.hasNext());
78 assertEquals("Returned set of wrong size.", 1, rules.size());
79 assertEquals("Rule isn't in ruleset.", rule, i.next());
80 }
81
82 public void testAddRuleSet() {
83 RuleSet set1 = new RuleSet();
84 set1.addRule(new MockRule("name", "desc", "msg"));
85 RuleSet set2 = new RuleSet();
86 set2.addRule(new MockRule("name", "desc", "msg"));
87 set1.addRuleSet(set2);
88 assertEquals("ruleset size wrong", 2, set1.size());
89 }
90
91 public void testApply0Rules() throws Throwable {
92 RuleSet IUT = new RuleSet();
93 verifyRuleSet(IUT, 0, new HashSet());
94 }
95
96 public void testApply1Rule() throws Throwable {
97 RuleSet IUT = new RuleSet();
98
99 MockRule rule = new MockRule("name", "desc", "msg");
100 RuleContext ctx = new RuleContext();
101 ctx.setSourceCodeFilename("filename");
102 RuleViolation violation = new RuleViolation(rule, 1, ctx);
103 rule.addViolation(violation);
104
105 IUT.addRule(rule);
106
107 verifyRuleSet(IUT, 1, Collections.singleton(violation));
108 }
109
110 public void testApplyNRule() throws Throwable {
111 RuleSet IUT = new RuleSet();
112
113 Random rand = new Random();
114 int numRules = rand.nextInt(10) + 1;
115 Set ruleViolations = new HashSet();
116
117 for (int i = 0; i < numRules; i++) {
118 MockRule rule = new MockRule("name", "desc", "msg");
119 RuleContext ctx = new RuleContext();
120 ctx.setSourceCodeFilename("filename");
121 RuleViolation violation = new RuleViolation(rule, i, ctx);
122
123 ruleViolations.add(violation);
124 rule.addViolation(violation);
125
126 IUT.addRule(rule);
127 }
128
129 verifyRuleSet(IUT, numRules, ruleViolations);
130 }
131
132 protected void verifyRuleSet(RuleSet IUT, int size, Set values) throws Throwable {
133
134 RuleContext context = new RuleContext();
135 Set reportedValues = new HashSet();
136 context.setReport(new Report());
137 IUT.apply(makeCompilationUnits(), context);
138
139 assertEquals("Invalid number of Violations Reported", size, context.getReport().size());
140
141 Iterator violations = context.getReport().iterator();
142 while (violations.hasNext()) {
143 RuleViolation violation = (RuleViolation) violations.next();
144
145 reportedValues.add(violation);
146 assertTrue("Unexpected Violation Returned: " + violation, values.contains(violation));
147 }
148
149 Iterator expected = values.iterator();
150 while (expected.hasNext()) {
151 RuleViolation violation = (RuleViolation) expected.next();
152 assertTrue("Expected Violation not Returned: " + violation, reportedValues.contains(violation));
153 }
154 }
155
156
157 protected List makeCompilationUnits() throws Throwable {
158 List RC = new ArrayList();
159 JavaParser parser = new JavaParser(new StringReader(javaCode));
160 RC.add(parser.CompilationUnit());
161 return RC;
162 }
163 }
This page was automatically generated by Maven