mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
search for tests task common for java test frameworks
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.execution.testframework;
|
||||
|
||||
import com.intellij.execution.CantRunException;
|
||||
import com.intellij.execution.ExecutionBundle;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.progress.ProcessCanceledException;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.Task;
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public abstract class SearchForTestsTask extends Task.Backgroundable {
|
||||
|
||||
private static final Logger LOG = Logger.getInstance("#" + SearchForTestsTask.class.getName());
|
||||
protected Socket mySocket;
|
||||
private ServerSocket myServerSocket;
|
||||
|
||||
public SearchForTestsTask(@Nullable final Project project,
|
||||
@NotNull final ServerSocket socket) {
|
||||
super(project, ExecutionBundle.message("seaching.test.progress.title"), true);
|
||||
myServerSocket = socket;
|
||||
}
|
||||
|
||||
|
||||
protected abstract void search() throws CantRunException;
|
||||
protected abstract void onFound();
|
||||
|
||||
@Override
|
||||
public void run(@NotNull ProgressIndicator indicator) {
|
||||
try {
|
||||
mySocket = myServerSocket.accept();
|
||||
final CantRunException[] ex = new CantRunException[1];
|
||||
DumbService.getInstance(getProject()).repeatUntilPassesInSmartMode(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
search();
|
||||
}
|
||||
catch (CantRunException e) {
|
||||
ex[0] = e;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (ex[0] != null) throw ex[0];
|
||||
}
|
||||
catch (ProcessCanceledException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel() {
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
DumbService.getInstance(getProject()).runWhenSmart(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
onFound();
|
||||
finish();
|
||||
startListening();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void finish() {
|
||||
DataOutputStream os = null;
|
||||
try {
|
||||
if (mySocket == null || mySocket.isClosed()) return;
|
||||
os = new DataOutputStream(mySocket.getOutputStream());
|
||||
os.writeBoolean(true);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (os != null) os.close();
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
|
||||
try {
|
||||
if (!myServerSocket.isClosed()) {
|
||||
myServerSocket.close();
|
||||
}
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void startListening() {}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ public class TestMethods extends TestMethod {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}, data.getPackageName(), true, false);
|
||||
}, data.getPackageName(), true);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -520,8 +520,7 @@ public abstract class TestObject implements JavaCommandLine {
|
||||
}
|
||||
|
||||
protected <T> void addClassesListToJavaParameters(Collection<? extends T> elements, Function<T, String> nameFunction, String packageName,
|
||||
boolean createTempFile,
|
||||
boolean junit4) throws CantRunException {
|
||||
boolean createTempFile) throws CantRunException {
|
||||
try {
|
||||
if (createTempFile) {
|
||||
createTempFiles();
|
||||
|
||||
@@ -27,18 +27,16 @@ import com.intellij.execution.process.ProcessAdapter;
|
||||
import com.intellij.execution.process.ProcessEvent;
|
||||
import com.intellij.execution.runners.ExecutionEnvironment;
|
||||
import com.intellij.execution.runners.ExecutionEnvironmentBuilder;
|
||||
import com.intellij.execution.testframework.SearchForTestsTask;
|
||||
import com.intellij.execution.testframework.SourceScope;
|
||||
import com.intellij.execution.testframework.TestSearchScope;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleManager;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
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 com.intellij.openapi.ui.MessageType;
|
||||
import com.intellij.openapi.ui.popup.Balloon;
|
||||
@@ -57,11 +55,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.event.HyperlinkEvent;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@@ -118,7 +114,7 @@ public class TestPackage extends TestObject {
|
||||
|
||||
return findTestsWithProgress(new FindCallback() {
|
||||
@Override
|
||||
public void found(@NotNull final Collection<PsiClass> classes, final boolean isJunit4) {
|
||||
public void found(@NotNull final Collection<PsiClass> classes) {
|
||||
try {
|
||||
addClassesListToJavaParameters(classes, new Function<PsiElement, String>() {
|
||||
@Override
|
||||
@@ -135,7 +131,7 @@ public class TestPackage extends TestObject {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}, getPackageName(data), false, isJunit4);
|
||||
}, getPackageName(data), false);
|
||||
}
|
||||
catch (CantRunException ignored) {
|
||||
//can't be here
|
||||
@@ -254,15 +250,14 @@ public class TestPackage extends TestObject {
|
||||
private MySearchForTestsTask findTestsWithProgress(final FindCallback callback, final TestClassFilter classFilter) {
|
||||
if (isSyncSearch()) {
|
||||
THashSet<PsiClass> classes = new THashSet<PsiClass>();
|
||||
boolean isJUnit4 = ConfigurationUtil.findAllTestClasses(classFilter, classes);
|
||||
callback.found(classes, isJUnit4);
|
||||
ConfigurationUtil.findAllTestClasses(classFilter, classes);
|
||||
callback.found(classes);
|
||||
return null;
|
||||
}
|
||||
|
||||
final THashSet<PsiClass> classes = new THashSet<PsiClass>();
|
||||
final boolean[] isJunit4 = new boolean[1];
|
||||
final MySearchForTestsTask task =
|
||||
new MySearchForTestsTask(classFilter, isJunit4, classes, callback);
|
||||
new MySearchForTestsTask(classFilter, classes, callback, myServerSocket);
|
||||
mySearchForTestsIndicator = new BackgroundableProcessIndicator(task);
|
||||
ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, mySearchForTestsIndicator);
|
||||
return task;
|
||||
@@ -323,100 +318,35 @@ public class TestPackage extends TestObject {
|
||||
/**
|
||||
* Invoked in dispatch thread
|
||||
*/
|
||||
void found(@NotNull Collection<PsiClass> classes, final boolean isJunit4);
|
||||
void found(@NotNull Collection<PsiClass> classes);
|
||||
}
|
||||
|
||||
protected abstract class SearchForTestsTask extends Task.Backgroundable {
|
||||
|
||||
protected Socket mySocket;
|
||||
|
||||
public SearchForTestsTask(@Nullable final Project project, @NotNull final String title, final boolean canBeCancelled) {
|
||||
super(project, title, canBeCancelled);
|
||||
}
|
||||
|
||||
|
||||
protected void finish() {
|
||||
DataOutputStream os = null;
|
||||
try {
|
||||
if (mySocket == null || mySocket.isClosed()) return;
|
||||
os = new DataOutputStream(mySocket.getOutputStream());
|
||||
os.writeBoolean(true);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (os != null) os.close();
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
|
||||
try {
|
||||
if (!myServerSocket.isClosed()) {
|
||||
myServerSocket.close();
|
||||
}
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel() {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
private class MySearchForTestsTask extends SearchForTestsTask {
|
||||
private final TestClassFilter myClassFilter;
|
||||
private final boolean[] myJunit4;
|
||||
private final THashSet<PsiClass> myClasses;
|
||||
private final FindCallback myCallback;
|
||||
|
||||
public MySearchForTestsTask(TestClassFilter classFilter, boolean[] junit4, THashSet<PsiClass> classes, FindCallback callback) {
|
||||
super(classFilter.getProject(), ExecutionBundle.message("seaching.test.progress.title"), true);
|
||||
public MySearchForTestsTask(TestClassFilter classFilter,
|
||||
THashSet<PsiClass> classes,
|
||||
FindCallback callback,
|
||||
ServerSocket serverSocket) {
|
||||
super(classFilter.getProject(), serverSocket);
|
||||
myClassFilter = classFilter;
|
||||
myJunit4 = junit4;
|
||||
myClasses = classes;
|
||||
myCallback = callback;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run(@NotNull ProgressIndicator indicator) {
|
||||
try {
|
||||
mySocket = myServerSocket.accept();
|
||||
DumbService.getInstance(getProject()).repeatUntilPassesInSmartMode(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
myClasses.clear();
|
||||
myJunit4[0] = ConfigurationUtil.findAllTestClasses(myClassFilter, myClasses);
|
||||
}
|
||||
});
|
||||
myFoundTests = !myClasses.isEmpty();
|
||||
}
|
||||
catch (ProcessCanceledException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
protected void search() {
|
||||
myClasses.clear();
|
||||
ConfigurationUtil.findAllTestClasses(myClassFilter, myClasses);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
DumbService.getInstance(getProject()).runWhenSmart(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
myCallback.found(myClasses, myJunit4[0]);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
protected void onFound() {
|
||||
myFoundTests = !myClasses.isEmpty();
|
||||
myCallback.found(myClasses);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,9 +26,9 @@ import com.intellij.execution.configurations.JavaParameters;
|
||||
import com.intellij.execution.configurations.RuntimeConfigurationException;
|
||||
import com.intellij.execution.configurations.RuntimeConfigurationWarning;
|
||||
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.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
import com.intellij.openapi.progress.Task;
|
||||
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator;
|
||||
@@ -40,10 +40,8 @@ import com.intellij.refactoring.listeners.RefactoringElementListener;
|
||||
import com.intellij.refactoring.listeners.RefactoringElementListenerComposite;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.FunctionUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -75,33 +73,22 @@ public class TestsPattern extends TestPackage {
|
||||
}
|
||||
|
||||
if (classNames.size() == data.getPatterns().size()) {
|
||||
final SearchForTestsTask task = new SearchForTestsTask(project, "Searching for tests...", true) {
|
||||
final SearchForTestsTask task = new SearchForTestsTask(project, myServerSocket) {
|
||||
@Override
|
||||
public void run(@NotNull ProgressIndicator indicator) {
|
||||
try {
|
||||
mySocket = myServerSocket.accept();
|
||||
addClassesListToJavaParameters(classNames,
|
||||
StringUtil.isEmpty(data.METHOD_NAME)
|
||||
? FunctionUtil.<String>id()
|
||||
: new Function<String, String>() {
|
||||
@Override
|
||||
public String fun(String className) {
|
||||
return className;
|
||||
}
|
||||
}, "", false, true);
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
protected void search() throws CantRunException {
|
||||
final Function<String, String> nameFunction = StringUtil.isEmpty(data.METHOD_NAME)
|
||||
? FunctionUtil.<String>id()
|
||||
: new Function<String, String>() {
|
||||
@Override
|
||||
public String fun(String className) {
|
||||
return className;
|
||||
}
|
||||
};
|
||||
addClassesListToJavaParameters(classNames, nameFunction, "", false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
finish();
|
||||
}
|
||||
protected void onFound() {}
|
||||
};
|
||||
mySearchForTestsIndicator = new BackgroundableProcessIndicator(task);
|
||||
ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, mySearchForTestsIndicator);
|
||||
|
||||
+15
-89
@@ -22,15 +22,12 @@ package com.theoryinpractice.testng.configuration;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationUtil;
|
||||
import com.intellij.execution.CantRunException;
|
||||
import com.intellij.execution.testframework.SearchForTestsTask;
|
||||
import com.intellij.execution.testframework.TestSearchScope;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.progress.ProcessCanceledException;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.Task;
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
@@ -57,18 +54,15 @@ import org.testng.xml.XmlSuite;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
public class SearchingForTestsTask extends Task.Backgroundable {
|
||||
public class SearchingForTestsTask extends SearchForTestsTask {
|
||||
private static final Logger LOG = Logger.getInstance("#" + SearchingForTestsTask.class.getName());
|
||||
private final Map<PsiClass, Collection<PsiMethod>> myClasses;
|
||||
private Socket mySocket;
|
||||
private final TestData myData;
|
||||
private final Project myProject;
|
||||
private final ServerSocket myServerSocket;
|
||||
private final TestNGConfiguration myConfig;
|
||||
private final File myTempFile;
|
||||
private final IDEARemoteTestRunnerClient myClient;
|
||||
@@ -77,96 +71,17 @@ public class SearchingForTestsTask extends Task.Backgroundable {
|
||||
TestNGConfiguration config,
|
||||
File tempFile,
|
||||
IDEARemoteTestRunnerClient client) {
|
||||
super(config.getProject(), "Searching For Tests ...", true);
|
||||
super(config.getProject(), serverSocket);
|
||||
myClient = client;
|
||||
myData = config.getPersistantData();
|
||||
myProject = config.getProject();
|
||||
myServerSocket = serverSocket;
|
||||
myConfig = config;
|
||||
myTempFile = tempFile;
|
||||
myClasses = new LinkedHashMap<PsiClass, Collection<PsiMethod>>();
|
||||
}
|
||||
|
||||
public void run(@NotNull ProgressIndicator indicator) {
|
||||
try {
|
||||
mySocket = myServerSocket.accept();
|
||||
try {
|
||||
final CantRunException[] ex = new CantRunException[1];
|
||||
DumbService.getInstance(myProject).repeatUntilPassesInSmartMode(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
myClasses.clear();
|
||||
try {
|
||||
fillTestObjects(myClasses);
|
||||
}
|
||||
catch (CantRunException e) {
|
||||
ex[0] = e;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (ex[0] != null) throw ex[0];
|
||||
}
|
||||
catch (CantRunException e) {
|
||||
logCantRunException(e);
|
||||
}
|
||||
}
|
||||
catch (ProcessCanceledException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
DumbService.getInstance(myProject).runWhenSmart(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
writeTempFile();
|
||||
finish();
|
||||
|
||||
if (!Registry.is("testng_sm_runner")) myClient.startListening(myConfig);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel() {
|
||||
finish();
|
||||
}
|
||||
|
||||
public void finish() {
|
||||
DataOutputStream os = null;
|
||||
try {
|
||||
if (mySocket == null || mySocket.isClosed()) return;
|
||||
os = new DataOutputStream(mySocket.getOutputStream());
|
||||
os.writeBoolean(true);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (os != null) os.close();
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
|
||||
try {
|
||||
myServerSocket.close();
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void writeTempFile() {
|
||||
protected void onFound() {
|
||||
if (myClasses.size() > 0) {
|
||||
composeTestSuiteFromClasses();
|
||||
}
|
||||
@@ -188,6 +103,17 @@ public class SearchingForTestsTask extends Task.Backgroundable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void search() throws CantRunException {
|
||||
myClasses.clear();
|
||||
fillTestObjects(myClasses);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void startListening() {
|
||||
if (!Registry.is("testng_sm_runner")) myClient.startListening(myConfig);
|
||||
}
|
||||
|
||||
private void logCantRunException(CantRunException e) {
|
||||
try {
|
||||
final String message = "CantRunException" + e.getMessage() + "\n";
|
||||
|
||||
Reference in New Issue
Block a user