Skip to content

Commit 330aca7

Browse files
Greg Adamspway99
andauthored
Java template uses depends on (#41)
* annotations to leadingAnnotation changes * JavaTemplate update to use dependsOn vs classpath * JavaTemplate update to use dependsOn vs classpath * deleting unused file * MockitoRunnerToMockitoExtension update * wip Co-authored-by: Patrick <[email protected]>
1 parent cb26765 commit 330aca7

22 files changed

+3206
-34
lines changed

build.gradle.kts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,9 @@ dependencies {
8181
testImplementation("org.jetbrains.kotlin:kotlin-reflect")
8282

8383
testRuntimeOnly("junit:junit:latest.release")
84-
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:latest.release")
85-
testRuntimeOnly("org.springframework:spring-test:latest.release")
84+
testRuntimeOnly("org.springframework:spring-test:4.+")
8685
testRuntimeOnly("ch.qos.logback:logback-classic:1.0.13")
8786
testRuntimeOnly("org.mockito:mockito-all:$mockito1Version")
88-
testRuntimeOnly("org.assertj:assertj-core:$assertJVersion")
89-
testRuntimeOnly("org.mockito:mockito-junit-jupiter:latest.release")
9087

9188
"beforeImplementation"("junit:junit:latest.release")
9289
"beforeImplementation"("org.mockito:mockito-all:$mockito1Version")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected TreeVisitor<?, ExecutionContext> getVisitor() {
3939
private class AddJUnitDependenciesVisitor extends MavenVisitor {
4040
@Override
4141
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
42-
if (Boolean.TRUE.equals(ctx.pollMessage(FindJUnit.JUNIT_REFS_EXIST_KEY))) {
42+
if (Boolean.TRUE.equals(ctx.pollMessage(FindJUnit5.JUNIT_REFS_EXIST_KEY))) {
4343
maybeAddDependency(
4444
"org.junit.jupiter",
4545
"junit-jupiter-api",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
5050
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
5151
Set<J.Annotation> categoryAnnotations = FindAnnotations.find(cd, "@" + categoryAnnotation);
5252
if (!categoryAnnotations.isEmpty()) {
53-
cd = cd.withAnnotations(cd.getAnnotations().stream()
53+
cd = cd.withLeadingAnnotations(cd.getLeadingAnnotations().stream()
5454
.flatMap(this::categoryAnnotationToTagAnnotations)
5555
.collect(Collectors.toList()));
5656
maybeRemoveImport(categoryAnnotation);
@@ -65,7 +65,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
6565
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
6666
Set<J.Annotation> categoryAnnotations = FindAnnotations.find(m, "@" + categoryAnnotation);
6767
if (!categoryAnnotations.isEmpty()) {
68-
m = m.withAnnotations(m.getAnnotations().stream()
68+
m = m.withLeadingAnnotations(m.getLeadingAnnotations().stream()
6969
.flatMap(this::categoryAnnotationToTagAnnotations)
7070
.collect(Collectors.toList()));
7171

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
package org.openrewrite.java.testing.junit5;
1717

1818
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Parser;
1920
import org.openrewrite.Recipe;
2021
import org.openrewrite.TreeVisitor;
2122
import org.openrewrite.java.AddImport;
2223
import org.openrewrite.java.JavaIsoVisitor;
24+
import org.openrewrite.java.JavaParser;
2325
import org.openrewrite.java.format.AutoFormatVisitor;
2426
import org.openrewrite.java.search.FindFields;
2527
import org.openrewrite.java.tree.*;
2628

27-
import java.util.ArrayList;
28-
import java.util.List;
29-
import java.util.Set;
29+
import java.util.*;
3030

3131
/**
3232
* Replace usages of JUnit 4's @Rule ExpectedException with JUnit 5 Assertions.assertThrows
@@ -88,6 +88,21 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
8888
m = m.withBody(
8989
m.getBody().withTemplate(
9090
template("{ assertThrows(#{}, () -> { #{} }); }")
91+
.javaParser(JavaParser.fromJavaVersion().dependsOn(Arrays.asList(
92+
Parser.Input.fromString("" +
93+
"package org.junit.jupiter.api;" +
94+
"import java.util.function.Supplier;" +
95+
"import org.junit.jupiter.api.function.Executable;" +
96+
"class AssertThrows {\n" +
97+
"static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable,Supplier<String> messageSupplier){}" +
98+
"}"),
99+
Parser.Input.fromString(
100+
"package org.junit.jupiter.api.function;" +
101+
"public interface Executable {\n" +
102+
"void execute() throws Throwable;\n" +
103+
"}"
104+
)
105+
)).build())
91106
.staticImports("org.junit.jupiter.api.Assertions.assertThrows")
92107
.build(),
93108
m.getBody().getCoordinates().replace(),
@@ -97,7 +112,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
97112
maybeRemoveImport("org.junit.Rule");
98113
maybeRemoveImport("org.junit.rules.ExpectedException");
99114

100-
doAfterVisit(new AddImport<>("org.junit.jupiter.api.Assertions", "assertThrows", false));
115+
maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows");
101116

102117
m = m.withBody((J.Block) new AutoFormatVisitor<ExecutionContext>().visit(m.getBody(), ctx, getCursor()));
103118
}

src/main/java/org/openrewrite/java/testing/junit5/FindJUnit.java renamed to src/main/java/org/openrewrite/java/testing/junit5/FindJUnit5.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.openrewrite.java.search.FindTypes;
2323
import org.openrewrite.java.tree.J;
2424

25-
public class FindJUnit extends Recipe {
25+
public class FindJUnit5 extends Recipe {
2626

2727
public static final String JUNIT_REFS_EXIST_KEY = "junitReferencesExist";
2828

@@ -35,8 +35,7 @@ private static class FindJUnitVisitor extends JavaVisitor<ExecutionContext> {
3535
@Override
3636
public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
3737
boolean junitReferencesExist =
38-
!FindTypes.find(cu, "junit.framework.Test").isEmpty()
39-
|| !FindTypes.find(cu, "org.junit.jupiter.api.Test").isEmpty();
38+
!FindTypes.find(cu, "org.junit.jupiter.api.Test").isEmpty();
4039
ctx.putMessage(JUNIT_REFS_EXIST_KEY, junitReferencesExist);
4140
return cu;
4241
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
package org.openrewrite.java.testing.junit5;
1717

1818
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Parser;
1920
import org.openrewrite.Recipe;
2021
import org.openrewrite.TreeVisitor;
2122
import org.openrewrite.java.AddImport;
23+
import org.openrewrite.java.AnnotationMatcher;
2224
import org.openrewrite.java.JavaIsoVisitor;
25+
import org.openrewrite.java.JavaParser;
2326
import org.openrewrite.java.search.SemanticallyEqual;
2427
import org.openrewrite.java.tree.*;
2528
import org.openrewrite.marker.Markers;
@@ -96,10 +99,10 @@ private static class AnnotationUpdateVisitor extends JavaIsoVisitor<ExecutionCon
9699
@Override
97100
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext ctx) {
98101

99-
List<J.Annotation> keepAnnotations = cd.getAnnotations().stream().filter(
102+
List<J.Annotation> keepAnnotations = cd.getLeadingAnnotations().stream().filter(
100103
a -> !shouldReplaceAnnotation(a)
101104
).collect(Collectors.toList());
102-
if (keepAnnotations.size() != cd.getAnnotations().size()) {
105+
if (keepAnnotations.size() != cd.getLeadingAnnotations().size()) {
103106
ctx.putMessage(MOCKITO_ANNOTATION_REPLACED_KEY, true);
104107
maybeAddImport("org.junit.jupiter.api.extension.ExtendWith");
105108
AddImport<ExecutionContext> op = new AddImport<>("org.mockito.junit.jupiter.MockitoExtension", null, false);
@@ -108,10 +111,21 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, Execution
108111
}
109112
Stream.of(mockitoRunnerAnnotationClassNames).forEach(this::maybeRemoveImport);
110113
maybeRemoveImport(FrameworkTypes.runWithType);
111-
cd = cd.withAnnotations(keepAnnotations);
114+
cd = cd.withLeadingAnnotations(keepAnnotations);
112115
cd = cd.withTemplate(
113116
template("@ExtendWith(MockitoExtension.class)")
114117
.imports("org.junit.jupiter.api.extension.ExtendWith", "org.mockito.junit.jupiter.MockitoExtension")
118+
.javaParser( JavaParser.fromJavaVersion().dependsOn(Arrays.asList(
119+
Parser.Input.fromString(
120+
"package org.junit.jupiter.api.extension;\n" +
121+
"@Target({ ElementType.TYPE, ElementType.METHOD })\n" +
122+
"public @interface ExtendWith {\n" +
123+
"Class<? extends Extension>[] value();\n" +
124+
"}"),
125+
Parser.Input.fromString(
126+
"package org.mockito.junit.jupiter;\n" +
127+
"public class MockitoExtension {}"
128+
))).build())
115129
.build(),
116130
cd.getCoordinates().addAnnotation(
117131
// TODO should this use some configuration (similar to styles) for annotation ordering?

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
package org.openrewrite.java.testing.junit5;
1717

1818
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Parser;
1920
import org.openrewrite.Recipe;
2021
import org.openrewrite.TreeVisitor;
2122
import org.openrewrite.java.JavaIsoVisitor;
23+
import org.openrewrite.java.JavaParser;
2224
import org.openrewrite.java.search.SemanticallyEqual;
2325
import org.openrewrite.java.tree.*;
2426
import org.openrewrite.marker.Markers;
2527

28+
import java.util.Arrays;
2629
import java.util.Collections;
2730
import java.util.Comparator;
2831
import java.util.List;
@@ -109,19 +112,30 @@ public static class SpringRunnerToSpringExtensionVisitor extends JavaIsoVisitor<
109112

110113
@Override
111114
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext ctx) {
112-
List<J.Annotation> keepAnnotations = cd.getAnnotations().stream().filter(
115+
List<J.Annotation> keepAnnotations = cd.getLeadingAnnotations().stream().filter(
113116
a -> !shouldReplaceAnnotation(a)
114117
).collect(Collectors.toList());
115-
if (keepAnnotations.size() != cd.getAnnotations().size()) {
118+
if (keepAnnotations.size() != cd.getLeadingAnnotations().size()) {
116119
maybeAddImport(extendWithType);
117120
maybeAddImport(springExtensionType);
118121
maybeRemoveImport(springRunnerType);
119122
maybeRemoveImport(springJUnit4ClassRunnerType);
120123
maybeRemoveImport(runWithType);
121-
cd = cd.withAnnotations(keepAnnotations);
124+
cd = cd.withLeadingAnnotations(keepAnnotations);
122125
cd = cd.withTemplate(
123126
template("@ExtendWith(SpringExtension.class)")
124127
.imports("org.junit.jupiter.api.extension.ExtendWith", springExtensionType)
128+
.javaParser( JavaParser.fromJavaVersion().dependsOn(Arrays.asList(
129+
Parser.Input.fromString(
130+
"package org.junit.jupiter.api.extension;\n" +
131+
"@Target({ ElementType.TYPE, ElementType.METHOD })\n" +
132+
"public @interface ExtendWith {\n" +
133+
"Class<? extends Extension>[] value();\n" +
134+
"}"),
135+
Parser.Input.fromString(
136+
"package org.springframework.test.context.junit.jupiter;\n" +
137+
"public class SpringExtension {}"
138+
))).build())
125139
.build(),
126140
cd.getCoordinates().addAnnotation(
127141
// TODO should this use some configuration (similar to styles) for annotation ordering?

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
package org.openrewrite.java.testing.junit5;
1717

1818
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Parser;
1920
import org.openrewrite.Recipe;
2021
import org.openrewrite.TreeVisitor;
2122
import org.openrewrite.internal.ListUtils;
2223
import org.openrewrite.java.JavaIsoVisitor;
24+
import org.openrewrite.java.JavaParser;
2325
import org.openrewrite.java.JavaVisitor;
2426
import org.openrewrite.java.tree.*;
2527

28+
import java.util.Collections;
2629
import java.util.HashSet;
2730
import java.util.List;
2831
import java.util.Set;
@@ -85,6 +88,11 @@ public J visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ct
8588
field = field.withTemplate(
8689
template("@TempDir\nFile#{};")
8790
.imports("java.io.File", "org.junit.jupiter.api.io.TempDir")
91+
.javaParser(JavaParser.fromJavaVersion().dependsOn(Collections.singletonList(
92+
Parser.Input.fromString("" +
93+
"package org.junit.jupiter.api.io;\n" +
94+
"public @interface TempDir {}")
95+
)).build())
8896
.build(),
8997
field.getCoordinates().replace(), fieldVars);
9098
tempDirFields.add(field);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon
6565
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
6666
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
6767

68-
List<J.Annotation> annotations = new ArrayList<>(m.getAnnotations());
68+
List<J.Annotation> annotations = new ArrayList<>(m.getLeadingAnnotations());
6969
for (J.Annotation a : annotations) {
7070

7171
if (TypeUtils.isOfClassType(a.getType(), "org.junit.Before") ||

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.testing.junit5;
1717

1818
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Parser;
1920
import org.openrewrite.Recipe;
2021
import org.openrewrite.TreeVisitor;
2122
import org.openrewrite.internal.ListUtils;
@@ -26,9 +27,7 @@
2627
import org.openrewrite.java.tree.Statement;
2728
import org.openrewrite.java.tree.TypeUtils;
2829

29-
import java.util.ArrayList;
30-
import java.util.Comparator;
31-
import java.util.List;
30+
import java.util.*;
3231
import java.util.stream.Collectors;
3332

3433
public class UpdateTestAnnotation extends Recipe {
@@ -51,7 +50,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
5150
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
5251

5352
boolean changed = false;
54-
List<J.Annotation> annotations = new ArrayList<>(m.getAnnotations());
53+
List<J.Annotation> annotations = new ArrayList<>(m.getLeadingAnnotations());
5554
for (int i = 0, annotationsSize = annotations.size(); i < annotationsSize; i++) {
5655

5756
J.Annotation a = annotations.get(i);
@@ -83,6 +82,25 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
8382
.collect(Collectors.joining(";")) + ";";
8483
m = m.withTemplate(
8584
template("{ assertThrows(#{}, () -> {#{}}); }")
85+
.javaParser(
86+
(JavaParser) JavaParser.fromJavaVersion().dependsOn(Arrays.asList(
87+
Parser.Input.fromString(
88+
"package org.junit.jupiter.api.function;\n" +
89+
"public interface Executable {\n" +
90+
" void execute() throws Throwable;\n" +
91+
"}"),
92+
Parser.Input.fromString(
93+
"package org.junit.jupiter.api;\n" +
94+
"import org.junit.jupiter.api.function.Executable;\n" +
95+
"public class Assertions {\n" +
96+
" public static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable) {\n" +
97+
" return null;\n" +
98+
" }\n" +
99+
"}")
100+
)
101+
)
102+
.build()
103+
)
86104
.staticImports("org.junit.jupiter.api.Assertions.assertThrows")
87105
.build(),
88106
m.getCoordinates().replaceBody(),
@@ -100,7 +118,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
100118
}
101119

102120
if (changed) {
103-
m = m.withAnnotations(annotations);
121+
m = m.withLeadingAnnotations(annotations);
104122
}
105123
return m;
106124
}

0 commit comments

Comments
 (0)