Commit efc9dcc2 authored by dreis2211's avatar dreis2211 Committed by Stephane Nicoll

Fix ModifiedClassPathRunner tests if run via IDEA

IntelliJ can shorten the classpath to a single classpath.jar in order to
circumvent errors originating from a too long classpath. This breaks the
filtering inside ModifiedClassPathRunner as the classpath contains only a
single jar to match against. This can be fixed by applying a similar
mechanism already provided for Surefire manifest-only booter JARs, which
extracts the real classpath from the JAR's Manifest file.

See gh-12535
parent 625c4e61
...@@ -27,6 +27,7 @@ import java.util.Collections; ...@@ -27,6 +27,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 org.apache.maven.repository.internal.MavenRepositorySystemUtils; import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.DefaultRepositorySystemSession;
...@@ -64,6 +65,9 @@ import org.springframework.util.StringUtils; ...@@ -64,6 +65,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);
} }
...@@ -102,7 +106,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { ...@@ -102,7 +106,7 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
private URL[] extractUrls(URLClassLoader classLoader) throws Exception { private URL[] extractUrls(URLClassLoader classLoader) throws Exception {
List<URL> extractedUrls = new ArrayList<URL>(); List<URL> extractedUrls = new ArrayList<URL>();
for (URL url : classLoader.getURLs()) { for (URL url : classLoader.getURLs()) {
if (isSurefireBooterJar(url)) { if (isManifestOnlyJar(url)) {
extractedUrls.addAll(extractUrlsFromManifestClassPath(url)); extractedUrls.addAll(extractUrlsFromManifestClassPath(url));
} }
else { else {
...@@ -112,10 +116,30 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { ...@@ -112,10 +116,30 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
return extractedUrls.toArray(new URL[extractedUrls.size()]); return extractedUrls.toArray(new URL[extractedUrls.size()]);
} }
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) throws Exception { private List<URL> extractUrlsFromManifestClassPath(URL booterJar) throws Exception {
List<URL> urls = new ArrayList<URL>(); List<URL> urls = new ArrayList<URL>();
for (String entry : getClassPath(booterJar)) { for (String entry : getClassPath(booterJar)) {
...@@ -125,10 +149,15 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner { ...@@ -125,10 +149,15 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
} }
private String[] getClassPath(URL booterJar) throws Exception { private String[] getClassPath(URL booterJar) throws Exception {
JarFile jarFile = new JarFile(new File(booterJar.toURI())); Attributes attributes = getManifestMainAttributesFromUrl(booterJar);
return StringUtils.delimitedListToStringArray(attributes
.getValue(Attributes.Name.CLASS_PATH), " ");
}
private Attributes getManifestMainAttributesFromUrl(URL url) throws Exception {
JarFile jarFile = new JarFile(new File(url.toURI()));
try { try {
return StringUtils.delimitedListToStringArray(jarFile.getManifest() return jarFile.getManifest().getMainAttributes();
.getMainAttributes().getValue(Attributes.Name.CLASS_PATH), " ");
} }
finally { finally {
jarFile.close(); jarFile.close();
......
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