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 net.sourceforge.pmd.AbstractRule;
26 import net.sourceforge.pmd.PMD;
27 import net.sourceforge.pmd.Report;
28 import net.sourceforge.pmd.ReportListener;
29 import net.sourceforge.pmd.RuleViolation;
30 import net.sourceforge.pmd.IRuleViolation;
31 import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
32 import net.sourceforge.pmd.stat.Metric;
33 import test.net.sourceforge.pmd.testframework.RuleTst;
34
35 import java.util.Iterator;
36
37 public class ReportTest extends RuleTst implements ReportListener {
38
39 private static class FooRule extends AbstractRule {
40 public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
41 if (c.getImage().equals("Foo")) addViolation(ctx, c);
42 return ctx;
43 }
44
45 public String getMessage() {
46 return "blah";
47 }
48
49 public String getName() {
50 return "Foo";
51 }
52
53 public String getRuleSetName() {
54 return "RuleSet";
55 }
56
57 public String getDescription() {
58 return "desc";
59 }
60 }
61
62 private boolean violationSemaphore;
63 private boolean metricSemaphore;
64
65 public void ruleViolationAdded(IRuleViolation ruleViolation) {
66 violationSemaphore = true;
67 }
68
69 public void metricAdded(Metric metric) {
70 metricSemaphore = true;
71 }
72
73 public void testBasic() throws Throwable {
74 Report r = new Report();
75 runTestFromString(TEST1, new FooRule(), r);
76 assertTrue(!r.isEmpty());
77 }
78
79
80 public void testMetric0() {
81 Report r = new Report();
82 assertTrue("Default report shouldn't contain metrics", !r.hasMetrics());
83 }
84
85 public void testMetric1() {
86 Report r = new Report();
87 assertTrue("Default report shouldn't contain metrics", !r.hasMetrics());
88
89 r.addMetric(new Metric("m1", 0, 0.0, 1.0, 2.0, 3.0, 4.0));
90 assertTrue("Expected metrics weren't there", r.hasMetrics());
91
92 Iterator ms = r.metrics();
93 assertTrue("Should have some metrics in there now", ms.hasNext());
94
95 Object o = ms.next();
96 assertTrue("Expected Metric, got " + o.getClass(), o instanceof Metric);
97
98 Metric m = (Metric) o;
99 assertEquals("metric name mismatch", "m1", m.getMetricName());
100 assertEquals("wrong low value", 1.0, m.getLowValue(), 0.05);
101 assertEquals("wrong high value", 2.0, m.getHighValue(), 0.05);
102 assertEquals("wrong avg value", 3.0, m.getAverage(), 0.05);
103 assertEquals("wrong std dev value", 4.0, m.getStandardDeviation(), 0.05);
104 }
105
106 public void testExclusionsInReportWithAnnotations() throws Throwable {
107 Report rpt = new Report();
108 runTestFromString15(TEST2, new FooRule(), rpt);
109 assertTrue(rpt.isEmpty());
110 assertEquals(1, rpt.getSuppressedRuleViolations().size());
111 }
112
113 public void testExclusionsInReportWithNOPMD() throws Throwable {
114 Report rpt = new Report();
115 runTestFromString(TEST3, new FooRule(), rpt);
116 assertTrue(rpt.isEmpty());
117 assertEquals(1, rpt.getSuppressedRuleViolations().size());
118 }
119
120 private static final String TEST1 =
121 "public class Foo {}" + PMD.EOL;
122
123 private static final String TEST2 =
124 "@SuppressWarnings(\"\")" + PMD.EOL +
125 "public class Foo {}";
126
127 private static final String TEST3 =
128 "public class Foo {} // NOPMD";
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 }