From 05eeaa62c56b309c94f6039b6a9ab89ce5e001c3 Mon Sep 17 00:00:00 2001 From: Anmol202005 Date: Thu, 17 Jul 2025 03:27:28 +0530 Subject: [PATCH] Issue #41: Updated AbstractRecipeTest to remove external report --- pom.xml | 7 + .../org/checkstyle/autofix/recipe/Header.java | 5 +- .../checkstyle/autofix/recipe/UpperEll.java | 11 +- .../autofix/recipe/AbstractRecipeTest.java | 122 ++++++++++++++---- .../checkstyle/autofix/recipe/HeaderTest.java | 60 +++++---- .../autofix/recipe/UpperEllTest.java | 33 ++--- .../autofix/recipe/header/config.xml | 15 --- .../autofix/recipe/header/report.xml | 19 --- .../InputComplexLongLiterals.java | 20 ++- .../OutputComplexLongLiterals.java | 20 ++- .../hexoctalliteral/InputHexOctalLiteral.java | 17 ++- .../OutputHexOctalLiteral.java | 17 ++- .../autofix/recipe/upperell/report.xml | 110 ---------------- .../InputStringAndComments.java | 9 ++ .../OutputStringAndComments.java | 9 ++ 15 files changed, 250 insertions(+), 224 deletions(-) delete mode 100644 src/test/resources/org/checkstyle/autofix/recipe/header/config.xml delete mode 100644 src/test/resources/org/checkstyle/autofix/recipe/header/report.xml delete mode 100644 src/test/resources/org/checkstyle/autofix/recipe/upperell/report.xml diff --git a/pom.xml b/pom.xml index be97b7a..94d7ab6 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,13 @@ + + com.puppycrawl.tools + checkstyle + ${checkstyle.version} + tests + test + org.openrewrite rewrite-test diff --git a/src/main/java/org/checkstyle/autofix/recipe/Header.java b/src/main/java/org/checkstyle/autofix/recipe/Header.java index 725e5ad..426f9f1 100644 --- a/src/main/java/org/checkstyle/autofix/recipe/Header.java +++ b/src/main/java/org/checkstyle/autofix/recipe/Header.java @@ -46,6 +46,7 @@ public class Header extends Recipe { private static final String HEADER_FILE_PROPERTY = "headerFile"; private static final String IGNORE_LINES_PROPERTY = "ignoreLines"; private static final String CHARSET_PROPERTY = "charset"; + private static final String LINE_SEPRATOR = "\n"; private final List violations; private final Configuration config; @@ -149,7 +150,7 @@ public J visit(Tree tree, ExecutionContext ctx) { currentHeader, ignoreLines); sourceFile = sourceFile.withPrefix( - Space.format(fixedHeader + System.lineSeparator())); + Space.format(fixedHeader + LINE_SEPRATOR)); } result = super.visit(sourceFile, ctx); } @@ -186,7 +187,7 @@ private static String fixHeaderLines(String licenseHeader, } } - return String.join(System.lineSeparator(), currentLines); + return String.join(LINE_SEPRATOR, currentLines); } private boolean hasViolation(Path filePath) { diff --git a/src/main/java/org/checkstyle/autofix/recipe/UpperEll.java b/src/main/java/org/checkstyle/autofix/recipe/UpperEll.java index c766fa5..ca62d72 100644 --- a/src/main/java/org/checkstyle/autofix/recipe/UpperEll.java +++ b/src/main/java/org/checkstyle/autofix/recipe/UpperEll.java @@ -166,7 +166,13 @@ private int computeLinePosition(J tree, J targetElement, Cursor cursor) { } private int computeColumnPosition(J tree, J targetElement, Cursor cursor) { - return computePosition(tree, targetElement, cursor, this::calculateColumnOffset); + return computePosition(tree, targetElement, cursor, out -> { + int column = calculateColumnOffset(out); + if (((J.Literal) targetElement).getValueSource().matches("^[+-].*")) { + column++; + } + return column; + }); } private int calculateColumnOffset(String out) { @@ -176,8 +182,9 @@ private int calculateColumnOffset(String out) { result = out.length(); } else { - result = out.length() - lineBreakIndex - 1; + result = out.length() - lineBreakIndex; } + return result; } diff --git a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java index 5d0da12..fa90ba0 100644 --- a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java +++ b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java @@ -20,62 +20,136 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.openrewrite.java.Assertions.java; -import java.io.IOException; +import java.io.ByteArrayOutputStream; +import java.io.File; import java.nio.charset.Charset; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Optional; import org.checkstyle.autofix.InputClassRenamer; +import org.checkstyle.autofix.parser.CheckstyleReportParser; +import org.checkstyle.autofix.parser.CheckstyleViolation; import org.openrewrite.Recipe; import org.openrewrite.test.RewriteTest; +import com.puppycrawl.tools.checkstyle.AbstractXmlTestSupport; +import com.puppycrawl.tools.checkstyle.Checker; +import com.puppycrawl.tools.checkstyle.XMLLogger; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Configuration; +import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser; +import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration; -public abstract class AbstractRecipeTest implements RewriteTest { +public abstract class AbstractRecipeTest extends AbstractXmlTestSupport implements RewriteTest { - private static final String BASE_TEST_RESOURCES_PATH = "src/test/resources/org" - + "/checkstyle/autofix/recipe/"; + protected abstract String getSubpackage(); + + protected abstract String getCheckName(); + + protected abstract Recipe createRecipe(List violations, + Configuration configuration); + + @Override + protected String getPackageLocation() { + return "org/checkstyle/autofix/recipe/" + getSubpackage(); + } private Recipe createPreprocessingRecipe() { return new InputClassRenamer(); } - protected abstract Recipe getRecipe() throws CheckstyleException; + protected void verify(String testCaseName) throws Exception { + final String inputFileName = "Input" + testCaseName + ".java"; + final String outputFileName = "Output" + testCaseName + ".java"; + final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName; + final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName; + + final Configuration config = getAllCheckConfigurations(inputPath); + final List violations = runCheckstyle(inputPath, config); + final Configuration checkConfig = extractCheckConfiguration(config, getCheckName()); + + final String beforeCode = readFile(getPath(inputPath)); + final String expectedAfterCode = readFile(getPath(outputPath)); + final Recipe mainRecipe = createRecipe(violations, checkConfig); + + testRecipe(beforeCode, expectedAfterCode, + getPath(inputPath), createPreprocessingRecipe(), mainRecipe); + } - protected void testRecipe(String recipePath, String testCaseName) throws IOException, - CheckstyleException { - final String testCaseDir = testCaseName.toLowerCase(); + protected void verify(Configuration checkConfig, String testCaseName) throws Exception { final String inputFileName = "Input" + testCaseName + ".java"; final String outputFileName = "Output" + testCaseName + ".java"; + final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName; + final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName; + + final List violations = runCheckstyle(inputPath, checkConfig); - final String beforeCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH - + recipePath + "/" + testCaseDir + "/" + inputFileName)); + final String beforeCode = readFile(getPath(inputPath)); + final String expectedAfterCode = readFile(getPath(outputPath)); - final String afterCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH - + recipePath + "/" + testCaseDir + "/" + outputFileName)); + final Recipe mainRecipe = createRecipe(violations, checkConfig); - final Recipe preprocessing = createPreprocessingRecipe(); - final Recipe mainRecipe = getRecipe(); + testRecipe(beforeCode, expectedAfterCode, + getPath(inputPath), createPreprocessingRecipe(), mainRecipe); + } + private List runCheckstyle(String inputPath, + Configuration config) throws Exception { + + final Checker checker = createChecker(config); + final ByteArrayOutputStream xmlOutput = new ByteArrayOutputStream(); + final XMLLogger logger = new XMLLogger(xmlOutput, XMLLogger.OutputStreamOptions.CLOSE); + checker.addListener(logger); + + final List filesToCheck = Collections.singletonList(new File(getPath(inputPath))); + checker.process(filesToCheck); + + final Path tempXmlPath = Files.createTempFile("checkstyle-report", ".xml"); + try { + Files.write(tempXmlPath, xmlOutput.toByteArray()); + return CheckstyleReportParser.parse(tempXmlPath); + } + finally { + Files.deleteIfExists(tempXmlPath); + } + } + + private Configuration getAllCheckConfigurations(String inputPath) throws Exception { + final String configFilePath = getPath(inputPath); + final TestInputConfiguration testInputConfiguration = + InlineConfigParser.parse(configFilePath); + return testInputConfiguration.createConfiguration(); + } + + private void testRecipe(String beforeCode, String expectedAfterCode, + String filePath, Recipe... recipes) { assertDoesNotThrow(() -> { rewriteRun( - spec -> spec.recipes(preprocessing, mainRecipe), - java(beforeCode, afterCode) + spec -> spec.recipes(recipes), + java(beforeCode, expectedAfterCode, spec -> spec.path(filePath)) ); }); } protected Configuration extractCheckConfiguration(Configuration config, String checkName) { - - return Arrays.stream(config.getChildren()) + return Optional.of(config) .filter(child -> checkName.equals(child.getName())) - .findFirst() - .orElseThrow(() -> { - return new IllegalArgumentException(checkName + "configuration not " - + "found"); - }); + .orElse(Arrays.stream(config.getChildren()) + .filter(child -> { + return "com.puppycrawl.tools.checkstyle.TreeWalker" + .equals(child.getName()); + }) + .flatMap(treeWalker -> Arrays.stream(treeWalker.getChildren())) + .filter(child -> checkName.equals(child.getName())) + .findFirst() + .orElseThrow(() -> { + return new IllegalArgumentException(checkName + + " configuration not found"); + })); } protected Charset getCharset(Configuration config) { diff --git a/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java b/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java index 6ea5d90..01ba791 100644 --- a/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java +++ b/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java @@ -17,55 +17,61 @@ package org.checkstyle.autofix.recipe; -import java.io.IOException; -import java.nio.file.Path; import java.util.List; -import java.util.Properties; -import org.checkstyle.autofix.parser.CheckstyleReportParser; import org.checkstyle.autofix.parser.CheckstyleViolation; import org.junit.jupiter.api.Test; import org.openrewrite.Recipe; -import com.puppycrawl.tools.checkstyle.ConfigurationLoader; -import com.puppycrawl.tools.checkstyle.PropertiesExpander; -import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; import com.puppycrawl.tools.checkstyle.api.Configuration; +import com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck; public class HeaderTest extends AbstractRecipeTest { @Override - protected Recipe getRecipe() throws CheckstyleException { - final String reportPath = "src/test/resources/org/checkstyle/autofix/recipe/header" - + "/report.xml"; - - final String configPath = "src/test/resources/org/checkstyle/autofix/recipe/header" - + "/config.xml"; + protected String getSubpackage() { + return "header"; + } - final Configuration config = ConfigurationLoader.loadConfiguration( - configPath, new PropertiesExpander(new Properties()) - ); + @Override + protected String getCheckName() { + return "Header"; + } - final List violations = - CheckstyleReportParser.parse(Path.of(reportPath)); + @Override + protected Recipe createRecipe(List violations, Configuration config) { - return new Header(violations, - extractCheckConfiguration(config, "Header"), getCharset(config)); + return new Header(violations, config, getCharset(config)); } @Test - void headerTest() throws IOException, CheckstyleException { - testRecipe("header", "HeaderBlankLines"); + void headerTest() throws Exception { + final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class); + final String headerPath = "src/test/resources/org/checkstyle/autofix/" + + "recipe/header/header.txt"; + checkConfig.addProperty("headerFile", headerPath); + checkConfig.addProperty("ignoreLines", "3"); + verify(checkConfig, "HeaderBlankLines"); } @Test - void headerCommentTest() throws IOException, CheckstyleException { - testRecipe("header", "HeaderComments"); + void headerCommentTest() throws Exception { + final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class); + final String headerPath = "src/test/resources/org/checkstyle/autofix/" + + "recipe/header/header.txt"; + checkConfig.addProperty("headerFile", headerPath); + checkConfig.addProperty("ignoreLines", "3"); + verify(checkConfig, "HeaderComments"); } @Test - void headerIncorrect() throws IOException, CheckstyleException { - testRecipe("header", "HeaderIncorrect"); + void headerIncorrect() throws Exception { + final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class); + final String headerPath = "src/test/resources/org/checkstyle/" + + "autofix/recipe/header/header.txt"; + checkConfig.addProperty("headerFile", headerPath); + checkConfig.addProperty("ignoreLines", "3"); + verify(checkConfig, "HeaderIncorrect"); } - } diff --git a/src/test/java/org/checkstyle/autofix/recipe/UpperEllTest.java b/src/test/java/org/checkstyle/autofix/recipe/UpperEllTest.java index 0225e12..0e4a12b 100644 --- a/src/test/java/org/checkstyle/autofix/recipe/UpperEllTest.java +++ b/src/test/java/org/checkstyle/autofix/recipe/UpperEllTest.java @@ -17,41 +17,44 @@ package org.checkstyle.autofix.recipe; -import java.io.IOException; -import java.nio.file.Path; import java.util.List; -import org.checkstyle.autofix.parser.CheckstyleReportParser; import org.checkstyle.autofix.parser.CheckstyleViolation; import org.junit.jupiter.api.Test; import org.openrewrite.Recipe; -import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.api.Configuration; public class UpperEllTest extends AbstractRecipeTest { @Override - protected Recipe getRecipe() { - final String reportPath = "src/test/resources/org/checkstyle/autofix/recipe/upperell" - + "/report.xml"; + protected String getSubpackage() { + return "upperell"; + } + + @Override + protected String getCheckName() { + return "com.puppycrawl.tools.checkstyle.checks.UpperEllCheck"; + } + + @Override + protected Recipe createRecipe(List violations, Configuration config) { - final List violations = - CheckstyleReportParser.parse(Path.of(reportPath)); return new UpperEll(violations); } @Test - void hexOctalLiteralTest() throws IOException, CheckstyleException { - testRecipe("upperell", "HexOctalLiteral"); + void hexOctalLiteral() throws Exception { + verify("HexOctalLiteral"); } @Test - void complexLongLiterals() throws IOException, CheckstyleException { - testRecipe("upperell", "ComplexLongLiterals"); + void complexLongLiterals() throws Exception { + verify("ComplexLongLiterals"); } @Test - void stringAndCommentTest() throws IOException, CheckstyleException { - testRecipe("upperell", "StringAndComments"); + void stringAndComments() throws Exception { + verify("StringAndComments"); } } diff --git a/src/test/resources/org/checkstyle/autofix/recipe/header/config.xml b/src/test/resources/org/checkstyle/autofix/recipe/header/config.xml deleted file mode 100644 index eb94d5d..0000000 --- a/src/test/resources/org/checkstyle/autofix/recipe/header/config.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/test/resources/org/checkstyle/autofix/recipe/header/report.xml b/src/test/resources/org/checkstyle/autofix/recipe/header/report.xml deleted file mode 100644 index d33ca9c..0000000 --- a/src/test/resources/org/checkstyle/autofix/recipe/header/report.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - diff --git a/src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/InputComplexLongLiterals.java b/src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/InputComplexLongLiterals.java index 6b4051a..01ca648 100644 --- a/src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/InputComplexLongLiterals.java +++ b/src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/InputComplexLongLiterals.java @@ -1,17 +1,31 @@ +/*xml + + + + + + + + + + +*/ + package org.checkstyle.autofix.recipe.upperell.complexlongliterals; public class InputComplexLongLiterals { private long withUnderscores = 1_000_000l; private long multipleUnderscores = 1_234_567_890l; - private long maxLong = 9223372036854775807l; - private long minLong = -9223372036854775808l; + private long maxLong = 9223372036854775807l; //suppressed violation + private long minLong = -9223372036854775808l; //suppressed violation private Long nullLong = null; private Long simpleLong = 1234l; private Long negativeLong = -5678l; private Long underscoreLong = 1_000_000l; - Long maxLongObject = 9223372036854775807l; //suppressed violation + Long maxLongObject = 9223372036854775807l; //suppressed violation Long minLongObject = -9223372036854775808l; //suppressed violation public long calculate(long input1, long input2) { diff --git a/src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/OutputComplexLongLiterals.java b/src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/OutputComplexLongLiterals.java index ebe86a0..c7e2bef 100644 --- a/src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/OutputComplexLongLiterals.java +++ b/src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/OutputComplexLongLiterals.java @@ -1,17 +1,31 @@ +/*xml + + + + + + + + + + +*/ + package org.checkstyle.autofix.recipe.upperell.complexlongliterals; public class OutputComplexLongLiterals { private long withUnderscores = 1_000_000L; private long multipleUnderscores = 1_234_567_890L; - private long maxLong = 9223372036854775807L; - private long minLong = -9223372036854775808L; + private long maxLong = 9223372036854775807l; //suppressed violation + private long minLong = -9223372036854775808l; //suppressed violation private Long nullLong = null; private Long simpleLong = 1234L; private Long negativeLong = -5678L; private Long underscoreLong = 1_000_000L; - Long maxLongObject = 9223372036854775807l; //suppressed violation + Long maxLongObject = 9223372036854775807l; //suppressed violation Long minLongObject = -9223372036854775808l; //suppressed violation public long calculate(long input1, long input2) { diff --git a/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java b/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java index fbaf30c..da45456 100644 --- a/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java +++ b/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java @@ -1,3 +1,16 @@ +/*xml + + + + + + + + + + +*/ + package org.checkstyle.autofix.recipe.upperell.hexoctalliteral; public class InputHexOctalLiteral { @@ -8,8 +21,8 @@ public class InputHexOctalLiteral { private long decimal = 12345l; public void calculateValues() { - long hexResult = 0xDEADBEEFl + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl - long octalResult = 01234l + 0xDEADBEEFl; //suppressed violation for 0xDEADBEFl + long hexResult = 0xDEADBEEFl + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl + long octalResult = 01234l + 0xDEADBEEFl; //suppressed violation for 01234L long binaryResult = 0b11110000l; } } diff --git a/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/OutputHexOctalLiteral.java b/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/OutputHexOctalLiteral.java index 840f57a..4523ba0 100644 --- a/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/OutputHexOctalLiteral.java +++ b/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/OutputHexOctalLiteral.java @@ -1,3 +1,16 @@ +/*xml + + + + + + + + + + +*/ + package org.checkstyle.autofix.recipe.upperell.hexoctalliteral; public class OutputHexOctalLiteral { @@ -8,8 +21,8 @@ public class OutputHexOctalLiteral { private long decimal = 12345L; public void calculateValues() { - long hexResult = 0xDEADBEEFL + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl - long octalResult = 01234L + 0xDEADBEEFl; //suppressed violation for 0xDEADBEFl + long hexResult = 0xDEADBEEFL + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl + long octalResult = 01234l + 0xDEADBEEFL; //suppressed violation for 01234L long binaryResult = 0b11110000L; } } diff --git a/src/test/resources/org/checkstyle/autofix/recipe/upperell/report.xml b/src/test/resources/org/checkstyle/autofix/recipe/upperell/report.xml deleted file mode 100644 index 2d26aaa..0000000 --- a/src/test/resources/org/checkstyle/autofix/recipe/upperell/report.xml +++ /dev/null @@ -1,110 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/InputStringAndComments.java b/src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/InputStringAndComments.java index 5929572..8ad1a22 100644 --- a/src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/InputStringAndComments.java +++ b/src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/InputStringAndComments.java @@ -1,3 +1,12 @@ +/*xml + + + + + + +*/ + package org.checkstyle.autofix.recipe.upperell.stringandcomments; public class InputStringAndComments { diff --git a/src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/OutputStringAndComments.java b/src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/OutputStringAndComments.java index cf274a1..bdf6e48 100644 --- a/src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/OutputStringAndComments.java +++ b/src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/OutputStringAndComments.java @@ -1,3 +1,12 @@ +/*xml + + + + + + +*/ + package org.checkstyle.autofix.recipe.upperell.stringandcomments; public class OutputStringAndComments {