Skip to content

Commit a6e8d04

Browse files
committed
Issue #41: Updated AbstractRecipeTest to remove external report
1 parent 4bf149c commit a6e8d04

File tree

12 files changed

+131
-151
lines changed

12 files changed

+131
-151
lines changed

config/import-control-test.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
<allow pkg="java.nio"/>
1313
<allow pkg="java.lang"/>
1414
<allow pkg="javax.xml.stream"/>
15+
<allow pkg="com.puppycrawl.tools.checkstyle"/>
1516
</import-control>

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/UpperEll.java

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

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

171177
private int calculateColumnOffset(String out) {
@@ -175,8 +181,9 @@ private int calculateColumnOffset(String out) {
175181
result = out.length();
176182
}
177183
else {
178-
result = out.length() - lineBreakIndex - 1;
184+
result = out.length() - lineBreakIndex;
179185
}
186+
180187
return result;
181188
}
182189

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

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,91 @@
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.file.Files;
25-
import java.nio.file.Paths;
26+
import java.nio.file.Path;
27+
import java.util.Collections;
28+
import java.util.List;
2629

2730
import org.checkstyle.autofix.InputClassRenamer;
31+
import org.checkstyle.autofix.parser.CheckstyleReportParser;
32+
import org.checkstyle.autofix.parser.CheckstyleViolation;
2833
import org.openrewrite.Recipe;
2934
import org.openrewrite.test.RewriteTest;
3035

31-
public abstract class AbstractRecipeTest implements RewriteTest {
36+
import com.puppycrawl.tools.checkstyle.AbstractXmlTestSupport;
37+
import com.puppycrawl.tools.checkstyle.Checker;
38+
import com.puppycrawl.tools.checkstyle.XMLLogger;
39+
import com.puppycrawl.tools.checkstyle.api.Configuration;
40+
import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser;
41+
import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration;
3242

33-
private static final String BASE_TEST_RESOURCES_PATH = "src/test/resources/org"
34-
+ "/checkstyle/autofix/recipe/";
43+
public abstract class AbstractRecipeTest extends AbstractXmlTestSupport implements RewriteTest {
44+
45+
protected abstract String getSubpackage();
46+
47+
protected abstract Recipe createRecipe(List<CheckstyleViolation> violations);
48+
49+
@Override
50+
protected String getPackageLocation() {
51+
return "org/checkstyle/autofix/recipe/" + getSubpackage();
52+
}
3553

3654
private Recipe createPreprocessingRecipe() {
3755
return new InputClassRenamer();
3856
}
3957

40-
protected abstract Recipe getRecipe();
41-
42-
protected void testRecipe(String recipePath, String testCaseName) throws IOException {
43-
final String testCaseDir = testCaseName.toLowerCase();
58+
protected void verify(String testCaseName) throws Exception {
4459
final String inputFileName = "Input" + testCaseName + ".java";
4560
final String outputFileName = "Output" + testCaseName + ".java";
61+
final String inputPath = testCaseName.toLowerCase() + "/" + inputFileName;
62+
final String outputPath = testCaseName.toLowerCase() + "/" + outputFileName;
63+
64+
final List<CheckstyleViolation> violations = runCheckstyleAndGetViolations(inputPath);
4665

47-
final String beforeCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH
48-
+ recipePath + "/" + testCaseDir + "/" + inputFileName));
66+
final String beforeCode = readFile(getPath(inputPath));
67+
final String expectedAfterCode = readFile(getPath(outputPath));
68+
final Recipe mainRecipe = createRecipe(violations);
4969

50-
final String afterCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH
51-
+ recipePath + "/" + testCaseDir + "/" + outputFileName));
70+
testRecipe(beforeCode, expectedAfterCode,
71+
getPath(inputPath), createPreprocessingRecipe(), mainRecipe);
72+
}
5273

53-
final Recipe preprocessing = createPreprocessingRecipe();
54-
final Recipe mainRecipe = getRecipe();
74+
private List<CheckstyleViolation> runCheckstyleAndGetViolations(String inputPath)
75+
throws Exception {
5576

77+
final String configFilePath = getPath(inputPath);
78+
final TestInputConfiguration testInputConfiguration =
79+
InlineConfigParser.parse(configFilePath);
80+
final Configuration parsedConfig = testInputConfiguration.createConfiguration();
81+
82+
final Checker checker = createChecker(parsedConfig);
83+
final ByteArrayOutputStream xmlOutput = new ByteArrayOutputStream();
84+
final XMLLogger logger = new XMLLogger(xmlOutput, XMLLogger.OutputStreamOptions.CLOSE);
85+
checker.addListener(logger);
86+
87+
final List<File> filesToCheck = Collections.singletonList(new File(configFilePath));
88+
checker.process(filesToCheck);
89+
90+
final Path tempXmlPath = Files.createTempFile("checkstyle-report", ".xml");
91+
try {
92+
Files.write(tempXmlPath, xmlOutput.toByteArray());
93+
return CheckstyleReportParser.parse(tempXmlPath);
94+
}
95+
finally {
96+
Files.deleteIfExists(tempXmlPath);
97+
}
98+
}
99+
100+
private void testRecipe(String beforeCode, String expectedAfterCode,
101+
String filePath, Recipe... recipes) {
56102
assertDoesNotThrow(() -> {
57103
rewriteRun(
58-
spec -> spec.recipes(preprocessing, mainRecipe),
59-
java(beforeCode, afterCode)
104+
spec -> spec.recipes(recipes),
105+
java(beforeCode, expectedAfterCode, spec -> spec.path(filePath))
60106
);
61107
});
62108
}
109+
63110
}

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,37 @@
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

2926
public class UpperEllTest extends AbstractRecipeTest {
3027

3128
@Override
32-
protected Recipe getRecipe() {
33-
final String reportPath = "src/test/resources/org/checkstyle/autofix/recipe/upperell"
34-
+ "/report.xml";
29+
protected String getSubpackage() {
30+
return "upperell";
31+
}
32+
33+
@Override
34+
protected Recipe createRecipe(List<CheckstyleViolation> violations) {
3535

36-
final List<CheckstyleViolation> violations =
37-
CheckstyleReportParser.parse(Path.of(reportPath));
3836
return new UpperEll(violations);
3937
}
4038

4139
@Test
42-
void hexOctalLiteralTest() throws IOException {
43-
testRecipe("upperell", "HexOctalLiteral");
40+
void hexOctalLiteral() throws Exception {
41+
verify("HexOctalLiteral");
4442
}
4543

4644
@Test
47-
void complexLongLiterals() throws IOException {
48-
testRecipe("upperell", "ComplexLongLiterals");
45+
void complexLongLiterals() throws Exception {
46+
verify("ComplexLongLiterals");
4947
}
5048

5149
@Test
52-
void stringAndCommentTest() throws IOException {
53-
testRecipe("upperell", "StringAndComments");
50+
void stringAndComments() throws Exception {
51+
verify("StringAndComments");
5452
}
5553
}

src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/InputComplexLongLiterals.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
com.puppycrawl.tools.checkstyle.checks.UpperEllCheck
3+
4+
*/
5+
16
package org.checkstyle.autofix.recipe.upperell.complexlongliterals;
27

38
public class InputComplexLongLiterals {
@@ -11,8 +16,8 @@ public class InputComplexLongLiterals {
1116
private Long simpleLong = 1234l;
1217
private Long negativeLong = -5678l;
1318
private Long underscoreLong = 1_000_000l;
14-
Long maxLongObject = 9223372036854775807l; //suppressed violation
15-
Long minLongObject = -9223372036854775808l; //suppressed violation
19+
Long maxLongObject = 9223372036854775807l;
20+
Long minLongObject = -9223372036854775808l;
1621

1722
public long calculate(long input1, long input2) {
1823
return input1 + input2 + 1000l;

src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/OutputComplexLongLiterals.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
com.puppycrawl.tools.checkstyle.checks.UpperEllCheck
3+
4+
*/
5+
16
package org.checkstyle.autofix.recipe.upperell.complexlongliterals;
27

38
public class OutputComplexLongLiterals {
@@ -11,8 +16,8 @@ public class OutputComplexLongLiterals {
1116
private Long simpleLong = 1234L;
1217
private Long negativeLong = -5678L;
1318
private Long underscoreLong = 1_000_000L;
14-
Long maxLongObject = 9223372036854775807l; //suppressed violation
15-
Long minLongObject = -9223372036854775808l; //suppressed violation
19+
Long maxLongObject = 9223372036854775807L;
20+
Long minLongObject = -9223372036854775808L;
1621

1722
public long calculate(long input1, long input2) {
1823
return input1 + input2 + 1000L;

src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
com.puppycrawl.tools.checkstyle.checks.UpperEllCheck
3+
4+
*/
5+
16
package org.checkstyle.autofix.recipe.upperell.hexoctalliteral;
27

38
public class InputHexOctalLiteral {
@@ -8,8 +13,8 @@ public class InputHexOctalLiteral {
813
private long decimal = 12345l;
914

1015
public void calculateValues() {
11-
long hexResult = 0xDEADBEEFl + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl
12-
long octalResult = 01234l + 0xDEADBEEFl; //suppressed violation for 0xDEADBEFl
16+
long hexResult = 0xDEADBEEFl + 0xDEADBEFl;
17+
long octalResult = 01234l + 0xDEADBEEFl;
1318
long binaryResult = 0b11110000l;
1419
}
1520
}

src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/OutputHexOctalLiteral.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
com.puppycrawl.tools.checkstyle.checks.UpperEllCheck
3+
4+
*/
5+
16
package org.checkstyle.autofix.recipe.upperell.hexoctalliteral;
27

38
public class OutputHexOctalLiteral {
@@ -8,8 +13,8 @@ public class OutputHexOctalLiteral {
813
private long decimal = 12345L;
914

1015
public void calculateValues() {
11-
long hexResult = 0xDEADBEEFL + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl
12-
long octalResult = 01234L + 0xDEADBEEFl; //suppressed violation for 0xDEADBEFl
16+
long hexResult = 0xDEADBEEFL + 0xDEADBEFL;
17+
long octalResult = 01234L + 0xDEADBEEFL;
1318
long binaryResult = 0b11110000L;
1419
}
1520
}

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

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

0 commit comments

Comments
 (0)