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