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;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.io.LineNumberReader;
30 import java.nio.charset.StandardCharsets;
31 import java.nio.file.Path;
32 import java.text.MessageFormat;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.Map;
40 import java.util.ResourceBundle;
41 import java.util.stream.Collectors;
42
43 import com.google.common.collect.ImmutableMap;
44 import com.google.common.collect.Maps;
45 import com.puppycrawl.tools.checkstyle.LocalizedMessage.Utf8Control;
46 import com.puppycrawl.tools.checkstyle.api.AuditListener;
47 import com.puppycrawl.tools.checkstyle.api.Configuration;
48 import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser;
49 import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration;
50 import com.puppycrawl.tools.checkstyle.bdd.TestInputViolation;
51 import com.puppycrawl.tools.checkstyle.internal.utils.BriefUtLogger;
52 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
53 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
54 import com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil;
55
56 public abstract class AbstractModuleTestSupport extends AbstractPathTestSupport {
57
58 protected static final String ROOT_MODULE_NAME = Checker.class.getSimpleName();
59
60 private final ByteArrayOutputStream stream = new ByteArrayOutputStream();
61
62
63
64
65
66
67 protected final ByteArrayOutputStream getStream() {
68 return stream;
69 }
70
71
72
73
74
75
76 protected final BriefUtLogger getBriefUtLogger() {
77 return new BriefUtLogger(stream);
78 }
79
80
81
82
83
84
85
86
87 protected static DefaultConfiguration createModuleConfig(Class<?> clazz) {
88 return new DefaultConfiguration(clazz.getName());
89 }
90
91
92
93
94
95
96
97
98 protected final Checker createChecker(Configuration moduleConfig)
99 throws Exception {
100 final String moduleName = moduleConfig.getName();
101 final Checker checker = new Checker();
102 checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
103
104 if (ROOT_MODULE_NAME.equals(moduleName)) {
105 checker.configure(moduleConfig);
106 }
107 else {
108 configureChecker(checker, moduleConfig);
109 }
110
111 checker.addListener(getBriefUtLogger());
112 return checker;
113 }
114
115
116
117
118
119
120
121
122 protected void configureChecker(Checker checker, Configuration moduleConfig) throws Exception {
123 final Class<?> moduleClass = Class.forName(moduleConfig.getName());
124
125 if (ModuleReflectionUtil.isCheckstyleTreeWalkerCheck(moduleClass)
126 || ModuleReflectionUtil.isTreeWalkerFilterModule(moduleClass)) {
127 final Configuration config = createTreeWalkerConfig(moduleConfig);
128 checker.configure(config);
129 }
130 else {
131 final Configuration config = createRootConfig(moduleConfig);
132 checker.configure(config);
133 }
134 }
135
136
137
138
139
140
141
142
143
144 protected static DefaultConfiguration createTreeWalkerConfig(Configuration config) {
145 final DefaultConfiguration rootConfig =
146 new DefaultConfiguration(ROOT_MODULE_NAME);
147 final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class);
148
149 rootConfig.addProperty("charset", StandardCharsets.UTF_8.name());
150 rootConfig.addChild(twConf);
151 twConf.addChild(config);
152 return rootConfig;
153 }
154
155
156
157
158
159
160
161 protected static DefaultConfiguration createRootConfig(Configuration config) {
162 final DefaultConfiguration rootConfig = new DefaultConfiguration(ROOT_MODULE_NAME);
163 if (config != null) {
164 rootConfig.addChild(config);
165 }
166 return rootConfig;
167 }
168
169
170
171
172
173
174
175
176
177 protected final String getNonCompilablePath(String filename) throws IOException {
178 return new File("src/" + getResourceLocation()
179 + "/resources-noncompilable/" + getPackageLocation() + "/"
180 + filename).getCanonicalPath();
181 }
182
183
184
185
186
187
188
189
190 protected final String getUriString(String filename) {
191 return new File("src/test/resources/" + getPackageLocation() + "/" + filename).toURI()
192 .toString();
193 }
194
195
196
197
198
199
200
201
202
203
204
205 protected final void verifyFilterWithInlineConfigParser(String filePath,
206 String[] expectedUnfiltered,
207 String... expectedFiltered)
208 throws Exception {
209 final TestInputConfiguration testInputConfiguration =
210 InlineConfigParser.parseWithFilteredViolations(filePath);
211 final DefaultConfiguration configWithoutFilters =
212 testInputConfiguration.createConfigurationWithoutFilters();
213 final List<TestInputViolation> violationsWithoutFilters =
214 new ArrayList<>(testInputConfiguration.getViolations());
215 violationsWithoutFilters.addAll(testInputConfiguration.getFilteredViolations());
216 Collections.sort(violationsWithoutFilters);
217 verifyViolations(configWithoutFilters, filePath, violationsWithoutFilters);
218 verify(configWithoutFilters, filePath, expectedUnfiltered);
219 final DefaultConfiguration configWithFilters =
220 testInputConfiguration.createConfiguration();
221 verifyViolations(configWithFilters, filePath, testInputConfiguration.getViolations());
222 verify(configWithFilters, filePath, expectedFiltered);
223 }
224
225
226
227
228
229
230
231
232
233
234 protected final void verifyWithInlineXmlConfig(String filePath, String... expected)
235 throws Exception {
236 final TestInputConfiguration testInputConfiguration =
237 InlineConfigParser.parseWithXmlHeader(filePath);
238 final Configuration xmlConfig =
239 testInputConfiguration.getXmlConfiguration();
240 verifyViolations(xmlConfig, filePath, testInputConfiguration.getViolations());
241 verify(xmlConfig, filePath, expected);
242 }
243
244
245
246
247
248
249
250
251
252
253 protected final void verifyWithInlineConfigParser(String filePath, String... expected)
254 throws Exception {
255 final TestInputConfiguration testInputConfiguration =
256 InlineConfigParser.parse(filePath);
257 final DefaultConfiguration parsedConfig =
258 testInputConfiguration.createConfiguration();
259 final List<String> actualViolations = getActualViolationsForFile(parsedConfig, filePath);
260 verifyViolations(filePath, testInputConfiguration.getViolations(), actualViolations);
261 assertWithMessage("Violations for %s differ.", filePath)
262 .that(actualViolations)
263 .containsExactlyElementsIn(expected);
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277 protected final void verifyWithInlineConfigParser(String filePath1,
278 String filePath2,
279 String... expected)
280 throws Exception {
281 final TestInputConfiguration testInputConfiguration1 =
282 InlineConfigParser.parse(filePath1);
283 final DefaultConfiguration parsedConfig =
284 testInputConfiguration1.createConfiguration();
285 final TestInputConfiguration testInputConfiguration2 =
286 InlineConfigParser.parse(filePath2);
287 verifyViolations(parsedConfig, filePath1, testInputConfiguration1.getViolations());
288 verifyViolations(parsedConfig, filePath2, testInputConfiguration2.getViolations());
289 verify(createChecker(parsedConfig),
290 new File[] {new File(filePath1), new File(filePath2)},
291 filePath1,
292 expected);
293 }
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308 protected final void verifyWithInlineConfigParser(String filePath1,
309 String filePath2,
310 List<String> expectedFromFile1,
311 List<String> expectedFromFile2)
312 throws Exception {
313 final TestInputConfiguration testInputConfiguration = InlineConfigParser.parse(filePath1);
314 final DefaultConfiguration parsedConfig = testInputConfiguration.createConfiguration();
315 final TestInputConfiguration testInputConfiguration2 = InlineConfigParser.parse(filePath2);
316 final DefaultConfiguration parsedConfig2 = testInputConfiguration.createConfiguration();
317 final File[] inputs = {new File(filePath1), new File(filePath2)};
318 verifyViolations(parsedConfig, filePath1, testInputConfiguration.getViolations());
319 verifyViolations(parsedConfig2, filePath2, testInputConfiguration2.getViolations());
320 verify(createChecker(parsedConfig), inputs, ImmutableMap.of(
321 filePath1, expectedFromFile1,
322 filePath2, expectedFromFile2));
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336 protected final void verifyWithInlineConfigParserSeparateConfigAndTarget(String fileWithConfig,
337 String targetFile,
338 String... expected)
339 throws Exception {
340 final TestInputConfiguration testInputConfiguration1 =
341 InlineConfigParser.parse(fileWithConfig);
342 final DefaultConfiguration parsedConfig =
343 testInputConfiguration1.createConfiguration();
344 final List<TestInputViolation> inputViolations =
345 InlineConfigParser.getViolationsFromInputFile(targetFile);
346 final List<String> actualViolations = getActualViolationsForFile(parsedConfig, targetFile);
347 verifyViolations(targetFile, inputViolations, actualViolations);
348 assertWithMessage("Violations for %s differ.", targetFile)
349 .that(actualViolations)
350 .containsExactlyElementsIn(expected);
351 }
352
353
354
355
356
357
358
359
360
361
362 protected void verifyWithInlineConfigParserTwice(String filePath, String... expected)
363 throws Exception {
364 final TestInputConfiguration testInputConfiguration =
365 InlineConfigParser.parse(filePath);
366 final DefaultConfiguration parsedConfig =
367 testInputConfiguration.createConfiguration();
368 verifyViolations(parsedConfig, filePath, testInputConfiguration.getViolations());
369 verify(parsedConfig, filePath, expected);
370 }
371
372
373
374
375
376
377
378
379
380
381
382
383 protected void verifyWithInlineConfigParserAndLogger(String inputFile,
384 String expectedReportFile,
385 AuditListener logger,
386 ByteArrayOutputStream outputStream)
387 throws Exception {
388 final TestInputConfiguration testInputConfiguration =
389 InlineConfigParser.parse(inputFile);
390 final DefaultConfiguration parsedConfig =
391 testInputConfiguration.createConfiguration();
392 final List<File> filesToCheck = Collections.singletonList(new File(inputFile));
393 final String basePath = Path.of("").toAbsolutePath().toString();
394
395 final Checker checker = createChecker(parsedConfig);
396 checker.setBasedir(basePath);
397 checker.addListener(logger);
398 checker.process(filesToCheck);
399
400 verifyContent(expectedReportFile, outputStream);
401 }
402
403
404
405
406
407
408
409
410
411
412
413
414 protected final void verify(Configuration config, String fileName, String... expected)
415 throws Exception {
416 verify(createChecker(config), fileName, fileName, expected);
417 }
418
419
420
421
422
423
424
425
426
427
428
429
430
431 protected void verify(Checker checker, String fileName, String... expected)
432 throws Exception {
433 verify(checker, fileName, fileName, expected);
434 }
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449 protected final void verify(Checker checker,
450 String processedFilename,
451 String messageFileName,
452 String... expected)
453 throws Exception {
454 verify(checker,
455 new File[] {new File(processedFilename)},
456 messageFileName, expected);
457 }
458
459
460
461
462
463
464
465
466
467
468
469 protected void verify(Checker checker,
470 File[] processedFiles,
471 String messageFileName,
472 String... expected)
473 throws Exception {
474 final Map<String, List<String>> expectedViolations = new HashMap<>();
475 expectedViolations.put(messageFileName, Arrays.asList(expected));
476 verify(checker, processedFiles, expectedViolations);
477 }
478
479
480
481
482
483
484
485
486
487 protected final void verify(Checker checker,
488 File[] processedFiles,
489 Map<String, List<String>> expectedViolations)
490 throws Exception {
491 stream.flush();
492 stream.reset();
493 final List<File> theFiles = new ArrayList<>();
494 Collections.addAll(theFiles, processedFiles);
495 final int errs = checker.process(theFiles);
496
497
498 final Map<String, List<String>> actualViolations = getActualViolations(errs);
499 final Map<String, List<String>> realExpectedViolations =
500 Maps.filterValues(expectedViolations, input -> !input.isEmpty());
501
502 assertWithMessage("Files with expected violations and actual violations differ.")
503 .that(actualViolations.keySet())
504 .isEqualTo(realExpectedViolations.keySet());
505
506 realExpectedViolations.forEach((fileName, violationList) -> {
507 assertWithMessage("Violations for %s differ.", fileName)
508 .that(actualViolations.get(fileName))
509 .containsExactlyElementsIn(violationList);
510 });
511
512 checker.destroy();
513 }
514
515
516
517
518
519
520
521
522 protected final void verifyWithLimitedResources(String fileName, String... expected)
523 throws Exception {
524
525 final Void result = TestUtil.getResultWithLimitedResources(() -> {
526 verifyWithInlineConfigParser(fileName, expected);
527 return null;
528 });
529 assertWithMessage("Verify should complete successfully.")
530 .that(result)
531 .isNull();
532 }
533
534
535
536
537
538
539
540
541 protected final void execute(Configuration config, String... filenames) throws Exception {
542 final Checker checker = createChecker(config);
543 final List<File> files = Arrays.stream(filenames)
544 .map(File::new)
545 .collect(Collectors.toUnmodifiableList());
546 checker.process(files);
547 checker.destroy();
548 }
549
550
551
552
553
554
555
556
557 protected static void execute(Checker checker, String... filenames) throws Exception {
558 final List<File> files = Arrays.stream(filenames)
559 .map(File::new)
560 .collect(Collectors.toUnmodifiableList());
561 checker.process(files);
562 checker.destroy();
563 }
564
565
566
567
568
569
570
571
572
573 private void verifyViolations(Configuration config,
574 String file,
575 List<TestInputViolation> testInputViolations)
576 throws Exception {
577 final List<String> actualViolations = getActualViolationsForFile(config, file);
578 final List<Integer> actualViolationLines = actualViolations.stream()
579 .map(violation -> violation.substring(0, violation.indexOf(':')))
580 .map(Integer::valueOf)
581 .collect(Collectors.toUnmodifiableList());
582 final List<Integer> expectedViolationLines = testInputViolations.stream()
583 .map(TestInputViolation::getLineNo)
584 .collect(Collectors.toUnmodifiableList());
585 assertWithMessage("Violation lines for %s differ.", file)
586 .that(actualViolationLines)
587 .isEqualTo(expectedViolationLines);
588 for (int index = 0; index < actualViolations.size(); index++) {
589 assertWithMessage("Actual and expected violations differ.")
590 .that(actualViolations.get(index))
591 .matches(testInputViolations.get(index).toRegex());
592 }
593 }
594
595
596
597
598
599
600
601
602 private static void verifyViolations(String file,
603 List<TestInputViolation> testInputViolations,
604 List<String> actualViolations) {
605 final List<Integer> actualViolationLines = actualViolations.stream()
606 .map(violation -> violation.substring(0, violation.indexOf(':')))
607 .map(Integer::valueOf)
608 .collect(Collectors.toUnmodifiableList());
609 final List<Integer> expectedViolationLines = testInputViolations.stream()
610 .map(TestInputViolation::getLineNo)
611 .collect(Collectors.toUnmodifiableList());
612 assertWithMessage("Violation lines for %s differ.", file)
613 .that(actualViolationLines)
614 .isEqualTo(expectedViolationLines);
615 for (int index = 0; index < actualViolations.size(); index++) {
616 assertWithMessage("Actual and expected violations differ.")
617 .that(actualViolations.get(index))
618 .matches(testInputViolations.get(index).toRegex());
619 }
620 }
621
622
623
624
625
626
627
628
629 private static void verifyContent(
630 String expectedOutputFile,
631 ByteArrayOutputStream outputStream) throws IOException {
632 final String expectedContent = readFile(expectedOutputFile);
633 final String actualContent =
634 toLfLineEnding(outputStream.toString(StandardCharsets.UTF_8));
635 assertWithMessage("Content should match")
636 .that(actualContent)
637 .isEqualTo(expectedContent);
638 }
639
640
641
642
643
644
645
646
647
648 private List<String> getActualViolationsForFile(Configuration config,
649 String file) throws Exception {
650 stream.flush();
651 stream.reset();
652 final List<File> files = Collections.singletonList(new File(file));
653 final Checker checker = createChecker(config);
654 final Map<String, List<String>> actualViolations =
655 getActualViolations(checker.process(files));
656 checker.destroy();
657 return actualViolations.getOrDefault(file, new ArrayList<>());
658 }
659
660
661
662
663
664
665
666
667
668
669 private Map<String, List<String>> getActualViolations(int errorCount) throws IOException {
670
671 try (ByteArrayInputStream inputStream =
672 new ByteArrayInputStream(stream.toByteArray());
673 LineNumberReader lnr = new LineNumberReader(
674 new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
675 final Map<String, List<String>> actualViolations = new HashMap<>();
676 for (String line = lnr.readLine(); line != null && lnr.getLineNumber() <= errorCount;
677 line = lnr.readLine()) {
678
679
680 final String[] actualViolation = line.split("(?<=.{2}):", 2);
681 final String actualViolationFileName = actualViolation[0];
682 final String actualViolationMessage = actualViolation[1];
683
684 actualViolations
685 .computeIfAbsent(actualViolationFileName, key -> new ArrayList<>())
686 .add(actualViolationMessage);
687 }
688
689 return actualViolations;
690 }
691 }
692
693
694
695
696
697
698
699
700
701 protected final String getCheckMessage(String messageKey, Object... arguments) {
702 return internalGetCheckMessage(getMessageBundle(), messageKey, arguments);
703 }
704
705
706
707
708
709
710
711
712
713
714 protected static String getCheckMessage(
715 Class<?> clazz, String messageKey, Object... arguments) {
716 return internalGetCheckMessage(getMessageBundle(clazz.getName()), messageKey, arguments);
717 }
718
719
720
721
722
723
724
725
726
727
728 private static String internalGetCheckMessage(
729 String messageBundle, String messageKey, Object... arguments) {
730 final ResourceBundle resourceBundle = ResourceBundle.getBundle(
731 messageBundle,
732 Locale.ROOT,
733 Thread.currentThread().getContextClassLoader(),
734 new Utf8Control());
735 final String pattern = resourceBundle.getString(messageKey);
736 final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT);
737 return formatter.format(arguments);
738 }
739
740
741
742
743
744
745 private String getMessageBundle() {
746 final String className = getClass().getName();
747 return getMessageBundle(className);
748 }
749
750
751
752
753
754
755
756 private static String getMessageBundle(String className) {
757 final String messageBundle;
758 final String messages = "messages";
759 final int endIndex = className.lastIndexOf('.');
760 final Map<String, String> messageBundleMappings = new HashMap<>();
761 messageBundleMappings.put("SeverityMatchFilterExamplesTest",
762 "com.puppycrawl.tools.checkstyle.checks.naming.messages");
763
764 if (endIndex < 0) {
765 messageBundle = messages;
766 }
767 else {
768 final String packageName = className.substring(0, endIndex);
769 if ("com.puppycrawl.tools.checkstyle.filters".equals(packageName)) {
770 messageBundle = messageBundleMappings.get(className.substring(endIndex + 1));
771 }
772 else {
773 messageBundle = packageName + "." + messages;
774 }
775 }
776 return messageBundle;
777 }
778
779
780
781
782
783
784
785
786 protected static String[] removeSuppressed(String[] actualViolations,
787 String... suppressedViolations) {
788 final List<String> actualViolationsList =
789 Arrays.stream(actualViolations).collect(Collectors.toCollection(ArrayList::new));
790 actualViolationsList.removeAll(Arrays.asList(suppressedViolations));
791 return actualViolationsList.toArray(CommonUtil.EMPTY_STRING_ARRAY);
792 }
793
794 }