diff --git a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java index 2003876fb..8f0a6958c 100644 --- a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java +++ b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java @@ -37,21 +37,21 @@ @AllArgsConstructor public class HamcrestMatcherToAssertJ extends Recipe { - @Option(displayName = "Hamcrest Matcher", + @Option(displayName = "Hamcrest matcher", description = "The Hamcrest `Matcher` to migrate to JUnit5.", example = "equalTo", required = false) @Nullable String matcher; - @Option(displayName = "AssertJ Assertion", + @Option(displayName = "AssertJ assertion", description = "The AssertJ method to migrate to.", example = "isEqualTo", required = false) @Nullable String assertion; - @Option(displayName = "Argument Type", + @Option(displayName = "Argument type", description = "The type of the argument to the Hamcrest `Matcher`.", example = "java.math.BigDecimal", required = false) diff --git a/src/main/resources/META-INF/rewrite/hamcrest.yml b/src/main/resources/META-INF/rewrite/hamcrest.yml index 1bcb7a12e..2271ea278 100644 --- a/src/main/resources/META-INF/rewrite/hamcrest.yml +++ b/src/main/resources/META-INF/rewrite/hamcrest.yml @@ -67,6 +67,40 @@ recipeList: matcher: comparesEqualTo assertion: isEqualByComparingTo + - org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ: + matcher: lessThan + assertion: isBefore + argumentType: java.util.Date + - org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ: + matcher: lessThanOrEqualTo + assertion: isBeforeOrEqualTo + argumentType: java.util.Date + - org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ: + matcher: greaterThan + assertion: isAfter + argumentType: java.util.Date + - org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ: + matcher: greaterThanOrEqualTo + assertion: isAfterOrEqualTo + argumentType: java.util.Date + + - org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ: + matcher: lessThan + assertion: isBefore + argumentType: java.time.Instant + - org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ: + matcher: lessThanOrEqualTo + assertion: isBeforeOrEqualTo + argumentType: java.time.Instant + - org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ: + matcher: greaterThan + assertion: isAfter + argumentType: java.time.Instant + - org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ: + matcher: greaterThanOrEqualTo + assertion: isAfterOrEqualTo + argumentType: java.time.Instant + - org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ: matcher: equalTo assertion: isEqualTo 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 5b44a19c1..b0229562f 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java @@ -21,6 +21,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.Issue; @@ -793,4 +794,47 @@ void bar(List list) { ) ); } + + @ParameterizedTest + @ValueSource( + strings = { + "java.util.Date", + "java.time.Instant" + } + ) + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/526") + void greaterThanOrEqualToDate(String type){ + rewriteRun( + java( + """ + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.greaterThan; + import static org.hamcrest.Matchers.greaterThanOrEqualTo; + import static org.hamcrest.Matchers.lessThan; + import static org.hamcrest.Matchers.lessThanOrEqualTo; + + class Foo { + void bar(%1$s type) { + assertThat(type, lessThan(type)); + assertThat(type, lessThanOrEqualTo(type)); + assertThat(type, greaterThan(type)); + assertThat(type, greaterThanOrEqualTo(type)); + } + } + """.formatted(type), + """ + import static org.assertj.core.api.Assertions.assertThat; + + class Foo { + void bar(%1$s type) { + assertThat(type).isBefore(type); + assertThat(type).isBeforeOrEqualTo(type); + assertThat(type).isAfter(type); + assertThat(type).isAfterOrEqualTo(type); + } + } + """.formatted(type) + ) + ); + } }