28
28
import java .util .List ;
29
29
import java .util .jar .Attributes ;
30
30
import java .util .jar .JarFile ;
31
+ import java .util .regex .Pattern ;
31
32
import java .util .stream .Stream ;
32
33
33
34
import org .apache .maven .repository .internal .MavenRepositorySystemUtils ;
66
67
*/
67
68
public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
68
69
70
+ private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern .compile (
71
+ ".*classpath(\\ d+)?.jar" );
72
+
69
73
public ModifiedClassPathRunner (Class <?> testClass ) throws InitializationError {
70
74
super (testClass );
71
75
}
@@ -98,7 +102,7 @@ private URLClassLoader createTestClassLoader(Class<?> testClass) throws Exceptio
98
102
private URL [] extractUrls (ClassLoader classLoader ) throws Exception {
99
103
List <URL > extractedUrls = new ArrayList <>();
100
104
doExtractUrls (classLoader ).forEach ((URL url ) -> {
101
- if (isSurefireBooterJar (url )) {
105
+ if (isManifestOnlyJar (url )) {
102
106
extractedUrls .addAll (extractUrlsFromManifestClassPath (url ));
103
107
}
104
108
else {
@@ -125,10 +129,30 @@ private URL toURL(String entry) {
125
129
}
126
130
}
127
131
132
+ private boolean isManifestOnlyJar (URL url ) {
133
+ return isSurefireBooterJar (url ) || isShortenedIntelliJJar (url );
134
+ }
135
+
128
136
private boolean isSurefireBooterJar (URL url ) {
129
137
return url .getPath ().contains ("surefirebooter" );
130
138
}
131
139
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
+
132
156
private List <URL > extractUrlsFromManifestClassPath (URL booterJar ) {
133
157
List <URL > urls = new ArrayList <>();
134
158
try {
@@ -143,9 +167,14 @@ private List<URL> extractUrlsFromManifestClassPath(URL booterJar) {
143
167
}
144
168
145
169
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 ();
149
178
}
150
179
}
151
180
0 commit comments