support multiple test frameworks in one class (IDEA-176333)

This commit is contained in:
Anna.Kozlova
2017-07-20 20:21:20 +02:00
parent e31945ac57
commit 56743dbe6b
4 changed files with 40 additions and 8 deletions
@@ -71,10 +71,12 @@ public abstract class AbstractJavaTestConfigurationProducer<T extends JavaTestCo
protected JavaTestFramework getCurrentFramework(PsiClass psiClass) {
if (psiClass != null) {
TestFramework framework = TestFrameworks.detectFramework(psiClass);
if (framework instanceof JavaTestFramework && ((JavaTestFramework)framework).isMyConfigurationType(getConfigurationType())) {
return (JavaTestFramework)framework;
}
ConfigurationType configurationType = getConfigurationType();
Set<TestFramework> frameworks = TestFrameworks.detectApplicableFrameworks(psiClass);
return frameworks.stream().filter(framework -> framework instanceof JavaTestFramework && ((JavaTestFramework)framework).isMyConfigurationType(configurationType))
.map(framework -> (JavaTestFramework)framework)
.findFirst()
.orElse(null);
}
return null;
}
@@ -26,6 +26,9 @@ import com.intellij.testIntegration.TestFramework;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* @author yole
*/
@@ -60,6 +63,29 @@ public abstract class TestFrameworks {
.create(computeFramework(psiClass), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT));
}
@NotNull
public static Set<TestFramework> detectApplicableFrameworks(@NotNull final PsiClass psiClass) {
return CachedValuesManager.getCachedValue(psiClass, () -> CachedValueProvider.Result
.create(computeFrameworks(psiClass), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT));
}
private static Set<TestFramework> computeFrameworks(PsiClass psiClass) {
Set<TestFramework> frameworks = new LinkedHashSet<>();
for (TestFramework framework : Extensions.getExtensions(TestFramework.EXTENSION_NAME)) {
if (framework.isTestClass(psiClass)) {
frameworks.add(framework);
}
}
for (TestFramework framework : Extensions.getExtensions(TestFramework.EXTENSION_NAME)) {
if (frameworks.contains(framework)) continue;;
if (framework.findSetUpMethod(psiClass) != null || framework.findTearDownMethod(psiClass) != null) {
frameworks.add(framework);
}
}
return frameworks;
}
@Nullable
private static TestFramework computeFramework(PsiClass psiClass) {
for (TestFramework framework : Extensions.getExtensions(TestFramework.EXTENSION_NAME)) {
@@ -23,6 +23,8 @@ import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.naming.ConventionInspection;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
public class JUnitTestClassNamingConventionInspectionBase extends ConventionInspection {
private static final int DEFAULT_MIN_LENGTH = 8;
private static final int DEFAULT_MAX_LENGTH = 64;
@@ -88,8 +90,8 @@ public class JUnitTestClassNamingConventionInspectionBase extends ConventionInsp
return;
}
final TestFramework framework = TestFrameworks.detectFramework(aClass);
if (framework == null || !framework.getName().startsWith("JUnit") || !framework.isTestClass(aClass)) {
final Set<TestFramework> frameworks = TestFrameworks.detectApplicableFrameworks(aClass);
if (frameworks.stream().noneMatch(framework -> framework.getName().startsWith("JUnit") && framework.isTestClass(aClass))) {
return;
}
@@ -28,6 +28,8 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
public class TestUtils {
public static final String RUN_WITH = "org.junit.runner.RunWith";
@@ -64,8 +66,8 @@ public class TestUtils {
if (method == null) return false;
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null) return false;
final TestFramework framework = TestFrameworks.detectFramework(containingClass);
return framework != null && framework.getName().startsWith("JUnit") && framework.isTestMethod(method, false);
final Set<TestFramework> frameworks = TestFrameworks.detectApplicableFrameworks(containingClass);
return frameworks.stream().anyMatch(framework -> framework.getName().startsWith("JUnit") && framework.isTestMethod(method, false));
}
public static boolean isRunnable(PsiMethod method) {