|
36 | 36 | import net.bytebuddy.implementation.FixedValue; |
37 | 37 | import net.bytebuddy.matcher.ElementMatchers; |
38 | 38 | import org.junit.jupiter.api.Test; |
39 | | -import org.junit.jupiter.api.condition.DisabledOnOs; |
40 | 39 | import org.junit.jupiter.api.condition.OS; |
41 | 40 | import org.junit.jupiter.api.io.TempDir; |
42 | 41 |
|
43 | 42 | @SuppressWarnings("ClassNamedLikeTypeParameter") |
44 | 43 | class InstrumentationModuleClassLoaderTest { |
45 | 44 |
|
| 45 | + static { |
| 46 | + // Windows holds open handles to JAR files loaded through URLClassLoader even after the |
| 47 | + // class loader is closed. Disabling URLConnection caching on Windows to prevent this. |
| 48 | + // Without this, instrumentation-module.jar cannot be deleted during test cleanup. |
| 49 | + if (OS.WINDOWS.isCurrentOs()) { |
| 50 | + try { |
| 51 | + // Must call setDefaultUseCaches on a jar: URL connection before any JARs are opened |
| 52 | + new URL("jar:file://dummy.jar!/").openConnection().setDefaultUseCaches(false); |
| 53 | + } catch (Exception e) { |
| 54 | + throw new RuntimeException(e); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
46 | 59 | @Test |
47 | 60 | void checkLookup() throws Throwable { |
48 | 61 | Map<String, BytecodeWithUrl> toInject = new HashMap<>(); |
@@ -107,9 +120,6 @@ void checkInjectedClassesHavePackage() throws Throwable { |
107 | 120 | assertThat(classPackage).isSameAs(clPackage); |
108 | 121 | } |
109 | 122 |
|
110 | | - // Windows holds open handles to jars loaded through URLClassLoader even after the loader is |
111 | | - // closed, which keeps the temp directory locked and causes test cleanup to fail. |
112 | | - @DisabledOnOs(value = OS.WINDOWS, disabledReason = "temp directory cleanup fails on Windows") |
113 | 123 | @Test |
114 | 124 | void checkClassLookupPrecedence(@TempDir Path tempDir) throws Exception { |
115 | 125 |
|
@@ -198,6 +208,14 @@ void checkClassLookupPrecedence(@TempDir Path tempDir) throws Exception { |
198 | 208 | appCl.close(); |
199 | 209 | agentCl.close(); |
200 | 210 | moduleSourceCl.close(); |
| 211 | + |
| 212 | + // On Windows, force garbage collection to release file handles to JAR files. |
| 213 | + // Without this, all three JAR files (dummy-app.jar, dummy-agent.jar, |
| 214 | + // instrumentation-module.jar) cannot be deleted during test cleanup, even with |
| 215 | + // setDefaultUseCaches(false) above. |
| 216 | + if (OS.WINDOWS.isCurrentOs()) { |
| 217 | + System.gc(); |
| 218 | + } |
201 | 219 | } |
202 | 220 | } |
203 | 221 |
|
|
0 commit comments