start search for test in package only when process was started

This commit is contained in:
anna
2011-01-17 17:42:46 +01:00
parent dda9899747
commit 2aa9dbd723
5 changed files with 99 additions and 98 deletions
@@ -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 <T> void addClassesListToJavaParameters(Collection<? extends T> elements, Function<T, String> nameFunction, String packageName,
boolean createTempFile,
@@ -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<PsiClass> classes, final boolean isJunit4) {
addClassesListToJavaParameters(classes, new Function<PsiElement, String>() {
@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<PsiClass> classes, final boolean isJunit4) {
addClassesListToJavaParameters(classes, new Function<PsiElement, String>() {
@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<PsiClass> classes = new THashSet<PsiClass>();
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) {
@@ -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<PsiClass, Collection<PsiMethod>>();
@@ -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<PsiClass> allClasses;
if (!myIs15) {
allClasses = AllClassesSearch.search(getSearchScope(), project).findAll();
Map<PsiClass, Collection<PsiMethod>> 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<PsiMethod> psiMethods = results.get(psiClass);
if (psiMethods == null) {
psiMethods = new LinkedHashSet<PsiMethod>();
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<PsiMethod> psiMethods = results.get(psiClass);
if (psiMethods == null) {
psiMethods = new LinkedHashSet<PsiMethod>();
results.put(psiClass, psiMethods);
}
if (psiMember instanceof PsiMethod) {
psiMethods.add((PsiMethod)psiMember);
}
}
}
@@ -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);
}
}
@@ -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<PsiClass, Collection<PsiMethod>> classes) throws CantRunException {
for (AbstractTestProxy proxy : failedTests) {