|
|
|
|
@@ -65,8 +65,7 @@ import org.springframework.util.StringUtils;
|
|
|
|
|
*/
|
|
|
|
|
public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
|
|
|
|
|
|
|
|
|
private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern
|
|
|
|
|
.compile(".*classpath(\\d+)?.jar");
|
|
|
|
|
private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern.compile(".*classpath(\\d+)?.jar");
|
|
|
|
|
|
|
|
|
|
public ModifiedClassPathRunner(Class<?> testClass) throws InitializationError {
|
|
|
|
|
super(testClass);
|
|
|
|
|
@@ -98,9 +97,8 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
|
|
|
|
|
|
|
|
|
private URLClassLoader createTestClassLoader(Class<?> testClass) throws Exception {
|
|
|
|
|
URLClassLoader classLoader = (URLClassLoader) this.getClass().getClassLoader();
|
|
|
|
|
return new ModifiedClassPathClassLoader(
|
|
|
|
|
processUrls(extractUrls(classLoader), testClass), classLoader.getParent(),
|
|
|
|
|
classLoader);
|
|
|
|
|
return new ModifiedClassPathClassLoader(processUrls(extractUrls(classLoader), testClass),
|
|
|
|
|
classLoader.getParent(), classLoader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private URL[] extractUrls(URLClassLoader classLoader) throws Exception {
|
|
|
|
|
@@ -150,8 +148,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
|
|
|
|
|
|
|
|
|
private String[] getClassPath(URL booterJar) throws Exception {
|
|
|
|
|
Attributes attributes = getManifestMainAttributesFromUrl(booterJar);
|
|
|
|
|
return StringUtils.delimitedListToStringArray(
|
|
|
|
|
attributes.getValue(Attributes.Name.CLASS_PATH), " ");
|
|
|
|
|
return StringUtils.delimitedListToStringArray(attributes.getValue(Attributes.Name.CLASS_PATH), " ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Attributes getManifestMainAttributesFromUrl(URL url) throws Exception {
|
|
|
|
|
@@ -177,8 +174,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<URL> getAdditionalUrls(Class<?> testClass) throws Exception {
|
|
|
|
|
ClassPathOverrides overrides = AnnotationUtils.findAnnotation(testClass,
|
|
|
|
|
ClassPathOverrides.class);
|
|
|
|
|
ClassPathOverrides overrides = AnnotationUtils.findAnnotation(testClass, ClassPathOverrides.class);
|
|
|
|
|
if (overrides == null) {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
@@ -186,26 +182,19 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<URL> resolveCoordinates(String[] coordinates) throws Exception {
|
|
|
|
|
DefaultServiceLocator serviceLocator = MavenRepositorySystemUtils
|
|
|
|
|
.newServiceLocator();
|
|
|
|
|
serviceLocator.addService(RepositoryConnectorFactory.class,
|
|
|
|
|
BasicRepositoryConnectorFactory.class);
|
|
|
|
|
DefaultServiceLocator serviceLocator = MavenRepositorySystemUtils.newServiceLocator();
|
|
|
|
|
serviceLocator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
|
|
|
|
|
serviceLocator.addService(TransporterFactory.class, HttpTransporterFactory.class);
|
|
|
|
|
RepositorySystem repositorySystem = serviceLocator
|
|
|
|
|
.getService(RepositorySystem.class);
|
|
|
|
|
RepositorySystem repositorySystem = serviceLocator.getService(RepositorySystem.class);
|
|
|
|
|
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
|
|
|
|
|
LocalRepository localRepository = new LocalRepository(
|
|
|
|
|
System.getProperty("user.home") + "/.m2/repository");
|
|
|
|
|
session.setLocalRepositoryManager(
|
|
|
|
|
repositorySystem.newLocalRepositoryManager(session, localRepository));
|
|
|
|
|
CollectRequest collectRequest = new CollectRequest(null,
|
|
|
|
|
Arrays.asList(new RemoteRepository.Builder("central", "default",
|
|
|
|
|
"https://repo.maven.apache.org/maven2").build()));
|
|
|
|
|
LocalRepository localRepository = new LocalRepository(System.getProperty("user.home") + "/.m2/repository");
|
|
|
|
|
session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepository));
|
|
|
|
|
CollectRequest collectRequest = new CollectRequest(null, Arrays.asList(
|
|
|
|
|
new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2").build()));
|
|
|
|
|
|
|
|
|
|
collectRequest.setDependencies(createDependencies(coordinates));
|
|
|
|
|
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);
|
|
|
|
|
DependencyResult result = repositorySystem.resolveDependencies(session,
|
|
|
|
|
dependencyRequest);
|
|
|
|
|
DependencyResult result = repositorySystem.resolveDependencies(session, dependencyRequest);
|
|
|
|
|
List<URL> resolvedArtifacts = new ArrayList<URL>();
|
|
|
|
|
for (ArtifactResult artifact : result.getArtifactResults()) {
|
|
|
|
|
resolvedArtifacts.add(artifact.getArtifact().getFile().toURI().toURL());
|
|
|
|
|
@@ -231,8 +220,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
|
|
|
|
private final AntPathMatcher matcher = new AntPathMatcher();
|
|
|
|
|
|
|
|
|
|
private ClassPathEntryFilter(Class<?> testClass) throws Exception {
|
|
|
|
|
ClassPathExclusions exclusions = AnnotationUtils.findAnnotation(testClass,
|
|
|
|
|
ClassPathExclusions.class);
|
|
|
|
|
ClassPathExclusions exclusions = AnnotationUtils.findAnnotation(testClass, ClassPathExclusions.class);
|
|
|
|
|
this.exclusions = (exclusions != null) ? Arrays.asList(exclusions.value())
|
|
|
|
|
: Collections.<String>emptyList();
|
|
|
|
|
}
|
|
|
|
|
@@ -259,15 +247,13 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
|
|
|
|
|
|
|
|
|
private final ClassLoader classLoader;
|
|
|
|
|
|
|
|
|
|
ModifiedClassPathTestClass(ClassLoader classLoader, String testClassName)
|
|
|
|
|
throws ClassNotFoundException {
|
|
|
|
|
ModifiedClassPathTestClass(ClassLoader classLoader, String testClassName) throws ClassNotFoundException {
|
|
|
|
|
super(classLoader.loadClass(testClassName));
|
|
|
|
|
this.classLoader = classLoader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<FrameworkMethod> getAnnotatedMethods(
|
|
|
|
|
Class<? extends Annotation> annotationClass) {
|
|
|
|
|
public List<FrameworkMethod> getAnnotatedMethods(Class<? extends Annotation> annotationClass) {
|
|
|
|
|
try {
|
|
|
|
|
return getAnnotatedMethods(annotationClass.getName());
|
|
|
|
|
}
|
|
|
|
|
@@ -277,29 +263,24 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private List<FrameworkMethod> getAnnotatedMethods(String annotationClassName)
|
|
|
|
|
throws ClassNotFoundException {
|
|
|
|
|
private List<FrameworkMethod> getAnnotatedMethods(String annotationClassName) throws ClassNotFoundException {
|
|
|
|
|
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) this.classLoader
|
|
|
|
|
.loadClass(annotationClassName);
|
|
|
|
|
List<FrameworkMethod> methods = super.getAnnotatedMethods(annotationClass);
|
|
|
|
|
return wrapFrameworkMethods(methods);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<FrameworkMethod> wrapFrameworkMethods(
|
|
|
|
|
List<FrameworkMethod> methods) {
|
|
|
|
|
List<FrameworkMethod> wrapped = new ArrayList<FrameworkMethod>(
|
|
|
|
|
methods.size());
|
|
|
|
|
private List<FrameworkMethod> wrapFrameworkMethods(List<FrameworkMethod> methods) {
|
|
|
|
|
List<FrameworkMethod> wrapped = new ArrayList<FrameworkMethod>(methods.size());
|
|
|
|
|
for (FrameworkMethod frameworkMethod : methods) {
|
|
|
|
|
wrapped.add(new ModifiedClassPathFrameworkMethod(
|
|
|
|
|
frameworkMethod.getMethod()));
|
|
|
|
|
wrapped.add(new ModifiedClassPathFrameworkMethod(frameworkMethod.getMethod()));
|
|
|
|
|
}
|
|
|
|
|
return wrapped;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private <T, E extends Throwable> T doWithModifiedClassPathThreadContextClassLoader(
|
|
|
|
|
ModifiedClassPathTcclAction<T, E> action) throws E {
|
|
|
|
|
ClassLoader originalClassLoader = Thread.currentThread()
|
|
|
|
|
.getContextClassLoader();
|
|
|
|
|
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
|
|
|
|
|
Thread.currentThread().setContextClassLoader(this.classLoader);
|
|
|
|
|
try {
|
|
|
|
|
return action.perform();
|
|
|
|
|
@@ -330,15 +311,13 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object invokeExplosively(final Object target, final Object... params)
|
|
|
|
|
throws Throwable {
|
|
|
|
|
public Object invokeExplosively(final Object target, final Object... params) throws Throwable {
|
|
|
|
|
return doWithModifiedClassPathThreadContextClassLoader(
|
|
|
|
|
new ModifiedClassPathTcclAction<Object, Throwable>() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object perform() throws Throwable {
|
|
|
|
|
return ModifiedClassPathFrameworkMethod.super.invokeExplosively(
|
|
|
|
|
target, params);
|
|
|
|
|
return ModifiedClassPathFrameworkMethod.super.invokeExplosively(target, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
@@ -355,8 +334,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
|
|
|
|
|
|
|
|
|
private final ClassLoader junitLoader;
|
|
|
|
|
|
|
|
|
|
ModifiedClassPathClassLoader(URL[] urls, ClassLoader parent,
|
|
|
|
|
ClassLoader junitLoader) {
|
|
|
|
|
ModifiedClassPathClassLoader(URL[] urls, ClassLoader parent, ClassLoader junitLoader) {
|
|
|
|
|
super(urls, parent);
|
|
|
|
|
this.junitLoader = junitLoader;
|
|
|
|
|
}
|
|
|
|
|
|