diff --git a/core/src/test/java/org/apache/seata/core/store/db/sql/distributed/lock/BaseDistributedLockSqlServerTest.java b/core/src/test/java/org/apache/seata/core/store/db/sql/distributed/lock/BaseDistributedLockSqlServerTest.java new file mode 100644 index 00000000000..ddcd17ef1ba --- /dev/null +++ b/core/src/test/java/org/apache/seata/core/store/db/sql/distributed/lock/BaseDistributedLockSqlServerTest.java @@ -0,0 +1,72 @@ +/* + * 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.core.store.db.sql.distributed.lock; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; + +/** + * the base distributed lock sql server test. + * @author cx + */ +public class BaseDistributedLockSqlServerTest { + + private static final String GLOBAL_TABLE = "global_table"; + + private static final String BRANCH_TABLE = "branch_table"; + + private static final String EXPECT_INSERT_GLOBAL_SQL = "INSERT INTO " + GLOBAL_TABLE + "(lock_key,lock_value,expire) VALUES (?, ?, ?)"; + + private static final String EXPECT_INSERT_BRANCH_SQL = "INSERT INTO " + BRANCH_TABLE + "(lock_key,lock_value,expire) VALUES (?, ?, ?)"; + + private static final String EXPECT_UPDATE_GLOBAL_SQL = "UPDATE " + GLOBAL_TABLE + " SET lock_value=?, expire=? WHERE lock_key=?"; + + private static final String EXPECT_UPDATE_BRANCH_SQL = "UPDATE " + BRANCH_TABLE + " SET lock_value=?, expire=? WHERE lock_key=?"; + + private static final String EXPECT_SELECT_FOR_UPDATE_GLOBAL_SQL = "SELECT lock_key,lock_value,expire FROM " + GLOBAL_TABLE + " WITH (ROWLOCK, UPDLOCK, HOLDLOCK) WHERE lock_key = ?"; + + private static final String EXPECT_SELECT_FOR_UPDATE_BRANCH_SQL = "SELECT lock_key,lock_value,expire FROM " + BRANCH_TABLE + " WITH (ROWLOCK, UPDLOCK, HOLDLOCK) WHERE lock_key = ?"; + + private static final DistributedLockSql DISTRIBUTEDLOCKSQL = DistributedLockSqlFactory.getDistributedLogStoreSql("sqlServer"); + + @Test + public void testGetInsertSql() { + String sql; + sql = DISTRIBUTEDLOCKSQL.getInsertSql(GLOBAL_TABLE); + Assertions.assertEquals(EXPECT_INSERT_GLOBAL_SQL, sql); + sql = DISTRIBUTEDLOCKSQL.getInsertSql(BRANCH_TABLE); + Assertions.assertEquals(EXPECT_INSERT_BRANCH_SQL, sql); + } + + @Test + public void testGetUpdateSql() { + String sql; + sql = DISTRIBUTEDLOCKSQL.getUpdateSql(GLOBAL_TABLE); + Assertions.assertEquals(EXPECT_UPDATE_GLOBAL_SQL, sql); + sql = DISTRIBUTEDLOCKSQL.getUpdateSql(BRANCH_TABLE); + Assertions.assertEquals(EXPECT_UPDATE_BRANCH_SQL, sql); + } + + @Test + public void testGetSelectDistributeForUpdateSql() { + String sql; + sql = DISTRIBUTEDLOCKSQL.getSelectDistributeForUpdateSql(GLOBAL_TABLE); + Assertions.assertEquals(EXPECT_SELECT_FOR_UPDATE_GLOBAL_SQL, sql); + sql = DISTRIBUTEDLOCKSQL.getSelectDistributeForUpdateSql(BRANCH_TABLE); + Assertions.assertEquals(EXPECT_SELECT_FOR_UPDATE_BRANCH_SQL, sql); + } +} diff --git a/core/src/test/java/org/apache/seata/core/store/db/sql/distributed/lock/BaseDistributedLockSqlTest.java b/core/src/test/java/org/apache/seata/core/store/db/sql/distributed/lock/BaseDistributedLockSqlTest.java new file mode 100644 index 00000000000..2b5cebf8255 --- /dev/null +++ b/core/src/test/java/org/apache/seata/core/store/db/sql/distributed/lock/BaseDistributedLockSqlTest.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.seata.core.store.db.sql.distributed.lock; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * the base distributed lock sql test. + * @author cx + */ +public class BaseDistributedLockSqlTest { + + private static final String GLOBAL_TABLE = "global_table"; + + private static final String BRANCH_TABLE = "branch_table"; + + private static final String EXPECT_INSERT_GLOBAL_SQL = "INSERT INTO " + GLOBAL_TABLE + "(lock_key,lock_value,expire) VALUES (?, ?, ?)"; + + private static final String EXPECT_INSERT_BRANCH_SQL = "INSERT INTO " + BRANCH_TABLE + "(lock_key,lock_value,expire) VALUES (?, ?, ?)"; + + private static final String EXPECT_UPDATE_GLOBAL_SQL = "UPDATE " + GLOBAL_TABLE + " SET lock_value=?, expire=? WHERE lock_key=?"; + + private static final String EXPECT_UPDATE_BRANCH_SQL = "UPDATE " + BRANCH_TABLE + " SET lock_value=?, expire=? WHERE lock_key=?"; + + private static final String EXPECT_SELECT_FOR_UPDATE_GLOBAL_SQL = "SELECT lock_key,lock_value,expire FROM " + GLOBAL_TABLE + " WHERE lock_key = ? FOR UPDATE"; + + private static final String EXPECT_SELECT_FOR_UPDATE_BRANCH_SQL = "SELECT lock_key,lock_value,expire FROM " + BRANCH_TABLE + " WHERE lock_key = ? FOR UPDATE"; + + private static final DistributedLockSql DISTRIBUTEDLOCKSQL = DistributedLockSqlFactory.getDistributedLogStoreSql(" "); + + @Test + public void testGetInsertSql() { + String sql; + sql = DISTRIBUTEDLOCKSQL.getInsertSql(GLOBAL_TABLE); + Assertions.assertEquals(EXPECT_INSERT_GLOBAL_SQL, sql); + sql = DISTRIBUTEDLOCKSQL.getInsertSql(BRANCH_TABLE); + Assertions.assertEquals(EXPECT_INSERT_BRANCH_SQL, sql); + } + + @Test + public void testGetUpdateSql() { + String sql; + sql = DISTRIBUTEDLOCKSQL.getUpdateSql(GLOBAL_TABLE); + Assertions.assertEquals(EXPECT_UPDATE_GLOBAL_SQL, sql); + sql = DISTRIBUTEDLOCKSQL.getUpdateSql(BRANCH_TABLE); + Assertions.assertEquals(EXPECT_UPDATE_BRANCH_SQL, sql); + } + + @Test + public void testGetSelectDistributeForUpdateSql() { + String sql; + sql = DISTRIBUTEDLOCKSQL.getSelectDistributeForUpdateSql(GLOBAL_TABLE); + Assertions.assertEquals(EXPECT_SELECT_FOR_UPDATE_GLOBAL_SQL, sql); + sql = DISTRIBUTEDLOCKSQL.getSelectDistributeForUpdateSql(BRANCH_TABLE); + Assertions.assertEquals(EXPECT_SELECT_FOR_UPDATE_BRANCH_SQL, sql); + } + +} diff --git a/core/src/test/java/org/apache/seata/core/store/db/sql/distributed/lock/DistributedLockSqlFactoryTest.java b/core/src/test/java/org/apache/seata/core/store/db/sql/distributed/lock/DistributedLockSqlFactoryTest.java new file mode 100644 index 00000000000..b289aeb0861 --- /dev/null +++ b/core/src/test/java/org/apache/seata/core/store/db/sql/distributed/lock/DistributedLockSqlFactoryTest.java @@ -0,0 +1,38 @@ +/* + * 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.core.store.db.sql.distributed.lock; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * the distributed lock sqlFactory test. + * @author cx + */ +public class DistributedLockSqlFactoryTest { + + @Test + public void testGetDistributedLogStoreSql() { + DistributedLockSql sqlServerDistributedLockSql = DistributedLockSqlFactory.getDistributedLogStoreSql("sqlServer"); + Assertions.assertInstanceOf(BaseDistributedLockSqlServer.class, sqlServerDistributedLockSql); + DistributedLockSql defaultDistributedLockSql = DistributedLockSqlFactory.getDistributedLogStoreSql(" "); + Assertions.assertInstanceOf(BaseDistributedLockSql.class, defaultDistributedLockSql); + Assertions.assertThrows(NullPointerException.class, () -> DistributedLockSqlFactory.getDistributedLogStoreSql(null)); + Assertions.assertThrows(IllegalArgumentException.class, () -> DistributedLockSqlFactory.getDistributedLogStoreSql("")); + } + +} diff --git a/core/src/test/java/org/apache/seata/core/store/db/sql/lock/LockStoreSqlFactoryTest.java b/core/src/test/java/org/apache/seata/core/store/db/sql/lock/LockStoreSqlFactoryTest.java index 4a598bbe52a..3ed624d0bc4 100644 --- a/core/src/test/java/org/apache/seata/core/store/db/sql/lock/LockStoreSqlFactoryTest.java +++ b/core/src/test/java/org/apache/seata/core/store/db/sql/lock/LockStoreSqlFactoryTest.java @@ -40,6 +40,8 @@ public class LockStoreSqlFactoryTest { private static LockStoreSql DM_LOCK_STORE = LockStoreSqlFactory.getLogStoreSql("dm"); + private static LockStoreSql SQLSERVER_LOCK_STORE = LockStoreSqlFactory.getLogStoreSql("sqlserver"); + private static String GLOBAL_TABLE = "global_table"; private static String BRANCH_TABLE = "branch_table"; @@ -56,6 +58,10 @@ public class LockStoreSqlFactoryTest { private static String EXPECT_BATCH_BRANCH_DELETE_LOCK_BY_BRANCHS_SQL = "delete from " + BRANCH_TABLE + " where xid = ? "; + private static String EXPECT_GET_ALL_LOCK_SQL_WHERE_CONDITION = " where xid = ? "; + private static String EXPECT_GET_GLOBAL_ALL_LOCK_SQL = "select xid, transaction_id, branch_id, resource_id, table_name, pk, row_key, gmt_create, gmt_modified,status from " + GLOBAL_TABLE + EXPECT_GET_ALL_LOCK_SQL_WHERE_CONDITION; + private static String EXPECT_GET_BRANCH_ALL_LOCK_SQL = "select xid, transaction_id, branch_id, resource_id, table_name, pk, row_key, gmt_create, gmt_modified,status from " + BRANCH_TABLE + EXPECT_GET_ALL_LOCK_SQL_WHERE_CONDITION; + @Test public void mysqlLockTest() { String sql; @@ -379,4 +385,56 @@ public void dmLockTest() { sql = DM_LOCK_STORE.getCheckLockableSql(BRANCH_TABLE, 3); Assertions.assertEquals(EXPECT_CHECK_BRANCH_LOCKABLE_SQL,sql); } + + @Test + public void SqlServerLockTest() { + String sql; + // Get insert lock sql string. + sql = SQLSERVER_LOCK_STORE.getInsertLockSQL(GLOBAL_TABLE); + Assertions.assertNotNull(sql); + sql = SQLSERVER_LOCK_STORE.getInsertLockSQL(BRANCH_TABLE); + Assertions.assertNotNull(sql); + + // Get delete lock sql string. + sql = SQLSERVER_LOCK_STORE.getDeleteLockSql(GLOBAL_TABLE); + Assertions.assertNotNull(sql); + sql = SQLSERVER_LOCK_STORE.getDeleteLockSql(BRANCH_TABLE); + Assertions.assertNotNull(sql); + + // Get batch delete lock sql string. + sql = SQLSERVER_LOCK_STORE.getBatchDeleteLockSql(GLOBAL_TABLE, 3); + Assertions.assertEquals(EXPECT_BATCH_GLOBAL_DELETE_LOCK_SQL,sql); + sql = SQLSERVER_LOCK_STORE.getBatchDeleteLockSql(BRANCH_TABLE, 3); + Assertions.assertEquals(EXPECT_BATCH_BRANCH_DELETE_LOCK_SQL,sql); + + // Get batch delete lock sql string. + sql = SQLSERVER_LOCK_STORE.getBatchDeleteLockSqlByBranchId(GLOBAL_TABLE); + Assertions.assertNotNull(sql); + sql = SQLSERVER_LOCK_STORE.getBatchDeleteLockSqlByBranchId(BRANCH_TABLE); + Assertions.assertNotNull(sql); + + // Get batch delete lock sql string. + sql = SQLSERVER_LOCK_STORE.getBatchDeleteLockSqlByXid(GLOBAL_TABLE); + Assertions.assertEquals(EXPECT_BATCH_GLOBAL_DELETE_LOCK_BY_BRANCHS_SQL,sql); + sql = SQLSERVER_LOCK_STORE.getBatchDeleteLockSqlByXid(BRANCH_TABLE); + Assertions.assertEquals(EXPECT_BATCH_BRANCH_DELETE_LOCK_BY_BRANCHS_SQL,sql); + + // Get query lock sql string. + sql = SQLSERVER_LOCK_STORE.getQueryLockSql(GLOBAL_TABLE); + Assertions.assertNotNull(sql); + sql = SQLSERVER_LOCK_STORE.getQueryLockSql(BRANCH_TABLE); + Assertions.assertNotNull(sql); + + // Get check lock sql string. + sql = SQLSERVER_LOCK_STORE.getCheckLockableSql(GLOBAL_TABLE, 3); + Assertions.assertEquals(EXPECT_CHECK_GLOBAL_LOCKABLE_SQL,sql); + sql = SQLSERVER_LOCK_STORE.getCheckLockableSql(BRANCH_TABLE, 3); + Assertions.assertEquals(EXPECT_CHECK_BRANCH_LOCKABLE_SQL,sql); + + // Get All lock sql string. + sql = SQLSERVER_LOCK_STORE.getAllLockSql(GLOBAL_TABLE, EXPECT_GET_ALL_LOCK_SQL_WHERE_CONDITION); + Assertions.assertEquals(EXPECT_GET_GLOBAL_ALL_LOCK_SQL,sql); + sql = SQLSERVER_LOCK_STORE.getAllLockSql(BRANCH_TABLE, EXPECT_GET_ALL_LOCK_SQL_WHERE_CONDITION); + Assertions.assertEquals(EXPECT_GET_BRANCH_ALL_LOCK_SQL,sql); + } }