From 9e4c8feb77cae0a7dc5f877f96318bf46d64869b Mon Sep 17 00:00:00 2001 From: Jay DeLuca Date: Thu, 5 Oct 2023 18:07:56 -0400 Subject: [PATCH 1/2] Convert internal-url-class-loader javaagent integration tests --- .../src/test/groovy/AddUrlTest.groovy | 51 ---------------- .../src/test/java/AddUrlTest.java | 61 +++++++++++++++++++ 2 files changed, 61 insertions(+), 51 deletions(-) delete mode 100644 instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/groovy/AddUrlTest.groovy create mode 100644 instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/java/AddUrlTest.java diff --git a/instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/groovy/AddUrlTest.groovy b/instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/groovy/AddUrlTest.groovy deleted file mode 100644 index 16a4efdb58dd..000000000000 --- a/instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/groovy/AddUrlTest.groovy +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification -import org.apache.commons.io.IOUtils -import org.apache.commons.lang3.SystemUtils - -class AddUrlTest extends AgentInstrumentationSpecification { - - def "should instrument class after it is loaded via addURL"() { - given: - TestURLClassLoader loader = new TestURLClassLoader() - - when: - // this is just to verify the assumption that TestURLClassLoader is not finding SystemUtils via - // the test class path (in which case the verification below would not be very meaningful) - loader.loadClass(SystemUtils.getName()) - - then: - thrown ClassNotFoundException - - when: - // loading a class in the URLClassLoader in order to trigger - // a negative cache hit on org.apache.commons.lang3.SystemUtils - loader.addURL(IOUtils.getProtectionDomain().getCodeSource().getLocation()) - loader.loadClass(IOUtils.getName()) - - loader.addURL(SystemUtils.getProtectionDomain().getCodeSource().getLocation()) - def clazz = loader.loadClass(SystemUtils.getName()) - - then: - clazz.getClassLoader() == loader - clazz.getMethod("getHostName").invoke(null) == "not-the-host-name" - } - - static class TestURLClassLoader extends URLClassLoader { - - TestURLClassLoader() { - super(new URL[0], (ClassLoader) null) - } - - // silence CodeNarc. URLClassLoader#addURL is protected, this method is public - @SuppressWarnings("UnnecessaryOverridingMethod") - @Override - void addURL(URL url) { - super.addURL(url) - } - } -} diff --git a/instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/java/AddUrlTest.java b/instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/java/AddUrlTest.java new file mode 100644 index 000000000000..e8880fdeff53 --- /dev/null +++ b/instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/java/AddUrlTest.java @@ -0,0 +1,61 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; + +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.SystemUtils; +import org.junit.jupiter.api.Test; + +class AddUrlTest { + + @Test + void testShouldInstrumentClassAfterItIsLoadedViaAddUrl() + throws ClassNotFoundException, + NoSuchMethodException, + InvocationTargetException, + IllegalAccessException { + TestUrlClassLoader loader = new TestUrlClassLoader(); + + // this is just to verify the assumption that TestURLClassLoader is not finding SystemUtils via + // the test class path (in which case the verification below would not be very meaningful) + Throwable thrown = + catchThrowable( + () -> { + loader.loadClass(SystemUtils.class.getName()); + }); + + assertThat(thrown).isInstanceOf(ClassNotFoundException.class); + + // loading a class in the URLClassLoader in order to trigger + // a negative cache hit on org.apache.commons.lang3.SystemUtils + loader.addURL(IOUtils.class.getProtectionDomain().getCodeSource().getLocation()); + loader.loadClass(IOUtils.class.getName()); + + loader.addURL(SystemUtils.class.getProtectionDomain().getCodeSource().getLocation()); + Class clazz = loader.loadClass(SystemUtils.class.getName()); + + assertThat(clazz.getClassLoader()).isEqualTo(loader); + assertThat(clazz.getMethod("getHostName").invoke(null)).isEqualTo("not-the-host-name"); + } + + static class TestUrlClassLoader extends URLClassLoader { + + TestUrlClassLoader() { + super(new URL[0], null); + } + + // silence CodeNarc. URLClassLoader#addURL is protected, this method is public + @SuppressWarnings("UnnecessaryOverridingMethod") + @Override + public void addURL(URL url) { + super.addURL(url); + } + } +} From 6d11c89b3b6e3f9a823c1429a1e2ad2ea776bfdc Mon Sep 17 00:00:00 2001 From: Jay DeLuca Date: Fri, 6 Oct 2023 08:16:28 -0400 Subject: [PATCH 2/2] simplify throws to just exception --- .../src/test/java/AddUrlTest.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/java/AddUrlTest.java b/instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/java/AddUrlTest.java index e8880fdeff53..779eb072da5f 100644 --- a/instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/java/AddUrlTest.java +++ b/instrumentation/internal/internal-url-class-loader/javaagent-integration-tests/src/test/java/AddUrlTest.java @@ -6,7 +6,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; -import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.net.URLClassLoader; import org.apache.commons.io.IOUtils; @@ -16,11 +15,7 @@ class AddUrlTest { @Test - void testShouldInstrumentClassAfterItIsLoadedViaAddUrl() - throws ClassNotFoundException, - NoSuchMethodException, - InvocationTargetException, - IllegalAccessException { + void testShouldInstrumentClassAfterItIsLoadedViaAddUrl() throws Exception { TestUrlClassLoader loader = new TestUrlClassLoader(); // this is just to verify the assumption that TestURLClassLoader is not finding SystemUtils via