Skip to content

Commit 4d6bb41

Browse files
Anmol202005rdiachenko
authored andcommitted
Issue checkstyle#41: added CheckstyleAutoFix as Test Recipe
1 parent 0243ae5 commit 4d6bb41

File tree

11 files changed

+107
-86
lines changed

11 files changed

+107
-86
lines changed

config/suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
<suppress checks="DesignForExtension" files="[\\/]src[\\/]test[\\/]java[\\/]org[\\/]checkstyle[\\/]autofix[\\/]recipe[\\/]AbstractRecipeTest\.java"/>
1212
<suppress checks="DesignForExtension" files="[\\/]src[\\/]test[\\/]java[\\/]org[\\/]checkstyle[\\/]autofix[\\/]recipe[\\/]AbstractRecipeTestSupport\.java"/>
1313
<suppress checks="DesignForExtension" files="[\\/]src[\\/]main[\\/]java[\\/]org[\\/]checkstyle[\\/]autofix[\\/]CheckstyleAutoFix\.java"/>
14+
<suppress checks="ClassDataAbstractionCoupling" files="[\\/]src[\\/]test[\\/]java[\\/]org[\\/]checkstyle[\\/]autofix[\\/]recipe[\\/]AbstractRecipeTestSupport\.java"/>
1415
</suppressions>

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

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

51+
public CheckstyleAutoFix() {
52+
// default constructor
53+
}
54+
55+
public CheckstyleAutoFix(String violationReportPath, String configurationPath) {
56+
this.violationReportPath = violationReportPath;
57+
this.configurationPath = configurationPath;
58+
}
59+
5160
@Override
5261
public String getDisplayName() {
5362
return "Checkstyle autoFix";

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
public enum CheckstyleCheck {
2424
UPPERELL("com.puppycrawl.tools.checkstyle.checks.UpperEllCheck"),
25-
HEADER("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
25+
HEADER("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"),
26+
FINALLOCALVARIABLE("com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck");
2627

2728
private final String id;
2829

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.checkstyle.autofix.parser.CheckConfiguration;
3030
import org.checkstyle.autofix.parser.CheckstyleViolation;
31+
import org.checkstyle.autofix.recipe.FinalLocalVariable;
3132
import org.checkstyle.autofix.recipe.Header;
3233
import org.checkstyle.autofix.recipe.UpperEll;
3334
import org.openrewrite.Recipe;
@@ -43,6 +44,7 @@ public final class CheckstyleRecipeRegistry {
4344

4445
static {
4546
RECIPE_MAP.put(CheckstyleCheck.UPPERELL, UpperEll::new);
47+
RECIPE_MAP.put(CheckstyleCheck.FINALLOCALVARIABLE, FinalLocalVariable::new);
4648
RECIPE_MAP_WITH_CONFIG.put(CheckstyleCheck.HEADER, Header::new);
4749
}
4850

src/main/java/org/checkstyle/autofix/parser/CheckConfiguration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.Set;
2223

2324
public final class CheckConfiguration {
2425
private final String name;
@@ -45,7 +46,7 @@ private CheckConfiguration getParent() {
4546
return parent;
4647
}
4748

48-
private List<CheckConfiguration> getChildren() {
49+
public List<CheckConfiguration> getChildren() {
4950
return children;
5051
}
5152

@@ -116,6 +117,10 @@ public CheckConfiguration getConfig(String childName) {
116117
return result;
117118
}
118119

120+
public Set<String> getPropertyNames() {
121+
return properties.keySet();
122+
}
123+
119124
private void setParent(CheckConfiguration parent) {
120125
this.parent = parent;
121126
}

src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.Map;
2626
import java.util.Properties;
2727

28+
import org.checkstyle.autofix.CheckstyleCheck;
29+
2830
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
2931
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
3032
import com.puppycrawl.tools.checkstyle.api.Configuration;
@@ -56,8 +58,12 @@ public static CheckConfiguration mapConfiguration(Configuration config) {
5658
for (int index = 0; index < checkstyleChildren.length; index++) {
5759
simpleChildren[index] = mapConfiguration(checkstyleChildren[index]);
5860
}
61+
String moduleName = config.getName().toUpperCase(Locale.ROOT);
62+
if (CheckstyleCheck.fromSource(config.getName()).isPresent()) {
63+
moduleName = CheckstyleCheck.fromSource(config.getName()).get().name();
64+
}
5965

60-
return new CheckConfiguration(config.getName().toUpperCase(Locale.ROOT),
66+
return new CheckConfiguration(moduleName,
6167
properties, List.of(simpleChildren));
6268
}
6369

src/main/java/org/checkstyle/autofix/recipe/Header.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ public J visit(Tree tree, ExecutionContext ctx) {
105105
if (tree instanceof JavaSourceFile) {
106106
JavaSourceFile sourceFile = (JavaSourceFile) tree;
107107
final Path filePath = sourceFile.getSourcePath().toAbsolutePath();
108+
final String currentHeader = extractCurrentHeader(sourceFile);
109+
final String fixedHeader = licenseHeader + LINE_SEPARATOR + currentHeader;
108110

109-
if (hasViolation(filePath)) {
110-
final String currentHeader = extractCurrentHeader(sourceFile);
111-
final String fixedHeader = licenseHeader + LINE_SEPARATOR + currentHeader;
111+
if (hasViolation(filePath) && !currentHeader.startsWith(licenseHeader)) {
112112

113113
sourceFile = sourceFile.withPrefix(
114114
Space.format(fixedHeader));

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

Lines changed: 77 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2121
import static org.openrewrite.java.Assertions.java;
2222

23+
import java.io.BufferedWriter;
2324
import java.io.ByteArrayOutputStream;
2425
import java.io.File;
26+
import java.io.FileWriter;
27+
import java.io.IOException;
2528
import java.nio.file.Files;
2629
import java.nio.file.Path;
2730
import java.util.Collections;
2831
import java.util.List;
2932

33+
import org.checkstyle.autofix.CheckstyleAutoFix;
3034
import org.checkstyle.autofix.InputClassRenamer;
3135
import org.checkstyle.autofix.RemoveViolationComments;
3236
import org.checkstyle.autofix.parser.CheckConfiguration;
@@ -48,9 +52,6 @@ public abstract class AbstractRecipeTestSupport extends AbstractXmlTestSupport
4852

4953
protected abstract String getSubpackage();
5054

51-
protected abstract Recipe createRecipe(List<CheckstyleViolation> violations,
52-
CheckConfiguration configuration);
53-
5455
@Override
5556
protected String getPackageLocation() {
5657
return "org/checkstyle/autofix/recipe/" + getSubpackage();
@@ -63,38 +64,55 @@ protected void verify(String testCaseName) throws Exception {
6364
final Configuration config = getCheckConfigurations(inputPath);
6465
verifyOutputFile(outputPath, config);
6566

66-
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config);
67+
final Path violations = runCheckstyle(inputPath, config);
68+
final Path configPath = createConfigFile(config);
6769

68-
verifyWithInlineConfigParser(getPath(inputPath), convertToExpectedMessages(violations));
70+
verifyWithInlineConfigParser(getPath(inputPath),
71+
convertToExpectedMessages(CheckstyleReportParser.parse(violations)));
6972

7073
final String beforeCode = readFile(getPath(inputPath));
7174
final String expectedAfterCode = readFile(getPath(outputPath));
72-
final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
73-
final Recipe mainRecipe = createRecipe(violations, checkConfig);
74-
testRecipe(beforeCode, expectedAfterCode,
75-
getPath(inputPath), new InputClassRenamer(),
76-
mainRecipe, new RemoveViolationComments());
75+
76+
try {
77+
final Recipe mainRecipe = new CheckstyleAutoFix(violations.toString(),
78+
configPath.toString());
79+
testRecipe(beforeCode, expectedAfterCode,
80+
getPath(inputPath), new InputClassRenamer(),
81+
mainRecipe, new RemoveViolationComments());
82+
}
83+
finally {
84+
Files.deleteIfExists(configPath);
85+
Files.deleteIfExists(violations);
86+
}
7787
}
7888

7989
protected void verify(Configuration config, String testCaseName) throws Exception {
80-
8190
final String inputPath = getInputFilePath(testCaseName);
8291
final String outputPath = getOutputFilePath(testCaseName);
8392

8493
verifyOutputFile(outputPath, config);
8594

86-
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config);
95+
final Path violationReportPath = runCheckstyle(inputPath, config);
8796

8897
final String beforeCode = readFile(getPath(inputPath));
8998
final String expectedAfterCode = readFile(getPath(outputPath));
90-
final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
91-
final Recipe mainRecipe = createRecipe(violations, checkConfig);
92-
testRecipe(beforeCode, expectedAfterCode,
93-
getPath(inputPath), new InputClassRenamer(), mainRecipe);
99+
100+
final Path configPath = createConfigFile(config);
101+
102+
try {
103+
final Recipe mainRecipe = new CheckstyleAutoFix(violationReportPath.toString(),
104+
configPath.toString());
105+
testRecipe(beforeCode, expectedAfterCode,
106+
getPath(inputPath), new InputClassRenamer(), mainRecipe);
107+
}
108+
finally {
109+
Files.deleteIfExists(configPath);
110+
Files.deleteIfExists(violationReportPath);
111+
}
94112
}
95113

96-
private List<CheckstyleViolation> runCheckstyle(String inputPath,
97-
Configuration config) throws Exception {
114+
private Path runCheckstyle(String inputPath,
115+
Configuration config) throws Exception {
98116

99117
final Checker checker = createChecker(config);
100118
final ByteArrayOutputStream xmlOutput = new ByteArrayOutputStream();
@@ -105,34 +123,50 @@ private List<CheckstyleViolation> runCheckstyle(String inputPath,
105123
checker.process(filesToCheck);
106124

107125
final Path tempXmlPath = Files.createTempFile("checkstyle-report", ".xml");
108-
try {
109-
Files.write(tempXmlPath, xmlOutput.toByteArray());
110-
return CheckstyleReportParser.parse(tempXmlPath);
111-
}
112-
finally {
113-
Files.deleteIfExists(tempXmlPath);
114-
}
126+
127+
Files.write(tempXmlPath, xmlOutput.toByteArray());
128+
return tempXmlPath;
115129
}
116130

117-
private void verifyOutputFile(String outputPath, Configuration config) throws Exception {
131+
private Path createConfigFile(Configuration config) throws Exception {
132+
final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
133+
final Path configPath = Files.createTempFile("checkstyle-config", ".xml");
134+
writeConfigurationToXml(checkConfig, configPath.toString());
135+
return configPath;
136+
}
118137

119-
final List<CheckstyleViolation> violations = runCheckstyle(outputPath, config);
120-
if (!violations.isEmpty()) {
121-
final StringBuilder violationMessage =
122-
new StringBuilder("Checkstyle violations found in the output file:\n");
138+
public static void writeConfigurationToXml(CheckConfiguration config, String filePath)
139+
throws IOException {
140+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
141+
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
142+
writer.write("<!DOCTYPE module PUBLIC \"-//Checkstyle//DTD Checkstyle "
143+
+ "Configuration 1.3//EN\" \"configuration_1_3.dtd\">\n");
144+
writeModule(config, writer);
145+
}
146+
}
147+
148+
private static void writeModule(CheckConfiguration config, BufferedWriter writer)
149+
throws IOException {
123150

124-
violationMessage.append("outputFile: ").append(getPath(outputPath)).append("\n");
151+
writer.write("<module name=\"" + config.getName() + "\">\n");
125152

126-
for (CheckstyleViolation violation : violations) {
127-
violationMessage
128-
.append("line: ").append(violation.getLine())
129-
.append(", col: ").append(violation.getColumn())
130-
.append(", message: ").append(violation.getMessage())
131-
.append("\n");
132-
}
153+
for (String propName : config.getPropertyNames()) {
154+
writer.write(" <property name=\"" + propName + "\" value=\""
155+
+ config.getProperty(propName) + "\"/>\n");
156+
}
133157

134-
throw new IllegalStateException(violationMessage.toString());
158+
for (CheckConfiguration child : config.getChildren()) {
159+
writeModule(child, writer);
135160
}
161+
162+
writer.write("</module>\n");
163+
}
164+
165+
private Configuration getCheckConfigurations(String inputPath) throws Exception {
166+
final String configFilePath = getPath(inputPath);
167+
final TestInputConfiguration testInputConfiguration =
168+
InlineConfigParser.parse(configFilePath);
169+
return testInputConfiguration.createConfiguration();
136170
}
137171

138172
private String[] convertToExpectedMessages(List<CheckstyleViolation> violations) {
@@ -146,13 +180,6 @@ private String[] convertToExpectedMessages(List<CheckstyleViolation> violations)
146180
.toArray(String[]::new);
147181
}
148182

149-
private Configuration getCheckConfigurations(String inputPath) throws Exception {
150-
final String configFilePath = getPath(inputPath);
151-
final TestInputConfiguration testInputConfiguration =
152-
InlineConfigParser.parse(configFilePath);
153-
return testInputConfiguration.createConfiguration();
154-
}
155-
156183
private void testRecipe(String beforeCode, String expectedAfterCode,
157184
String filePath, Recipe... recipes) {
158185
assertDoesNotThrow(() -> {
@@ -173,4 +200,8 @@ private String getOutputFilePath(String testCaseName) {
173200
return testCaseName.toLowerCase() + "/" + inputFileName;
174201
}
175202

203+
private void verifyOutputFile(String outputPath, Configuration config) throws Exception {
204+
verify(config, getPath(outputPath), new String[0]);
205+
}
206+
176207
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@
1717

1818
package org.checkstyle.autofix.recipe;
1919

20-
import java.util.List;
21-
22-
import org.checkstyle.autofix.parser.CheckConfiguration;
23-
import org.checkstyle.autofix.parser.CheckstyleViolation;
2420
import org.junit.jupiter.api.Test;
25-
import org.openrewrite.Recipe;
2621

2722
public class FinalLocalVariableTest extends AbstractRecipeTestSupport {
2823

@@ -31,12 +26,6 @@ protected String getSubpackage() {
3126
return "finallocalvariable";
3227
}
3328

34-
@Override
35-
protected Recipe createRecipe(List<CheckstyleViolation> violations, CheckConfiguration config) {
36-
37-
return new FinalLocalVariable(violations);
38-
}
39-
4029
@Test
4130
void singleLocalTest() throws Exception {
4231
verify("SingleLocalTest");

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@
1717

1818
package org.checkstyle.autofix.recipe;
1919

20-
import java.util.List;
21-
22-
import org.checkstyle.autofix.parser.CheckConfiguration;
23-
import org.checkstyle.autofix.parser.CheckstyleViolation;
2420
import org.junit.jupiter.api.Test;
25-
import org.openrewrite.Recipe;
2621

2722
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
2823
import com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck;
@@ -34,13 +29,6 @@ protected String getSubpackage() {
3429
return "header";
3530
}
3631

37-
@Override
38-
protected Recipe createRecipe(List<CheckstyleViolation> violations, CheckConfiguration config) {
39-
40-
return new Header(violations, config);
41-
42-
}
43-
4432
@Test
4533
void headerTest() throws Exception {
4634
verify(getHeaderConfig(), "HeaderBlankLines");

0 commit comments

Comments
 (0)