20
20
import org .openrewrite .internal .ListUtils ;
21
21
import org .openrewrite .java .*;
22
22
import org .openrewrite .java .search .UsesType ;
23
+ import org .openrewrite .java .service .AnnotationService ;
23
24
import org .openrewrite .java .tree .*;
24
25
import org .openrewrite .marker .Markers ;
25
26
@@ -34,13 +35,15 @@ public class ParameterizedRunnerToParameterized extends Recipe {
34
35
private static final AnnotationMatcher JUNIT_TEST = new AnnotationMatcher ("@org.junit.Test" );
35
36
private static final AnnotationMatcher JUPITER_TEST = new AnnotationMatcher ("@org.junit.jupiter.api.Test" );
36
37
private static final AnnotationMatcher PARAMETERS = new AnnotationMatcher ("@org.junit.runners.Parameterized$Parameters" );
38
+ private static final AnnotationMatcher BEFORE = new AnnotationMatcher ("@org.junit.Before" );
37
39
private static final AnnotationMatcher PARAMETER = new AnnotationMatcher ("@org.junit.runners.Parameterized$Parameter" );
38
40
private static final AnnotationMatcher PARAMETERIZED_TEST = new AnnotationMatcher ("@org.junit.jupiter.params.ParameterizedTest" );
39
41
40
42
private static final String PARAMETERS_ANNOTATION_ARGUMENTS = "parameters-annotation-args" ;
41
43
private static final String CONSTRUCTOR_ARGUMENTS = "constructor-args" ;
42
44
private static final String FIELD_INJECTION_ARGUMENTS = "field-injection-args" ;
43
45
private static final String PARAMETERS_METHOD_NAME = "parameters-method-name" ;
46
+ private static final String BEFORE_METHOD_NAME = "before-method-name" ;
44
47
45
48
@ Override
46
49
public String getDisplayName () {
@@ -69,16 +72,17 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
69
72
List <Statement > constructorParams = (List <Statement >) params .get (CONSTRUCTOR_ARGUMENTS );
70
73
Map <Integer , Statement > fieldInjectionParams = (Map <Integer , Statement >) params .get (FIELD_INJECTION_ARGUMENTS );
71
74
String initMethodName = "init" + cd .getSimpleName ();
75
+ String beforeMethodName = (String ) params .getOrDefault (BEFORE_METHOD_NAME , null );
72
76
73
77
// Constructor Injected Test
74
78
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 ));
76
80
}
77
81
78
82
// Field Injected Test
79
83
else if (parametersMethodName != null && fieldInjectionParams != null ) {
80
84
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 ));
82
86
}
83
87
}
84
88
return cd ;
@@ -88,17 +92,19 @@ else if (parametersMethodName != null && fieldInjectionParams != null) {
88
92
public J .MethodDeclaration visitMethodDeclaration (J .MethodDeclaration method , ExecutionContext ctx ) {
89
93
J .MethodDeclaration m = super .visitMethodDeclaration (method , ctx );
90
94
Cursor classDeclCursor = getCursor ().dropParentUntil (J .ClassDeclaration .class ::isInstance );
95
+ Map <String , Object > params = classDeclCursor .computeMessageIfAbsent (((J .ClassDeclaration ) classDeclCursor .getValue ()).getId ().toString (), v -> new HashMap <>());
91
96
if (m .isConstructor ()) {
92
- Map <String , Object > params = classDeclCursor .computeMessageIfAbsent (((J .ClassDeclaration ) classDeclCursor .getValue ()).getId ().toString (), v -> new HashMap <>());
93
97
params .put (CONSTRUCTOR_ARGUMENTS , m .getParameters ());
94
98
}
95
- for (J .Annotation annotation : m . getLeadingAnnotations ( )) {
99
+ for (J .Annotation annotation : service ( AnnotationService . class ). getAllAnnotations ( getCursor () )) {
96
100
if (PARAMETERS .matches (annotation )) {
97
- Map <String , Object > params = classDeclCursor .computeMessageIfAbsent (((J .ClassDeclaration ) classDeclCursor .getValue ()).getId ().toString (), v -> new HashMap <>());
98
101
params .put (PARAMETERS_ANNOTATION_ARGUMENTS , annotation .getArguments ());
99
102
params .put (PARAMETERS_METHOD_NAME , method .getSimpleName ());
100
103
break ;
101
104
}
105
+ if (BEFORE .matches (annotation )) {
106
+ params .put (BEFORE_METHOD_NAME , method .getSimpleName ());
107
+ }
102
108
}
103
109
return m ;
104
110
}
@@ -109,7 +115,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
109
115
Cursor classDeclCursor = getCursor ().dropParentUntil (J .ClassDeclaration .class ::isInstance );
110
116
J .Annotation parameterAnnotation = null ;
111
117
Integer position = 0 ;
112
- for (J .Annotation leadingAnnotation : variableDeclarations . getLeadingAnnotations ( )) {
118
+ for (J .Annotation leadingAnnotation : service ( AnnotationService . class ). getAllAnnotations ( getCursor () )) {
113
119
if (PARAMETER .matches (leadingAnnotation )) {
114
120
parameterAnnotation = leadingAnnotation ;
115
121
if (parameterAnnotation .getArguments () != null && !(parameterAnnotation .getArguments ().get (0 ) instanceof J .Empty )) {
@@ -161,6 +167,7 @@ public ParameterizedRunnerToParameterizedTestsVisitor(J.ClassDeclaration scope,
161
167
@ Nullable List <Expression > parameterizedTestAnnotationParameters ,
162
168
List <Statement > parameterizedTestMethodParameters ,
163
169
boolean isConstructorInjection ,
170
+ @ Nullable String beforeMethodName ,
164
171
ExecutionContext ctx ) {
165
172
this .scope = scope ;
166
173
this .initMethodName = initMethodName ;
@@ -218,6 +225,9 @@ public ParameterizedRunnerToParameterizedTestsVisitor(J.ClassDeclaration scope,
218
225
for (String p : initStatementParams ) {
219
226
initMethodTemplate .append (" this." ).append (p ).append (" = " ).append (p ).append (";\n " );
220
227
}
228
+ if (beforeMethodName != null ) {
229
+ initMethodTemplate .append (" this." ).append (beforeMethodName ).append ("();\n " );
230
+ }
221
231
222
232
initMethodTemplate .append ("}" );
223
233
this .initMethodDeclarationTemplate = JavaTemplate .builder (initMethodTemplate .toString ())
0 commit comments