Convert internal-url-class-loader javaagent integration tests (#9614)

This commit is contained in:
Jay DeLuca 2023-10-09 07:33:38 -04:00 committed by GitHub
parent 432dc54de8
commit 19e8530416
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 51 deletions

View File

@ -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)
}
}
}

View File

@ -0,0 +1,56 @@
/*
* 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.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 Exception {
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);
}
}
}