Skip to content

Commit

Permalink
Add support for Junit 4 matchers on AssertionsArgumentOrder (#637)
Browse files Browse the repository at this point in the history
* Add support for Junit 4 matchers

* Rework handling of junit 4 matchers starting with a message. Added support for junit4 assertArrayEquals and assertNull.

* Remove excess newline between tests

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
adambir and timtebeek authored Nov 18, 2024
1 parent 93d76cf commit b20da71
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,21 @@ public class AssertionsArgumentOrder extends Recipe {
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 assertArrayEquals(..)")
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 assertArrayEquals(..)"),
new MethodMatcher("org.junit.Assert assertSame(..)"),
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 @@ -56,6 +69,8 @@ public class AssertionsArgumentOrder extends Recipe {

static {
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 @@ -97,7 +112,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu

final Expression expected;
final Expression actual;
if (isJupiterAssertion(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 @@ -174,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;
}
}
}
Loading

0 comments on commit b20da71

Please sign in to comment.