|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.lang.annotation.Annotation;
|
| 21 | +import java.lang.management.ManagementFactory; |
21 | 22 | import java.lang.reflect.Method;
|
22 | 23 | import java.net.URL;
|
23 | 24 | import java.net.URLClassLoader;
|
|
27 | 28 | import java.util.List;
|
28 | 29 | import java.util.jar.Attributes;
|
29 | 30 | import java.util.jar.JarFile;
|
| 31 | +import java.util.stream.Stream; |
30 | 32 |
|
31 | 33 | import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
|
32 | 34 | import org.eclipse.aether.DefaultRepositorySystemSession;
|
@@ -87,33 +89,55 @@ protected Object createTest() throws Exception {
|
87 | 89 | }
|
88 | 90 |
|
89 | 91 | private URLClassLoader createTestClassLoader(Class<?> testClass) throws Exception {
|
90 |
| - URLClassLoader classLoader = (URLClassLoader) this.getClass().getClassLoader(); |
| 92 | + ClassLoader classLoader = this.getClass().getClassLoader(); |
91 | 93 | return new ModifiedClassPathClassLoader(
|
92 | 94 | processUrls(extractUrls(classLoader), testClass), classLoader.getParent(),
|
93 | 95 | classLoader);
|
94 | 96 | }
|
95 | 97 |
|
96 |
| - private URL[] extractUrls(URLClassLoader classLoader) throws Exception { |
| 98 | + private URL[] extractUrls(ClassLoader classLoader) throws Exception { |
97 | 99 | List<URL> extractedUrls = new ArrayList<>();
|
98 |
| - for (URL url : classLoader.getURLs()) { |
| 100 | + doExtractUrls(classLoader).forEach((URL url) -> { |
99 | 101 | if (isSurefireBooterJar(url)) {
|
100 | 102 | extractedUrls.addAll(extractUrlsFromManifestClassPath(url));
|
101 | 103 | }
|
102 | 104 | else {
|
103 | 105 | extractedUrls.add(url);
|
104 | 106 | }
|
105 |
| - } |
| 107 | + }); |
106 | 108 | return extractedUrls.toArray(new URL[extractedUrls.size()]);
|
107 | 109 | }
|
108 | 110 |
|
| 111 | + private Stream<URL> doExtractUrls(ClassLoader classLoader) throws Exception { |
| 112 | + if (classLoader instanceof URLClassLoader) { |
| 113 | + return Stream.of(((URLClassLoader) classLoader).getURLs()); |
| 114 | + } |
| 115 | + return Stream.of(ManagementFactory.getRuntimeMXBean().getClassPath() |
| 116 | + .split(File.pathSeparator)).map(this::toURL); |
| 117 | + } |
| 118 | + |
| 119 | + private URL toURL(String entry) { |
| 120 | + try { |
| 121 | + return new File(entry).toURI().toURL(); |
| 122 | + } |
| 123 | + catch (Exception ex) { |
| 124 | + throw new IllegalArgumentException(ex); |
| 125 | + } |
| 126 | + } |
| 127 | + |
109 | 128 | private boolean isSurefireBooterJar(URL url) {
|
110 | 129 | return url.getPath().contains("surefirebooter");
|
111 | 130 | }
|
112 | 131 |
|
113 |
| - private List<URL> extractUrlsFromManifestClassPath(URL booterJar) throws Exception { |
| 132 | + private List<URL> extractUrlsFromManifestClassPath(URL booterJar) { |
114 | 133 | List<URL> urls = new ArrayList<>();
|
115 |
| - for (String entry : getClassPath(booterJar)) { |
116 |
| - urls.add(new URL(entry)); |
| 134 | + try { |
| 135 | + for (String entry : getClassPath(booterJar)) { |
| 136 | + urls.add(new URL(entry)); |
| 137 | + } |
| 138 | + } |
| 139 | + catch (Exception ex) { |
| 140 | + throw new RuntimeException(ex); |
117 | 141 | }
|
118 | 142 | return urls;
|
119 | 143 | }
|
|
0 commit comments