|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
1 | 6 | package io.opentelemetry.javaagent.instrumentation.internal.classloader; |
2 | 7 |
|
3 | | -import org.junit.jupiter.api.Test; |
4 | | -import java.io.IOException; |
| 8 | +import static io.opentelemetry.instrumentation.test.utils.GcUtils.awaitGc; |
| 9 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 10 | + |
| 11 | +import java.io.BufferedReader; |
| 12 | +import java.io.InputStreamReader; |
5 | 13 | import java.lang.ref.WeakReference; |
6 | 14 | import java.net.URL; |
7 | 15 | import java.net.URLClassLoader; |
| 16 | +import java.nio.charset.Charset; |
8 | 17 | import java.time.Duration; |
| 18 | +import java.util.Collections; |
9 | 19 | import java.util.Enumeration; |
10 | | -import java.util.concurrent.TimeoutException; |
| 20 | +import java.util.List; |
11 | 21 | import java.util.concurrent.atomic.AtomicReference; |
12 | | - |
13 | | -import static io.opentelemetry.instrumentation.test.utils.GcUtils.awaitGc; |
14 | | -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
15 | | - |
| 22 | +import org.apache.commons.lang3.SystemUtils; |
| 23 | +import org.junit.jupiter.api.Test; |
16 | 24 |
|
17 | 25 | class ResourceInjectionTest { |
18 | 26 |
|
| 27 | + private static String trimStream(URL url) throws Exception { |
| 28 | + try (BufferedReader reader = |
| 29 | + new BufferedReader(new InputStreamReader(url.openStream(), Charset.defaultCharset()))) { |
| 30 | + return reader.readLine().trim(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
19 | 34 | @Test |
20 | 35 | @SuppressWarnings("UnnecessaryAsync") |
21 | | - void resourcesInjectedToNonDelegatingClassLoader() |
22 | | - throws IOException, ClassNotFoundException, InterruptedException, TimeoutException { |
| 36 | + void resourcesInjectedToNonDelegatingClassLoader() throws Exception { |
| 37 | + |
23 | 38 | String resourceName = "test-resources/test-resource.txt"; |
24 | | - URL[] urls = {ResourceInjectionTest.class.getProtectionDomain().getCodeSource().getLocation()}; |
25 | | - AtomicReference<URLClassLoader> emptyLoader = new AtomicReference<>(new URLClassLoader(urls, null)); |
| 39 | + URL[] urls = {SystemUtils.class.getProtectionDomain().getCodeSource().getLocation()}; |
| 40 | + AtomicReference<URLClassLoader> emptyLoader = |
| 41 | + new AtomicReference<>(new URLClassLoader(urls, null)); |
26 | 42 |
|
27 | 43 | Enumeration<URL> resourceUrls = emptyLoader.get().getResources(resourceName); |
28 | 44 | assertThat(resourceUrls.hasMoreElements()).isFalse(); |
29 | 45 |
|
30 | 46 | URLClassLoader notInjectedLoader = new URLClassLoader(urls, null); |
31 | 47 |
|
32 | 48 | // this triggers resource injection |
33 | | - emptyLoader.get().loadClass(ResourceInjectionTest.class.getName()); |
| 49 | + emptyLoader.get().loadClass(SystemUtils.class.getName()); |
34 | 50 |
|
35 | | - for (int i = 0; i < 2; i++) { |
36 | | - |
37 | | - URL test = ( resourceUrls.asIterator(); |
38 | | - if (i == 0) { |
39 | | - assertThat(test).isEqualTo("Hello world!"); |
40 | | - } else { |
41 | | - assertThat(test).isEqualTo("Hello there"); |
42 | | - } |
43 | | - } |
44 | | - assertThat(resourceUrls.hasMoreElements()).isFalse(); |
| 51 | + List<URL> resourceList = Collections.list(emptyLoader.get().getResources(resourceName)); |
45 | 52 |
|
46 | | - |
47 | | -// resourceUrls = (Enumeration<URL>) Collections.list(emptyLoader.get().getResources(resourceName)); |
48 | | -// assertThat(resourceUrls.).isEqualTo(2); |
49 | | -// assertThat(list.get(0).openStream().toString().trim()).isEqualTo("Hello world!"); |
50 | | -// assertThat(list.get(1).openStream().toString().trim()).isEqualTo("Hello there"); |
| 53 | + assertThat(resourceList.size()).isEqualTo(2); |
| 54 | + assertThat(trimStream(resourceList.get(0))).isEqualTo("Hello world!"); |
| 55 | + assertThat(trimStream(resourceList.get(1))).isEqualTo("Hello there"); |
51 | 56 |
|
52 | 57 | assertThat(notInjectedLoader.getResources(resourceName).hasMoreElements()).isFalse(); |
53 | 58 |
|
54 | 59 | // references to emptyloader are gone |
55 | | - emptyLoader.get().close(); |
| 60 | + emptyLoader.get().close(); // cleanup |
56 | 61 | WeakReference<URLClassLoader> ref = new WeakReference<>(emptyLoader.get()); |
57 | 62 | emptyLoader.set(null); |
58 | 63 |
|
59 | 64 | awaitGc(ref, Duration.ofSeconds(10)); |
60 | 65 |
|
61 | 66 | assertThat(ref.get()).isNull(); |
62 | 67 | } |
63 | | - |
64 | 68 | } |
0 commit comments