mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
assertEquals can be replaced with assertSame: introduce junit 5; extract common fix
This commit is contained in:
+1
-1
@@ -1791,7 +1791,7 @@ unnecessarily.qualified.statically.imported.element.display.name=Unnecessarily q
|
||||
unnecessarily.qualified.statically.imported.element.problem.descriptor=Statically imported element ''{0}'' is unnecessarily qualified with <code>#ref</code> #loc
|
||||
unnecessarily.qualified.statically.imported.element.quickfix=Remove unnecessary qualifier
|
||||
ignore.instanceof.on.library.classes=Ignore instanceof on library classes
|
||||
assertequals.may.be.assertsame.quickfix=Replace with 'assertSame()'
|
||||
replace.assertequals.quickfix=Replace with ''{0}''
|
||||
for.can.be.foreach.option=Report indexed 'java.util.List' loops
|
||||
for.can.be.foreach.option2=Do not report iterations over untyped collections
|
||||
cast.conflicts.with.instanceof.quickfix1=Replace ''{0}'' with ''{1}'' in cast
|
||||
|
||||
+2
-41
@@ -15,19 +15,12 @@
|
||||
*/
|
||||
package com.siyeh.ig.junit;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.PsiReplacementUtil;
|
||||
import com.siyeh.ig.psiutils.ImportUtils;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AssertEqualsCalledOnArrayInspection extends BaseInspection {
|
||||
@@ -47,39 +40,7 @@ public class AssertEqualsCalledOnArrayInspection extends BaseInspection {
|
||||
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new AssertEqualsCalledOnArrayFix((String)infos[0]);
|
||||
}
|
||||
|
||||
private static class AssertEqualsCalledOnArrayFix extends InspectionGadgetsFix {
|
||||
|
||||
private final String myAssertClassName;
|
||||
|
||||
public AssertEqualsCalledOnArrayFix(String assertClassName) {
|
||||
myAssertClassName = assertClassName;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return InspectionGadgetsBundle.message("assertequals.called.on.arrays.quickfix");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
|
||||
final PsiElement methodNameIdentifier = descriptor.getPsiElement();
|
||||
final PsiElement parent = methodNameIdentifier.getParent();
|
||||
if (!(parent instanceof PsiReferenceExpression)) {
|
||||
return;
|
||||
}
|
||||
final PsiReferenceExpression methodExpression = (PsiReferenceExpression)parent;
|
||||
final PsiExpression qualifier = methodExpression.getQualifierExpression();
|
||||
if (qualifier == null && ImportUtils.addStaticImport(myAssertClassName, "assertArrayEquals", methodExpression)) {
|
||||
PsiReplacementUtil.replaceExpression(methodExpression, "assertArrayEquals");
|
||||
}
|
||||
else {
|
||||
PsiReplacementUtil.replaceExpression(methodExpression, myAssertClassName + ".assertArrayEquals");
|
||||
}
|
||||
}
|
||||
return new ReplaceAssertEqualsFix("assertArrayEquals");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -104,7 +65,7 @@ public class AssertEqualsCalledOnArrayInspection extends BaseInspection {
|
||||
if (!(type1 instanceof PsiArrayType) || !(type2 instanceof PsiArrayType)) {
|
||||
return;
|
||||
}
|
||||
registerMethodCallError(expression, assertEqualsHint.getAssertClassName());
|
||||
registerMethodCallError(expression);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-8
@@ -25,16 +25,10 @@ import org.jetbrains.annotations.NonNls;
|
||||
public class AssertEqualsHint {
|
||||
private final int myArgIndex;
|
||||
private final PsiMethod myMethod;
|
||||
private final String myClassName;
|
||||
|
||||
private AssertEqualsHint(int index, PsiMethod method, String className) {
|
||||
private AssertEqualsHint(int index, PsiMethod method) {
|
||||
myArgIndex = index;
|
||||
myMethod = method;
|
||||
myClassName = className;
|
||||
}
|
||||
|
||||
public String getAssertClassName() {
|
||||
return myClassName;
|
||||
}
|
||||
|
||||
public int getArgIndex() {
|
||||
@@ -60,6 +54,7 @@ public class AssertEqualsHint {
|
||||
InheritanceUtil.isInheritor(containingClass, "org.testng.Assert");
|
||||
if (!InheritanceUtil.isInheritor(containingClass, JUnitCommonClassNames.JUNIT_FRAMEWORK_ASSERT) &&
|
||||
!InheritanceUtil.isInheritor(containingClass, JUnitCommonClassNames.ORG_JUNIT_ASSERT) &&
|
||||
!InheritanceUtil.isInheritor(containingClass, JUnitCommonClassNames.JUNIT_FRAMEWORK_TEST_CASE) &&
|
||||
!InheritanceUtil.isInheritor(containingClass, "org.testng.AssertJUnit") &&
|
||||
!messageOnLastPosition) {
|
||||
return null;
|
||||
@@ -85,7 +80,7 @@ public class AssertEqualsHint {
|
||||
}
|
||||
argumentIndex = 0;
|
||||
}
|
||||
return new AssertEqualsHint(argumentIndex, method, messageOnLastPosition ? JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_ASSERTIONS : JUnitCommonClassNames.ORG_JUNIT_ASSERT);
|
||||
return new AssertEqualsHint(argumentIndex, method);
|
||||
}
|
||||
|
||||
public static String areExpectedActualTypesCompatible(PsiMethodCallExpression expression) {
|
||||
|
||||
+6
-69
@@ -15,17 +15,11 @@
|
||||
*/
|
||||
package com.siyeh.ig.junit;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.PsiReplacementUtil;
|
||||
import com.siyeh.ig.psiutils.ImportUtils;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AssertEqualsMayBeAssertSameInspection extends BaseInspection {
|
||||
@@ -44,50 +38,7 @@ public class AssertEqualsMayBeAssertSameInspection extends BaseInspection {
|
||||
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new AssertEqualsMayBeAssertSameFix();
|
||||
}
|
||||
|
||||
private static class AssertEqualsMayBeAssertSameFix extends InspectionGadgetsFix {
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return InspectionGadgetsBundle.message("assertequals.may.be.assertsame.quickfix");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
final PsiElement parent = element.getParent();
|
||||
if (!(parent instanceof PsiReferenceExpression)) {
|
||||
return;
|
||||
}
|
||||
final PsiReferenceExpression methodExpression = (PsiReferenceExpression)parent;
|
||||
final PsiElement grandParent = methodExpression.getParent();
|
||||
if (!(grandParent instanceof PsiMethodCallExpression)) {
|
||||
return;
|
||||
}
|
||||
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent;
|
||||
final PsiMethod method = methodCallExpression.resolveMethod();
|
||||
if (method == null) {
|
||||
return;
|
||||
}
|
||||
final PsiClass containingClass = method.getContainingClass();
|
||||
if (containingClass == null) {
|
||||
return;
|
||||
}
|
||||
final String className = containingClass.getQualifiedName();
|
||||
if (className == null) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression qualifier = methodExpression.getQualifierExpression();
|
||||
if (qualifier == null && ImportUtils.addStaticImport(className, "assertSame", methodExpression)) {
|
||||
PsiReplacementUtil.replaceExpression(methodExpression, "assertSame");
|
||||
}
|
||||
else {
|
||||
PsiReplacementUtil.replaceExpression(methodExpression, className + ".assertSame");
|
||||
}
|
||||
}
|
||||
return new ReplaceAssertEqualsFix("assertSame");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -100,9 +51,8 @@ public class AssertEqualsMayBeAssertSameInspection extends BaseInspection {
|
||||
@Override
|
||||
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
|
||||
super.visitMethodCallExpression(expression);
|
||||
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
|
||||
@NonNls final String name = methodExpression.getReferenceName();
|
||||
if (!"assertEquals".equals(name)) {
|
||||
final AssertEqualsHint assertEqualsHint = AssertEqualsHint.create(expression);
|
||||
if (assertEqualsHint == null) {
|
||||
return;
|
||||
}
|
||||
final PsiExpressionList argumentList = expression.getArgumentList();
|
||||
@@ -110,25 +60,12 @@ public class AssertEqualsMayBeAssertSameInspection extends BaseInspection {
|
||||
if (arguments.length != 3 && arguments.length != 2) {
|
||||
return;
|
||||
}
|
||||
final PsiMethod method = expression.resolveMethod();
|
||||
if (method == null) {
|
||||
return;
|
||||
}
|
||||
final PsiClass aClass = method.getContainingClass();
|
||||
if (aClass == null) {
|
||||
return;
|
||||
}
|
||||
final String qualifiedName = aClass.getQualifiedName();
|
||||
if (!JUnitCommonClassNames.ORG_JUNIT_ASSERT.equals(qualifiedName) && !JUnitCommonClassNames.JUNIT_FRAMEWORK_ASSERT
|
||||
.equals(qualifiedName) &&
|
||||
!"junit.framework.TestCase".equals(qualifiedName)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression argument1 = arguments[arguments.length - 2];
|
||||
final int argIndex = assertEqualsHint.getArgIndex();
|
||||
final PsiExpression argument1 = arguments[argIndex];
|
||||
if (!couldBeAssertSameArgument(argument1)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression argument2 = arguments[arguments.length - 1];
|
||||
final PsiExpression argument2 = arguments[argIndex + 1];
|
||||
if (!couldBeAssertSameArgument(argument2)) {
|
||||
return;
|
||||
}
|
||||
|
||||
+1
-1
@@ -83,7 +83,7 @@ public class JUnitAbstractTestClassNamingConventionInspectionBase extends Conven
|
||||
return;
|
||||
}
|
||||
if (!InheritanceUtil.isInheritor(aClass,
|
||||
"junit.framework.TestCase")) {
|
||||
JUnitCommonClassNames.JUNIT_FRAMEWORK_TEST_CASE)) {
|
||||
return;
|
||||
}
|
||||
final String name = aClass.getName();
|
||||
|
||||
+1
@@ -19,4 +19,5 @@ public class JUnitCommonClassNames {
|
||||
public static final String ORG_JUNIT_ASSERT = "org.junit.Assert";
|
||||
public static final String JUNIT_FRAMEWORK_ASSERT = "junit.framework.Assert";
|
||||
public static final String ORG_JUNIT_JUPITER_API_ASSERTIONS = "org.junit.jupiter.api.Assertions";
|
||||
public static final String JUNIT_FRAMEWORK_TEST_CASE = "junit.framework.TestCase";
|
||||
}
|
||||
|
||||
+1
-1
@@ -83,7 +83,7 @@ public class JUnitTestClassNamingConventionInspectionBase extends ConventionInsp
|
||||
return;
|
||||
}
|
||||
if (!InheritanceUtil.isInheritor(aClass,
|
||||
"junit.framework.TestCase")) {
|
||||
JUnitCommonClassNames.JUNIT_FRAMEWORK_TEST_CASE)) {
|
||||
if (!hasJUnit4TestMethods(aClass)) {
|
||||
return;
|
||||
}
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ public class MalformedSetUpTearDownInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
final PsiClass targetClass = method.getContainingClass();
|
||||
if (!InheritanceUtil.isInheritor(targetClass, "junit.framework.TestCase")) {
|
||||
if (!InheritanceUtil.isInheritor(targetClass, JUnitCommonClassNames.JUNIT_FRAMEWORK_TEST_CASE)) {
|
||||
return;
|
||||
}
|
||||
if (method.getParameterList().getParametersCount() != 0 ||
|
||||
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2000-2016 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.junit;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.PsiReplacementUtil;
|
||||
import com.siyeh.ig.psiutils.ImportUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
class ReplaceAssertEqualsFix extends InspectionGadgetsFix {
|
||||
private final String myMethodName;
|
||||
|
||||
public ReplaceAssertEqualsFix(String methodName) {
|
||||
myMethodName = methodName;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return InspectionGadgetsBundle.message("replace.assertequals.quickfix", myMethodName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
final PsiElement parent = element.getParent();
|
||||
if (!(parent instanceof PsiReferenceExpression)) {
|
||||
return;
|
||||
}
|
||||
final PsiReferenceExpression methodExpression = (PsiReferenceExpression)parent;
|
||||
final PsiElement grandParent = methodExpression.getParent();
|
||||
if (!(grandParent instanceof PsiMethodCallExpression)) {
|
||||
return;
|
||||
}
|
||||
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent;
|
||||
final PsiMethod method = methodCallExpression.resolveMethod();
|
||||
if (method == null) {
|
||||
return;
|
||||
}
|
||||
final PsiClass containingClass = method.getContainingClass();
|
||||
if (containingClass == null) {
|
||||
return;
|
||||
}
|
||||
final String className = containingClass.getQualifiedName();
|
||||
if (className == null) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression qualifier = methodExpression.getQualifierExpression();
|
||||
if (qualifier == null && ImportUtils.addStaticImport(className, myMethodName, methodExpression)) {
|
||||
PsiReplacementUtil.replaceExpression(methodExpression, myMethodName);
|
||||
}
|
||||
else {
|
||||
PsiReplacementUtil.replaceExpression(methodExpression, StringUtil.getQualifiedName(className, myMethodName));
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -721,7 +721,8 @@ public class SimplifiableJUnitAssertionInspection extends BaseInspection {
|
||||
return false;
|
||||
}
|
||||
final String qualifiedName = targetClass.getQualifiedName();
|
||||
return JUnitCommonClassNames.JUNIT_FRAMEWORK_ASSERT.equals(qualifiedName) || "junit.framework.TestCase".equals(qualifiedName) ||
|
||||
return JUnitCommonClassNames.JUNIT_FRAMEWORK_ASSERT.equals(qualifiedName) || JUnitCommonClassNames.JUNIT_FRAMEWORK_TEST_CASE
|
||||
.equals(qualifiedName) ||
|
||||
JUnitCommonClassNames.ORG_JUNIT_ASSERT.equals(qualifiedName);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -66,7 +66,7 @@ public class StaticSuiteInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
if (!InheritanceUtil.isInheritor(aClass,
|
||||
"junit.framework.TestCase")) {
|
||||
JUnitCommonClassNames.JUNIT_FRAMEWORK_TEST_CASE)) {
|
||||
return;
|
||||
}
|
||||
final PsiParameterList parameterList = method.getParameterList();
|
||||
|
||||
+1
-1
@@ -59,7 +59,7 @@ public class SuperTearDownInFinallyInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
final PsiClass containingClass = method.getContainingClass();
|
||||
if (!InheritanceUtil.isInheritor(containingClass, "junit.framework.TestCase")) {
|
||||
if (!InheritanceUtil.isInheritor(containingClass, JUnitCommonClassNames.JUNIT_FRAMEWORK_TEST_CASE)) {
|
||||
return;
|
||||
}
|
||||
final PsiTryStatement tryStatement =
|
||||
|
||||
+1
-1
@@ -84,7 +84,7 @@ public class TestCaseWithNoTestMethodsInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
if (!InheritanceUtil.isInheritor(aClass,
|
||||
"junit.framework.TestCase")) {
|
||||
JUnitCommonClassNames.JUNIT_FRAMEWORK_TEST_CASE)) {
|
||||
return;
|
||||
}
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
|
||||
+1
-1
@@ -80,7 +80,7 @@ public class UseOfObsoleteAssertInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
final String name = containingClass.getQualifiedName();
|
||||
if (JUnitCommonClassNames.JUNIT_FRAMEWORK_ASSERT.equals(name) || "junit.framework.TestCase".equals(name)) {
|
||||
if (JUnitCommonClassNames.JUNIT_FRAMEWORK_ASSERT.equals(name) || JUnitCommonClassNames.JUNIT_FRAMEWORK_TEST_CASE.equals(name)) {
|
||||
registerMethodCallError(expression, name);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -24,6 +24,7 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.siyeh.ig.junit.JUnitCommonClassNames;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -99,7 +100,7 @@ public class TestUtils {
|
||||
}
|
||||
|
||||
public static boolean isJUnitTestClass(@Nullable PsiClass targetClass) {
|
||||
return targetClass != null && InheritanceUtil.isInheritor(targetClass, "junit.framework.TestCase");
|
||||
return targetClass != null && InheritanceUtil.isInheritor(targetClass, JUnitCommonClassNames.JUNIT_FRAMEWORK_TEST_CASE);
|
||||
}
|
||||
|
||||
public static boolean isJUnit4TestClass(@Nullable PsiClass aClass, boolean runWithIsTestClass) {
|
||||
|
||||
+1
-1
@@ -43,7 +43,7 @@ public class AssertEqualsCalledOnArrayInspectionTest extends IGQuickFixesTestCas
|
||||
}
|
||||
|
||||
private void doFixTest() {
|
||||
doTest(getTestName(true), InspectionGadgetsBundle.message("assertequals.called.on.arrays.quickfix"));
|
||||
doTest(getTestName(true), InspectionGadgetsBundle.message("replace.assertequals.quickfix", "assertArrayEquals"));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user