Skip to content

Commit

Permalink
Correctly replace Date and Instant hamcrest assertions to AssertJ
Browse files Browse the repository at this point in the history
Fixes #526
  • Loading branch information
timtebeek committed Jul 6, 2024
1 parent e2bb0ae commit 33c97a2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions src/main/resources/META-INF/rewrite/hamcrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -793,4 +794,47 @@ void bar(List<String> 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)
)
);
}
}

0 comments on commit 33c97a2

Please sign in to comment.