From 8389108a3ca1c9662c2ed2cd82360af8684283cd Mon Sep 17 00:00:00 2001 From: Brijeshthummar02 Date: Sun, 17 May 2026 10:10:09 +0530 Subject: [PATCH] Issue #416: test should be updated to reuse inlined to input violation messages --- .../AbstractPatchFilterEvaluationTest.java | 94 ++++++++++++++++--- .../newline/Test2.java | 2 +- .../newline/expected.txt | 1 - .../patchedline/Test2.java | 2 +- .../patchedline/expected.txt | 1 - .../SingleNewLine/newline/Test.java | 2 +- .../SingleNewLine/newline/expected.txt | 1 - .../SingleNewLine/patchedline/Test.java | 2 +- .../SingleNewLine/patchedline/expected.txt | 1 - 9 files changed, 87 insertions(+), 19 deletions(-) delete mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/newline/expected.txt delete mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/patchedline/expected.txt delete mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/newline/expected.txt delete mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/patchedline/expected.txt diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/AbstractPatchFilterEvaluationTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/AbstractPatchFilterEvaluationTest.java index d7a94da4..b1ff3121 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/filters/AbstractPatchFilterEvaluationTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/AbstractPatchFilterEvaluationTest.java @@ -29,11 +29,12 @@ import java.io.LineNumberReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport; import com.puppycrawl.tools.checkstyle.ConfigurationLoader; @@ -48,6 +49,14 @@ abstract class AbstractPatchFilterEvaluationTest extends AbstractModuleTestSuppo private static final String CONTEXT_CONFIG_PATTERN = "(default|zero)ContextConfig.xml"; + /** + * Pattern to match inline violation comments in test files. + * Matches: // violation 'message' + * or: // filtered violation below 'message' + */ + private static final Pattern VIOLATION_PATTERN = Pattern.compile( + "//\\s*(?:filtered\\s+)?violation(?:\\s+below)?\\s+'([^']+)'"); + protected abstract String getPatchFileLocation(); protected void testByConfig(String configPath) @@ -95,18 +104,81 @@ private void assertResults(String configPath, String path, int errorCounter, try (ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray()); LineNumberReader lnr = new LineNumberReader( new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { - final String expectedFile = configPath.replaceFirst(CONTEXT_CONFIG_PATTERN, - "expected.txt"); - final Path expectedFilePath = Paths.get(getPath(expectedFile)); - final List expected = Files.readAllLines(expectedFilePath); - final List actuals = lnr.lines().toList(); + + final List expected = extractExpectedViolations(path); + final List actuals = lnr.lines() + .filter(line -> !line.equals("Audit done.")) + .toList(); + + assertEquals(expected.size(), actuals.size(), + "Number of violations does not match"); for (int index = 0; index < expected.size(); index++) { - final String expectedResult = path + File.separator + expected.get(index); - assertEquals(expectedResult, actuals.get(index), - "error message " + index + ". Expected file: " + expectedFilePath); + final String expectedViolation = expected.get(index); + final String actualViolation = actuals.get(index); + final String expectedResult = path + File.separator + expectedViolation; + assertEquals(expectedResult, actualViolation, "error message " + index); } - assertEquals(expected.size(), errorCounter, "unexpected output: " + lnr.readLine()); + assertEquals(expected.size(), errorCounter, + "unexpected output: " + lnr.readLine()); } } + + /** + * Extracts expected violation messages from inline comments in test files. + * Falls back to expected.txt if no inline violations are found. + * Parses comments like: // violation 'message' + * + * @param path the directory path containing test files + * @return list of expected violation messages in format "filename:line: message" + * @throws IOException if file reading fails + */ + private List extractExpectedViolations(String path) throws IOException { + final List violations = new ArrayList<>(); + final File directory = new File(path); + final File[] files = directory.listFiles((dir, name) -> { + return name.endsWith(".java") || name.endsWith(".properties"); + }); + + if (files != null) { + Arrays.sort(files); + + for (File file : files) { + final List lines = + Files.readAllLines(file.toPath(), StandardCharsets.UTF_8); + for (int index = 0; index < lines.size(); index++) { + final String line = lines.get(index); + final Matcher matcher = VIOLATION_PATTERN.matcher(line); + if (matcher.find()) { + final String violationMessage = matcher.group(1); + final int lineNumber = index + 1; + final String formattedMessage; + if (violationMessage.matches("\\d+:.*")) { + formattedMessage = ":" + violationMessage; + } + else { + formattedMessage = ": " + violationMessage; + } + violations.add(file.getName() + ":" + lineNumber + formattedMessage); + } + } + } + } + + final List result; + if (violations.isEmpty()) { + final File expectedFile = new File(path, "expected.txt"); + if (expectedFile.exists()) { + result = Files.readAllLines(expectedFile.toPath(), StandardCharsets.UTF_8); + } + else { + result = violations; + } + } + else { + result = violations; + } + + return result; + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/newline/Test2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/newline/Test2.java index a250be0c..bb97108f 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/newline/Test2.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/newline/Test2.java @@ -1,5 +1,5 @@ package Checker.FileTabCharacter; public class Test2 { - // Contains a tab -> <- //warn // violation without filter + // Contains a tab -> <- //warn // violation '25: Line contains a tab character.' } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/newline/expected.txt b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/newline/expected.txt deleted file mode 100644 index 26d89a35..00000000 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/newline/expected.txt +++ /dev/null @@ -1 +0,0 @@ -Test2.java:4:25: Line contains a tab character. \ No newline at end of file diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/patchedline/Test2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/patchedline/Test2.java index a250be0c..bb97108f 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/patchedline/Test2.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/patchedline/Test2.java @@ -1,5 +1,5 @@ package Checker.FileTabCharacter; public class Test2 { - // Contains a tab -> <- //warn // violation without filter + // Contains a tab -> <- //warn // violation '25: Line contains a tab character.' } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/patchedline/expected.txt b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/patchedline/expected.txt deleted file mode 100644 index 26d89a35..00000000 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/MoveCodeToAnotherFileWithMinorChanges/patchedline/expected.txt +++ /dev/null @@ -1 +0,0 @@ -Test2.java:4:25: Line contains a tab character. \ No newline at end of file diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/newline/Test.java b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/newline/Test.java index aada79dc..772d3d48 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/newline/Test.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/newline/Test.java @@ -7,7 +7,7 @@ public class Test { - // Contains a tab -> <- //warns // violation without filter + // Contains a tab -> <- //warns // violation '25: Line contains a tab character.' // Long line ---------------------------------------------------------------- // Long line ---------------------------------------------------------------- // Contains a tab -> <- //warns diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/newline/expected.txt b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/newline/expected.txt deleted file mode 100644 index b6202e4e..00000000 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/newline/expected.txt +++ /dev/null @@ -1 +0,0 @@ -Test.java:10:25: Line contains a tab character. \ No newline at end of file diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/patchedline/Test.java b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/patchedline/Test.java index aada79dc..772d3d48 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/patchedline/Test.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/patchedline/Test.java @@ -7,7 +7,7 @@ public class Test { - // Contains a tab -> <- //warns // violation without filter + // Contains a tab -> <- //warns // violation '25: Line contains a tab character.' // Long line ---------------------------------------------------------------- // Long line ---------------------------------------------------------------- // Contains a tab -> <- //warns diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/patchedline/expected.txt b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/patchedline/expected.txt deleted file mode 100644 index b6202e4e..00000000 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchfilter/FileTabCharacter/SingleNewLine/patchedline/expected.txt +++ /dev/null @@ -1 +0,0 @@ -Test.java:10:25: Line contains a tab character. \ No newline at end of file