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