Skip to content

Commit 006dece

Browse files
committed
Merge branch '1.5.x'
2 parents 88faaba + 345b2c5 commit 006dece

File tree

1 file changed

+33
-4
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath

1 file changed

+33
-4
lines changed

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunner.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929
import java.util.jar.Attributes;
3030
import java.util.jar.JarFile;
31+
import java.util.regex.Pattern;
3132
import java.util.stream.Stream;
3233

3334
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
@@ -66,6 +67,9 @@
6667
*/
6768
public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
6869

70+
private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern.compile(
71+
".*classpath(\\d+)?.jar");
72+
6973
public ModifiedClassPathRunner(Class<?> testClass) throws InitializationError {
7074
super(testClass);
7175
}
@@ -98,7 +102,7 @@ private URLClassLoader createTestClassLoader(Class<?> testClass) throws Exceptio
98102
private URL[] extractUrls(ClassLoader classLoader) throws Exception {
99103
List<URL> extractedUrls = new ArrayList<>();
100104
doExtractUrls(classLoader).forEach((URL url) -> {
101-
if (isSurefireBooterJar(url)) {
105+
if (isManifestOnlyJar(url)) {
102106
extractedUrls.addAll(extractUrlsFromManifestClassPath(url));
103107
}
104108
else {
@@ -125,10 +129,30 @@ private URL toURL(String entry) {
125129
}
126130
}
127131

132+
private boolean isManifestOnlyJar(URL url) {
133+
return isSurefireBooterJar(url) || isShortenedIntelliJJar(url);
134+
}
135+
128136
private boolean isSurefireBooterJar(URL url) {
129137
return url.getPath().contains("surefirebooter");
130138
}
131139

140+
private boolean isShortenedIntelliJJar(URL url) {
141+
String urlPath = url.getPath();
142+
boolean isCandidate = INTELLIJ_CLASSPATH_JAR_PATTERN.matcher(urlPath).matches();
143+
if (isCandidate) {
144+
try {
145+
Attributes attributes = getManifestMainAttributesFromUrl(url);
146+
String createdBy = attributes.getValue("Created-By");
147+
return createdBy != null && createdBy.contains("IntelliJ");
148+
}
149+
catch (Exception ex) {
150+
return false;
151+
}
152+
}
153+
return false;
154+
}
155+
132156
private List<URL> extractUrlsFromManifestClassPath(URL booterJar) {
133157
List<URL> urls = new ArrayList<>();
134158
try {
@@ -143,9 +167,14 @@ private List<URL> extractUrlsFromManifestClassPath(URL booterJar) {
143167
}
144168

145169
private String[] getClassPath(URL booterJar) throws Exception {
146-
try (JarFile jarFile = new JarFile(new File(booterJar.toURI()))) {
147-
return StringUtils.delimitedListToStringArray(jarFile.getManifest()
148-
.getMainAttributes().getValue(Attributes.Name.CLASS_PATH), " ");
170+
Attributes attributes = getManifestMainAttributesFromUrl(booterJar);
171+
return StringUtils.delimitedListToStringArray(attributes
172+
.getValue(Attributes.Name.CLASS_PATH), " ");
173+
}
174+
175+
private Attributes getManifestMainAttributesFromUrl(URL url) throws Exception {
176+
try (JarFile jarFile = new JarFile(new File(url.toURI()))) {
177+
return jarFile.getManifest().getMainAttributes();
149178
}
150179
}
151180

0 commit comments

Comments
 (0)