mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
prepare for search in the classpath for testng
This commit is contained in:
@@ -19,6 +19,7 @@ import com.intellij.execution.configurations.ConfigurationFactory;
|
||||
import com.intellij.execution.configurations.JavaRunConfigurationModule;
|
||||
import com.intellij.execution.configurations.ModuleBasedConfiguration;
|
||||
import com.intellij.execution.configurations.RefactoringListenerProvider;
|
||||
import com.intellij.execution.testframework.TestSearchScope;
|
||||
import com.intellij.execution.testframework.sm.runner.SMRunnerConsolePropertiesProvider;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
@@ -54,4 +55,6 @@ public abstract class JavaTestConfigurationBase extends ModuleBasedConfiguration
|
||||
public String prepareParameterizedParameter(String paramSetName) {
|
||||
return paramSetName;
|
||||
}
|
||||
|
||||
public abstract TestSearchScope getTestSearchScope();
|
||||
}
|
||||
|
||||
+7
-31
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.execution.junit;
|
||||
package com.intellij.execution;
|
||||
|
||||
import com.intellij.execution.testframework.TestSearchScope;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
@@ -30,20 +30,20 @@ import com.intellij.util.lang.UrlClassLoader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class TestClassCollector {
|
||||
|
||||
private static final Logger LOG = Logger.getInstance(TestClassCollector.class);
|
||||
|
||||
public static String[] collectClassFQNames(String packageName, JUnitConfiguration configuration) {
|
||||
public static String[] collectClassFQNames(String packageName, JavaTestConfigurationBase configuration, Function<ClassLoader, Predicate<Class<?>>> predicateProducer) {
|
||||
Module module = configuration.getConfigurationModule().getModule();
|
||||
List<URL> urls = new ArrayList<>();
|
||||
|
||||
@@ -59,7 +59,7 @@ public class TestClassCollector {
|
||||
}
|
||||
|
||||
Path rootPath = null;
|
||||
if (configuration.getPersistentData().getScope() == TestSearchScope.SINGLE_MODULE) {
|
||||
if (configuration.getTestSearchScope() == TestSearchScope.SINGLE_MODULE) {
|
||||
CompilerModuleExtension moduleExtension = CompilerModuleExtension.getInstance(module);
|
||||
if (moduleExtension != null) {
|
||||
VirtualFile tests = moduleExtension.getCompilerOutputPathForTests();
|
||||
@@ -75,14 +75,7 @@ public class TestClassCollector {
|
||||
String packagePath = packageName.replace('.', '/');
|
||||
Enumeration<URL> resources = classLoader.getResources(packagePath);
|
||||
|
||||
Class<?> testCaseClass = Class.forName("junit.framework.TestCase", true, classLoader);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<? extends Annotation> runWithClass = (Class<? extends Annotation>)Class.forName("org.junit.runner.RunWith", true, classLoader);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<? extends Annotation> testClass = (Class<? extends Annotation>)Class.forName("org.junit.Test", true, classLoader);
|
||||
|
||||
Predicate<Class<?>> classPredicate = predicateProducer.apply(classLoader);
|
||||
while (resources.hasMoreElements()) {
|
||||
URL url = resources.nextElement();
|
||||
Path baseDir = Paths.get(url.toURI());
|
||||
@@ -111,26 +104,9 @@ public class TestClassCollector {
|
||||
aClass.isMemberClass() && !Modifier.isStatic(modifiers)) {
|
||||
return result;
|
||||
}
|
||||
//junit 3
|
||||
if (testCaseClass.isAssignableFrom(aClass)) {
|
||||
if (classPredicate.test(aClass)) {
|
||||
classes.add(fqName);
|
||||
}
|
||||
else {
|
||||
//annotation
|
||||
if (aClass.isAnnotationPresent(runWithClass)) {
|
||||
classes.add(fqName);
|
||||
}
|
||||
else {
|
||||
//junit 4 & suite
|
||||
for (Method method : aClass.getMethods()) {
|
||||
if (Modifier.isStatic(method.getModifiers()) && "suite".equals(method.getName()) ||
|
||||
method.isAnnotationPresent(testClass)) {
|
||||
classes.add(fqName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Throwable e) {
|
||||
LOG.error(e);
|
||||
@@ -320,6 +320,11 @@ public class JUnitConfiguration extends JavaTestConfigurationBase {
|
||||
return getTestObject().isConfiguredByElement(this, testClass, testMethod, testPackage, testDir);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TestSearchScope getTestSearchScope() {
|
||||
return getPersistentData().getScope();
|
||||
}
|
||||
|
||||
public void beFromSourcePosition(PsiLocation<PsiMethod> sourceLocation) {
|
||||
myData.setTestMethod(sourceLocation);
|
||||
myData.TEST_OBJECT = BY_SOURCE_POSITION;
|
||||
|
||||
@@ -40,7 +40,11 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class TestPackage extends TestObject {
|
||||
|
||||
@@ -73,7 +77,7 @@ public class TestPackage extends TestObject {
|
||||
long start = System.currentTimeMillis();
|
||||
if (Registry.is("junit4.search.4.tests.in.classpath", false)) {
|
||||
String packageName = getPackageName(data);
|
||||
String[] classNames = TestClassCollector.collectClassFQNames(packageName, getConfiguration());
|
||||
String[] classNames = TestClassCollector.collectClassFQNames(packageName, getConfiguration(), TestPackage::createPredicate);
|
||||
PsiManager manager = PsiManager.getInstance(myProject);
|
||||
Arrays.stream(classNames)
|
||||
.filter(className -> acceptClassName(className)) //check patterns
|
||||
@@ -211,4 +215,44 @@ public class TestPackage extends TestObject {
|
||||
public File getWorkingDirsFile() {
|
||||
return myWorkingDirsFile;
|
||||
}
|
||||
|
||||
private static Predicate<Class<?>> createPredicate(ClassLoader classLoader) {
|
||||
|
||||
try {
|
||||
Class<?> testCaseClass = Class.forName("junit.framework.TestCase", true, classLoader);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<? extends Annotation> runWithClass = (Class<? extends Annotation>)Class.forName("org.junit.runner.RunWith", true, classLoader);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<? extends Annotation> testClass = (Class<? extends Annotation>)Class.forName("org.junit.Test", true, classLoader);
|
||||
|
||||
return aClass -> {
|
||||
//junit 3
|
||||
if (testCaseClass.isAssignableFrom(aClass)) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
//annotation
|
||||
if (aClass.isAnnotationPresent(runWithClass)) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
//junit 4 & suite
|
||||
for (Method method : aClass.getMethods()) {
|
||||
if (Modifier.isStatic(method.getModifiers()) && "suite".equals(method.getName()) ||
|
||||
method.isAnnotationPresent(testClass)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
LOG.error(e);
|
||||
return aClass -> false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,6 +228,11 @@ public class TestNGConfiguration extends JavaTestConfigurationBase {
|
||||
return TestNGConfigurationProducer.getInvocationNumber(paramSetName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TestSearchScope getTestSearchScope() {
|
||||
return getPersistantData().getScope();
|
||||
}
|
||||
|
||||
public void setPackageConfiguration(Module module, PsiPackage pkg) {
|
||||
data.setPackage(pkg);
|
||||
setModule(module);
|
||||
|
||||
Reference in New Issue
Block a user