Skip to content

Commit 05eeaa6

Browse files
committed
Issue #41: Updated AbstractRecipeTest to remove external report
1 parent 51ba40b commit 05eeaa6

File tree

15 files changed

+250
-224
lines changed

15 files changed

+250
-224
lines changed

pom.xml

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

6767
<!-- Test dependencies -->
68+
<dependency>
69+
<groupId>com.puppycrawl.tools</groupId>
70+
<artifactId>checkstyle</artifactId>
71+
<version>${checkstyle.version}</version>
72+
<classifier>tests</classifier>
73+
<scope>test</scope>
74+
</dependency>
6875
<dependency>
6976
<groupId>org.openrewrite</groupId>
7077
<artifactId>rewrite-test</artifactId>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class Header extends Recipe {
4646
private static final String HEADER_FILE_PROPERTY = "headerFile";
4747
private static final String IGNORE_LINES_PROPERTY = "ignoreLines";
4848
private static final String CHARSET_PROPERTY = "charset";
49+
private static final String LINE_SEPRATOR = "\n";
4950

5051
private final List<CheckstyleViolation> violations;
5152
private final Configuration config;
@@ -149,7 +150,7 @@ public J visit(Tree tree, ExecutionContext ctx) {
149150
currentHeader, ignoreLines);
150151

151152
sourceFile = sourceFile.withPrefix(
152-
Space.format(fixedHeader + System.lineSeparator()));
153+
Space.format(fixedHeader + LINE_SEPRATOR));
153154
}
154155
result = super.visit(sourceFile, ctx);
155156
}
@@ -186,7 +187,7 @@ private static String fixHeaderLines(String licenseHeader,
186187
}
187188
}
188189

189-
return String.join(System.lineSeparator(), currentLines);
190+
return String.join(LINE_SEPRATOR, currentLines);
190191
}
191192

192193
private boolean hasViolation(Path filePath) {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ private int computeLinePosition(J tree, J targetElement, Cursor cursor) {
166166
}
167167

168168
private int computeColumnPosition(J tree, J targetElement, Cursor cursor) {
169-
return computePosition(tree, targetElement, cursor, this::calculateColumnOffset);
169+
return computePosition(tree, targetElement, cursor, out -> {
170+
int column = calculateColumnOffset(out);
171+
if (((J.Literal) targetElement).getValueSource().matches("^[+-].*")) {
172+
column++;
173+
}
174+
return column;
175+
});
170176
}
171177

172178
private int calculateColumnOffset(String out) {
@@ -176,8 +182,9 @@ private int calculateColumnOffset(String out) {
176182
result = out.length();
177183
}
178184
else {
179-
result = out.length() - lineBreakIndex - 1;
185+
result = out.length() - lineBreakIndex;
180186
}
187+
181188
return result;
182189
}
183190

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

Lines changed: 98 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,136 @@
2020
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2121
import static org.openrewrite.java.Assertions.java;
2222

23-
import java.io.IOException;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.File;
2425
import java.nio.charset.Charset;
2526
import java.nio.file.Files;
26-
import java.nio.file.Paths;
27+
import java.nio.file.Path;
2728
import java.util.Arrays;
29+
import java.util.Collections;
30+
import java.util.List;
31+
import java.util.Optional;
2832

2933
import org.checkstyle.autofix.InputClassRenamer;
34+
import org.checkstyle.autofix.parser.CheckstyleReportParser;
35+
import org.checkstyle.autofix.parser.CheckstyleViolation;
3036
import org.openrewrite.Recipe;
3137
import org.openrewrite.test.RewriteTest;
3238

39+
import com.puppycrawl.tools.checkstyle.AbstractXmlTestSupport;
40+
import com.puppycrawl.tools.checkstyle.Checker;
41+
import com.puppycrawl.tools.checkstyle.XMLLogger;
3342
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
3443
import com.puppycrawl.tools.checkstyle.api.Configuration;
44+
import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser;
45+
import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration;
3546

36-
public abstract class AbstractRecipeTest implements RewriteTest {
47+
public abstract class AbstractRecipeTest extends AbstractXmlTestSupport implements RewriteTest {
3748

38-
private static final String BASE_TEST_RESOURCES_PATH = "src/test/resources/org"
39-
+ "/checkstyle/autofix/recipe/";
49+
protected abstract String getSubpackage();
50+
51+
protected abstract String getCheckName();
52+
53+
protected abstract Recipe createRecipe(List<CheckstyleViolation> violations,
54+
Configuration configuration);
55+
56+
@Override
57+
protected String getPackageLocation() {
58+
return "org/checkstyle/autofix/recipe/" + getSubpackage();
59+
}
4060

4161
private Recipe createPreprocessingRecipe() {
4262
return new InputClassRenamer();
4363
}
4464

45-
protected abstract Recipe getRecipe() throws CheckstyleException;
65+
protected void verify(String testCaseName) throws Exception {
66+
final String inputFileName = "Input" + testCaseName + ".java";
67+
final String outputFileName = "Output" + testCaseName + ".java";
68+
final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName;
69+
final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName;
70+
71+
final Configuration config = getAllCheckConfigurations(inputPath);
72+
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config);
73+
final Configuration checkConfig = extractCheckConfiguration(config, getCheckName());
74+
75+
final String beforeCode = readFile(getPath(inputPath));
76+
final String expectedAfterCode = readFile(getPath(outputPath));
77+
final Recipe mainRecipe = createRecipe(violations, checkConfig);
78+
79+
testRecipe(beforeCode, expectedAfterCode,
80+
getPath(inputPath), createPreprocessingRecipe(), mainRecipe);
81+
}
4682

47-
protected void testRecipe(String recipePath, String testCaseName) throws IOException,
48-
CheckstyleException {
49-
final String testCaseDir = testCaseName.toLowerCase();
83+
protected void verify(Configuration checkConfig, String testCaseName) throws Exception {
5084
final String inputFileName = "Input" + testCaseName + ".java";
5185
final String outputFileName = "Output" + testCaseName + ".java";
86+
final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName;
87+
final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName;
88+
89+
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, checkConfig);
5290

53-
final String beforeCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH
54-
+ recipePath + "/" + testCaseDir + "/" + inputFileName));
91+
final String beforeCode = readFile(getPath(inputPath));
92+
final String expectedAfterCode = readFile(getPath(outputPath));
5593

56-
final String afterCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH
57-
+ recipePath + "/" + testCaseDir + "/" + outputFileName));
94+
final Recipe mainRecipe = createRecipe(violations, checkConfig);
5895

59-
final Recipe preprocessing = createPreprocessingRecipe();
60-
final Recipe mainRecipe = getRecipe();
96+
testRecipe(beforeCode, expectedAfterCode,
97+
getPath(inputPath), createPreprocessingRecipe(), mainRecipe);
98+
}
6199

100+
private List<CheckstyleViolation> runCheckstyle(String inputPath,
101+
Configuration config) throws Exception {
102+
103+
final Checker checker = createChecker(config);
104+
final ByteArrayOutputStream xmlOutput = new ByteArrayOutputStream();
105+
final XMLLogger logger = new XMLLogger(xmlOutput, XMLLogger.OutputStreamOptions.CLOSE);
106+
checker.addListener(logger);
107+
108+
final List<File> filesToCheck = Collections.singletonList(new File(getPath(inputPath)));
109+
checker.process(filesToCheck);
110+
111+
final Path tempXmlPath = Files.createTempFile("checkstyle-report", ".xml");
112+
try {
113+
Files.write(tempXmlPath, xmlOutput.toByteArray());
114+
return CheckstyleReportParser.parse(tempXmlPath);
115+
}
116+
finally {
117+
Files.deleteIfExists(tempXmlPath);
118+
}
119+
}
120+
121+
private Configuration getAllCheckConfigurations(String inputPath) throws Exception {
122+
final String configFilePath = getPath(inputPath);
123+
final TestInputConfiguration testInputConfiguration =
124+
InlineConfigParser.parse(configFilePath);
125+
return testInputConfiguration.createConfiguration();
126+
}
127+
128+
private void testRecipe(String beforeCode, String expectedAfterCode,
129+
String filePath, Recipe... recipes) {
62130
assertDoesNotThrow(() -> {
63131
rewriteRun(
64-
spec -> spec.recipes(preprocessing, mainRecipe),
65-
java(beforeCode, afterCode)
132+
spec -> spec.recipes(recipes),
133+
java(beforeCode, expectedAfterCode, spec -> spec.path(filePath))
66134
);
67135
});
68136
}
69137

70138
protected Configuration extractCheckConfiguration(Configuration config, String checkName) {
71-
72-
return Arrays.stream(config.getChildren())
139+
return Optional.of(config)
73140
.filter(child -> checkName.equals(child.getName()))
74-
.findFirst()
75-
.orElseThrow(() -> {
76-
return new IllegalArgumentException(checkName + "configuration not "
77-
+ "found");
78-
});
141+
.orElse(Arrays.stream(config.getChildren())
142+
.filter(child -> {
143+
return "com.puppycrawl.tools.checkstyle.TreeWalker"
144+
.equals(child.getName());
145+
})
146+
.flatMap(treeWalker -> Arrays.stream(treeWalker.getChildren()))
147+
.filter(child -> checkName.equals(child.getName()))
148+
.findFirst()
149+
.orElseThrow(() -> {
150+
return new IllegalArgumentException(checkName
151+
+ " configuration not found");
152+
}));
79153
}
80154

81155
protected Charset getCharset(Configuration config) {

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

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,61 @@
1717

1818
package org.checkstyle.autofix.recipe;
1919

20-
import java.io.IOException;
21-
import java.nio.file.Path;
2220
import java.util.List;
23-
import java.util.Properties;
2421

25-
import org.checkstyle.autofix.parser.CheckstyleReportParser;
2622
import org.checkstyle.autofix.parser.CheckstyleViolation;
2723
import org.junit.jupiter.api.Test;
2824
import org.openrewrite.Recipe;
2925

30-
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
31-
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
32-
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
26+
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
3327
import com.puppycrawl.tools.checkstyle.api.Configuration;
28+
import com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck;
3429

3530
public class HeaderTest extends AbstractRecipeTest {
3631

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

45-
final Configuration config = ConfigurationLoader.loadConfiguration(
46-
configPath, new PropertiesExpander(new Properties())
47-
);
37+
@Override
38+
protected String getCheckName() {
39+
return "Header";
40+
}
4841

49-
final List<CheckstyleViolation> violations =
50-
CheckstyleReportParser.parse(Path.of(reportPath));
42+
@Override
43+
protected Recipe createRecipe(List<CheckstyleViolation> violations, Configuration config) {
5144

52-
return new Header(violations,
53-
extractCheckConfiguration(config, "Header"), getCharset(config));
45+
return new Header(violations, config, getCharset(config));
5446
}
5547

5648
@Test
57-
void headerTest() throws IOException, CheckstyleException {
58-
testRecipe("header", "HeaderBlankLines");
49+
void headerTest() throws Exception {
50+
final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
51+
final String headerPath = "src/test/resources/org/checkstyle/autofix/"
52+
+ "recipe/header/header.txt";
53+
checkConfig.addProperty("headerFile", headerPath);
54+
checkConfig.addProperty("ignoreLines", "3");
55+
verify(checkConfig, "HeaderBlankLines");
5956
}
6057

6158
@Test
62-
void headerCommentTest() throws IOException, CheckstyleException {
63-
testRecipe("header", "HeaderComments");
59+
void headerCommentTest() throws Exception {
60+
final DefaultConfiguration checkConfig = createModuleConfig(HeaderCheck.class);
61+
final String headerPath = "src/test/resources/org/checkstyle/autofix/"
62+
+ "recipe/header/header.txt";
63+
checkConfig.addProperty("headerFile", headerPath);
64+
checkConfig.addProperty("ignoreLines", "3");
65+
verify(checkConfig, "HeaderComments");
6466
}
6567

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

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,44 @@
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

24-
import org.checkstyle.autofix.parser.CheckstyleReportParser;
2522
import org.checkstyle.autofix.parser.CheckstyleViolation;
2623
import org.junit.jupiter.api.Test;
2724
import org.openrewrite.Recipe;
2825

29-
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
26+
import com.puppycrawl.tools.checkstyle.api.Configuration;
3027

3128
public class UpperEllTest extends AbstractRecipeTest {
3229

3330
@Override
34-
protected Recipe getRecipe() {
35-
final String reportPath = "src/test/resources/org/checkstyle/autofix/recipe/upperell"
36-
+ "/report.xml";
31+
protected String getSubpackage() {
32+
return "upperell";
33+
}
34+
35+
@Override
36+
protected String getCheckName() {
37+
return "com.puppycrawl.tools.checkstyle.checks.UpperEllCheck";
38+
}
39+
40+
@Override
41+
protected Recipe createRecipe(List<CheckstyleViolation> violations, Configuration config) {
3742

38-
final List<CheckstyleViolation> violations =
39-
CheckstyleReportParser.parse(Path.of(reportPath));
4043
return new UpperEll(violations);
4144
}
4245

4346
@Test
44-
void hexOctalLiteralTest() throws IOException, CheckstyleException {
45-
testRecipe("upperell", "HexOctalLiteral");
47+
void hexOctalLiteral() throws Exception {
48+
verify("HexOctalLiteral");
4649
}
4750

4851
@Test
49-
void complexLongLiterals() throws IOException, CheckstyleException {
50-
testRecipe("upperell", "ComplexLongLiterals");
52+
void complexLongLiterals() throws Exception {
53+
verify("ComplexLongLiterals");
5154
}
5255

5356
@Test
54-
void stringAndCommentTest() throws IOException, CheckstyleException {
55-
testRecipe("upperell", "StringAndComments");
57+
void stringAndComments() throws Exception {
58+
verify("StringAndComments");
5659
}
5760
}

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

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

0 commit comments

Comments
 (0)