2020import org .openrewrite .internal .ListUtils ;
2121import org .openrewrite .java .*;
2222import org .openrewrite .java .search .UsesType ;
23+ import org .openrewrite .java .service .AnnotationService ;
2324import org .openrewrite .java .tree .*;
2425import 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 ())
0 commit comments