Commit 006decea authored by Stephane Nicoll's avatar Stephane Nicoll

Merge branch '1.5.x'

parents 88faabaa 345b2c5d
...@@ -28,6 +28,7 @@ import java.util.Collections; ...@@ -28,6 +28,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.jar.Attributes; import java.util.jar.Attributes;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.regex.Pattern;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils; import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
...@@ -66,6 +67,9 @@ import org.springframework.util.StringUtils; ...@@ -66,6 +67,9 @@ import org.springframework.util.StringUtils;
*/ */
public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern.compile(
".*classpath(\\d+)?.jar");
public ModifiedClassPathRunner(Class<?> testClass) throws InitializationError { public ModifiedClassPathRunner(Class<?> testClass) throws InitializationError {
super(testClass); super(testClass);
} }
...@@ -98,7 +102,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { ...@@ -98,7 +102,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
private URL[] extractUrls(ClassLoader classLoader) throws Exception { private URL[] extractUrls(ClassLoader classLoader) throws Exception {
List<URL> extractedUrls = new ArrayList<>(); List<URL> extractedUrls = new ArrayList<>();
doExtractUrls(classLoader).forEach((URL url) -> { doExtractUrls(classLoader).forEach((URL url) -> {
if (isSurefireBooterJar(url)) { if (isManifestOnlyJar(url)) {
extractedUrls.addAll(extractUrlsFromManifestClassPath(url)); extractedUrls.addAll(extractUrlsFromManifestClassPath(url));
} }
else { else {
...@@ -125,10 +129,30 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { ...@@ -125,10 +129,30 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
} }
} }
private boolean isManifestOnlyJar(URL url) {
return isSurefireBooterJar(url) || isShortenedIntelliJJar(url);
}
private boolean isSurefireBooterJar(URL url) { private boolean isSurefireBooterJar(URL url) {
return url.getPath().contains("surefirebooter"); 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;
}
}
return false;
}
private List<URL> extractUrlsFromManifestClassPath(URL booterJar) { private List<URL> extractUrlsFromManifestClassPath(URL booterJar) {
List<URL> urls = new ArrayList<>(); List<URL> urls = new ArrayList<>();
try { try {
...@@ -143,9 +167,14 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { ...@@ -143,9 +167,14 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
} }
private String[] getClassPath(URL booterJar) throws Exception { private String[] getClassPath(URL booterJar) throws Exception {
try (JarFile jarFile = new JarFile(new File(booterJar.toURI()))) { Attributes attributes = getManifestMainAttributesFromUrl(booterJar);
return StringUtils.delimitedListToStringArray(jarFile.getManifest() return StringUtils.delimitedListToStringArray(attributes
.getMainAttributes().getValue(Attributes.Name.CLASS_PATH), " "); .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();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment