From 77bfc08bc8f2636cc4bf0e0f16503bc6b3076cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E6=B8=85?= <33415199+lightClouds917@users.noreply.github.com> Date: Thu, 11 Jan 2024 17:11:50 +0800 Subject: [PATCH] optimize: increase seata-core module unit test coverage (#6250) --- changes/en-us/2.x.md | 2 +- changes/zh-cn/2.x.md | 2 +- .../core/auth/DefaultAuthSignerTest.java | 54 +++++++ .../seata/core/auth/RamSignAdapterTest.java | 88 +++++++++++ .../core/compressor/CompressorTypeTest.java | 42 +++++ .../seata/core/context/ContextCoreTest.java | 18 +++ .../seata/core/context/RootContextTest.java | 66 ++++++++ .../context/ThreadLocalContextCoreTest.java | 68 ++++++++ .../seata/core/event/ExceptionEventTest.java | 35 +++++ .../event/GlobalTransactionEventTest.java | 96 ++++++++++++ .../seata/core/lock/AbstractLockerTest.java | 147 ++++++++++++++++++ .../io/seata/core/lock/LocalDBLockerTest.java | 73 +++++++++ .../core/model/GlobalLockConfigTest.java | 49 ++++++ .../io/seata/core/model/GlobalStatusTest.java | 23 +++ .../java/io/seata/core/model/ResultTest.java | 97 ++++++++++++ 15 files changed, 858 insertions(+), 2 deletions(-) create mode 100644 core/src/test/java/io/seata/core/auth/DefaultAuthSignerTest.java create mode 100644 core/src/test/java/io/seata/core/auth/RamSignAdapterTest.java create mode 100644 core/src/test/java/io/seata/core/compressor/CompressorTypeTest.java create mode 100644 core/src/test/java/io/seata/core/context/ThreadLocalContextCoreTest.java create mode 100644 core/src/test/java/io/seata/core/event/ExceptionEventTest.java create mode 100644 core/src/test/java/io/seata/core/event/GlobalTransactionEventTest.java create mode 100644 core/src/test/java/io/seata/core/lock/AbstractLockerTest.java create mode 100644 core/src/test/java/io/seata/core/lock/LocalDBLockerTest.java create mode 100644 core/src/test/java/io/seata/core/model/GlobalLockConfigTest.java create mode 100644 core/src/test/java/io/seata/core/model/ResultTest.java diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 87220092a3b..c6a1cd2f55f 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -65,6 +65,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6081](https://github.com/apache/incubator-seata/pull/6081)] add `test-os.yml` for testing the OS - [[#6125](https://github.com/apache/incubator-seata/pull/6125)] unbind xid in TransactionTemplateTest - [[#6157](https://github.com/apache/incubator-seata/pull/6157)] increase common module unit test coverage +- [[#6250](https://github.com/apache/incubator-seata/pull/6250)] increase seata-core module unit test coverage Thanks to these contributors for their code commits. Please report an unintended omission. @@ -91,5 +92,4 @@ Thanks to these contributors for their code commits. Please report an unintended - [jsbxyyx](https://github.com/jsbxyyx) - [liuqiufeng](https://github.com/liuqiufeng) - 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 fd356729050..0730187615b 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -65,6 +65,7 @@ - [[#6081](https://github.com/apache/incubator-seata/pull/6081)] 添加 `test-os.yml` 用于测试seata在各种操作系统下的运行情况 - [[#6125](https://github.com/apache/incubator-seata/pull/6125)] TransactionTemplateTest单测unbind xid - [[#6157](https://github.com/apache/incubator-seata/pull/6157)] 增加common模块单测覆盖率 +- [[#6250](https://github.com/apache/incubator-seata/pull/6250)] 增加seata-core模块单测覆盖率 非常感谢以下 contributors 的代码贡献。若有无意遗漏,请报告。 @@ -91,5 +92,4 @@ - [jsbxyyx](https://github.com/jsbxyyx) - [liuqiufeng](https://github.com/liuqiufeng) - 同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。 diff --git a/core/src/test/java/io/seata/core/auth/DefaultAuthSignerTest.java b/core/src/test/java/io/seata/core/auth/DefaultAuthSignerTest.java new file mode 100644 index 00000000000..88f814da635 --- /dev/null +++ b/core/src/test/java/io/seata/core/auth/DefaultAuthSignerTest.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 io.seata.core.auth; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * The DefaultAuthSigner Test + */ +public class DefaultAuthSignerTest { + @Test + public void testGetRamSignNotNull() { + String data = "testGroup,127.0.0.1,1702564471650"; + String key = "exampleEncryptKey"; + String expectedSign = "6g9nMk6BRLFxl7bf5ZfWaEZvGdho3JBmwvx5rqgSUCE="; + DefaultAuthSigner signer = new DefaultAuthSigner(); + String sign = signer.sign(data, key); + Assertions.assertEquals(expectedSign, sign); + } + + @Test + public void testGetRamSignNull() { + String data = null; + String key = "exampleEncryptKey"; + DefaultAuthSigner signer = new DefaultAuthSigner(); + String sign = signer.sign(data, key); + Assertions.assertNull(sign); + } + + @Test + public void testGetSignVersion() { + DefaultAuthSigner signer = new DefaultAuthSigner(); + String expectedVersion = "V4"; + String actualVersion = signer.getSignVersion(); + + // Assert the returned version matches the expected version + Assertions.assertEquals(expectedVersion, actualVersion); + } +} diff --git a/core/src/test/java/io/seata/core/auth/RamSignAdapterTest.java b/core/src/test/java/io/seata/core/auth/RamSignAdapterTest.java new file mode 100644 index 00000000000..b859453f390 --- /dev/null +++ b/core/src/test/java/io/seata/core/auth/RamSignAdapterTest.java @@ -0,0 +1,88 @@ +/* + * 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 io.seata.core.auth; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * The RamSignAdapter Test + */ +public class RamSignAdapterTest { + @Test + public void testGetDateSigningKey() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + String secret = "mySecret"; + String date = "20220101"; + String signMethod = "HmacSHA256"; + byte[] expectArray = new byte[]{-96, 108, 42, 75, -59, 121, -63, 108, -3, -126, 67, 3, 118, 2, 39, 59, -68, -37, -98, 122, -25, -120, 77, 56, -70, 24, -115, 33, 125, -128, -10, -26}; + + RamSignAdapter adapter = new RamSignAdapter(); + // Use reflection to access the private method + Method getDateSigningKeyMethod = RamSignAdapter.class.getDeclaredMethod("getDateSigningKey", String.class, String.class, String.class); + getDateSigningKeyMethod.setAccessible(true); + byte[] signingKey = (byte[]) getDateSigningKeyMethod.invoke(adapter, secret, date, signMethod); + Assertions.assertEquals(32, signingKey.length); + Assertions.assertArrayEquals(expectArray, signingKey); + } + + @Test + public void testGetRegionSigningKey() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { + String secret = "mySecret"; + String date = "20220101"; + String region = "cn-beijing"; + String signMethod = "HmacSHA256"; + byte[] expectArray = new byte[]{-40, 5, 2, 41, -48, 82, 10, -102, 125, -24, -44, -83, 127, 6, -85, 93, -26, 88, -88, 65, 56, 79, -5, -66, 65, -106, 19, -64, -85, 103, -32, 110}; + + RamSignAdapter adapter = new RamSignAdapter(); + // Use reflection to access the private method + Method getRegionSigningKeyMethod = RamSignAdapter.class.getDeclaredMethod("getRegionSigningKey", String.class, String.class, String.class, String.class); + getRegionSigningKeyMethod.setAccessible(true); + byte[] signingKey = (byte[]) getRegionSigningKeyMethod.invoke(adapter, secret, date, region, signMethod); + Assertions.assertEquals(32, signingKey.length); + Assertions.assertArrayEquals(expectArray, signingKey); + } + + @Test + public void testGetProductSigningKey() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + String secret = "mySecret"; + String date = "20220101"; + String region = "cn-beijing"; + String productCode = "seata"; + String signMethod = "HmacSHA256"; + byte[] expectArray = new byte[]{62, 98, -65, 30, -8, -3, 66, -111, 0, 123, 126, 78, -30, -74, 55, -79, 101, -18, -97, -5, 78, -19, -17, 0, 88, 30, -92, 108, 103, 87, 49, -22}; + + RamSignAdapter adapter = new RamSignAdapter(); + Method getProductSigningKeyMethod = RamSignAdapter.class.getDeclaredMethod("getProductSigningKey", String.class, String.class, String.class, String.class, String.class); + getProductSigningKeyMethod.setAccessible(true); + byte[] signingKey = (byte[]) getProductSigningKeyMethod.invoke(adapter, secret, date, region, productCode, signMethod); + Assertions.assertEquals(32, signingKey.length); + Assertions.assertArrayEquals(expectArray, signingKey); + } + + @Test + public void testGetRamSign() { + String encryptText = "testGroup,127.0.0.1,1702564471650"; + String encryptKey = "exampleEncryptKey"; + String expectedSign = "6g9nMk6BRLFxl7bf5ZfWaEZvGdho3JBmwvx5rqgSUCE="; + String actualSign = RamSignAdapter.getRamSign(encryptText, encryptKey); + // Assert the generated sign matches the expected sign + Assertions.assertEquals(expectedSign, actualSign); + } +} diff --git a/core/src/test/java/io/seata/core/compressor/CompressorTypeTest.java b/core/src/test/java/io/seata/core/compressor/CompressorTypeTest.java new file mode 100644 index 00000000000..c99bb97c1ea --- /dev/null +++ b/core/src/test/java/io/seata/core/compressor/CompressorTypeTest.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 io.seata.core.compressor; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * The CompressorType Test + */ +public class CompressorTypeTest { + @Test + public void testGetByCode() { + int code = 1; + CompressorType expectedType = CompressorType.GZIP; + CompressorType actualType = CompressorType.getByCode(code); + // Assert the returned type matches the expected type + Assertions.assertEquals(expectedType, actualType); + } + @Test + public void testGetByName() { + String name = "gzip"; + CompressorType expectedType = CompressorType.GZIP; + CompressorType actualType = CompressorType.getByName(name); + // Assert the returned type matches the expected type + Assertions.assertEquals(expectedType, actualType); + } +} diff --git a/core/src/test/java/io/seata/core/context/ContextCoreTest.java b/core/src/test/java/io/seata/core/context/ContextCoreTest.java index 6e85f2c8e3f..29ccbe48efe 100644 --- a/core/src/test/java/io/seata/core/context/ContextCoreTest.java +++ b/core/src/test/java/io/seata/core/context/ContextCoreTest.java @@ -18,6 +18,8 @@ import org.junit.jupiter.api.Test; +import java.util.Map; + import static org.assertj.core.api.Assertions.assertThat; /** @@ -68,6 +70,22 @@ public void testGet() { load.remove(NOT_EXIST_KEY); } + /** + * Test entries. + */ + @Test + public void testEntries() { + ContextCore load = ContextCoreLoader.load(); + load.put(FIRST_KEY, FIRST_VALUE); + load.put(SECOND_KEY, FIRST_VALUE); + Map entries = load.entries(); + assertThat(entries.get(FIRST_KEY)).isEqualTo(FIRST_VALUE); + assertThat(entries.get(SECOND_KEY)).isEqualTo(FIRST_VALUE); + load.remove(FIRST_KEY); + load.remove(SECOND_KEY); + load.remove(NOT_EXIST_KEY); + } + /** * Test remove. */ diff --git a/core/src/test/java/io/seata/core/context/RootContextTest.java b/core/src/test/java/io/seata/core/context/RootContextTest.java index 1edcfe53281..0fadf94e9f1 100644 --- a/core/src/test/java/io/seata/core/context/RootContextTest.java +++ b/core/src/test/java/io/seata/core/context/RootContextTest.java @@ -21,6 +21,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.Map; + import static org.assertj.core.api.Assertions.assertThat; /** @@ -57,6 +59,70 @@ public void testGetXID() { assertThat(RootContext.getXID()).isNull(); } + /** + * Test set timeout. + */ + @Test + public void testSetTimeout() { + RootContext.setTimeout(100); + assertThat(RootContext.getTimeout()).isEqualTo(100); + RootContext.setTimeout(null); + assertThat(RootContext.getTimeout()).isEqualTo(null); + } + + /** + * Test get timeout. + */ + @Test + public void testGetTimeout() { + RootContext.setTimeout(100); + assertThat(RootContext.getTimeout()).isEqualTo(100); + RootContext.setTimeout(null); + assertThat(RootContext.getTimeout()).isEqualTo(null); + } + + /** + * Test bind global lock flag. + */ + @Test + public void testBindGlobalLockFlag() { + RootContext.bindGlobalLockFlag(); + assertThat(RootContext.requireGlobalLock()).isEqualTo(true); + } + + /** + * Test unbind global lock flag. + */ + @Test + public void testUnBindGlobalLockFlag() { + RootContext.bindGlobalLockFlag(); + assertThat(RootContext.requireGlobalLock()).isEqualTo(true); + RootContext.unbindGlobalLockFlag(); + assertThat(RootContext.requireGlobalLock()).isEqualTo(false); + } + + /** + * Test require global lock. + */ + @Test + public void testRequireGlobalLock() { + RootContext.bindGlobalLockFlag(); + assertThat(RootContext.requireGlobalLock()).isEqualTo(true); + RootContext.unbindGlobalLockFlag(); + assertThat(RootContext.requireGlobalLock()).isEqualTo(false); + } + + /** + * Test entries. + */ + @Test + public void testEntries() { + RootContext.bind(DEFAULT_XID); + Map entries = RootContext.entries(); + assertThat(entries.get(RootContext.KEY_XID)).isEqualTo(DEFAULT_XID); + RootContext.unbind(); + } + /** * Test bind and unbind branchType. */ diff --git a/core/src/test/java/io/seata/core/context/ThreadLocalContextCoreTest.java b/core/src/test/java/io/seata/core/context/ThreadLocalContextCoreTest.java new file mode 100644 index 00000000000..337bb9efe6a --- /dev/null +++ b/core/src/test/java/io/seata/core/context/ThreadLocalContextCoreTest.java @@ -0,0 +1,68 @@ +/* + * 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 io.seata.core.context; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * The type Thread local context core test. + */ +public class ThreadLocalContextCoreTest { + private static ThreadLocalContextCore contextCore ; + + + @BeforeAll + public static void setUp() { + contextCore = new ThreadLocalContextCore(); + } + @Test + public void testPutAndGet() { + // Test putting and getting a value + contextCore.put("key", "value"); + assertEquals("value", contextCore.get("key")); + contextCore.remove("key"); + } + + @Test + public void testRemove() { + // Test putting and removing a value + contextCore.put("key", "value"); + assertEquals("value", contextCore.remove("key")); + assertNull(contextCore.get("key")); + } + + @Test + public void testEntries() { + // Test getting all entries + contextCore.put("key1", "value1"); + contextCore.put("key2", "value2"); + contextCore.put("key3", "value3"); + assertEquals(3, contextCore.entries().size()); + assertTrue(contextCore.entries().containsKey("key1")); + assertTrue(contextCore.entries().containsKey("key2")); + assertTrue(contextCore.entries().containsKey("key3")); + contextCore.remove("key1"); + contextCore.remove("key2"); + contextCore.remove("key3"); + assertNull(contextCore.get("key1")); + assertNull(contextCore.get("key2")); + assertNull(contextCore.get("key3")); + } +} diff --git a/core/src/test/java/io/seata/core/event/ExceptionEventTest.java b/core/src/test/java/io/seata/core/event/ExceptionEventTest.java new file mode 100644 index 00000000000..8f286dfa59d --- /dev/null +++ b/core/src/test/java/io/seata/core/event/ExceptionEventTest.java @@ -0,0 +1,35 @@ +/* + * 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 io.seata.core.event; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * The ExceptionEvent Test + */ +public class ExceptionEventTest { + @Test + public void testGetName() { + // Create an ExceptionEvent with a code + ExceptionEvent exceptionEvent = new ExceptionEvent("CODE123"); + + // Test the getName method + assertEquals("CODE123", exceptionEvent.getName()); + } +} diff --git a/core/src/test/java/io/seata/core/event/GlobalTransactionEventTest.java b/core/src/test/java/io/seata/core/event/GlobalTransactionEventTest.java new file mode 100644 index 00000000000..aa93a54c518 --- /dev/null +++ b/core/src/test/java/io/seata/core/event/GlobalTransactionEventTest.java @@ -0,0 +1,96 @@ +/* + * 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 io.seata.core.event; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * The GlobalTransactionEvent Test + */ +public class GlobalTransactionEventTest { + + private static GlobalTransactionEvent event ; + + @BeforeAll + public static void setUp() { + event = new GlobalTransactionEvent(123456789L, "tc", "EventName", "AppID", "Group1", 123456789L, 1234567890L, "committed", true,false); + } + @Test + public void testGetId() { + // Test the getId method + assertEquals(123456789L, event.getId()); + } + + @Test + public void testGetRole() { + // Test the getRole method + assertEquals("tc", event.getRole()); + } + + @Test + public void testGetName() { + // Test the getName method + assertEquals("EventName", event.getName()); + } + + @Test + public void testGetApplicationId() { + // Test the getApplicationId method + assertEquals("AppID", event.getApplicationId()); + } + + @Test + public void testGetGroup() { + // Test the getGroup method + assertEquals("Group1", event.getGroup()); + } + + @Test + public void testGetBeginTime() { + // Test the getBeginTime method + assertEquals(123456789L, event.getBeginTime().longValue()); + } + + @Test + public void testGetEndTime() { + // Test the getEndTime method + assertEquals(1234567890L, event.getEndTime().longValue()); + } + + @Test + public void testGetStatus() { + // Test the getStatus method + assertEquals("committed", event.getStatus()); + } + + @Test + public void testIsRetryGlobal() { + // Test the isSuccess method + assertTrue(event.isRetryGlobal()); + } + + + @Test + public void testIsRetryBranch() { + // Test the isSuccess method + assertFalse(event.isRetryBranch()); + } + +} diff --git a/core/src/test/java/io/seata/core/lock/AbstractLockerTest.java b/core/src/test/java/io/seata/core/lock/AbstractLockerTest.java new file mode 100644 index 00000000000..79a8950d015 --- /dev/null +++ b/core/src/test/java/io/seata/core/lock/AbstractLockerTest.java @@ -0,0 +1,147 @@ +/* + * 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 io.seata.core.lock; + +import io.seata.core.model.LockStatus; +import io.seata.core.store.LockDO; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * The AbstractLocker Test + */ +public class AbstractLockerTest { + + @Test + public void testConvertToLockDO() { + List lockDOs = getLockDOS(); + + // Assert that the converted LockDO objects have the correct values + Assertions.assertEquals(2, lockDOs.size()); + + LockDO lockDO1 = lockDOs.get(0); + Assertions.assertEquals(1, lockDO1.getBranchId()); + Assertions.assertEquals("123", lockDO1.getPk()); + Assertions.assertEquals("resource1", lockDO1.getResourceId()); + Assertions.assertEquals("xid1", lockDO1.getXid()); + Assertions.assertEquals(1122L, lockDO1.getTransactionId()); + Assertions.assertEquals("table1", lockDO1.getTableName()); + + LockDO lockDO2 = lockDOs.get(1); + Assertions.assertEquals(2, lockDO2.getBranchId()); + Assertions.assertEquals("456", lockDO2.getPk()); + Assertions.assertEquals("resource2", lockDO2.getResourceId()); + Assertions.assertEquals("xid2", lockDO2.getXid()); + Assertions.assertEquals(3344L, lockDO2.getTransactionId()); + Assertions.assertEquals("table2", lockDO2.getTableName()); + } + + private static List getLockDOS() { + AbstractLocker locker = new AbstractLocker() { + @Override + public boolean acquireLock(List rowLock) { + return false; + } + + @Override + public boolean acquireLock(List rowLock, boolean autoCommit, boolean skipCheckLock) { + return false; + } + + @Override + public boolean releaseLock(List rowLock) { + return false; + } + + @Override + public boolean isLockable(List rowLock) { + return false; + } + + @Override + public void updateLockStatus(String xid, LockStatus lockStatus) { + + } + }; + List locks = getRowLocks(); + + // Call the convertToLockDO method + return locker.convertToLockDO(locks); + } + + + @Test + public void testGetRowKey() { + AbstractLocker locker = new AbstractLocker() { + @Override + public boolean acquireLock(List rowLock) { + return false; + } + + @Override + public boolean acquireLock(List rowLock, boolean autoCommit, boolean skipCheckLock) { + return false; + } + + @Override + public boolean releaseLock(List rowLock) { + return false; + } + + @Override + public boolean isLockable(List rowLock) { + return false; + } + + @Override + public void updateLockStatus(String xid, LockStatus lockStatus) { + + } + }; + + // Call the getRowKey method + String rowKey = locker.getRowKey("resource1", "table1", "123"); + + // Assert that the row key is constructed correctly + Assertions.assertEquals("resource1^^^table1^^^123", rowKey); + } + + private static List getRowLocks() { + List locks = new ArrayList<>(); + RowLock rowLock1 = new RowLock(); + rowLock1.setBranchId(1L); + rowLock1.setPk("123"); + rowLock1.setResourceId("resource1"); + rowLock1.setXid("xid1"); + rowLock1.setTransactionId(1122L); + rowLock1.setTableName("table1"); + locks.add(rowLock1); + + RowLock rowLock2 = new RowLock(); + rowLock2.setBranchId(2L); + rowLock2.setPk("456"); + rowLock2.setResourceId("resource2"); + rowLock2.setXid("xid2"); + rowLock2.setTransactionId(3344L); + rowLock2.setTableName("table2"); + locks.add(rowLock2); + return locks; + } +} diff --git a/core/src/test/java/io/seata/core/lock/LocalDBLockerTest.java b/core/src/test/java/io/seata/core/lock/LocalDBLockerTest.java new file mode 100644 index 00000000000..a3a990bc537 --- /dev/null +++ b/core/src/test/java/io/seata/core/lock/LocalDBLockerTest.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 io.seata.core.lock; + +import io.seata.core.model.LockStatus; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * The LocalDBLocker Test + */ +public class LocalDBLockerTest { + @Test + public void testAcquireLock() { + LocalDBLocker locker = new LocalDBLocker(); + List rowLocks = new ArrayList<>(); + boolean result = locker.acquireLock(rowLocks); + // Assert the result of the acquireLock method + Assertions.assertFalse(result); + } + + @Test + public void testAcquireLockWithAutoCommitAndSkipCheckLock() { + LocalDBLocker locker = new LocalDBLocker(); + List rowLocks = new ArrayList<>(); + boolean result = locker.acquireLock(rowLocks, true, true); + // Assert the result of the acquireLock method with autoCommit and skipCheckLock parameters + Assertions.assertFalse(result); + } + + @Test + public void testReleaseLock() { + LocalDBLocker locker = new LocalDBLocker(); + List rowLocks = new ArrayList<>(); + boolean result = locker.releaseLock(rowLocks); + // Assert the result of the releaseLock method + Assertions.assertFalse(result); + } + + @Test + public void testIsLockable() { + LocalDBLocker locker = new LocalDBLocker(); + List rowLocks = new ArrayList<>(); + boolean result = locker.isLockable(rowLocks); + // Assert the result of the isLockable method + Assertions.assertFalse(result); + } + + @Test + public void testUpdateLockStatus() { + LocalDBLocker locker = new LocalDBLocker(); + String xid = "xid"; + LockStatus lockStatus = LockStatus.Locked; + locker.updateLockStatus(xid, lockStatus); + } +} diff --git a/core/src/test/java/io/seata/core/model/GlobalLockConfigTest.java b/core/src/test/java/io/seata/core/model/GlobalLockConfigTest.java new file mode 100644 index 00000000000..1ea5905a5e6 --- /dev/null +++ b/core/src/test/java/io/seata/core/model/GlobalLockConfigTest.java @@ -0,0 +1,49 @@ +/* + * 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 io.seata.core.model; + +import io.seata.common.LockStrategyMode; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * The GlobalLockConfig Test + */ +public class GlobalLockConfigTest { + + @Test + public void testGetLockTimeout() { + GlobalLockConfig config = new GlobalLockConfig(); + config.setLockRetryTimes(5000); + assertEquals(5000, config.getLockRetryTimes()); + } + + @Test + public void testGetLockRetryInterval() { + GlobalLockConfig config = new GlobalLockConfig(); + config.setLockRetryInterval(1000); + assertEquals(1000, config.getLockRetryInterval()); + } + + @Test + public void testIsLockEnabled() { + GlobalLockConfig config = new GlobalLockConfig(); + config.setLockStrategyMode(LockStrategyMode.OPTIMISTIC); + assertEquals(LockStrategyMode.OPTIMISTIC, config.getLockStrategyMode()); + } +} diff --git a/core/src/test/java/io/seata/core/model/GlobalStatusTest.java b/core/src/test/java/io/seata/core/model/GlobalStatusTest.java index 92999abae36..69f4c0bc239 100644 --- a/core/src/test/java/io/seata/core/model/GlobalStatusTest.java +++ b/core/src/test/java/io/seata/core/model/GlobalStatusTest.java @@ -63,4 +63,27 @@ public void testGetByCode() { Assertions.assertThrows(IllegalArgumentException.class, () -> GlobalStatus.get(NONE)); } + @Test + public void testIsOnePhaseTimeout() { + Assertions.assertFalse(GlobalStatus.isOnePhaseTimeout(GlobalStatus.Begin)); + Assertions.assertFalse(GlobalStatus.isOnePhaseTimeout(GlobalStatus.Rollbacking)); + Assertions.assertTrue(GlobalStatus.isOnePhaseTimeout(GlobalStatus.TimeoutRollbacking)); + Assertions.assertTrue(GlobalStatus.isOnePhaseTimeout(GlobalStatus.TimeoutRollbackRetrying)); + Assertions.assertTrue(GlobalStatus.isOnePhaseTimeout(GlobalStatus.TimeoutRollbacked)); + Assertions.assertTrue(GlobalStatus.isOnePhaseTimeout(GlobalStatus.TimeoutRollbackFailed)); + } + + @Test + public void testIsTwoPhaseSuccess() { + Assertions.assertTrue(GlobalStatus.isTwoPhaseSuccess(GlobalStatus.Committed)); + Assertions.assertTrue(GlobalStatus.isTwoPhaseSuccess(GlobalStatus.Rollbacked)); + Assertions.assertTrue(GlobalStatus.isTwoPhaseSuccess(GlobalStatus.TimeoutRollbacked)); + Assertions.assertFalse(GlobalStatus.isTwoPhaseSuccess(GlobalStatus.Begin)); + } + + @Test + public void testIsTwoPhaseHeuristic() { + Assertions.assertTrue(GlobalStatus.isTwoPhaseHeuristic(GlobalStatus.Finished)); + Assertions.assertFalse(GlobalStatus.isTwoPhaseHeuristic(GlobalStatus.Begin)); + } } diff --git a/core/src/test/java/io/seata/core/model/ResultTest.java b/core/src/test/java/io/seata/core/model/ResultTest.java new file mode 100644 index 00000000000..bd5dca20ebe --- /dev/null +++ b/core/src/test/java/io/seata/core/model/ResultTest.java @@ -0,0 +1,97 @@ +/* + * 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 io.seata.core.model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * The Result Test + */ +public class ResultTest { + @Test + public void testGetResult() { + // Create a Result with a result value + Result result = new Result<>("Success", null, null); + + // Test the getResult method + assertEquals("Success", result.getResult()); + } + + @Test + public void testGetErrMsg() { + // Create a Result with an error message + Result result = new Result<>(null, "Error", null); + + // Test the getErrMsg method + assertEquals("Error", result.getErrMsg()); + } + + @Test + public void testGetErrMsgParams() { + // Create a Result with error message parameters + Result result = new Result<>(null, null, new Object[]{"param1", "param2"}); + + // Test the getErrMsgParams method + assertArrayEquals(new Object[]{"param1", "param2"}, result.getErrMsgParams()); + } + + @Test + public void testOk() { + // Test the ok method + Result result = Result.ok(); + + // Verify that the result is true + assertTrue(result.getResult()); + assertNull(result.getErrMsg()); + assertNull(result.getErrMsgParams()); + } + + @Test + public void testBuild() { + // Create a Result with a result value + Result result = Result.build(100); + + // Test the getResult method + assertEquals(100, result.getResult().intValue()); + assertNull(result.getErrMsg()); + assertNull(result.getErrMsgParams()); + } + + @Test + public void testBuildWithErrMsg() { + // Create a Result with a result value and an error message + Result result = Result.build(3.14, "Invalid value"); + + // Test the getResult and getErrMsg methods + assertEquals(3.14, result.getResult(), 0.001); + assertEquals("Invalid value", result.getErrMsg()); + assertNull(result.getErrMsgParams()); + } + + @Test + public void testBuildWithParams() { + // Create a Result with a result value, an error message, and error message parameters + Result result = Result.buildWithParams("Hello", "Invalid {0} value: {1}", "parameter", 42); + + // Test the getResult, getErrMsg, and getErrMsgParams methods + assertEquals("Hello", result.getResult()); + assertEquals("Invalid {0} value: {1}", result.getErrMsg()); + assertArrayEquals(new Object[]{"parameter", 42}, result.getErrMsgParams()); + } +}