From 2be58983230a83dfd5b519ea0c906b621405ba1b Mon Sep 17 00:00:00 2001 From: timo-abele <129042237+timo-abele@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:32:45 +0100 Subject: [PATCH 1/3] add testcase to verify bug this testcase uses a different import for the is matcher --- .../HamcrestIsMatcherToAssertJTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJTest.java index 63d7a8a75..abf2d5276 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJTest.java @@ -196,4 +196,46 @@ void testEquals() { } """)); } + + @Test + @DocumentExample + void isMatcherFromCore() { + rewriteRun( + //language=java + java( + """ + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.core.Is.is; + import org.junit.jupiter.api.Test; + + class DebugTest { + class Foo { + int i = 8; + } + @Test + void ba() { + assertThat(System.out, is(System.out)); + assertThat(new Foo(), is(new Foo())); + } + } + """, """ + import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + import org.junit.jupiter.api.Test; + + class DebugTest { + + class Foo { + int i = 8; + } + + @Test + void ba() { + assertThat(System.out).isEqualTo(System.out); + assertThat(new Foo()).isEqualTo(new Foo()); + } + } + """)); + } + + } From f180f43383c5d6c2c72dc7ca049d7005aeab9085 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 14 Apr 2024 23:10:37 +0200 Subject: [PATCH 2/3] ChangeMethodTargetToStatic from Is.is to Matchers.is first --- .../resources/META-INF/rewrite/hamcrest.yml | 7 +- .../HamcrestIsMatcherToAssertJTest.java | 304 ++++++++---------- .../MigrateHamcrestToAssertJTest.java | 99 ++++-- 3 files changed, 216 insertions(+), 194 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/hamcrest.yml b/src/main/resources/META-INF/rewrite/hamcrest.yml index 16209981e..080579007 100644 --- a/src/main/resources/META-INF/rewrite/hamcrest.yml +++ b/src/main/resources/META-INF/rewrite/hamcrest.yml @@ -39,7 +39,12 @@ tags: - hamcrest - assertj recipeList: - # First remove wrapping `is(Matcher)` calls such that further recipes will match + # First change `is(..)` to `Matchers.is(..)` for consist + - org.openrewrite.java.ChangeMethodTargetToStatic: + methodPattern: org.hamcrest.core.Is is(..) + fullyQualifiedTargetTypeName: org.hamcrest.Matchers + + # Then remove wrapping `is(Matcher)` calls such that further recipes will match - org.openrewrite.java.testing.hamcrest.RemoveIsMatcher # Then remove calls to `MatcherAssert.assertThat(String, is(Matcher))` diff --git a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJTest.java index abf2d5276..af6565bbf 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJTest.java @@ -41,33 +41,36 @@ void isMatcher() { rewriteRun( //language=java java( - """ - import org.junit.jupiter.api.Test; - import static org.hamcrest.MatcherAssert.assertThat; - import static org.hamcrest.Matchers.is; - - class ATest { - @Test - void testEquals() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - assertThat(str1, is(str2)); - } - } - """, """ - import org.junit.jupiter.api.Test; - - import static org.assertj.core.api.Assertions.assertThat; - - class ATest { - @Test - void testEquals() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - assertThat(str1).isEqualTo(str2); - } - } - """)); + """ + import org.junit.jupiter.api.Test; + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.is; + + class ATest { + @Test + void testEquals() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + assertThat(str1, is(str2)); + } + } + """, + """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class ATest { + @Test + void testEquals() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + assertThat(str1).isEqualTo(str2); + } + } + """ + ) + ); } @Test @@ -75,35 +78,38 @@ void isMatcherWithReason() { rewriteRun( //language=java java( - """ - import org.junit.jupiter.api.Test; - import static org.hamcrest.MatcherAssert.assertThat; - import static org.hamcrest.Matchers.is; - - class ATest { - @Test - void testEquals() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - // Foo - assertThat("Reason", str1, is(str2)); - } - } - """, """ - import org.junit.jupiter.api.Test; - - import static org.assertj.core.api.Assertions.assertThat; - - class ATest { - @Test - void testEquals() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - // Foo - assertThat(str1).as("Reason").isEqualTo(str2); - } - } - """)); + """ + import org.junit.jupiter.api.Test; + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.is; + + class ATest { + @Test + void testEquals() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + // Foo + assertThat("Reason", str1, is(str2)); + } + } + """, + """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class ATest { + @Test + void testEquals() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + // Foo + assertThat(str1).as("Reason").isEqualTo(str2); + } + } + """ + ) + ); } @@ -112,21 +118,23 @@ void isMatcherWithMatcher() { rewriteRun( //language=java java( - """ - import org.junit.jupiter.api.Test; - import static org.hamcrest.MatcherAssert.assertThat; - import static org.hamcrest.Matchers.is; - import static org.hamcrest.Matchers.equalTo; - - class ATest { - @Test - void test() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - assertThat(str1, is(equalTo(str2))); - } - } - """)); + """ + import org.junit.jupiter.api.Test; + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.is; + import static org.hamcrest.Matchers.equalTo; + + class ATest { + @Test + void test() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + assertThat(str1, is(equalTo(str2))); + } + } + """ + ) + ); } @Test @@ -134,108 +142,72 @@ void isObjectArray() { rewriteRun( //language=java java( - """ - import org.junit.jupiter.api.Test; - import static org.hamcrest.MatcherAssert.assertThat; - import static org.hamcrest.Matchers.is; - - class ATest { - @Test - void testEquals() { - String[] str1 = new String[]{"Hello world!"}; - String[] str2 = new String[]{"Hello world!"}; - assertThat(str1, is(str2)); - } - } - """, """ - import org.junit.jupiter.api.Test; - - import static org.assertj.core.api.Assertions.assertThat; - - class ATest { - @Test - void testEquals() { - String[] str1 = new String[]{"Hello world!"}; - String[] str2 = new String[]{"Hello world!"}; - assertThat(str1).containsExactly(str2); - } - } - """)); + """ + import org.junit.jupiter.api.Test; + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.is; + + class ATest { + @Test + void testEquals() { + String[] str1 = new String[]{"Hello world!"}; + String[] str2 = new String[]{"Hello world!"}; + assertThat(str1, is(str2)); + } + } + """, + """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class ATest { + @Test + void testEquals() { + String[] str1 = new String[]{"Hello world!"}; + String[] str2 = new String[]{"Hello world!"}; + assertThat(str1).containsExactly(str2); + } + } + """ + ) + ); } @Test void isPrimitiveArray() { - rewriteRun( - //language=java - java( - """ - import org.junit.jupiter.api.Test; - import static org.hamcrest.MatcherAssert.assertThat; - import static org.hamcrest.Matchers.is; - - class ATest { - @Test - void testEquals() { - int[] str1 = new int[]{1}; - int[] str2 = new int[]{1}; - assertThat(str1, is(str2)); - } - } - """, """ - import org.junit.jupiter.api.Test; - - import static org.assertj.core.api.Assertions.assertThat; - - class ATest { - @Test - void testEquals() { - int[] str1 = new int[]{1}; - int[] str2 = new int[]{1}; - assertThat(str1).containsExactly(str2); - } - } - """)); - } - - @Test - @DocumentExample - void isMatcherFromCore() { rewriteRun( //language=java java( """ - import static org.hamcrest.MatcherAssert.assertThat; - import static org.hamcrest.core.Is.is; - import org.junit.jupiter.api.Test; - - class DebugTest { - class Foo { - int i = 8; - } - @Test - void ba() { - assertThat(System.out, is(System.out)); - assertThat(new Foo(), is(new Foo())); - } - } - """, """ - import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - import org.junit.jupiter.api.Test; - - class DebugTest { - - class Foo { - int i = 8; - } - - @Test - void ba() { - assertThat(System.out).isEqualTo(System.out); - assertThat(new Foo()).isEqualTo(new Foo()); - } - } - """)); + import org.junit.jupiter.api.Test; + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.is; + + class ATest { + @Test + void testEquals() { + int[] str1 = new int[]{1}; + int[] str2 = new int[]{1}; + assertThat(str1, is(str2)); + } + } + """, + """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class ATest { + @Test + void testEquals() { + int[] str1 = new int[]{1}; + int[] str2 = new int[]{1}; + assertThat(str1).containsExactly(str2); + } + } + """ + ) + ); } - - } diff --git a/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java index 8d55469de..bc3c30f62 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java @@ -67,7 +67,7 @@ class Biscuit { Biscuit(String name) { this.name = name; } - + int getChocolateChipCount() { return 10; } @@ -121,12 +121,12 @@ void allOfStringMatchersAndConvert() { java( """ import org.junit.jupiter.api.Test; - + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasLength; - + class ATest { @Test void test() { @@ -163,12 +163,12 @@ void convertAnyOfMatchersAfterSatisfiesAnyOfConversion() { java( """ import org.junit.jupiter.api.Test; - + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasLength; - + class ATest { @Test void test() { @@ -217,9 +217,9 @@ void arrayReplacements(String actual, String hamcrestMatcher, String matcherArgs //language=java String template = """ import org.junit.jupiter.api.Test; - + %s - + class ATest { @Test void test() { @@ -275,9 +275,9 @@ void stringReplacements(String actual, String hamcrestMatcher, String matcherArg //language=java String template = """ import org.junit.jupiter.api.Test; - + %s - + class ATest { @Test void test() { @@ -314,9 +314,9 @@ void objectReplacements(String actual, String hamcrestMatcher, String matcherArg //language=java String template = """ import org.junit.jupiter.api.Test; - + %s - + class ATest { @Test void test() { @@ -361,9 +361,9 @@ void numberReplacements(String actual, String hamcrestMatcher, String matcherArg //language=java String template = """ import org.junit.jupiter.api.Test; - + %s - + class ATest { @Test void test() { @@ -403,9 +403,9 @@ void listReplacements(String actual, String hamcrestMatcher, String matcherArgs, String template = """ import java.util.List; import org.junit.jupiter.api.Test; - + %s - + class ATest { @Test void test(String item) { @@ -440,9 +440,9 @@ void mapReplacements(String actual, String hamcrestMatcher, String matcherArgs, String template = """ import java.util.Map; import org.junit.jupiter.api.Test; - + %s - + class ATest { @Test void test(String key, String value) { @@ -489,9 +489,9 @@ void notReplacements(String actual, String hamcrestMatcher, String matcherArgs, //language=java String template = """ import org.junit.jupiter.api.Test; - + %s - + class ATest { @Test void test() { @@ -511,10 +511,10 @@ class Dependencies { @Language("java") private static final String JAVA_BEFORE = """ import org.junit.jupiter.api.Test; - + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; - + class ATest { @Test void test() { @@ -525,9 +525,9 @@ void test() { @Language("java") private static final String JAVA_AFTER = """ import org.junit.jupiter.api.Test; - + import static org.assertj.core.api.Assertions.assertThat; - + class ATest { @Test void test() { @@ -595,11 +595,11 @@ void assertjGradleDependencyAddedWithTestScope() { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { testImplementation "org.hamcrest:hamcrest:2.2" } @@ -608,11 +608,11 @@ void assertjGradleDependencyAddedWithTestScope() { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { testImplementation "org.assertj:%s" testImplementation "org.hamcrest:hamcrest:2.2" @@ -673,4 +673,49 @@ void bar() { ); } } + + @Test + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/497") + void isMatcherFromCore() { + rewriteRun( + //language=java + java( + """ + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.core.Is.is; + + import org.junit.jupiter.api.Test; + + class DebugTest { + class Foo { + int i = 8; + } + + @Test + void ba() { + assertThat(System.out, is(System.out)); + assertThat(new Foo(), is(new Foo())); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + + import org.junit.jupiter.api.Test; + + class DebugTest { + class Foo { + int i = 8; + } + + @Test + void ba() { + assertThat(System.out).isEqualTo(System.out); + assertThat(new Foo()).isEqualTo(new Foo()); + } + } + """ + ) + ); + } } From 184e110160f24336d4ca16ac965c5775563e46b8 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 14 Apr 2024 23:10:37 +0200 Subject: [PATCH 3/3] ChangeMethodTargetToStatic from Is.is to Matchers.is first --- src/main/resources/META-INF/rewrite/hamcrest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/META-INF/rewrite/hamcrest.yml b/src/main/resources/META-INF/rewrite/hamcrest.yml index 080579007..997af9b0f 100644 --- a/src/main/resources/META-INF/rewrite/hamcrest.yml +++ b/src/main/resources/META-INF/rewrite/hamcrest.yml @@ -39,7 +39,7 @@ tags: - hamcrest - assertj recipeList: - # First change `is(..)` to `Matchers.is(..)` for consist + # First change `is(..)` to `Matchers.is(..)` for consistent matching - org.openrewrite.java.ChangeMethodTargetToStatic: methodPattern: org.hamcrest.core.Is is(..) fullyQualifiedTargetTypeName: org.hamcrest.Matchers