Skip to content

Commit 5a55f28

Browse files
committed
Issue #41: Updated headerTest to use AbstractRecipeTestSupport
1 parent a2d69a9 commit 5a55f28

File tree

7 files changed

+46
-135
lines changed

7 files changed

+46
-135
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/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: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
import java.util.List;
2929

3030
import org.checkstyle.autofix.InputClassRenamer;
31+
import org.checkstyle.autofix.parser.CheckConfiguration;
3132
import org.checkstyle.autofix.parser.CheckstyleReportParser;
3233
import org.checkstyle.autofix.parser.CheckstyleViolation;
34+
import org.checkstyle.autofix.parser.ConfigurationLoader;
3335
import org.openrewrite.Recipe;
3436
import org.openrewrite.test.RewriteTest;
3537

@@ -45,7 +47,8 @@ public abstract class AbstractRecipeTestSupport extends AbstractXmlTestSupport
4547

4648
protected abstract String getSubpackage();
4749

48-
protected abstract Recipe createRecipe(List<CheckstyleViolation> violations);
50+
protected abstract Recipe createRecipe(List<CheckstyleViolation> violations,
51+
CheckConfiguration configuration);
4952

5053
@Override
5154
protected String getPackageLocation() {
@@ -57,19 +60,21 @@ private Recipe createPreprocessingRecipe() {
5760
}
5861

5962
protected void verify(String testCaseName) throws Exception {
63+
verify(getCheckConfigurations(testCaseName), testCaseName);
64+
}
65+
66+
protected void verify(Configuration config, String testCaseName) throws Exception {
6067
final String inputFileName = "Input" + testCaseName + ".java";
6168
final String outputFileName = "Output" + testCaseName + ".java";
6269
final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName;
6370
final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName;
6471

65-
final Configuration config = getCheckConfigurations(inputPath);
6672
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config);
6773

6874
final String beforeCode = readFile(getPath(inputPath));
6975
final String expectedAfterCode = readFile(getPath(outputPath));
70-
71-
final Recipe mainRecipe = createRecipe(violations);
72-
76+
final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
77+
final Recipe mainRecipe = createRecipe(violations, checkConfig);
7378
testRecipe(beforeCode, expectedAfterCode,
7479
getPath(inputPath), createPreprocessingRecipe(), mainRecipe);
7580
}
@@ -95,7 +100,9 @@ private List<CheckstyleViolation> runCheckstyle(String inputPath,
95100
}
96101
}
97102

98-
private Configuration getCheckConfigurations(String inputPath) throws Exception {
103+
private Configuration getCheckConfigurations(String testCaseName) throws Exception {
104+
final String inputFileName = "Input" + testCaseName + ".java";
105+
final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName;
99106
final String configFilePath = getPath(inputPath);
100107
final TestInputConfiguration testInputConfiguration =
101108
InlineConfigParser.parse(configFilePath);

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)