3333import java .util .stream .Collectors ;
3434
3535@ SuppressWarnings ("NullableProblems" )
36- public class HamcrestAnyOfToAssertJ extends Recipe {
36+ public class HamcrestOfMatchersToAssertJ extends Recipe {
3737 @ Override
3838 public String getDisplayName () {
3939 return "Migrate `anyOf` Hamcrest Matcher to AssertJ" ;
@@ -46,25 +46,29 @@ public String getDescription() {
4646
4747 private static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher ("org.hamcrest.MatcherAssert assertThat(..)" );
4848 private static final MethodMatcher ANY_OF_MATCHER = new MethodMatcher ("org.hamcrest.Matchers anyOf(..)" );
49+ private static final MethodMatcher ALL_OF_MATCHER = new MethodMatcher ("org.hamcrest.Matchers allOf(..)" );
4950
5051 @ Override
5152 public TreeVisitor <?, ExecutionContext > getVisitor () {
52- return Preconditions .check (new UsesMethod <>(ANY_OF_MATCHER ), new AnyOfToAssertJVisitor ());
53+ return Preconditions .check (Preconditions .or (
54+ new UsesMethod <>(ANY_OF_MATCHER ),
55+ new UsesMethod <>(ALL_OF_MATCHER )
56+ ), new AnyOfToAssertJVisitor ());
5357 }
5458
5559 private static class AnyOfToAssertJVisitor extends JavaIsoVisitor <ExecutionContext > {
56-
5760 @ Override
5861 public J .MethodInvocation visitMethodInvocation (J .MethodInvocation methodInvocation , ExecutionContext ctx ) {
5962 J .MethodInvocation mi = super .visitMethodInvocation (methodInvocation , ctx );
6063 List <Expression > arguments = mi .getArguments ();
61- Expression anyOfExpression = arguments .get (arguments .size () - 1 );
62- if (!ASSERT_THAT_MATCHER .matches (mi ) || !ANY_OF_MATCHER .matches (anyOfExpression )) {
64+ Expression ofExpression = arguments .get (arguments .size () - 1 );
65+ boolean allOfMatcherMatches = ALL_OF_MATCHER .matches (ofExpression );
66+ if (!ASSERT_THAT_MATCHER .matches (mi ) || !(ANY_OF_MATCHER .matches (ofExpression ) || allOfMatcherMatches )) {
6367 return mi ;
6468 }
6569
6670 // Skip anyOf(Iterable)
67- List <Expression > anyOfArguments = ((J .MethodInvocation ) anyOfExpression ).getArguments ();
71+ List <Expression > anyOfArguments = ((J .MethodInvocation ) ofExpression ).getArguments ();
6872 if (TypeUtils .isAssignableTo ("java.lang.Iterable" , anyOfArguments .get (0 ).getType ())) {
6973 return mi ;
7074 }
@@ -82,15 +86,16 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
8286 parameters .add (arguments .get (0 ));
8387 }
8488
85- // .satisfiesAnyOf(...)
86- template .append (".satisfiesAnyOf(\n " );
89+ // .satisfiesAnyOf(...) or .satisfies(...)
90+ template .append (allOfMatcherMatches ? ".satisfies( \n " : ".satisfiesAnyOf(\n " );
8791 template .append (anyOfArguments .stream ()
8892 .map (arg -> "arg -> assertThat(arg, #{any()})" )
8993 .collect (Collectors .joining (",\n " )));
9094 parameters .addAll (anyOfArguments );
9195 template .append ("\n );" );
9296
9397 maybeRemoveImport ("org.hamcrest.Matchers.anyOf" );
98+ maybeRemoveImport ("org.hamcrest.Matchers.allOf" );
9499 maybeAddImport ("org.assertj.core.api.Assertions" , "assertThat" );
95100 return JavaTemplate .builder (template .toString ())
96101 .contextSensitive ()
0 commit comments