junit 4 -> junit 5: replace with static imports after refactoring (IDEA-179927)

if replace simultaneously, all old assertions have to be checked if they have no conflicts with current static import
This commit is contained in:
Anna.Kozlova
2017-10-18 10:47:10 +02:00
parent b014132f00
commit 14d7f57946
12 changed files with 105 additions and 26 deletions
@@ -23,6 +23,7 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
@@ -110,17 +111,17 @@ public class AddOnDemandStaticImportAction extends BaseElementAtCaretIntentionAc
return classToImport != null;
}
public static void invoke(final Project project, PsiFile file, final Editor editor, PsiElement element) {
public static boolean invoke(final Project project, PsiFile file, final Editor editor, PsiElement element) {
final PsiJavaCodeReferenceElement refExpr = (PsiJavaCodeReferenceElement)element.getParent();
final PsiClass aClass = (PsiClass)refExpr.resolve();
if (aClass == null) {
return;
return false;
}
final PsiClass containingClass = PsiUtil.getTopLevelClass(refExpr);
if (aClass != containingClass) {
PsiImportList importList = ((PsiJavaFile)file).getImportList();
if (importList == null) {
return;
return false;
}
boolean alreadyImported = false;
for (PsiImportStaticStatement statement : importList.getImportStaticStatements()) {
@@ -138,6 +139,7 @@ public class AddOnDemandStaticImportAction extends BaseElementAtCaretIntentionAc
}
}
Ref<Boolean> conflict = new Ref<>(false);
List<PsiFile> roots = file.getViewProvider().getAllFiles();
for (final PsiFile root : roots) {
PsiElement copy = root.copy();
@@ -163,6 +165,9 @@ public class AddOnDemandStaticImportAction extends BaseElementAtCaretIntentionAc
if (manager.areElementsEquivalent(after, resolved)) {
expressionToDequalifyOffsets.add(expression.getTextRange().getStartOffset() + delta);
}
else {
conflict.set(true);
}
}
catch (IncorrectOperationException e) {
LOG.error(e);
@@ -191,6 +196,7 @@ public class AddOnDemandStaticImportAction extends BaseElementAtCaretIntentionAc
return true;
});
}
return conflict.get();
}
@Override
@@ -30,7 +30,6 @@ import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.psiutils.ImportUtils;
import com.siyeh.ig.testFrameworks.AssertHint;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -190,11 +189,8 @@ public class JUnit5AssertionsConverterInspection extends BaseInspection {
}
PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (qualifier != null || !ImportUtils.addStaticImport(qualifiedName, methodName, methodExpression)) {
methodExpression.setQualifierExpression(JavaPsiFacade.getElementFactory(project).createReferenceExpression(newAssertClass));
JavaCodeStyleManager.getInstance(project).shortenClassReferences(methodExpression);
}
methodExpression.setQualifierExpression(JavaPsiFacade.getElementFactory(project).createReferenceExpression(newAssertClass));
JavaCodeStyleManager.getInstance(project).shortenClassReferences(methodExpression);
}
@Nls
@@ -17,6 +17,7 @@ package com.siyeh.ig.junit;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.TestFrameworks;
import com.intellij.codeInsight.intention.impl.AddOnDemandStaticImportAction;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.actions.CleanupInspectionIntention;
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
@@ -219,13 +220,18 @@ public class JUnit5ConverterInspection extends BaseInspection {
return ArrayUtil.mergeArrays(usages, descriptors);
}
List<SmartPsiElementPointer<PsiElement>> myReplacedRefs = new ArrayList<>();
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
List<UsageInfo> migrateUsages = new ArrayList<>();
List<ProblemDescriptor> descriptions = new ArrayList<>();
SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
for (UsageInfo usage : usages) {
if (usage instanceof MyDescriptionBasedUsageInfo) {
descriptions.add (((MyDescriptionBasedUsageInfo)usage).myDescriptor);
ProblemDescriptor descriptor = ((MyDescriptionBasedUsageInfo)usage).myDescriptor;
descriptions.add (descriptor);
markUsagesImportedThroughStaticImport(smartPointerManager, descriptor);
}
else {
migrateUsages.add(usage);
@@ -234,6 +240,46 @@ public class JUnit5ConverterInspection extends BaseInspection {
super.performRefactoring(migrateUsages.toArray(new UsageInfo[migrateUsages.size()]));
CleanupInspectionIntention.applyFixes(myProject, "Convert Assertions", descriptions, JUnit5AssertionsConverterInspection.ReplaceObsoleteAssertsFix.class, false);
}
@Override
protected void performPsiSpoilingRefactoring() {
super.performPsiSpoilingRefactoring();
tryToRestoreStaticImportsOnNewAssertions();
}
private void markUsagesImportedThroughStaticImport(SmartPointerManager smartPointerManager, ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
PsiMethodCallExpression callExpression = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class);
if (callExpression != null) {
PsiReferenceExpression methodExpression = callExpression.getMethodExpression();
PsiElement scope = methodExpression.getQualifierExpression() == null
? methodExpression.advancedResolve(false).getCurrentFileResolveScope()
: null;
if (scope instanceof PsiImportStaticStatement && ((PsiImportStaticStatement)scope).isOnDemand()) {
myReplacedRefs.add(smartPointerManager.createSmartPsiElementPointer(callExpression));
}
}
}
private void tryToRestoreStaticImportsOnNewAssertions() {
for (SmartPsiElementPointer<PsiElement> ref : myReplacedRefs) {
PsiElement element = ref.getElement();
if (element instanceof PsiMethodCallExpression) {
PsiExpression qualifierExpression = ((PsiMethodCallExpression)element).getMethodExpression().getQualifierExpression();
if (qualifierExpression != null) {
PsiElement referenceNameElement = ((PsiReferenceExpression)qualifierExpression).getReferenceNameElement();
PsiClass aClass = referenceNameElement != null ? AddOnDemandStaticImportAction
.getClassToPerformStaticImport(referenceNameElement) : null;
PsiFile containingFile = element.getContainingFile();
if (aClass != null && !AddOnDemandStaticImportAction.invoke(myProject, containingFile, null, referenceNameElement)) {
PsiImportStatementBase importReferenceTo = PsiTreeUtil
.getParentOfType(((PsiJavaFile)containingFile).findImportReferenceTo(aClass), PsiImportStatementBase.class);
if (importReferenceTo != null) importReferenceTo.delete();
}
}
}
}
}
}
}
@@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.junit.jupiter.api.Assertions;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
class Test1 {
@org.junit.jupiter.api.Test
public void test_first() throws Exception {
assertArrayEquals(new Object[] {}, null);
Assertions.assertArrayEquals(new Object[] {}, null);
}
}
@@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.junit.jupiter.api.Assertions;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
class Test1 {
@org.junit.jupiter.api.Test
public void test_first() throws Exception {
assertArrayEquals(new Object[] {}, null, "message");
Assertions.assertArrayEquals(new Object[] {}, null, "message");
}
}
@@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.junit.jupiter.api.Assertions;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
class Test1 {
@org.junit.jupiter.api.Test
public void test_first() throws Exception {
assertEquals("Expected", "actual", "message");
Assertions.assertEquals("Expected", "actual", "message");
}
}
@@ -13,14 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.junit.jupiter.api.Assertions;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
class Test1 {
@org.junit.jupiter.api.Test
public void test_first() throws Exception {
assertTrue(false, "message");
Assertions.assertTrue(false, "message");
}
}
@@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.junit.jupiter.api.Assumptions;
import static org.junit.Assume.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
class Test1 {
@org.junit.jupiter.api.Test
public void test_first() throws Exception {
assumeTrue(false, "message");
Assumptions.assumeTrue(false, "message");
}
}
@@ -0,0 +1,11 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class FullConversion {
@Test
public void testAssertions() {
fail("fail");
assertTrue(true, "always");
}
}
@@ -0,0 +1,11 @@
import static org.junit.Assert.*;
import org.junit.Test;
public class Full<caret>Conversion {
@Test
public void testAssertions() {
fail("fail");
assertTrue("always", true);
}
}
@@ -13,22 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.Assert.*;
public class Simple {
@Test
public void test_first() throws Exception {
assertArrayEquals(new Object[] {}, null);
assertArrayEquals(new Object[] {}, null, "message");
Assertions.assertArrayEquals(new Object[] {}, null);
Assertions.assertArrayEquals(new Object[] {}, null, "message");
assertEquals("Expected", "actual");
assertEquals("Expected", "actual", "message");
Assertions.assertEquals("Expected", "actual");
Assertions.assertEquals("Expected", "actual", "message");
fail();
fail("");
Assertions.fail("");
}
}
@@ -26,6 +26,9 @@ public class Junit5ConverterFixTest extends IGQuickFixesTestCase {
public void testSimple() {
doTest();
}
public void testFullConversion() {
doTest();
}
@Override
protected void tuneFixture(JavaModuleFixtureBuilder builder) throws Exception {