Skip to content

Commit 650840d

Browse files
authored
Replace Hamcrest CoreMatchers as well using wildcards (#425)
1 parent da6a123 commit 650840d

File tree

6 files changed

+63
-20
lines changed

6 files changed

+63
-20
lines changed

src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJ.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public String getDescription() {
3939
return "Migrate Hamcrest `is(Object)` to AssertJ `Assertions.assertThat(..)`.";
4040
}
4141

42-
static final MethodMatcher IS_OBJECT_MATCHER = new MethodMatcher("org.hamcrest.Matchers is(..)");
42+
static final MethodMatcher IS_OBJECT_MATCHER = new MethodMatcher("org.hamcrest.*Matchers is(..)");
4343

4444
@Override
4545
public TreeVisitor<?, ExecutionContext> getVisitor() {

src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ public String getDescription() {
6363

6464
@Override
6565
public TreeVisitor<?, ExecutionContext> getVisitor() {
66-
return Preconditions.check(new UsesMethod<>("org.hamcrest.Matchers " + matcher + "(..)"), new MigrateToAssertJVisitor());
66+
return Preconditions.check(new UsesMethod<>("org.hamcrest.*Matchers " + matcher + "(..)"), new MigrateToAssertJVisitor());
6767
}
6868

6969
private class MigrateToAssertJVisitor extends JavaIsoVisitor<ExecutionContext> {
7070
private final MethodMatcher assertThatMatcher = new MethodMatcher("org.hamcrest.MatcherAssert assertThat(..)");
71-
private final MethodMatcher matchersMatcher = new MethodMatcher("org.hamcrest.Matchers " + matcher + "(..)");
72-
private final MethodMatcher subMatcher = new MethodMatcher("org.hamcrest.Matchers *(org.hamcrest.Matcher)");
71+
private final MethodMatcher matchersMatcher = new MethodMatcher("org.hamcrest.*Matchers " + matcher + "(..)");
72+
private final MethodMatcher subMatcher = new MethodMatcher("org.hamcrest.*Matchers *(org.hamcrest.Matcher)");
7373

7474

7575
@Override
@@ -104,6 +104,7 @@ actual, assertion, getArgumentsTemplate(matcherArgumentMethod)))
104104
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
105105
maybeAddImport("org.assertj.core.api.Assertions", "within");
106106
maybeRemoveImport("org.hamcrest.Matchers." + matcher);
107+
maybeRemoveImport("org.hamcrest.CoreMatchers." + matcher);
107108
maybeRemoveImport("org.hamcrest.MatcherAssert");
108109
maybeRemoveImport("org.hamcrest.MatcherAssert.assertThat");
109110

src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestNotMatcherToAssertJ.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ public String getDescription() {
6262

6363
@Override
6464
public TreeVisitor<?, ExecutionContext> getVisitor() {
65-
return Preconditions.check(new UsesMethod<>("org.hamcrest.Matchers " + notMatcher + "(..)"), new MigrateToAssertJVisitor());
65+
return Preconditions.check(new UsesMethod<>("org.hamcrest.*Matchers " + notMatcher + "(..)"), new MigrateToAssertJVisitor());
6666
}
6767

6868
private class MigrateToAssertJVisitor extends JavaIsoVisitor<ExecutionContext> {
6969
private final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.hamcrest.MatcherAssert assertThat(..)");
70-
private final MethodMatcher NOT_MATCHER = new MethodMatcher("org.hamcrest.Matchers not(org.hamcrest.Matcher)");
71-
private final MethodMatcher MATCHERS_MATCHER = new MethodMatcher("org.hamcrest.Matchers " + notMatcher + "(..)");
72-
private final MethodMatcher SUB_MATCHER = new MethodMatcher("org.hamcrest.Matchers *(org.hamcrest.Matcher)");
70+
private final MethodMatcher NOT_MATCHER = new MethodMatcher("org.hamcrest.*Matchers not(org.hamcrest.Matcher)");
71+
private final MethodMatcher MATCHERS_MATCHER = new MethodMatcher("org.hamcrest.*Matchers " + notMatcher + "(..)");
72+
private final MethodMatcher SUB_MATCHER = new MethodMatcher("org.hamcrest.*Matchers *(org.hamcrest.Matcher)");
7373

7474
@Override
7575
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
@@ -110,6 +110,8 @@ private J.MethodInvocation handleTwoArgumentCase(J.MethodInvocation mi, Expressi
110110
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
111111
maybeRemoveImport("org.hamcrest.Matchers.not");
112112
maybeRemoveImport("org.hamcrest.Matchers." + notMatcher);
113+
maybeRemoveImport("org.hamcrest.CoreMatchers.not");
114+
maybeRemoveImport("org.hamcrest.CoreMatchers." + notMatcher);
113115
maybeRemoveImport("org.hamcrest.MatcherAssert");
114116
maybeRemoveImport("org.hamcrest.MatcherAssert.assertThat");
115117

src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestOfMatchersToAssertJ.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public String getDescription() {
4545
}
4646

4747
private static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.hamcrest.MatcherAssert assertThat(..)");
48-
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(..)");
48+
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(..)");
5050

5151
@Override
5252
public TreeVisitor<?, ExecutionContext> getVisitor() {
@@ -96,6 +96,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
9696

9797
maybeRemoveImport("org.hamcrest.Matchers.anyOf");
9898
maybeRemoveImport("org.hamcrest.Matchers.allOf");
99+
maybeRemoveImport("org.hamcrest.CoreMatchers.anyOf");
100+
maybeRemoveImport("org.hamcrest.CoreMatchers.allOf");
99101
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
100102
return JavaTemplate.builder(template.toString())
101103
.contextSensitive()

src/main/java/org/openrewrite/java/testing/hamcrest/RemoveIsMatcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public String getDescription() {
3636
return "Remove Hamcrest `is(Matcher)` ahead of migration.";
3737
}
3838

39-
static final MethodMatcher IS_MATCHER = new MethodMatcher("org.hamcrest.Matchers is(org.hamcrest.Matcher)");
39+
static final MethodMatcher IS_MATCHER = new MethodMatcher("org.hamcrest.*Matchers is(org.hamcrest.Matcher)");
4040
static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.hamcrest.MatcherAssert assertThat(..)");
4141

4242
@Override
@@ -48,6 +48,7 @@ public J visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) {
4848
getCursor().putMessage("ASSERT_THAT", mi);
4949
} else if (IS_MATCHER.matches(mi) && getCursor().pollNearestMessage("ASSERT_THAT") != null) {
5050
maybeRemoveImport("org.hamcrest.Matchers.is");
51+
maybeRemoveImport("org.hamcrest.CoreMatchers.is");
5152
return mi.getArguments().get(0).withPrefix(mi.getPrefix());
5253
}
5354
return super.visitMethodInvocation(mi, ctx);

src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJTest.java

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,43 @@ void test() {
164164
""")
165165
);
166166
}
167+
168+
@Test
169+
void coreMatchers() {
170+
rewriteRun(
171+
spec -> spec.recipe(new HamcrestMatcherToAssertJ("startsWith", "startsWith")),
172+
//language=java
173+
java(
174+
"""
175+
import org.junit.jupiter.api.Test;
176+
177+
import static org.hamcrest.MatcherAssert.assertThat;
178+
import static org.hamcrest.CoreMatchers.startsWith;
179+
180+
class ATest {
181+
@Test
182+
void test() {
183+
String str1 = "Hello world!";
184+
assertThat(str1, startsWith("Hello"));
185+
}
186+
}
187+
""",
188+
"""
189+
import org.junit.jupiter.api.Test;
190+
191+
import static org.assertj.core.api.Assertions.assertThat;
192+
193+
class ATest {
194+
@Test
195+
void test() {
196+
String str1 = "Hello world!";
197+
assertThat(str1).startsWith("Hello");
198+
}
199+
}
200+
"""
201+
)
202+
);
203+
}
167204
}
168205

169206
@Nested
@@ -352,7 +389,7 @@ void test() {
352389
353390
import java.util.ArrayList;
354391
import java.util.List;
355-
392+
356393
import static org.assertj.core.api.Assertions.assertThat;
357394
358395
class ATest {
@@ -374,10 +411,10 @@ void closeToTest() {
374411
//language=java
375412
java("""
376413
import org.junit.jupiter.api.Test;
377-
414+
378415
import static org.hamcrest.MatcherAssert.assertThat;
379416
import static org.hamcrest.Matchers.closeTo;
380-
417+
381418
class ATest {
382419
@Test
383420
void replaceCloseTo() {
@@ -387,10 +424,10 @@ void replaceCloseTo() {
387424
""",
388425
"""
389426
import org.junit.jupiter.api.Test;
390-
427+
391428
import static org.assertj.core.api.Assertions.assertThat;
392429
import static org.assertj.core.api.Assertions.within;
393-
430+
394431
class ATest {
395432
@Test
396433
void replaceCloseTo() {
@@ -409,10 +446,10 @@ void closeToWorksWithBigDecimal() {
409446
java("""
410447
import org.junit.jupiter.api.Test;
411448
import java.math.BigDecimal;
412-
449+
413450
import static org.hamcrest.MatcherAssert.assertThat;
414451
import static org.hamcrest.Matchers.closeTo;
415-
452+
416453
class ATest {
417454
@Test
418455
void replaceCloseTo() {
@@ -426,10 +463,10 @@ void replaceCloseTo() {
426463
"""
427464
import org.junit.jupiter.api.Test;
428465
import java.math.BigDecimal;
429-
466+
430467
import static org.assertj.core.api.Assertions.assertThat;
431468
import static org.assertj.core.api.Assertions.within;
432-
469+
433470
class ATest {
434471
@Test
435472
void replaceCloseTo() {

0 commit comments

Comments
 (0)