Merge changes from boots ModifiedClassPathRunner
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,6 +28,7 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
|
||||
@@ -66,6 +67,9 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
||||
|
||||
private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern
|
||||
.compile(".*classpath(\\d+)?\\.jar");
|
||||
|
||||
public ModifiedClassPathRunner(Class<?> testClass) throws InitializationError {
|
||||
super(testClass);
|
||||
}
|
||||
@@ -98,14 +102,14 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
||||
private URL[] extractUrls(ClassLoader classLoader) throws Exception {
|
||||
List<URL> extractedUrls = new ArrayList<>();
|
||||
doExtractUrls(classLoader).forEach((URL url) -> {
|
||||
if (isSurefireBooterJar(url)) {
|
||||
if (isManifestOnlyJar(url)) {
|
||||
extractedUrls.addAll(extractUrlsFromManifestClassPath(url));
|
||||
}
|
||||
else {
|
||||
extractedUrls.add(url);
|
||||
}
|
||||
});
|
||||
return extractedUrls.toArray(new URL[extractedUrls.size()]);
|
||||
return extractedUrls.toArray(new URL[0]);
|
||||
}
|
||||
|
||||
private Stream<URL> doExtractUrls(ClassLoader classLoader) throws Exception {
|
||||
@@ -125,10 +129,29 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isManifestOnlyJar(URL url) {
|
||||
return isSurefireBooterJar(url) || isShortenedIntelliJJar(url);
|
||||
}
|
||||
|
||||
private boolean isSurefireBooterJar(URL url) {
|
||||
return url.getPath().contains("surefirebooter");
|
||||
}
|
||||
|
||||
private boolean isShortenedIntelliJJar(URL url) {
|
||||
String urlPath = url.getPath();
|
||||
boolean isCandidate = INTELLIJ_CLASSPATH_JAR_PATTERN.matcher(urlPath).matches();
|
||||
if (isCandidate) {
|
||||
try {
|
||||
Attributes attributes = getManifestMainAttributesFromUrl(url);
|
||||
String createdBy = attributes.getValue("Created-By");
|
||||
return createdBy != null && createdBy.contains("IntelliJ");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private List<URL> extractUrlsFromManifestClassPath(URL booterJar) {
|
||||
List<URL> urls = new ArrayList<>();
|
||||
try {
|
||||
@@ -143,9 +166,14 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
||||
}
|
||||
|
||||
private String[] getClassPath(URL booterJar) throws Exception {
|
||||
try (JarFile jarFile = new JarFile(new File(booterJar.toURI()))) {
|
||||
return StringUtils.delimitedListToStringArray(jarFile.getManifest()
|
||||
.getMainAttributes().getValue(Attributes.Name.CLASS_PATH), " ");
|
||||
Attributes attributes = getManifestMainAttributesFromUrl(booterJar);
|
||||
return StringUtils.delimitedListToStringArray(
|
||||
attributes.getValue(Attributes.Name.CLASS_PATH), " ");
|
||||
}
|
||||
|
||||
private Attributes getManifestMainAttributesFromUrl(URL url) throws Exception {
|
||||
try (JarFile jarFile = new JarFile(new File(url.toURI()))) {
|
||||
return jarFile.getManifest().getMainAttributes();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +186,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
||||
processedUrls.add(url);
|
||||
}
|
||||
}
|
||||
return processedUrls.toArray(new URL[processedUrls.size()]);
|
||||
return processedUrls.toArray(new URL[0]);
|
||||
}
|
||||
|
||||
private List<URL> getAdditionalUrls(Class<?> testClass) throws Exception {
|
||||
@@ -185,7 +213,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
||||
repositorySystem.newLocalRepositoryManager(session, localRepository));
|
||||
CollectRequest collectRequest = new CollectRequest(null,
|
||||
Arrays.asList(new RemoteRepository.Builder("central", "default",
|
||||
"http://central.maven.org/maven2").build()));
|
||||
"https://repo.maven.apache.org/maven2").build()));
|
||||
|
||||
collectRequest.setDependencies(createDependencies(coordinates));
|
||||
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);
|
||||
@@ -317,7 +345,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invokeExplosively(final Object target, final Object... params)
|
||||
public Object invokeExplosively(Object target, Object... params)
|
||||
throws Throwable {
|
||||
return doWithModifiedClassPathThreadContextClassLoader(
|
||||
() -> ModifiedClassPathFrameworkMethod.super.invokeExplosively(
|
||||
@@ -351,4 +379,4 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user