3838@ AllArgsConstructor
3939@ NoArgsConstructor
4040public class SimplifyChainedAssertJAssertion extends Recipe {
41- @ Option (displayName = "AssertJ Assertion " ,
41+ @ Option (displayName = "AssertJ chained assertion " ,
4242 description = "The chained AssertJ assertion to move to dedicated assertion." ,
4343 example = "equals" ,
4444 required = false )
4545 @ Nullable
4646 String chainedAssertion ;
4747
48- @ Option (displayName = "AssertJ Assertion " ,
48+ @ Option (displayName = "AssertJ replaced assertion " ,
4949 description = "The AssertJ assert that should be replaced." ,
5050 example = "isTrue" ,
5151 required = false )
5252 @ Nullable
5353 String assertToReplace ;
5454
55- @ Option (displayName = "AssertJ Assertion " ,
55+ @ Option (displayName = "AssertJ replacement assertion " ,
5656 description = "The AssertJ method to migrate to." ,
5757 example = "isEqualTo" ,
5858 required = false )
5959 @ Nullable
6060 String dedicatedAssertion ;
6161
62- @ Option (displayName = "Required Type " ,
63- description = "Specifies the type the recipe should run on ." ,
62+ @ Option (displayName = "Required type " ,
63+ description = "The type of the actual assertion argument ." ,
6464 example = "java.lang.String" ,
6565 required = false )
6666 @ Nullable
@@ -120,11 +120,6 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
120120 List <Expression > arguments = new ArrayList <>();
121121 arguments .add (actual );
122122
123- // Special case for more expressive assertions: assertThat(x.size()).isEqualTo(0) -> isEmpty()
124- if ("size" .equals (chainedAssertion ) && "isEqualTo" .equals (assertToReplace ) && hasZeroArgument (mi )) {
125- return applyTemplate ("assertThat(#{any()}).isEmpty()" , arguments , mi , ctx );
126- }
127-
128123 String template = getStringTemplateAndAppendArguments (assertThatArg , mi , arguments );
129124 return applyTemplate (String .format (template , dedicatedAssertion ), arguments , mi , ctx );
130125 }
@@ -136,38 +131,38 @@ private J.MethodInvocation applyTemplate(String formattedTemplate, List<Expressi
136131 .build ()
137132 .apply (getCursor (), mi .getCoordinates ().replace (), arguments .toArray ());
138133 }
139- }
140134
141- private String getStringTemplateAndAppendArguments (J .MethodInvocation assertThatArg , J .MethodInvocation methodToReplace , List <Expression > arguments ) {
142- Expression assertThatArgument = assertThatArg .getArguments ().get (0 );
143- Expression methodToReplaceArgument = methodToReplace .getArguments ().get (0 );
144- boolean assertThatArgumentIsEmpty = assertThatArgument instanceof J .Empty ;
145- boolean methodToReplaceArgumentIsEmpty = methodToReplaceArgument instanceof J .Empty ;
135+ private String getStringTemplateAndAppendArguments (J .MethodInvocation assertThatArg , J .MethodInvocation methodToReplace , List <Expression > arguments ) {
136+ Expression assertThatArgument = assertThatArg .getArguments ().get (0 );
137+ Expression methodToReplaceArgument = methodToReplace .getArguments ().get (0 );
138+ boolean assertThatArgumentIsEmpty = assertThatArgument instanceof J .Empty ;
139+ boolean methodToReplaceArgumentIsEmpty = methodToReplaceArgument instanceof J .Empty ;
146140
147- // If both arguments are empty, then the select is already added to the arguments list, and we use a minimal template
148- if (assertThatArgumentIsEmpty && methodToReplaceArgumentIsEmpty ) {
149- return "assertThat(#{any()}).%s()" ;
150- }
151-
152- // If both arguments are not empty, then we add both to the arguments to the arguments list, and return a template with two arguments
153- if (!assertThatArgumentIsEmpty && !methodToReplaceArgumentIsEmpty ) {
154- // This should only happen for map assertions using a key and value
155- arguments .add (assertThatArgument );
156- arguments .add (methodToReplaceArgument );
157- return "assertThat(#{any()}).%s(#{any()}, #{any()})" ;
158- }
141+ // If both arguments are empty, then the select is already added to the arguments list, and we use a minimal template
142+ if (assertThatArgumentIsEmpty && methodToReplaceArgumentIsEmpty ) {
143+ return "assertThat(#{any()}).%s()" ;
144+ }
159145
160- // If either argument is empty, we choose which one to add to the arguments list, and optionally extract the select
161- arguments .add (extractEitherArgument (assertThatArgumentIsEmpty , assertThatArgument , methodToReplaceArgument ));
146+ // If both arguments are not empty, then we add both to the arguments to the arguments list, and return a template with two arguments
147+ if (!assertThatArgumentIsEmpty && !methodToReplaceArgumentIsEmpty ) {
148+ // This should only happen for map assertions using a key and value
149+ arguments .add (assertThatArgument );
150+ arguments .add (methodToReplaceArgument );
151+ return "assertThat(#{any()}).%s(#{any()}, #{any()})" ;
152+ }
162153
163- // Special case for Path.of() assertions
164- if ("java.nio.file.Path" .equals (requiredType ) && dedicatedAssertion .contains ("Raw" )
165- && TypeUtils .isAssignableTo ("java.lang.String" , assertThatArgument .getType ())) {
166- return "assertThat(#{any()}).%s(Path.of(#{any()}))" ;
167- }
154+ // If either argument is empty, we choose which one to add to the arguments list, and optionally extract the select
155+ arguments .add (extractEitherArgument (assertThatArgumentIsEmpty , assertThatArgument , methodToReplaceArgument ));
168156
169- return "assertThat(#{any()}).%s(#{any()})" ;
157+ // Special case for Path.of() assertions
158+ if ("java.nio.file.Path" .equals (requiredType ) && dedicatedAssertion .contains ("Raw" )
159+ && TypeUtils .isAssignableTo ("java.lang.String" , assertThatArgument .getType ())) {
160+ maybeAddImport ("java.nio.file.Path" );
161+ return "assertThat(#{any()}).%s(Path.of(#{any()}))" ;
162+ }
170163
164+ return "assertThat(#{any()}).%s(#{any()})" ;
165+ }
171166 }
172167
173168 private static Expression extractEitherArgument (boolean assertThatArgumentIsEmpty , Expression assertThatArgument , Expression methodToReplaceArgument ) {
@@ -183,13 +178,4 @@ private static Expression extractEitherArgument(boolean assertThatArgumentIsEmpt
183178 }
184179 return assertThatArgument ;
185180 }
186-
187- private boolean hasZeroArgument (J .MethodInvocation method ) {
188- List <Expression > arguments = method .getArguments ();
189- if (arguments .size () == 1 && arguments .get (0 ) instanceof J .Literal ) {
190- J .Literal literalArg = (J .Literal ) arguments .get (0 );
191- return literalArg .getValue () != null && literalArg .getValue ().equals (0 );
192- }
193- return false ;
194- }
195181}
0 commit comments