diff --git a/src/main/resources/META-INF/rewrite/hamcrest.yml b/src/main/resources/META-INF/rewrite/hamcrest.yml index 2271ea278..68748c9fa 100644 --- a/src/main/resources/META-INF/rewrite/hamcrest.yml +++ b/src/main/resources/META-INF/rewrite/hamcrest.yml @@ -39,6 +39,14 @@ tags: - hamcrest - assertj recipeList: + # Add dependency if not already present + - org.openrewrite.java.dependencies.AddDependency: + groupId: org.assertj + artifactId: assertj-core + version: 3.x + onlyIfUsing: org.hamcrest.* + acceptTransitive: true + # First change `is(..)` to `Matchers.is(..)` for consistent matching - org.openrewrite.java.ChangeMethodTargetToStatic: methodPattern: org.hamcrest.core.* *(..) @@ -319,10 +327,3 @@ recipeList: notMatcher: empty assertion: isNotEmpty - # Add dependency if not already present - - org.openrewrite.java.dependencies.AddDependency: - groupId: org.assertj - artifactId: assertj-core - version: 3.x - onlyIfUsing: org.assertj.core.api.Assertions - acceptTransitive: true 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 b0229562f..b1ea8767a 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java @@ -68,7 +68,7 @@ class Biscuit { Biscuit(String name) { this.name = name; } - + int getChocolateChipCount() { return 10; } @@ -120,40 +120,40 @@ void allOfStringMatchersAndConvert() { rewriteRun( //language=java 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() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - assertThat(str1, allOf(equalTo(str2), hasLength(12))); - } - } - """, """ - import org.junit.jupiter.api.Test; - - import static org.assertj.core.api.Assertions.assertThat; - - class ATest { - @Test - void test() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - assertThat(str1) - .satisfies( - arg -> assertThat(arg).isEqualTo(str2), - arg -> assertThat(arg).hasSize(12) - ); - } - } - """)); + """ + 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() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + assertThat(str1, allOf(equalTo(str2), hasLength(12))); + } + } + """, """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class ATest { + @Test + void test() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + assertThat(str1) + .satisfies( + arg -> assertThat(arg).isEqualTo(str2), + arg -> assertThat(arg).hasSize(12) + ); + } + } + """)); } @Test @@ -162,40 +162,40 @@ void convertAnyOfMatchersAfterSatisfiesAnyOfConversion() { rewriteRun( //language=java 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() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - assertThat(str1, anyOf(equalTo(str2), hasLength(12))); - } - } - """, """ - import org.junit.jupiter.api.Test; - - import static org.assertj.core.api.Assertions.assertThat; - - class ATest { - @Test - void test() { - String str1 = "Hello world!"; - String str2 = "Hello world!"; - assertThat(str1) - .satisfiesAnyOf( - arg -> assertThat(arg).isEqualTo(str2), - arg -> assertThat(arg).hasSize(12) - ); - } - } - """)); + """ + 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() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + assertThat(str1, anyOf(equalTo(str2), hasLength(12))); + } + } + """, """ + import org.junit.jupiter.api.Test; + + import static org.assertj.core.api.Assertions.assertThat; + + class ATest { + @Test + void test() { + String str1 = "Hello world!"; + String str2 = "Hello world!"; + assertThat(str1) + .satisfiesAnyOf( + arg -> assertThat(arg).isEqualTo(str2), + arg -> assertThat(arg).hasSize(12) + ); + } + } + """)); } private static Stream arrayReplacements() { @@ -218,9 +218,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() { @@ -276,9 +276,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() { @@ -315,9 +315,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() { @@ -331,14 +331,14 @@ void test() { String after = template.formatted(importsAfter, "assertThat(%s).%s(%s);".formatted(actual, assertJAssertion, matcherArgs)); rewriteRun( java( - """ - class Biscuit { - String name; - Biscuit(String name) { - this.name = name; - } - } - """), + """ + class Biscuit { + String name; + Biscuit(String name) { + this.name = name; + } + } + """), java(before, after)); } @@ -362,9 +362,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() { @@ -404,9 +404,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) { @@ -441,9 +441,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) { @@ -490,9 +490,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() { @@ -512,10 +512,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() { @@ -527,9 +527,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() { @@ -541,90 +541,91 @@ void test() { @Test void assertjMavenDependencyAddedWithTestScope() { rewriteRun( - spec -> spec.expectedCyclesThatMakeChanges(2), mavenProject("project", srcTestJava(java(JAVA_BEFORE, JAVA_AFTER)), //language=xml pomXml(""" - - 4.0.0 - com.example - demo - 0.0.1-SNAPSHOT - - - org.hamcrest - hamcrest - 2.2 - test - - - - """, - sourceSpecs -> sourceSpecs.after(after -> """ - - 4.0.0 - com.example - demo - 0.0.1-SNAPSHOT - - - org.assertj - assertj-core - %s - test - - - org.hamcrest - hamcrest - 2.2 - test - - - - """.formatted(Pattern.compile("(3\\.2.*)").matcher(requireNonNull(after)).results().findFirst().orElseThrow().group(1)))) - ) - ); + + 4.0.0 + com.example + demo + 0.0.1-SNAPSHOT + + + org.hamcrest + hamcrest + 2.2 + test + + + + """, + sourceSpecs -> sourceSpecs.after(after -> """ + + 4.0.0 + com.example + demo + 0.0.1-SNAPSHOT + + + org.assertj + assertj-core + %s + test + + + org.hamcrest + hamcrest + 2.2 + test + + + + """.formatted(Pattern.compile("(3\\.2.*)").matcher(requireNonNull(after)).results().findFirst().orElseThrow().group(1)))) + ) + ); } @Test void assertjGradleDependencyAddedWithTestScope() { rewriteRun( - spec -> spec.beforeRecipe(withToolingApi()).expectedCyclesThatMakeChanges(2), + spec -> spec.beforeRecipe(withToolingApi()), mavenProject("project", srcTestJava(java(JAVA_BEFORE, JAVA_AFTER)), - buildGradle(""" - plugins { - id "java-library" - } - - repositories { - mavenCentral() - } - - dependencies { - testImplementation "org.hamcrest:hamcrest:2.2" - } - """, - sourceSpecs -> sourceSpecs.after(after -> """ - plugins { - id "java-library" - } - - repositories { - mavenCentral() - } - - dependencies { - testImplementation "org.assertj:%s" - testImplementation "org.hamcrest:hamcrest:2.2" - } + //language=groovy + buildGradle( """ - .formatted(Pattern.compile("(assertj-core:[^\"]*)").matcher(requireNonNull(after)).results().findFirst().orElseThrow().group(1)) + plugins { + id "java-library" + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation "org.hamcrest:hamcrest:2.2" + } + """, + sourceSpecs -> sourceSpecs.after(after -> """ + plugins { + id "java-library" + } + + repositories { + mavenCentral() + } + + dependencies { + testImplementation "org.assertj:%s" + testImplementation "org.hamcrest:hamcrest:2.2" + } + """ + .formatted(Pattern.compile("(assertj-core:[^\"]*)").matcher(requireNonNull(after)).results().findFirst().orElseThrow().group(1)) + ) ) ) - ) - ); + ); } } @@ -685,14 +686,14 @@ void isMatcherFromCore() { """ 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)); @@ -702,14 +703,14 @@ void ba() { """, """ 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); @@ -732,9 +733,9 @@ void isEqualMatcherFromCore() { import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.IsSame.sameInstance; - + import org.junit.jupiter.api.Test; - + class DebugTest { @Test void ba() { @@ -746,9 +747,9 @@ void ba() { """, """ import static org.assertj.core.api.Assertions.assertThat; - + import org.junit.jupiter.api.Test; - + class DebugTest { @Test void ba() { @@ -803,7 +804,7 @@ void bar(List list) { } ) @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/526") - void greaterThanOrEqualToDate(String type){ + void greaterThanOrEqualToDate(String type) { rewriteRun( java( """