Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Junit 4 matchers on AssertionsArgumentOrder #637

Merged
merged 4 commits into from
Nov 18, 2024
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 @@ -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(..)")
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
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