From 37f833cad61aed4baa801f0e4afb4ec292249b4e Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 9 Dec 2015 17:30:57 +0100 Subject: [PATCH] IG: use correct JUnit Assert class when simplifying (IDEA-116423) --- .../SimplifiableJUnitAssertionInspection.java | 58 ++++++++------- .../JUnit3TestCase.after.java | 8 ++ .../JUnit3TestCase.java | 8 ++ .../JUnit4TestCase.after.java | 12 +++ .../JUnit4TestCase.java | 11 +++ .../SimplifiableJUnitAssertionFixTest.java | 74 +++++++++++++++++++ 6 files changed, 146 insertions(+), 25 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit3TestCase.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit3TestCase.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit4TestCase.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit4TestCase.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/junit/SimplifiableJUnitAssertionFixTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/SimplifiableJUnitAssertionInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/SimplifiableJUnitAssertionInspection.java index b972890d8055..eaa051683395 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/SimplifiableJUnitAssertionInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/SimplifiableJUnitAssertionInspection.java @@ -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(','); diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit3TestCase.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit3TestCase.after.java new file mode 100644 index 000000000000..80672a1a88f3 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit3TestCase.after.java @@ -0,0 +1,8 @@ +import junit.framework.*; + +public class JUnit3TestCase extends TestCase { + + public void testOne() { + assertEquals(1, 1); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit3TestCase.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit3TestCase.java new file mode 100644 index 000000000000..c6493a4204a2 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit3TestCase.java @@ -0,0 +1,8 @@ +import junit.framework.*; + +public class JUnit3TestCase extends TestCase { + + public void testOne() { + assertTrue(1 == 1); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit4TestCase.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit4TestCase.after.java new file mode 100644 index 000000000000..5733b2474fa0 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit4TestCase.after.java @@ -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); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit4TestCase.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit4TestCase.java new file mode 100644 index 000000000000..2fe005dd6323 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/simplifiable_junit_assertion/JUnit4TestCase.java @@ -0,0 +1,11 @@ +import org.junit.*; + +import static org.junit.Assert.assertTrue; + +public class JUnit4TestCase { + + @Test + public void testOne() { + assertTrue(1 == 1); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/junit/SimplifiableJUnitAssertionFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/junit/SimplifiableJUnitAssertionFixTest.java new file mode 100644 index 000000000000..382ca346d3ce --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/junit/SimplifiableJUnitAssertionFixTest.java @@ -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 {}"); + } +}