diff --git a/src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java b/src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java index 154eca0..7ee2073 100644 --- a/src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java +++ b/src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java @@ -34,7 +34,7 @@ private ConfigurationLoader() { // utility class } - private static CheckConfiguration mapConfiguration(Configuration config) { + public static CheckConfiguration mapConfiguration(Configuration config) { final Map properties = new HashMap<>(); final String[] propertyNames = config.getPropertyNames(); for (String propertyName : propertyNames) { diff --git a/src/main/java/org/checkstyle/autofix/recipe/Header.java b/src/main/java/org/checkstyle/autofix/recipe/Header.java index 37c7ecc..4c3e8bc 100644 --- a/src/main/java/org/checkstyle/autofix/recipe/Header.java +++ b/src/main/java/org/checkstyle/autofix/recipe/Header.java @@ -44,6 +44,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_SEPARATOR = "\n"; private final List violations; private final CheckConfiguration config; @@ -129,7 +130,7 @@ public J visit(Tree tree, ExecutionContext ctx) { currentHeader, ignoreLines); sourceFile = sourceFile.withPrefix( - Space.format(fixedHeader + System.lineSeparator())); + Space.format(fixedHeader + LINE_SEPARATOR)); } result = super.visit(sourceFile, ctx); } @@ -166,7 +167,7 @@ private static String fixHeaderLines(String licenseHeader, } } - return String.join(System.lineSeparator(), currentLines); + return String.join(LINE_SEPARATOR, currentLines); } private boolean hasViolation(Path filePath) { diff --git a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java deleted file mode 100644 index 1c1b0b2..0000000 --- a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////////////////////// -// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. -// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -/////////////////////////////////////////////////////////////////////////////////////////////// - -package org.checkstyle.autofix.recipe; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.openrewrite.java.Assertions.java; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; - -import org.checkstyle.autofix.InputClassRenamer; -import org.openrewrite.Recipe; -import org.openrewrite.test.RewriteTest; - -import com.puppycrawl.tools.checkstyle.api.CheckstyleException; - -public abstract class AbstractRecipeTest implements RewriteTest { - - private static final String BASE_TEST_RESOURCES_PATH = "src/test/resources/org" - + "/checkstyle/autofix/recipe/"; - - private Recipe createPreprocessingRecipe() { - return new InputClassRenamer(); - } - - protected abstract Recipe getRecipe() throws CheckstyleException; - - protected void testRecipe(String recipePath, String testCaseName) throws IOException, - CheckstyleException { - final String testCaseDir = testCaseName.toLowerCase(); - final String inputFileName = "Input" + testCaseName + ".java"; - final String outputFileName = "Output" + testCaseName + ".java"; - - final String beforeCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH - + recipePath + "/" + testCaseDir + "/" + inputFileName)); - - final String afterCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH - + recipePath + "/" + testCaseDir + "/" + outputFileName)); - - final Recipe preprocessing = createPreprocessingRecipe(); - final Recipe mainRecipe = getRecipe(); - - assertDoesNotThrow(() -> { - rewriteRun( - spec -> spec.recipes(preprocessing, mainRecipe), - java(beforeCode, afterCode) - ); - }); - } - -} diff --git a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java index cfea242..3757cda 100644 --- a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java +++ b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java @@ -29,8 +29,10 @@ import org.checkstyle.autofix.InputClassRenamer; import org.checkstyle.autofix.RemoveViolationComments; +import org.checkstyle.autofix.parser.CheckConfiguration; import org.checkstyle.autofix.parser.CheckstyleReportParser; import org.checkstyle.autofix.parser.CheckstyleViolation; +import org.checkstyle.autofix.parser.ConfigurationLoader; import org.openrewrite.Recipe; import org.openrewrite.test.RewriteTest; @@ -46,7 +48,8 @@ public abstract class AbstractRecipeTestSupport extends AbstractXmlTestSupport protected abstract String getSubpackage(); - protected abstract Recipe createRecipe(List violations); + protected abstract Recipe createRecipe(List violations, + CheckConfiguration configuration); @Override protected String getPackageLocation() { @@ -54,21 +57,22 @@ protected String getPackageLocation() { } 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; + verify(getCheckConfigurations(getInputFilePath(testCaseName)), testCaseName); + } + + protected void verify(Configuration config, String testCaseName) throws Exception { - verifyOutputFile(outputPath); + final String inputPath = getInputFilePath(testCaseName); + final String outputPath = getOutputFilePath(testCaseName); + + verifyOutputFile(outputPath, config); - final Configuration config = getCheckConfigurations(inputPath); final List violations = runCheckstyle(inputPath, config); final String beforeCode = readFile(getPath(inputPath)); final String expectedAfterCode = readFile(getPath(outputPath)); - - final Recipe mainRecipe = createRecipe(violations); - + final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config); + final Recipe mainRecipe = createRecipe(violations, checkConfig); testRecipe(beforeCode, expectedAfterCode, getPath(inputPath), new InputClassRenamer(), new RemoveViolationComments(), mainRecipe); @@ -95,9 +99,8 @@ private List runCheckstyle(String inputPath, } } - private void verifyOutputFile(String outputPath) throws Exception { + private void verifyOutputFile(String outputPath, Configuration config) throws Exception { - final Configuration config = getCheckConfigurations(outputPath); final List violations = runCheckstyle(outputPath, config); if (!violations.isEmpty()) { final StringBuilder violationMessage = @@ -133,4 +136,15 @@ private void testRecipe(String beforeCode, String expectedAfterCode, ); }); } + + private String getInputFilePath(String testCaseName) { + final String inputFileName = "Input" + testCaseName + ".java"; + return testCaseName.toLowerCase() + "/" + inputFileName; + } + + private String getOutputFilePath(String testCaseName) { + final String inputFileName = "Output" + testCaseName + ".java"; + return testCaseName.toLowerCase() + "/" + inputFileName; + } + } diff --git a/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java b/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java index 65eb986..6b641e7 100644 --- a/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java +++ b/src/test/java/org/checkstyle/autofix/recipe/HeaderTest.java @@ -17,53 +17,52 @@ package org.checkstyle.autofix.recipe; -import java.io.IOException; -import java.nio.file.Path; import java.util.List; import org.checkstyle.autofix.parser.CheckConfiguration; -import org.checkstyle.autofix.parser.CheckstyleReportParser; import org.checkstyle.autofix.parser.CheckstyleViolation; -import org.checkstyle.autofix.parser.ConfigurationLoader; import org.junit.jupiter.api.Test; import org.openrewrite.Recipe; -import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck; -public class HeaderTest extends AbstractRecipeTest { +public class HeaderTest extends AbstractRecipeTestSupport { @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"; - - final CheckConfiguration config = ConfigurationLoader.loadConfiguration(configPath, null); + protected String getSubpackage() { + return "header"; + } - final List violations = - CheckstyleReportParser.parse(Path.of(reportPath)); + @Override + protected Recipe createRecipe(List violations, CheckConfiguration config) { - final CheckConfiguration checkConfig = config - .getChildConfig("Header"); + return new Header(violations, config); - return new Header(violations, checkConfig); } @Test - void headerTest() throws IOException, CheckstyleException { - testRecipe("header", "HeaderBlankLines"); + void headerTest() throws Exception { + verify(getHeaderConfig(), "HeaderBlankLines"); } @Test - void headerCommentTest() throws IOException, CheckstyleException { - testRecipe("header", "HeaderComments"); + void headerCommentTest() throws Exception { + verify(getHeaderConfig(), "HeaderComments"); } @Test - void headerIncorrect() throws IOException, CheckstyleException { - testRecipe("header", "HeaderIncorrect"); + void headerIncorrect() throws Exception { + verify(getHeaderConfig(), "HeaderIncorrect"); + } + + private DefaultConfiguration getHeaderConfig() { + 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"); + return checkConfig; } } diff --git a/src/test/java/org/checkstyle/autofix/recipe/UpperEllTest.java b/src/test/java/org/checkstyle/autofix/recipe/UpperEllTest.java index a2c5132..3595425 100644 --- a/src/test/java/org/checkstyle/autofix/recipe/UpperEllTest.java +++ b/src/test/java/org/checkstyle/autofix/recipe/UpperEllTest.java @@ -19,6 +19,7 @@ import java.util.List; +import org.checkstyle.autofix.parser.CheckConfiguration; import org.checkstyle.autofix.parser.CheckstyleViolation; import org.junit.jupiter.api.Test; import org.openrewrite.Recipe; @@ -31,7 +32,7 @@ protected String getSubpackage() { } @Override - protected Recipe createRecipe(List violations) { + protected Recipe createRecipe(List violations, CheckConfiguration config) { return new UpperEll(violations); } 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 @@ - - - - - - - - - - - - -