diff --git a/pom.xml b/pom.xml
index 94d7ab6..647c9e5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -65,6 +65,12 @@
+
+ com.google.truth
+ truth
+ 1.4.4
+ test
+
com.puppycrawl.tools
checkstyle
diff --git a/src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java b/src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java
index a4fd3a7..86ce556 100644
--- a/src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java
+++ b/src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java
@@ -48,6 +48,11 @@ public class CheckstyleAutoFix extends Recipe {
required = false)
private String propertiesPath;
+ public CheckstyleAutoFix(String violationPath, String configPath) {
+ this.violationReportPath = violationPath;
+ this.configurationPath = configPath;
+ }
+
@Override
public String getDisplayName() {
return "Checkstyle autoFix";
diff --git a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java
index 3757cda..35bf393 100644
--- a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java
+++ b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java
@@ -20,19 +20,18 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.openrewrite.java.Assertions.java;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
+import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
+import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
+import org.checkstyle.autofix.CheckstyleAutoFix;
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;
@@ -57,7 +56,25 @@ protected String getPackageLocation() {
}
protected void verify(String testCaseName) throws Exception {
- verify(getCheckConfigurations(getInputFilePath(testCaseName)), testCaseName);
+
+ final String inputPath = getInputFilePath(testCaseName);
+ final String outputPath = getOutputFilePath(testCaseName);
+ final Configuration config = getCheckConfigurations(inputPath);
+ verifyOutputFile(outputPath, config);
+
+ final Path violationPath = runCheckstyle(inputPath, config);
+ List lines = Files.readAllLines(Path.of(getPath(inputPath)));
+ List inlineConfig = getInlineConfig(lines);
+ final Path configPath = Files.createTempFile("checkstyle-config", ".xml");
+ Files.write(configPath, inlineConfig);
+
+ final String beforeCode = readFile(getPath(inputPath));
+ final String expectedAfterCode = readFile(getPath(outputPath));
+
+ final Recipe recipe = new CheckstyleAutoFix(violationPath.toString(), configPath.toString());
+
+ testRecipe(beforeCode, expectedAfterCode,
+ getPath(inputPath), new InputClassRenamer(), recipe, new RemoveViolationComments());
}
protected void verify(Configuration config, String testCaseName) throws Exception {
@@ -67,18 +84,27 @@ protected void verify(Configuration config, String testCaseName) throws Exceptio
verifyOutputFile(outputPath, config);
- final List violations = runCheckstyle(inputPath, config);
+ final Path violationPath = runCheckstyle(inputPath, config);
+
+
final String beforeCode = readFile(getPath(inputPath));
final String expectedAfterCode = readFile(getPath(outputPath));
- final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
- final Recipe mainRecipe = createRecipe(violations, checkConfig);
+ final Path configPath = Files.createTempFile("checkstyle-config", ".xml");
+ writeConfigurationToXml(config, configPath.toString());
+
+ List lines = Files.readAllLines(configPath);
+ for (String line : lines) {
+ System.out.println(line);
+ }
+
+ final Recipe recipe = new CheckstyleAutoFix(violationPath.toString(), configPath.toString());
testRecipe(beforeCode, expectedAfterCode,
- getPath(inputPath), new InputClassRenamer(),
- new RemoveViolationComments(), mainRecipe);
+ getPath(inputPath), new InputClassRenamer(), recipe,
+ new RemoveViolationComments());
}
- private List runCheckstyle(String inputPath,
+ private Path runCheckstyle(String inputPath,
Configuration config) throws Exception {
final Checker checker = createChecker(config);
@@ -90,34 +116,14 @@ private List runCheckstyle(String 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 void verifyOutputFile(String outputPath, Configuration config) throws Exception {
-
- final List violations = runCheckstyle(outputPath, config);
- if (!violations.isEmpty()) {
- final StringBuilder violationMessage =
- new StringBuilder("Checkstyle violations found in the output file:\n");
- violationMessage.append("outputFile: ").append(getPath(outputPath)).append("\n");
+ Files.write(tempXmlPath, xmlOutput.toByteArray());
+ return tempXmlPath;
- for (CheckstyleViolation violation : violations) {
- violationMessage
- .append("line: ").append(violation.getLine())
- .append(", col: ").append(violation.getColumn())
- .append(", message: ").append(violation.getMessage())
- .append("\n");
- }
+ }
- throw new IllegalStateException(violationMessage.toString());
- }
+ private void verifyOutputFile(String outputPath, Configuration config) throws Exception {
+ verify(config, getPath(outputPath), new String[0]);
}
private Configuration getCheckConfigurations(String inputPath) throws Exception {
@@ -137,6 +143,33 @@ private void testRecipe(String beforeCode, String expectedAfterCode,
});
}
+ public static void writeConfigurationToXml(Configuration config, String filePath) throws IOException, CheckstyleException {
+ try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
+ writer.write("\n");
+ writeModule(config, writer);
+ }
+ }
+
+ private static void writeModule(Configuration config, BufferedWriter writer) throws IOException, CheckstyleException {
+ writer.write("\n");
+ for (String propName : config.getPropertyNames())
+ writer.write(" ", ">")
+ .replace("\"", """).replace("'", "'")
+ + "\"/>\n");
+ for (Configuration child : config.getChildren())
+ writeModule(child, writer);
+ writer.write("\n");
+ }
+
+ private List getInlineConfig(List lines) {
+ return lines.stream()
+ .skip(1L)
+ .takeWhile((line) -> !line.startsWith("*/"))
+ .toList();
+ }
+
private String getInputFilePath(String testCaseName) {
final String inputFileName = "Input" + testCaseName + ".java";
return testCaseName.toLowerCase() + "/" + inputFileName;