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.Files;
32 import java.nio.file.Path;
33 import java.text.MessageFormat;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.Map;
41 import java.util.ResourceBundle;
42 import java.util.stream.Collectors;
43
44 import com.google.common.collect.ImmutableMap;
45 import com.google.common.collect.Maps;
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.violations());
243 violationsWithoutFilters.addAll(testInputConfiguration.filteredViolations());
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.violations());
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.xmlConfiguration();
268 verifyViolations(xmlConfig, filePath, testInputConfiguration.violations());
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.violations(), 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 protected final void verifyWithInlineConfigParser(List<String> filePaths,
326 List<List<String>> expectedViolations)
327 throws Exception {
328 assertWithMessage("At least one input file must be supplied")
329 .that(filePaths)
330 .isNotEmpty();
331 assertWithMessage("Expected violations must be supplied for every input file")
332 .that(expectedViolations)
333 .hasSize(filePaths.size());
334 final TestInputConfiguration firstTestInputConfiguration =
335 InlineConfigParser.parse(filePaths.getFirst());
336 final DefaultConfiguration parsedConfig =
337 firstTestInputConfiguration.createConfiguration();
338 final File[] inputs = new File[filePaths.size()];
339 final ImmutableMap.Builder<String, List<String>> expectedViolationsByFile =
340 ImmutableMap.builder();
341
342 for (int index = 0; index < filePaths.size(); index++) {
343 final String filePath = filePaths.get(index);
344 final TestInputConfiguration testInputConfiguration =
345 InlineConfigParser.parse(filePath);
346 final DefaultConfiguration inputConfig =
347 testInputConfiguration.createConfiguration();
348 verifyViolations(inputConfig, filePath, testInputConfiguration.violations());
349 inputs[index] = new File(filePath);
350 expectedViolationsByFile.put(filePath, expectedViolations.get(index));
351 }
352
353 verify(createChecker(parsedConfig), inputs, expectedViolationsByFile.buildOrThrow());
354 }
355
356
357
358
359
360
361
362
363
364
365
366
367 protected final void verifyWithInlineConfigParser(String filePath1,
368 String filePath2,
369 String... expected)
370 throws Exception {
371 final TestInputConfiguration testInputConfiguration1 =
372 InlineConfigParser.parse(filePath1);
373 final DefaultConfiguration parsedConfig =
374 testInputConfiguration1.createConfiguration();
375 final TestInputConfiguration testInputConfiguration2 =
376 InlineConfigParser.parse(filePath2);
377 verifyViolations(parsedConfig, filePath1, testInputConfiguration1.violations());
378 verifyViolations(parsedConfig, filePath2, testInputConfiguration2.violations());
379 verify(createChecker(parsedConfig),
380 new File[] {new File(filePath1), new File(filePath2)},
381 filePath1,
382 expected);
383 }
384
385
386
387
388
389
390
391
392
393
394
395
396
397 protected final void verifyWithInlineConfigParser(String filePath1,
398 String filePath2,
399 List<String> expectedFromFile1,
400 List<String> expectedFromFile2)
401 throws Exception {
402 final TestInputConfiguration testInputConfiguration = InlineConfigParser.parse(filePath1);
403 final DefaultConfiguration parsedConfig = testInputConfiguration.createConfiguration();
404 final TestInputConfiguration testInputConfiguration2 = InlineConfigParser.parse(filePath2);
405 final DefaultConfiguration parsedConfig2 = testInputConfiguration.createConfiguration();
406 final File[] inputs = {new File(filePath1), new File(filePath2)};
407 verifyViolations(parsedConfig, filePath1, testInputConfiguration.violations());
408 verifyViolations(parsedConfig2, filePath2, testInputConfiguration2.violations());
409 verify(createChecker(parsedConfig), inputs, ImmutableMap.of(
410 filePath1, expectedFromFile1,
411 filePath2, expectedFromFile2));
412 }
413
414
415
416
417
418
419
420
421
422
423
424
425 protected final void verifyWithInlineConfigParserSeparateConfigAndTarget(String fileWithConfig,
426 String targetFile,
427 String... expected)
428 throws Exception {
429 final TestInputConfiguration testInputConfiguration1 =
430 InlineConfigParser.parse(fileWithConfig);
431 final DefaultConfiguration parsedConfig =
432 testInputConfiguration1.createConfiguration();
433 final List<String> actualViolations = getActualViolationsForFile(parsedConfig, targetFile);
434
435 if (!Files.readString(Path.of(targetFile)).isBlank()) {
436 final List<TestInputViolation> inputViolations =
437 InlineConfigParser.getViolationsFromInputFile(targetFile);
438 verifyViolations(targetFile, inputViolations, actualViolations);
439 }
440 assertWithMessage("Violations for %s differ.", targetFile)
441 .that(actualViolations)
442 .containsExactlyElementsIn(expected);
443 }
444
445
446
447
448
449
450
451
452
453
454 protected void verifyWithInlineConfigParserTwice(String filePath, String... expected)
455 throws Exception {
456 final TestInputConfiguration testInputConfiguration =
457 InlineConfigParser.parse(filePath);
458 final DefaultConfiguration parsedConfig =
459 testInputConfiguration.createConfiguration();
460 verifyViolations(parsedConfig, filePath, testInputConfiguration.violations());
461 verify(parsedConfig, filePath, expected);
462 }
463
464
465
466
467
468
469
470
471
472
473
474
475 protected void verifyWithInlineConfigParserAndLogger(String inputFile,
476 String expectedReportFile,
477 AuditListener logger,
478 ByteArrayOutputStream outputStream)
479 throws Exception {
480 final TestInputConfiguration testInputConfiguration =
481 InlineConfigParser.parse(inputFile);
482 final DefaultConfiguration parsedConfig =
483 testInputConfiguration.createConfiguration();
484 final List<File> filesToCheck = Collections.singletonList(new File(inputFile));
485 final String basePath = Path.of("").toAbsolutePath().toString();
486
487 final Checker checker = createChecker(parsedConfig);
488 checker.setBasedir(basePath);
489 checker.addListener(logger);
490 checker.process(filesToCheck);
491
492 verifyContent(expectedReportFile, outputStream);
493 }
494
495
496
497
498
499
500
501
502
503
504
505
506 protected final void verifyWithInlineConfigParserAndDefaultLogger(String inputFile,
507 String expectedOutputFile,
508 AuditListener logger,
509 ByteArrayOutputStream outputStream)
510 throws Exception {
511 final TestInputConfiguration testInputConfiguration =
512 InlineConfigParser.parseWithXmlHeader(inputFile);
513 final Configuration parsedConfig =
514 testInputConfiguration.xmlConfiguration();
515 final List<File> filesToCheck = Collections.singletonList(new File(inputFile));
516 final String basePath = Path.of("").toAbsolutePath().toString();
517
518 final Checker checker = createChecker(parsedConfig);
519 checker.setBasedir(basePath);
520 checker.addListener(logger);
521 checker.process(filesToCheck);
522
523 verifyCleanedMessageContent(expectedOutputFile, outputStream, basePath);
524 }
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543 protected final void verifyWithInlineConfigParserAndDefaultLogger(String inputFile,
544 String expectedInfoFile,
545 String expectedErrorFile,
546 AuditListener logger,
547 ByteArrayOutputStream infoStream,
548 ByteArrayOutputStream errorStream)
549 throws Exception {
550 final TestInputConfiguration testInputConfiguration =
551 InlineConfigParser.parseWithXmlHeader(inputFile);
552 final Configuration parsedConfig =
553 testInputConfiguration.xmlConfiguration();
554 final List<File> filesToCheck = Collections.singletonList(new File(inputFile));
555 final String basePath = Path.of("").toAbsolutePath().toString();
556
557 final Checker checker = createChecker(parsedConfig);
558 checker.setBasedir(basePath);
559 checker.addListener(logger);
560 checker.process(filesToCheck);
561
562 verifyContent(expectedInfoFile, infoStream);
563 verifyCleanedMessageContent(expectedErrorFile, errorStream, basePath);
564 }
565
566
567
568
569
570
571
572
573
574
575
576
577 protected final void verify(Configuration config, String fileName, String... expected)
578 throws Exception {
579 verify(createChecker(config), fileName, fileName, expected);
580 }
581
582
583
584
585
586
587
588
589
590
591
592
593
594 protected void verify(Checker checker, String fileName, String... expected)
595 throws Exception {
596 verify(checker, fileName, fileName, expected);
597 }
598
599
600
601
602
603
604
605
606
607 protected final void verify(Checker checker,
608 File[] processedFiles,
609 Map<String, List<String>> expectedViolations)
610 throws Exception {
611 stream.flush();
612 stream.reset();
613 final List<File> theFiles = new ArrayList<>();
614 Collections.addAll(theFiles, processedFiles);
615 checker.process(theFiles);
616
617
618 final Map<String, List<String>> actualViolations = getActualViolations();
619 final Map<String, List<String>> realExpectedViolations =
620 Maps.filterValues(expectedViolations, input -> !input.isEmpty());
621
622 assertWithMessage("Files with expected violations and actual violations differ.")
623 .that(actualViolations.keySet())
624 .isEqualTo(realExpectedViolations.keySet());
625
626 realExpectedViolations.forEach((fileName, violationList) -> {
627 assertWithMessage("Violations for %s differ.", fileName)
628 .that(actualViolations.get(fileName))
629 .containsExactlyElementsIn(violationList);
630 });
631
632 checker.destroy();
633 }
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648 protected final void verify(Checker checker,
649 String processedFilename,
650 String messageFileName,
651 String... expected)
652 throws Exception {
653 verify(checker,
654 new File[] {new File(processedFilename)},
655 messageFileName, expected);
656 }
657
658
659
660
661
662
663
664
665
666
667
668 protected void verify(Checker checker,
669 File[] processedFiles,
670 String messageFileName,
671 String... expected)
672 throws Exception {
673 final Map<String, List<String>> expectedViolations = new HashMap<>();
674 expectedViolations.put(messageFileName, Arrays.asList(expected));
675 verify(checker, processedFiles, expectedViolations);
676 }
677
678
679
680
681
682
683
684
685 protected final void verifyWithLimitedResources(String fileName, String... expected)
686 throws Exception {
687 TestUtil.getResultWithLimitedResources(() -> {
688 verifyWithInlineConfigParser(fileName, expected);
689 return null;
690 });
691 }
692
693
694
695
696
697
698
699
700
701
702 protected final void verifyWithLimitedXpathResources(String fileName, String... expected)
703 throws Exception {
704 TestUtil.runWithLimitedXpathResources(() -> {
705 verifyWithInlineConfigParser(fileName, expected);
706 return null;
707 });
708 }
709
710
711
712
713
714
715
716
717 protected final void execute(Configuration config, String... filenames) throws Exception {
718 final Checker checker = createChecker(config);
719 final List<File> files = Arrays.stream(filenames)
720 .map(File::new)
721 .toList();
722 checker.process(files);
723 checker.destroy();
724 }
725
726
727
728
729
730
731
732
733 protected static void execute(Checker checker, String... filenames) throws Exception {
734 final List<File> files = Arrays.stream(filenames)
735 .map(File::new)
736 .toList();
737 checker.process(files);
738 checker.destroy();
739 }
740
741
742
743
744
745
746
747
748
749 private void verifyViolations(Configuration config,
750 String file,
751 List<TestInputViolation> testInputViolations)
752 throws Exception {
753 final List<String> actualViolations = getActualViolationsForFile(config, file);
754 final List<Integer> actualViolationLines = actualViolations.stream()
755 .map(violation -> violation.substring(0, violation.indexOf(':')))
756 .map(Integer::valueOf)
757 .toList();
758 final List<Integer> expectedViolationLines = testInputViolations.stream()
759 .map(TestInputViolation::getLineNo)
760 .toList();
761 assertWithMessage("Violation lines for %s differ.", file)
762 .that(actualViolationLines)
763 .isEqualTo(expectedViolationLines);
764 for (int index = 0; index < actualViolations.size(); index++) {
765 assertWithMessage("Actual and expected violations differ.")
766 .that(actualViolations.get(index))
767 .matches(testInputViolations.get(index).toRegex());
768 }
769 }
770
771
772
773
774
775
776
777
778 private static void verifyViolations(String file,
779 List<TestInputViolation> testInputViolations,
780 List<String> actualViolations) {
781 final List<Integer> actualViolationLines = actualViolations.stream()
782 .map(violation -> violation.substring(0, violation.indexOf(':')))
783 .map(Integer::valueOf)
784 .toList();
785 final List<Integer> expectedViolationLines = testInputViolations.stream()
786 .map(TestInputViolation::getLineNo)
787 .toList();
788 assertWithMessage("Violation lines for %s differ.", file)
789 .that(actualViolationLines)
790 .isEqualTo(expectedViolationLines);
791 for (int index = 0; index < actualViolations.size(); index++) {
792 assertWithMessage("Actual and expected violations differ.")
793 .that(actualViolations.get(index))
794 .matches(testInputViolations.get(index).toRegex());
795 }
796 }
797
798
799
800
801
802
803
804
805 private static void verifyContent(
806 String expectedOutputFile,
807 ByteArrayOutputStream outputStream)
808 throws IOException {
809 final String expectedContent = readFile(expectedOutputFile);
810 final String actualContent =
811 toLfLineEnding(outputStream.toString(StandardCharsets.UTF_8));
812 assertWithMessage("Content should match")
813 .that(actualContent)
814 .isEqualTo(expectedContent);
815 }
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836 private static void verifyCleanedMessageContent(
837 String expectedOutputFile,
838 ByteArrayOutputStream outputStream,
839 String basePath)
840 throws IOException {
841 final String expectedContent = readFile(expectedOutputFile);
842 final String rawActualContent =
843 toLfLineEnding(outputStream.toString(StandardCharsets.UTF_8));
844
845 final String cleanedActualContent = rawActualContent.lines()
846 .filter(line -> {
847 return line.startsWith("[")
848 || line.contains("Starting audit...")
849 || line.contains("Audit done.");
850 })
851 .map(line -> line.replace(basePath, ""))
852 .map(line -> line.replace('\\', '/'))
853 .collect(Collectors.joining("\n", "", "\n"));
854
855 assertWithMessage("Content should match")
856 .that(cleanedActualContent)
857 .isEqualTo(expectedContent);
858 }
859
860
861
862
863
864
865
866
867
868 private List<String> getActualViolationsForFile(Configuration config,
869 String file)
870 throws Exception {
871 stream.flush();
872 stream.reset();
873 final List<File> files = Collections.singletonList(new File(file));
874 final Checker checker = createChecker(config);
875 checker.process(files);
876 final Map<String, List<String>> actualViolations =
877 getActualViolations();
878 checker.destroy();
879 return actualViolations.getOrDefault(file, new ArrayList<>());
880 }
881
882
883
884
885
886
887
888
889
890 private Map<String, List<String>> getActualViolations() throws IOException {
891
892 try (ByteArrayInputStream inputStream =
893 new ByteArrayInputStream(stream.toByteArray());
894 LineNumberReader lnr = new LineNumberReader(
895 new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
896 final Map<String, List<String>> actualViolations = new HashMap<>();
897 for (String line = lnr.readLine(); line != null;
898 line = lnr.readLine()) {
899 if ("Audit done.".equals(line) || line.contains("at com")) {
900 break;
901 }
902
903
904 final String[] actualViolation = line.split("(?<=.{2}):", 2);
905 final String actualViolationFileName = actualViolation[0];
906 final String actualViolationMessage = actualViolation[1];
907
908 actualViolations
909 .computeIfAbsent(actualViolationFileName, key -> new ArrayList<>())
910 .add(actualViolationMessage);
911 }
912
913 return actualViolations;
914 }
915 }
916
917
918
919
920
921
922
923
924 protected final String getCheckMessage(String messageKey, Object... arguments) {
925 return internalGetCheckMessage(getMessageBundle(), messageKey, arguments);
926 }
927
928
929
930
931
932
933
934
935
936 protected static String getCheckMessage(
937 Class<?> clazz, String messageKey, Object... arguments) {
938 return internalGetCheckMessage(getMessageBundle(clazz.getName()), messageKey, arguments);
939 }
940
941
942
943
944
945
946
947
948
949 private static String internalGetCheckMessage(
950 String messageBundle, String messageKey, Object... arguments) {
951 final ResourceBundle resourceBundle = ResourceBundle.getBundle(
952 messageBundle,
953 Locale.ROOT,
954 Thread.currentThread().getContextClassLoader());
955 final String pattern = resourceBundle.getString(messageKey);
956 final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT);
957 return formatter.format(arguments);
958 }
959
960
961
962
963
964
965 private String getMessageBundle() {
966 final String className = getClass().getName();
967 return getMessageBundle(className);
968 }
969
970
971
972
973
974
975
976 private static String getMessageBundle(String className) {
977 final String messageBundle;
978 final String messages = "messages";
979 final int endIndex = className.lastIndexOf('.');
980 final Map<String, String> messageBundleMappings = new HashMap<>();
981 messageBundleMappings.put("SeverityMatchFilterExamplesTest",
982 "com.puppycrawl.tools.checkstyle.checks.naming.messages");
983
984 if (endIndex < 0) {
985 messageBundle = messages;
986 }
987 else {
988 final String packageName = className.substring(0, endIndex);
989 if ("com.puppycrawl.tools.checkstyle.filters".equals(packageName)) {
990 messageBundle = messageBundleMappings.get(className.substring(endIndex + 1));
991 }
992 else {
993 messageBundle = packageName + "." + messages;
994 }
995 }
996 return messageBundle;
997 }
998
999
1000
1001
1002
1003
1004
1005
1006 protected static String[] removeSuppressed(String[] actualViolations,
1007 String... suppressedViolations) {
1008 final List<String> actualViolationsList =
1009 Arrays.stream(actualViolations).collect(Collectors.toCollection(ArrayList::new));
1010 actualViolationsList.removeAll(Arrays.asList(suppressedViolations));
1011 return actualViolationsList.toArray(CommonUtil.EMPTY_STRING_ARRAY);
1012 }
1013
1014 }