add setup method before first test method: setting needed? (IDEA-61605)

This commit is contained in:
anna
2010-11-25 13:49:21 +03:00
parent 5ebb6cc3c2
commit cd5b299bf4
8 changed files with 49 additions and 18 deletions

View File

@@ -31,6 +31,7 @@ import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.containers.Convertor;
import junit.runner.BaseTestRunner;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
@@ -209,6 +210,18 @@ public class JUnitUtil {
return false;
}
@Nullable
protected static PsiMethod findFirstTestMethod(PsiClass clazz) {
PsiMethod testMethod = null;
for (PsiMethod method : clazz.getMethods()) {
if (isTestMethod(MethodLocation.elementInClass(method, clazz)) || isSuiteMethod(method)) {
testMethod = method;
break;
}
}
return testMethod;
}
public static class TestMethodFilter implements Condition<PsiMethod> {
private final PsiClass myClass;

View File

@@ -2,12 +2,12 @@ import org.junit.*;
public class T {
private int i;
@Test
public void test() {
}
@Before
public void setUp() throws Exception {
i = 9;
}
@Test
public void test() {
}
}

View File

@@ -3,12 +3,12 @@ import org.junit.Test;
public class T {
private int i;
@Test
public void test() {
}
@Before
public void setUp() throws Exception {
i = 9;
}
@Test
public void test() {
}
}

View File

@@ -2,11 +2,11 @@ import junit.framework.TestCase;
public class T extends TestCase {
private int i;
public void test() {
}
protected void setUp() throws Exception {
super.setUp();
i = 9;
}
public void test() {
}
}

View File

@@ -9,11 +9,11 @@ class Base extends TestCase {
public class T extends Base {
private int i;
public void test() {
}
public void setUp() throws Exception {
super.setUp();
i = 9;
}
public void test() {
}
}

View File

@@ -93,6 +93,10 @@ public class JUnit3Framework extends JavaTestFramework {
PsiMethod inClass = clazz.findMethodBySignature(patternMethod, false);
if (inClass == null) {
PsiMethod testMethod = JUnitUtil.findFirstTestMethod(clazz);
if (testMethod != null) {
return (PsiMethod)clazz.addBefore(patternMethod, testMethod);
}
return (PsiMethod)clazz.add(patternMethod);
}
else if (inClass.getBody() == null) {

View File

@@ -86,7 +86,12 @@ 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);
method = (PsiMethod)clazz.add(method);
final PsiMethod testMethod = JUnitUtil.findFirstTestMethod(clazz);
if (testMethod != null) {
method = (PsiMethod)clazz.addBefore(method, testMethod);
} else {
method = (PsiMethod)clazz.add(method);
}
JavaCodeStyleManager.getInstance(manager.getProject()).shortenClassReferences(method);
return method;

View File

@@ -28,6 +28,7 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.swing.*;
@@ -115,14 +116,22 @@ public class TestNGFramework extends JavaTestFramework {
final PsiMethod[] psiMethods = clazz.getMethods();
PsiMethod inClass = null;
PsiMethod testMethod = null;
for (PsiMethod psiMethod : psiMethods) {
if (AnnotationUtil.isAnnotated(psiMethod, BeforeMethod.class.getName(), false)) {
if (inClass == null && AnnotationUtil.isAnnotated(psiMethod, BeforeMethod.class.getName(), false)) {
inClass = psiMethod;
break;
}
if (testMethod == null && AnnotationUtil.isAnnotated(psiMethod, Test.class.getName(), false) && !psiMethod.hasModifierProperty(PsiModifier.PRIVATE)) {
testMethod = psiMethod;
}
}
if (inClass == null) {
final PsiMethod psiMethod = (PsiMethod)clazz.add(patternMethod);
final PsiMethod psiMethod;
if (testMethod != null) {
psiMethod = (PsiMethod)clazz.addBefore(patternMethod, testMethod);
} else {
psiMethod = (PsiMethod)clazz.add(patternMethod);
}
JavaCodeStyleManager.getInstance(clazz.getProject()).shortenClassReferences(clazz);
return psiMethod;
}