IG: use correct JUnit Assert class when simplifying (IDEA-116423)

This commit is contained in:
Bas Leijdekkers
2015-12-09 18:05:25 +01:00
parent afb1cf80d0
commit 37f833cad6
6 changed files with 146 additions and 25 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@ import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
@@ -63,7 +62,7 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
}
@Override
public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
public void doFix(Project project, ProblemDescriptor descriptor) {
final PsiElement methodNameIdentifier = descriptor.getPsiElement();
final PsiElement parent = methodNameIdentifier.getParent();
if (parent == null) {
@@ -87,7 +86,28 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
}
}
private static void replaceAssertWithFail(PsiMethodCallExpression callExpression) throws IncorrectOperationException {
private static void addStaticImportOrQualifier(String methodName, PsiMethodCallExpression originalMethodCall, StringBuilder out) {
final PsiReferenceExpression methodExpression = originalMethodCall.getMethodExpression();
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (qualifier == null) {
final PsiMethod method = originalMethodCall.resolveMethod();
if (method == null) {
return;
}
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null && "org.junit.Assert".equals(containingClass.getQualifiedName()) &&
!ImportUtils.addStaticImport("org.junit.Assert", methodName, originalMethodCall)) {
// add qualifier if old call was to JUnit4 method and adding static import failed
out.append("org.junit.Assert.");
}
}
else {
// apparently not statically imported, keep old qualifier in new assert call
out.append(qualifier.getText()).append('.');
}
}
private static void replaceAssertWithFail(PsiMethodCallExpression callExpression) {
final PsiMethod method = callExpression.resolveMethod();
if (method == null) {
return;
@@ -102,9 +122,7 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
message = null;
}
@NonNls final StringBuilder newExpression = new StringBuilder();
if (!ImportUtils.addStaticImport("org.junit.Assert", "fail", callExpression)) {
newExpression.append("org.junit.Assert.");
}
addStaticImportOrQualifier("fail", callExpression, newExpression);
newExpression.append("fail(");
if (message != null) {
newExpression.append(message.getText());
@@ -113,7 +131,7 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
PsiReplacementUtil.replaceExpressionAndShorten(callExpression, newExpression.toString());
}
private static void replaceAssertTrueWithAssertEquals(PsiMethodCallExpression callExpression) throws IncorrectOperationException {
private static void replaceAssertTrueWithAssertEquals(PsiMethodCallExpression callExpression) {
final PsiMethod method = callExpression.resolveMethod();
if (method == null) {
return;
@@ -159,9 +177,7 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
return;
}
@NonNls final StringBuilder newExpression = new StringBuilder();
if (!ImportUtils.addStaticImport("org.junit.Assert", "assertEquals", callExpression)) {
newExpression.append("org.junit.Assert.");
}
addStaticImportOrQualifier("assertEquals", callExpression, newExpression);
newExpression.append("assertEquals(");
if (message != null) {
newExpression.append(message.getText()).append(',');
@@ -174,7 +190,7 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
PsiReplacementUtil.replaceExpressionAndShorten(callExpression, newExpression.toString());
}
private static void replaceAssertWithAssertNull(PsiMethodCallExpression callExpression) throws IncorrectOperationException {
private static void replaceAssertWithAssertNull(PsiMethodCallExpression callExpression) {
final PsiMethod method = callExpression.resolveMethod();
if (method == null) {
return;
@@ -215,9 +231,7 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
else {
memberName = "assertNull";
}
if (!ImportUtils.addStaticImport("org.junit.Assert", memberName, callExpression)) {
newExpression.append("org.junit.Assert.");
}
addStaticImportOrQualifier(memberName, callExpression, newExpression);
newExpression.append(memberName).append('(');
if (message != null) {
newExpression.append(message.getText()).append(',');
@@ -226,7 +240,7 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
PsiReplacementUtil.replaceExpressionAndShorten(callExpression, newExpression.toString());
}
private static void replaceAssertWithAssertSame(PsiMethodCallExpression callExpression) throws IncorrectOperationException {
private static void replaceAssertWithAssertSame(PsiMethodCallExpression callExpression) {
final PsiMethod method = callExpression.resolveMethod();
if (method == null) {
return;
@@ -269,9 +283,7 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
else {
memberName = "assertSame";
}
if (!ImportUtils.addStaticImport("org.junit.Assert", memberName, callExpression)) {
newExpression.append("org.junit.Assert.");
}
addStaticImportOrQualifier(memberName, callExpression, newExpression);
newExpression.append(memberName).append('(');
if (message != null) {
newExpression.append(message.getText()).append(',');
@@ -280,9 +292,7 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
PsiReplacementUtil.replaceExpressionAndShorten(callExpression, newExpression.toString());
}
private static void replaceAssertEqualsWithAssertLiteral(
PsiMethodCallExpression callExpression)
throws IncorrectOperationException {
private static void replaceAssertEqualsWithAssertLiteral(PsiMethodCallExpression callExpression) {
final PsiMethod method = callExpression.resolveMethod();
if (method == null) {
return;
@@ -321,9 +331,7 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
final String uppercaseLiteralValue = Character.toUpperCase(literalValue.charAt(0)) + literalValue.substring(1);
@NonNls final StringBuilder newExpression = new StringBuilder();
@NonNls final String methodName = "assert" + uppercaseLiteralValue;
if (!ImportUtils.addStaticImport("org.junit.Assert", methodName, callExpression)) {
newExpression.append("org.junit.Assert.");
}
addStaticImportOrQualifier(methodName, callExpression, newExpression);
newExpression.append(methodName).append('(');
if (message != null) {
newExpression.append(message.getText()).append(',');
@@ -0,0 +1,8 @@
import junit.framework.*;
public class JUnit3TestCase extends TestCase {
public void testOne() {
assertEquals(1, 1);
}
}
@@ -0,0 +1,8 @@
import junit.framework.*;
public class JUnit3TestCase extends TestCase {
public void testOne() {
<caret>assertTrue(1 == 1);
}
}
@@ -0,0 +1,12 @@
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class JUnit4TestCase {
@Test
public void testOne() {
assertEquals(1, 1);
}
}
@@ -0,0 +1,11 @@
import org.junit.*;
import static org.junit.Assert.assertTrue;
public class JUnit4TestCase {
@Test
public void testOne() {
<caret>assertTrue(1 == 1);
}
}
@@ -0,0 +1,74 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.fixes.junit;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.IGQuickFixesTestCase;
import com.siyeh.ig.junit.SimplifiableJUnitAssertionInspection;
/**
* @author Bas Leijdekkers
*/
public class SimplifiableJUnitAssertionFixTest extends IGQuickFixesTestCase {
public void testJUnit3TestCase() {
doTest();
}
public void testJUnit4TestCase() {
doTest();
}
@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(new SimplifiableJUnitAssertionInspection());
myRelativePath = "junit/simplifiable_junit_assertion";
myDefaultHint = InspectionGadgetsBundle.message("simplify.junit.assertion.simplify.quickfix");
myFixture.addClass("package junit.framework;" +
"public abstract class TestCase extends Assert {" +
" protected void setUp() throws Exception {}" +
" protected void tearDown() throws Exception {}" +
"}");
myFixture.addClass("package junit.framework;" +
"public class Assert {" +
" public static void assertTrue(String message, boolean condition) {}" +
" public static void assertTrue(boolean condition) {}" +
" public static void assertEquals(String message, Object expected, Object actual) {}" +
" public static void assertEquals(Object expected, Object actual) {}" +
" public static void assertFalse(String message, boolean condition) {}" +
" public static void assertFalse(boolean condition) {}" +
"}");
myFixture.addClass("package org.junit;" +
"public class Assert {" +
" public static public void assertTrue(boolean condition) {}" +
" public static void assertEquals(boolean expected, boolean actual) {}" +
" public static void assertFalse(String message, boolean condition) {}" +
"}");
myFixture.addClass("package org.junit;" +
"import java.lang.annotation.ElementType;" +
"import java.lang.annotation.Retention;" +
"import java.lang.annotation.RetentionPolicy;" +
"import java.lang.annotation.Target;" +
"@Retention(RetentionPolicy.RUNTIME)" +
"@Target({ElementType.METHOD})" +
"public @interface Test {}");
}
}