insert static import when replacing assertEquals() with intention

This commit is contained in:
Bas Leijdekkers
2014-07-12 18:19:29 +02:00
parent 591d723efb
commit 4dd5b62584
4 changed files with 70 additions and 7 deletions
@@ -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,8 +15,8 @@
*/
package com.siyeh.ipp.junit;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.PsiReplacementUtil;
@@ -86,11 +86,10 @@ public class ReplaceAssertEqualsWithAssertLiteralIntention extends MutablyNamedI
final PsiElement qualifier = expression.getQualifier();
@NonNls final StringBuilder newExpression = new StringBuilder();
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", assertString, 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", assertString, element)) {
newExpression.append("org.junit.Assert.");
}
}
else {
@@ -0,0 +1,8 @@
import static org.junit.Assert.assertEquals;
class OutsideTestMethod {
void m() {
<caret>assertEquals("asdf", null);
}
}
@@ -0,0 +1,9 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
class OutsideTestMethod {
void m() {
assertNull("asdf");
}
}
@@ -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 ReplaceAssertEqualsWithAssertLiteralIntention
* @author Bas Leijdekkers
*/
public class ReplaceAssertEqualsWithAssertLiteralIntentionTest extends IPPTestCase {
public void testOutsideTestMethod() { doTest(); }
@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.addClass("package org.junit;" +
"class Assert {" +
" static public void assertEquals(Object expected, Object actual) {}" +
"}");
}
@Override
protected String getRelativePath() {
return "junit/replace_assert_equals_with_assert_literal";
}
@Override
protected String getIntentionName() {
return IntentionPowerPackBundle.message("replace.assert.equals.with.assert.literal.intention.name", "assertNull");
}
}