diff --git a/java/java-psi-api/src/com/intellij/codeInsight/TestFrameworks.java b/java/java-psi-api/src/com/intellij/codeInsight/TestFrameworks.java index b93ec588d2bb..4fb64e6e245c 100644 --- a/java/java-psi-api/src/com/intellij/codeInsight/TestFrameworks.java +++ b/java/java-psi-api/src/com/intellij/codeInsight/TestFrameworks.java @@ -19,6 +19,9 @@ import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.extensions.Extensions; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiMethod; +import com.intellij.psi.util.CachedValueProvider; +import com.intellij.psi.util.CachedValuesManager; +import com.intellij.psi.util.PsiModificationTracker; import com.intellij.testIntegration.TestFramework; import org.jetbrains.annotations.Nullable; @@ -51,7 +54,18 @@ public abstract class TestFrameworks { } @Nullable - public static TestFramework detectFramework(PsiClass psiClass) { + public static TestFramework detectFramework(final PsiClass psiClass) { + return CachedValuesManager.getCachedValue(psiClass, new CachedValueProvider() { + @Nullable + @Override + public Result compute() { + return Result.create(computeFramework(psiClass), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); + } + }); + } + + @Nullable + private static TestFramework computeFramework(PsiClass psiClass) { for (TestFramework framework : Extensions.getExtensions(TestFramework.EXTENSION_NAME)) { if (framework.isTestClass(psiClass)) { return framework; @@ -63,7 +77,6 @@ public abstract class TestFrameworks { return framework; } } - return null; } } diff --git a/plugins/devkit/src/testAssistant/TestDataGuessByExistingFilesUtil.java b/plugins/devkit/src/testAssistant/TestDataGuessByExistingFilesUtil.java index 058c7c9a1324..b46949e56a25 100644 --- a/plugins/devkit/src/testAssistant/TestDataGuessByExistingFilesUtil.java +++ b/plugins/devkit/src/testAssistant/TestDataGuessByExistingFilesUtil.java @@ -16,8 +16,8 @@ package org.jetbrains.idea.devkit.testAssistant; import com.intellij.codeInsight.AnnotationUtil; +import com.intellij.codeInsight.TestFrameworks; import com.intellij.ide.util.gotoByName.GotoFileModel; -import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; @@ -66,20 +66,11 @@ public class TestDataGuessByExistingFilesUtil { */ @Nullable static List collectTestDataByExistingFiles(@NotNull PsiMethod psiMethod) { - String testName = getTestName(psiMethod); - if (testName == null) { - return null; - } - PsiFile psiFile = PsiTreeUtil.getParentOfType(psiMethod, PsiFile.class); - if (psiFile == null) { - return null; - } TestDataDescriptor descriptor = buildDescriptorFromExistingTestData(psiMethod); if (descriptor == null || !descriptor.isComplete()) { return null; } - - return descriptor.generate(testName); + return descriptor.generate(); } static String guessTestDataName(PsiMethod method) { @@ -87,14 +78,16 @@ public class TestDataGuessByExistingFilesUtil { if (testName == null) return null; PsiClass psiClass = method.getContainingClass(); if (psiClass == null) return null; + int count = 5; PsiMethod prev = PsiTreeUtil.getPrevSiblingOfType(method, PsiMethod.class); - while (prev != null) { + while (prev != null && count-- > 0) { String s = getFilePath(prev, testName); if (s != null) return s; prev = PsiTreeUtil.getPrevSiblingOfType(method, PsiMethod.class); } + count = 5; PsiMethod next = PsiTreeUtil.getNextSiblingOfType(method, PsiMethod.class); - while (next != null) { + while (next != null && count-- > 0) { String s = getFilePath(next, testName); if (s != null) return s; next = PsiTreeUtil.getPrevSiblingOfType(method, PsiMethod.class); @@ -119,14 +112,7 @@ public class TestDataGuessByExistingFilesUtil { return null; } - TestFramework[] frameworks = Extensions.getExtensions(TestFramework.EXTENSION_NAME); - TestFramework framework = null; - for (TestFramework each : frameworks) { - if (each.isTestClass(psiClass)) { - framework = each; - break; - } - } + TestFramework framework = TestFrameworks.detectFramework(psiClass); if (framework == null || isUtilityMethod(method, psiClass, framework)) { return null; @@ -234,7 +220,7 @@ public class TestDataGuessByExistingFilesUtil { break; } } - return new TestDataDescriptor(descriptors); + return new TestDataDescriptor(descriptors, test); } private static Collection getAllFileNames(final String testName, final GotoFileModel model) { @@ -374,11 +360,13 @@ public class TestDataGuessByExistingFilesUtil { } private static class TestDataDescriptor { - private static final TestDataDescriptor NOTHING_FOUND = new TestDataDescriptor(Collections.emptyList()); + private static final TestDataDescriptor NOTHING_FOUND = new TestDataDescriptor(Collections.emptyList(), null); private final List myDescriptors = new ArrayList(); + private final String myTestName; - TestDataDescriptor(Collection descriptors) { + TestDataDescriptor(Collection descriptors, String testName) { + myTestName = testName; myDescriptors.addAll(descriptors); } @@ -400,6 +388,11 @@ public class TestDataGuessByExistingFilesUtil { return generate(testName, null); } + @NotNull + public List generate() { + return generate(myTestName, null); + } + @NotNull public List generate(@NotNull final String testName, String root) { List result = new ArrayList();