1
2
3
4 package net.sourceforge.pmd;
5
6 import static org.junit.Assert.assertEquals;
7 import junit.framework.JUnit4TestAdapter;
8 import net.sourceforge.pmd.lang.LanguageVersion;
9 import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
10 import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
11 import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
12 import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
13 import net.sourceforge.pmd.testframework.RuleTst;
14
15 import org.junit.Test;
16
17
18 public class SuppressWarningsTest extends RuleTst {
19
20 private static class FooRule extends AbstractJavaRule {
21 @Override
22 public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
23 if (c.getImage().equalsIgnoreCase("Foo")) {
24 addViolation(ctx, c);
25 }
26 return super.visit(c, ctx);
27 }
28
29 @Override
30 public Object visit(ASTVariableDeclaratorId c, Object ctx) {
31 if (c.getImage().equalsIgnoreCase("Foo")) {
32 addViolation(ctx, c);
33 }
34 return super.visit(c, ctx);
35 }
36
37 @Override
38 public String getName() {
39 return "NoFoo";
40 }
41 }
42
43 private static class BarRule extends AbstractJavaRule {
44 @Override
45 public Object visit(ASTCompilationUnit cu, Object ctx) {
46
47 for (ASTClassOrInterfaceDeclaration c : cu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class)) {
48 if (c.getImage().equalsIgnoreCase("bar")) {
49 addViolation(ctx, cu);
50 }
51 }
52 return super.visit(cu, ctx);
53 }
54
55 @Override
56 public String getName() {
57 return "NoBar";
58 }
59 }
60
61 @Test
62 public void testClassLevelSuppression() throws Throwable {
63 Report rpt = new Report();
64 runTestFromString(TEST1, new FooRule(), rpt, LanguageVersion.JAVA_15);
65 assertEquals(0, rpt.size());
66 runTestFromString(TEST2, new FooRule(), rpt, LanguageVersion.JAVA_15);
67 assertEquals(0, rpt.size());
68 }
69
70 @Test
71 public void testInheritedSuppression() throws Throwable {
72 Report rpt = new Report();
73 runTestFromString(TEST3, new FooRule(), rpt, LanguageVersion.JAVA_15);
74 assertEquals(0, rpt.size());
75 }
76
77 @Test
78 public void testMethodLevelSuppression() throws Throwable {
79 Report rpt = new Report();
80 runTestFromString(TEST4, new FooRule(), rpt, LanguageVersion.JAVA_15);
81 assertEquals(1, rpt.size());
82 }
83
84 @Test
85 public void testConstructorLevelSuppression() throws Throwable {
86 Report rpt = new Report();
87 runTestFromString(TEST5, new FooRule(), rpt, LanguageVersion.JAVA_15);
88 assertEquals(0, rpt.size());
89 }
90
91 @Test
92 public void testFieldLevelSuppression() throws Throwable {
93 Report rpt = new Report();
94 runTestFromString(TEST6, new FooRule(), rpt, LanguageVersion.JAVA_15);
95 assertEquals(1, rpt.size());
96 }
97
98 @Test
99 public void testParameterLevelSuppression() throws Throwable {
100 Report rpt = new Report();
101 runTestFromString(TEST7, new FooRule(), rpt, LanguageVersion.JAVA_15);
102 assertEquals(1, rpt.size());
103 }
104
105 @Test
106 public void testLocalVariableLevelSuppression() throws Throwable {
107 Report rpt = new Report();
108 runTestFromString(TEST8, new FooRule(), rpt, LanguageVersion.JAVA_15);
109 assertEquals(1, rpt.size());
110 }
111
112 @Test
113 public void testSpecificSuppression() throws Throwable {
114 Report rpt = new Report();
115 runTestFromString(TEST9, new FooRule(), rpt, LanguageVersion.JAVA_15);
116 assertEquals(1, rpt.size());
117 }
118
119 @Test
120 public void testSpecificSuppressionValue1() throws Throwable {
121 Report rpt = new Report();
122 runTestFromString(TEST9_VALUE1, new FooRule(), rpt, LanguageVersion.JAVA_15);
123 assertEquals(1, rpt.size());
124 }
125
126 @Test
127 public void testSpecificSuppressionValue2() throws Throwable {
128 Report rpt = new Report();
129 runTestFromString(TEST9_VALUE2, new FooRule(), rpt, LanguageVersion.JAVA_15);
130 assertEquals(1, rpt.size());
131 }
132
133 @Test
134 public void testSpecificSuppressionValue3() throws Throwable {
135 Report rpt = new Report();
136 runTestFromString(TEST9_VALUE3, new FooRule(), rpt, LanguageVersion.JAVA_15);
137 assertEquals(1, rpt.size());
138 }
139
140 @Test
141 public void testSpecificSuppressionMulitpleValues1() throws Throwable {
142 Report rpt = new Report();
143 runTestFromString(TEST9_MULTIPLE_VALUES_1, new FooRule(), rpt, LanguageVersion.JAVA_15);
144 assertEquals(0, rpt.size());
145 }
146
147 @Test
148 public void testSpecificSuppressionMulitpleValues2() throws Throwable {
149 Report rpt = new Report();
150 runTestFromString(TEST9_MULTIPLE_VALUES_2, new FooRule(), rpt, LanguageVersion.JAVA_15);
151 assertEquals(0, rpt.size());
152 }
153
154 @Test
155 public void testNoSuppressionBlank() throws Throwable {
156 Report rpt = new Report();
157 runTestFromString(TEST10, new FooRule(), rpt, LanguageVersion.JAVA_15);
158 assertEquals(2, rpt.size());
159 }
160
161 @Test
162 public void testNoSuppressionSomethingElseS() throws Throwable {
163 Report rpt = new Report();
164 runTestFromString(TEST11, new FooRule(), rpt, LanguageVersion.JAVA_15);
165 assertEquals(2, rpt.size());
166 }
167
168 @Test
169 public void testSuppressAll() throws Throwable {
170 Report rpt = new Report();
171 runTestFromString(TEST12, new FooRule(), rpt, LanguageVersion.JAVA_15);
172 assertEquals(0, rpt.size());
173 }
174
175 @Test
176 public void testSpecificSuppressionAtTopLevel() throws Throwable {
177 Report rpt = new Report();
178 runTestFromString(TEST13, new BarRule(), rpt, LanguageVersion.JAVA_15);
179 assertEquals(0, rpt.size());
180 }
181
182 private static final String TEST1 =
183 "@SuppressWarnings(\"PMD\")" + PMD.EOL +
184 "public class Foo {}";
185
186 private static final String TEST2 =
187 "@SuppressWarnings(\"PMD\")" + PMD.EOL +
188 "public class Foo {" + PMD.EOL +
189 " void bar() {" + PMD.EOL +
190 " int foo;" + PMD.EOL +
191 " }" + PMD.EOL +
192 "}";
193
194 private static final String TEST3 =
195 "public class Baz {" + PMD.EOL +
196 " @SuppressWarnings(\"PMD\")" + PMD.EOL +
197 " public class Bar {" + PMD.EOL +
198 " void bar() {" + PMD.EOL +
199 " int foo;" + PMD.EOL +
200 " }" + PMD.EOL +
201 " }" + PMD.EOL +
202 "}";
203
204 private static final String TEST4 =
205 "public class Foo {" + PMD.EOL +
206 " @SuppressWarnings(\"PMD\")" + PMD.EOL +
207 " void bar() {" + PMD.EOL +
208 " int foo;" + PMD.EOL +
209 " }" + PMD.EOL +
210 "}";
211
212 private static final String TEST5 =
213 "public class Bar {" + PMD.EOL +
214 " @SuppressWarnings(\"PMD\")" + PMD.EOL +
215 " public Bar() {" + PMD.EOL +
216 " int foo;" + PMD.EOL +
217 " }" + PMD.EOL +
218 "}";
219
220 private static final String TEST6 =
221 "public class Bar {" + PMD.EOL +
222 " @SuppressWarnings(\"PMD\")" + PMD.EOL +
223 " int foo;" + PMD.EOL +
224 " void bar() {" + PMD.EOL +
225 " int foo;" + PMD.EOL +
226 " }" + PMD.EOL +
227 "}";
228
229 private static final String TEST7 =
230 "public class Bar {" + PMD.EOL +
231 " int foo;" + PMD.EOL +
232 " void bar(@SuppressWarnings(\"PMD\") int foo) {}" + PMD.EOL +
233 "}";
234
235 private static final String TEST8 =
236 "public class Bar {" + PMD.EOL +
237 " int foo;" + PMD.EOL +
238 " void bar() {" + PMD.EOL +
239 " @SuppressWarnings(\"PMD\") int foo;" + PMD.EOL +
240 " }" + PMD.EOL +
241 "}";
242
243 private static final String TEST9 =
244 "public class Bar {" + PMD.EOL +
245 " int foo;" + PMD.EOL +
246 " void bar() {" + PMD.EOL +
247 " @SuppressWarnings(\"PMD.NoFoo\") int foo;" + PMD.EOL +
248 " }" + PMD.EOL +
249 "}";
250
251 private static final String TEST9_VALUE1 =
252 "public class Bar {" + PMD.EOL +
253 " int foo;" + PMD.EOL +
254 " void bar() {" + PMD.EOL +
255 " @SuppressWarnings(value = \"PMD.NoFoo\") int foo;" + PMD.EOL +
256 " }" + PMD.EOL +
257 "}";
258
259 private static final String TEST9_VALUE2 =
260 "public class Bar {" + PMD.EOL +
261 " int foo;" + PMD.EOL +
262 " void bar() {" + PMD.EOL +
263 " @SuppressWarnings({\"PMD.NoFoo\"}) int foo;" + PMD.EOL +
264 " }" + PMD.EOL +
265 "}";
266
267 private static final String TEST9_VALUE3 =
268 "public class Bar {" + PMD.EOL +
269 " int foo;" + PMD.EOL +
270 " void bar() {" + PMD.EOL +
271 " @SuppressWarnings(value = {\"PMD.NoFoo\"}) int foo;" + PMD.EOL +
272 " }" + PMD.EOL +
273 "}";
274
275 private static final String TEST9_MULTIPLE_VALUES_1 =
276 "@SuppressWarnings({\"PMD.NoFoo\", \"PMD.NoBar\"})" + PMD.EOL +
277 "public class Bar {" + PMD.EOL +
278 " int foo;" + PMD.EOL +
279 " void bar() {" + PMD.EOL +
280 " int foo;" + PMD.EOL +
281 " }" + PMD.EOL +
282 "}";
283
284 private static final String TEST9_MULTIPLE_VALUES_2 =
285 "@SuppressWarnings(value = {\"PMD.NoFoo\", \"PMD.NoBar\"})" + PMD.EOL +
286 "public class Bar {" + PMD.EOL +
287 " int foo;" + PMD.EOL +
288 " void bar() {" + PMD.EOL +
289 " int foo;" + PMD.EOL +
290 " }" + PMD.EOL +
291 "}";
292
293 private static final String TEST10 =
294 "public class Bar {" + PMD.EOL +
295 " int foo;" + PMD.EOL +
296 " void bar() {" + PMD.EOL +
297 " @SuppressWarnings(\"\") int foo;" + PMD.EOL +
298 " }" + PMD.EOL +
299 "}";
300
301 private static final String TEST11 =
302 "public class Bar {" + PMD.EOL +
303 " int foo;" + PMD.EOL +
304 " void bar() {" + PMD.EOL +
305 " @SuppressWarnings(\"SomethingElse\") int foo;" + PMD.EOL +
306 " }" + PMD.EOL +
307 "}";
308
309 private static final String TEST12 =
310 "public class Bar {" + PMD.EOL +
311 " @SuppressWarnings(\"all\") int foo;" + PMD.EOL +
312 "}";
313
314 private static final String TEST13 =
315 "@SuppressWarnings(\"PMD.NoBar\")" + PMD.EOL +
316 "public class Bar {" + PMD.EOL +
317 "}";
318
319 public static junit.framework.Test suite() {
320 return new JUnit4TestAdapter(SuppressWarningsTest.class);
321 }
322 }
323
324