diff --git a/java/execution/impl/src/com/intellij/execution/testframework/SearchForTestsTask.java b/java/execution/impl/src/com/intellij/execution/testframework/SearchForTestsTask.java index 5c38823c1666..80bb3c4785e8 100644 --- a/java/execution/impl/src/com/intellij/execution/testframework/SearchForTestsTask.java +++ b/java/execution/impl/src/com/intellij/execution/testframework/SearchForTestsTask.java @@ -17,10 +17,13 @@ package com.intellij.execution.testframework; import com.intellij.execution.CantRunException; import com.intellij.execution.ExecutionBundle; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.Task; +import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator; import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; @@ -36,6 +39,7 @@ public abstract class SearchForTestsTask extends Task.Backgroundable { private static final Logger LOG = Logger.getInstance("#" + SearchForTestsTask.class.getName()); protected Socket mySocket; private ServerSocket myServerSocket; + private ProgressIndicator myProcessIndicator; public SearchForTestsTask(@Nullable final Project project, @NotNull final ServerSocket socket) { @@ -47,6 +51,28 @@ public abstract class SearchForTestsTask extends Task.Backgroundable { protected abstract void search() throws CantRunException; protected abstract void onFound(); + public void ensureFinished() { + if (myProcessIndicator != null && !myProcessIndicator.isCanceled()) { + finish(); + } + } + + public void startSearch() { + if (ApplicationManager.getApplication().isUnitTestMode()) { + try { + search(); + onFound(); + } + catch (Throwable e) { + LOG.error(e); + } + } + else { + myProcessIndicator = new BackgroundableProcessIndicator(this); + ProgressManager.getInstance().runProcessWithProgressAsynchronously(this, myProcessIndicator); + } + } + @Override public void run(@NotNull ProgressIndicator indicator) { try { diff --git a/java/java-tests/testSrc/com/intellij/execution/ConfigurationsTest.java b/java/java-tests/testSrc/com/intellij/execution/ConfigurationsTest.java index ac9844c4ced8..73ddb14b4805 100644 --- a/java/java-tests/testSrc/com/intellij/execution/ConfigurationsTest.java +++ b/java/java-tests/testSrc/com/intellij/execution/ConfigurationsTest.java @@ -9,6 +9,7 @@ import com.intellij.execution.junit.*; import com.intellij.execution.junit2.configuration.JUnitConfigurable; import com.intellij.execution.junit2.configuration.JUnitConfigurationModel; import com.intellij.execution.runners.ExecutionEnvironmentBuilder; +import com.intellij.execution.testframework.SearchForTestsTask; import com.intellij.execution.testframework.TestSearchScope; import com.intellij.execution.ui.CommonJavaParametersPanel; import com.intellij.openapi.module.Module; @@ -426,7 +427,9 @@ public class ConfigurationsTest extends BaseConfigurationTestCase { if (state instanceof TestPackage) { @SuppressWarnings("UnusedDeclaration") final JavaParameters parameters = ((TestPackage)state).getJavaParameters(); - ((TestPackage)state).findTests(); + final SearchForTestsTask task = ((TestPackage)state).createSearchingForTestsTask(); + assertNotNull(task); + task.startSearch(); } try { configuration.checkConfiguration(); diff --git a/plugins/junit/src/com/intellij/execution/junit/TestPackage.java b/plugins/junit/src/com/intellij/execution/junit/TestPackage.java index 3ff5c9ec10fe..53bda2b9fb75 100644 --- a/plugins/junit/src/com/intellij/execution/junit/TestPackage.java +++ b/plugins/junit/src/com/intellij/execution/junit/TestPackage.java @@ -27,9 +27,6 @@ import com.intellij.execution.runners.ExecutionEnvironment; import com.intellij.execution.testframework.*; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.module.Module; -import com.intellij.openapi.progress.ProgressManager; -import com.intellij.openapi.progress.Task; -import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Comparing; import com.intellij.psi.*; @@ -47,7 +44,6 @@ import java.net.ServerSocket; import java.util.Collection; public class TestPackage extends TestObject { - protected BackgroundableProcessIndicator mySearchForTestsIndicator; protected ServerSocket myServerSocket; private boolean myFoundTests = true; @@ -64,51 +60,51 @@ public class TestPackage extends TestObject { @Override protected JUnitProcessHandler createHandler(Executor executor) throws ExecutionException { final JUnitProcessHandler handler = super.createHandler(executor); - final SearchForTestsTask[] tasks = new SearchForTestsTask[1]; + final SearchForTestsTask task = createSearchingForTestsTask(); handler.addProcessListener(new ProcessAdapter() { @Override public void startNotified(ProcessEvent event) { super.startNotified(event); - tasks[0] = (SearchForTestsTask)findTests(); + task.startSearch(); } @Override public void processTerminated(ProcessEvent event) { handler.removeProcessListener(this); - if (mySearchForTestsIndicator != null && !mySearchForTestsIndicator.isCanceled()) { - if (tasks[0] != null) { - tasks[0].finish(); - } - } + task.ensureFinished(); } }); return handler; } - public Task findTests() { + public SearchForTestsTask createSearchingForTestsTask() { final JUnitConfiguration.Data data = myConfiguration.getPersistentData(); - final TestClassFilter filter; - try { - filter = getClassFilter(data); - } - catch (CantRunException ignored) { - //should not happen - return null; - } - return findTestsWithProgress(new Consumer>() { + return new SearchForTestsTask(myConfiguration.getProject(), myServerSocket) { + private final THashSet myClasses = new THashSet(); @Override - public void consume(Collection classes) { + protected void search() { + myClasses.clear(); try { - addClassesListToJavaParameters(classes, new Function() { + ConfigurationUtil.findAllTestClasses(getClassFilter(data), myClasses); + } + catch (CantRunException ignored) {} + } + + @Override + protected void onFound() { + myFoundTests = !myClasses.isEmpty(); + + try { + addClassesListToJavaParameters(myClasses, new Function() { @Override @Nullable - public String fun(PsiElement element) { + public String fun(final PsiElement element) { if (element instanceof PsiClass) { return JavaExecutionUtil.getRuntimeQualifiedName((PsiClass)element); } else if (element instanceof PsiMethod) { - PsiMethod method = (PsiMethod)element; + final PsiMethod method = (PsiMethod)element; return JavaExecutionUtil.getRuntimeQualifiedName(method.getContainingClass()) + "," + method.getName(); } else { @@ -117,11 +113,9 @@ public class TestPackage extends TestObject { } }, getPackageName(data), false); } - catch (CantRunException ignored) { - //can't be here - } + catch (CantRunException ignored) {} } - }, filter); + }; } protected String getPackageName(JUnitConfiguration.Data data) throws CantRunException { @@ -231,34 +225,6 @@ public class TestPackage extends TestObject { } } - private SearchForTestsTask findTestsWithProgress(final Consumer> callback, final TestClassFilter classFilter) { - if (ApplicationManager.getApplication().isUnitTestMode()) { - THashSet classes = new THashSet(); - ConfigurationUtil.findAllTestClasses(classFilter, classes); - callback.consume(classes); - return null; - } - - final THashSet classes = new THashSet(); - final SearchForTestsTask task = - new SearchForTestsTask(classFilter.getProject(), myServerSocket) { - @Override - protected void search() { - classes.clear(); - ConfigurationUtil.findAllTestClasses(classFilter, classes); - } - - @Override - protected void onFound() { - myFoundTests = !classes.isEmpty(); - callback.consume(classes); - } - }; - mySearchForTestsIndicator = new BackgroundableProcessIndicator(task); - ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, mySearchForTestsIndicator); - return task; - } - @Override protected void notifyByBalloon(JUnitRunningModel model, boolean started, final JUnitConsoleProperties consoleProperties) { if (myFoundTests || !ResetConfigurationModuleAdapter.tryWithAnotherModule(myConfiguration, consoleProperties.isDebug())) { diff --git a/plugins/junit/src/com/intellij/execution/junit/TestsPattern.java b/plugins/junit/src/com/intellij/execution/junit/TestsPattern.java index 1dcf4625c8cd..6494bc498133 100644 --- a/plugins/junit/src/com/intellij/execution/junit/TestsPattern.java +++ b/plugins/junit/src/com/intellij/execution/junit/TestsPattern.java @@ -29,9 +29,6 @@ import com.intellij.execution.runners.ExecutionEnvironment; import com.intellij.execution.testframework.SearchForTestsTask; import com.intellij.execution.util.JavaParametersUtil; import com.intellij.openapi.module.Module; -import com.intellij.openapi.progress.ProgressManager; -import com.intellij.openapi.progress.Task; -import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; @@ -61,7 +58,7 @@ public class TestsPattern extends TestPackage { } @Override - public Task findTests() { + public SearchForTestsTask createSearchingForTestsTask() { final JUnitConfiguration.Data data = myConfiguration.getPersistentData(); final Project project = myConfiguration.getProject(); final Set classNames = new LinkedHashSet(); @@ -73,7 +70,7 @@ public class TestsPattern extends TestPackage { } if (classNames.size() == data.getPatterns().size()) { - final SearchForTestsTask task = new SearchForTestsTask(project, myServerSocket) { + return new SearchForTestsTask(project, myServerSocket) { @Override protected void search() throws CantRunException { final Function nameFunction = StringUtil.isEmpty(data.METHOD_NAME) @@ -90,12 +87,9 @@ public class TestsPattern extends TestPackage { @Override protected void onFound() {} }; - mySearchForTestsIndicator = new BackgroundableProcessIndicator(task); - ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, mySearchForTestsIndicator); - return task; } - return super.findTests(); + return super.createSearchingForTestsTask(); } private static PsiClass getTestClass(Project project, String className) { diff --git a/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGRunnableState.java b/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGRunnableState.java index 19935250ded2..510c8e3f90fe 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGRunnableState.java +++ b/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGRunnableState.java @@ -36,8 +36,6 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.module.EffectiveLanguageLevelUtil; import com.intellij.openapi.module.Module; -import com.intellij.openapi.progress.ProgressManager; -import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator; import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.projectRoots.ex.JavaSdkUtil; @@ -86,7 +84,6 @@ public class TestNGRunnableState extends JavaCommandLineState { private int port; private String debugPort; private File myTempFile; - private BackgroundableProcessIndicator mySearchForTestIndicator; private ServerSocket myServerSocket; public TestNGRunnableState(ExecutionEnvironment environment, TestNGConfiguration config) { @@ -135,9 +132,7 @@ public class TestNGRunnableState extends JavaCommandLineState { public void processTerminated(final ProcessEvent event) { unboundOutputRoot.flush(); - if (mySearchForTestIndicator != null && !mySearchForTestIndicator.isCanceled()) { - task.finish(); - } + task.ensureFinished(); } @Override @@ -148,8 +143,7 @@ public class TestNGRunnableState extends JavaCommandLineState { } client.prepareListening(listener, config.getProject(), port); myStarted = true; - mySearchForTestIndicator = new BackgroundableProcessIndicator(task); - ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, mySearchForTestIndicator); + task.startSearch(); } @Override @@ -229,16 +223,12 @@ public class TestNGRunnableState extends JavaCommandLineState { handler.addProcessListener(new ProcessAdapter() { @Override public void processTerminated(final ProcessEvent event) { - - if (mySearchForTestIndicator != null && !mySearchForTestIndicator.isCanceled()) { - task.finish(); - } + task.ensureFinished(); } @Override public void startNotified(final ProcessEvent event) { - mySearchForTestIndicator = new BackgroundableProcessIndicator(task); - ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, mySearchForTestIndicator); + task.startSearch(); } });