This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new faf667b442a Add ClassLoaderContextTest (#37462)
faf667b442a is described below
commit faf667b442ab038dabc9ece0f5f8c040e0f87a7b
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Dec 23 00:30:29 2025 +0800
Add ClassLoaderContextTest (#37462)
* Add ClassLoaderContextTest
* Add ClassLoaderContextTest
---
.../plugin/classloader/ClassLoaderContextTest.java | 64 ++++++++++++++++++++++
1 file changed, 64 insertions(+)
diff --git
a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/classloader/ClassLoaderContextTest.java
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/classloader/ClassLoaderContextTest.java
new file mode 100644
index 00000000000..ab2685941b8
--- /dev/null
+++
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/classloader/ClassLoaderContextTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.core.plugin.classloader;
+
+import lombok.SneakyThrows;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.internal.configuration.plugins.Plugins;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Collections;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class ClassLoaderContextTest {
+
+ @AfterEach
+ void cleanAgentClassLoaders() {
+ getAgentClassLoaders().clear();
+ }
+
+ @Test
+ void assertCreatePluginClassLoaderWhenAbsent() {
+ ClassLoader appClassLoader = new URLClassLoader(new URL[0], null);
+ AgentPluginClassLoader actual = new ClassLoaderContext(appClassLoader,
Collections.emptyList()).getPluginClassLoader();
+ Map<ClassLoader, AgentPluginClassLoader> agentClassLoaders =
getAgentClassLoaders();
+ assertThat(agentClassLoaders.size(), is(1));
+ assertThat(actual, is(agentClassLoaders.get(appClassLoader)));
+ }
+
+ @Test
+ void assertReuseExistingPluginClassLoader() {
+ ClassLoader appClassLoader = new URLClassLoader(new URL[0], null);
+ AgentPluginClassLoader expected = new
AgentPluginClassLoader(appClassLoader, Collections.emptyList());
+ Map<ClassLoader, AgentPluginClassLoader> agentClassLoaders =
getAgentClassLoaders();
+ agentClassLoaders.put(appClassLoader, expected);
+ AgentPluginClassLoader actual = new ClassLoaderContext(appClassLoader,
Collections.emptyList()).getPluginClassLoader();
+ assertThat(actual, is(expected));
+ }
+
+ @SuppressWarnings("unchecked")
+ @SneakyThrows(ReflectiveOperationException.class)
+ private Map<ClassLoader, AgentPluginClassLoader> getAgentClassLoaders() {
+ return (Map<ClassLoader, AgentPluginClassLoader>)
Plugins.getMemberAccessor().get(ClassLoaderContext.class.getDeclaredField("AGENT_CLASS_LOADERS"),
null);
+ }
+}