Skip to content

Convert internal-class-loader tests from groovy to java #9560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.internal.classloader;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URL;
import java.net.URLClassLoader;
import org.junit.jupiter.api.Test;

class ClassLoadingTest {

@Test
void testDelegatesToBootstrapClassLoaderForAgentClasses() throws ClassNotFoundException {
NonDelegatingUrlClassLoader classLoader = new NonDelegatingUrlClassLoader();
Class<?> clazz =
Class.forName(
"io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge", false, classLoader);

assertThat(clazz).isNotNull();
assertThat(clazz.getClassLoader()).isNull();
}

@Test
void testDelegatesToBootstrapClassLoaderForAgentClassesTwoArguments()
throws ClassNotFoundException {
NonDelegatingUrlClassLoader classLoader = new NonDelegatingUrlClassLoader();
Class<?> clazz =
classLoader.loadClass("io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge", false);
assertThat(clazz).isNotNull();
assertThat(clazz.getClassLoader()).isNull();
}

static class NonDelegatingUrlClassLoader extends URLClassLoader {

NonDelegatingUrlClassLoader() {
super(new URL[0]);
}

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
synchronized (getClassLoadingLock(name)) {
Class<?> clazz = findLoadedClass(name);
if (clazz == null) {
clazz = findClass(name);
}
if (resolve) {
resolveClass(clazz);
}
return clazz;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.internal.classloader;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.jboss.modules.Module;
import org.jboss.modules.ModuleClassLoader;
import org.jboss.modules.ModuleFinder;
import org.jboss.modules.ModuleIdentifier;
import org.jboss.modules.ModuleLoadException;
import org.jboss.modules.ModuleLoader;
import org.jboss.modules.ModuleSpec;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

class JbossClassloadingTest {

@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

@Test
void testDelegatesToBootstrapClassLoaderForAgentClasses()
throws ModuleLoadException, ClassNotFoundException {
ModuleFinder[] moduleFinders = new ModuleFinder[1];
moduleFinders[0] = (identifier, delegateLoader) -> ModuleSpec.build(identifier).create();

ModuleLoader moduleLoader = new ModuleLoader(moduleFinders);
ModuleIdentifier moduleId = ModuleIdentifier.fromString("test");
Module testModule = moduleLoader.loadModule(moduleId);
ModuleClassLoader classLoader = testModule.getClassLoader();

Class<?> clazz =
Class.forName(
"io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge", false, classLoader);

assertThat(clazz).isNotNull();
assertThat(clazz.getClassLoader()).isNull();
}
}
Loading