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

jimin 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 800a69c105 test: add unit tests for the `seata-common` module (#7672)
800a69c105 is described below

commit 800a69c1053eddde37de9f90d76fbc5ec2436d31
Author: Shuxin Pan <[email protected]>
AuthorDate: Mon Sep 29 14:13:08 2025 +0800

    test: add unit tests for the `seata-common` module (#7672)
---
 changes/en-us/2.x.md                               |   3 +-
 changes/zh-cn/2.x.md                               |   3 +-
 .../org/apache/seata/common/util/PageUtilTest.java | 165 +++++++++++++++++++++
 3 files changed, 169 insertions(+), 2 deletions(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 32658d7018..a140ef6c9e 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -69,6 +69,7 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#7580](https://github.com/seata/seata/pull/7580)]  fix the exception 
caused by the disorder of test case execution order
 - [[#7584](https://github.com/apache/incubator-seata/pull/7584)] deflake 
ConsulConfigurationTest#testInitSeataConfig with short await/retry to absorb CI 
timing delay
 - [[#7610](https://github.com/apache/incubator-seata/pull/7610)] Enable Nacos 
integration tests when nacosCaseEnabled is true
+- [[#7672](https://github.com/apache/incubator-seata/pull/7672)] Add unit 
tests for the `seata-common` module
 
 ### refactor:
 
@@ -99,6 +100,6 @@ Thanks to these contributors for their code commits. Please 
report an unintended
 - [WangzJi](https://github.com/WangzJi)
 - [Asuka-star](https://github.com/Asuka-star)
 - [YoWuwuuuw](https://github.com/YoWuwuuuw)
-
+- [psxjoy](https://github.com/psxjoy)
 
 Also, we receive many valuable issues, questions and advices from our 
community. Thanks for you all.
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 7bdf2afd13..eeb1a495ad 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -69,6 +69,7 @@
 - [[#7580](https://github.com/seata/seata/pull/7580)]  修复测试用例顺序错乱导致的异常
 - [[#7584](https://github.com/seata/seata/pull/7584)] 修复 
ConsulConfigurationTest#testInitSeataConfig 在 CI 中由于等待/重试时间过短导致的不稳定问题
 - [[#7610](https://github.com/apache/incubator-seata/pull/7610)] 
当nacosCaseEnabled为true时启用nacos集成测试
+- [[#7672](https://github.com/apache/incubator-seata/pull/7672)] 增加 
`seata-common` 模块的测试用例
 
 
 ### refactor:
@@ -100,6 +101,6 @@
 - [WangzJi](https://github.com/WangzJi)
 - [Asuka-star](https://github.com/Asuka-star)
 - [YoWuwuuuw](https://github.com/YoWuwuuuw)
-
+- [psxjoy](https://github.com/psxjoy)
 
 同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
diff --git 
a/common/src/test/java/org/apache/seata/common/util/PageUtilTest.java 
b/common/src/test/java/org/apache/seata/common/util/PageUtilTest.java
index 1ae40a56b9..4abafce5d8 100644
--- a/common/src/test/java/org/apache/seata/common/util/PageUtilTest.java
+++ b/common/src/test/java/org/apache/seata/common/util/PageUtilTest.java
@@ -34,6 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doNothing;
 
 /**
@@ -74,6 +75,7 @@ public class PageUtilTest {
         assertEquals(PageUtil.pageSql(sourceSql, "oceanbase", 1, 5), 
mysqlTargetSql);
         assertEquals(PageUtil.pageSql(sourceSql, "dm", 1, 5), mysqlTargetSql);
         assertEquals(PageUtil.pageSql(sourceSql, "oscar", 1, 5), 
mysqlTargetSql);
+        assertEquals(PageUtil.pageSql(sourceSql, "kingbase", 1, 5), 
mysqlTargetSql);
         assertEquals(PageUtil.pageSql(sourceSql, "oracle", 1, 5), 
oracleTargetSql);
         assertEquals(PageUtil.pageSql(sourceSql, "sqlserver", 1, 5), 
sqlserverTargetSql);
 
@@ -92,6 +94,7 @@ public class PageUtilTest {
         assertEquals(PageUtil.countSql(sourceSql, "oceanbase"), targetSql);
         assertEquals(PageUtil.countSql(sourceSql, "dm"), targetSql);
         assertEquals(PageUtil.countSql(sourceSql, "oscar"), targetSql);
+        assertEquals(PageUtil.countSql(sourceSql, "kingbase"), targetSql);
         assertEquals(PageUtil.countSql(sourceSql, "oracle"), targetSql);
         assertEquals(PageUtil.countSql(sourceSql, "sqlserver"), targetSql);
 
@@ -189,4 +192,166 @@ public class PageUtilTest {
                 IllegalArgumentException.class,
                 () -> PageUtil.getTimeStartSql(notSupportedDBType, 
validTimeColumnName));
     }
+
+    @Test
+    public void testGetTimeEndSqlSupportedDBTypes() {
+        String[] supportedDBTypes = {"mysql", "oracle", "postgresql", 
"sqlserver", "dm", "oscar"};
+        String expectedSQL = " and FLOOR(gmt_create/1000) <= ? ";
+
+        for (String dbType : supportedDBTypes) {
+            assertEquals(expectedSQL, PageUtil.getTimeEndSql(dbType, 
validTimeColumnName));
+        }
+    }
+
+    @Test
+    public void testGetTimeEndSqlNotSupportedDBType() {
+        String notSupportedDBType = "xxx";
+        assertThrows(
+                IllegalArgumentException.class, () -> 
PageUtil.getTimeEndSql(notSupportedDBType, validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeStartSqlMySQL() {
+        String expectedSQL = " and UNIX_TIMESTAMP(gmt_create) >= ? ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeStartSql("mysql", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeStartSqlPostgreSQL() {
+        String expectedSQL = " and gmt_create >= TO_TIMESTAMP(?) ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeStartSql("postgresql", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeStartSqlOracle() {
+        String expectedSQL =
+                " and gmt_create >= TO_TIMESTAMP('1970-01-01 00:00:00', 
'YYYY-MM-DD HH24:MI:SS') + NUMTODSINTERVAL(?, 'SECOND') ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeStartSql("oracle", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeStartSqlSQLServer() {
+        String expectedSQL = " and gmt_create >= DATEADD(SECOND, ?, 
'1970-01-01 00:00:00') ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeStartSql("sqlserver", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeStartSqlDM() {
+        String expectedSQL =
+                " and gmt_create >= TO_TIMESTAMP('1970-01-01 00:00:00', 
'YYYY-MM-DD HH24:MI:SS') + NUMTODSINTERVAL(?, 'SECOND') ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeStartSql("dm", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeStartSqlOscar() {
+        String expectedSQL =
+                " and gmt_create >= TO_TIMESTAMP('1970-01-01 00:00:00', 
'YYYY-MM-DD HH24:MI:SS') + NUMTODSINTERVAL(?, 'SECOND') ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeStartSql("oscar", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeStartSqlNotSupportedDBType() {
+        String notSupportedDBType = "xxx";
+        assertThrows(
+                IllegalArgumentException.class,
+                () -> PageUtil.getDateTimeStartSql(notSupportedDBType, 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeEndSqlMySQL() {
+        String expectedSQL = " and UNIX_TIMESTAMP(gmt_create) <= ? ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeEndSql("mysql", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeEndSqlPostgreSQL() {
+        String expectedSQL = " and gmt_create <= TO_TIMESTAMP(?) ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeEndSql("postgresql", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeEndSqlOracle() {
+        String expectedSQL =
+                " and gmt_create <= TO_TIMESTAMP('1970-01-01 00:00:00', 
'YYYY-MM-DD HH24:MI:SS') + NUMTODSINTERVAL(?, 'SECOND') ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeEndSql("oracle", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeEndSqlSQLServer() {
+        String expectedSQL = " and gmt_create <= DATEADD(SECOND, ?, 
'1970-01-01 00:00:00') ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeEndSql("sqlserver", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeEndSqlDM() {
+        String expectedSQL =
+                " and gmt_create <= TO_TIMESTAMP('1970-01-01 00:00:00', 
'YYYY-MM-DD HH24:MI:SS') + NUMTODSINTERVAL(?, 'SECOND') ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeEndSql("dm", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeEndSqlOscar() {
+        String expectedSQL =
+                " and gmt_create <= TO_TIMESTAMP('1970-01-01 00:00:00', 
'YYYY-MM-DD HH24:MI:SS') + NUMTODSINTERVAL(?, 'SECOND') ";
+        assertEquals(expectedSQL, PageUtil.getDateTimeEndSql("oscar", 
validTimeColumnName));
+    }
+
+    @Test
+    public void testGetDateTimeEndSqlNotSupportedDBType() {
+        String notSupportedDBType = "xxx";
+        assertThrows(
+                IllegalArgumentException.class,
+                () -> PageUtil.getDateTimeEndSql(notSupportedDBType, 
validTimeColumnName));
+    }
+
+    @Test
+    public void testCountSqlWithOrderByPostgreSQL() {
+        String sourceSqlWithOrderBy = "select * from test where a = 1 order by 
id desc";
+        String expectedSql = "select count(1) from test where a = 1 ";
+
+        assertEquals(expectedSql, PageUtil.countSql(sourceSqlWithOrderBy, 
"postgresql"));
+        assertEquals(expectedSql, PageUtil.countSql(sourceSqlWithOrderBy, 
"kingbase"));
+        assertEquals(expectedSql, PageUtil.countSql(sourceSqlWithOrderBy, 
"sqlserver"));
+    }
+
+    @Test
+    public void testCountSqlWithoutOrderByPostgreSQL() {
+        String sourceSqlWithoutOrderBy = "select * from test where a = 1";
+        String expectedSql = "select count(1) from test where a = 1";
+
+        assertEquals(expectedSql, PageUtil.countSql(sourceSqlWithoutOrderBy, 
"postgresql"));
+        assertEquals(expectedSql, PageUtil.countSql(sourceSqlWithoutOrderBy, 
"kingbase"));
+        assertEquals(expectedSql, PageUtil.countSql(sourceSqlWithoutOrderBy, 
"sqlserver"));
+    }
+
+    @Test
+    public void testSetObjectWithSQLException() throws SQLException {
+        List<Object> params = new ArrayList<>();
+        params.add("test");
+
+        PreparedStatement preparedStatement = 
Mockito.mock(PreparedStatement.class);
+        Mockito.doThrow(new SQLException("Test exception"))
+                .when(preparedStatement)
+                .setObject(anyInt(), any());
+
+        assertThrows(SQLException.class, () -> 
PageUtil.setObject(preparedStatement, params));
+    }
+
+    @Test
+    public void testSetObjectWithMultipleTypes() throws SQLException {
+        List<Object> params = new ArrayList<>();
+        params.add(new Date(System.currentTimeMillis()));
+        params.add("testString");
+        params.add(123);
+        params.add(null);
+
+        PreparedStatement preparedStatement = 
Mockito.mock(PreparedStatement.class);
+        doNothing().when(preparedStatement).setDate(anyInt(), 
any(java.sql.Date.class));
+        doNothing().when(preparedStatement).setObject(anyInt(), any());
+
+        PageUtil.setObject(preparedStatement, params);
+
+        Mockito.verify(preparedStatement, Mockito.times(1)).setDate(eq(1), 
any(java.sql.Date.class));
+        Mockito.verify(preparedStatement, 
Mockito.times(3)).setObject(anyInt(), any());
+    }
 }


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

Reply via email to