Skip to content

Commit e52a3c0

Browse files
committed
Issue #41: Updated headerTest to use AbstractRecipeTestSupport
1 parent 92623b1 commit e52a3c0

File tree

8 files changed

+65
-146
lines changed

8 files changed

+65
-146
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private ConfigurationLoader() {
3434
// utility class
3535
}
3636

37-
private static CheckConfiguration mapConfiguration(Configuration config) {
37+
public static CheckConfiguration mapConfiguration(Configuration config) {
3838
final Map<String, String> properties = new HashMap<>();
3939
final String[] propertyNames = config.getPropertyNames();
4040
for (String propertyName : propertyNames) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class Header extends Recipe {
4444
private static final String HEADER_FILE_PROPERTY = "headerFile";
4545
private static final String IGNORE_LINES_PROPERTY = "ignoreLines";
4646
private static final String CHARSET_PROPERTY = "charset";
47+
private static final String LINE_SEPARATOR = "\n";
4748

4849
private final List<CheckstyleViolation> violations;
4950
private final CheckConfiguration config;
@@ -129,7 +130,7 @@ public J visit(Tree tree, ExecutionContext ctx) {
129130
currentHeader, ignoreLines);
130131

131132
sourceFile = sourceFile.withPrefix(
132-
Space.format(fixedHeader + System.lineSeparator()));
133+
Space.format(fixedHeader + LINE_SEPARATOR));
133134
}
134135
result = super.visit(sourceFile, ctx);
135136
}
@@ -139,16 +140,16 @@ public J visit(Tree tree, ExecutionContext ctx) {
139140
private String extractCurrentHeader(JavaSourceFile sourceFile) {
140141
return sourceFile.getComments().stream()
141142
.map(comment -> comment.printComment(getCursor()))
142-
.collect(Collectors.joining(System.lineSeparator()));
143+
.collect(Collectors.joining(LINE_SEPARATOR));
143144
}
144145

145146
private static String fixHeaderLines(String licenseHeader,
146147
String currentHeader, List<Integer> ignoreLines) {
147148
final List<String> currentLines = Arrays
148-
.stream(currentHeader.split(System.lineSeparator()))
149+
.stream(currentHeader.split(LINE_SEPARATOR))
149150
.collect(Collectors.toList());
150151
final List<String> licenseLines = Arrays.stream(licenseHeader.split(
151-
System.lineSeparator(), -1)).toList();
152+
LINE_SEPARATOR, -1)).toList();
152153

153154
final Set<Integer> ignoredLineNumbers = new HashSet<>(ignoreLines);
154155

@@ -166,7 +167,7 @@ private static String fixHeaderLines(String licenseHeader,
166167
}
167168
}
168169

169-
return String.join(System.lineSeparator(), currentLines);
170+
return String.join(LINE_SEPARATOR, currentLines);
170171
}
171172

172173
private boolean hasViolation(Path filePath) {

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

Lines changed: 0 additions & 67 deletions
This file was deleted.

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929

3030
import org.checkstyle.autofix.InputClassRenamer;
3131
import org.checkstyle.autofix.RemoveViolationComments;
32+
import org.checkstyle.autofix.parser.CheckConfiguration;
3233
import org.checkstyle.autofix.parser.CheckstyleReportParser;
3334
import org.checkstyle.autofix.parser.CheckstyleViolation;
35+
import org.checkstyle.autofix.parser.ConfigurationLoader;
3436
import org.openrewrite.Recipe;
3537
import org.openrewrite.test.RewriteTest;
3638

@@ -46,29 +48,31 @@ public abstract class AbstractRecipeTestSupport extends AbstractXmlTestSupport
4648

4749
protected abstract String getSubpackage();
4850

49-
protected abstract Recipe createRecipe(List<CheckstyleViolation> violations);
51+
protected abstract Recipe createRecipe(List<CheckstyleViolation> violations,
52+
CheckConfiguration configuration);
5053

5154
@Override
5255
protected String getPackageLocation() {
5356
return "org/checkstyle/autofix/recipe/" + getSubpackage();
5457
}
5558

5659
protected void verify(String testCaseName) throws Exception {
57-
final String inputFileName = "Input" + testCaseName + ".java";
58-
final String outputFileName = "Output" + testCaseName + ".java";
59-
final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName;
60-
final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName;
60+
verify(getCheckConfigurations(getInputFilePath(testCaseName)), testCaseName);
61+
}
62+
63+
protected void verify(Configuration config, String testCaseName) throws Exception {
6164

62-
verifyOutputFile(outputPath);
65+
final String inputPath = getInputFilePath(testCaseName);
66+
final String outputPath = getOutputFilePath(testCaseName);
67+
68+
verifyOutputFile(outputPath, config);
6369

64-
final Configuration config = getCheckConfigurations(inputPath);
6570
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config);
6671

6772
final String beforeCode = readFile(getPath(inputPath));
6873
final String expectedAfterCode = readFile(getPath(outputPath));
69-
70-
final Recipe mainRecipe = createRecipe(violations);
71-
74+
final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
75+
final Recipe mainRecipe = createRecipe(violations, checkConfig);
7276
testRecipe(beforeCode, expectedAfterCode,
7377
getPath(inputPath), new InputClassRenamer(),
7478
new RemoveViolationComments(), mainRecipe);
@@ -95,9 +99,8 @@ private List<CheckstyleViolation> runCheckstyle(String inputPath,
9599
}
96100
}
97101

98-
private void verifyOutputFile(String outputPath) throws Exception {
102+
private void verifyOutputFile(String outputPath, Configuration config) throws Exception {
99103

100-
final Configuration config = getCheckConfigurations(outputPath);
101104
final List<CheckstyleViolation> violations = runCheckstyle(outputPath, config);
102105
if (!violations.isEmpty()) {
103106
final StringBuilder violationMessage =
@@ -133,4 +136,15 @@ private void testRecipe(String beforeCode, String expectedAfterCode,
133136
);
134137
});
135138
}
139+
140+
private String getInputFilePath(String testCaseName) {
141+
final String inputFileName = "Input" + testCaseName + ".java";
142+
return testCaseName.toLowerCase() + "/" + inputFileName;
143+
}
144+
145+
private String getOutputFilePath(String testCaseName) {
146+
final String inputFileName = "Output" + testCaseName + ".java";
147+
return testCaseName.toLowerCase() + "/" + inputFileName;
148+
}
149+
136150
}

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,57 @@
1717

1818
package org.checkstyle.autofix.recipe;
1919

20-
import java.io.IOException;
21-
import java.nio.file.Path;
2220
import java.util.List;
2321

2422
import org.checkstyle.autofix.parser.CheckConfiguration;
25-
import org.checkstyle.autofix.parser.CheckstyleReportParser;
2623
import org.checkstyle.autofix.parser.CheckstyleViolation;
27-
import org.checkstyle.autofix.parser.ConfigurationLoader;
2824
import org.junit.jupiter.api.Test;
2925
import org.openrewrite.Recipe;
3026

31-
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
27+
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28+
import com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck;
3229

33-
public class HeaderTest extends AbstractRecipeTest {
30+
public class HeaderTest extends AbstractRecipeTestSupport {
3431

3532
@Override
36-
protected Recipe getRecipe() throws CheckstyleException {
37-
final String reportPath = "src/test/resources/org/checkstyle/autofix/recipe/header"
38-
+ "/report.xml";
39-
40-
final String configPath = "src/test/resources/org/checkstyle/autofix/recipe/header"
41-
+ "/config.xml";
42-
43-
final CheckConfiguration config = ConfigurationLoader.loadConfiguration(configPath, null);
33+
protected String getSubpackage() {
34+
return "header";
35+
}
4436

45-
final List<CheckstyleViolation> violations =
46-
CheckstyleReportParser.parse(Path.of(reportPath));
37+
@Override
38+
protected Recipe createRecipe(List<CheckstyleViolation> violations, CheckConfiguration config) {
4739

48-
final CheckConfiguration checkConfig = config
49-
.getChildConfig("Header");
40+
return new Header(violations, config);
5041

51-
return new Header(violations, checkConfig);
5242
}
5343

5444
@Test
55-
void headerTest() throws IOException, CheckstyleException {
56-
testRecipe("header", "HeaderBlankLines");
45+
void headerTest() throws Exception {
46+
final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
47+
final String headerPath = "src/test/resources/org/checkstyle/autofix/"
48+
+ "recipe/header/header.txt";
49+
checkConfig.addProperty("headerFile", headerPath);
50+
checkConfig.addProperty("ignoreLines", "3");
51+
verify(checkConfig, "HeaderBlankLines");
5752
}
5853

5954
@Test
60-
void headerCommentTest() throws IOException, CheckstyleException {
61-
testRecipe("header", "HeaderComments");
55+
void headerCommentTest() throws Exception {
56+
final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
57+
final String headerPath = "src/test/resources/org/checkstyle/autofix/"
58+
+ "recipe/header/header.txt";
59+
checkConfig.addProperty("headerFile", headerPath);
60+
checkConfig.addProperty("ignoreLines", "3");
61+
verify(checkConfig, "HeaderComments");
6262
}
6363

6464
@Test
65-
void headerIncorrect() throws IOException, CheckstyleException {
66-
testRecipe("header", "HeaderIncorrect");
65+
void headerIncorrect() throws Exception {
66+
final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
67+
final String headerPath = "src/test/resources/org/checkstyle/"
68+
+ "autofix/recipe/header/header.txt";
69+
checkConfig.addProperty("headerFile", headerPath);
70+
checkConfig.addProperty("ignoreLines", "3");
71+
verify(checkConfig, "HeaderIncorrect");
6772
}
68-
6973
}

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

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

2020
import java.util.List;
2121

22+
import org.checkstyle.autofix.parser.CheckConfiguration;
2223
import org.checkstyle.autofix.parser.CheckstyleViolation;
2324
import org.junit.jupiter.api.Test;
2425
import org.openrewrite.Recipe;
@@ -31,7 +32,7 @@ protected String getSubpackage() {
3132
}
3233

3334
@Override
34-
protected Recipe createRecipe(List<CheckstyleViolation> violations) {
35+
protected Recipe createRecipe(List<CheckstyleViolation> violations, CheckConfiguration config) {
3536

3637
return new UpperEll(violations);
3738
}

src/test/resources/org/checkstyle/autofix/recipe/header/config.xml

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/test/resources/org/checkstyle/autofix/recipe/header/report.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)