Skip to content

Commit

Permalink
Limit Hamcrest replacements by type (#469)
Browse files Browse the repository at this point in the history
Fixes #468
  • Loading branch information
timtebeek authored Jan 19, 2024
1 parent affabc8 commit 7a07f04
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
String replacement = 2 <= arguments.size() &&
TypeUtils.asArray(arguments.get(arguments.size() - 2).getType()) != null ?
"containsExactly" : "isEqualTo";
doAfterVisit(new HamcrestMatcherToAssertJ("is", replacement).getVisitor());
doAfterVisit(new HamcrestMatcherToAssertJ("is", replacement, null).getVisitor());

return super.visitMethodInvocation(methodInvocation, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -51,6 +52,13 @@ public class HamcrestMatcherToAssertJ extends Recipe {
@Nullable
String assertion;

@Option(displayName = "Argument Type",
description = "The type of the argument to the Hamcrest `Matcher`.",
example = "java.math.BigDecimal",
required = false)
@Nullable
String argumentType;

@Override
public String getDisplayName() {
return "Migrate from Hamcrest `Matcher` to AssertJ";
Expand All @@ -71,7 +79,6 @@ private class MigrateToAssertJVisitor extends JavaIsoVisitor<ExecutionContext> {
private final MethodMatcher matchersMatcher = new MethodMatcher("org.hamcrest.*Matchers " + matcher + "(..)");
private final MethodMatcher subMatcher = new MethodMatcher("org.hamcrest.*Matchers *(org.hamcrest.Matcher)");


@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
Expand All @@ -89,6 +96,10 @@ private J.MethodInvocation replace(J.MethodInvocation mi, ExecutionContext ctx)
if (!matchersMatcher.matches(matcherArgument) || subMatcher.matches(matcherArgument)) {
return mi;
}
if (argumentType != null && !TypeUtils.isOfClassType(actualArgument.getType(), argumentType)) {
return mi;
}

String actual = typeToIndicator(actualArgument.getType());
J.MethodInvocation matcherArgumentMethod = (J.MethodInvocation) matcherArgument;
JavaTemplate template = JavaTemplate.builder(String.format(
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/rewrite/hamcrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ recipeList:
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
matcher: comparesEqualTo
assertion: isEqualTo
argumentType: java.lang.String
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
matcher: comparesEqualTo
assertion: isEqualByComparingTo

- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
matcher: equalTo
assertion: isEqualTo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DoNotConvert {
@Test
void notMatcher() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("not", "isNotEqualTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("not", "isNotEqualTo", null)),
//language=java
java(
"""
Expand All @@ -64,7 +64,7 @@ void test() {
@Test
void isMatcher() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("is", "isEqualTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("is", "isEqualTo", null)),
//language=java
java(
"""
Expand All @@ -87,7 +87,7 @@ void test() {
@Test
void anyOfVarargsMatcher() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("is", "isEqualTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("is", "isEqualTo", null)),
//language=java
java(
"""
Expand All @@ -110,7 +110,7 @@ void test() {
@Test
void anyOfIterableMatcher() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("is", "isEqualTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("is", "isEqualTo", null)),
//language=java
java(
"""
Expand All @@ -137,7 +137,7 @@ class NoArgument {
@Test
void isEmpty() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("isEmptyString", "isEmpty")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("isEmptyString", "isEmpty", null)),
//language=java
java(
"""
Expand Down Expand Up @@ -173,7 +173,7 @@ void test() {
@Test
void coreMatchers() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("startsWith", "startsWith")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("startsWith", "startsWith", null)),
//language=java
java(
"""
Expand Down Expand Up @@ -214,7 +214,7 @@ class TwoArguments {
@DocumentExample
void equalToString() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("equalTo", "isEqualTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("equalTo", "isEqualTo", null)),
//language=java
java(
"""
Expand Down Expand Up @@ -252,7 +252,7 @@ void test() {
@Test
void equalToStringLiteral() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("equalTo", "isEqualTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("equalTo", "isEqualTo", null)),
//language=java
java(
"""
Expand Down Expand Up @@ -288,7 +288,7 @@ void test() {
@Test
void equalToObject() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("equalTo", "isEqualTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("equalTo", "isEqualTo", null)),
//language=java
java(
"""
Expand Down Expand Up @@ -336,7 +336,7 @@ void test() {
@Test
void lessThanNumber() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("lessThan", "isLessThan")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("lessThan", "isLessThan", null)),
//language=java
java(
"""
Expand Down Expand Up @@ -375,7 +375,7 @@ void test() {
@Test
void containsInAnyOrderWithArray() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("containsInAnyOrder", "containsExactlyInAnyOrder")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("containsInAnyOrder", "containsExactlyInAnyOrder", null)),
//language=java
java(
"""
Expand Down Expand Up @@ -418,7 +418,7 @@ void test() {
@Test
void closeToTest() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("closeTo", "isCloseTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("closeTo", "isCloseTo", null)),
//language=java
java(
"""
Expand Down Expand Up @@ -453,7 +453,7 @@ void replaceCloseTo() {
@Test
void closeToWorksWithBigDecimal() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("closeTo", "isCloseTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("closeTo", "isCloseTo", null)),
//language=java
java(
"""
Expand Down Expand Up @@ -499,7 +499,7 @@ class ThreeArguments {
@Test
void reasonAsLiteral() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("equalTo", "isEqualTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("equalTo", "isEqualTo", null)),
//language=java
java(
"""
Expand Down Expand Up @@ -537,7 +537,7 @@ void test() {
@Test
void reasonAsMethodCall() {
rewriteRun(
spec -> spec.recipe(new HamcrestMatcherToAssertJ("equalTo", "isEqualTo")),
spec -> spec.recipe(new HamcrestMatcherToAssertJ("equalTo", "isEqualTo", null)),
//language=java
java(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,55 +60,56 @@ void isEqualTo() {
//language=java
rewriteRun(
java(
"""
class Biscuit {
String name;
Biscuit(String name) {
this.name = name;
}
int getChocolateChipCount() {
return 10;
}
"""
class Biscuit {
String name;
Biscuit(String name) {
this.name = name;
}
int getChocolateChipCount() {
return 10;
}
int getHazelnutCount() {
return 3;
}
}
"""),
int getHazelnutCount() {
return 3;
}
}
"""),
java(
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class BiscuitTest {
@Test
public void biscuits() {
Biscuit theBiscuit = new Biscuit("Ginger");
Biscuit myBiscuit = new Biscuit("Ginger");
assertThat(theBiscuit, equalTo(myBiscuit));
assertThat("chocolate chips", theBiscuit.getChocolateChipCount(), equalTo(10));
assertThat("hazelnuts", theBiscuit.getHazelnutCount(), equalTo(3));
}
}
""", """
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BiscuitTest {
@Test
public void biscuits() {
Biscuit theBiscuit = new Biscuit("Ginger");
Biscuit myBiscuit = new Biscuit("Ginger");
assertThat(theBiscuit).isEqualTo(myBiscuit);
assertThat(theBiscuit.getChocolateChipCount()).as("chocolate chips").isEqualTo(10);
assertThat(theBiscuit.getHazelnutCount()).as("hazelnuts").isEqualTo(3);
}
}
"""));
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class BiscuitTest {
@Test
public void biscuits() {
Biscuit theBiscuit = new Biscuit("Ginger");
Biscuit myBiscuit = new Biscuit("Ginger");
assertThat(theBiscuit, equalTo(myBiscuit));
assertThat("chocolate chips", theBiscuit.getChocolateChipCount(), equalTo(10));
assertThat("hazelnuts", theBiscuit.getHazelnutCount(), equalTo(3));
}
}
""",
"""
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BiscuitTest {
@Test
public void biscuits() {
Biscuit theBiscuit = new Biscuit("Ginger");
Biscuit myBiscuit = new Biscuit("Ginger");
assertThat(theBiscuit).isEqualTo(myBiscuit);
assertThat(theBiscuit.getChocolateChipCount()).as("chocolate chips").isEqualTo(10);
assertThat(theBiscuit.getHazelnutCount()).as("hazelnuts").isEqualTo(3);
}
}
"""));
}

@Test
Expand Down Expand Up @@ -623,4 +624,51 @@ void assertjGradleDependencyAddedWithTestScope() {
}
}

@Nested
class Issues {
@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/468")
void comparesEqualToBigDecimals() {
//language=java
rewriteRun(
java(
"""
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.comparesEqualTo;
import java.math.BigDecimal;
class A {
void foo() {
var a = new BigDecimal("1");
var b = new BigDecimal("1.00");
assertThat(a, comparesEqualTo(b));
}
void bar() {
var a = "1";
var b = "1.00";
assertThat(a, comparesEqualTo(b));
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
class A {
void foo() {
var a = new BigDecimal("1");
var b = new BigDecimal("1.00");
assertThat(a).isEqualByComparingTo(b);
}
void bar() {
var a = "1";
var b = "1.00";
assertThat(a).isEqualTo(b);
}
}
"""
)
);
}
}
}

0 comments on commit 7a07f04

Please sign in to comment.