27
27
import java .util .List ;
28
28
import java .util .jar .Attributes ;
29
29
import java .util .jar .JarFile ;
30
+ import java .util .regex .Pattern ;
30
31
31
32
import org .apache .maven .repository .internal .MavenRepositorySystemUtils ;
32
33
import org .eclipse .aether .DefaultRepositorySystemSession ;
64
65
*/
65
66
public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
66
67
68
+ private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern .compile (
69
+ ".*classpath(\\ d+)?.jar" );
70
+
67
71
public ModifiedClassPathRunner (Class <?> testClass ) throws InitializationError {
68
72
super (testClass );
69
73
}
@@ -102,7 +106,7 @@ private URLClassLoader createTestClassLoader(Class<?> testClass) throws Exceptio
102
106
private URL [] extractUrls (URLClassLoader classLoader ) throws Exception {
103
107
List <URL > extractedUrls = new ArrayList <URL >();
104
108
for (URL url : classLoader .getURLs ()) {
105
- if (isSurefireBooterJar (url )) {
109
+ if (isManifestOnlyJar (url )) {
106
110
extractedUrls .addAll (extractUrlsFromManifestClassPath (url ));
107
111
}
108
112
else {
@@ -112,10 +116,30 @@ private URL[] extractUrls(URLClassLoader classLoader) throws Exception {
112
116
return extractedUrls .toArray (new URL [extractedUrls .size ()]);
113
117
}
114
118
119
+ private boolean isManifestOnlyJar (URL url ) {
120
+ return isSurefireBooterJar (url ) || isShortenedIntelliJJar (url );
121
+ }
122
+
115
123
private boolean isSurefireBooterJar (URL url ) {
116
124
return url .getPath ().contains ("surefirebooter" );
117
125
}
118
126
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
+
119
143
private List <URL > extractUrlsFromManifestClassPath (URL booterJar ) throws Exception {
120
144
List <URL > urls = new ArrayList <URL >();
121
145
for (String entry : getClassPath (booterJar )) {
@@ -125,10 +149,15 @@ private List<URL> extractUrlsFromManifestClassPath(URL booterJar) throws Excepti
125
149
}
126
150
127
151
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 ()));
129
159
try {
130
- return StringUtils .delimitedListToStringArray (jarFile .getManifest ()
131
- .getMainAttributes ().getValue (Attributes .Name .CLASS_PATH ), " " );
160
+ return jarFile .getManifest ().getMainAttributes ();
132
161
}
133
162
finally {
134
163
jarFile .close ();
0 commit comments