Skip to content

Commit 754f115

Browse files
committed
Issue #67: use ChecstyleAutoFix in Tests
1 parent 9279400 commit 754f115

File tree

3 files changed

+80
-36
lines changed

3 files changed

+80
-36
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
</dependency>
6666

6767
<!-- Test dependencies -->
68+
<dependency>
69+
<groupId>com.google.truth</groupId>
70+
<artifactId>truth</artifactId>
71+
<version>1.4.4</version>
72+
<scope>test</scope>
73+
</dependency>
6874
<dependency>
6975
<groupId>com.puppycrawl.tools</groupId>
7076
<artifactId>checkstyle</artifactId>

src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public class CheckstyleAutoFix extends Recipe {
4848
required = false)
4949
private String propertiesPath;
5050

51+
public CheckstyleAutoFix(String violationPath, String configPath) {
52+
this.violationReportPath = violationPath;
53+
this.configurationPath = configPath;
54+
}
55+
5156
@Override
5257
public String getDisplayName() {
5358
return "Checkstyle autoFix";

src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@
2020
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2121
import static org.openrewrite.java.Assertions.java;
2222

23-
import java.io.ByteArrayOutputStream;
24-
import java.io.File;
23+
import java.io.*;
2524
import java.nio.file.Files;
2625
import java.nio.file.Path;
2726
import java.util.Collections;
2827
import java.util.List;
2928

29+
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
30+
import org.checkstyle.autofix.CheckstyleAutoFix;
3031
import org.checkstyle.autofix.InputClassRenamer;
3132
import org.checkstyle.autofix.RemoveViolationComments;
3233
import org.checkstyle.autofix.parser.CheckConfiguration;
33-
import org.checkstyle.autofix.parser.CheckstyleReportParser;
3434
import org.checkstyle.autofix.parser.CheckstyleViolation;
35-
import org.checkstyle.autofix.parser.ConfigurationLoader;
3635
import org.openrewrite.Recipe;
3736
import org.openrewrite.test.RewriteTest;
3837

@@ -57,7 +56,25 @@ protected String getPackageLocation() {
5756
}
5857

5958
protected void verify(String testCaseName) throws Exception {
60-
verify(getCheckConfigurations(getInputFilePath(testCaseName)), testCaseName);
59+
60+
final String inputPath = getInputFilePath(testCaseName);
61+
final String outputPath = getOutputFilePath(testCaseName);
62+
final Configuration config = getCheckConfigurations(inputPath);
63+
verifyOutputFile(outputPath, config);
64+
65+
final Path violationPath = runCheckstyle(inputPath, config);
66+
List<String> lines = Files.readAllLines(Path.of(getPath(inputPath)));
67+
List<String> inlineConfig = getInlineConfig(lines);
68+
final Path configPath = Files.createTempFile("checkstyle-config", ".xml");
69+
Files.write(configPath, inlineConfig);
70+
71+
final String beforeCode = readFile(getPath(inputPath));
72+
final String expectedAfterCode = readFile(getPath(outputPath));
73+
74+
final Recipe recipe = new CheckstyleAutoFix(violationPath.toString(), configPath.toString());
75+
76+
testRecipe(beforeCode, expectedAfterCode,
77+
getPath(inputPath), new InputClassRenamer(), recipe, new RemoveViolationComments());
6178
}
6279

6380
protected void verify(Configuration config, String testCaseName) throws Exception {
@@ -67,18 +84,27 @@ protected void verify(Configuration config, String testCaseName) throws Exceptio
6784

6885
verifyOutputFile(outputPath, config);
6986

70-
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config);
87+
final Path violationPath = runCheckstyle(inputPath, config);
88+
89+
7190

7291
final String beforeCode = readFile(getPath(inputPath));
7392
final String expectedAfterCode = readFile(getPath(outputPath));
74-
final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
75-
final Recipe mainRecipe = createRecipe(violations, checkConfig);
93+
final Path configPath = Files.createTempFile("checkstyle-config", ".xml");
94+
writeConfigurationToXml(config, configPath.toString());
95+
96+
List<String> lines = Files.readAllLines(configPath);
97+
for (String line : lines) {
98+
System.out.println(line);
99+
}
100+
101+
final Recipe recipe = new CheckstyleAutoFix(violationPath.toString(), configPath.toString());
76102
testRecipe(beforeCode, expectedAfterCode,
77-
getPath(inputPath), new InputClassRenamer(),
78-
new RemoveViolationComments(), mainRecipe);
103+
getPath(inputPath), new InputClassRenamer(), recipe,
104+
new RemoveViolationComments());
79105
}
80106

81-
private List<CheckstyleViolation> runCheckstyle(String inputPath,
107+
private Path runCheckstyle(String inputPath,
82108
Configuration config) throws Exception {
83109

84110
final Checker checker = createChecker(config);
@@ -90,34 +116,14 @@ private List<CheckstyleViolation> runCheckstyle(String inputPath,
90116
checker.process(filesToCheck);
91117

92118
final Path tempXmlPath = Files.createTempFile("checkstyle-report", ".xml");
93-
try {
94-
Files.write(tempXmlPath, xmlOutput.toByteArray());
95-
return CheckstyleReportParser.parse(tempXmlPath);
96-
}
97-
finally {
98-
Files.deleteIfExists(tempXmlPath);
99-
}
100-
}
101-
102-
private void verifyOutputFile(String outputPath, Configuration config) throws Exception {
103-
104-
final List<CheckstyleViolation> violations = runCheckstyle(outputPath, config);
105-
if (!violations.isEmpty()) {
106-
final StringBuilder violationMessage =
107-
new StringBuilder("Checkstyle violations found in the output file:\n");
108119

109-
violationMessage.append("outputFile: ").append(getPath(outputPath)).append("\n");
120+
Files.write(tempXmlPath, xmlOutput.toByteArray());
121+
return tempXmlPath;
110122

111-
for (CheckstyleViolation violation : violations) {
112-
violationMessage
113-
.append("line: ").append(violation.getLine())
114-
.append(", col: ").append(violation.getColumn())
115-
.append(", message: ").append(violation.getMessage())
116-
.append("\n");
117-
}
123+
}
118124

119-
throw new IllegalStateException(violationMessage.toString());
120-
}
125+
private void verifyOutputFile(String outputPath, Configuration config) throws Exception {
126+
verify(config, getPath(outputPath), new String[0]);
121127
}
122128

123129
private Configuration getCheckConfigurations(String inputPath) throws Exception {
@@ -137,6 +143,33 @@ private void testRecipe(String beforeCode, String expectedAfterCode,
137143
});
138144
}
139145

146+
public static void writeConfigurationToXml(Configuration config, String filePath) throws IOException, CheckstyleException {
147+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
148+
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
149+
writeModule(config, writer);
150+
}
151+
}
152+
153+
private static void writeModule(Configuration config, BufferedWriter writer) throws IOException, CheckstyleException {
154+
writer.write("<module name=\"" + config.getName() + "\">\n");
155+
for (String propName : config.getPropertyNames())
156+
writer.write(" <property name=\"" + propName + "\" value=\""
157+
+ config.getProperty(propName).replace("&", "&amp;")
158+
.replace("<", "&lt;").replace(">", "&gt;")
159+
.replace("\"", "&quot;").replace("'", "&apos;")
160+
+ "\"/>\n");
161+
for (Configuration child : config.getChildren())
162+
writeModule(child, writer);
163+
writer.write("</module>\n");
164+
}
165+
166+
private List<String> getInlineConfig(List<String> lines) {
167+
return lines.stream()
168+
.skip(1L)
169+
.takeWhile((line) -> !line.startsWith("*/"))
170+
.toList();
171+
}
172+
140173
private String getInputFilePath(String testCaseName) {
141174
final String inputFileName = "Input" + testCaseName + ".java";
142175
return testCaseName.toLowerCase() + "/" + inputFileName;

0 commit comments

Comments
 (0)