Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/resources/META-INF/rewrite/hamcrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,27 @@ recipeList:
notMatcher: empty
assertion: isNotEmpty

- org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ:
notMatcher: hasKey
assertion: doesNotContainKey
- org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ:
notMatcher: hasValue
assertion: doesNotContainValue
- org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ:
notMatcher: hasEntry
assertion: doesNotContainEntry
- org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ:
notMatcher: anEmptyMap
assertion: isNotEmpty

- org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ:
notMatcher: isEmptyOrNullString
assertion: isNotEmpty
- org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ:
notMatcher: emptyOrNullString
assertion: isNotEmpty

- org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ:
notMatcher: isA
assertion: isNotInstanceOf

Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,43 @@ void test(String key, String value) {
rewriteRun(java(before, after));
}

private static Stream<Arguments> notMapReplacements() {
return Stream.of(
Arguments.arguments("map1", "hasKey", "key", "doesNotContainKey"),
Arguments.arguments("map1", "hasValue", "value", "doesNotContainValue"),
Arguments.arguments("map1", "hasEntry", "key, value", "doesNotContainEntry"),
Arguments.arguments("map1", "anEmptyMap", "", "isNotEmpty")
);
}

@MethodSource("notMapReplacements")
@ParameterizedTest
void notMapReplacements(String actual, String hamcrestMatcher, String matcherArgs, String assertJAssertion) {
String importsBefore = """
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.%s;""".formatted(hamcrestMatcher);
String importsAfter = "import static org.assertj.core.api.Assertions.assertThat;";
//language=java
String template = """
import java.util.Map;
import org.junit.jupiter.api.Test;

%s

class ATest {
@Test
void test(String key, String value) {
Map<String, String> map1 = Map.of("a", "b", "c", "d");
%s
}
}
""";
String before = template.formatted(importsBefore, "assertThat(%s, not(%s(%s)));".formatted(actual, hamcrestMatcher, matcherArgs));
String after = template.formatted(importsAfter, "assertThat(%s).%s(%s);".formatted(actual, assertJAssertion, matcherArgs));
rewriteRun(java(before, after));
}

private static Stream<Arguments> notReplacements() {
return Stream.of(
Arguments.arguments("str1", "equalTo", "str2", "isNotEqualTo"),
Expand All @@ -480,7 +517,10 @@ private static Stream<Arguments> notReplacements() {
Arguments.arguments("str1", "equalToIgnoringCase", "str2", "isNotEqualToIgnoringCase"),
Arguments.arguments("str1", "equalToIgnoringWhiteSpace", "str2", "isNotEqualToIgnoringWhitespace"),
Arguments.arguments("str1", "blankString", "", "isNotBlank"),
Arguments.arguments("str1", "emptyString", "", "isNotEmpty")
Arguments.arguments("str1", "emptyString", "", "isNotEmpty"),
Arguments.arguments("str1", "isEmptyOrNullString", "", "isNotEmpty"),
Arguments.arguments("str1", "emptyOrNullString", "", "isNotEmpty"),
Arguments.arguments("str1", "isA", "String.class", "isNotInstanceOf")
);
}

Expand Down
Loading