diff --git a/plugins/junit/src/com/intellij/execution/junit/TestObject.java b/plugins/junit/src/com/intellij/execution/junit/TestObject.java index 84ba85436d9e..c356c86eb29a 100644 --- a/plugins/junit/src/com/intellij/execution/junit/TestObject.java +++ b/plugins/junit/src/com/intellij/execution/junit/TestObject.java @@ -253,7 +253,7 @@ public abstract class TestObject implements JavaCommandLine { } public ExecutionResult execute(final Executor executor, @NotNull final ProgramRunner runner) throws ExecutionException { - final JUnitProcessHandler handler = JUnitProcessHandler.runJava(getJavaParameters(), myProject); + final JUnitProcessHandler handler = createHandler(); for(final RunConfigurationExtension ext: Extensions.getExtensions(RunConfigurationExtension.EP_NAME)) { ext.handleStartProcess(myConfiguration, handler); } @@ -348,6 +348,10 @@ public abstract class TestObject implements JavaCommandLine { return result; } + protected JUnitProcessHandler createHandler() throws ExecutionException { + return JUnitProcessHandler.runJava(getJavaParameters(), myProject); + } + protected void addClassesListToJavaParameters(Collection elements, Function nameFunction, String packageName, boolean createTempFile, diff --git a/plugins/junit/src/com/intellij/execution/junit/TestPackage.java b/plugins/junit/src/com/intellij/execution/junit/TestPackage.java index e1fff140e9ad..09fe8fec03fb 100644 --- a/plugins/junit/src/com/intellij/execution/junit/TestPackage.java +++ b/plugins/junit/src/com/intellij/execution/junit/TestPackage.java @@ -21,6 +21,8 @@ import com.intellij.execution.configurations.ConfigurationPerRunnerSettings; import com.intellij.execution.configurations.RunnerSettings; import com.intellij.execution.configurations.RuntimeConfigurationException; import com.intellij.execution.configurations.RuntimeConfigurationWarning; +import com.intellij.execution.process.ProcessAdapter; +import com.intellij.execution.process.ProcessEvent; import com.intellij.execution.runners.ProgramRunner; import com.intellij.execution.testframework.SourceScope; import com.intellij.execution.testframework.TestSearchScope; @@ -53,6 +55,7 @@ import java.util.Collection; public class TestPackage extends TestObject { private BackgroundableProcessIndicator mySearchForTestsIndicator; + private ServerSocket myServerSocket; public TestPackage(final Project project, final JUnitConfiguration configuration, @@ -68,22 +71,57 @@ public class TestPackage extends TestObject { } @Override - public ExecutionResult execute(Executor executor, @NotNull ProgramRunner runner) throws ExecutionException { - try { - return super.execute(executor, runner); - } - catch (ExecutionException e) { - if (mySearchForTestsIndicator != null && !mySearchForTestsIndicator.isCanceled()) { - mySearchForTestsIndicator.cancel(); //ensure that search for tests stops anyway + protected JUnitProcessHandler createHandler() throws ExecutionException { + final JUnitProcessHandler handler = super.createHandler(); + handler.addProcessListener(new ProcessAdapter() { + @Override + public void startNotified(ProcessEvent event) { + super.startNotified(event); + final JUnitConfiguration.Data data = myConfiguration.getPersistentData(); + final TestClassFilter filter; + try { + filter = getClassFilter(data); + } + catch (CantRunException e) { + //should not happen + return; + } + findTestsWithProgress(new FindCallback() { + public void found(@NotNull final Collection classes, final boolean isJunit4) { + addClassesListToJavaParameters(classes, new Function() { + @Nullable + public String fun(PsiElement element) { + if (element instanceof PsiClass) { + return JavaExecutionUtil.getRuntimeQualifiedName((PsiClass)element); + } + else if (element instanceof PsiMethod) { + PsiMethod method = (PsiMethod)element; + return JavaExecutionUtil.getRuntimeQualifiedName(method.getContainingClass()) + "," + method.getName(); + } + else { + return null; + } + } + }, data.getPackageName(), false, isJunit4); + } + }, filter); } - throw e; - } + + @Override + public void processTerminated(ProcessEvent event) { + handler.removeProcessListener(this); + if (mySearchForTestsIndicator != null && !mySearchForTestsIndicator.isCanceled()) { + mySearchForTestsIndicator.cancel(); //ensure that search for tests stops anyway + } + } + }); + return handler; } protected void initialize() throws ExecutionException { super.initialize(); final JUnitConfiguration.Data data = myConfiguration.getPersistentData(); - final TestClassFilter filter = getClassFilter(data); + getClassFilter(data);//check if junit found final ExecutionException[] exception = new ExecutionException[1]; ApplicationManager.getApplication().runReadAction(new Runnable() { public void run() { @@ -109,27 +147,8 @@ public class TestPackage extends TestObject { } try { - final ServerSocket serverSocket = new ServerSocket(0, 0, InetAddress.getByName(null)); - myJavaParameters.getProgramParametersList().add("-socket" + serverSocket.getLocalPort()); - findTestsWithProgress(new FindCallback() { - public void found(@NotNull final Collection classes, final boolean isJunit4) { - addClassesListToJavaParameters(classes, new Function() { - @Nullable - public String fun(PsiElement element) { - if (element instanceof PsiClass) { - return JavaExecutionUtil.getRuntimeQualifiedName((PsiClass)element); - } - else if (element instanceof PsiMethod) { - PsiMethod method = (PsiMethod)element; - return JavaExecutionUtil.getRuntimeQualifiedName(method.getContainingClass()) + "," + method.getName(); - } - else { - return null; - } - } - }, data.getPackageName(), false, isJunit4); - } - }, filter, serverSocket); + myServerSocket = new ServerSocket(0, 0, InetAddress.getByName(null)); + myJavaParameters.getProgramParametersList().add("-socket" + myServerSocket.getLocalPort()); } catch (IOException e) { LOG.error(e); @@ -192,7 +211,7 @@ public class TestPackage extends TestObject { } } - private void findTestsWithProgress(final FindCallback callback, final TestClassFilter classFilter, final ServerSocket serverSocket) { + private void findTestsWithProgress(final FindCallback callback, final TestClassFilter classFilter) { if (isSyncSearch()) { THashSet classes = new THashSet(); boolean isJUnit4 = ConfigurationUtil.findAllTestClasses(classFilter, classes); @@ -209,7 +228,7 @@ public class TestPackage extends TestObject { public void run(@NotNull ProgressIndicator indicator) { try { - mySocket = serverSocket.accept(); + mySocket = myServerSocket.accept(); } catch (IOException e) { LOG.info(e); @@ -251,7 +270,7 @@ public class TestPackage extends TestObject { } try { - serverSocket.close(); + myServerSocket.close(); } catch (Throwable e) { LOG.info(e); @@ -263,8 +282,8 @@ public class TestPackage extends TestObject { @Override public void cancel() { try {//ensure that serverSocket.accept was interrupted - if (!serverSocket.isClosed()) { - new Socket(InetAddress.getLocalHost(), serverSocket.getLocalPort()); + if (!myServerSocket.isClosed()) { + new Socket(InetAddress.getLocalHost(), myServerSocket.getLocalPort()); } } catch (Throwable e) { diff --git a/plugins/testng/src/com/theoryinpractice/testng/configuration/SearchingForTestsTask.java b/plugins/testng/src/com/theoryinpractice/testng/configuration/SearchingForTestsTask.java index d049dd790065..96a77c08ae3e 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/configuration/SearchingForTestsTask.java +++ b/plugins/testng/src/com/theoryinpractice/testng/configuration/SearchingForTestsTask.java @@ -64,13 +64,11 @@ public class SearchingForTestsTask extends Task.Backgroundable { private final TestData myData; private final Project myProject; private final ServerSocket myServerSocket; - private final boolean myIs15; private final TestNGConfiguration myConfig; private final File myTempFile; private final IDEARemoteTestRunnerClient myClient; public SearchingForTestsTask(ServerSocket serverSocket, - boolean is15, TestNGConfiguration config, File tempFile, IDEARemoteTestRunnerClient client) { @@ -79,7 +77,6 @@ public class SearchingForTestsTask extends Task.Backgroundable { myData = config.getPersistantData(); myProject = config.getProject(); myServerSocket = serverSocket; - myIs15 = is15; myConfig = config; myTempFile = tempFile; myClasses = new HashMap>(); @@ -416,31 +413,21 @@ public class SearchingForTestsTask extends Task.Backgroundable { public void run() { if (!dependencies.isEmpty()) { final Project project = classes[0].getProject(); - //we get all classes in the module to figure out which are in the groups we depend on - Collection allClasses; - if (!myIs15) { - allClasses = AllClassesSearch.search(getSearchScope(), project).findAll(); - Map> filteredClasses = TestNGUtil.filterAnnotations("groups", dependencies, allClasses); - //we now have a list of dependencies, and a list of classes that match those dependencies - results.putAll(filteredClasses); - } - else { - final PsiClass testAnnotation = - JavaPsiFacade.getInstance(project).findClass(TestNGUtil.TEST_ANNOTATION_FQN, GlobalSearchScope.allScope(project)); - LOG.assertTrue(testAnnotation != null); - for (PsiMember psiMember : AnnotatedMembersSearch.search(testAnnotation, getSearchScope())) { - if (TestNGUtil - .isAnnotatedWithParameter(AnnotationUtil.findAnnotation(psiMember, TestNGUtil.TEST_ANNOTATION_FQN), "groups", - dependencies)) { - final PsiClass psiClass = psiMember instanceof PsiClass ? ((PsiClass)psiMember) : psiMember.getContainingClass(); - Collection psiMethods = results.get(psiClass); - if (psiMethods == null) { - psiMethods = new LinkedHashSet(); - results.put(psiClass, psiMethods); - } - if (psiMember instanceof PsiMethod) { - psiMethods.add((PsiMethod)psiMember); - } + final PsiClass testAnnotation = + JavaPsiFacade.getInstance(project).findClass(TestNGUtil.TEST_ANNOTATION_FQN, GlobalSearchScope.allScope(project)); + LOG.assertTrue(testAnnotation != null); + for (PsiMember psiMember : AnnotatedMembersSearch.search(testAnnotation, getSearchScope())) { + if (TestNGUtil + .isAnnotatedWithParameter(AnnotationUtil.findAnnotation(psiMember, TestNGUtil.TEST_ANNOTATION_FQN), "groups", + dependencies)) { + final PsiClass psiClass = psiMember instanceof PsiClass ? ((PsiClass)psiMember) : psiMember.getContainingClass(); + Collection psiMethods = results.get(psiClass); + if (psiMethods == null) { + psiMethods = new LinkedHashSet(); + results.put(psiClass, psiMethods); + } + if (psiMember instanceof PsiMethod) { + psiMethods.add((PsiMethod)psiMember); } } } diff --git a/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGRunnableState.java b/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGRunnableState.java index e7fcaff77b73..6baf0b0d4bc4 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGRunnableState.java +++ b/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGRunnableState.java @@ -89,6 +89,7 @@ public class TestNGRunnableState extends JavaCommandLineState { private String debugPort; private File myTempFile; private BackgroundableProcessIndicator mySearchForTestIndicator; + private ServerSocket myServerSocket; public TestNGRunnableState(ExecutionEnvironment environment, TestNGConfiguration config) { super(environment); @@ -117,16 +118,7 @@ public class TestNGRunnableState extends JavaCommandLineState { @Override public ExecutionResult execute(@NotNull final Executor executor, @NotNull final ProgramRunner runner) throws ExecutionException { - OSProcessHandler processHandler = null; - try { - processHandler = startProcess(); - } - catch (ExecutionException e) { - if (mySearchForTestIndicator != null && !mySearchForTestIndicator.isCanceled()) { - mySearchForTestIndicator.cancel(); - } - throw e; - } + OSProcessHandler processHandler = startProcess(); final TreeRootNode unboundOutputRoot = new TreeRootNode(); final TestNGConsoleView console = new TestNGConsoleView(config, runnerSettings, myConfigurationPerRunnerSettings, unboundOutputRoot, executor); @@ -167,7 +159,22 @@ public class TestNGRunnableState extends JavaCommandLineState { public void startNotified(final ProcessEvent event) { TestNGRemoteListener listener = new TestNGRemoteListener(console, unboundOutputRoot); client.prepareListening(listener, port); - + final SearchingForTestsTask task = createSearchingForTestsTask(myServerSocket, config, myTempFile); + mySearchForTestIndicator = new BackgroundableProcessIndicator(task) { + @Override + public void cancel() { + try {//ensure that serverSocket.accept was interrupted + if (!myServerSocket.isClosed()) { + new Socket(InetAddress.getLocalHost(), myServerSocket.getLocalPort()); + } + } + catch (Throwable e) { + LOG.info(e); + } + super.cancel(); + } + }; + ProgressManagerImpl.runProcessWithProgressAsynchronously(task, mySearchForTestIndicator); } @Override @@ -324,27 +331,11 @@ public class TestNGRunnableState extends JavaCommandLineState { javaParameters.getProgramParametersList().add(TestNGCommandLineArgs.SRC_COMMAND_OPT, sb.toString()); }*/ try { - final ServerSocket serverSocket = new ServerSocket(0, 0, InetAddress.getByName(null)); - javaParameters.getProgramParametersList().add("-socket" + serverSocket.getLocalPort()); + myServerSocket = new ServerSocket(0, 0, InetAddress.getByName(null)); + javaParameters.getProgramParametersList().add("-socket" + myServerSocket.getLocalPort()); myTempFile = FileUtil.createTempFile("idea_testng", ".tmp"); myTempFile.deleteOnExit(); javaParameters.getProgramParametersList().add("-temp", myTempFile.getAbsolutePath()); - final SearchingForTestsTask task = createSearchingForTestsTask(serverSocket, is15, config, myTempFile); - mySearchForTestIndicator = new BackgroundableProcessIndicator(task) { - @Override - public void cancel() { - try {//ensure that serverSocket.accept was interrupted - if (!serverSocket.isClosed()) { - new Socket(InetAddress.getLocalHost(), serverSocket.getLocalPort()); - } - } - catch (Throwable e) { - LOG.info(e); - } - super.cancel(); - } - }; - ProgressManagerImpl.runProcessWithProgressAsynchronously(task, mySearchForTestIndicator); } catch (IOException e) { LOG.error(e); @@ -367,9 +358,9 @@ public class TestNGRunnableState extends JavaCommandLineState { return javaParameters; } - protected SearchingForTestsTask createSearchingForTestsTask(ServerSocket serverSocket, boolean is15, + protected SearchingForTestsTask createSearchingForTestsTask(ServerSocket serverSocket, final TestNGConfiguration config, final File tempFile) { - return new SearchingForTestsTask(serverSocket, is15, config, tempFile, client); + return new SearchingForTestsTask(serverSocket, config, tempFile, client); } } diff --git a/plugins/testng/src/com/theoryinpractice/testng/ui/actions/RerunFailedTestsAction.java b/plugins/testng/src/com/theoryinpractice/testng/ui/actions/RerunFailedTestsAction.java index 8cc6c90fd38c..633ab0a37a07 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/ui/actions/RerunFailedTestsAction.java +++ b/plugins/testng/src/com/theoryinpractice/testng/ui/actions/RerunFailedTestsAction.java @@ -50,9 +50,9 @@ public class RerunFailedTestsAction extends AbstractRerunFailedTestsAction { return new TestNGRunnableState(env, configuration) { @Override - protected SearchingForTestsTask createSearchingForTestsTask(ServerSocket serverSocket, boolean is15, + protected SearchingForTestsTask createSearchingForTestsTask(ServerSocket serverSocket, final TestNGConfiguration config, final File tempFile) { - return new SearchingForTestsTask(serverSocket, is15, config, tempFile, client) { + return new SearchingForTestsTask(serverSocket, config, tempFile, client) { @Override protected void fillTestObjects(final Map> classes) throws CantRunException { for (AbstractTestProxy proxy : failedTests) {