Skip to content

Commit

Permalink
Rework handling of junit 4 matchers starting with a message. Added su…
Browse files Browse the repository at this point in the history
…pport for junit4 assertArrayEquals and assertNull.
  • Loading branch information
adambir committed Nov 14, 2024
1 parent 9c21b64 commit 8c41ec2
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,26 @@

public class AssertionsArgumentOrder extends Recipe {

private static final MethodMatcher[] junitAssertionsMatchers = new MethodMatcher[]{
private static final MethodMatcher[] jupiterAssertionMatchers = new MethodMatcher[]{
new MethodMatcher("org.junit.jupiter.api.Assertions assertArrayEquals(..)"),
new MethodMatcher("org.junit.jupiter.api.Assertions assertEquals(..)"),
new MethodMatcher("org.junit.jupiter.api.Assertions assertNotEquals(..)"),
new MethodMatcher("org.junit.jupiter.api.Assertions assertSame(..)"),
new MethodMatcher("org.junit.jupiter.api.Assertions assertNotSame(..)"),
new MethodMatcher("org.junit.jupiter.api.Assertions assertNotSame(..)")
};

private static final MethodMatcher[] junitAssertMatchers = new MethodMatcher[]{
new MethodMatcher("org.junit.Assert assertEquals(..)"),
new MethodMatcher("org.junit.Assert assertEquals(..)"),
new MethodMatcher("org.junit.Assert assertNotEquals(..)"),
new MethodMatcher("org.junit.Assert assertArrayEquals(..)"),
new MethodMatcher("org.junit.Assert assertSame(..)"),
new MethodMatcher("org.junit.Assert assertNotSame(..)")
new MethodMatcher("org.junit.Assert assertNotSame(..)"),
new MethodMatcher("org.junit.Assert assert*Null(String, Object)")
};

private static final MethodMatcher[] junitAssertWithMessageMatchers = new MethodMatcher[]{
new MethodMatcher("org.junit.Assert assertEquals(String, ..)"),
new MethodMatcher("org.junit.Assert assertArrayEquals(String, ..)")
};
private static final MethodMatcher jupiterAssertIterableEqualsMatcher = new MethodMatcher("org.junit.jupiter.api.Assertions assertIterableEquals(..)");

Expand All @@ -58,7 +68,9 @@ public class AssertionsArgumentOrder extends Recipe {
private static final TreeVisitor<?, ExecutionContext> precondition;

static {
List<MethodMatcher> matchers = new ArrayList<>(Arrays.asList(junitAssertionsMatchers));
List<MethodMatcher> matchers = new ArrayList<>(Arrays.asList(jupiterAssertionMatchers));
matchers.addAll(Arrays.asList(junitAssertMatchers));
matchers.addAll(Arrays.asList(junitAssertWithMessageMatchers));
matchers.add(jupiterAssertIterableEqualsMatcher);
matchers.add(jupiterAssertNullMatcher);
matchers.addAll(Arrays.asList(testNgMatcher));
Expand Down Expand Up @@ -100,7 +112,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu

final Expression expected;
final Expression actual;
if (isJunitAssertion(mi)) {
if (isJunitAssertEqualsWithMessage(mi)) {
expected = mi.getArguments().get(1);
actual = mi.getArguments().get(2);
} else if (isJunitAssertion(mi) || isJupiterAssertion(mi)) {
expected = mi.getArguments().get(0);
actual = mi.getArguments().get(1);
} else if (isTestNgAssertion(mi)) {
Expand Down Expand Up @@ -160,8 +175,8 @@ private boolean isConstant(Expression expression, J.MethodInvocation mi) {
return false;
}

private boolean isJunitAssertion(J.MethodInvocation mi) {
for (MethodMatcher assertionMethodMatcher : junitAssertionsMatchers) {
private boolean isJupiterAssertion(J.MethodInvocation mi) {
for (MethodMatcher assertionMethodMatcher : jupiterAssertionMatchers) {
if (assertionMethodMatcher.matches(mi)) {
return true;
}
Expand All @@ -177,5 +192,23 @@ private boolean isTestNgAssertion(J.MethodInvocation mi) {
}
return false;
}

private boolean isJunitAssertion(J.MethodInvocation mi) {
for (MethodMatcher assertionMethodMatcher : junitAssertMatchers) {
if (assertionMethodMatcher.matches(mi)) {
return true;
}
}
return false;
}

private boolean isJunitAssertEqualsWithMessage(J.MethodInvocation mi) {
for (MethodMatcher actExpMatcher : junitAssertWithMessageMatchers) {
if (actExpMatcher.matches(mi)) {
return true;
}
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ String result() {

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/636")
void junit4AssertEqualsHavingPrimitiveArg() {
void junit4AssertEqualsHavingStringArg() {
rewriteRun(
//language=java
java(
Expand All @@ -83,8 +83,9 @@ void junit4AssertEqualsHavingPrimitiveArg() {
class MyTest {
void someMethod() {
assertEquals(result(), "result");
assertEquals(result(), "result", "message");
assertEquals(0L, 1L);
assertEquals("result", result());
assertEquals("message", result(), "result");
assertEquals("message", "result", result());
}
String result() {
return "result";
Expand All @@ -97,8 +98,9 @@ String result() {
class MyTest {
void someMethod() {
assertEquals("result", result());
assertEquals("result", result(), "message");
assertEquals(0L, 1L);
assertEquals("result", result());
assertEquals("message", "result", result());
assertEquals("message", "result", result());
}
String result() {
return "result";
Expand All @@ -109,9 +111,53 @@ String result() {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/636")
void junit4AssertEqualsHavingPrimitiveArg() {
rewriteRun(
//language=java
java(
"""
import static org.junit.Assert.assertEquals;
class MyTest {
void someMethod() {
assertEquals(0L, 1L);
assertEquals(1L, 0L);
assertEquals(longResult(), 0L);
assertEquals(0L, longResult());
assertEquals("message", 0L, longResult());
assertEquals("message", longResult(), 0L);
}
long longResult() {
return 0L;
}
}
""",
"""
import static org.junit.Assert.assertEquals;
class MyTest {
void someMethod() {
assertEquals(0L, 1L);
assertEquals(1L, 0L);
assertEquals(0L, longResult());
assertEquals(0L, longResult());
assertEquals("message", 0L, longResult());
assertEquals("message", 0L, longResult());
}
long longResult() {
return 0L;
}
}
"""
)
);
}


@Test
void junitAssertNullAndAssertNotNull() {
void jupiterAssertNullAndAssertNotNull() {
rewriteRun(
//language=java
java(
Expand Down Expand Up @@ -151,6 +197,48 @@ String result() {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/636")
void junitAssertNullAndAssertNotNull() {
rewriteRun(
//language=java
java(
"""
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
class MyTest {
void someMethod() {
assertNull(result(), "message");
assertNull("message", result());
assertNotNull(result(), "message");
assertNotNull("message", result());
}
String result() {
return "result";
}
}
""",
"""
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
class MyTest {
void someMethod() {
assertNull("message", result());
assertNull("message", result());
assertNotNull("message", result());
assertNotNull("message", result());
}
String result() {
return "result";
}
}
"""
)
);
}

@Test
void jupiterAssertSameNotSame() {
rewriteRun(
Expand Down Expand Up @@ -293,6 +381,46 @@ String[] result() {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/636")
void junitAssertArrayEquals() {
rewriteRun(
//language=java
java(
"""
import static org.junit.Assert.assertArrayEquals;
class MyTest {
void someMethod() {
assertArrayEquals(result(), new String[]{""});
assertArrayEquals(new String[]{""}, result());
assertArrayEquals("message", new String[]{""}, result());
assertArrayEquals("message", result(), new String[]{""});
}
String[] result() {
return null;
}
}
""",
"""
import static org.junit.Assert.assertArrayEquals;
class MyTest {
void someMethod() {
assertArrayEquals(new String[]{""}, result());
assertArrayEquals(new String[]{""}, result());
assertArrayEquals("message", new String[]{""}, result());
assertArrayEquals("message", new String[]{""}, result());
}
String[] result() {
return null;
}
}
"""
)
);
}

@Test
void junitIterableEquals() {
rewriteRun(
Expand Down

0 comments on commit 8c41ec2

Please sign in to comment.