1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.filters;
21
22 import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
23
24 import org.junit.jupiter.api.Test;
25
26 import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
27 import com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck;
28 import com.puppycrawl.tools.checkstyle.checks.coding.IllegalThrowsCheck;
29 import com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck;
30 import com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck;
31 import com.puppycrawl.tools.checkstyle.checks.metrics.CyclomaticComplexityCheck;
32 import com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck;
33 import com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck;
34 import com.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck;
35 import com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck;
36
37 public class SuppressionXpathFilterExamplesTest extends AbstractExamplesModuleTestSupport {
38
39 @Override
40 public String getPackageLocation() {
41 return "com/puppycrawl/tools/checkstyle/filters/suppressionxpathfilter";
42 }
43
44 @Test
45 public void testExample1() throws Exception {
46
47 final String[] expectedWithoutFilter = {
48 "41:3: " + getCheckMessage(CyclomaticComplexityCheck.class,
49 CyclomaticComplexityCheck.MSG_KEY, 4, 3),
50 };
51
52 final String[] expectedWithFilter = {
53 "41:3: " + getCheckMessage(CyclomaticComplexityCheck.class,
54 CyclomaticComplexityCheck.MSG_KEY, 4, 3),
55 };
56
57 System.setProperty("config.folder", "src/xdocs-examples/resources/"
58 + getPackageLocation());
59 verifyFilterWithInlineConfigParser(getPath("Example1.java"),
60 expectedWithoutFilter,
61 expectedWithFilter);
62 }
63
64 @Test
65 public void testExample2() throws Exception {
66
67 final String[] expectedWithoutFilter = {
68 "41:3: " + getCheckMessage(CyclomaticComplexityCheck.class,
69 CyclomaticComplexityCheck.MSG_KEY, 4, 3),
70 };
71
72 final String[] expectedWithFilter = {
73 "41:3: " + getCheckMessage(CyclomaticComplexityCheck.class,
74 CyclomaticComplexityCheck.MSG_KEY, 4, 3),
75 };
76
77 System.setProperty("config.folder", "src/xdocs-examples/resources/"
78 + getPackageLocation());
79 verifyFilterWithInlineConfigParser(getPath("Example2.java"),
80 expectedWithoutFilter,
81 expectedWithFilter);
82 }
83
84 @Test
85 public void testExample3() throws Exception {
86
87 final String[] expectedWithoutFilter = {
88 "40:3: " + getCheckMessage(CyclomaticComplexityCheck.class,
89 CyclomaticComplexityCheck.MSG_KEY, 4, 3),
90 };
91
92 final String[] expectedWithFilter = {
93 "40:3: " + getCheckMessage(CyclomaticComplexityCheck.class,
94 CyclomaticComplexityCheck.MSG_KEY, 4, 3),
95 };
96
97 System.setProperty("config.folder", "src/xdocs-examples/resources/"
98 + getPackageLocation());
99 verifyFilterWithInlineConfigParser(getPath("Example3.java"),
100 expectedWithoutFilter,
101 expectedWithFilter);
102 }
103
104 @Test
105 public void testUseCase4() throws Exception {
106
107 final String[] expectedWithoutFilter = {
108 "13:1: " + getCheckMessage(EmptyLineSeparatorCheck.class,
109 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "package"),
110 "21:3: " + getCheckMessage(EmptyLineSeparatorCheck.class,
111 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
112 "22:3: " + getCheckMessage(EmptyLineSeparatorCheck.class,
113 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
114 "23:3: " + getCheckMessage(EmptyLineSeparatorCheck.class,
115 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
116 };
117
118 final String[] expectedWithFilter = {
119 "21:3: " + getCheckMessage(EmptyLineSeparatorCheck.class,
120 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
121 "22:3: " + getCheckMessage(EmptyLineSeparatorCheck.class,
122 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
123 "23:3: " + getCheckMessage(EmptyLineSeparatorCheck.class,
124 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
125 };
126
127 System.setProperty("config.folder", "src/xdocs-examples/resources/"
128 + getPackageLocation());
129 verifyFilterWithInlineConfigParser(getPath("UseCase4.java"),
130 expectedWithoutFilter,
131 expectedWithFilter);
132 }
133
134 @Test
135 public void testUseCase5() throws Exception {
136
137 final String[] expectedWithoutFilter = {
138 "19:23: " + getCheckMessage(LeftCurlyCheck.class,
139 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 23),
140 "27:36: " + getCheckMessage(LeftCurlyCheck.class,
141 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 36),
142 "31:27: " + getCheckMessage(LeftCurlyCheck.class,
143 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 27),
144 "35:28: " + getCheckMessage(LeftCurlyCheck.class,
145 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 28),
146 "40:31: " + getCheckMessage(LeftCurlyCheck.class,
147 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 31),
148 "41:35: " + getCheckMessage(LeftCurlyCheck.class,
149 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 35),
150 "44:23: " + getCheckMessage(LeftCurlyCheck.class,
151 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 23),
152 };
153
154 final String[] expectedWithFilter = {
155 "27:36: " + getCheckMessage(LeftCurlyCheck.class,
156 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 36),
157 "31:27: " + getCheckMessage(LeftCurlyCheck.class,
158 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 27),
159 "35:28: " + getCheckMessage(LeftCurlyCheck.class,
160 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 28),
161 "40:31: " + getCheckMessage(LeftCurlyCheck.class,
162 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 31),
163 "41:35: " + getCheckMessage(LeftCurlyCheck.class,
164 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 35),
165 "44:23: " + getCheckMessage(LeftCurlyCheck.class,
166 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 23),
167 };
168
169 System.setProperty("config.folder", "src/xdocs-examples/resources/"
170 + getPackageLocation());
171 verifyFilterWithInlineConfigParser(getPath("UseCase5.java"),
172 expectedWithoutFilter,
173 expectedWithFilter);
174 }
175
176 @Test
177 public void testUseCase6() throws Exception {
178
179 final String[] expectedWithoutFilter = {
180 "20:3: " + getCheckMessage(EmptyLineSeparatorCheck.class,
181 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
182 "21:3: " + getCheckMessage(EmptyLineSeparatorCheck.class,
183 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
184 "22:3: " + getCheckMessage(EmptyLineSeparatorCheck.class,
185 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
186 };
187
188 final String[] expectedWithFilter = {
189 "20:3: " + getCheckMessage(EmptyLineSeparatorCheck.class,
190 EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
191 };
192
193 System.setProperty("config.folder", "src/xdocs-examples/resources/"
194 + getPackageLocation());
195 verifyFilterWithInlineConfigParser(getPath("UseCase6.java"),
196 expectedWithoutFilter,
197 expectedWithFilter);
198 }
199
200 @Test
201 public void testUseCase7() throws Exception {
202
203 final String pattern = "^[a-z][a-zA-Z0-9]*$";
204
205 final String[] expectedWithoutFilter = {
206 "20:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
207 "SetSomeVar", pattern),
208 "21:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
209 "DoMATH", pattern),
210 "48:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
211 "Test1", pattern),
212 "51:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
213 "Test2", pattern),
214 };
215
216 final String[] expectedWithFilter = {
217 "21:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
218 "DoMATH", pattern),
219 "48:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
220 "Test1", pattern),
221 "51:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
222 "Test2", pattern),
223 };
224
225 System.setProperty("config.folder", "src/xdocs-examples/resources/"
226 + getPackageLocation());
227 verifyFilterWithInlineConfigParser(getPath("UseCase7.java"),
228 expectedWithoutFilter,
229 expectedWithFilter);
230 }
231
232 @Test
233 public void testUseCase8() throws Exception {
234
235 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
236
237 final String[] expectedWithoutFilter = {
238 "34:9: " + getCheckMessage(LocalVariableNameCheck.class, MSG_INVALID_PATTERN,
239 "TestVariable", pattern),
240 "35:9: " + getCheckMessage(LocalVariableNameCheck.class, MSG_INVALID_PATTERN,
241 "WeirdName", pattern),
242 };
243
244 final String[] expectedWithFilter = {
245 "35:9: " + getCheckMessage(LocalVariableNameCheck.class, MSG_INVALID_PATTERN,
246 "WeirdName", pattern),
247 };
248
249 System.setProperty("config.folder", "src/xdocs-examples/resources/"
250 + getPackageLocation());
251 verifyFilterWithInlineConfigParser(getPath("UseCase8.java"),
252 expectedWithoutFilter,
253 expectedWithFilter);
254 }
255
256 @Test
257 public void testUseCase9() throws Exception {
258
259 final String pattern = "^[a-z][a-zA-Z0-9]*$";
260
261 final String[] expectedWithoutFilter = {
262 "19:13: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "23"),
263 "20:27: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "11"),
264 "21:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
265 "SetSomeVar", pattern),
266 "22:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
267 "DoMATH", pattern),
268 "31:11: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "24"),
269 "49:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
270 "Test1", pattern),
271 "52:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
272 "Test2", pattern),
273 "56:19: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "11"),
274 "57:8: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
275 "FOO", pattern),
276 };
277
278 final String[] expectedWithFilter = {
279 "19:13: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "23"),
280 "20:27: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "11"),
281 "21:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
282 "SetSomeVar", pattern),
283 "31:11: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "24"),
284 "49:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
285 "Test1", pattern),
286 "52:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
287 "Test2", pattern),
288 "56:19: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "11"),
289 };
290
291 System.setProperty("config.folder", "src/xdocs-examples/resources/"
292 + getPackageLocation());
293 verifyFilterWithInlineConfigParser(getPath("UseCase9.java"),
294 expectedWithoutFilter,
295 expectedWithFilter);
296 }
297
298 @Test
299 public void testUseCase10() throws Exception {
300
301 final String[] expectedWithoutFilter = {
302 "32:5: " + getCheckMessage(RequireThisCheck.class, RequireThisCheck.MSG_VARIABLE,
303 "age", ""),
304 "41:9: " + getCheckMessage(RequireThisCheck.class, RequireThisCheck.MSG_VARIABLE,
305 "age", ""),
306 "41:20: " + getCheckMessage(RequireThisCheck.class, RequireThisCheck.MSG_VARIABLE,
307 "wordCount", ""),
308 "44:14: " + getCheckMessage(RequireThisCheck.class, RequireThisCheck.MSG_VARIABLE,
309 "age", ""),
310 };
311
312 final String[] expectedWithFilter = {
313 "41:9: " + getCheckMessage(RequireThisCheck.class, RequireThisCheck.MSG_VARIABLE,
314 "age", ""),
315 "41:20: " + getCheckMessage(RequireThisCheck.class, RequireThisCheck.MSG_VARIABLE,
316 "wordCount", ""),
317 "44:14: " + getCheckMessage(RequireThisCheck.class, RequireThisCheck.MSG_VARIABLE,
318 "age", ""),
319 };
320
321 System.setProperty("config.folder", "src/xdocs-examples/resources/"
322 + getPackageLocation());
323 verifyFilterWithInlineConfigParser(getPath("UseCase10.java"),
324 expectedWithoutFilter,
325 expectedWithFilter);
326 }
327
328 @Test
329 public void testUseCase11() throws Exception {
330
331 final String[] expectedWithoutFilter = {
332 "23:37: " + getCheckMessage(IllegalThrowsCheck.class, IllegalThrowsCheck.MSG_KEY,
333 "RuntimeException"),
334 };
335
336 final String[] expectedWithFilter = {};
337
338 System.setProperty("config.folder", "src/xdocs-examples/resources/"
339 + getPackageLocation());
340 verifyFilterWithInlineConfigParser(getPath("UseCase11.java"),
341 expectedWithoutFilter,
342 expectedWithFilter);
343 }
344
345 @Test
346 public void testUseCase12() throws Exception {
347
348 final String[] expectedWithoutFilter = {
349 "25:9: " + getCheckMessage(ModifierOrderCheck.class,
350 ModifierOrderCheck.MSG_MODIFIER_ORDER, "public"),
351 "26:14: " + getCheckMessage(ModifierOrderCheck.class,
352 ModifierOrderCheck.MSG_MODIFIER_ORDER, "abstract"),
353 };
354
355 final String[] expectedWithFilter = {};
356
357 System.setProperty("config.folder", "src/xdocs-examples/resources/"
358 + getPackageLocation());
359 verifyFilterWithInlineConfigParser(getPath("UseCase12.java"),
360 expectedWithoutFilter,
361 expectedWithFilter);
362 }
363
364 @Test
365 public void testUseCase13() throws Exception {
366 final String[] expectedWithoutFilter = {
367 "18:13: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "23"),
368 "19:27: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "11"),
369 "30:11: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "24"),
370 };
371
372 final String[] expectedWithFilter = {
373 "18:13: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "23"),
374 "30:11: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "24"),
375 };
376
377 System.setProperty("config.folder", "src/xdocs-examples/resources/"
378 + getPackageLocation());
379 verifyFilterWithInlineConfigParser(getPath("UseCase13.java"),
380 expectedWithoutFilter,
381 expectedWithFilter);
382 }
383
384 @Test
385 public void testUseCase1() throws Exception {
386
387 final String[] expectedWithoutFilter = {
388 "18:13: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "23"),
389 "19:27: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "11"),
390 "30:11: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "24"),
391 };
392
393 final String[] expectedWithFilter = {
394 "18:13: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "23"),
395 "30:11: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY, "24"),
396 };
397
398 System.setProperty("config.folder", "src/xdocs-examples/resources/"
399 + getPackageLocation());
400 verifyFilterWithInlineConfigParser(getPath("UseCase1.java"),
401 expectedWithoutFilter,
402 expectedWithFilter);
403 }
404
405 @Test
406 public void testUseCase2() throws Exception {
407
408 final String pattern = "^[a-z][a-zA-Z0-9]*$";
409
410 final String[] expectedWithoutFilter = {
411 "19:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
412 "SetSomeVar", pattern),
413 "20:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
414 "DoMATH", pattern),
415 "47:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
416 "Test1", pattern),
417 "50:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
418 "Test2", pattern),
419 };
420
421 final String[] expectedWithFilter = {
422 "19:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
423 "SetSomeVar", pattern),
424 "20:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
425 "DoMATH", pattern),
426 };
427
428 System.setProperty("config.folder", "src/xdocs-examples/resources/"
429 + getPackageLocation());
430 verifyFilterWithInlineConfigParser(getPath("UseCase2.java"),
431 expectedWithoutFilter,
432 expectedWithFilter);
433 }
434
435 @Test
436 public void testUseCase3() throws Exception {
437
438 final String pattern = "^[a-z][a-zA-Z0-9]*$";
439
440 final String[] expectedWithoutFilter = {
441 "19:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
442 "SetSomeVar", pattern),
443 "20:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
444 "DoMATH", pattern),
445 "47:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
446 "Test1", pattern),
447 "50:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
448 "Test2", pattern),
449 };
450
451 final String[] expectedWithFilter = {
452 "19:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
453 "SetSomeVar", pattern),
454 "20:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
455 "DoMATH", pattern),
456 "47:15: " + getCheckMessage(MethodNameCheck.class, MSG_INVALID_PATTERN,
457 "Test1", pattern),
458 };
459
460 System.setProperty("config.folder", "src/xdocs-examples/resources/"
461 + getPackageLocation());
462 verifyFilterWithInlineConfigParser(getPath("UseCase3.java"),
463 expectedWithoutFilter,
464 expectedWithFilter);
465 }
466
467 }