Skip to content

Commit 7438634

Browse files
committed
Implemented Input Files based testing
1 parent 5bece85 commit 7438634

File tree

9 files changed

+235
-24
lines changed

9 files changed

+235
-24
lines changed

config/import-control-test.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
<allow pkg="org.junit"/>
88
<allow pkg="org.openrewrite"/>
99
<allow pkg="org.checkstyle"/>
10-
</import-control>
10+
<allow pkg="java.io"/>
11+
<allow pkg="java.nio"/>
12+
</import-control>
Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.checkstyle.autofix.recipe;
22

3-
import static org.junit.jupiter.api.Assertions.assertTrue;
4-
import static org.openrewrite.java.Assertions.java;
3+
import static org.checkstyle.autofix.util.RecipeTestUtil.testRecipe;
4+
5+
import java.io.IOException;
56

67
import org.junit.jupiter.api.Test;
78
import org.openrewrite.test.RecipeSpec;
@@ -15,26 +16,17 @@ public void defaults(RecipeSpec spec) {
1516
}
1617

1718
@Test
18-
void fixesLowercase() {
19-
rewriteRun(
20-
21-
java(
22-
"class Test {\n"
23-
+ " int value1 = 123l;\n"
24-
+ " long value2 = 0x123l;\n"
25-
+ " long value3 = 0123l;\n"
26-
+ " long value4 = 0b101l;\n"
27-
+ " String value5 = null;\n"
28-
+ "}\n",
29-
"class Test {\n"
30-
+ " int value1 = 123L;\n"
31-
+ " long value2 = 0x123L;\n"
32-
+ " long value3 = 0123L;\n"
33-
+ " long value4 = 0b101L;\n"
34-
+ " String value5 = null;\n"
35-
+ "}\n"
36-
)
37-
);
38-
assertTrue(true, "Test completed successfully");
19+
void hexOctalLiteralTest() throws IOException {
20+
testRecipe(this, "upperell", "HexOctalLiteral");
21+
}
22+
23+
@Test
24+
void complexLongLiterals() throws IOException {
25+
testRecipe(this, "upperell", "ComplexLongLiterals");
26+
}
27+
28+
@Test
29+
void stringAndCommentTest() throws IOException {
30+
testRecipe(this, "upperell", "StringAndComments");
3931
}
4032
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.checkstyle.autofix.util;
2+
3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4+
import static org.openrewrite.java.Assertions.java;
5+
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Paths;
9+
10+
import org.openrewrite.test.RewriteTest;
11+
12+
/**
13+
* Helper class for recipe testing that provides common functionality
14+
* for reading test files and executing recipe tests.
15+
*/
16+
public final class RecipeTestUtil {
17+
18+
private static final String BASE_TEST_RESOURCES_PATH = "src/test/resources/org"
19+
+ "/checkstyle/autofix/recipe/";
20+
21+
/**
22+
* Private constructor to prevent instantiation of utility class.
23+
*/
24+
private RecipeTestUtil() {
25+
// Utility class - no instantiation allowed
26+
}
27+
28+
/**
29+
* Tests a recipe with the given recipe path and test case name.
30+
* Expects input and output files to follow the naming convention:
31+
* - Input: {recipePath}/{testCaseName}/Input{testCaseName}.txt
32+
* - Output: {recipePath}/{testCaseName}/Output{testCaseName}.txt
33+
*
34+
* @param rewriteTest the RewriteTest instance to use for running the test
35+
* @param recipePath the recipe-specific path (e.g., "upperell", "unusedimports", etc.)
36+
* @param testCaseName the name of the test case (should match directory and file names)
37+
* @throws IOException if files cannot be read
38+
*/
39+
public static void testRecipe(RewriteTest rewriteTest, String recipePath, String testCaseName)
40+
throws IOException {
41+
final String testCaseDir = testCaseName.toLowerCase();
42+
final String inputFileName = "Input" + testCaseName + ".txt";
43+
final String outputFileName = "Output" + testCaseName + ".txt";
44+
45+
final String beforeCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH
46+
+ recipePath + "/" + testCaseDir + "/" + inputFileName));
47+
48+
final String afterCode = Files.readString(Paths.get(BASE_TEST_RESOURCES_PATH
49+
+ recipePath + "/" + testCaseDir + "/" + outputFileName));
50+
51+
assertDoesNotThrow(() -> rewriteTest.rewriteRun(java(beforeCode, afterCode)));
52+
}
53+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.checkstyle.autofix.recipe.upperell.complexlongliterals;
2+
3+
public class ComplexLongLiterals {
4+
private long withUnderscores = 1_000_000l;
5+
private long multipleUnderscores = 1_234_567_890l;
6+
7+
private long maxLong = 9223372036854775807l;
8+
private long minLong = -9223372036854775808l;
9+
10+
private Long nullLong = null;
11+
private Long simpleLong = 1234l;
12+
private Long negativeLong = -5678l;
13+
private Long underscoreLong = 1_000_000l;
14+
Long maxLongObject = 9223372036854775807l;
15+
Long minLongObject = -9223372036854775808l;
16+
17+
public long calculate(long input1, long input2) {
18+
return input1 + input2 + 1000l;
19+
}
20+
21+
public void lambdaUsage() {
22+
java.util.function.LongSupplier supplier = () -> 42l;
23+
java.util.Arrays.asList(1l, 2l, 3l).forEach(System.out::println);
24+
}
25+
26+
public java.util.List<Long> getNumbers() {
27+
return java.util.Arrays.asList(100l, 200l, 300l);
28+
}
29+
30+
public void longObjectOperations() {
31+
Long a = null;
32+
Long b = 1234l;
33+
Long c = Long.valueOf(5678l);
34+
Long d = new Long(9999l);
35+
36+
Long conditional = (a != null) ? a : 0l;
37+
long primitive = b != null ? b : 0l;
38+
Long boxed = primitive + 1000l;
39+
}
40+
41+
public Long methodReturningLong(Long param) {
42+
if (param == null) {
43+
return 12345l;
44+
}
45+
return param + 6789l;
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.checkstyle.autofix.recipe.upperell.complexlongliterals;
2+
3+
public class ComplexLongLiterals {
4+
private long withUnderscores = 1_000_000L;
5+
private long multipleUnderscores = 1_234_567_890L;
6+
7+
private long maxLong = 9223372036854775807L;
8+
private long minLong = -9223372036854775808L;
9+
10+
private Long nullLong = null;
11+
private Long simpleLong = 1234L;
12+
private Long negativeLong = -5678L;
13+
private Long underscoreLong = 1_000_000L;
14+
Long maxLongObject = 9223372036854775807L;
15+
Long minLongObject = -9223372036854775808L;
16+
17+
public long calculate(long input1, long input2) {
18+
return input1 + input2 + 1000L;
19+
}
20+
21+
public void lambdaUsage() {
22+
java.util.function.LongSupplier supplier = () -> 42L;
23+
java.util.Arrays.asList(1L, 2L, 3L).forEach(System.out::println);
24+
}
25+
26+
public java.util.List<Long> getNumbers() {
27+
return java.util.Arrays.asList(100L, 200L, 300L);
28+
}
29+
30+
public void longObjectOperations() {
31+
Long a = null;
32+
Long b = 1234L;
33+
Long c = Long.valueOf(5678L);
34+
Long d = new Long(9999L);
35+
36+
Long conditional = (a != null) ? a : 0L;
37+
long primitive = b != null ? b : 0L;
38+
Long boxed = primitive + 1000L;
39+
}
40+
41+
public Long methodReturningLong(Long param) {
42+
if (param == null) {
43+
return 12345L;
44+
}
45+
return param + 6789L;
46+
}
47+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.checkstyle.autofix.recipe.upperell.hexoctalliteral;
2+
3+
public class HexOctalLiteral {
4+
private long hexLower = 0x1ABCl;
5+
private long hexUpper = 0X2DEFl;
6+
private long octal = 0777l;
7+
private long binary = 0b1010l;
8+
private long decimal = 12345l;
9+
10+
public void calculateValues() {
11+
long hexResult = 0xDEADBEEFl;
12+
long octalResult = 01234l;
13+
long binaryResult = 0b11110000l;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.checkstyle.autofix.recipe.upperell.hexoctalliteral;
2+
3+
public class HexOctalLiteral {
4+
private long hexLower = 0x1ABCL;
5+
private long hexUpper = 0X2DEFL;
6+
private long octal = 0777L;
7+
private long binary = 0b1010L;
8+
private long decimal = 12345L;
9+
10+
public void calculateValues() {
11+
long hexResult = 0xDEADBEEFL;
12+
long octalResult = 01234L;
13+
long binaryResult = 0b11110000L;
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.checkstyle.autofix.recipe.upperell.stringandcomments;
2+
3+
public class StringAndComments {
4+
/**
5+
* This comment mentions 123l but should not change
6+
*/
7+
private String message = "The value 456l should not change in strings";
8+
private String code = "long value = 789l;"; // This 789l in string should not change
9+
10+
// Only this actual long literal should change
11+
private long actualLong = 999l;
12+
13+
/*
14+
* Multi-line comment with 111l should not change
15+
*/
16+
public void method() {
17+
// Single line comment with 222l should not change
18+
long value = 333l; // This should change
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.checkstyle.autofix.recipe.upperell.stringandcomments;
2+
3+
public class StringAndComments {
4+
/**
5+
* This comment mentions 123l but should not change
6+
*/
7+
private String message = "The value 456l should not change in strings";
8+
private String code = "long value = 789l;"; // This 789l in string should not change
9+
10+
// Only this actual long literal should change
11+
private long actualLong = 999L;
12+
13+
/*
14+
* Multi-line comment with 111l should not change
15+
*/
16+
public void method() {
17+
// Single line comment with 222l should not change
18+
long value = 333L; // This should change
19+
}
20+
}

0 commit comments

Comments
 (0)