Skip to content

Commit e5867a3

Browse files
amishra-utimtebeek
andauthored
Fix ParameterizedRunnerToParameterized recipe: Preserve field-dependent setup logic (#708)
* Fix ParameterizedRunnerToParameterizedT Migration Recipe: Preserve Field-Dependent Setup Logic * Switch to AnnotationService instead of getLeadingAnnotations --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 83a8d7b commit e5867a3

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openrewrite.internal.ListUtils;
2121
import org.openrewrite.java.*;
2222
import org.openrewrite.java.search.UsesType;
23+
import org.openrewrite.java.service.AnnotationService;
2324
import org.openrewrite.java.tree.*;
2425
import org.openrewrite.marker.Markers;
2526

@@ -34,13 +35,15 @@ public class ParameterizedRunnerToParameterized extends Recipe {
3435
private static final AnnotationMatcher JUNIT_TEST = new AnnotationMatcher("@org.junit.Test");
3536
private static final AnnotationMatcher JUPITER_TEST = new AnnotationMatcher("@org.junit.jupiter.api.Test");
3637
private static final AnnotationMatcher PARAMETERS = new AnnotationMatcher("@org.junit.runners.Parameterized$Parameters");
38+
private static final AnnotationMatcher BEFORE = new AnnotationMatcher("@org.junit.Before");
3739
private static final AnnotationMatcher PARAMETER = new AnnotationMatcher("@org.junit.runners.Parameterized$Parameter");
3840
private static final AnnotationMatcher PARAMETERIZED_TEST = new AnnotationMatcher("@org.junit.jupiter.params.ParameterizedTest");
3941

4042
private static final String PARAMETERS_ANNOTATION_ARGUMENTS = "parameters-annotation-args";
4143
private static final String CONSTRUCTOR_ARGUMENTS = "constructor-args";
4244
private static final String FIELD_INJECTION_ARGUMENTS = "field-injection-args";
4345
private static final String PARAMETERS_METHOD_NAME = "parameters-method-name";
46+
private static final String BEFORE_METHOD_NAME = "before-method-name";
4447

4548
@Override
4649
public String getDisplayName() {
@@ -69,16 +72,17 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
6972
List<Statement> constructorParams = (List<Statement>) params.get(CONSTRUCTOR_ARGUMENTS);
7073
Map<Integer, Statement> fieldInjectionParams = (Map<Integer, Statement>) params.get(FIELD_INJECTION_ARGUMENTS);
7174
String initMethodName = "init" + cd.getSimpleName();
75+
String beforeMethodName = (String) params.getOrDefault(BEFORE_METHOD_NAME, null);
7276

7377
// Constructor Injected Test
7478
if (parametersMethodName != null && constructorParams != null && constructorParams.stream().anyMatch(org.openrewrite.java.tree.J.VariableDeclarations.class::isInstance)) {
75-
doAfterVisit(new ParameterizedRunnerToParameterizedTestsVisitor(classDecl, parametersMethodName, initMethodName, parametersAnnotationArguments, constructorParams, true, ctx));
79+
doAfterVisit(new ParameterizedRunnerToParameterizedTestsVisitor(classDecl, parametersMethodName, initMethodName, parametersAnnotationArguments, constructorParams, true, beforeMethodName,ctx));
7680
}
7781

7882
// Field Injected Test
7983
else if (parametersMethodName != null && fieldInjectionParams != null) {
8084
List<Statement> fieldParams = new ArrayList<>(fieldInjectionParams.values());
81-
doAfterVisit(new ParameterizedRunnerToParameterizedTestsVisitor(classDecl, parametersMethodName, initMethodName, parametersAnnotationArguments, fieldParams, false, ctx));
85+
doAfterVisit(new ParameterizedRunnerToParameterizedTestsVisitor(classDecl, parametersMethodName, initMethodName, parametersAnnotationArguments, fieldParams, false, beforeMethodName, ctx));
8286
}
8387
}
8488
return cd;
@@ -88,17 +92,19 @@ else if (parametersMethodName != null && fieldInjectionParams != null) {
8892
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
8993
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
9094
Cursor classDeclCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance);
95+
Map<String, Object> params = classDeclCursor.computeMessageIfAbsent(((J.ClassDeclaration) classDeclCursor.getValue()).getId().toString(), v -> new HashMap<>());
9196
if (m.isConstructor()) {
92-
Map<String, Object> params = classDeclCursor.computeMessageIfAbsent(((J.ClassDeclaration) classDeclCursor.getValue()).getId().toString(), v -> new HashMap<>());
9397
params.put(CONSTRUCTOR_ARGUMENTS, m.getParameters());
9498
}
95-
for (J.Annotation annotation : m.getLeadingAnnotations()) {
99+
for (J.Annotation annotation : service(AnnotationService.class).getAllAnnotations(getCursor())) {
96100
if (PARAMETERS.matches(annotation)) {
97-
Map<String, Object> params = classDeclCursor.computeMessageIfAbsent(((J.ClassDeclaration) classDeclCursor.getValue()).getId().toString(), v -> new HashMap<>());
98101
params.put(PARAMETERS_ANNOTATION_ARGUMENTS, annotation.getArguments());
99102
params.put(PARAMETERS_METHOD_NAME, method.getSimpleName());
100103
break;
101104
}
105+
if (BEFORE.matches(annotation)) {
106+
params.put(BEFORE_METHOD_NAME, method.getSimpleName());
107+
}
102108
}
103109
return m;
104110
}
@@ -109,7 +115,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
109115
Cursor classDeclCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance);
110116
J.Annotation parameterAnnotation = null;
111117
Integer position = 0;
112-
for (J.Annotation leadingAnnotation : variableDeclarations.getLeadingAnnotations()) {
118+
for (J.Annotation leadingAnnotation : service(AnnotationService.class).getAllAnnotations(getCursor())) {
113119
if (PARAMETER.matches(leadingAnnotation)) {
114120
parameterAnnotation = leadingAnnotation;
115121
if (parameterAnnotation.getArguments() != null && !(parameterAnnotation.getArguments().get(0) instanceof J.Empty)) {
@@ -161,6 +167,7 @@ public ParameterizedRunnerToParameterizedTestsVisitor(J.ClassDeclaration scope,
161167
@Nullable List<Expression> parameterizedTestAnnotationParameters,
162168
List<Statement> parameterizedTestMethodParameters,
163169
boolean isConstructorInjection,
170+
@Nullable String beforeMethodName,
164171
ExecutionContext ctx) {
165172
this.scope = scope;
166173
this.initMethodName = initMethodName;
@@ -218,6 +225,9 @@ public ParameterizedRunnerToParameterizedTestsVisitor(J.ClassDeclaration scope,
218225
for (String p : initStatementParams) {
219226
initMethodTemplate.append(" this.").append(p).append(" = ").append(p).append(";\n");
220227
}
228+
if (beforeMethodName != null) {
229+
initMethodTemplate.append(" this.").append(beforeMethodName).append("();\n");
230+
}
221231

222232
initMethodTemplate.append("}");
223233
this.initMethodDeclarationTemplate = JavaTemplate.builder(initMethodTemplate.toString())

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

+9
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ void parameterizedFieldInjectionToParameterizedTest() {
235235
spec -> spec.typeValidationOptions(TypeValidation.none()),
236236
java(
237237
"""
238+
import org.junit.Before;
238239
import org.junit.Test;
239240
import org.junit.runner.RunWith;
240241
import org.junit.runners.Parameterized;
@@ -261,12 +262,16 @@ public static List<Object[]> parameters() {
261262
return Arrays.asList(new Object[]{124, "Otis", "TheDog", Map.of("toys", "ball", "treats", "bacon")}, new Object[]{126, "Garfield", "TheBoss", Map.of("toys", "yarn", "treats", "fish")});
262263
}
263264
265+
@Before
266+
public void setUp() {}
267+
264268
@Test
265269
public void checkName() {
266270
}
267271
}
268272
""",
269273
"""
274+
import org.junit.Before;
270275
import org.junit.jupiter.params.ParameterizedTest;
271276
import org.junit.jupiter.params.provider.MethodSource;
272277
@@ -284,6 +289,9 @@ public static List<Object[]> parameters() {
284289
return Arrays.asList(new Object[]{124, "Otis", "TheDog", Map.of("toys", "ball", "treats", "bacon")}, new Object[]{126, "Garfield", "TheBoss", Map.of("toys", "yarn", "treats", "fish")});
285290
}
286291
292+
@Before
293+
public void setUp() {}
294+
287295
@MethodSource("parameters")
288296
@ParameterizedTest(name = "{index}: {0} {1} - {2}")
289297
public void checkName(Integer id, String name, String nickName, Map<String, String> stuff) {
@@ -295,6 +303,7 @@ public void initRewriteTests(Integer id, String name, String nickName, Map<Strin
295303
this.name = name;
296304
this.nickName = nickName;
297305
this.stuff = stuff;
306+
this.setUp();
298307
}
299308
}
300309
"""

0 commit comments

Comments
 (0)