Skip to content

Commit 345b2c5

Browse files
committed
Merge pull request #12535 from dreis2211:intellij-modified-classpath-runner
* pr/12535: Fix ModifiedClassPathRunner tests if run via IDEA
2 parents 625c4e6 + efc9dcc commit 345b2c5

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828
import java.util.jar.Attributes;
2929
import java.util.jar.JarFile;
30+
import java.util.regex.Pattern;
3031

3132
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
3233
import org.eclipse.aether.DefaultRepositorySystemSession;
@@ -64,6 +65,9 @@
6465
*/
6566
public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
6667

68+
private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern.compile(
69+
".*classpath(\\d+)?.jar");
70+
6771
public ModifiedClassPathRunner(Class<?> testClass) throws InitializationError {
6872
super(testClass);
6973
}
@@ -102,7 +106,7 @@ private URLClassLoader createTestClassLoader(Class<?> testClass) throws Exceptio
102106
private URL[] extractUrls(URLClassLoader classLoader) throws Exception {
103107
List<URL> extractedUrls = new ArrayList<URL>();
104108
for (URL url : classLoader.getURLs()) {
105-
if (isSurefireBooterJar(url)) {
109+
if (isManifestOnlyJar(url)) {
106110
extractedUrls.addAll(extractUrlsFromManifestClassPath(url));
107111
}
108112
else {
@@ -112,10 +116,30 @@ private URL[] extractUrls(URLClassLoader classLoader) throws Exception {
112116
return extractedUrls.toArray(new URL[extractedUrls.size()]);
113117
}
114118

119+
private boolean isManifestOnlyJar(URL url) {
120+
return isSurefireBooterJar(url) || isShortenedIntelliJJar(url);
121+
}
122+
115123
private boolean isSurefireBooterJar(URL url) {
116124
return url.getPath().contains("surefirebooter");
117125
}
118126

127+
private boolean isShortenedIntelliJJar(URL url) {
128+
String urlPath = url.getPath();
129+
boolean isCandidate = INTELLIJ_CLASSPATH_JAR_PATTERN.matcher(urlPath).matches();
130+
if (isCandidate) {
131+
try {
132+
Attributes attributes = getManifestMainAttributesFromUrl(url);
133+
String createdBy = attributes.getValue("Created-By");
134+
return createdBy != null && createdBy.contains("IntelliJ");
135+
}
136+
catch (Exception ex) {
137+
return false;
138+
}
139+
}
140+
return false;
141+
}
142+
119143
private List<URL> extractUrlsFromManifestClassPath(URL booterJar) throws Exception {
120144
List<URL> urls = new ArrayList<URL>();
121145
for (String entry : getClassPath(booterJar)) {
@@ -125,10 +149,15 @@ private List<URL> extractUrlsFromManifestClassPath(URL booterJar) throws Excepti
125149
}
126150

127151
private String[] getClassPath(URL booterJar) throws Exception {
128-
JarFile jarFile = new JarFile(new File(booterJar.toURI()));
152+
Attributes attributes = getManifestMainAttributesFromUrl(booterJar);
153+
return StringUtils.delimitedListToStringArray(attributes
154+
.getValue(Attributes.Name.CLASS_PATH), " ");
155+
}
156+
157+
private Attributes getManifestMainAttributesFromUrl(URL url) throws Exception {
158+
JarFile jarFile = new JarFile(new File(url.toURI()));
129159
try {
130-
return StringUtils.delimitedListToStringArray(jarFile.getManifest()
131-
.getMainAttributes().getValue(Attributes.Name.CLASS_PATH), " ");
160+
return jarFile.getManifest().getMainAttributes();
132161
}
133162
finally {
134163
jarFile.close();

0 commit comments

Comments
 (0)