This is an automated email from the ASF dual-hosted git repository.

WangzJi pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.x by this push:
     new e6d0860a43 test: cover resource id initializers (#8146)
e6d0860a43 is described below

commit e6d0860a4345b10cb59c65c78215ec51d67f59d1
Author: Sicheng Han <[email protected]>
AuthorDate: Mon Jul 13 14:42:42 2026 +0800

    test: cover resource id initializers (#8146)
---
 changes/en-us/2.x.md                               |   2 +
 changes/zh-cn/2.x.md                               |   2 +
 .../initializer/db/ResourceIdInitializerTest.java  | 168 +++++++++++++++++++++
 3 files changed, 172 insertions(+)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index b53f6011aa..9070b7d4aa 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -40,6 +40,7 @@ Add changes here for all PR submitted to the 2.x branch.
 
 ### test:
 
+- [[#8146](https://github.com/apache/incubator-seata/pull/8146)] test: cover 
rm-datasource resource id initializers
 
 ### refactor:
 
@@ -56,6 +57,7 @@ Thanks to these contributors for their code commits. Please 
report an unintended
 - [Seol-JY](https://github.com/Seol-JY)
 - [lhozy](https://github.com/lhozy)
 - [Zhengcy05](https://github.com/Zhengcy05)
+- [neu-hsc](https://github.com/neu-hsc)
 
 
 
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 1fff30f7be..255f14c622 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -39,6 +39,7 @@
 
 ### test:
 
+- [[#8146](https://github.com/apache/incubator-seata/pull/8146)] test: 覆盖 
rm-datasource resource id initializers
 
 ### refactor:
 
@@ -55,6 +56,7 @@
 - [Seol-JY](https://github.com/Seol-JY)
 - [lhozy](https://github.com/lhozy)
 - [Zhengcy05](https://github.com/Zhengcy05)
+- [neu-hsc](https://github.com/neu-hsc)
 
 
 
diff --git 
a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/initializer/db/ResourceIdInitializerTest.java
 
b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/initializer/db/ResourceIdInitializerTest.java
new file mode 100644
index 0000000000..f74fbff7e4
--- /dev/null
+++ 
b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/initializer/db/ResourceIdInitializerTest.java
@@ -0,0 +1,168 @@
+/*
+ * 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.seata.rm.datasource.initializer.db;
+
+import org.apache.seata.rm.datasource.DataSourceProxy;
+import org.apache.seata.rm.datasource.initializer.ResourceIdInitializer;
+import 
org.apache.seata.rm.datasource.initializer.ResourceIdInitializerRegistry;
+import org.apache.seata.sqlparser.util.JdbcConstants;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class ResourceIdInitializerTest {
+
+    @Test
+    public void testRegistryReturnsDbSpecificInitializer() {
+        DataSourceProxy proxy = mock(DataSourceProxy.class);
+
+        assertInstanceOf(
+                PostgresqlResourceIdInitializer.class,
+                
ResourceIdInitializerRegistry.getInitializer(JdbcConstants.POSTGRESQL, proxy));
+        assertInstanceOf(
+                OracleResourceIdInitializer.class,
+                
ResourceIdInitializerRegistry.getInitializer(JdbcConstants.ORACLE, proxy));
+        assertInstanceOf(
+                MysqlResourceIdInitializer.class,
+                
ResourceIdInitializerRegistry.getInitializer(JdbcConstants.MYSQL, proxy));
+        assertInstanceOf(
+                MysqlResourceIdInitializer.class,
+                
ResourceIdInitializerRegistry.getInitializer(JdbcConstants.POLARDBX, proxy));
+        assertInstanceOf(
+                SqlServerResourceIdInitializer.class,
+                
ResourceIdInitializerRegistry.getInitializer(JdbcConstants.SQLSERVER, proxy));
+        assertInstanceOf(
+                DMResourceIdInitializer.class, 
ResourceIdInitializerRegistry.getInitializer(JdbcConstants.DM, proxy));
+        assertInstanceOf(
+                OscarResourceIdInitializer.class,
+                
ResourceIdInitializerRegistry.getInitializer(JdbcConstants.OSCAR, proxy));
+    }
+
+    @Test
+    public void testRegistryFallsBackToDefaultInitializer() {
+        ResourceIdInitializer initializer =
+                ResourceIdInitializerRegistry.getInitializer("h2", 
mock(DataSourceProxy.class));
+
+        assertInstanceOf(DefaultResourceIdInitializer.class, initializer);
+    }
+
+    @Test
+    public void testDefaultInitializerStripsQueryString() {
+        assertResourceId(
+                new DefaultResourceIdInitializer(), 
"jdbc:h2:mem:seata?mode=mysql&trace=true", "jdbc:h2:mem:seata");
+    }
+
+    @Test
+    public void testDefaultInitializerDoesNotSupportAnyDbTypeDirectly() {
+        assertFalse(new 
DefaultResourceIdInitializer().supports(JdbcConstants.MYSQL, 
mock(DataSourceProxy.class)));
+    }
+
+    @Test
+    public void testMysqlInitializerSupportsMysqlAndPolardbx() {
+        MysqlResourceIdInitializer initializer = new 
MysqlResourceIdInitializer();
+
+        assertTrue(initializer.supports(JdbcConstants.MYSQL, 
mock(DataSourceProxy.class)));
+        assertTrue(initializer.supports(JdbcConstants.POLARDBX, 
mock(DataSourceProxy.class)));
+    }
+
+    @Test
+    public void 
testMysqlLoadbalanceInitializerNormalizesHostsAndStripsQueryString() {
+        assertResourceId(
+                new MysqlResourceIdInitializer(),
+                
"jdbc:mysql:loadbalance://192.168.0.1:3306,192.168.0.2:3306/seata?useSSL=false",
+                
"jdbc:mysql:loadbalance://192.168.0.1:3306|192.168.0.2:3306/seata");
+    }
+
+    @Test
+    public void testMysqlInitializerStripsQueryStringForRegularUrl() {
+        assertResourceId(
+                new MysqlResourceIdInitializer(),
+                "jdbc:mysql://127.0.0.1:3306/seata?useSSL=false",
+                "jdbc:mysql://127.0.0.1:3306/seata");
+    }
+
+    @Test
+    public void 
testPostgresqlInitializerKeepsCurrentSchemaAndNormalizesSeparators() {
+        assertResourceId(
+                new PostgresqlResourceIdInitializer(),
+                
"jdbc:postgresql://host1:5432,host2:5432/seata?ssl=true&currentSchema=app,public&targetServerType=primary",
+                
"jdbc:postgresql://host1:5432|host2:5432/seata?currentSchema=app!public");
+    }
+
+    @Test
+    public void testDmInitializerKeepsSchemaAndRemovesQuotes() {
+        assertResourceId(
+                new DMResourceIdInitializer(),
+                
"jdbc:dm://127.0.0.1:5236?compatibleMode=mysql&schema=\"APP\"&socketTimeout=30",
+                "jdbc:dm://127.0.0.1:5236?schema=APP");
+    }
+
+    @Test
+    public void testOracleInitializerAppendsUserNameAndStripsQueryString() {
+        assertResourceId(
+                new OracleResourceIdInitializer(),
+                "jdbc:oracle:thin:@127.0.0.1:1521:orcl?remarksReporting=true",
+                "SEATA",
+                "jdbc:oracle:thin:@127.0.0.1:1521:orcl/SEATA");
+    }
+
+    @Test
+    public void testOscarInitializerAppendsUserNameAndStripsQueryString() {
+        assertResourceId(
+                new OscarResourceIdInitializer(),
+                "jdbc:oscar://127.0.0.1:2003/seata?characterEncoding=utf8",
+                "SEATA",
+                "jdbc:oscar://127.0.0.1:2003/seata/SEATA");
+    }
+
+    @Test
+    public void testSqlServerInitializerKeepsOnlyResourceIdentityProperties() {
+        assertResourceId(
+                new SqlServerResourceIdInitializer(),
+                
"jdbc:sqlserver://localhost:1433;encrypt=true;databaseName=seata;INSTANCENAME=sqlexpress;"
+                        + "trustServerCertificate=true",
+                
"jdbc:sqlserver://localhost:1433;databaseName=seata;INSTANCENAME=sqlexpress");
+    }
+
+    @Test
+    public void 
testSqlServerInitializerKeepsBaseUrlWhenNoIdentityPropertiesExist() {
+        assertResourceId(
+                new SqlServerResourceIdInitializer(),
+                
"jdbc:sqlserver://localhost:1433;encrypt=true;trustServerCertificate=true",
+                "jdbc:sqlserver://localhost:1433");
+    }
+
+    private void assertResourceId(ResourceIdInitializer initializer, String 
jdbcUrl, String expectedResourceId) {
+        assertResourceId(initializer, jdbcUrl, null, expectedResourceId);
+    }
+
+    private void assertResourceId(
+            ResourceIdInitializer initializer, String jdbcUrl, String 
userName, String expectedResourceId) {
+        DataSourceProxy proxy = mock(DataSourceProxy.class);
+        when(proxy.getJdbcUrl()).thenReturn(jdbcUrl);
+        when(proxy.getUserName()).thenReturn(userName);
+
+        initializer.initResourceId(proxy);
+
+        verify(proxy).setResourceId(expectedResourceId);
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to