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
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public class HamcrestInstanceOfToJUnit5 extends Recipe {
@Getter
final String description = "Migrate from Hamcrest `instanceOf` and `isA` matcher to JUnit5 `assertInstanceOf` assertion.";

private static final MethodMatcher INSTANCE_OF_MATCHER = new MethodMatcher("org.hamcrest.Matchers instanceOf(..)");
private static final MethodMatcher IS_A_MATCHER = new MethodMatcher("org.hamcrest.Matchers isA(..)");
private static final MethodMatcher INSTANCE_OF_MATCHER = new MethodMatcher("org.hamcrest.*Matchers instanceOf(..)");
private static final MethodMatcher IS_A_MATCHER = new MethodMatcher("org.hamcrest.*Matchers isA(..)");
private static final MethodMatcher ASSERT_THAT_MATCHER = new MethodMatcher("org.hamcrest.MatcherAssert assertThat(.., org.hamcrest.Matcher)");

@Override
Expand Down Expand Up @@ -70,7 +70,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, Execution
}

J.MethodInvocation matcherInvocation = (J.MethodInvocation) hamcrestMatcher;
while ("not".equals(matcherInvocation.getSimpleName())) {
while (RemoveNotMatcherVisitor.NOT_MATCHER.matches(matcherInvocation)) {
maybeRemoveImport("org.hamcrest.Matchers.not");
maybeRemoveImport("org.hamcrest.CoreMatchers.not");
matcherInvocation = (J.MethodInvocation) new RemoveNotMatcherVisitor().visit(matcherInvocation, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
J.MethodInvocation matcherInvocation = (J.MethodInvocation) hamcrestMatcher;
maybeRemoveImport("org.hamcrest.MatcherAssert.assertThat");

while ("not".equals(matcherInvocation.getSimpleName())) {
while (RemoveNotMatcherVisitor.NOT_MATCHER.matches(matcherInvocation)) {
maybeRemoveImport("org.hamcrest.Matchers.not");
maybeRemoveImport("org.hamcrest.CoreMatchers.not");
matcherInvocation = (J.MethodInvocation) new RemoveNotMatcherVisitor().visit(matcherInvocation, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Objects;

class RemoveNotMatcherVisitor extends JavaIsoVisitor<ExecutionContext> {
static final MethodMatcher NOT_MATCHER = new MethodMatcher("org.hamcrest.Matchers not(..)");
static final MethodMatcher NOT_MATCHER = new MethodMatcher("org.hamcrest.*Matchers not(..)");

public static boolean getLogicalContext(J.MethodInvocation mi, ExecutionContext ctx) throws InvalidParameterException {
Object msg = ctx.getMessage(mi.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
package org.openrewrite.java.testing.hamcrest;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import java.util.concurrent.TimeUnit;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.TypeValidation.all;

Expand Down Expand Up @@ -81,6 +84,87 @@ void testInstance() {
);
}

@Test
@Timeout(value = 30, unit = TimeUnit.SECONDS, threadMode = Timeout.ThreadMode.SEPARATE_THREAD)
void notFromCoreMatchers() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Test;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.not;

class ATest {
private static final List<Integer> list = List.of();
@Test
void testInstance() {
assertThat(list, not(instanceOf(Integer.class)));
}
}
""",
"""
import org.junit.jupiter.api.Test;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;

class ATest {
private static final List<Integer> list = List.of();
@Test
void testInstance() {
assertFalse(Integer.class.isAssignableFrom(list.getClass()));
}
}
"""
)
);
}

@Test
void instanceOfAndIsAFromCoreMatchers() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Test;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.isA;

class ATest {
private static final List<Integer> list = List.of();
@Test
void testInstance() {
assertThat(list, instanceOf(Iterable.class));
assertThat(list, isA(Iterable.class));
}
}
""",
"""
import org.junit.jupiter.api.Test;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;

class ATest {
private static final List<Integer> list = List.of();
@Test
void testInstance() {
assertInstanceOf(Iterable.class, list);
assertInstanceOf(Iterable.class, list);
}
}
"""
)
);
}

@Test
void assertionsWithReason() {
//language=java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
package org.openrewrite.java.testing.hamcrest;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import java.util.concurrent.TimeUnit;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.TypeValidation.all;

Expand Down Expand Up @@ -158,6 +161,46 @@ void testEquals() {
);
}

@Test
@Timeout(value = 30, unit = TimeUnit.SECONDS, threadMode = Timeout.ThreadMode.SEPARATE_THREAD)
void notFromCoreMatchers() {
//language=java
rewriteRun(
spec -> spec.typeValidationOptions(all().immutableExecutionContext(false)),
java(
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;

class ATest {
@Test
void testEquals() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1, not(equalTo(str2)));
}
}
""",
"""
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotEquals;

class ATest {
@Test
void testEquals() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertNotEquals(str1, str2);
}
}
"""
)
);
}

@Test
void greaterThan() {
//language=java
Expand Down
Loading