From b3922178be0cf9b8ca02b07241b90c09d7b6b0ad Mon Sep 17 00:00:00 2001 From: anna Date: Mon, 21 Nov 2011 09:39:19 +0100 Subject: [PATCH] introduce field: avoid dbl setup creation when no annotation present (IDEA-76926) --- .../afterBeforeExistNonAnnotated.java | 13 +++++++ .../beforeBeforeExistNonAnnotated.java | 9 +++++ ...roduceFieldWitSetUpInitializationTest.java | 3 ++ .../execution/junit/JUnit4Framework.java | 17 +++++++++ .../testng/TestNGFramework.java | 36 ++++++++++++++++--- 5 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 java/java-tests/testData/refactoring/introduceField/afterBeforeExistNonAnnotated.java create mode 100644 java/java-tests/testData/refactoring/introduceField/beforeBeforeExistNonAnnotated.java diff --git a/java/java-tests/testData/refactoring/introduceField/afterBeforeExistNonAnnotated.java b/java/java-tests/testData/refactoring/introduceField/afterBeforeExistNonAnnotated.java new file mode 100644 index 000000000000..1396c41705e0 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceField/afterBeforeExistNonAnnotated.java @@ -0,0 +1,13 @@ +import org.junit.*; +public class T { + private int i; + + @Test + public void test() { + } + + @Before + public void setUp(){ + i = 9; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/introduceField/beforeBeforeExistNonAnnotated.java b/java/java-tests/testData/refactoring/introduceField/beforeBeforeExistNonAnnotated.java new file mode 100644 index 000000000000..cfb4c61933ce --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceField/beforeBeforeExistNonAnnotated.java @@ -0,0 +1,9 @@ +import org.junit.*; +public class T { + @Test + public void test() { + int i = 9; + } + + public void setUp(){} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceFieldWitSetUpInitializationTest.java b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceFieldWitSetUpInitializationTest.java index 86513151f1b6..c17db6f3eb62 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceFieldWitSetUpInitializationTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceFieldWitSetUpInitializationTest.java @@ -83,6 +83,9 @@ public class IntroduceFieldWitSetUpInitializationTest extends CodeInsightTestCas doTest(); } + public void testBeforeExistNonAnnotated() throws Exception { + doTest(); + } private void doTest() throws Exception { configureByFile("/refactoring/introduceField/before" + getTestName(false) + ".java"); diff --git a/plugins/junit/src/com/intellij/execution/junit/JUnit4Framework.java b/plugins/junit/src/com/intellij/execution/junit/JUnit4Framework.java index c91257e24abb..4dbbe014d6b5 100644 --- a/plugins/junit/src/com/intellij/execution/junit/JUnit4Framework.java +++ b/plugins/junit/src/com/intellij/execution/junit/JUnit4Framework.java @@ -15,9 +15,14 @@ */ package com.intellij.execution.junit; +import com.intellij.CommonBundle; import com.intellij.codeInsight.AnnotationUtil; +import com.intellij.codeInsight.intention.AddAnnotationFix; import com.intellij.ide.fileTemplates.FileTemplateDescriptor; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.projectRoots.ex.JavaSdkUtil; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.ui.Messages; import com.intellij.psi.*; import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.testIntegration.JavaTestFramework; @@ -87,6 +92,18 @@ public class JUnit4Framework extends JavaTestFramework { PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); method = factory.createMethodFromText("@org.junit.Before public void setUp() throws Exception {\n}", null); + PsiMethod existingMethod = clazz.findMethodBySignature(method, false); + if (existingMethod != null) { + int exit = ApplicationManager.getApplication().isUnitTestMode() ? + DialogWrapper.OK_EXIT_CODE : + Messages.showOkCancelDialog("Method setUp already exist but is not annotated as @Before. Annotate?", + CommonBundle.getWarningTitle(), + Messages.getWarningIcon()); + if (exit == DialogWrapper.OK_EXIT_CODE) { + new AddAnnotationFix("org.junit.Before", existingMethod).invoke(existingMethod.getProject(), null, existingMethod.getContainingFile()); + return existingMethod; + } + } final PsiMethod testMethod = JUnitUtil.findFirstTestMethod(clazz); if (testMethod != null) { method = (PsiMethod)clazz.addBefore(method, testMethod); diff --git a/plugins/testng/src/com/theoryinpractice/testng/TestNGFramework.java b/plugins/testng/src/com/theoryinpractice/testng/TestNGFramework.java index 2dcb9d188663..a69c5f28f85a 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/TestNGFramework.java +++ b/plugins/testng/src/com/theoryinpractice/testng/TestNGFramework.java @@ -15,8 +15,13 @@ */ package com.theoryinpractice.testng; +import com.intellij.CommonBundle; import com.intellij.codeInsight.AnnotationUtil; +import com.intellij.codeInsight.intention.AddAnnotationFix; import com.intellij.ide.fileTemplates.FileTemplateDescriptor; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.ui.Messages; import com.intellij.psi.*; import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.testIntegration.JavaTestFramework; @@ -93,8 +98,29 @@ public class TestNGFramework extends JavaTestFramework { final PsiManager manager = clazz.getManager(); final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); - PsiMethod patternMethod = - factory.createMethodFromText("@org.testng.annotations.BeforeMethod\n protected void setUp() throws Exception {}", null); + String setUpName = "setUp"; + PsiMethod patternMethod = factory.createMethodFromText("@" + BeforeMethod.class.getName() + "\n protected void " + setUpName + "() throws Exception {}", null); + PsiMethod inClass = clazz.findMethodBySignature(patternMethod, false); + if (inClass != null) { + int exit = ApplicationManager.getApplication().isUnitTestMode() ? + DialogWrapper.OK_EXIT_CODE : + Messages.showYesNoDialog("Method \'" + setUpName + "\' already exist but is not annotated as @BeforeMethod.", + CommonBundle.getWarningTitle(), + "Annotate", + "Create new method", + Messages.getWarningIcon()); + if (exit == DialogWrapper.OK_EXIT_CODE) { + new AddAnnotationFix(BeforeMethod.class.getName(), inClass).invoke(inClass.getProject(), null, inClass.getContainingFile()); + return inClass; + } else if (exit == DialogWrapper.CANCEL_EXIT_CODE) { + inClass = null; + int i = 0; + while (clazz.findMethodBySignature(patternMethod, false) != null) { + patternMethod.setName(setUpName + (++i)); + } + setUpName = patternMethod.getName(); + } + } final PsiClass superClass = clazz.getSuperClass(); if (superClass != null) { @@ -102,20 +128,20 @@ public class TestNGFramework extends JavaTestFramework { if (methods.length > 0) { final PsiModifierList modifierList = methods[0].getModifierList(); if (!modifierList.hasModifierProperty(PsiModifier.PRIVATE)) { //do not override private method - @NonNls String pattern = "@org.testng.annotations.BeforeMethod\n"; + @NonNls String pattern = "@" + BeforeMethod.class.getName() + "\n"; if (modifierList.hasModifierProperty(PsiModifier.PROTECTED)) { pattern += "protected "; } else if (modifierList.hasModifierProperty(PsiModifier.PUBLIC)) { pattern += "public "; } - patternMethod = factory.createMethodFromText(pattern + "void setUp() throws Exception {\nsuper.setUp();\n}", null); + patternMethod = factory.createMethodFromText(pattern + "void " + setUpName + "() throws Exception {\nsuper." + setUpName + "();\n}", null); } } } final PsiMethod[] psiMethods = clazz.getMethods(); - PsiMethod inClass = null; + PsiMethod testMethod = null; for (PsiMethod psiMethod : psiMethods) { if (inClass == null && AnnotationUtil.isAnnotated(psiMethod, BeforeMethod.class.getName(), false)) {