View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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       * Returns log stream.
66       *
67       * @return stream with log
68       */
69      protected final ByteArrayOutputStream getStream() {
70          return stream;
71      }
72  
73      /**
74       * Returns test logger.
75       *
76       * @return logger for tests
77       */
78      protected final BriefUtLogger getBriefUtLogger() {
79          return new BriefUtLogger(stream);
80      }
81  
82      /**
83       * Creates a default module configuration {@link DefaultConfiguration} for a given object
84       * of type {@link Class}.
85       *
86       * @param clazz a {@link Class} type object.
87       * @return default module configuration for the given {@link Class} instance.
88       */
89      protected static DefaultConfiguration createModuleConfig(Class<?> clazz) {
90          return new DefaultConfiguration(clazz.getName());
91      }
92  
93      /**
94       * Creates {@link Checker} instance based on the given {@link Configuration} instance.
95       *
96       * @param moduleConfig {@link Configuration} instance.
97       * @return {@link Checker} instance based on the given {@link Configuration} instance.
98       * @throws Exception if an exception occurs during checker configuration.
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      * Configures the {@code checker} instance with {@code moduleConfig}.
119      *
120      * @param checker {@link Checker} instance.
121      * @param moduleConfig {@link Configuration} instance.
122      * @throws Exception if an exception occurs during configuration.
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      * Creates {@link DefaultConfiguration} for the {@link TreeWalker}
140      * based on the given {@link Configuration} instance.
141      *
142      * @param config {@link Configuration} instance.
143      * @return {@link DefaultConfiguration} for the {@link TreeWalker}
144      *     based on the given {@link Configuration} instance.
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         // make sure that the tests always run with this charset
151         rootConfig.addProperty("charset", StandardCharsets.UTF_8.name());
152         rootConfig.addChild(twConf);
153         twConf.addChild(config);
154         return rootConfig;
155     }
156 
157     /**
158      * Creates {@link DefaultConfiguration} for the given {@link Configuration} instance.
159      *
160      * @param config {@link Configuration} instance.
161      * @return {@link DefaultConfiguration} for the given {@link Configuration} instance.
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      * Returns canonical path for the file with the given file name.
173      * The path is formed base on the non-compilable resources location.
174      *
175      * @param filename file name.
176      * @return canonical path for the file with the given file name.
177      * @throws IOException if I/O exception occurs while forming the path.
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      * Creates a RootNode for non-compilable test files.
187      *
188      * @param fileName name of the test file
189      * @return RootNode for the parsed AST
190      * @throws Exception if file parsing fails
191      */
192     protected RootNode getRootNodeForNonCompilable(String fileName) throws Exception {
193         final File file = new File(getNonCompilablePath(fileName));
194         final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
195         return new RootNode(rootAst);
196     }
197 
198     /**
199      * Returns URI-representation of the path for the given file name.
200      * The path is formed base on the root location.
201      *
202      * @param filename file name.
203      * @return URI-representation of the path for the file with the given file name.
204      */
205     protected final String getUriString(String filename) {
206         return new File("src/test/resources/" + getPackageLocation() + "/" + filename).toURI()
207                 .toString();
208     }
209 
210     /**
211      * Performs verification of the file with the given file path using specified configuration
212      * and the array of expected messages. Also performs verification of the config with filters
213      * specified in the input file.
214      *
215      * @param filePath file path to verify.
216      * @param expectedUnfiltered an array of expected unfiltered config.
217      * @param expectedFiltered an array of expected config with filters.
218      * @throws Exception if exception occurs during verification process.
219      */
220     protected final void verifyFilterWithInlineConfigParser(String filePath,
221                                                             String[] expectedUnfiltered,
222                                                             String... expectedFiltered)
223             throws Exception {
224         final TestInputConfiguration testInputConfiguration =
225                 InlineConfigParser.parseWithFilteredViolations(filePath);
226         final DefaultConfiguration configWithoutFilters =
227                 testInputConfiguration.createConfigurationWithoutFilters();
228         final List<TestInputViolation> violationsWithoutFilters =
229                 new ArrayList<>(testInputConfiguration.getViolations());
230         violationsWithoutFilters.addAll(testInputConfiguration.getFilteredViolations());
231         Collections.sort(violationsWithoutFilters);
232         verifyViolations(configWithoutFilters, filePath, violationsWithoutFilters);
233         verify(configWithoutFilters, filePath, expectedUnfiltered);
234         final DefaultConfiguration configWithFilters =
235                 testInputConfiguration.createConfiguration();
236         verifyViolations(configWithFilters, filePath, testInputConfiguration.getViolations());
237         verify(configWithFilters, filePath, expectedFiltered);
238     }
239 
240     /**
241      * Performs verification of the file with given file path using configurations parsed from
242      * xml header of the file and the array expected messages. Also performs verification of
243      * the config specified in input file.
244      *
245      * @param filePath file path to verify
246      * @param expected an array of expected messages
247      * @throws Exception if exception occurs
248      */
249     protected final void verifyWithInlineXmlConfig(String filePath, String... expected)
250             throws Exception {
251         final TestInputConfiguration testInputConfiguration =
252                 InlineConfigParser.parseWithXmlHeader(filePath);
253         final Configuration xmlConfig =
254                 testInputConfiguration.getXmlConfiguration();
255         verifyViolations(xmlConfig, filePath, testInputConfiguration.getViolations());
256         verify(xmlConfig, filePath, expected);
257     }
258 
259     /**
260      * Performs verification of the file with the given file path using specified configuration
261      * and the array expected messages. Also performs verification of the config specified in
262      * input file.
263      *
264      * @param filePath file path to verify.
265      * @param expected an array of expected messages.
266      * @throws Exception if exception occurs during verification process.
267      */
268     protected final void verifyWithInlineConfigParser(String filePath, String... expected)
269             throws Exception {
270         final TestInputConfiguration testInputConfiguration =
271                 InlineConfigParser.parse(filePath);
272         final DefaultConfiguration parsedConfig =
273                 testInputConfiguration.createConfiguration();
274         final List<String> actualViolations = getActualViolationsForFile(parsedConfig, filePath);
275         verifyViolations(filePath, testInputConfiguration.getViolations(), actualViolations);
276         assertWithMessage("Violations for %s differ.", filePath)
277             .that(actualViolations)
278             .containsExactlyElementsIn(expected);
279     }
280 
281     /**
282      * Performs verification of two files with their given file paths using specified
283      * configuration of one file only. Also performs verification of the config specified
284      * in the input file. This method needs to be implemented when two given files need to be
285      * checked through a single check only.
286      *
287      * @param filePath1 file path of first file to verify
288      * @param filePath2 file path of second file to verify
289      * @param expected an array of expected messages
290      * @throws Exception if exception occurs during verification process
291      */
292     protected final void verifyWithInlineConfigParser(String filePath1,
293                                                       String filePath2,
294                                                       String... expected)
295             throws Exception {
296         final TestInputConfiguration testInputConfiguration1 =
297                 InlineConfigParser.parse(filePath1);
298         final DefaultConfiguration parsedConfig =
299                 testInputConfiguration1.createConfiguration();
300         final TestInputConfiguration testInputConfiguration2 =
301                 InlineConfigParser.parse(filePath2);
302         verifyViolations(parsedConfig, filePath1, testInputConfiguration1.getViolations());
303         verifyViolations(parsedConfig, filePath2, testInputConfiguration2.getViolations());
304         verify(createChecker(parsedConfig),
305                 new File[] {new File(filePath1), new File(filePath2)},
306                 filePath1,
307                 expected);
308     }
309 
310     /**
311      * Performs verification of two files with their given file paths.
312      * using specified configuration of one file only. Also performs
313      * verification of the config specified in the input file. This method
314      * needs to be implemented when two given files need to be
315      * checked through a single check only.
316      *
317      * @param filePath1 file path of first file to verify
318      * @param filePath2 file path of first file to verify
319      * @param expectedFromFile1 list of expected message
320      * @param expectedFromFile2 list of expected message
321      * @throws Exception if exception occurs during verification process
322      */
323     protected final void verifyWithInlineConfigParser(String filePath1,
324                                                       String filePath2,
325                                                       List<String> expectedFromFile1,
326                                                       List<String> expectedFromFile2)
327             throws Exception {
328         final TestInputConfiguration testInputConfiguration = InlineConfigParser.parse(filePath1);
329         final DefaultConfiguration parsedConfig = testInputConfiguration.createConfiguration();
330         final TestInputConfiguration testInputConfiguration2 = InlineConfigParser.parse(filePath2);
331         final DefaultConfiguration parsedConfig2 = testInputConfiguration.createConfiguration();
332         final File[] inputs = {new File(filePath1), new File(filePath2)};
333         verifyViolations(parsedConfig, filePath1, testInputConfiguration.getViolations());
334         verifyViolations(parsedConfig2, filePath2, testInputConfiguration2.getViolations());
335         verify(createChecker(parsedConfig), inputs, ImmutableMap.of(
336             filePath1, expectedFromFile1,
337             filePath2, expectedFromFile2));
338     }
339 
340     /**
341      * Verifies the target file against the configuration specified in a separate configuration
342      * file.
343      * This method is intended for use cases when the configuration is stored in one file and the
344      * content to verify is stored in another file.
345      *
346      * @param fileWithConfig file path of the configuration file
347      * @param targetFile file path of the target file to be verified
348      * @param expected an array of expected messages
349      * @throws Exception if an exception occurs during verification process
350      */
351     protected final void verifyWithInlineConfigParserSeparateConfigAndTarget(String fileWithConfig,
352                                                                              String targetFile,
353                                                                              String... expected)
354             throws Exception {
355         final TestInputConfiguration testInputConfiguration1 =
356                 InlineConfigParser.parse(fileWithConfig);
357         final DefaultConfiguration parsedConfig =
358                 testInputConfiguration1.createConfiguration();
359         final List<TestInputViolation> inputViolations =
360                 InlineConfigParser.getViolationsFromInputFile(targetFile);
361         final List<String> actualViolations = getActualViolationsForFile(parsedConfig, targetFile);
362         verifyViolations(targetFile, inputViolations, actualViolations);
363         assertWithMessage("Violations for %s differ.", targetFile)
364                 .that(actualViolations)
365                 .containsExactlyElementsIn(expected);
366     }
367 
368     /**
369      * Performs verification of the file with the given file path using specified configuration
370      * and the array of expected messages. Also performs verification of the config with filters
371      * specified in the input file.
372      *
373      * @param fileWithConfig file path of the configuration file.
374      * @param targetFilePath file path of the target file to be verified.
375      * @param expectedUnfiltered an array of expected unfiltered config.
376      * @param expectedFiltered an array of expected config with filters.
377      * @throws Exception if exception occurs during verification process.
378      */
379     protected final void verifyFilterWithInlineConfigParserSeparateConfigAndTarget(
380             String fileWithConfig,
381             String targetFilePath,
382             String[] expectedUnfiltered,
383             String... expectedFiltered)
384             throws Exception {
385         final TestInputConfiguration testInputConfiguration =
386                 InlineConfigParser.parseWithFilteredViolations(fileWithConfig);
387         final DefaultConfiguration configWithoutFilters =
388                 testInputConfiguration.createConfigurationWithoutFilters();
389         final List<TestInputViolation> violationsWithoutFilters = new ArrayList<>(
390                 InlineConfigParser.getFilteredViolationsFromInputFile(targetFilePath));
391         violationsWithoutFilters.addAll(
392                 InlineConfigParser.getViolationsFromInputFile(targetFilePath));
393         Collections.sort(violationsWithoutFilters);
394         verifyViolations(configWithoutFilters, targetFilePath, violationsWithoutFilters);
395         verify(configWithoutFilters, targetFilePath, expectedUnfiltered);
396         final DefaultConfiguration configWithFilters =
397                 testInputConfiguration.createConfiguration();
398         final List<TestInputViolation> violationsWithFilters =
399                 InlineConfigParser.getViolationsFromInputFile(targetFilePath);
400         verifyViolations(configWithFilters, targetFilePath, violationsWithFilters);
401         verify(configWithFilters, targetFilePath, expectedFiltered);
402     }
403 
404     /**
405      * Performs verification of the file with the given file path using specified configuration
406      * and the array expected messages. Also performs verification of the config specified in
407      * input file
408      *
409      * @param filePath file path to verify.
410      * @param expected an array of expected messages.
411      * @throws Exception if exception occurs during verification process.
412      */
413     protected void verifyWithInlineConfigParserTwice(String filePath, String... expected)
414             throws Exception {
415         final TestInputConfiguration testInputConfiguration =
416                 InlineConfigParser.parse(filePath);
417         final DefaultConfiguration parsedConfig =
418                 testInputConfiguration.createConfiguration();
419         verifyViolations(parsedConfig, filePath, testInputConfiguration.getViolations());
420         verify(parsedConfig, filePath, expected);
421     }
422 
423     /**
424      * Verifies logger output using the inline configuration parser.
425      * Expects an input file with configuration and violations, and a report file with expected
426      * output.
427      *
428      * @param inputFile path to the file with configuration and violations
429      * @param expectedReportFile path to the expected logger report file
430      * @param logger logger to test
431      * @param outputStream output stream where the logger writes its actual output
432      * @throws Exception if an exception occurs during verification
433      */
434     protected void verifyWithInlineConfigParserAndLogger(String inputFile,
435                                                          String expectedReportFile,
436                                                          AuditListener logger,
437                                                          ByteArrayOutputStream outputStream)
438             throws Exception {
439         final TestInputConfiguration testInputConfiguration =
440                 InlineConfigParser.parse(inputFile);
441         final DefaultConfiguration parsedConfig =
442                 testInputConfiguration.createConfiguration();
443         final List<File> filesToCheck = Collections.singletonList(new File(inputFile));
444         final String basePath = Path.of("").toAbsolutePath().toString();
445 
446         final Checker checker = createChecker(parsedConfig);
447         checker.setBasedir(basePath);
448         checker.addListener(logger);
449         checker.process(filesToCheck);
450 
451         verifyContent(expectedReportFile, outputStream);
452     }
453 
454     /**
455      * Verifies logger output using the inline configuration parser for default logger.
456      * Expects an input file with configuration and violations, and expected output file.
457      * Uses full Checker configuration.
458      *
459      * @param inputFile path to the file with configuration and violations
460      * @param expectedOutputFile path to the expected info stream output file
461      * @param logger logger to test
462      * @param outputStream where the logger writes its actual info stream output
463      * @throws Exception if an exception occurs during verification
464      */
465     protected final void verifyWithInlineConfigParserAndDefaultLogger(String inputFile,
466                                                               String expectedOutputFile,
467                                                               AuditListener logger,
468                                                               ByteArrayOutputStream outputStream)
469             throws Exception {
470         final TestInputConfiguration testInputConfiguration =
471                 InlineConfigParser.parseWithXmlHeader(inputFile);
472         final Configuration parsedConfig =
473                 testInputConfiguration.getXmlConfiguration();
474         final List<File> filesToCheck = Collections.singletonList(new File(inputFile));
475         final String basePath = Path.of("").toAbsolutePath().toString();
476 
477         final Checker checker = createChecker(parsedConfig);
478         checker.setBasedir(basePath);
479         checker.addListener(logger);
480         checker.process(filesToCheck);
481 
482         verifyCleanedMessageContent(expectedOutputFile, outputStream, basePath);
483     }
484 
485     /**
486      * Verifies logger output using the inline configuration parser for default logger.
487      * Expects an input file with configuration and violations, and separate expected output files
488      * for info and error streams.
489      * Uses full Checker configuration.
490      *
491      * @param inputFile path to the file with configuration and violations
492      * @param expectedInfoFile path to the expected info stream output file
493      * @param expectedErrorFile path to the expected error stream output file
494      * @param logger logger to test
495      * @param infoStream where the logger writes its actual info stream output
496      * @param errorStream where the logger writes its actual error stream output
497      * @throws Exception if an exception occurs during verification
498      * @noinspection MethodWithTooManyParameters
499      * @noinspectionreason MethodWithTooManyParameters - Method requires a lot of parameters to
500      *                     verify the default logger output.
501      */
502     protected final void verifyWithInlineConfigParserAndDefaultLogger(String inputFile,
503                                                          String expectedInfoFile,
504                                                          String expectedErrorFile,
505                                                          AuditListener logger,
506                                                          ByteArrayOutputStream infoStream,
507                                                          ByteArrayOutputStream errorStream)
508             throws Exception {
509         final TestInputConfiguration testInputConfiguration =
510                 InlineConfigParser.parseWithXmlHeader(inputFile);
511         final Configuration parsedConfig =
512                 testInputConfiguration.getXmlConfiguration();
513         final List<File> filesToCheck = Collections.singletonList(new File(inputFile));
514         final String basePath = Path.of("").toAbsolutePath().toString();
515 
516         final Checker checker = createChecker(parsedConfig);
517         checker.setBasedir(basePath);
518         checker.addListener(logger);
519         checker.process(filesToCheck);
520 
521         verifyContent(expectedInfoFile, infoStream);
522         verifyCleanedMessageContent(expectedErrorFile, errorStream, basePath);
523     }
524 
525     /**
526      * Performs verification of the file with the given file name. Uses specified configuration.
527      * Expected messages are represented by the array of strings.
528      * This implementation uses overloaded
529      * {@link AbstractModuleTestSupport#verify(Checker, File[], String, String...)} method inside.
530      *
531      * @param config configuration.
532      * @param fileName file name to verify.
533      * @param expected an array of expected messages.
534      * @throws Exception if exception occurs during verification process.
535      */
536     protected final void verify(Configuration config, String fileName, String... expected)
537             throws Exception {
538         verify(createChecker(config), fileName, fileName, expected);
539     }
540 
541     /**
542      * Performs verification of the file with the given file name.
543      * Uses provided {@link Checker} instance.
544      * Expected messages are represented by the array of strings.
545      * This implementation uses overloaded
546      * {@link AbstractModuleTestSupport#verify(Checker, String, String, String...)} method inside.
547      *
548      * @param checker {@link Checker} instance.
549      * @param fileName file name to verify.
550      * @param expected an array of expected messages.
551      * @throws Exception if exception occurs during verification process.
552      */
553     protected void verify(Checker checker, String fileName, String... expected)
554             throws Exception {
555         verify(checker, fileName, fileName, expected);
556     }
557 
558     /**
559      * Performs verification of the file with the given file name.
560      * Uses provided {@link Checker} instance.
561      * Expected messages are represented by the array of strings.
562      * This implementation uses overloaded
563      * {@link AbstractModuleTestSupport#verify(Checker, File[], String, String...)} method inside.
564      *
565      * @param checker {@link Checker} instance.
566      * @param processedFilename file name to verify.
567      * @param messageFileName message file name.
568      * @param expected an array of expected messages.
569      * @throws Exception if exception occurs during verification process.
570      */
571     protected final void verify(Checker checker,
572                           String processedFilename,
573                           String messageFileName,
574                           String... expected)
575             throws Exception {
576         verify(checker,
577                 new File[] {new File(processedFilename)},
578                 messageFileName, expected);
579     }
580 
581     /**
582      *  Performs verification of the given files against the array of
583      *  expected messages using the provided {@link Checker} instance.
584      *
585      *  @param checker {@link Checker} instance.
586      *  @param processedFiles list of files to verify.
587      *  @param messageFileName message file name.
588      *  @param expected an array of expected messages.
589      *  @throws Exception if exception occurs during verification process.
590      */
591     protected void verify(Checker checker,
592                           File[] processedFiles,
593                           String messageFileName,
594                           String... expected)
595             throws Exception {
596         final Map<String, List<String>> expectedViolations = new HashMap<>();
597         expectedViolations.put(messageFileName, Arrays.asList(expected));
598         verify(checker, processedFiles, expectedViolations);
599     }
600 
601     /**
602      * Performs verification of the given files.
603      *
604      * @param checker {@link Checker} instance
605      * @param processedFiles files to process.
606      * @param expectedViolations a map of expected violations per files.
607      * @throws Exception if exception occurs during verification process.
608      */
609     protected final void verify(Checker checker,
610                           File[] processedFiles,
611                           Map<String, List<String>> expectedViolations)
612             throws Exception {
613         stream.flush();
614         stream.reset();
615         final List<File> theFiles = new ArrayList<>();
616         Collections.addAll(theFiles, processedFiles);
617         checker.process(theFiles);
618 
619         // process each of the lines
620         final Map<String, List<String>> actualViolations = getActualViolations();
621         final Map<String, List<String>> realExpectedViolations =
622                 Maps.filterValues(expectedViolations, input -> !input.isEmpty());
623 
624         assertWithMessage("Files with expected violations and actual violations differ.")
625             .that(actualViolations.keySet())
626             .isEqualTo(realExpectedViolations.keySet());
627 
628         realExpectedViolations.forEach((fileName, violationList) -> {
629             assertWithMessage("Violations for %s differ.", fileName)
630                 .that(actualViolations.get(fileName))
631                 .containsExactlyElementsIn(violationList);
632         });
633 
634         checker.destroy();
635     }
636 
637     /**
638      * Runs 'verifyWithInlineConfigParser' with limited stack size and time duration.
639      *
640      * @param fileName file name to verify.
641      * @param expected an array of expected messages.
642      * @throws Exception if exception occurs during verification process.
643      */
644     protected final void verifyWithLimitedResources(String fileName, String... expected)
645             throws Exception {
646         // We return null here, which gives us a result to make an assertion about
647         final Void result = TestUtil.getResultWithLimitedResources(() -> {
648             verifyWithInlineConfigParser(fileName, expected);
649             return null;
650         });
651         assertWithMessage("Verify should complete successfully.")
652                 .that(result)
653                 .isNull();
654     }
655 
656     /**
657      * Executes given config on a list of files only. Does not verify violations.
658      *
659      * @param config check configuration
660      * @param filenames names of files to process
661      * @throws Exception if there is a problem during checker configuration
662      */
663     protected final void execute(Configuration config, String... filenames) throws Exception {
664         final Checker checker = createChecker(config);
665         final List<File> files = Arrays.stream(filenames)
666                 .map(File::new)
667                 .toList();
668         checker.process(files);
669         checker.destroy();
670     }
671 
672     /**
673      * Executes given config on a list of files only. Does not verify violations.
674      *
675      * @param checker check configuration
676      * @param filenames names of files to process
677      * @throws Exception if there is a problem during checker configuration
678      */
679     protected static void execute(Checker checker, String... filenames) throws Exception {
680         final List<File> files = Arrays.stream(filenames)
681                 .map(File::new)
682                 .toList();
683         checker.process(files);
684         checker.destroy();
685     }
686 
687     /**
688      * Performs verification of violation lines.
689      *
690      * @param config parsed config.
691      * @param file file path.
692      * @param testInputViolations List of TestInputViolation objects.
693      * @throws Exception if exception occurs during verification process.
694      */
695     private void verifyViolations(Configuration config,
696                                   String file,
697                                   List<TestInputViolation> testInputViolations)
698             throws Exception {
699         final List<String> actualViolations = getActualViolationsForFile(config, file);
700         final List<Integer> actualViolationLines = actualViolations.stream()
701                 .map(violation -> violation.substring(0, violation.indexOf(':')))
702                 .map(Integer::valueOf)
703                 .toList();
704         final List<Integer> expectedViolationLines = testInputViolations.stream()
705                 .map(TestInputViolation::getLineNo)
706                 .toList();
707         assertWithMessage("Violation lines for %s differ.", file)
708                 .that(actualViolationLines)
709                 .isEqualTo(expectedViolationLines);
710         for (int index = 0; index < actualViolations.size(); index++) {
711             assertWithMessage("Actual and expected violations differ.")
712                     .that(actualViolations.get(index))
713                     .matches(testInputViolations.get(index).toRegex());
714         }
715     }
716 
717     /**
718      * Performs verification of violation lines.
719      *
720      * @param file file path.
721      * @param testInputViolations List of TestInputViolation objects.
722      * @param actualViolations for a file
723      */
724     private static void verifyViolations(String file,
725                                   List<TestInputViolation> testInputViolations,
726                                   List<String> actualViolations) {
727         final List<Integer> actualViolationLines = actualViolations.stream()
728                 .map(violation -> violation.substring(0, violation.indexOf(':')))
729                 .map(Integer::valueOf)
730                 .toList();
731         final List<Integer> expectedViolationLines = testInputViolations.stream()
732                 .map(TestInputViolation::getLineNo)
733                 .toList();
734         assertWithMessage("Violation lines for %s differ.", file)
735                 .that(actualViolationLines)
736                 .isEqualTo(expectedViolationLines);
737         for (int index = 0; index < actualViolations.size(); index++) {
738             assertWithMessage("Actual and expected violations differ.")
739                     .that(actualViolations.get(index))
740                     .matches(testInputViolations.get(index).toRegex());
741         }
742     }
743 
744     /**
745      * Verifies that the logger's actual output matches the expected report file.
746      *
747      * @param expectedOutputFile path to the expected logger report file
748      * @param outputStream output stream containing the actual logger output
749      * @throws IOException if an exception occurs while reading the file
750      */
751     private static void verifyContent(
752             String expectedOutputFile,
753             ByteArrayOutputStream outputStream) throws IOException {
754         final String expectedContent = readFile(expectedOutputFile);
755         final String actualContent =
756                 toLfLineEnding(outputStream.toString(StandardCharsets.UTF_8));
757         assertWithMessage("Content should match")
758                 .that(actualContent)
759                 .isEqualTo(expectedContent);
760     }
761 
762     /**
763      * Verifies that the logger output matches the expected report file content,
764      * keeping only severity-tagged lines (e.g. [ERROR], [WARN], [INFO]) or lines containing
765      * "Starting audit..." or "Audit done".
766      *
767      * <p>
768      * This method strips:
769      * <ul>
770      *   <li>any stack trace lines from exception outputs (i.e. lines not starting with a severity
771      *   tag),</li>
772      *   <li>any absolute {@code basePath} prefixes in the message content.</li>
773      * </ul>
774      * The result is compared with expected output that includes only severity-tagged lines.
775      *
776      * @param expectedOutputFile path to a file that contains the expected first line
777      * @param outputStream output stream containing the actual logger output
778      * @param basePath absolute path prefix to strip before comparison
779      * @throws IOException if an exception occurs while reading the file
780      */
781     private static void verifyCleanedMessageContent(
782             String expectedOutputFile,
783             ByteArrayOutputStream outputStream,
784             String basePath) throws IOException {
785         final String expectedContent = readFile(expectedOutputFile);
786         final String rawActualContent =
787                 toLfLineEnding(outputStream.toString(StandardCharsets.UTF_8));
788 
789         final String cleanedActualContent = rawActualContent.lines()
790                 .filter(line -> {
791                     return line.startsWith("[")
792                             || line.contains("Starting audit...")
793                             || line.contains("Audit done.");
794                 })
795                 .map(line -> line.replace(basePath, ""))
796                 .map(line -> line.replace('\\', '/'))
797                 .collect(Collectors.joining("\n", "", "\n"));
798 
799         assertWithMessage("Content should match")
800                 .that(cleanedActualContent)
801                 .isEqualTo(expectedContent);
802     }
803 
804     /**
805      * Tests the file with the check config.
806      *
807      * @param config check configuration.
808      * @param file input file path.
809      * @return list of actual violations.
810      * @throws Exception if exception occurs during verification process.
811      */
812     private List<String> getActualViolationsForFile(Configuration config,
813                                                     String file) throws Exception {
814         stream.flush();
815         stream.reset();
816         final List<File> files = Collections.singletonList(new File(file));
817         final Checker checker = createChecker(config);
818         checker.process(files);
819         final Map<String, List<String>> actualViolations =
820                 getActualViolations();
821         checker.destroy();
822         return actualViolations.getOrDefault(file, new ArrayList<>());
823     }
824 
825     /**
826      * Returns the actual violations for each file that has been checked against {@link Checker}.
827      * Each file is mapped to their corresponding violation messages. Reads input stream for these
828      * messages using instance of {@link InputStreamReader}.
829      *
830      * @return a {@link Map} object containing file names and the corresponding violation messages.
831      * @throws IOException exception can occur when reading input stream.
832      */
833     private Map<String, List<String>> getActualViolations() throws IOException {
834         // process each of the lines
835         try (ByteArrayInputStream inputStream =
836                 new ByteArrayInputStream(stream.toByteArray());
837             LineNumberReader lnr = new LineNumberReader(
838                 new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
839             final Map<String, List<String>> actualViolations = new HashMap<>();
840             for (String line = lnr.readLine(); line != null;
841                  line = lnr.readLine()) {
842                 if ("Audit done.".equals(line) || line.contains("at com")) {
843                     break;
844                 }
845                 // have at least 2 characters before the splitting colon,
846                 // to not split after the drive letter on Windows
847                 final String[] actualViolation = line.split("(?<=.{2}):", 2);
848                 final String actualViolationFileName = actualViolation[0];
849                 final String actualViolationMessage = actualViolation[1];
850 
851                 actualViolations
852                         .computeIfAbsent(actualViolationFileName, key -> new ArrayList<>())
853                         .add(actualViolationMessage);
854             }
855 
856             return actualViolations;
857         }
858     }
859 
860     /**
861      * Gets the check message 'as is' from appropriate 'messages.properties'
862      * file.
863      *
864      * @param messageKey the key of message in 'messages.properties' file.
865      * @param arguments  the arguments of message in 'messages.properties' file.
866      * @return The message of the check with the arguments applied.
867      */
868     protected final String getCheckMessage(String messageKey, Object... arguments) {
869         return internalGetCheckMessage(getMessageBundle(), messageKey, arguments);
870     }
871 
872     /**
873      * Gets the check message 'as is' from appropriate 'messages.properties'
874      * file.
875      *
876      * @param clazz the related check class.
877      * @param messageKey the key of message in 'messages.properties' file.
878      * @param arguments the arguments of message in 'messages.properties' file.
879      * @return The message of the check with the arguments applied.
880      */
881     protected static String getCheckMessage(
882             Class<?> clazz, String messageKey, Object... arguments) {
883         return internalGetCheckMessage(getMessageBundle(clazz.getName()), messageKey, arguments);
884     }
885 
886     /**
887      * Gets the check message 'as is' from appropriate 'messages.properties'
888      * file.
889      *
890      * @param messageBundle the bundle name.
891      * @param messageKey the key of message in 'messages.properties' file.
892      * @param arguments the arguments of message in 'messages.properties' file.
893      * @return The message of the check with the arguments applied.
894      */
895     private static String internalGetCheckMessage(
896             String messageBundle, String messageKey, Object... arguments) {
897         final ResourceBundle resourceBundle = ResourceBundle.getBundle(
898                 messageBundle,
899                 Locale.ROOT,
900                 Thread.currentThread().getContextClassLoader(),
901                 new Utf8Control());
902         final String pattern = resourceBundle.getString(messageKey);
903         final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT);
904         return formatter.format(arguments);
905     }
906 
907     /**
908      * Returns message bundle for a class specified by its class name.
909      *
910      * @return a string of message bundles for the class using class name.
911      */
912     private String getMessageBundle() {
913         final String className = getClass().getName();
914         return getMessageBundle(className);
915     }
916 
917     /**
918      * Returns message bundles for a class by providing class name.
919      *
920      * @param className name of the class.
921      * @return message bundles containing package name.
922      */
923     private static String getMessageBundle(String className) {
924         final String messageBundle;
925         final String messages = "messages";
926         final int endIndex = className.lastIndexOf('.');
927         final Map<String, String> messageBundleMappings = new HashMap<>();
928         messageBundleMappings.put("SeverityMatchFilterExamplesTest",
929                 "com.puppycrawl.tools.checkstyle.checks.naming.messages");
930 
931         if (endIndex < 0) {
932             messageBundle = messages;
933         }
934         else {
935             final String packageName = className.substring(0, endIndex);
936             if ("com.puppycrawl.tools.checkstyle.filters".equals(packageName)) {
937                 messageBundle = messageBundleMappings.get(className.substring(endIndex + 1));
938             }
939             else {
940                 messageBundle = packageName + "." + messages;
941             }
942         }
943         return messageBundle;
944     }
945 
946     /**
947      * Remove suppressed violation messages from actual violation messages.
948      *
949      * @param actualViolations actual violation messages
950      * @param suppressedViolations suppressed violation messages
951      * @return an array of actual violation messages minus suppressed violation messages
952      */
953     protected static String[] removeSuppressed(String[] actualViolations,
954                                                String... suppressedViolations) {
955         final List<String> actualViolationsList =
956             Arrays.stream(actualViolations).collect(Collectors.toCollection(ArrayList::new));
957         actualViolationsList.removeAll(Arrays.asList(suppressedViolations));
958         return actualViolationsList.toArray(CommonUtil.EMPTY_STRING_ARRAY);
959     }
960 
961 }