Skip to content

Commit

Permalink
support for allOf hamcrest matcher (#396)
Browse files Browse the repository at this point in the history
* recipe now supports allOf

* Quick polish

* Rename recipe

* Drop FlattenAllOf for HamcrestOfMatchersToAssertJ

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
AlekSimpson and timtebeek authored Aug 4, 2023
1 parent c78bbd3 commit 6d5f1ae
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 272 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.stream.Collectors;

@SuppressWarnings("NullableProblems")
public class HamcrestAnyOfToAssertJ extends Recipe {
public class HamcrestOfMatchersToAssertJ extends Recipe {
@Override
public String getDisplayName() {
return "Migrate `anyOf` Hamcrest Matcher to AssertJ";
Expand All @@ -46,25 +46,29 @@ public String getDescription() {

private static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.hamcrest.MatcherAssert assertThat(..)");
private static final MethodMatcher ANY_OF_MATCHER = new MethodMatcher("org.hamcrest.Matchers anyOf(..)");
private static final MethodMatcher ALL_OF_MATCHER = new MethodMatcher("org.hamcrest.Matchers allOf(..)");

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(ANY_OF_MATCHER), new AnyOfToAssertJVisitor());
return Preconditions.check(Preconditions.or(
new UsesMethod<>(ANY_OF_MATCHER),
new UsesMethod<>(ALL_OF_MATCHER)
), new AnyOfToAssertJVisitor());
}

private static class AnyOfToAssertJVisitor extends JavaIsoVisitor<ExecutionContext> {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocation, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(methodInvocation, ctx);
List<Expression> arguments = mi.getArguments();
Expression anyOfExpression = arguments.get(arguments.size() - 1);
if (!ASSERT_THAT_MATCHER.matches(mi) || !ANY_OF_MATCHER.matches(anyOfExpression)) {
Expression ofExpression = arguments.get(arguments.size() - 1);
boolean allOfMatcherMatches = ALL_OF_MATCHER.matches(ofExpression);
if (!ASSERT_THAT_MATCHER.matches(mi) || !(ANY_OF_MATCHER.matches(ofExpression) || allOfMatcherMatches)) {
return mi;
}

// Skip anyOf(Iterable)
List<Expression> anyOfArguments = ((J.MethodInvocation) anyOfExpression).getArguments();
List<Expression> anyOfArguments = ((J.MethodInvocation) ofExpression).getArguments();
if (TypeUtils.isAssignableTo("java.lang.Iterable", anyOfArguments.get(0).getType())) {
return mi;
}
Expand All @@ -82,15 +86,16 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
parameters.add(arguments.get(0));
}

// .satisfiesAnyOf(...)
template.append(".satisfiesAnyOf(\n");
// .satisfiesAnyOf(...) or .satisfies(...)
template.append(allOfMatcherMatches ? ".satisfies(\n" : ".satisfiesAnyOf(\n");
template.append(anyOfArguments.stream()
.map(arg -> "arg -> assertThat(arg, #{any()})")
.collect(Collectors.joining(",\n")));
parameters.addAll(anyOfArguments);
template.append("\n);");

maybeRemoveImport("org.hamcrest.Matchers.anyOf");
maybeRemoveImport("org.hamcrest.Matchers.allOf");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
return JavaTemplate.builder(template.toString())
.contextSensitive()
Expand Down
9 changes: 3 additions & 6 deletions src/main/resources/META-INF/rewrite/hamcrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@ recipeList:
# First remove wrapping `is(Matcher)` calls such that further recipes will match
- org.openrewrite.java.testing.hamcrest.RemoveIsMatcher

# Then remove calls to `MatcherAssert.assertThat(String, Matcher)`
# Then remove calls to `MatcherAssert.assertThat(String, is(Matcher))`
- org.openrewrite.java.testing.hamcrest.HamcrestIsMatcherToAssertJ

# Flatten calls to `MatcherAssert.assertThat(.., allOf(..))` to easier to migrate individual statements
- org.openrewrite.java.testing.hamcrest.FlattenAllOf

# Then remove calls to `MatcherAssert.assertThat(String, anyOf(..))`
- org.openrewrite.java.testing.hamcrest.HamcrestAnyOfToAssertJ
# Then remove calls to `MatcherAssert.assertThat(String, anyOf(..))` and `allOf(..)`
- org.openrewrite.java.testing.hamcrest.HamcrestOfMatchersToAssertJ

# Then remove calls to `MatcherAssert.assertThat(String, boolean)`
- org.openrewrite.java.testing.hamcrest.AssertThatBooleanToAssertJ
Expand Down

This file was deleted.

Loading

0 comments on commit 6d5f1ae

Please sign in to comment.