From 4febd80d66635eeb387377f61dd717bbf50ab7b8 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 11 Jul 2014 23:37:55 +0200 Subject: [PATCH] intention should insert static import when replacing assertNull() with assertEquals() --- ...ssertLiteralWithAssertEqualsIntention.java | 13 +++-- .../OutsideTestMethod.java | 8 ++++ .../OutsideTestMethod_after.java | 9 ++++ ...tLiteralWithAssertEqualsIntentionTest.java | 47 +++++++++++++++++++ 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/junit/replace_assert_literal_with_assert_equals/OutsideTestMethod.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/junit/replace_assert_literal_with_assert_equals/OutsideTestMethod_after.java create mode 100644 plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/junit/ReplaceAssertLiteralWithAssertEqualsIntentionTest.java diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/ReplaceAssertLiteralWithAssertEqualsIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/ReplaceAssertLiteralWithAssertEqualsIntention.java index 7bc8b2beec90..a3e318e5196a 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/ReplaceAssertLiteralWithAssertEqualsIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/junit/ReplaceAssertLiteralWithAssertEqualsIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2014 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. @@ -15,9 +15,9 @@ */ package com.siyeh.ipp.junit; -import com.intellij.codeInsight.AnnotationUtil; import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; +import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PsiTreeUtil; import com.siyeh.IntentionPowerPackBundle; import com.siyeh.ig.PsiReplacementUtil; @@ -73,11 +73,10 @@ public class ReplaceAssertLiteralWithAssertEqualsIntention extends MutablyNamedI @NonNls final StringBuilder newExpression = new StringBuilder(); final PsiElement qualifier = methodExpression.getQualifier(); if (qualifier == null) { - final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(call, PsiMethod.class); - if (containingMethod != null && AnnotationUtil.isAnnotated(containingMethod, "org.junit.Test", true)) { - if (!ImportUtils.addStaticImport("org.junit.Assert", "assertEquals", element)) { - newExpression.append("org.junit.Assert."); - } + final PsiClass containingClass = PsiTreeUtil.getParentOfType(element, PsiClass.class); + if (!InheritanceUtil.isInheritor(containingClass, "junit.framework.Assert") && + !ImportUtils.addStaticImport("org.junit.Assert", "assertEquals", element)) { + newExpression.append("org.junit.Assert."); } } else { diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/junit/replace_assert_literal_with_assert_equals/OutsideTestMethod.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/junit/replace_assert_literal_with_assert_equals/OutsideTestMethod.java new file mode 100644 index 000000000000..34c98500412e --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/junit/replace_assert_literal_with_assert_equals/OutsideTestMethod.java @@ -0,0 +1,8 @@ +import static org.junit.Assert.assertNull; + +class OutsideTestMethod { + + void m() { + assertNull("asdf"); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/junit/replace_assert_literal_with_assert_equals/OutsideTestMethod_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/junit/replace_assert_literal_with_assert_equals/OutsideTestMethod_after.java new file mode 100644 index 000000000000..0d6315f93c7b --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/junit/replace_assert_literal_with_assert_equals/OutsideTestMethod_after.java @@ -0,0 +1,9 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +class OutsideTestMethod { + + void m() { + assertEquals(null, "asdf"); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/junit/ReplaceAssertLiteralWithAssertEqualsIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/junit/ReplaceAssertLiteralWithAssertEqualsIntentionTest.java new file mode 100644 index 000000000000..00be3ec3e87f --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/junit/ReplaceAssertLiteralWithAssertEqualsIntentionTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2014 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.ipp.junit; + +import com.siyeh.IntentionPowerPackBundle; +import com.siyeh.ipp.IPPTestCase; + +/** + * @see com.siyeh.ipp.junit.ReplaceAssertLiteralWithAssertEqualsIntention + * @author Bas Leijdekkers + */ +public class ReplaceAssertLiteralWithAssertEqualsIntentionTest extends IPPTestCase { + + public void testOutsideTestMethod() { doTest(); } + + @Override + protected void setUp() throws Exception { + super.setUp(); + myFixture.addClass("package org.junit;" + + "class Assert {" + + " static public void assertNull(Object actual) {}" + + "}"); + } + + @Override + protected String getRelativePath() { + return "junit/replace_assert_literal_with_assert_equals"; + } + + @Override + protected String getIntentionName() { + return IntentionPowerPackBundle.message("replace.assert.literal.with.assert.equals.intention.name", "assertNull", "null"); + } +}