Skip to content

Commit 941d017

Browse files
Adding TestNgGuard scanning recipe. Needs refinement, but this is a first pass
1 parent 0992577 commit 941d017

File tree

3 files changed

+577
-367
lines changed

3 files changed

+577
-367
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package org.openrewrite.java.testing.junit5;
2+
3+
import lombok.Value;
4+
import org.jspecify.annotations.Nullable;
5+
import org.openrewrite.*;
6+
import org.openrewrite.java.dependencies.search.DoesNotIncludeDependency;
7+
import org.openrewrite.java.marker.JavaProject;
8+
import org.openrewrite.java.search.DoesNotUseType;
9+
import org.openrewrite.java.search.UsesType;
10+
import org.openrewrite.marker.SearchResult;
11+
12+
import java.util.HashSet;
13+
import java.util.Optional;
14+
import java.util.Set;
15+
16+
public class TestNgGuard extends ScanningRecipe<TestNgGuard.Accumulator> {
17+
@Override
18+
public String getDisplayName() {
19+
return "JUnit Jupiter migration from JUnit 4.x";
20+
}
21+
22+
@Override
23+
public String getDescription() {
24+
return "Migrates JUnit 4.x tests to JUnit Jupiter.";
25+
}
26+
27+
@Value
28+
public static class Accumulator {
29+
Set<JavaProject> projectsWithoutTestNgDependency;
30+
Set<JavaProject> projectsWithoutTestNgTypeUsage;
31+
Set<JavaProject> projectsWithTestNgDependency;
32+
Set<JavaProject> projectsWithTestNgTypeUsage;
33+
}
34+
35+
@Override
36+
public Accumulator getInitialValue(ExecutionContext ctx) {
37+
return new Accumulator(new HashSet<>(), new HashSet<>(), new HashSet<>(), new HashSet<>());
38+
}
39+
40+
@Override
41+
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
42+
return new TreeVisitor<Tree, ExecutionContext>() {
43+
private final TreeVisitor<?, ExecutionContext> dnut = new DoesNotUseType("org.testng..*", true).getVisitor();
44+
private final TreeVisitor<?, ExecutionContext> ut = new UsesType<>("org.testng..*", true);
45+
private final TreeVisitor<?, ExecutionContext> dnid = new DoesNotIncludeDependency("org.testng", "testng*", null, null, null).getVisitor();
46+
47+
@Override
48+
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
49+
assert tree != null;
50+
if (!(tree instanceof SourceFile)) {
51+
return tree;
52+
}
53+
SourceFile s = (SourceFile) tree;
54+
if (dnid.isAcceptable(s, ctx)) {
55+
Tree after = dnid.visit(tree, ctx);
56+
if (after != tree) {
57+
tree
58+
.getMarkers()
59+
.findFirst(JavaProject.class)
60+
.ifPresent(acc.projectsWithoutTestNgDependency::add);
61+
} else {
62+
tree
63+
.getMarkers()
64+
.findFirst(JavaProject.class)
65+
.ifPresent(acc.projectsWithTestNgDependency::add);
66+
}
67+
} else if (ut.isAcceptable(s, ctx)) {
68+
Tree after = ut.visit(tree, ctx);
69+
if (after == tree) {
70+
tree
71+
.getMarkers()
72+
.findFirst(JavaProject.class)
73+
.ifPresent(acc.projectsWithoutTestNgTypeUsage::add);
74+
} else {
75+
tree
76+
.getMarkers()
77+
.findFirst(JavaProject.class)
78+
.ifPresent(acc.projectsWithTestNgTypeUsage::add);
79+
}
80+
}
81+
return tree;
82+
}
83+
};
84+
}
85+
86+
@Override
87+
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
88+
return new TreeVisitor<Tree, ExecutionContext>() {
89+
private final TreeVisitor<?, ExecutionContext> ut = new UsesType<>("org.testng..*", true);
90+
private final TreeVisitor<?, ExecutionContext> dnid = new DoesNotIncludeDependency("org.testng", "testng*", null, null, null).getVisitor();
91+
92+
@Override
93+
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
94+
return ut.isAcceptable(sourceFile, ctx) || dnid.isAcceptable(sourceFile, ctx);
95+
}
96+
97+
@Override
98+
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
99+
assert tree != null;
100+
Optional<JavaProject> maybeJp = tree.getMarkers().findFirst(JavaProject.class);
101+
if (!maybeJp.isPresent()) {
102+
return tree;
103+
}
104+
JavaProject jp = maybeJp.get();
105+
boolean pwoTngd = acc.getProjectsWithoutTestNgDependency().contains(jp);
106+
boolean pwoTngtu = acc.getProjectsWithoutTestNgTypeUsage().contains(jp);
107+
boolean pwTngd = acc.getProjectsWithTestNgDependency().contains(jp);
108+
boolean pwTngtu = acc.getProjectsWithTestNgTypeUsage().contains(jp);
109+
if (pwTngtu || pwTngd) {
110+
return tree;
111+
}
112+
if (!pwoTngd && !pwoTngtu) {
113+
return tree;
114+
}
115+
if (
116+
(pwoTngd || acc.getProjectsWithoutTestNgDependency().isEmpty())
117+
&& (pwoTngtu || acc.getProjectsWithoutTestNgTypeUsage().isEmpty())
118+
) {
119+
return SearchResult.found(tree);
120+
}
121+
return tree;
122+
}
123+
};
124+
}
125+
126+
// @Override
127+
// public TreeVisitor<?, ExecutionContext> getVisitor() {
128+
// return new TreeVisitor<Tree, ExecutionContext>() {
129+
// private final TreeVisitor<?, ExecutionContext> dnut = new DoesNotUseType("org.testng..*", true).getVisitor();
130+
// private final TreeVisitor<?, ExecutionContext> dnid = new DoesNotIncludeDependency("org.testng", "testng*", null, null, null).getVisitor();
131+
//
132+
// @Override
133+
// public boolean isAcceptable(SourceFile sourceFile, ExecutionContext executionContext) {
134+
// return dnut.isAcceptable(sourceFile, executionContext) || dnid.isAcceptable(sourceFile, executionContext);
135+
// }
136+
//
137+
// @Override
138+
// public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
139+
// if (!(tree instanceof SourceFile)) {
140+
// return tree;
141+
// }
142+
// SourceFile s = (SourceFile) tree;
143+
// SourceFile after = s;
144+
// if (dnid.isAcceptable(s, ctx)) {
145+
// after = (SourceFile) dnid.visitNonNull(s, ctx);
146+
// } else if (dnut.isAcceptable(s, ctx)) {
147+
// after = (SourceFile) dnut.visitNonNull(s, ctx);
148+
// }
149+
// if (after == s) {
150+
// return s;
151+
// }
152+
// return SearchResult.found(after);
153+
// }
154+
// };
155+
// }
156+
}

src/main/resources/META-INF/rewrite/junit5.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ tags:
5858
- testing
5959
- junit
6060
preconditions:
61-
- org.openrewrite.java.search.DoesNotUseType:
62-
fullyQualifiedTypeName: org.testng..*
63-
includeImplicit: true
64-
- org.openrewrite.java.dependencies.search.DoesNotIncludeDependency:
65-
groupId: org.testng
66-
artifactId: testng*
61+
- org.openrewrite.java.testing.junit5.TestNgGuard
6762
recipeList:
6863
- org.openrewrite.java.testing.junit5.EnvironmentVariables
6964
- org.openrewrite.java.testing.junit5.UseWiremockExtension

0 commit comments

Comments
 (0)