mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-78962 JUnit test filters based on regular expressions: inclusion
This commit is contained in:
@@ -67,7 +67,7 @@ public class ConfigurationUtil {
|
||||
if (containingClass instanceof PsiAnonymousClass) return;
|
||||
if (containingClass.hasModifierProperty(PsiModifier.ABSTRACT)) return;
|
||||
if (containingClass.getContainingClass() != null && !containingClass.hasModifierProperty(PsiModifier.STATIC)) return;
|
||||
if (JUnitUtil.isSuiteMethod(method)) {
|
||||
if (JUnitUtil.isSuiteMethod(method) && testClassFilter.isAccepted(containingClass)) {
|
||||
found.add(containingClass);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -503,7 +503,8 @@ public class JUnitConfiguration extends ModuleBasedConfiguration<JavaRunConfigur
|
||||
if (TEST_PATTERN.equals(TEST_OBJECT)) {
|
||||
final int size = myPattern.size();
|
||||
if (size == 0) return "Temp suite";
|
||||
return StringUtil.getShortName(myPattern.iterator().next()) + (size > 1 ? " and " + (size - 1) + " more" : "");
|
||||
final String fqName = myPattern.iterator().next();
|
||||
return (fqName.contains("*") ? fqName : StringUtil.getShortName(fqName)) + (size > 1 ? " and " + (size - 1) + " more" : "");
|
||||
}
|
||||
final String className = JavaExecutionUtil.getPresentableClassName(getMainClassName(), configurationModule);
|
||||
if (TEST_METHOD.equals(TEST_OBJECT)) {
|
||||
|
||||
@@ -28,6 +28,8 @@ import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.util.PsiUtilBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class TestClassFilter implements ClassFilter.ClassFilterWithScope {
|
||||
private final PsiClass myBase;
|
||||
private final Project myProject;
|
||||
@@ -60,6 +62,18 @@ public class TestClassFilter implements ClassFilter.ClassFilterWithScope {
|
||||
return new TestClassFilter(testCase, sourceScope.getGlobalSearchScope());
|
||||
}
|
||||
|
||||
public static TestClassFilter create(final SourceScope sourceScope, Module module, final String pattern) throws JUnitUtil.NoJUnitException {
|
||||
if (sourceScope == null) throw new JUnitUtil.NoJUnitException();
|
||||
PsiClass testCase = module == null ? JUnitUtil.getTestCaseClass(sourceScope) : JUnitUtil.getTestCaseClass(module);
|
||||
return new TestClassFilter(testCase, sourceScope.getGlobalSearchScope()){
|
||||
private final Pattern myPattern = Pattern.compile(pattern);
|
||||
@Override
|
||||
public boolean isAccepted(PsiClass aClass) {
|
||||
return super.isAccepted(aClass) && myPattern.matcher(aClass.getQualifiedName()).matches();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public GlobalSearchScope getScope() { return myScope; }
|
||||
public PsiClass getBase() { return myBase; }
|
||||
}
|
||||
|
||||
@@ -69,8 +69,8 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class TestPackage extends TestObject {
|
||||
private BackgroundableProcessIndicator mySearchForTestsIndicator;
|
||||
private ServerSocket myServerSocket;
|
||||
protected BackgroundableProcessIndicator mySearchForTestsIndicator;
|
||||
protected ServerSocket myServerSocket;
|
||||
private boolean myFoundTests = true;
|
||||
|
||||
public TestPackage(final Project project,
|
||||
@@ -89,12 +89,12 @@ public class TestPackage extends TestObject {
|
||||
@Override
|
||||
protected JUnitProcessHandler createHandler() throws ExecutionException {
|
||||
final JUnitProcessHandler handler = super.createHandler();
|
||||
final MySearchForTestsTask[] tasks = new MySearchForTestsTask[1];
|
||||
final SearchForTestsTask[] tasks = new SearchForTestsTask[1];
|
||||
handler.addProcessListener(new ProcessAdapter() {
|
||||
@Override
|
||||
public void startNotified(ProcessEvent event) {
|
||||
super.startNotified(event);
|
||||
tasks[0] = (MySearchForTestsTask)findTests();
|
||||
tasks[0] = (SearchForTestsTask)findTests();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -138,7 +138,7 @@ public class TestPackage extends TestObject {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}, getPackage(data).getQualifiedName(), false, isJunit4);
|
||||
}, getPackageName(data), false, isJunit4);
|
||||
}
|
||||
catch (CantRunException e) {
|
||||
//can't be here
|
||||
@@ -147,24 +147,15 @@ public class TestPackage extends TestObject {
|
||||
}, filter);
|
||||
}
|
||||
|
||||
protected String getPackageName(JUnitConfiguration.Data data) throws CantRunException {
|
||||
return getPackage(data).getQualifiedName();
|
||||
}
|
||||
|
||||
protected void initialize() throws ExecutionException {
|
||||
super.initialize();
|
||||
final JUnitConfiguration.Data data = myConfiguration.getPersistentData();
|
||||
getClassFilter(data);//check if junit found
|
||||
final ExecutionException[] exception = new ExecutionException[1];
|
||||
ApplicationManager.getApplication().runReadAction(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
myConfiguration.configureClasspath(myJavaParameters);
|
||||
}
|
||||
catch (CantRunException e) {
|
||||
exception[0] = e;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (exception[0] != null) {
|
||||
throw exception[0];
|
||||
}
|
||||
configureClasspath();
|
||||
|
||||
try {
|
||||
myTempFile = FileUtil.createTempFile("idea_junit", ".tmp");
|
||||
@@ -184,7 +175,24 @@ public class TestPackage extends TestObject {
|
||||
}
|
||||
}
|
||||
|
||||
private TestClassFilter getClassFilter(final JUnitConfiguration.Data data) throws CantRunException {
|
||||
protected void configureClasspath() throws ExecutionException {
|
||||
final ExecutionException[] exception = new ExecutionException[1];
|
||||
ApplicationManager.getApplication().runReadAction(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
myConfiguration.configureClasspath(myJavaParameters);
|
||||
}
|
||||
catch (CantRunException e) {
|
||||
exception[0] = e;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (exception[0] != null) {
|
||||
throw exception[0];
|
||||
}
|
||||
}
|
||||
|
||||
protected TestClassFilter getClassFilter(final JUnitConfiguration.Data data) throws CantRunException {
|
||||
Module module = myConfiguration.getConfigurationModule().getModule();
|
||||
if (myConfiguration.getPersistentData().getScope() == TestSearchScope.WHOLE_PROJECT){
|
||||
module = null;
|
||||
@@ -320,8 +328,55 @@ public class TestPackage extends TestObject {
|
||||
void found(@NotNull Collection<PsiClass> classes, final boolean isJunit4);
|
||||
}
|
||||
|
||||
private class MySearchForTestsTask extends Task.Backgroundable {
|
||||
private Socket mySocket;
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DumbModeAction getDumbModeAction() {
|
||||
return DumbModeAction.WAIT;
|
||||
}
|
||||
}
|
||||
private class MySearchForTestsTask extends SearchForTestsTask {
|
||||
private final TestClassFilter myClassFilter;
|
||||
private final boolean[] myJunit4;
|
||||
private final THashSet<PsiClass> myClasses;
|
||||
@@ -355,45 +410,6 @@ public class TestPackage extends TestObject {
|
||||
myCallback.found(myClasses, myJunit4[0]);
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel() {
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DumbModeAction getDumbModeAction() {
|
||||
return DumbModeAction.WAIT;
|
||||
}
|
||||
|
||||
private 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class ResetConfigurationModuleAdapter extends HyperlinkAdapter {
|
||||
|
||||
@@ -20,12 +20,16 @@
|
||||
*/
|
||||
package com.intellij.execution.junit;
|
||||
|
||||
import com.intellij.execution.ExecutionException;
|
||||
import com.intellij.execution.CantRunException;
|
||||
import com.intellij.execution.JavaExecutionUtil;
|
||||
import com.intellij.execution.configurations.*;
|
||||
import com.intellij.execution.util.JavaParametersUtil;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtil;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.Task;
|
||||
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator;
|
||||
import com.intellij.openapi.progress.impl.ProgressManagerImpl;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiClass;
|
||||
@@ -36,13 +40,14 @@ import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.refactoring.listeners.RefactoringElementListener;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.FunctionUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class TestsPattern extends TestObject {
|
||||
public class TestsPattern extends TestPackage {
|
||||
public TestsPattern(final Project project,
|
||||
final JUnitConfiguration configuration,
|
||||
RunnerSettings runnerSettings,
|
||||
@@ -51,41 +56,100 @@ public class TestsPattern extends TestObject {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initialize() throws ExecutionException {
|
||||
super.initialize();
|
||||
protected TestClassFilter getClassFilter(JUnitConfiguration.Data data) throws CantRunException {
|
||||
return TestClassFilter.create(getSourceScope(), myConfiguration.getConfigurationModule().getModule(), data.getPatternPresentation());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPackageName(JUnitConfiguration.Data data) throws CantRunException {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Task findTests() {
|
||||
final JUnitConfiguration.Data data = myConfiguration.getPersistentData();
|
||||
final Project project = myConfiguration.getProject();
|
||||
boolean isJUnit4 = false;
|
||||
final ArrayList<String> classNames = new ArrayList<String>();
|
||||
final Set<Module> modules = new HashSet<Module>();
|
||||
final Set<String> classNames = new HashSet<String>();
|
||||
for (String className : data.getPatterns()) {
|
||||
final PsiClass psiClass = JavaExecutionUtil.findMainClass(project, className.contains(",") ? className.substring(0, className.indexOf(',')) : className, GlobalSearchScope.allScope(project));
|
||||
final PsiClass psiClass = JavaExecutionUtil.findMainClass(project,
|
||||
className.contains(",")
|
||||
? className.substring(0, className.indexOf(','))
|
||||
: className,
|
||||
GlobalSearchScope.allScope(project));
|
||||
if (psiClass != null && JUnitUtil.isTestClass(psiClass)) {
|
||||
classNames.add(className);
|
||||
modules.add(ModuleUtil.findModuleForPsiElement(psiClass));
|
||||
if (JUnitUtil.isJUnit4TestClass(psiClass)) {
|
||||
isJUnit4 = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (classNames.size() == data.getPatterns().size()) {
|
||||
final SearchForTestsTask task = new SearchForTestsTask(project, "Searching for tests...", true) {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
finish();
|
||||
}
|
||||
};
|
||||
mySearchForTestsIndicator = new BackgroundableProcessIndicator(task);
|
||||
ProgressManagerImpl.runProcessWithProgressAsynchronously(task, mySearchForTestsIndicator);
|
||||
return task;
|
||||
}
|
||||
|
||||
return super.findTests();
|
||||
}
|
||||
|
||||
protected void configureClasspath() throws CantRunException {
|
||||
final JUnitConfiguration.Data data = myConfiguration.getPersistentData();
|
||||
final Project project = myConfiguration.getProject();
|
||||
final Set<Module> modules = new HashSet<Module>();
|
||||
for (String className : data.getPatterns()) {
|
||||
final PsiClass psiClass = JavaExecutionUtil.findMainClass(project,
|
||||
className.contains(",")
|
||||
? className.substring(0, className.indexOf(','))
|
||||
: className,
|
||||
GlobalSearchScope.allScope(project));
|
||||
if (psiClass != null && JUnitUtil.isTestClass(psiClass)) {
|
||||
modules.add(ModuleUtil.findModuleForPsiElement(psiClass));
|
||||
}
|
||||
}
|
||||
|
||||
final String jreHome = myConfiguration.isAlternativeJrePathEnabled() ? myConfiguration.getAlternativeJrePath() : null;
|
||||
|
||||
Module module = myConfiguration.getConfigurationModule().getModule();
|
||||
if (module == null && modules.size() == 1 && modules.iterator().next() != null) {
|
||||
module = modules.iterator().next();
|
||||
if (module == null && modules.size() == 1) {
|
||||
final Module nextModule = modules.iterator().next();
|
||||
if (nextModule != null) {
|
||||
module = nextModule;
|
||||
}
|
||||
}
|
||||
|
||||
if (module != null) {
|
||||
JavaParametersUtil.configureModule(module, myJavaParameters, JavaParameters.JDK_AND_CLASSES_AND_TESTS, jreHome);
|
||||
} else {
|
||||
JavaParametersUtil.configureProject(project, myJavaParameters, JavaParameters.JDK_AND_CLASSES_AND_TESTS, jreHome);
|
||||
}
|
||||
addClassesListToJavaParameters(classNames, StringUtil.isEmpty(data.METHOD_NAME) ? FunctionUtil.<String>id() : new Function<String, String>() {
|
||||
@Override
|
||||
public String fun(String className) {
|
||||
return className;
|
||||
}
|
||||
}, "", true, isJUnit4);
|
||||
else {
|
||||
JavaParametersUtil
|
||||
.configureProject(myConfiguration.getProject(), myJavaParameters, JavaParameters.JDK_AND_CLASSES_AND_TESTS, jreHome);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -124,10 +188,7 @@ public class TestsPattern extends TestObject {
|
||||
for (String pattern : patterns) {
|
||||
final String className = pattern.contains(",") ? StringUtil.getPackageName(pattern, ',') : pattern;
|
||||
final PsiClass psiClass = JavaExecutionUtil.findMainClass(myConfiguration.getProject(), className, searchScope);
|
||||
if (psiClass == null) {
|
||||
throw new RuntimeConfigurationWarning("Class " + className + " not found");
|
||||
}
|
||||
if (!JUnitUtil.isTestClass(psiClass)) {
|
||||
if (psiClass != null && !JUnitUtil.isTestClass(psiClass)) {
|
||||
throw new RuntimeConfigurationWarning("Class " + className + " not a test");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user