Skip to content

Commit b81f295

Browse files
committed
convert, still debugging
1 parent eabaeba commit b81f295

File tree

3 files changed

+42
-86
lines changed

3 files changed

+42
-86
lines changed

instrumentation/internal/internal-class-loader/javaagent-integration-tests/src/test/groovy/ResourceInjectionTest.groovy

Lines changed: 0 additions & 53 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
package io.opentelemetry.javaagent.instrumentation.internal.classloader;
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
25

3-
import org.junit.jupiter.api.Test;
6+
package io.opentelemetry.javaagent.instrumentation.internal.classloader;
47

58
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
69

10+
import org.junit.jupiter.api.Test;
11+
712
class RegressionTest {
813
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/5155
914
// loading a class that is extended/implemented by a helper class causes
10-
// java.lang.LinkageError: loader 'app' (instance of jdk.internal.loader.ClassLoaders$AppClassLoader)
15+
// java.lang.LinkageError: loader 'app' (instance of
16+
// jdk.internal.loader.ClassLoaders$AppClassLoader)
1117
// attempted duplicate interface definition for org.apache.commons.lang3.function.FailableCallable
1218
// this test verifies that the duplicate class definition LinkageError is not thrown into
1319
// application code
1420
@Test
1521
void noDuplicateClassDefinition() throws ClassNotFoundException {
1622
assertThat(Class.forName("org.apache.commons.lang3.function.FailableCallable")).isNotNull();
1723
}
18-
1924
}
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,68 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
package io.opentelemetry.javaagent.instrumentation.internal.classloader;
27

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;
513
import java.lang.ref.WeakReference;
614
import java.net.URL;
715
import java.net.URLClassLoader;
16+
import java.nio.charset.Charset;
817
import java.time.Duration;
18+
import java.util.Collections;
919
import java.util.Enumeration;
10-
import java.util.concurrent.TimeoutException;
20+
import java.util.List;
1121
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;
1624

1725
class ResourceInjectionTest {
1826

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+
1934
@Test
2035
@SuppressWarnings("UnnecessaryAsync")
21-
void resourcesInjectedToNonDelegatingClassLoader()
22-
throws IOException, ClassNotFoundException, InterruptedException, TimeoutException {
36+
void resourcesInjectedToNonDelegatingClassLoader() throws Exception {
37+
2338
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));
2642

2743
Enumeration<URL> resourceUrls = emptyLoader.get().getResources(resourceName);
2844
assertThat(resourceUrls.hasMoreElements()).isFalse();
2945

3046
URLClassLoader notInjectedLoader = new URLClassLoader(urls, null);
3147

3248
// this triggers resource injection
33-
emptyLoader.get().loadClass(ResourceInjectionTest.class.getName());
49+
emptyLoader.get().loadClass(SystemUtils.class.getName());
3450

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));
4552

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");
5156

5257
assertThat(notInjectedLoader.getResources(resourceName).hasMoreElements()).isFalse();
5358

5459
// references to emptyloader are gone
55-
emptyLoader.get().close();
60+
emptyLoader.get().close(); // cleanup
5661
WeakReference<URLClassLoader> ref = new WeakReference<>(emptyLoader.get());
5762
emptyLoader.set(null);
5863

5964
awaitGc(ref, Duration.ofSeconds(10));
6065

6166
assertThat(ref.get()).isNull();
6267
}
63-
6468
}

0 commit comments

Comments
 (0)