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 31b3ba946db Add coverage for core, plugin SPI, metrics registry and 
tracing advices (#37523)
31b3ba946db is described below

commit 31b3ba946db89e68077a30390cc4928ce5344da7
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Dec 25 22:26:36 2025 +0800

    Add coverage for core, plugin SPI, metrics registry and tracing advices 
(#37523)
    
    * Add coverage for core, plugin SPI, metrics registry and tracing advices
    
    * Add coverage for core, plugin SPI, metrics registry and tracing advices
    
    * Add coverage for core, plugin SPI, metrics registry and tracing advices
---
 .../config/AdvisorConfigurationLoaderTest.java     |  87 ++++++++++++++++
 .../core/advisor/executor/AdviceFactoryTest.java   |  54 ++++++++++
 .../TargetAdviceObjectBuilderInterceptorTest.java  |  42 ++++++++
 .../plugin/classloader/ClassLoaderContextTest.java |  44 +++++++++
 .../agent/core/yaml/AgentYamlConstructorTest.java  |  34 +++++++
 .../ShardingSphereDataSourceContextHolderTest.java |  46 +++++++++
 .../plugin/core/spi/PluginServiceLoaderTest.java   |  66 +++++++++++++
 .../plugin/core/util/SQLStatementUtilsTest.java    |  73 ++++++++++++++
 .../collector/MetricsCollectorRegistryTest.java    |  48 +++++++++
 .../plugin/tracing/core/RootSpanContextTest.java   |  40 ++++++++
 .../core/advice/TracingRootSpanAdviceTest.java     | 109 +++++++++++++++++++++
 11 files changed, 643 insertions(+)

diff --git 
a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/AdvisorConfigurationLoaderTest.java
 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/AdvisorConfigurationLoaderTest.java
new file mode 100644
index 00000000000..562ac41c23a
--- /dev/null
+++ 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/AdvisorConfigurationLoaderTest.java
@@ -0,0 +1,87 @@
+/*
+ * 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.advisor.config;
+
+import net.bytebuddy.matcher.ElementMatcher;
+import net.bytebuddy.matcher.ElementMatchers;
+import 
org.apache.shardingsphere.agent.core.advisor.config.yaml.fixture.YamlAdviceFixture;
+import 
org.apache.shardingsphere.agent.core.advisor.config.yaml.fixture.YamlTargetObjectFixture;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class AdvisorConfigurationLoaderTest {
+    
+    private static final String TARGET = 
YamlTargetObjectFixture.class.getName();
+    
+    private static final String ADVICE = YamlAdviceFixture.class.getName();
+    
+    @Test
+    void assertLoadAndMergeAdvisorConfigurations() throws IOException {
+        File jarFile = File.createTempFile("advisor-config", ".jar");
+        try (JarOutputStream jarOutputStream = new 
JarOutputStream(Files.newOutputStream(jarFile.toPath()))) {
+            writeAdvisorResource(jarOutputStream, "fixture", "constructor");
+            writeAdvisorResource(jarOutputStream, "another", "method");
+        }
+        try (JarFile jar = new JarFile(jarFile)) {
+            Map<String, AdvisorConfiguration> actual = 
AdvisorConfigurationLoader.load(Collections.singleton(jar), 
Arrays.asList("FIXTURE", "ANOTHER", "MISSING"));
+            assertThat(actual.size(), is(1));
+            AdvisorConfiguration config = actual.get(TARGET);
+            assertThat(config.getTargetClassName(), is(TARGET));
+            assertThat(config.getAdvisors().size(), is(2));
+            assertAdvisor(config.getAdvisors(), 
ElementMatchers.isConstructor(), "FIXTURE");
+            assertAdvisor(config.getAdvisors(), ElementMatchers.named("call"), 
"ANOTHER");
+        } finally {
+            assertTrue(jarFile.delete());
+        }
+    }
+    
+    private void writeAdvisorResource(final JarOutputStream jarOutputStream, 
final String pluginType, final String pointcutType) throws IOException {
+        JarEntry entry = new JarEntry(String.join("/", "META-INF", "conf", 
String.join("-", pluginType, "advisors.yaml")));
+        jarOutputStream.putNextEntry(entry);
+        jarOutputStream.write(createYaml(pointcutType).getBytes());
+        jarOutputStream.closeEntry();
+    }
+    
+    private String createYaml(final String pointcutType) {
+        return String.join(System.lineSeparator(),
+                "advisors:",
+                "  - target: " + TARGET,
+                "    advice: " + ADVICE,
+                "    pointcuts:",
+                "      - type: " + pointcutType,
+                "        name: call");
+    }
+    
+    private void assertAdvisor(final Collection<MethodAdvisorConfiguration> 
actual, final ElementMatcher<?> pointcut, final String pluginType) {
+        assertTrue(actual.stream().anyMatch(each -> 
each.getAdviceClassName().equals(ADVICE) && 
each.getPluginType().equals(pluginType) && 
each.getPointcut().equals(pointcut)));
+    }
+}
diff --git 
a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/executor/AdviceFactoryTest.java
 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/executor/AdviceFactoryTest.java
new file mode 100644
index 00000000000..6f68c830cac
--- /dev/null
+++ 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/executor/AdviceFactoryTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.advisor.executor;
+
+import 
org.apache.shardingsphere.agent.core.plugin.classloader.AgentPluginClassLoader;
+import 
org.apache.shardingsphere.agent.core.plugin.classloader.ClassLoaderContext;
+import org.apache.shardingsphere.fixture.advice.BarAdvice;
+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;
+import static org.hamcrest.Matchers.isA;
+
+class AdviceFactoryTest {
+    
+    @AfterEach
+    void resetCache() throws ReflectiveOperationException {
+        ((Map<?, ?>) 
Plugins.getMemberAccessor().get(AdviceFactory.class.getDeclaredField("CACHED_ADVICES"),
 null)).clear();
+        ((Map<?, ?>) 
Plugins.getMemberAccessor().get(ClassLoaderContext.class.getDeclaredField("AGENT_CLASS_LOADERS"),
 null)).clear();
+    }
+    
+    @Test
+    void assertGetAdvice() {
+        ClassLoaderContext classLoaderContext = new ClassLoaderContext(new 
URLClassLoader(new URL[0], getClass().getClassLoader()), 
Collections.emptyList());
+        AdviceFactory adviceFactory = new AdviceFactory(classLoaderContext);
+        Object first = adviceFactory.getAdvice(BarAdvice.class.getName());
+        Object second = adviceFactory.getAdvice(BarAdvice.class.getName());
+        assertThat(first, isA(BarAdvice.class));
+        assertThat(first, is(second));
+        assertThat(classLoaderContext.getPluginClassLoader(), 
isA(AgentPluginClassLoader.class));
+    }
+}
diff --git 
a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/builder/interceptor/impl/TargetAdviceObjectBuilderInterceptorTest.java
 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/builder/interceptor/impl/TargetAdviceObjectBuilderInterceptorTest.java
new file mode 100644
index 00000000000..d9e94ac2926
--- /dev/null
+++ 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/builder/interceptor/impl/TargetAdviceObjectBuilderInterceptorTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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.builder.interceptor.impl;
+
+import net.bytebuddy.ByteBuddy;
+import net.bytebuddy.dynamic.DynamicType;
+import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
+import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class TargetAdviceObjectBuilderInterceptorTest {
+    
+    @Test
+    void assertInterceptDefineFieldAndInterface() throws 
ReflectiveOperationException {
+        DynamicType.Unloaded<?> unloaded = new 
TargetAdviceObjectBuilderInterceptor().intercept(new 
ByteBuddy().subclass(Object.class)).make();
+        Class<?> targetClass = unloaded.load(getClass().getClassLoader(), 
ClassLoadingStrategy.Default.INJECTION).getLoaded();
+        assertTrue(TargetAdviceObject.class.isAssignableFrom(targetClass));
+        TargetAdviceObject instance = (TargetAdviceObject) 
targetClass.getDeclaredConstructor().newInstance();
+        instance.setAttachment("foo");
+        assertThat(instance.getAttachment(), is("foo"));
+        assertThat(targetClass.getDeclaredField("_$EXTRA_DATA$_").getType(), 
is(Object.class));
+    }
+}
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..51b2bb47255
--- /dev/null
+++ 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/classloader/ClassLoaderContextTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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 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 reset() throws ReflectiveOperationException {
+        ((Map<?, ?>) 
Plugins.getMemberAccessor().get(ClassLoaderContext.class.getDeclaredField("AGENT_CLASS_LOADERS"),
 null)).clear();
+    }
+    
+    @Test
+    void assertGetPluginClassLoader() {
+        ClassLoaderContext context = new ClassLoaderContext(new 
URLClassLoader(new URL[0], getClass().getClassLoader()), 
Collections.emptyList());
+        assertThat(context.getPluginClassLoader(), 
is(context.getPluginClassLoader()));
+    }
+}
diff --git 
a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/yaml/AgentYamlConstructorTest.java
 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/yaml/AgentYamlConstructorTest.java
new file mode 100644
index 00000000000..92f63135cdd
--- /dev/null
+++ 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/yaml/AgentYamlConstructorTest.java
@@ -0,0 +1,34 @@
+/*
+ * 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.yaml;
+
+import 
org.apache.shardingsphere.agent.core.plugin.config.yaml.entity.YamlAgentConfiguration;
+import org.junit.jupiter.api.Test;
+import org.yaml.snakeyaml.LoaderOptions;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class AgentYamlConstructorTest {
+    
+    @Test
+    void assertAcceptRootClass() throws ClassNotFoundException {
+        AgentYamlConstructor constructor = new 
AgentYamlConstructor(YamlAgentConfiguration.class, new LoaderOptions());
+        
assertThat(constructor.getClassForName(YamlAgentConfiguration.class.getName()), 
is((Object) YamlAgentConfiguration.class));
+    }
+}
diff --git 
a/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/holder/ShardingSphereDataSourceContextHolderTest.java
 
b/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/holder/ShardingSphereDataSourceContextHolderTest.java
new file mode 100644
index 00000000000..785d6354a54
--- /dev/null
+++ 
b/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/holder/ShardingSphereDataSourceContextHolderTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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.plugin.core.holder;
+
+import 
org.apache.shardingsphere.agent.plugin.core.context.ShardingSphereDataSourceContext;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+class ShardingSphereDataSourceContextHolderTest {
+    
+    @AfterEach
+    void reset() {
+        
ShardingSphereDataSourceContextHolder.getShardingSphereDataSourceContexts().clear();
+    }
+    
+    @Test
+    void assertPutGetAndRemove() {
+        ShardingSphereDataSourceContext context = new 
ShardingSphereDataSourceContext("logic_db", mock(ContextManager.class));
+        ShardingSphereDataSourceContextHolder.put("instance_1", context);
+        
assertThat(ShardingSphereDataSourceContextHolder.getShardingSphereDataSourceContexts().size(),
 is(1));
+        
assertThat(ShardingSphereDataSourceContextHolder.getShardingSphereDataSourceContexts().get("instance_1"),
 is(context));
+        ShardingSphereDataSourceContextHolder.remove("instance_1");
+        
assertTrue(ShardingSphereDataSourceContextHolder.getShardingSphereDataSourceContexts().isEmpty());
+    }
+}
diff --git 
a/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/spi/PluginServiceLoaderTest.java
 
b/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/spi/PluginServiceLoaderTest.java
new file mode 100644
index 00000000000..b88cc5a279f
--- /dev/null
+++ 
b/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/spi/PluginServiceLoaderTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.plugin.core.spi;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.internal.configuration.plugins.Plugins;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.ServiceLoader;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+class PluginServiceLoaderTest {
+    
+    @AfterEach
+    void reset() throws ReflectiveOperationException {
+        ((Map<?, ?>) 
Plugins.getMemberAccessor().get(PluginServiceLoader.class.getDeclaredField("LOADERS"),
 null)).clear();
+    }
+    
+    @SuppressWarnings("rawtypes")
+    @Test
+    void assertGetServiceLoader() {
+        PluginTypedSPI service = mock(PluginTypedSPI.class);
+        when(service.getType()).thenReturn("FIXTURE");
+        try (MockedStatic<ServiceLoader> ignored = mockServiceLoader(service)) 
{
+            
assertThat(PluginServiceLoader.getServiceLoader(PluginTypedSPI.class).getService("fixture").getType(),
 is("FIXTURE"));
+        }
+    }
+    
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private MockedStatic<ServiceLoader> mockServiceLoader(final 
PluginTypedSPI... services) {
+        ServiceLoader<PluginTypedSPI> serviceLoader = 
mock(ServiceLoader.class);
+        Iterator<PluginTypedSPI> iterator = services.length == 0 ? 
Collections.emptyIterator() : Arrays.asList(services).iterator();
+        when(serviceLoader.iterator()).thenReturn(iterator);
+        return mockStatic(ServiceLoader.class, invocation -> {
+            if ("load".equals(invocation.getMethod().getName())) {
+                return serviceLoader;
+            }
+            return invocation.callRealMethod();
+        });
+    }
+}
diff --git 
a/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/SQLStatementUtilsTest.java
 
b/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/SQLStatementUtilsTest.java
new file mode 100644
index 00000000000..7a7d9beb482
--- /dev/null
+++ 
b/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/SQLStatementUtilsTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.plugin.core.util;
+
+import org.apache.shardingsphere.agent.plugin.core.enums.SQLStatementType;
+import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
+import org.apache.shardingsphere.distsql.statement.type.ral.RALStatement;
+import org.apache.shardingsphere.distsql.statement.type.rdl.RDLStatement;
+import org.apache.shardingsphere.distsql.statement.type.rql.RQLStatement;
+import org.apache.shardingsphere.distsql.statement.type.rul.RULStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.DCLStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.DDLStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.DMLStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.DeleteStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.InsertStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.UpdateStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.TCLStatement;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.stream.Stream;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+
+class SQLStatementUtilsTest {
+    
+    @ParameterizedTest(name = "{index}: {0}")
+    @MethodSource("provideStatements")
+    void assertGetType(final String name, final SQLStatement sqlStatement, 
final SQLStatementType expectedType) {
+        assertThat(SQLStatementUtils.getType(sqlStatement), is(expectedType));
+    }
+    
+    private static Stream<Arguments> provideStatements() {
+        return Stream.of(
+                Arguments.of("null", null, SQLStatementType.OTHER),
+                Arguments.of("select", mock(SelectStatement.class), 
SQLStatementType.SELECT),
+                Arguments.of("insert", mock(InsertStatement.class), 
SQLStatementType.INSERT),
+                Arguments.of("update", mock(UpdateStatement.class), 
SQLStatementType.UPDATE),
+                Arguments.of("delete", mock(DeleteStatement.class), 
SQLStatementType.DELETE),
+                Arguments.of("dml-other", mock(DMLStatement.class), 
SQLStatementType.DML),
+                Arguments.of("ddl", mock(DDLStatement.class), 
SQLStatementType.DDL),
+                Arguments.of("dcl", mock(DCLStatement.class), 
SQLStatementType.DCL),
+                Arguments.of("dal", mock(DALStatement.class), 
SQLStatementType.DAL),
+                Arguments.of("tcl", mock(TCLStatement.class), 
SQLStatementType.TCL),
+                Arguments.of("rql", mock(RQLStatement.class), 
SQLStatementType.RQL),
+                Arguments.of("rdl", mock(RDLStatement.class), 
SQLStatementType.RDL),
+                Arguments.of("ral", mock(RALStatement.class), 
SQLStatementType.RAL),
+                Arguments.of("rul", mock(RULStatement.class), 
SQLStatementType.RUL),
+                Arguments.of("distsql-other", mock(DistSQLStatement.class), 
SQLStatementType.OTHER),
+                Arguments.of("other", mock(SQLStatement.class), 
SQLStatementType.OTHER));
+    }
+}
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/collector/MetricsCollectorRegistryTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/collector/MetricsCollectorRegistryTest.java
new file mode 100644
index 00000000000..c2b2f00f847
--- /dev/null
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/collector/MetricsCollectorRegistryTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.plugin.metrics.core.collector;
+
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.CounterMetricsCollector;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.internal.configuration.plugins.Plugins;
+
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class MetricsCollectorRegistryTest {
+    
+    @AfterEach
+    void reset() throws ReflectiveOperationException {
+        ((Map<?, ?>) 
Plugins.getMemberAccessor().get(MetricsCollectorRegistry.class.getDeclaredField("COLLECTORS"),
 null)).clear();
+    }
+    
+    @Test
+    void assertGet() {
+        MetricConfiguration metricConfig = new 
MetricConfiguration("fixture_metric", MetricCollectorType.COUNTER, "help");
+        CounterMetricsCollector first = 
MetricsCollectorRegistry.get(metricConfig, "FIXTURE");
+        first.inc();
+        CounterMetricsCollector second = 
MetricsCollectorRegistry.get(metricConfig, "FIXTURE");
+        assertThat(first, is(second));
+        assertThat(second.toString(), is("1"));
+    }
+}
diff --git 
a/agent/plugins/tracing/core/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/core/RootSpanContextTest.java
 
b/agent/plugins/tracing/core/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/core/RootSpanContextTest.java
new file mode 100644
index 00000000000..f53ad2d4a4b
--- /dev/null
+++ 
b/agent/plugins/tracing/core/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/core/RootSpanContextTest.java
@@ -0,0 +1,40 @@
+/*
+ * 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.plugin.tracing.core;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class RootSpanContextTest {
+    
+    @AfterEach
+    void reset() {
+        RootSpanContext.set(null);
+    }
+    
+    @Test
+    void assertSetAndGet() {
+        assertNull(RootSpanContext.get());
+        RootSpanContext.set("root");
+        assertThat(RootSpanContext.get(), is("root"));
+    }
+}
diff --git 
a/agent/plugins/tracing/core/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/core/advice/TracingRootSpanAdviceTest.java
 
b/agent/plugins/tracing/core/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/core/advice/TracingRootSpanAdviceTest.java
new file mode 100644
index 00000000000..67ca4cc4017
--- /dev/null
+++ 
b/agent/plugins/tracing/core/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/core/advice/TracingRootSpanAdviceTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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.plugin.tracing.core.advice;
+
+import org.apache.shardingsphere.agent.api.advice.TargetAdviceMethod;
+import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
+import org.apache.shardingsphere.agent.plugin.tracing.core.RootSpanContext;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class TracingRootSpanAdviceTest {
+    
+    @AfterEach
+    void reset() {
+        RootSpanContext.set(null);
+    }
+    
+    @Test
+    void assertCreateRootSpanAndContextSet() {
+        RecordingTracingRootSpanAdvice advice = new 
RecordingTracingRootSpanAdvice();
+        TargetAdviceObject target = new SimpleTargetAdviceObject();
+        TargetAdviceMethod method = new TargetAdviceMethod("mock");
+        advice.beforeMethod(target, method, new Object[]{"arg"}, "FIXTURE");
+        assertThat(RootSpanContext.get(), is("root-mock"));
+    }
+    
+    @Test
+    void assertFinishRootSpan() {
+        RecordingTracingRootSpanAdvice advice = new 
RecordingTracingRootSpanAdvice();
+        TargetAdviceObject target = new SimpleTargetAdviceObject();
+        TargetAdviceMethod method = new TargetAdviceMethod("mock");
+        advice.beforeMethod(target, method, new Object[]{"arg"}, "FIXTURE");
+        advice.afterMethod(target, method, new Object[]{"arg"}, null, 
"FIXTURE");
+        assertThat(advice.getFinished(), is("root-mock"));
+    }
+    
+    @Test
+    void assertRecordException() {
+        RecordingTracingRootSpanAdvice advice = new 
RecordingTracingRootSpanAdvice();
+        TargetAdviceObject target = new SimpleTargetAdviceObject();
+        TargetAdviceMethod method = new TargetAdviceMethod("mock");
+        advice.beforeMethod(target, method, new Object[]{"arg"}, "FIXTURE");
+        advice.onThrowing(target, method, new Object[]{"arg"}, new 
IllegalStateException("error"), "FIXTURE");
+        assertThat(advice.getExceptionRecorded(), is("root-mock"));
+    }
+    
+    private static final class RecordingTracingRootSpanAdvice extends 
TracingRootSpanAdvice<String> {
+        
+        private String finished;
+        
+        private String exceptionRecorded;
+        
+        @Override
+        protected String createRootSpan(final TargetAdviceObject target, final 
TargetAdviceMethod method, final Object[] args) {
+            return "root-" + method.getName();
+        }
+        
+        @Override
+        protected void finishRootSpan(final String rootSpan, final 
TargetAdviceObject target) {
+            finished = rootSpan;
+        }
+        
+        @Override
+        protected void recordException(final String rootSpan, final 
TargetAdviceObject target, final Throwable throwable) {
+            exceptionRecorded = rootSpan;
+        }
+        
+        String getFinished() {
+            return finished;
+        }
+        
+        String getExceptionRecorded() {
+            return exceptionRecorded;
+        }
+    }
+    
+    private static final class SimpleTargetAdviceObject implements 
TargetAdviceObject {
+        
+        private Object attachment;
+        
+        @Override
+        public Object getAttachment() {
+            return attachment;
+        }
+        
+        @Override
+        public void setAttachment(final Object attachment) {
+            this.attachment = attachment;
+        }
+    }
+}


Reply via email to