MigrateAssertToMatcherAssert inspection: swap actual and expected parameters to preserve proper order + fix for statically imported assertion method IDEA-159137

This commit is contained in:
Dmitry Batkovich
2016-08-05 13:32:11 +03:00
parent 7e22a7e141
commit 6af0c778f6
2 changed files with 20 additions and 12 deletions
@@ -25,7 +25,6 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiTreeUtil;
@@ -51,11 +50,11 @@ public class MigrateAssertToMatcherAssertInspection extends LocalInspectionTool
private final static Map<String, Pair<String, String>> ASSERT_METHODS = new HashMap<String, Pair<String, String>>();
static {
ASSERT_METHODS.put("assertArrayEquals", Pair.create("$a$, $b$", "$a$, org.hamcrest.CoreMatchers.is($b$)"));
ASSERT_METHODS.put("assertEquals", Pair.create("$a$, $b$", "$a$, org.hamcrest.CoreMatchers.is($b$)"));
ASSERT_METHODS.put("assertNotEquals", Pair.create("$a$, $b$", "$a$, org.hamcrest.CoreMatchers.not(org.hamcrest.CoreMatchers.is($b$))"));
ASSERT_METHODS.put("assertSame", Pair.create("$a$, $b$", "$a$, org.hamcrest.CoreMatchersSame.sameInstance($b$)"));
ASSERT_METHODS.put("assertNotSame", Pair.create("$a$, $b$", "$a$, org.hamcrest.CoreMatchers.not(org.hamcrest.CoreMatchersSame.sameInstance($b$))"));
ASSERT_METHODS.put("assertArrayEquals", Pair.create("$expected$, $actual$", "$actual$, org.hamcrest.CoreMatchers.is($expected$)"));
ASSERT_METHODS.put("assertEquals", Pair.create("$expected$, $actual$", "$actual$, org.hamcrest.CoreMatchers.is($expected$)"));
ASSERT_METHODS.put("assertNotEquals", Pair.create("$expected$, $actual$", "$actual$, org.hamcrest.CoreMatchers.not(org.hamcrest.CoreMatchers.is($expected$))"));
ASSERT_METHODS.put("assertSame", Pair.create("$expected$, $actual$", "$actual$, org.hamcrest.CoreMatchersSame.sameInstance($expected$)"));
ASSERT_METHODS.put("assertNotSame", Pair.create("$expected$, $actual$", "$actual$, org.hamcrest.CoreMatchers.not(org.hamcrest.CoreMatchersSame.sameInstance($expected$))"));
ASSERT_METHODS.put("assertNotNull", Pair.create("$obj$", "$obj$, org.hamcrest.CoreMatchers.notNullValue()"));
ASSERT_METHODS.put("assertNull", Pair.create("$obj$", "$obj$, org.hamcrest.CoreMatchers.nullValue()"));
ASSERT_METHODS.put("assertTrue", Pair.create("$cond$", "$cond$, org.hamcrest.CoreMatchers.is(true)"));
@@ -73,6 +72,9 @@ public class MigrateAssertToMatcherAssertInspection extends LocalInspectionTool
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
if (JavaPsiFacade.getInstance(holder.getProject()).findClass("org.hamcrest.CoreMatchers", holder.getFile().getResolveScope()) == null) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
@@ -88,7 +90,7 @@ public class MigrateAssertToMatcherAssertInspection extends LocalInspectionTool
return;
}
holder
.registerProblem(expression, "Assert expression <code>#ref</code> can be replaced with 'assertThat' call #loc", new MyQuickFix());
.registerProblem(expression.getMethodExpression(), "Assert expression <code>#ref</code> can be replaced with 'assertThat' call #loc", new MyQuickFix());
}
};
}
@@ -110,7 +112,9 @@ public class MigrateAssertToMatcherAssertInspection extends LocalInspectionTool
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)descriptor.getPsiElement();
final PsiElement element = descriptor.getPsiElement();
if (element == null || !element.isValid() || !(element.getParent() instanceof PsiMethodCallExpression)) return;
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)element.getParent();
final PsiMethod method = methodCall.resolveMethod();
if (method == null) {
return;
@@ -171,7 +175,11 @@ public class MigrateAssertToMatcherAssertInspection extends LocalInspectionTool
}
final boolean hasMessage = hasMessage(method);
final String searchTemplate = "'Assert*." + method.getName() + "(" + (hasMessage ? "$msg$, " : "") + templatePair.getFirst() + ")";
final String replaceTemplate = "$Assert$.assertThat(" + (hasMessage ? "$msg$, " : "") + templatePair.getSecond() + ")";
final PsiClass containingClass = method.getContainingClass();
LOG.assertTrue(containingClass != null);
final String qualifier = containingClass.getQualifiedName();
LOG.assertTrue(qualifier != null);
final String replaceTemplate = qualifier + ".assertThat(" + (hasMessage ? "$msg$, " : "") + templatePair.getSecond() + ")";
return Pair.create(searchTemplate, replaceTemplate);
}
@@ -29,13 +29,13 @@ public class TestCase {
void m3(Collection c, Object o) {
Assert.assertThat(o, anyOf(c));
Assert.assertThat(c, is(o));
Assert.assertThat("msg", c, is(o));
Assert.assertThat(o, is(c));
Assert.assertThat("msg", o, is(c));
Assert.assertThat(c, notNullValue());
Assert.assertThat(c, nullValue());
}
void m(int[] a, int[] b) {
Assert.assertThat(a, is(b));
Assert.assertThat(b, is(a));
}
}