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.api.DetailAST;
49 import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser;
50 import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration;
51 import com.puppycrawl.tools.checkstyle.bdd.TestInputViolation;
52 import com.puppycrawl.tools.checkstyle.internal.utils.BriefUtLogger;
53 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
54 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
55 import com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil;
56 import com.puppycrawl.tools.checkstyle.xpath.RootNode;
57
58 public abstract class AbstractModuleTestSupport extends AbstractPathTestSupport {
59
60 protected static final String ROOT_MODULE_NAME = Checker.class.getSimpleName();
61
62 private final ByteArrayOutputStream stream = new ByteArrayOutputStream();
63
64
65
66
67
68
69 protected final ByteArrayOutputStream getStream() {
70 return stream;
71 }
72
73
74
75
76
77
78 protected final DefaultLogger getBriefUtLogger() {
79 return new BriefUtLogger(stream);
80 }
81
82
83
84
85
86
87
88
89 protected static DefaultConfiguration createModuleConfig(Class<?> clazz) {
90 return new DefaultConfiguration(clazz.getName());
91 }
92
93
94
95
96
97
98
99
100 protected final Checker createChecker(Configuration moduleConfig)
101 throws Exception {
102 final String moduleName = moduleConfig.getName();
103 final Checker checker = new Checker();
104 checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
105
106 if (ROOT_MODULE_NAME.equals(moduleName)) {
107 checker.configure(moduleConfig);
108 }
109 else {
110 configureChecker(checker, moduleConfig);
111 }
112
113 checker.addListener(getBriefUtLogger());
114 return checker;
115 }
116
117
118
119
120
121
122
123
124 protected void configureChecker(Checker checker, Configuration moduleConfig) throws Exception {
125 final Class<?> moduleClass = Class.forName(moduleConfig.getName());
126
127 final Configuration config;
128 if (ModuleReflectionUtil.isCheckstyleTreeWalkerCheck(moduleClass)
129 || ModuleReflectionUtil.isTreeWalkerFilterModule(moduleClass)) {
130 config = createTreeWalkerConfig(moduleConfig);
131 }
132 else {
133 config = createRootConfig(moduleConfig);
134 }
135 checker.configure(config);
136 }
137
138
139
140
141
142
143
144
145
146 protected static DefaultConfiguration createTreeWalkerConfig(Configuration config) {
147 final DefaultConfiguration rootConfig =
148 new DefaultConfiguration(ROOT_MODULE_NAME);
149 final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class);
150
151 rootConfig.addProperty("charset", StandardCharsets.UTF_8.name());
152 rootConfig.addChild(twConf);
153 twConf.addChild(config);
154 return rootConfig;
155 }
156
157
158
159
160
161
162
163 protected static DefaultConfiguration createRootConfig(Configuration config) {
164 final DefaultConfiguration rootConfig = new DefaultConfiguration(ROOT_MODULE_NAME);
165 if (config != null) {
166 rootConfig.addChild(config);
167 }
168 return rootConfig;
169 }
170
171
172
173
174
175
176
177
178
179 protected final String getNonCompilablePath(String filename) throws IOException {
180 return new File("src/" + getResourceLocation()
181 + "/resources-noncompilable/" + getPackageLocation() + "/"
182 + filename).getCanonicalPath();
183 }
184
185
186
187
188
189
190
191
192 protected final String getJavadocWithErrorPath(String filename) throws IOException {
193 return new File("src/" + getResourceLocation()
194 + "/resources-with-javadoc-error/" + getPackageLocation() + "/"
195 + filename).getCanonicalPath();
196 }
197
198
199
200
201
202
203
204
205 protected RootNode getRootNodeForNonCompilable(String fileName) throws Exception {
206 final File file = new File(getNonCompilablePath(fileName));
207 final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
208 return new RootNode(rootAst);
209 }
210
211
212
213
214
215
216
217
218 protected final String getUriString(String filename) {
219 return new File("src/test/resources/" + getPackageLocation() + "/" + filename).toURI()
220 .toString();
221 }
222
223
224
225
226
227
228
229
230
231
232
233 protected final void verifyFilterWithInlineConfigParser(String filePath,
234 String[] expectedUnfiltered,
235 String... expectedFiltered)
236 throws Exception {
237 final TestInputConfiguration testInputConfiguration =
238 InlineConfigParser.parseWithFilteredViolations(filePath);
239 final DefaultConfiguration configWithoutFilters =
240 testInputConfiguration.createConfigurationWithoutFilters();
241 final List<TestInputViolation> violationsWithoutFilters =
242 new ArrayList<>(testInputConfiguration.getViolations());
243 violationsWithoutFilters.addAll(testInputConfiguration.getFilteredViolations());
244 Collections.sort(violationsWithoutFilters);
245 verifyViolations(configWithoutFilters, filePath, violationsWithoutFilters);
246 verify(configWithoutFilters, filePath, expectedUnfiltered);
247 final DefaultConfiguration configWithFilters =
248 testInputConfiguration.createConfiguration();
249 verifyViolations(configWithFilters, filePath, testInputConfiguration.getViolations());
250 verify(configWithFilters, filePath, expectedFiltered);
251 }
252
253
254
255
256
257
258
259
260
261
262 protected final void verifyWithInlineXmlConfig(String filePath, String... expected)
263 throws Exception {
264 final TestInputConfiguration testInputConfiguration =
265 InlineConfigParser.parseWithXmlHeader(filePath);
266 final Configuration xmlConfig =
267 testInputConfiguration.getXmlConfiguration();
268 verifyViolations(xmlConfig, filePath, testInputConfiguration.getViolations());
269 verify(xmlConfig, filePath, expected);
270 }
271
272
273
274
275
276
277
278
279
280
281 protected void verifyWithExternalXmlConfig(
282 String configPath,
283 String filePath,
284 String... expected)
285 throws Exception {
286 final Configuration config =
287 ConfigurationLoader.loadConfiguration(
288 configPath,
289 new PropertiesExpander(System.getProperties()),
290 ConfigurationLoader.IgnoredModulesOptions.EXECUTE);
291 verify(config, filePath, expected);
292 }
293
294
295
296
297
298
299
300
301
302
303 protected final void verifyWithInlineConfigParser(String filePath, String... expected)
304 throws Exception {
305 final TestInputConfiguration testInputConfiguration =
306 InlineConfigParser.parse(filePath);
307 final DefaultConfiguration parsedConfig =
308 testInputConfiguration.createConfiguration();
309 final List<String> actualViolations = getActualViolationsForFile(parsedConfig, filePath);
310 verifyViolations(filePath, testInputConfiguration.getViolations(), actualViolations);
311 assertWithMessage("Violations for %s differ.", filePath)
312 .that(actualViolations)
313 .containsExactlyElementsIn(expected);
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327 protected final void verifyWithInlineConfigParser(String filePath1,
328 String filePath2,
329 String... expected)
330 throws Exception {
331 final TestInputConfiguration testInputConfiguration1 =
332 InlineConfigParser.parse(filePath1);
333 final DefaultConfiguration parsedConfig =
334 testInputConfiguration1.createConfiguration();
335 final TestInputConfiguration testInputConfiguration2 =
336 InlineConfigParser.parse(filePath2);
337 verifyViolations(parsedConfig, filePath1, testInputConfiguration1.getViolations());
338 verifyViolations(parsedConfig, filePath2, testInputConfiguration2.getViolations());
339 verify(createChecker(parsedConfig),
340 new File[] {new File(filePath1), new File(filePath2)},
341 filePath1,
342 expected);
343 }
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358 protected final void verifyWithInlineConfigParser(String filePath1,
359 String filePath2,
360 List<String> expectedFromFile1,
361 List<String> expectedFromFile2)
362 throws Exception {
363 final TestInputConfiguration testInputConfiguration = InlineConfigParser.parse(filePath1);
364 final DefaultConfiguration parsedConfig = testInputConfiguration.createConfiguration();
365 final TestInputConfiguration testInputConfiguration2 = InlineConfigParser.parse(filePath2);
366 final DefaultConfiguration parsedConfig2 = testInputConfiguration.createConfiguration();
367 final File[] inputs = {new File(filePath1), new File(filePath2)};
368 verifyViolations(parsedConfig, filePath1, testInputConfiguration.getViolations());
369 verifyViolations(parsedConfig2, filePath2, testInputConfiguration2.getViolations());
370 verify(createChecker(parsedConfig), inputs, ImmutableMap.of(
371 filePath1, expectedFromFile1,
372 filePath2, expectedFromFile2));
373 }
374
375
376
377
378
379
380
381
382
383
384
385
386 protected final void verifyWithInlineConfigParserSeparateConfigAndTarget(String fileWithConfig,
387 String targetFile,
388 String... expected)
389 throws Exception {
390 final TestInputConfiguration testInputConfiguration1 =
391 InlineConfigParser.parse(fileWithConfig);
392 final DefaultConfiguration parsedConfig =
393 testInputConfiguration1.createConfiguration();
394 final List<TestInputViolation> inputViolations =
395 InlineConfigParser.getViolationsFromInputFile(targetFile);
396 final List<String> actualViolations = getActualViolationsForFile(parsedConfig, targetFile);
397 verifyViolations(targetFile, inputViolations, actualViolations);
398 assertWithMessage("Violations for %s differ.", targetFile)
399 .that(actualViolations)
400 .containsExactlyElementsIn(expected);
401 }
402
403
404
405
406
407
408
409
410
411
412
413
414 protected final void verifyFilterWithInlineConfigParserSeparateConfigAndTarget(
415 String fileWithConfig,
416 String targetFilePath,
417 String[] expectedUnfiltered,
418 String... expectedFiltered)
419 throws Exception {
420 final TestInputConfiguration testInputConfiguration =
421 InlineConfigParser.parseWithFilteredViolations(fileWithConfig);
422 final DefaultConfiguration configWithoutFilters =
423 testInputConfiguration.createConfigurationWithoutFilters();
424 final List<TestInputViolation> violationsWithoutFilters = new ArrayList<>(
425 InlineConfigParser.getFilteredViolationsFromInputFile(targetFilePath));
426 violationsWithoutFilters.addAll(
427 InlineConfigParser.getViolationsFromInputFile(targetFilePath));
428 Collections.sort(violationsWithoutFilters);
429 verifyViolations(configWithoutFilters, targetFilePath, violationsWithoutFilters);
430 verify(configWithoutFilters, targetFilePath, expectedUnfiltered);
431 final DefaultConfiguration configWithFilters =
432 testInputConfiguration.createConfiguration();
433 final List<TestInputViolation> violationsWithFilters =
434 InlineConfigParser.getViolationsFromInputFile(targetFilePath);
435 verifyViolations(configWithFilters, targetFilePath, violationsWithFilters);
436 verify(configWithFilters, targetFilePath, expectedFiltered);
437 }
438
439
440
441
442
443
444
445
446
447
448 protected void verifyWithInlineConfigParserTwice(String filePath, String... expected)
449 throws Exception {
450 final TestInputConfiguration testInputConfiguration =
451 InlineConfigParser.parse(filePath);
452 final DefaultConfiguration parsedConfig =
453 testInputConfiguration.createConfiguration();
454 verifyViolations(parsedConfig, filePath, testInputConfiguration.getViolations());
455 verify(parsedConfig, filePath, expected);
456 }
457
458
459
460
461
462
463
464
465
466
467
468
469 protected void verifyWithInlineConfigParserAndLogger(String inputFile,
470 String expectedReportFile,
471 AuditListener logger,
472 ByteArrayOutputStream outputStream)
473 throws Exception {
474 final TestInputConfiguration testInputConfiguration =
475 InlineConfigParser.parse(inputFile);
476 final DefaultConfiguration parsedConfig =
477 testInputConfiguration.createConfiguration();
478 final List<File> filesToCheck = Collections.singletonList(new File(inputFile));
479 final String basePath = Path.of("").toAbsolutePath().toString();
480
481 final Checker checker = createChecker(parsedConfig);
482 checker.setBasedir(basePath);
483 checker.addListener(logger);
484 checker.process(filesToCheck);
485
486 verifyContent(expectedReportFile, outputStream);
487 }
488
489
490
491
492
493
494
495
496
497
498
499
500 protected final void verifyWithInlineConfigParserAndDefaultLogger(String inputFile,
501 String expectedOutputFile,
502 AuditListener logger,
503 ByteArrayOutputStream outputStream)
504 throws Exception {
505 final TestInputConfiguration testInputConfiguration =
506 InlineConfigParser.parseWithXmlHeader(inputFile);
507 final Configuration parsedConfig =
508 testInputConfiguration.getXmlConfiguration();
509 final List<File> filesToCheck = Collections.singletonList(new File(inputFile));
510 final String basePath = Path.of("").toAbsolutePath().toString();
511
512 final Checker checker = createChecker(parsedConfig);
513 checker.setBasedir(basePath);
514 checker.addListener(logger);
515 checker.process(filesToCheck);
516
517 verifyCleanedMessageContent(expectedOutputFile, outputStream, basePath);
518 }
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537 protected final void verifyWithInlineConfigParserAndDefaultLogger(String inputFile,
538 String expectedInfoFile,
539 String expectedErrorFile,
540 AuditListener logger,
541 ByteArrayOutputStream infoStream,
542 ByteArrayOutputStream errorStream)
543 throws Exception {
544 final TestInputConfiguration testInputConfiguration =
545 InlineConfigParser.parseWithXmlHeader(inputFile);
546 final Configuration parsedConfig =
547 testInputConfiguration.getXmlConfiguration();
548 final List<File> filesToCheck = Collections.singletonList(new File(inputFile));
549 final String basePath = Path.of("").toAbsolutePath().toString();
550
551 final Checker checker = createChecker(parsedConfig);
552 checker.setBasedir(basePath);
553 checker.addListener(logger);
554 checker.process(filesToCheck);
555
556 verifyContent(expectedInfoFile, infoStream);
557 verifyCleanedMessageContent(expectedErrorFile, errorStream, basePath);
558 }
559
560
561
562
563
564
565
566
567
568
569
570
571 protected final void verify(Configuration config, String fileName, String... expected)
572 throws Exception {
573 verify(createChecker(config), fileName, fileName, expected);
574 }
575
576
577
578
579
580
581
582
583
584
585
586
587
588 protected void verify(Checker checker, String fileName, String... expected)
589 throws Exception {
590 verify(checker, fileName, fileName, expected);
591 }
592
593
594
595
596
597
598
599
600
601 protected final void verify(Checker checker,
602 File[] processedFiles,
603 Map<String, List<String>> expectedViolations)
604 throws Exception {
605 stream.flush();
606 stream.reset();
607 final List<File> theFiles = new ArrayList<>();
608 Collections.addAll(theFiles, processedFiles);
609 checker.process(theFiles);
610
611
612 final Map<String, List<String>> actualViolations = getActualViolations();
613 final Map<String, List<String>> realExpectedViolations =
614 Maps.filterValues(expectedViolations, input -> !input.isEmpty());
615
616 assertWithMessage("Files with expected violations and actual violations differ.")
617 .that(actualViolations.keySet())
618 .isEqualTo(realExpectedViolations.keySet());
619
620 realExpectedViolations.forEach((fileName, violationList) -> {
621 assertWithMessage("Violations for %s differ.", fileName)
622 .that(actualViolations.get(fileName))
623 .containsExactlyElementsIn(violationList);
624 });
625
626 checker.destroy();
627 }
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642 protected final void verify(Checker checker,
643 String processedFilename,
644 String messageFileName,
645 String... expected)
646 throws Exception {
647 verify(checker,
648 new File[] {new File(processedFilename)},
649 messageFileName, expected);
650 }
651
652
653
654
655
656
657
658
659
660
661
662 protected void verify(Checker checker,
663 File[] processedFiles,
664 String messageFileName,
665 String... expected)
666 throws Exception {
667 final Map<String, List<String>> expectedViolations = new HashMap<>();
668 expectedViolations.put(messageFileName, Arrays.asList(expected));
669 verify(checker, processedFiles, expectedViolations);
670 }
671
672
673
674
675
676
677
678
679 protected final void verifyWithLimitedResources(String fileName, String... expected)
680 throws Exception {
681 TestUtil.getResultWithLimitedResources(() -> {
682 verifyWithInlineConfigParser(fileName, expected);
683 return null;
684 });
685 }
686
687
688
689
690
691
692
693
694
695
696 protected final void verifyWithLimitedXpathResources(String fileName, String... expected)
697 throws Exception {
698 TestUtil.runWithLimitedXpathResources(() -> {
699 verifyWithInlineConfigParser(fileName, expected);
700 return null;
701 });
702 }
703
704
705
706
707
708
709
710
711 protected final void execute(Configuration config, String... filenames) throws Exception {
712 final Checker checker = createChecker(config);
713 final List<File> files = Arrays.stream(filenames)
714 .map(File::new)
715 .toList();
716 checker.process(files);
717 checker.destroy();
718 }
719
720
721
722
723
724
725
726
727 protected static void execute(Checker checker, String... filenames) throws Exception {
728 final List<File> files = Arrays.stream(filenames)
729 .map(File::new)
730 .toList();
731 checker.process(files);
732 checker.destroy();
733 }
734
735
736
737
738
739
740
741
742
743 private void verifyViolations(Configuration config,
744 String file,
745 List<TestInputViolation> testInputViolations)
746 throws Exception {
747 final List<String> actualViolations = getActualViolationsForFile(config, file);
748 final List<Integer> actualViolationLines = actualViolations.stream()
749 .map(violation -> violation.substring(0, violation.indexOf(':')))
750 .map(Integer::valueOf)
751 .toList();
752 final List<Integer> expectedViolationLines = testInputViolations.stream()
753 .map(TestInputViolation::getLineNo)
754 .toList();
755 assertWithMessage("Violation lines for %s differ.", file)
756 .that(actualViolationLines)
757 .isEqualTo(expectedViolationLines);
758 for (int index = 0; index < actualViolations.size(); index++) {
759 assertWithMessage("Actual and expected violations differ.")
760 .that(actualViolations.get(index))
761 .matches(testInputViolations.get(index).toRegex());
762 }
763 }
764
765
766
767
768
769
770
771
772 private static void verifyViolations(String file,
773 List<TestInputViolation> testInputViolations,
774 List<String> actualViolations) {
775 final List<Integer> actualViolationLines = actualViolations.stream()
776 .map(violation -> violation.substring(0, violation.indexOf(':')))
777 .map(Integer::valueOf)
778 .toList();
779 final List<Integer> expectedViolationLines = testInputViolations.stream()
780 .map(TestInputViolation::getLineNo)
781 .toList();
782 assertWithMessage("Violation lines for %s differ.", file)
783 .that(actualViolationLines)
784 .isEqualTo(expectedViolationLines);
785 for (int index = 0; index < actualViolations.size(); index++) {
786 assertWithMessage("Actual and expected violations differ.")
787 .that(actualViolations.get(index))
788 .matches(testInputViolations.get(index).toRegex());
789 }
790 }
791
792
793
794
795
796
797
798
799 private static void verifyContent(
800 String expectedOutputFile,
801 ByteArrayOutputStream outputStream) throws IOException {
802 final String expectedContent = readFile(expectedOutputFile);
803 final String actualContent =
804 toLfLineEnding(outputStream.toString(StandardCharsets.UTF_8));
805 assertWithMessage("Content should match")
806 .that(actualContent)
807 .isEqualTo(expectedContent);
808 }
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829 private static void verifyCleanedMessageContent(
830 String expectedOutputFile,
831 ByteArrayOutputStream outputStream,
832 String basePath) throws IOException {
833 final String expectedContent = readFile(expectedOutputFile);
834 final String rawActualContent =
835 toLfLineEnding(outputStream.toString(StandardCharsets.UTF_8));
836
837 final String cleanedActualContent = rawActualContent.lines()
838 .filter(line -> {
839 return line.startsWith("[")
840 || line.contains("Starting audit...")
841 || line.contains("Audit done.");
842 })
843 .map(line -> line.replace(basePath, ""))
844 .map(line -> line.replace('\\', '/'))
845 .collect(Collectors.joining("\n", "", "\n"));
846
847 assertWithMessage("Content should match")
848 .that(cleanedActualContent)
849 .isEqualTo(expectedContent);
850 }
851
852
853
854
855
856
857
858
859
860 private List<String> getActualViolationsForFile(Configuration config,
861 String file) throws Exception {
862 stream.flush();
863 stream.reset();
864 final List<File> files = Collections.singletonList(new File(file));
865 final Checker checker = createChecker(config);
866 checker.process(files);
867 final Map<String, List<String>> actualViolations =
868 getActualViolations();
869 checker.destroy();
870 return actualViolations.getOrDefault(file, new ArrayList<>());
871 }
872
873
874
875
876
877
878
879
880
881 private Map<String, List<String>> getActualViolations() throws IOException {
882
883 try (ByteArrayInputStream inputStream =
884 new ByteArrayInputStream(stream.toByteArray());
885 LineNumberReader lnr = new LineNumberReader(
886 new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
887 final Map<String, List<String>> actualViolations = new HashMap<>();
888 for (String line = lnr.readLine(); line != null;
889 line = lnr.readLine()) {
890 if ("Audit done.".equals(line) || line.contains("at com")) {
891 break;
892 }
893
894
895 final String[] actualViolation = line.split("(?<=.{2}):", 2);
896 final String actualViolationFileName = actualViolation[0];
897 final String actualViolationMessage = actualViolation[1];
898
899 actualViolations
900 .computeIfAbsent(actualViolationFileName, key -> new ArrayList<>())
901 .add(actualViolationMessage);
902 }
903
904 return actualViolations;
905 }
906 }
907
908
909
910
911
912
913
914
915
916 protected final String getCheckMessage(String messageKey, Object... arguments) {
917 return internalGetCheckMessage(getMessageBundle(), messageKey, arguments);
918 }
919
920
921
922
923
924
925
926
927
928
929 protected static String getCheckMessage(
930 Class<?> clazz, String messageKey, Object... arguments) {
931 return internalGetCheckMessage(getMessageBundle(clazz.getName()), messageKey, arguments);
932 }
933
934
935
936
937
938
939
940
941
942
943 private static String internalGetCheckMessage(
944 String messageBundle, String messageKey, Object... arguments) {
945 final ResourceBundle resourceBundle = ResourceBundle.getBundle(
946 messageBundle,
947 Locale.ROOT,
948 Thread.currentThread().getContextClassLoader(),
949 new Utf8Control());
950 final String pattern = resourceBundle.getString(messageKey);
951 final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT);
952 return formatter.format(arguments);
953 }
954
955
956
957
958
959
960 private String getMessageBundle() {
961 final String className = getClass().getName();
962 return getMessageBundle(className);
963 }
964
965
966
967
968
969
970
971 private static String getMessageBundle(String className) {
972 final String messageBundle;
973 final String messages = "messages";
974 final int endIndex = className.lastIndexOf('.');
975 final Map<String, String> messageBundleMappings = new HashMap<>();
976 messageBundleMappings.put("SeverityMatchFilterExamplesTest",
977 "com.puppycrawl.tools.checkstyle.checks.naming.messages");
978
979 if (endIndex < 0) {
980 messageBundle = messages;
981 }
982 else {
983 final String packageName = className.substring(0, endIndex);
984 if ("com.puppycrawl.tools.checkstyle.filters".equals(packageName)) {
985 messageBundle = messageBundleMappings.get(className.substring(endIndex + 1));
986 }
987 else {
988 messageBundle = packageName + "." + messages;
989 }
990 }
991 return messageBundle;
992 }
993
994
995
996
997
998
999
1000
1001 protected static String[] removeSuppressed(String[] actualViolations,
1002 String... suppressedViolations) {
1003 final List<String> actualViolationsList =
1004 Arrays.stream(actualViolations).collect(Collectors.toCollection(ArrayList::new));
1005 actualViolationsList.removeAll(Arrays.asList(suppressedViolations));
1006 return actualViolationsList.toArray(CommonUtil.EMPTY_STRING_ARRAY);
1007 }
1008
1009 }