Skip to content

Commit cde1c39

Browse files
amitojduggalAmitoj Duggaltimtebeek
authored
Add Kotlin support for Junit 5 recipes 1: CleanupJUnitImports (#528)
* Adding Kotlin support to the recipes, fixing the recipes to work use preVisit instead of visitCompilationUnit. * Do a single recipe run per unit test * Stop after pre visit, since we're only updating imports --------- Co-authored-by: Amitoj Duggal <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent 65711d5 commit cde1c39

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies {
4343

4444
testImplementation("org.openrewrite:rewrite-java-17")
4545
testImplementation("org.openrewrite:rewrite-groovy")
46+
testImplementation("org.openrewrite:rewrite-kotlin:$rewriteVersion")
4647
testImplementation("org.openrewrite.gradle.tooling:model:$rewriteVersion")
4748

4849
testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")

src/main/java/org/openrewrite/java/testing/junit5/CleanupJUnitImports.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.java.JavaIsoVisitor;
2323
import org.openrewrite.java.search.UsesType;
2424
import org.openrewrite.java.tree.J;
25+
import org.openrewrite.java.tree.JavaSourceFile;
2526

2627
public class CleanupJUnitImports extends Recipe {
2728
@Override
@@ -44,14 +45,18 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4445

4546
public static class CleanupJUnitImportsVisitor extends JavaIsoVisitor<ExecutionContext> {
4647
@Override
47-
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
48-
for (J.Import im : cu.getImports()) {
49-
String packageName = im.getPackageName();
50-
if (packageName.startsWith("junit") || (packageName.startsWith("org.junit") && !packageName.contains("jupiter"))) {
51-
maybeRemoveImport(im.getTypeName());
48+
public J preVisit(J tree, ExecutionContext ctx) {
49+
stopAfterPreVisit();
50+
if (tree instanceof JavaSourceFile) {
51+
JavaSourceFile c = (JavaSourceFile) tree;
52+
for (J.Import imp : c.getImports()) {
53+
String packageName = imp.getPackageName();
54+
if (packageName.startsWith("junit") || (packageName.startsWith("org.junit") && !packageName.contains("jupiter"))) {
55+
maybeRemoveImport(imp.getTypeName());
56+
}
5257
}
5358
}
54-
return cu;
59+
return tree;
5560
}
5661
}
5762
}

src/test/java/org/openrewrite/java/testing/junit5/CleanupJUnitImportsTest.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.InMemoryExecutionContext;
2121
import org.openrewrite.java.JavaParser;
22+
import org.openrewrite.kotlin.KotlinParser;
2223
import org.openrewrite.test.RecipeSpec;
2324
import org.openrewrite.test.RewriteTest;
2425

2526
import static org.openrewrite.java.Assertions.java;
27+
import static org.openrewrite.kotlin.Assertions.kotlin;
2628

2729
class CleanupJUnitImportsTest implements RewriteTest {
2830

@@ -31,14 +33,16 @@ public void defaults(RecipeSpec spec) {
3133
spec
3234
.parser(JavaParser.fromJavaVersion()
3335
.classpathFromResources(new InMemoryExecutionContext(), "junit-4.13"))
36+
.parser(KotlinParser.builder()
37+
.classpathFromResources(new InMemoryExecutionContext(), "junit-4.13"))
3438
.recipe(new CleanupJUnitImports());
3539
}
3640

3741
@DocumentExample
3842
@Test
3943
void removesUnusedImport() {
40-
//language=java
4144
rewriteRun(
45+
//language=java
4246
java(
4347
"""
4448
import org.junit.Test;
@@ -48,14 +52,25 @@ public class MyTest {}
4852
"""
4953
public class MyTest {}
5054
"""
55+
),
56+
//language=kotlin
57+
kotlin(
58+
"""
59+
import org.junit.Test
60+
61+
class MyTest {}
62+
""",
63+
"""
64+
class MyTest {}
65+
"""
5166
)
5267
);
5368
}
5469

5570
@Test
5671
void leavesOtherImportsAlone() {
57-
//language=java
5872
rewriteRun(
73+
//language=java
5974
java(
6075
"""
6176
import java.util.Arrays;
@@ -65,14 +80,25 @@ void leavesOtherImportsAlone() {
6580
public class MyTest {
6681
}
6782
"""
83+
),
84+
//language=kotlin
85+
kotlin(
86+
"""
87+
import java.util.Arrays
88+
import java.util.Collections
89+
import java.util.HashSet
90+
91+
class MyTest {
92+
}
93+
"""
6894
)
6995
);
7096
}
7197

7298
@Test
7399
void leavesUsedJUnitImportAlone() {
74-
//language=java
75100
rewriteRun(
101+
//language=java
76102
java(
77103
"""
78104
import org.junit.Test;
@@ -82,7 +108,19 @@ public class MyTest {
82108
public void foo() {}
83109
}
84110
"""
111+
),
112+
//language=kotlin
113+
kotlin(
114+
"""
115+
import org.junit.Test
116+
117+
class MyTest {
118+
@Test
119+
fun foo() {}
120+
}
121+
"""
85122
)
86123
);
124+
87125
}
88126
}

0 commit comments

Comments
 (0)