Skip to content

Commit 8720326

Browse files
authored
Only migrate the two-argument InvocationInterceptor.interceptDynamicTest in RemoveInterceptDynamicTest (#1221)
* Only migrate the two-argument `InvocationInterceptor.interceptDynamicTest` in `RemoveInterceptDynamicTest` The recipe matched `interceptDynamicTest(..)`, so it also deleted overrides of the three-argument `interceptDynamicTest(Invocation, DynamicTestInvocationContext, ExtensionContext)` that JUnit 6 still has, silently dropping user interceptor logic. It now only matches the removed two-argument signature, and rather than deleting that override it adds a `DynamicTestInvocationContext` parameter so the body keeps running under JUnit 6, as it did through the JUnit 5 default delegation. The two-argument override is still removed when the class already overrides the three-argument one, or for non-Java sources. * Update `recipes.csv` for the new `RemoveInterceptDynamicTest` display name
1 parent f920ae1 commit 8720326

3 files changed

Lines changed: 141 additions & 15 deletions

File tree

‎src/main/java/org/openrewrite/java/testing/junit6/RemoveInterceptDynamicTest.java‎

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,62 @@
2121
import org.openrewrite.Preconditions;
2222
import org.openrewrite.Recipe;
2323
import org.openrewrite.TreeVisitor;
24+
import org.openrewrite.internal.ListUtils;
2425
import org.openrewrite.java.JavaIsoVisitor;
26+
import org.openrewrite.java.JavaParser;
27+
import org.openrewrite.java.JavaTemplate;
2528
import org.openrewrite.java.MethodMatcher;
2629
import org.openrewrite.java.search.DeclaresMethod;
2730
import org.openrewrite.java.tree.J;
31+
import org.openrewrite.java.tree.JavaType;
32+
import org.openrewrite.java.tree.Statement;
33+
34+
import static org.openrewrite.java.VariableNameUtils.GenerationStrategy.INCREMENT_NUMBER;
35+
import static org.openrewrite.java.VariableNameUtils.generateVariableName;
2836

2937
public class RemoveInterceptDynamicTest extends Recipe {
3038

31-
private static final MethodMatcher INTERCEPT_DYNAMIC_TEST_MATCHER = new MethodMatcher(
32-
"org.junit.jupiter.api.extension.InvocationInterceptor interceptDynamicTest(..)", true);
39+
private static final String DYNAMIC_TEST_INVOCATION_CONTEXT = "org.junit.jupiter.api.extension.DynamicTestInvocationContext";
40+
private static final MethodMatcher TWO_ARGUMENT_INTERCEPT_DYNAMIC_TEST = new MethodMatcher("org.junit.jupiter.api.extension.InvocationInterceptor interceptDynamicTest(org.junit.jupiter.api.extension.InvocationInterceptor$Invocation, org.junit.jupiter.api.extension.ExtensionContext)", true);
41+
private static final MethodMatcher THREE_ARGUMENT_INTERCEPT_DYNAMIC_TEST = new MethodMatcher("org.junit.jupiter.api.extension.InvocationInterceptor interceptDynamicTest(org.junit.jupiter.api.extension.InvocationInterceptor$Invocation, " + DYNAMIC_TEST_INVOCATION_CONTEXT + ", org.junit.jupiter.api.extension.ExtensionContext)", true);
3342

3443
@Getter
35-
final String displayName = "Remove `InvocationInterceptor.interceptDynamicTest`";
44+
final String displayName = "Migrate the two-argument `InvocationInterceptor.interceptDynamicTest`";
3645

3746
@Getter
38-
final String description = "JUnit 6 removed the `interceptDynamicTest(Invocation, ExtensionContext)` method from " +
39-
"`InvocationInterceptor`. This recipe removes implementations of this deprecated method.";
47+
final String description = "JUnit 6 removed the deprecated `InvocationInterceptor#interceptDynamicTest(Invocation, ExtensionContext)`. " +
48+
"Java overrides gain a `DynamicTestInvocationContext` parameter, so they keep their body and override the remaining three-argument overload. " +
49+
"Overrides in classes that already override the three-argument overload, and non-Java overrides, are removed.";
4050

4151
@Override
4252
public TreeVisitor<?, ExecutionContext> getVisitor() {
43-
return Preconditions.check(new DeclaresMethod<>(INTERCEPT_DYNAMIC_TEST_MATCHER), new JavaIsoVisitor<ExecutionContext>() {
53+
return Preconditions.check(new DeclaresMethod<>(TWO_ARGUMENT_INTERCEPT_DYNAMIC_TEST), new JavaIsoVisitor<ExecutionContext>() {
4454
@Override
4555
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
4656
J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx);
47-
if (INTERCEPT_DYNAMIC_TEST_MATCHER.matches(md.getMethodType())) {
57+
JavaType.Method methodType = md.getMethodType();
58+
if (methodType == null || !TWO_ARGUMENT_INTERCEPT_DYNAMIC_TEST.matches(methodType)) {
59+
return md;
60+
}
61+
if (getCursor().firstEnclosing(J.CompilationUnit.class) == null ||
62+
methodType.getDeclaringType().getMethods().stream().anyMatch(THREE_ARGUMENT_INTERCEPT_DYNAMIC_TEST::matches)) {
4863
return null;
4964
}
50-
return md;
65+
66+
String parameterName = generateVariableName("invocationContext", getCursor(), INCREMENT_NUMBER);
67+
J.MethodDeclaration withContext = JavaTemplate.builder("DynamicTestInvocationContext " + parameterName)
68+
.imports(DYNAMIC_TEST_INVOCATION_CONTEXT)
69+
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-6"))
70+
.build()
71+
.apply(getCursor(), md.getCoordinates().replaceParameters());
72+
Statement contextParameter = withContext.getParameters().get(0).withPrefix(md.getParameters().get(1).getPrefix());
73+
JavaType contextType = withContext.getMethodType().getParameterTypes().get(0);
74+
maybeAddImport(DYNAMIC_TEST_INVOCATION_CONTEXT);
75+
return md
76+
.withParameters(ListUtils.insert(md.getParameters(), contextParameter, 1))
77+
.withMethodType(methodType
78+
.withParameterNames(ListUtils.insert(methodType.getParameterNames(), parameterName, 1))
79+
.withParameterTypes(ListUtils.insert(methodType.getParameterTypes(), contextType, 1)));
5180
}
5281
});
5382
}

‎src/main/resources/META-INF/rewrite/recipes.csv‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.tes
208208
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit6.MinimumJreConditions,Migrate JUnit JRE conditions,"This recipe will:
209209
- Remove tests that are only active on JREs that are below the specified version.
210210
- Adjust ranges to use minimum the specified version.",1,JUnit 6,Testing,Java,Recipes for migrating to [JUnit 6](https://junit.org/).,,Basic building blocks for transforming Java code.,"[{""name"":""javaVersion"",""type"":""String"",""displayName"":""JRE version"",""description"":""The minimum JRE version to use for test conditions."",""example"":""17"",""required"":true}]",
211-
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit6.RemoveInterceptDynamicTest,Remove `InvocationInterceptor.interceptDynamicTest`,"JUnit 6 removed the `interceptDynamicTest(Invocation, ExtensionContext)` method from `InvocationInterceptor`. This recipe removes implementations of this deprecated method.",1,JUnit 6,Testing,Java,Recipes for migrating to [JUnit 6](https://junit.org/).,,Basic building blocks for transforming Java code.,,
211+
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit6.RemoveInterceptDynamicTest,Migrate the two-argument `InvocationInterceptor.interceptDynamicTest`,"JUnit 6 removed the deprecated `InvocationInterceptor#interceptDynamicTest(Invocation, ExtensionContext)`. Java overrides gain a `DynamicTestInvocationContext` parameter, so they keep their body and override the remaining three-argument overload. Overrides in classes that already override the three-argument overload, and non-Java overrides, are removed.",1,JUnit 6,Testing,Java,Recipes for migrating to [JUnit 6](https://junit.org/).,,Basic building blocks for transforming Java code.,,
212212
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.junit6.RemoveJreOther,Remove deprecated `JRE.OTHER` from `@EnabledOnJre`/`@DisabledOnJre` arrays,JUnit 6.1 deprecated `JRE.OTHER` in favor of `int`/`int[]` annotation attributes. This recipe removes `JRE.OTHER` entries from `@EnabledOnJre` and `@DisabledOnJre` array values when other JRE constants remain. Lone `JRE.OTHER` usages are left untouched because they have no mechanical replacement; review them manually.,1,JUnit 6,Testing,Java,Recipes for migrating to [JUnit 6](https://junit.org/).,,Basic building blocks for transforming Java code.,,
213213
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.AddJUnit4ReplacingPowerMockModule,Add JUnit 4 where the PowerMock JUnit 4 module provided it,"Adds `junit:junit` to modules that use JUnit 4 and depend on `powermock-module-junit4`, which brings JUnit 4 in transitively, so that their tests still compile once the PowerMock dependencies are removed. A module that declares `junit:junit` itself is left as it is.",2,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,,
214214
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.mockito.AddMockitoExtensionIfAnnotationsUsed,Adds Mockito extensions to Mockito tests,Adds `@ExtendWith(MockitoExtension.class)` to JUnit 5 tests or `@RunWith(MockitoJUnitRunner.class)` to JUnit 4 tests using Mockito annotations like `@Mock` or `@Captor`.,1,Mockito,Testing,Java,,,Basic building blocks for transforming Java code.,,

‎src/test/java/org/openrewrite/java/testing/junit6/RemoveInterceptDynamicTestTest.java‎

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ public void defaults(RecipeSpec spec) {
3636

3737
@DocumentExample
3838
@Test
39-
void removeInterceptDynamicTestMethod() {
39+
void addInvocationContextParameter() {
4040
rewriteRun(
4141
java(
4242
"""
4343
import org.junit.jupiter.api.extension.ExtensionContext;
4444
import org.junit.jupiter.api.extension.InvocationInterceptor;
45-
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
4645
4746
class MyInterceptor implements InvocationInterceptor {
4847
@Override
@@ -51,28 +50,126 @@ public void interceptDynamicTest(Invocation<Void> invocation, ExtensionContext e
5150
invocation.proceed();
5251
System.out.println("After dynamic test");
5352
}
53+
}
54+
""",
55+
"""
56+
import org.junit.jupiter.api.extension.DynamicTestInvocationContext;
57+
import org.junit.jupiter.api.extension.ExtensionContext;
58+
import org.junit.jupiter.api.extension.InvocationInterceptor;
5459
60+
class MyInterceptor implements InvocationInterceptor {
5561
@Override
56-
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
62+
public void interceptDynamicTest(Invocation<Void> invocation, DynamicTestInvocationContext invocationContext, ExtensionContext extensionContext) throws Throwable {
63+
System.out.println("Before dynamic test");
64+
invocation.proceed();
65+
System.out.println("After dynamic test");
66+
}
67+
}
68+
"""
69+
)
70+
);
71+
}
72+
73+
@Test
74+
void avoidClashWithExistingName() {
75+
rewriteRun(
76+
java(
77+
"""
78+
import org.junit.jupiter.api.extension.ExtensionContext;
79+
import org.junit.jupiter.api.extension.InvocationInterceptor;
80+
81+
class MyInterceptor implements InvocationInterceptor {
82+
private final String invocationContext = "dynamic";
83+
84+
@Override
85+
public void interceptDynamicTest(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable {
86+
System.out.println(invocationContext);
87+
invocation.proceed();
88+
}
89+
}
90+
""",
91+
"""
92+
import org.junit.jupiter.api.extension.DynamicTestInvocationContext;
93+
import org.junit.jupiter.api.extension.ExtensionContext;
94+
import org.junit.jupiter.api.extension.InvocationInterceptor;
95+
96+
class MyInterceptor implements InvocationInterceptor {
97+
private final String invocationContext = "dynamic";
98+
99+
@Override
100+
public void interceptDynamicTest(Invocation<Void> invocation, DynamicTestInvocationContext invocationContext1, ExtensionContext extensionContext) throws Throwable {
101+
System.out.println(invocationContext);
102+
invocation.proceed();
103+
}
104+
}
105+
"""
106+
)
107+
);
108+
}
109+
110+
@Test
111+
void removeTwoArgumentOverloadWhenThreeArgumentOverloadIsDeclared() {
112+
rewriteRun(
113+
java(
114+
"""
115+
import org.junit.jupiter.api.extension.DynamicTestInvocationContext;
116+
import org.junit.jupiter.api.extension.ExtensionContext;
117+
import org.junit.jupiter.api.extension.InvocationInterceptor;
118+
119+
class MyInterceptor implements InvocationInterceptor {
120+
@Override
121+
public void interceptDynamicTest(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable {
57122
invocation.proceed();
58123
}
59124
60-
private static class Method {}
125+
@Override
126+
public void interceptDynamicTest(Invocation<Void> invocation, DynamicTestInvocationContext invocationContext, ExtensionContext extensionContext) throws Throwable {
127+
System.out.println(invocationContext.getExecutable());
128+
invocation.proceed();
129+
}
61130
}
62131
""",
63132
"""
133+
import org.junit.jupiter.api.extension.DynamicTestInvocationContext;
134+
import org.junit.jupiter.api.extension.ExtensionContext;
135+
import org.junit.jupiter.api.extension.InvocationInterceptor;
136+
137+
class MyInterceptor implements InvocationInterceptor {
138+
139+
@Override
140+
public void interceptDynamicTest(Invocation<Void> invocation, DynamicTestInvocationContext invocationContext, ExtensionContext extensionContext) throws Throwable {
141+
System.out.println(invocationContext.getExecutable());
142+
invocation.proceed();
143+
}
144+
}
145+
"""
146+
)
147+
);
148+
}
149+
150+
@Test
151+
void keepThreeArgumentOverload() {
152+
rewriteRun(
153+
java(
154+
"""
155+
import java.lang.reflect.Method;
156+
157+
import org.junit.jupiter.api.extension.DynamicTestInvocationContext;
64158
import org.junit.jupiter.api.extension.ExtensionContext;
65159
import org.junit.jupiter.api.extension.InvocationInterceptor;
66160
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
67161
68162
class MyInterceptor implements InvocationInterceptor {
163+
@Override
164+
public void interceptDynamicTest(Invocation<Void> invocation, DynamicTestInvocationContext invocationContext, ExtensionContext extensionContext) throws Throwable {
165+
System.out.println(invocationContext.getExecutable());
166+
invocation.proceed();
167+
}
69168
70169
@Override
71170
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
72171
invocation.proceed();
73172
}
74-
75-
private static class Method {}
76173
}
77174
"""
78175
)

0 commit comments

Comments
 (0)