From 18e3edc446e1b5b629f207fb5580ff8934632085 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Sun, 5 May 2024 14:18:40 +0800 Subject: [PATCH 01/18] fix(6509): improve the test case coverage of [saga] module to 70% --- .../spel/SpringELExpressionFactoryTest.java | 30 +++++ .../spel/SpringELExpressionObject.java | 16 +++ .../spel/SpringELExpressionTest.java | 45 +++++++ .../DefaultSagaTransactionalTemplateTest.java | 86 +++++++++++++ .../saga/engine/tm/MockGlobalTransaction.java | 121 ++++++++++++++++++ 5 files changed, 298 insertions(+) create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java new file mode 100644 index 00000000000..67d42b47812 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java @@ -0,0 +1,30 @@ +/* + * 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.saga.engine.expression.spel; + +import org.junit.jupiter.api.Test; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class SpringELExpressionFactoryTest { + @Test + public void testCreateExpression() { + SpringELExpressionFactory factory = new SpringELExpressionFactory(null); + factory.createExpression("'Hello World'.concat('!')"); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java new file mode 100644 index 00000000000..31d38a480a9 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java @@ -0,0 +1,16 @@ +package org.apache.seata.saga.engine.expression.spel; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class SpringELExpressionObject { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java new file mode 100644 index 00000000000..a76194cd035 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java @@ -0,0 +1,45 @@ +package org.apache.seata.saga.engine.expression.spel; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.expression.EvaluationContext; +import org.springframework.expression.Expression; +import org.springframework.expression.spel.standard.SpelExpression; +import org.springframework.expression.ExpressionParser; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.expression.spel.support.StandardEvaluationContext; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class SpringELExpressionTest { + @Test + public void testGetValue() { + ExpressionParser parser = new SpelExpressionParser(); + Expression defaultExpression = parser.parseExpression("'Hello World'.concat('!')"); + String value = (String) new SpringELExpression(defaultExpression).getValue(null); + Assertions.assertEquals(value, "Hello World!"); + } + + @Test + void testSetValue() { + ExpressionParser parser = new SpelExpressionParser(); + String expression = "name"; + SpringELExpressionObject springELExpressionObject = new SpringELExpressionObject(); + Expression defaultExpression = parser.parseExpression(expression); + SpringELExpression springELExpression = new SpringELExpression(defaultExpression); + springELExpression.setValue("test", springELExpressionObject); + Assertions.assertEquals(springELExpressionObject.getName(), "test"); + } + + @Test + void testGetExpressionString() { + ExpressionParser parser = new SpelExpressionParser(); + Expression defaultExpression = parser.parseExpression("'Hello World'.concat('!')"); + SpringELExpression springELExpression = new SpringELExpression(defaultExpression); + Assertions.assertEquals(springELExpression.getExpressionString(), "'Hello World'.concat('!')"); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java new file mode 100644 index 00000000000..fe9faefdc15 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java @@ -0,0 +1,86 @@ +package org.apache.seata.saga.engine.tm; + +import org.apache.seata.core.model.BranchStatus; +import org.apache.seata.core.model.BranchType; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.core.model.Resource; +import org.apache.seata.core.model.ResourceManager; +import org.apache.seata.rm.DefaultResourceManager; +import org.apache.seata.tm.api.GlobalTransactionContext; +import org.apache.seata.tm.api.transaction.TransactionInfo; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.mockito.ArgumentMatchers.any; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class DefaultSagaTransactionalTemplateTest { + private static SagaTransactionalTemplate sagaTransactionalTemplate; + + @BeforeEach + public void init() { + sagaTransactionalTemplate = new DefaultSagaTransactionalTemplate(); + } + + @Test + public void testCommitTransaction() { + MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.commitTransaction(mockGlobalTransaction)); + } + + @Test + public void testRollbackTransaction() { + MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.rollbackTransaction(mockGlobalTransaction, null)); + } + + @Test + public void testBeginTransaction() { + TransactionInfo transactionInfo = new TransactionInfo(); + Assertions.assertThrows(NoClassDefFoundError.class, + () -> sagaTransactionalTemplate.beginTransaction(transactionInfo)); + } + + @Test + public void testReloadTransaction() { + Assertions.assertThrows(NoClassDefFoundError.class, + () -> sagaTransactionalTemplate.reloadTransaction("")); + } + + @Test + public void testReportTransaction() { + MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); + GlobalStatus globalStatus = GlobalStatus.Committed; + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.reportTransaction(mockGlobalTransaction, globalStatus)); + } + + @Test + public void testBranchRegister() { + ResourceManager resourceManager = Mockito.mock(ResourceManager.class); + Mockito.doNothing().when(resourceManager).registerResource(any(Resource.class)); + DefaultResourceManager.get(); + DefaultResourceManager.mockResourceManager(BranchType.SAGA, resourceManager); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.branchRegister("", + "", "", "", "")); + } + + @Test + public void testBranchReport() { + ResourceManager resourceManager = Mockito.mock(ResourceManager.class); + Mockito.doNothing().when(resourceManager).registerResource(any(Resource.class)); + DefaultResourceManager.get(); + DefaultResourceManager.mockResourceManager(BranchType.SAGA, resourceManager); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.branchReport("", + 0, BranchStatus.Unknown, "")); + } + + @Test + public void testTriggerAfterCompletion() { + MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.triggerAfterCompletion(mockGlobalTransaction)); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java new file mode 100644 index 00000000000..be9c68235c9 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java @@ -0,0 +1,121 @@ +/* + * 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.saga.engine.tm; + +import org.apache.seata.core.context.RootContext; +import org.apache.seata.core.exception.TransactionException; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.saga.engine.sequence.UUIDSeqGenerator; +import org.apache.seata.tm.api.GlobalTransaction; +import org.apache.seata.tm.api.GlobalTransactionRole; +import org.apache.seata.tm.api.transaction.SuspendedResourcesHolder; + + +public class MockGlobalTransaction implements GlobalTransaction { + + private String xid; + private GlobalStatus status; + private long createTime; + + private static UUIDSeqGenerator uuidSeqGenerator = new UUIDSeqGenerator(); + + public MockGlobalTransaction() {} + + public MockGlobalTransaction(String xid) { + this.xid = xid; + } + + public MockGlobalTransaction(String xid, GlobalStatus status) { + this.xid = xid; + this.status = status; + } + + @Override + public void begin() throws TransactionException { + begin(60000); + } + + @Override + public void begin(int timeout) throws TransactionException { + this.createTime = System.currentTimeMillis(); + status = GlobalStatus.Begin; + xid = uuidSeqGenerator.generate(null); + RootContext.bind(xid); + } + + @Override + public void begin(int timeout, String name) throws TransactionException { + + } + + @Override + public void commit() throws TransactionException { + + } + + @Override + public void rollback() throws TransactionException { + + } + + @Override + public SuspendedResourcesHolder suspend() throws TransactionException { + return null; + } + + @Override + public SuspendedResourcesHolder suspend(boolean clean) + throws TransactionException { + return null; + } + + @Override + public void resume(SuspendedResourcesHolder suspendedResourcesHolder) + throws TransactionException { + + } + + @Override + public GlobalStatus getStatus() throws TransactionException { + return status; + } + + @Override + public String getXid() { + return xid; + } + + @Override + public void globalReport(GlobalStatus globalStatus) throws TransactionException { + + } + + @Override + public GlobalStatus getLocalStatus() { + return status; + } + + @Override + public GlobalTransactionRole getGlobalTransactionRole() { + return null; + } + + @Override + public long getCreateTime() { + return createTime; + } +} From f1c3a32d5f894aa8699fddb7f3206176f7835460 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Sun, 5 May 2024 14:32:15 +0800 Subject: [PATCH 02/18] fix(6509): add test --- .../spel/SpringELExpressionObject.java | 16 ++++++++++++++ .../spel/SpringELExpressionTest.java | 22 ++++++++++++++----- .../DefaultSagaTransactionalTemplateTest.java | 1 - 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java index 31d38a480a9..68f4fcdbf8f 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java @@ -1,3 +1,19 @@ +/* + * 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.saga.engine.expression.spel; /** diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java index a76194cd035..8c6db198e6c 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java @@ -1,16 +1,26 @@ +/* + * 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.saga.engine.expression.spel; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; -import org.springframework.expression.spel.standard.SpelExpression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.expression.spel.support.StandardEvaluationContext; /** * @author jingliu_xiong@foxmail.com diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java index fe9faefdc15..ee7e25baba8 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java @@ -6,7 +6,6 @@ import org.apache.seata.core.model.Resource; import org.apache.seata.core.model.ResourceManager; import org.apache.seata.rm.DefaultResourceManager; -import org.apache.seata.tm.api.GlobalTransactionContext; import org.apache.seata.tm.api.transaction.TransactionInfo; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; From 6842ca8f31f111fd80e4dd0f80f858a34fe04192 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Sun, 5 May 2024 15:08:02 +0800 Subject: [PATCH 03/18] fix(6509): add test --- .github/workflows/codecov.yml | 42 +++++++++++++++++++ .../DefaultSagaTransactionalTemplateTest.java | 16 +++++++ 2 files changed, 58 insertions(+) create mode 100644 .github/workflows/codecov.yml diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml new file mode 100644 index 00000000000..09df23ec461 --- /dev/null +++ b/.github/workflows/codecov.yml @@ -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. +# + +codecov: + require_ci_to_pass: yes +coverage: + status: + patch: no + project: + default: + threshold: 1% + if_not_found: success + changes: no + precision: 2 + range: "50...100" +ignore: + - "test/.*" + - ".github/.*" + - ".mvn/.*" + - ".style/.*" + - "*.md" + - "rm-datasource/src/test/java/org/apache/seata/rm/datasource/mock" + - "sqlparser/seata-sqlparser-antlr/src/main/java/org/apache/seata/sqlparser/antlr/mysql/antlr/.*" + - "sqlparser/seata-sqlparser-antlr/src/main/java/org/apache/seata/sqlparser/antlr/mysql/parser/.*" +comment: + layout: "reach,diff,flags,tree" + behavior: default + require_changes: no diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java index ee7e25baba8..2fa23aa28c2 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java @@ -1,3 +1,19 @@ +/* + * 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.saga.engine.tm; import org.apache.seata.core.model.BranchStatus; From cbabf5a1bba70dddb1c056ffa2c0eace60c2e582 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Mon, 6 May 2024 11:34:07 +0800 Subject: [PATCH 04/18] fix(6509): add test --- .github/workflows/codecov.yml | 2 ++ .../engine/tm/DefaultSagaTransactionalTemplateTest.java | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 09df23ec461..f88a383720c 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +on: + push codecov: require_ci_to_pass: yes diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java index 2fa23aa28c2..917b4374376 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.saga.engine.tm; +import org.apache.seata.common.exception.FrameworkException; import org.apache.seata.core.model.BranchStatus; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.model.GlobalStatus; @@ -56,14 +57,13 @@ public void testRollbackTransaction() { @Test public void testBeginTransaction() { TransactionInfo transactionInfo = new TransactionInfo(); - Assertions.assertThrows(NoClassDefFoundError.class, + Assertions.assertThrows(FrameworkException.class, () -> sagaTransactionalTemplate.beginTransaction(transactionInfo)); } @Test public void testReloadTransaction() { - Assertions.assertThrows(NoClassDefFoundError.class, - () -> sagaTransactionalTemplate.reloadTransaction("")); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.reloadTransaction("")); } @Test From 334921c4b85d29b3254d8dadbcb0d17424cd43c8 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Mon, 6 May 2024 11:36:12 +0800 Subject: [PATCH 05/18] fix(6509): add test --- .github/workflows/codecov.yml | 44 ----------------------------------- 1 file changed, 44 deletions(-) delete mode 100644 .github/workflows/codecov.yml diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml deleted file mode 100644 index f88a383720c..00000000000 --- a/.github/workflows/codecov.yml +++ /dev/null @@ -1,44 +0,0 @@ -# -# 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. -# -on: - push - -codecov: - require_ci_to_pass: yes -coverage: - status: - patch: no - project: - default: - threshold: 1% - if_not_found: success - changes: no - precision: 2 - range: "50...100" -ignore: - - "test/.*" - - ".github/.*" - - ".mvn/.*" - - ".style/.*" - - "*.md" - - "rm-datasource/src/test/java/org/apache/seata/rm/datasource/mock" - - "sqlparser/seata-sqlparser-antlr/src/main/java/org/apache/seata/sqlparser/antlr/mysql/antlr/.*" - - "sqlparser/seata-sqlparser-antlr/src/main/java/org/apache/seata/sqlparser/antlr/mysql/parser/.*" -comment: - layout: "reach,diff,flags,tree" - behavior: default - require_changes: no From 2a609ce19e91cec2f25203852a946bcb72c235b5 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Mon, 6 May 2024 23:03:17 +0800 Subject: [PATCH 06/18] fix(6509): add test --- .../saga/proctrl/ProcessControllerTests.java | 12 +- .../handler/DefaultRouterHandlerTest.java | 56 ++++++++ .../proctrl/impl/ProcessContextImplTest.java | 123 ++++++++++++++++++ .../saga/proctrl/mock/MockProcessRouter.java | 3 + .../impl/CustomizeBusinessProcessorTest.java | 58 +++++++++ .../src/test/resources/file.conf | 78 +++++++++++ .../src/test/resources/registry.conf | 90 +++++++++++++ 7 files changed, 414 insertions(+), 6 deletions(-) create mode 100644 saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java create mode 100644 saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java create mode 100644 saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java create mode 100644 saga/seata-saga-spring/src/test/resources/file.conf create mode 100644 saga/seata-saga-spring/src/test/resources/registry.conf diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java index 95a6e76535d..200a856f59c 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java @@ -16,12 +16,6 @@ */ package org.apache.seata.saga.proctrl; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - import org.apache.seata.saga.proctrl.eventing.impl.AsyncEventBus; import org.apache.seata.saga.proctrl.eventing.impl.DirectEventBus; import org.apache.seata.saga.proctrl.eventing.impl.ProcessCtrlEventConsumer; @@ -38,6 +32,12 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + /** * ProcessController Tests * diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java new file mode 100644 index 00000000000..0ab1a2d250a --- /dev/null +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java @@ -0,0 +1,56 @@ +/* + * + * * 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.saga.proctrl.handler; + +import org.apache.seata.common.exception.FrameworkException; +import org.apache.seata.saga.proctrl.ProcessRouter; +import org.apache.seata.saga.proctrl.ProcessType; +import org.apache.seata.saga.proctrl.impl.ProcessContextImpl; +import org.apache.seata.saga.proctrl.mock.MockProcessRouter; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + + +/** + * @author jingliu_xiong@foxmail.com + */ +public class DefaultRouterHandlerTest { + + @Test + public void testRouteOfFrameworkException() { + ProcessContextImpl context = new ProcessContextImpl(); + DefaultRouterHandler defaultRouterHandler = new DefaultRouterHandler(); + Assertions.assertThrows(FrameworkException.class, () -> defaultRouterHandler.route(context)); + } + + @Test + public void testRouteOfException() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable("exception", new Object()); + DefaultRouterHandler defaultRouterHandler = new DefaultRouterHandler(); + Map processRouters = new HashMap<>(); + ProcessRouter processRouter = new MockProcessRouter(); + processRouters.put(ProcessType.STATE_LANG.getCode(), processRouter); + defaultRouterHandler.setProcessRouters(processRouters); + Assertions.assertThrows(RuntimeException.class, () -> defaultRouterHandler.route(context)); + } +} \ No newline at end of file diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java new file mode 100644 index 00000000000..ec160494af7 --- /dev/null +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java @@ -0,0 +1,123 @@ +/* + * + * * 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.saga.proctrl.impl; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class ProcessContextImplTest { + + @Test + public void testGetVariableFromParent() { + ProcessContextImpl context = new ProcessContextImpl(); + ProcessContextImpl parentContext = new ProcessContextImpl(); + parentContext.setVariable("key", "value"); + context.setParent(parentContext); + Assertions.assertEquals("value", context.getVariable("key")); + } + + @Test + public void testSetVariable() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable("key", "value"); + context.setVariable("key", "value1"); + Assertions.assertEquals("value1", context.getVariable("key")); + context.removeVariable("key"); + ProcessContextImpl parentContext = new ProcessContextImpl(); + parentContext.setVariable("key", "value"); + context.setParent(parentContext); + Assertions.assertEquals("value", context.getVariable("key")); + } + + @Test + public void testGetVariables() { + ProcessContextImpl context = new ProcessContextImpl(); + ProcessContextImpl parentContext = new ProcessContextImpl(); + parentContext.setVariable("key", "value"); + context.setParent(parentContext); + Assertions.assertEquals(1, context.getVariables().size()); + } + + @Test + public void testSetVariables() { + ProcessContextImpl context = new ProcessContextImpl(); + Map map = new HashMap<>(); + map.put("key", "value"); + context.setVariables(map); + Assertions.assertEquals(1, context.getVariables().size()); + } + + @Test + public void testGetVariableLocally() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable("key", "value"); + Assertions.assertEquals("value", context.getVariableLocally("key")); + } + + + @Test + public void testSetVariablesLocally() { + ProcessContextImpl context = new ProcessContextImpl(); + Map map = new HashMap<>(); + map.put("key", "value"); + context.setVariablesLocally(map); + Assertions.assertEquals("value", context.getVariableLocally("key")); + } + + @Test + public void testHasVariable() { + ProcessContextImpl context = new ProcessContextImpl(); + Assertions.assertFalse(context.hasVariable("key")); + } + + @Test + public void testRemoveVariable() { + ProcessContextImpl context = new ProcessContextImpl(); + ProcessContextImpl parentContext = new ProcessContextImpl(); + parentContext.setVariable("key", "value"); + context.setParent(parentContext); + context.setVariable("key1", "value1"); + context.removeVariable("key"); + context.removeVariable("key1"); + Assertions.assertEquals(0, context.getVariables().size()); + } + + @Test + public void testRemoveVariableLocally() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable("key", "value"); + context.removeVariableLocally("key"); + Assertions.assertEquals(0, context.getVariables().size()); + } + + @Test + public void testClearLocally() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable("key", "value"); + context.clearLocally(); + Assertions.assertEquals(0, context.getVariables().size()); + } +} \ No newline at end of file diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/mock/MockProcessRouter.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/mock/MockProcessRouter.java index 966a84dfbc0..97a2459ee79 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/mock/MockProcessRouter.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/mock/MockProcessRouter.java @@ -39,6 +39,9 @@ public Instruction route(ProcessContext context) throws FrameworkException { return null;//end process } } + if (context.hasVariable("exception")) { + throw new RuntimeException("exception"); + } return instruction; } } diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java new file mode 100644 index 00000000000..c34e72cb062 --- /dev/null +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java @@ -0,0 +1,58 @@ +/* + * + * * 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.saga.proctrl.process.impl; + +import org.apache.seata.common.exception.FrameworkException; +import org.apache.seata.saga.proctrl.ProcessContext; +import org.apache.seata.saga.proctrl.ProcessType; +import org.apache.seata.saga.proctrl.handler.ProcessHandler; +import org.apache.seata.saga.proctrl.handler.RouterHandler; +import org.apache.seata.saga.proctrl.impl.ProcessContextImpl; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class CustomizeBusinessProcessorTest { + + @Test + public void testProcessFail() { + CustomizeBusinessProcessor customizeBusinessProcessor = new CustomizeBusinessProcessor(); + ProcessContext processContext = new ProcessContextImpl(); + processContext.setVariable(ProcessContext.VAR_NAME_PROCESS_TYPE, ProcessType.STATE_LANG); + Map processHandlerMap = new HashMap<>(1); + processHandlerMap.put(ProcessType.STATE_LANG.getCode(), null); + customizeBusinessProcessor.setProcessHandlers(processHandlerMap); + Assertions.assertThrows(FrameworkException.class, () -> customizeBusinessProcessor.process(processContext)); + } + + @Test + public void testRouteFail() { + CustomizeBusinessProcessor customizeBusinessProcessor = new CustomizeBusinessProcessor(); + ProcessContext processContext = new ProcessContextImpl(); + Map routerHandlerMap = new HashMap<>(1); + routerHandlerMap.put(ProcessType.STATE_LANG.getCode(), null); + customizeBusinessProcessor.setRouterHandlers(routerHandlerMap); + Assertions.assertDoesNotThrow(() -> customizeBusinessProcessor.route(processContext)); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/resources/file.conf b/saga/seata-saga-spring/src/test/resources/file.conf new file mode 100644 index 00000000000..94863827468 --- /dev/null +++ b/saga/seata-saga-spring/src/test/resources/file.conf @@ -0,0 +1,78 @@ +# +# 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. +# + +transport { + # tcp, unix-domain-socket + type = "TCP" + #NIO, NATIVE + server = "NIO" + #enable heartbeat + heartbeat = true + # the tm client batch send request enable + enableTmClientBatchSendRequest = true + # the rm client batch send request enable + enableRmClientBatchSendRequest = true + #thread factory for netty + threadFactory { + bossThreadPrefix = "NettyBoss" + workerThreadPrefix = "NettyServerNIOWorker" + serverExecutorThread-prefix = "NettyServerBizHandler" + shareBossWorker = false + clientSelectorThreadPrefix = "NettyClientSelector" + clientSelectorThreadSize = 1 + clientWorkerThreadPrefix = "NettyClientWorkerThread" + # netty boss thread size + bossThreadSize = 1 + #auto default pin or 8 + workerThreadSize = "default" + } + shutdown { + # when destroy server, wait seconds + wait = 3 + } + serialization = "seata" + compressor = "none" + + enableRmClientChannelCheckFailFast = false + enableTmClientChannelCheckFailFast = false +} + + +service { + #transaction service group mapping + vgroupMapping.default_tx_group = "default" + vgroupMapping.mock_tx_group = "mock" + #only support when registry.type=file, please don't set multiple addresses + default.grouplist = "127.0.0.1:8091" + mock.grouplist = "127.0.0.1:8099" + #disable seata + disableGlobalTransaction = false +} + +client { + rm { + reportSuccessEnable = false + sagaBranchRegisterEnable = false + sagaJsonParser = jackson + sagaRetryPersistModeUpdate = false + sagaCompensatePersistModeUpdate = false + } + loadBalance { + type = "XID" + virtualNodes = 10 + } +} diff --git a/saga/seata-saga-spring/src/test/resources/registry.conf b/saga/seata-saga-spring/src/test/resources/registry.conf new file mode 100644 index 00000000000..bab6e8ec0ef --- /dev/null +++ b/saga/seata-saga-spring/src/test/resources/registry.conf @@ -0,0 +1,90 @@ +# +# 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. +# + +registry { + # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa + type = "file" + + nacos { + serverAddr = "localhost" + namespace = "" + cluster = "default" + } + eureka { + serviceUrl = "http://localhost:8761/eureka" + application = "default" + weight = "1" + } + redis { + serverAddr = "localhost:6379" + db = "0" + } + zk { + cluster = "default" + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + } + consul { + cluster = "default" + serverAddr = "127.0.0.1:8500" + } + etcd3 { + cluster = "default" + serverAddr = "http://localhost:2379" + } + sofa { + serverAddr = "127.0.0.1:9603" + application = "default" + region = "DEFAULT_ZONE" + datacenter = "DefaultDataCenter" + cluster = "default" + group = "SEATA_GROUP" + addressWaitTime = "3000" + } + file { + name = "file.conf" + } +} + +config { + # file、nacos 、apollo、zk、consul、etcd3 + type = "file" + + nacos { + serverAddr = "localhost" + namespace = "" + } + consul { + serverAddr = "127.0.0.1:8500" + } + apollo { + appId = "seata-server" + apolloMeta = "http://192.168.1.204:8801" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + } + etcd3 { + serverAddr = "http://localhost:2379" + } + file { + name = "file.conf" + } +} From 3aaabd8eb88d1ee0b9b92ad302a0f93b6f888126 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Tue, 7 May 2024 19:24:52 +0800 Subject: [PATCH 07/18] fix(6509): add test --- .github/workflows/build.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d57645eb419..2b7c227f46a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,6 +34,10 @@ jobs: # step 3 - name: "Print maven version" run: ./mvnw -version + # step 5 + - name: "Codecov" + if: matrix.java == '8' + uses: codecov/codecov-action@v3.1.4 # step 4.1 - name: "Test, Check style, Check PMD, Check license with Maven and Java8" if: matrix.java == '8' @@ -49,10 +53,7 @@ jobs: ./mvnw -T 4C clean test \ -Dmaven.git-commit-id.skip=true \ -e -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn; - # step 5 - - name: "Codecov" - if: matrix.java == '8' - uses: codecov/codecov-action@v3.1.4 + # job 2: Build on 'arm64v8/ubuntu' OS (Skip tests). build_arm64-binary: From a924dff0810ae8faf59b706b97d4f5ea5eeb8996 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Tue, 7 May 2024 19:40:06 +0800 Subject: [PATCH 08/18] fix(6509): add test --- .github/workflows/build.yml | 9 +++---- .../handler/DefaultRouterHandlerTest.java | 26 +++++++++--------- .../proctrl/impl/ProcessContextImplTest.java | 27 +++++++++---------- .../impl/CustomizeBusinessProcessorTest.java | 26 +++++++++--------- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2b7c227f46a..d57645eb419 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,10 +34,6 @@ jobs: # step 3 - name: "Print maven version" run: ./mvnw -version - # step 5 - - name: "Codecov" - if: matrix.java == '8' - uses: codecov/codecov-action@v3.1.4 # step 4.1 - name: "Test, Check style, Check PMD, Check license with Maven and Java8" if: matrix.java == '8' @@ -53,7 +49,10 @@ jobs: ./mvnw -T 4C clean test \ -Dmaven.git-commit-id.skip=true \ -e -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn; - + # step 5 + - name: "Codecov" + if: matrix.java == '8' + uses: codecov/codecov-action@v3.1.4 # job 2: Build on 'arm64v8/ubuntu' OS (Skip tests). build_arm64-binary: diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java index 0ab1a2d250a..4df69565fa1 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java @@ -1,20 +1,18 @@ /* + * 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 * - * * 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. + * 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.saga.proctrl.handler; diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java index ec160494af7..26d6bbd3419 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java @@ -1,22 +1,19 @@ /* + * 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 * - * * 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. + * 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.saga.proctrl.impl; import org.junit.jupiter.api.Assertions; diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java index c34e72cb062..bb27e869a06 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java @@ -1,20 +1,18 @@ /* + * 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 * - * * 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. + * 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.saga.proctrl.process.impl; From d039a4b022995573d528ec3c0591896b35853217 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Wed, 8 May 2024 19:28:44 +0800 Subject: [PATCH 09/18] fix(6509): add test --- .../seata/saga/proctrl/handler/DefaultRouterHandlerTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java index 4df69565fa1..9b4518dc373 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java @@ -27,7 +27,6 @@ import java.util.HashMap; import java.util.Map; - /** * @author jingliu_xiong@foxmail.com */ From bc16200a26975653d0713194eb2115dfd0d739a7 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Wed, 8 May 2024 19:40:50 +0800 Subject: [PATCH 10/18] fix(6509): add test --- .../seata/saga/proctrl/handler/DefaultRouterHandlerTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java index 9b4518dc373..30ca65eccc8 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java @@ -31,7 +31,6 @@ * @author jingliu_xiong@foxmail.com */ public class DefaultRouterHandlerTest { - @Test public void testRouteOfFrameworkException() { ProcessContextImpl context = new ProcessContextImpl(); From a708aded8ecfc78dcdd185baa2c897d6d39e5463 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Sun, 5 May 2024 14:18:40 +0800 Subject: [PATCH 11/18] fix(6509): improve the test case coverage of [saga] module to 70% --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 1 + .../saga/proctrl/ProcessControllerTests.java | 12 +- .../handler/DefaultRouterHandlerTest.java | 52 ++++++++ .../proctrl/impl/ProcessContextImplTest.java | 120 +++++++++++++++++ .../saga/proctrl/mock/MockProcessRouter.java | 3 + .../impl/CustomizeBusinessProcessorTest.java | 56 ++++++++ .../spel/SpringELExpressionFactoryTest.java | 30 +++++ .../spel/SpringELExpressionObject.java | 32 +++++ .../spel/SpringELExpressionTest.java | 55 ++++++++ .../DefaultSagaTransactionalTemplateTest.java | 101 +++++++++++++++ .../saga/engine/tm/MockGlobalTransaction.java | 121 ++++++++++++++++++ .../src/test/resources/file.conf | 78 +++++++++++ .../src/test/resources/registry.conf | 90 +++++++++++++ 14 files changed, 746 insertions(+), 6 deletions(-) create mode 100644 saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java create mode 100644 saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java create mode 100644 saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java create mode 100644 saga/seata-saga-spring/src/test/resources/file.conf create mode 100644 saga/seata-saga-spring/src/test/resources/registry.conf diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index bc99f91a0aa..c90396860b0 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -152,6 +152,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6456](https://github.com/apache/incubator-seata/pull/6456)] adjust the test cases related to dynamic configuration - [[#6466](https://github.com/apache/incubator-seata/pull/6466)] support redis integration testing - [[#6484](https://github.com/apache/incubator-seata/pull/6484)] fix FileConfigurationTest and MockServerTest fail +- [[#6519](https://github.com/apache/incubator-seata/pull/6484)] 增加saga模块的测试用例覆盖率 ### refactor: - [[#6280](https://github.com/apache/incubator-seata/pull/6280)] refactor Saga designer using diagram-js diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index f3e54c5f897..2dc35ebb4be 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -149,6 +149,7 @@ - [[#6456](https://github.com/apache/incubator-seata/pull/6456)] 调整动态配置监听测试用例 - [[#6466](https://github.com/apache/incubator-seata/pull/6466)] 支持redis的集成测试 - [[#6484](https://github.com/apache/incubator-seata/pull/6484)] 修复FileConfigurationTest和MockServerTest失败 +- [[#6519](https://github.com/apache/incubator-seata/pull/6484)] 增加saga模块的测试用例覆盖率 ### refactor: - [[#6280](https://github.com/apache/incubator-seata/pull/6280)] 使用diagram-js重构Saga设计器 diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java index 95a6e76535d..200a856f59c 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java @@ -16,12 +16,6 @@ */ package org.apache.seata.saga.proctrl; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - import org.apache.seata.saga.proctrl.eventing.impl.AsyncEventBus; import org.apache.seata.saga.proctrl.eventing.impl.DirectEventBus; import org.apache.seata.saga.proctrl.eventing.impl.ProcessCtrlEventConsumer; @@ -38,6 +32,12 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + /** * ProcessController Tests * diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java new file mode 100644 index 00000000000..30ca65eccc8 --- /dev/null +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java @@ -0,0 +1,52 @@ +/* + * 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.saga.proctrl.handler; + +import org.apache.seata.common.exception.FrameworkException; +import org.apache.seata.saga.proctrl.ProcessRouter; +import org.apache.seata.saga.proctrl.ProcessType; +import org.apache.seata.saga.proctrl.impl.ProcessContextImpl; +import org.apache.seata.saga.proctrl.mock.MockProcessRouter; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class DefaultRouterHandlerTest { + @Test + public void testRouteOfFrameworkException() { + ProcessContextImpl context = new ProcessContextImpl(); + DefaultRouterHandler defaultRouterHandler = new DefaultRouterHandler(); + Assertions.assertThrows(FrameworkException.class, () -> defaultRouterHandler.route(context)); + } + + @Test + public void testRouteOfException() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable("exception", new Object()); + DefaultRouterHandler defaultRouterHandler = new DefaultRouterHandler(); + Map processRouters = new HashMap<>(); + ProcessRouter processRouter = new MockProcessRouter(); + processRouters.put(ProcessType.STATE_LANG.getCode(), processRouter); + defaultRouterHandler.setProcessRouters(processRouters); + Assertions.assertThrows(RuntimeException.class, () -> defaultRouterHandler.route(context)); + } +} \ No newline at end of file diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java new file mode 100644 index 00000000000..26d6bbd3419 --- /dev/null +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java @@ -0,0 +1,120 @@ +/* + * 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.saga.proctrl.impl; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class ProcessContextImplTest { + + @Test + public void testGetVariableFromParent() { + ProcessContextImpl context = new ProcessContextImpl(); + ProcessContextImpl parentContext = new ProcessContextImpl(); + parentContext.setVariable("key", "value"); + context.setParent(parentContext); + Assertions.assertEquals("value", context.getVariable("key")); + } + + @Test + public void testSetVariable() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable("key", "value"); + context.setVariable("key", "value1"); + Assertions.assertEquals("value1", context.getVariable("key")); + context.removeVariable("key"); + ProcessContextImpl parentContext = new ProcessContextImpl(); + parentContext.setVariable("key", "value"); + context.setParent(parentContext); + Assertions.assertEquals("value", context.getVariable("key")); + } + + @Test + public void testGetVariables() { + ProcessContextImpl context = new ProcessContextImpl(); + ProcessContextImpl parentContext = new ProcessContextImpl(); + parentContext.setVariable("key", "value"); + context.setParent(parentContext); + Assertions.assertEquals(1, context.getVariables().size()); + } + + @Test + public void testSetVariables() { + ProcessContextImpl context = new ProcessContextImpl(); + Map map = new HashMap<>(); + map.put("key", "value"); + context.setVariables(map); + Assertions.assertEquals(1, context.getVariables().size()); + } + + @Test + public void testGetVariableLocally() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable("key", "value"); + Assertions.assertEquals("value", context.getVariableLocally("key")); + } + + + @Test + public void testSetVariablesLocally() { + ProcessContextImpl context = new ProcessContextImpl(); + Map map = new HashMap<>(); + map.put("key", "value"); + context.setVariablesLocally(map); + Assertions.assertEquals("value", context.getVariableLocally("key")); + } + + @Test + public void testHasVariable() { + ProcessContextImpl context = new ProcessContextImpl(); + Assertions.assertFalse(context.hasVariable("key")); + } + + @Test + public void testRemoveVariable() { + ProcessContextImpl context = new ProcessContextImpl(); + ProcessContextImpl parentContext = new ProcessContextImpl(); + parentContext.setVariable("key", "value"); + context.setParent(parentContext); + context.setVariable("key1", "value1"); + context.removeVariable("key"); + context.removeVariable("key1"); + Assertions.assertEquals(0, context.getVariables().size()); + } + + @Test + public void testRemoveVariableLocally() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable("key", "value"); + context.removeVariableLocally("key"); + Assertions.assertEquals(0, context.getVariables().size()); + } + + @Test + public void testClearLocally() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable("key", "value"); + context.clearLocally(); + Assertions.assertEquals(0, context.getVariables().size()); + } +} \ No newline at end of file diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/mock/MockProcessRouter.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/mock/MockProcessRouter.java index 966a84dfbc0..97a2459ee79 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/mock/MockProcessRouter.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/mock/MockProcessRouter.java @@ -39,6 +39,9 @@ public Instruction route(ProcessContext context) throws FrameworkException { return null;//end process } } + if (context.hasVariable("exception")) { + throw new RuntimeException("exception"); + } return instruction; } } diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java new file mode 100644 index 00000000000..bb27e869a06 --- /dev/null +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java @@ -0,0 +1,56 @@ +/* + * 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.saga.proctrl.process.impl; + +import org.apache.seata.common.exception.FrameworkException; +import org.apache.seata.saga.proctrl.ProcessContext; +import org.apache.seata.saga.proctrl.ProcessType; +import org.apache.seata.saga.proctrl.handler.ProcessHandler; +import org.apache.seata.saga.proctrl.handler.RouterHandler; +import org.apache.seata.saga.proctrl.impl.ProcessContextImpl; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class CustomizeBusinessProcessorTest { + + @Test + public void testProcessFail() { + CustomizeBusinessProcessor customizeBusinessProcessor = new CustomizeBusinessProcessor(); + ProcessContext processContext = new ProcessContextImpl(); + processContext.setVariable(ProcessContext.VAR_NAME_PROCESS_TYPE, ProcessType.STATE_LANG); + Map processHandlerMap = new HashMap<>(1); + processHandlerMap.put(ProcessType.STATE_LANG.getCode(), null); + customizeBusinessProcessor.setProcessHandlers(processHandlerMap); + Assertions.assertThrows(FrameworkException.class, () -> customizeBusinessProcessor.process(processContext)); + } + + @Test + public void testRouteFail() { + CustomizeBusinessProcessor customizeBusinessProcessor = new CustomizeBusinessProcessor(); + ProcessContext processContext = new ProcessContextImpl(); + Map routerHandlerMap = new HashMap<>(1); + routerHandlerMap.put(ProcessType.STATE_LANG.getCode(), null); + customizeBusinessProcessor.setRouterHandlers(routerHandlerMap); + Assertions.assertDoesNotThrow(() -> customizeBusinessProcessor.route(processContext)); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java new file mode 100644 index 00000000000..67d42b47812 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java @@ -0,0 +1,30 @@ +/* + * 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.saga.engine.expression.spel; + +import org.junit.jupiter.api.Test; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class SpringELExpressionFactoryTest { + @Test + public void testCreateExpression() { + SpringELExpressionFactory factory = new SpringELExpressionFactory(null); + factory.createExpression("'Hello World'.concat('!')"); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java new file mode 100644 index 00000000000..68f4fcdbf8f --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java @@ -0,0 +1,32 @@ +/* + * 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.saga.engine.expression.spel; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class SpringELExpressionObject { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java new file mode 100644 index 00000000000..8c6db198e6c --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java @@ -0,0 +1,55 @@ +/* + * 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.saga.engine.expression.spel; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.expression.Expression; +import org.springframework.expression.ExpressionParser; +import org.springframework.expression.spel.standard.SpelExpressionParser; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class SpringELExpressionTest { + @Test + public void testGetValue() { + ExpressionParser parser = new SpelExpressionParser(); + Expression defaultExpression = parser.parseExpression("'Hello World'.concat('!')"); + String value = (String) new SpringELExpression(defaultExpression).getValue(null); + Assertions.assertEquals(value, "Hello World!"); + } + + @Test + void testSetValue() { + ExpressionParser parser = new SpelExpressionParser(); + String expression = "name"; + SpringELExpressionObject springELExpressionObject = new SpringELExpressionObject(); + Expression defaultExpression = parser.parseExpression(expression); + SpringELExpression springELExpression = new SpringELExpression(defaultExpression); + springELExpression.setValue("test", springELExpressionObject); + Assertions.assertEquals(springELExpressionObject.getName(), "test"); + } + + @Test + void testGetExpressionString() { + ExpressionParser parser = new SpelExpressionParser(); + Expression defaultExpression = parser.parseExpression("'Hello World'.concat('!')"); + SpringELExpression springELExpression = new SpringELExpression(defaultExpression); + Assertions.assertEquals(springELExpression.getExpressionString(), "'Hello World'.concat('!')"); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java new file mode 100644 index 00000000000..917b4374376 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java @@ -0,0 +1,101 @@ +/* + * 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.saga.engine.tm; + +import org.apache.seata.common.exception.FrameworkException; +import org.apache.seata.core.model.BranchStatus; +import org.apache.seata.core.model.BranchType; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.core.model.Resource; +import org.apache.seata.core.model.ResourceManager; +import org.apache.seata.rm.DefaultResourceManager; +import org.apache.seata.tm.api.transaction.TransactionInfo; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.mockito.ArgumentMatchers.any; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class DefaultSagaTransactionalTemplateTest { + private static SagaTransactionalTemplate sagaTransactionalTemplate; + + @BeforeEach + public void init() { + sagaTransactionalTemplate = new DefaultSagaTransactionalTemplate(); + } + + @Test + public void testCommitTransaction() { + MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.commitTransaction(mockGlobalTransaction)); + } + + @Test + public void testRollbackTransaction() { + MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.rollbackTransaction(mockGlobalTransaction, null)); + } + + @Test + public void testBeginTransaction() { + TransactionInfo transactionInfo = new TransactionInfo(); + Assertions.assertThrows(FrameworkException.class, + () -> sagaTransactionalTemplate.beginTransaction(transactionInfo)); + } + + @Test + public void testReloadTransaction() { + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.reloadTransaction("")); + } + + @Test + public void testReportTransaction() { + MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); + GlobalStatus globalStatus = GlobalStatus.Committed; + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.reportTransaction(mockGlobalTransaction, globalStatus)); + } + + @Test + public void testBranchRegister() { + ResourceManager resourceManager = Mockito.mock(ResourceManager.class); + Mockito.doNothing().when(resourceManager).registerResource(any(Resource.class)); + DefaultResourceManager.get(); + DefaultResourceManager.mockResourceManager(BranchType.SAGA, resourceManager); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.branchRegister("", + "", "", "", "")); + } + + @Test + public void testBranchReport() { + ResourceManager resourceManager = Mockito.mock(ResourceManager.class); + Mockito.doNothing().when(resourceManager).registerResource(any(Resource.class)); + DefaultResourceManager.get(); + DefaultResourceManager.mockResourceManager(BranchType.SAGA, resourceManager); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.branchReport("", + 0, BranchStatus.Unknown, "")); + } + + @Test + public void testTriggerAfterCompletion() { + MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.triggerAfterCompletion(mockGlobalTransaction)); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java new file mode 100644 index 00000000000..be9c68235c9 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java @@ -0,0 +1,121 @@ +/* + * 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.saga.engine.tm; + +import org.apache.seata.core.context.RootContext; +import org.apache.seata.core.exception.TransactionException; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.saga.engine.sequence.UUIDSeqGenerator; +import org.apache.seata.tm.api.GlobalTransaction; +import org.apache.seata.tm.api.GlobalTransactionRole; +import org.apache.seata.tm.api.transaction.SuspendedResourcesHolder; + + +public class MockGlobalTransaction implements GlobalTransaction { + + private String xid; + private GlobalStatus status; + private long createTime; + + private static UUIDSeqGenerator uuidSeqGenerator = new UUIDSeqGenerator(); + + public MockGlobalTransaction() {} + + public MockGlobalTransaction(String xid) { + this.xid = xid; + } + + public MockGlobalTransaction(String xid, GlobalStatus status) { + this.xid = xid; + this.status = status; + } + + @Override + public void begin() throws TransactionException { + begin(60000); + } + + @Override + public void begin(int timeout) throws TransactionException { + this.createTime = System.currentTimeMillis(); + status = GlobalStatus.Begin; + xid = uuidSeqGenerator.generate(null); + RootContext.bind(xid); + } + + @Override + public void begin(int timeout, String name) throws TransactionException { + + } + + @Override + public void commit() throws TransactionException { + + } + + @Override + public void rollback() throws TransactionException { + + } + + @Override + public SuspendedResourcesHolder suspend() throws TransactionException { + return null; + } + + @Override + public SuspendedResourcesHolder suspend(boolean clean) + throws TransactionException { + return null; + } + + @Override + public void resume(SuspendedResourcesHolder suspendedResourcesHolder) + throws TransactionException { + + } + + @Override + public GlobalStatus getStatus() throws TransactionException { + return status; + } + + @Override + public String getXid() { + return xid; + } + + @Override + public void globalReport(GlobalStatus globalStatus) throws TransactionException { + + } + + @Override + public GlobalStatus getLocalStatus() { + return status; + } + + @Override + public GlobalTransactionRole getGlobalTransactionRole() { + return null; + } + + @Override + public long getCreateTime() { + return createTime; + } +} diff --git a/saga/seata-saga-spring/src/test/resources/file.conf b/saga/seata-saga-spring/src/test/resources/file.conf new file mode 100644 index 00000000000..94863827468 --- /dev/null +++ b/saga/seata-saga-spring/src/test/resources/file.conf @@ -0,0 +1,78 @@ +# +# 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. +# + +transport { + # tcp, unix-domain-socket + type = "TCP" + #NIO, NATIVE + server = "NIO" + #enable heartbeat + heartbeat = true + # the tm client batch send request enable + enableTmClientBatchSendRequest = true + # the rm client batch send request enable + enableRmClientBatchSendRequest = true + #thread factory for netty + threadFactory { + bossThreadPrefix = "NettyBoss" + workerThreadPrefix = "NettyServerNIOWorker" + serverExecutorThread-prefix = "NettyServerBizHandler" + shareBossWorker = false + clientSelectorThreadPrefix = "NettyClientSelector" + clientSelectorThreadSize = 1 + clientWorkerThreadPrefix = "NettyClientWorkerThread" + # netty boss thread size + bossThreadSize = 1 + #auto default pin or 8 + workerThreadSize = "default" + } + shutdown { + # when destroy server, wait seconds + wait = 3 + } + serialization = "seata" + compressor = "none" + + enableRmClientChannelCheckFailFast = false + enableTmClientChannelCheckFailFast = false +} + + +service { + #transaction service group mapping + vgroupMapping.default_tx_group = "default" + vgroupMapping.mock_tx_group = "mock" + #only support when registry.type=file, please don't set multiple addresses + default.grouplist = "127.0.0.1:8091" + mock.grouplist = "127.0.0.1:8099" + #disable seata + disableGlobalTransaction = false +} + +client { + rm { + reportSuccessEnable = false + sagaBranchRegisterEnable = false + sagaJsonParser = jackson + sagaRetryPersistModeUpdate = false + sagaCompensatePersistModeUpdate = false + } + loadBalance { + type = "XID" + virtualNodes = 10 + } +} diff --git a/saga/seata-saga-spring/src/test/resources/registry.conf b/saga/seata-saga-spring/src/test/resources/registry.conf new file mode 100644 index 00000000000..bab6e8ec0ef --- /dev/null +++ b/saga/seata-saga-spring/src/test/resources/registry.conf @@ -0,0 +1,90 @@ +# +# 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. +# + +registry { + # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa + type = "file" + + nacos { + serverAddr = "localhost" + namespace = "" + cluster = "default" + } + eureka { + serviceUrl = "http://localhost:8761/eureka" + application = "default" + weight = "1" + } + redis { + serverAddr = "localhost:6379" + db = "0" + } + zk { + cluster = "default" + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + } + consul { + cluster = "default" + serverAddr = "127.0.0.1:8500" + } + etcd3 { + cluster = "default" + serverAddr = "http://localhost:2379" + } + sofa { + serverAddr = "127.0.0.1:9603" + application = "default" + region = "DEFAULT_ZONE" + datacenter = "DefaultDataCenter" + cluster = "default" + group = "SEATA_GROUP" + addressWaitTime = "3000" + } + file { + name = "file.conf" + } +} + +config { + # file、nacos 、apollo、zk、consul、etcd3 + type = "file" + + nacos { + serverAddr = "localhost" + namespace = "" + } + consul { + serverAddr = "127.0.0.1:8500" + } + apollo { + appId = "seata-server" + apolloMeta = "http://192.168.1.204:8801" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + } + etcd3 { + serverAddr = "http://localhost:2379" + } + file { + name = "file.conf" + } +} From 472aa1aef15928435bb91698ffe1c6c6fa0a8183 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Sat, 11 May 2024 21:52:53 +0800 Subject: [PATCH 12/18] fix(6509): add test --- .../engine/pcext/utils/LoopTaskUtils.java | 16 +- .../config/DbStateMachineConfigTest.java | 60 +++++++ .../saga/engine/config/MockDataSource.java | 78 +++++++++ .../saga/engine/invoker/impl/MockService.java | 40 +++++ .../impl/SpringBeanServiceInvokerTest.java | 160 ++++++++++++++++++ .../db/DbAndReportTcStateLogStoreTest.java | 140 +++++++++++++++ .../store/db/MockSagaTransactionTemplate.java | 84 +++++++++ .../DefaultSagaTransactionalTemplateTest.java | 27 ++- .../saga/engine/tm/MockGlobalTransaction.java | 2 +- .../saga/engine/tm/MockTransactionHook.java | 43 +++++ .../statelang/parser/StateParserTests.java | 30 +++- 11 files changed, 663 insertions(+), 17 deletions(-) create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/MockDataSource.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/MockService.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java create mode 100644 saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java index 37479b65964..75db9e53bb5 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java @@ -16,14 +16,6 @@ */ package org.apache.seata.saga.engine.pcext.utils; -import java.util.ArrayList; -import java.util.Collection; -import java.util.EmptyStackException; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - import org.apache.seata.common.exception.FrameworkErrorCode; import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.common.util.NumberUtils; @@ -45,6 +37,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.Collection; +import java.util.EmptyStackException; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + /** * Loop Task Util * diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java new file mode 100644 index 00000000000..6db3f278ea2 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java @@ -0,0 +1,60 @@ +/* + * 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.saga.engine.config; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; + +import static org.mockito.Mockito.when; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class DbStateMachineConfigTest { + @Test + public void testGetDbTypeFromDataSource() throws SQLException { + Connection connection = Mockito.mock(Connection.class); + DatabaseMetaData databaseMetaData = Mockito.mock(DatabaseMetaData.class); + when(connection.getMetaData()).thenReturn(databaseMetaData); + when(databaseMetaData.getDatabaseProductName()).thenReturn("test"); + MockDataSource mockDataSource = new MockDataSource(); + mockDataSource.setConnection(connection); + Assertions.assertEquals(DbStateMachineConfig.getDbTypeFromDataSource(mockDataSource), "test"); + } + + @Test + public void testAfterPropertiesSet() throws Exception { + DbStateMachineConfig dbStateMachineConfig = new DbStateMachineConfig(); + Connection connection = Mockito.mock(Connection.class); + DatabaseMetaData databaseMetaData = Mockito.mock(DatabaseMetaData.class); + when(connection.getMetaData()).thenReturn(databaseMetaData); + when(databaseMetaData.getDatabaseProductName()).thenReturn("test"); + MockDataSource mockDataSource = new MockDataSource(); + mockDataSource.setConnection(connection); + dbStateMachineConfig.setDataSource(mockDataSource); + dbStateMachineConfig.setApplicationId("test"); + dbStateMachineConfig.setTxServiceGroup("test"); + + Assertions.assertDoesNotThrow(dbStateMachineConfig::afterPropertiesSet); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/MockDataSource.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/MockDataSource.java new file mode 100644 index 00000000000..ed734cc2ecd --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/MockDataSource.java @@ -0,0 +1,78 @@ +/* + * 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.saga.engine.config; + +import javax.sql.DataSource; +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.logging.Logger; + + +public class MockDataSource implements DataSource { + private Connection connection; + + public void setConnection(Connection connection) { + this.connection = connection; + } + + @Override + public Connection getConnection() throws SQLException { + return connection; + } + + @Override + public Connection getConnection(String username, String password) throws SQLException { + return null; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } + + @Override + public PrintWriter getLogWriter() throws SQLException { + return null; + } + + @Override + public void setLogWriter(PrintWriter out) throws SQLException { + + } + + @Override + public void setLoginTimeout(int seconds) throws SQLException { + + } + + @Override + public int getLoginTimeout() throws SQLException { + return 0; + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return null; + } +} diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/MockService.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/MockService.java new file mode 100644 index 00000000000..8ab967bf37a --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/MockService.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.seata.saga.engine.invoker.impl; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class MockService { + private int times; + + public String mockInvoke(String param) { + return param; + } + + public boolean mockInvoke(boolean param) { + return param; + } + public String mockInvokeRetry(String param) { + times++; + if (times > 2) { + return param; + } + throw new RuntimeException("mockInvokeRetry"); + } +} diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java new file mode 100644 index 00000000000..bf0aa02e676 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java @@ -0,0 +1,160 @@ +/* + * 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.saga.engine.invoker.impl; + +import org.apache.seata.saga.statelang.domain.impl.AbstractTaskState; +import org.apache.seata.saga.statelang.domain.impl.ServiceTaskStateImpl; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.context.ApplicationContext; + +import java.util.Collections; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import static org.mockito.Mockito.anyString; +import static org.mockito.Mockito.when; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class SpringBeanServiceInvokerTest { + @Test + public void testInvokeByClassParam() throws Throwable { + SpringBeanServiceInvoker springBeanServiceInvoker = new SpringBeanServiceInvoker(); + Object[] input = new Object[]{"param"}; + ServiceTaskStateImpl serviceTaskState = new ServiceTaskStateImpl(); + serviceTaskState.setServiceName("mockService"); + serviceTaskState.setServiceMethod("mockInvoke"); + serviceTaskState.setParameterTypes(Collections.singletonList("java.lang.String")); + MockService mockService = new MockService(); + ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); + when(applicationContext.getBean(anyString())).thenReturn(mockService); + springBeanServiceInvoker.setThreadPoolExecutor(new ThreadPoolExecutor(1, + 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>())); + springBeanServiceInvoker.setApplicationContext(applicationContext); + + String output = (String) springBeanServiceInvoker.invoke(serviceTaskState, input); + Assertions.assertEquals(output, "param"); + } + + @Test + public void testInvokeByPrimitiveParam() throws Throwable { + SpringBeanServiceInvoker springBeanServiceInvoker = new SpringBeanServiceInvoker(); + Object[] input = new Object[]{false}; + ServiceTaskStateImpl serviceTaskState = new ServiceTaskStateImpl(); + serviceTaskState.setServiceName("mockService"); + serviceTaskState.setServiceMethod("mockInvoke"); + serviceTaskState.setParameterTypes(Collections.singletonList("boolean")); + MockService mockService = new MockService(); + serviceTaskState.setMethod(mockService.getClass().getMethod("mockInvoke", boolean.class)); + ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); + when(applicationContext.getBean(anyString())).thenReturn(mockService); + springBeanServiceInvoker.setThreadPoolExecutor(new ThreadPoolExecutor(1, + 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>())); + springBeanServiceInvoker.setApplicationContext(applicationContext); + + boolean output = (boolean) springBeanServiceInvoker.invoke(serviceTaskState, input); + Assertions.assertEquals(output, false); + } + + @Test + public void testInvokeRetryFailed() throws Throwable { + SpringBeanServiceInvoker springBeanServiceInvoker = new SpringBeanServiceInvoker(); + Object[] input = new Object[]{"param"}; + ServiceTaskStateImpl serviceTaskState = new ServiceTaskStateImpl(); + serviceTaskState.setServiceName("mockService"); + serviceTaskState.setServiceMethod("mockInvokeRetry"); + serviceTaskState.setParameterTypes(Collections.singletonList("java.lang.String")); + AbstractTaskState.RetryImpl retry = new AbstractTaskState.RetryImpl(); + retry.setMaxAttempts(3); + retry.setExceptions(Collections.singletonList("java.lang.NullPoint")); + serviceTaskState.setRetry(Collections.singletonList(retry)); + MockService mockService = new MockService(); + ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); + when(applicationContext.getBean(anyString())).thenReturn(mockService); + springBeanServiceInvoker.setThreadPoolExecutor(new ThreadPoolExecutor(1, + 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>())); + springBeanServiceInvoker.setApplicationContext(applicationContext); + + Assertions.assertThrows(java.lang.RuntimeException.class, () -> springBeanServiceInvoker.invoke(serviceTaskState, input)); + } + + @Test + public void testInvokeRetrySuccess() throws Throwable { + SpringBeanServiceInvoker springBeanServiceInvoker = new SpringBeanServiceInvoker(); + Object[] input = new Object[]{"param"}; + ServiceTaskStateImpl serviceTaskState = new ServiceTaskStateImpl(); + serviceTaskState.setServiceName("mockService"); + serviceTaskState.setServiceMethod("mockInvokeRetry"); + serviceTaskState.setParameterTypes(Collections.singletonList("java.lang.String")); + AbstractTaskState.RetryImpl retry = new AbstractTaskState.RetryImpl(); + retry.setMaxAttempts(3); + retry.setExceptions(Collections.singletonList("java.lang.RuntimeException")); + serviceTaskState.setRetry(Collections.singletonList(retry)); + MockService mockService = new MockService(); + ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); + when(applicationContext.getBean(anyString())).thenReturn(mockService); + springBeanServiceInvoker.setThreadPoolExecutor(new ThreadPoolExecutor(1, + 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>())); + springBeanServiceInvoker.setApplicationContext(applicationContext); + + String output = (String) springBeanServiceInvoker.invoke(serviceTaskState, input); + Assertions.assertEquals(output, "param"); + } + + @Test + public void testInvokeAsync() throws Throwable { + SpringBeanServiceInvoker springBeanServiceInvoker = new SpringBeanServiceInvoker(); + Object[] input = new Object[]{"param"}; + ServiceTaskStateImpl serviceTaskState = new ServiceTaskStateImpl(); + serviceTaskState.setServiceName("mockService"); + serviceTaskState.setServiceMethod("mockInvoke"); + serviceTaskState.setParameterTypes(Collections.singletonList("java.lang.String")); + serviceTaskState.setAsync(true); + MockService mockService = new MockService(); + ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); + when(applicationContext.getBean(anyString())).thenReturn(mockService); + springBeanServiceInvoker.setThreadPoolExecutor(new ThreadPoolExecutor(1, + 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>())); + springBeanServiceInvoker.setApplicationContext(applicationContext); + + String output = (String) springBeanServiceInvoker.invoke(serviceTaskState, input); + Assertions.assertEquals(output, null); + } + + @Test + public void testInvokeAsyncButSync() throws Throwable { + SpringBeanServiceInvoker springBeanServiceInvoker = new SpringBeanServiceInvoker(); + Object[] input = new Object[]{"param"}; + ServiceTaskStateImpl serviceTaskState = new ServiceTaskStateImpl(); + serviceTaskState.setServiceName("mockService"); + serviceTaskState.setServiceMethod("mockInvoke"); + serviceTaskState.setParameterTypes(Collections.singletonList("java.lang.String")); + serviceTaskState.setAsync(true); + MockService mockService = new MockService(); + ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); + when(applicationContext.getBean(anyString())).thenReturn(mockService); + springBeanServiceInvoker.setApplicationContext(applicationContext); + + String output = (String) springBeanServiceInvoker.invoke(serviceTaskState, input); + Assertions.assertEquals(output, "param"); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java new file mode 100644 index 00000000000..54e783f5152 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java @@ -0,0 +1,140 @@ +/* + * 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.saga.engine.store.db; + +import org.apache.seata.saga.engine.config.DbStateMachineConfig; +import org.apache.seata.saga.engine.sequence.UUIDSeqGenerator; +import org.apache.seata.saga.proctrl.impl.ProcessContextImpl; +import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.impl.StateInstanceImpl; +import org.apache.seata.saga.statelang.domain.impl.StateMachineInstanceImpl; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; + +import static org.mockito.ArgumentMatchers.any; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class DbAndReportTcStateLogStoreTest { + private DbAndReportTcStateLogStore dbAndReportTcStateLogStore; + + @BeforeEach + public void setUp() { + DbAndReportTcStateLogStore mock = Mockito.spy(DbAndReportTcStateLogStore.class); + dbAndReportTcStateLogStore = mock; + dbAndReportTcStateLogStore.setSeqGenerator(new UUIDSeqGenerator()); + dbAndReportTcStateLogStore.setSagaTransactionalTemplate(new MockSagaTransactionTemplate()); + dbAndReportTcStateLogStore.setTablePrefix("test_"); + Mockito.doReturn(new StateInstanceImpl()).when(mock).selectOne(any(), any(), any(), any()); + Mockito.doReturn(Collections.singletonList(new StateInstanceImpl())).when(mock).selectList(any(), any(), any()); + Mockito.doReturn(1).when(mock).executeUpdate(any(), any(), any()); + Mockito.doReturn(1).when(mock).executeUpdate(any(), any(), any()); + } + + @Test + public void testRecordStateMachineStarted() { + DbAndReportTcStateLogStore dbAndReportTcStateLogStore = new DbAndReportTcStateLogStore(); + StateMachineInstanceImpl stateMachineInstance = new StateMachineInstanceImpl(); + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG, new DbStateMachineConfig()); + Assertions.assertThrows(NullPointerException.class, + () -> dbAndReportTcStateLogStore.recordStateMachineStarted(stateMachineInstance, context)); + } + + @Test + public void testRecordStateMachineFinished() { + DbAndReportTcStateLogStore dbAndReportTcStateLogStore = new DbAndReportTcStateLogStore(); + StateMachineInstanceImpl stateMachineInstance = new StateMachineInstanceImpl(); + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG, new DbStateMachineConfig()); + Assertions.assertThrows(NullPointerException.class, + () -> dbAndReportTcStateLogStore.recordStateMachineFinished(stateMachineInstance, context)); + } + + @Test + public void testRecordStateMachineRestarted() { + DbAndReportTcStateLogStore dbAndReportTcStateLogStore = new DbAndReportTcStateLogStore(); + StateMachineInstanceImpl stateMachineInstance = new StateMachineInstanceImpl(); + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG, new DbStateMachineConfig()); + Assertions.assertThrows(NullPointerException.class, + () -> dbAndReportTcStateLogStore.recordStateMachineRestarted(stateMachineInstance, context)); + } + + @Test + public void testRecordStateStarted() { + DbAndReportTcStateLogStore dbAndReportTcStateLogStore = new DbAndReportTcStateLogStore(); + StateInstanceImpl stateMachineInstance = new StateInstanceImpl(); + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG, new DbStateMachineConfig()); + Assertions.assertThrows(NullPointerException.class, + () -> dbAndReportTcStateLogStore.recordStateStarted(stateMachineInstance, context)); + } + + @Test + public void testRecordStateFinished() { + DbAndReportTcStateLogStore dbAndReportTcStateLogStore = new DbAndReportTcStateLogStore(); + StateInstanceImpl stateMachineInstance = new StateInstanceImpl(); + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG, new DbStateMachineConfig()); + Assertions.assertThrows(NullPointerException.class, + () -> dbAndReportTcStateLogStore.recordStateFinished(stateMachineInstance, context)); + } + + @Test + public void testGetStateMachineInstance() { + Assertions.assertDoesNotThrow(() -> dbAndReportTcStateLogStore.getStateInstance("test", "test")); + } + + @Test + public void testGetStateMachineInstanceByBusinessKey() { + StateMachineInstanceImpl stateMachineInstance = new StateMachineInstanceImpl(); + stateMachineInstance.setStateMap(new HashMap<>()); + stateMachineInstance.setStateList(new ArrayList<>()); + Mockito.doReturn(stateMachineInstance).when(dbAndReportTcStateLogStore).selectOne(any(), any(), any(), any()); + dbAndReportTcStateLogStore.getStateMachineInstanceByBusinessKey("test", "test"); + } + + @Test + public void testQueryStateMachineInstanceByParentId() { + Assertions.assertDoesNotThrow(() -> dbAndReportTcStateLogStore.queryStateMachineInstanceByParentId("test")); + } + + @Test + public void testGetStateInstance() { + Assertions.assertDoesNotThrow(() -> dbAndReportTcStateLogStore.getStateInstance("test", "test")); + } + + @Test + public void testQueryStateInstanceListByMachineInstanceId() { + Assertions.assertDoesNotThrow(() -> dbAndReportTcStateLogStore.queryStateInstanceListByMachineInstanceId("test")); + } + + @Test + public void testClearUp() { + ProcessContextImpl context = new ProcessContextImpl(); + context.setVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST, new StateMachineInstanceImpl()); + Assertions.assertDoesNotThrow(() -> dbAndReportTcStateLogStore.clearUp(context)); + } +} \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java new file mode 100644 index 00000000000..233b9a65a96 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java @@ -0,0 +1,84 @@ +/* + * 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.saga.engine.store.db; + +import org.apache.seata.core.exception.TransactionException; +import org.apache.seata.core.model.BranchStatus; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.saga.engine.tm.MockGlobalTransaction; +import org.apache.seata.saga.engine.tm.SagaTransactionalTemplate; +import org.apache.seata.tm.api.GlobalTransaction; +import org.apache.seata.tm.api.TransactionalExecutor.ExecutionException; +import org.apache.seata.tm.api.transaction.TransactionInfo; + +import java.util.Random; + + +public class MockSagaTransactionTemplate implements SagaTransactionalTemplate { + + @Override + public void commitTransaction(GlobalTransaction tx) throws ExecutionException { + + } + + @Override + public void rollbackTransaction(GlobalTransaction tx, Throwable ex) throws TransactionException, ExecutionException { + + } + + @Override + public GlobalTransaction beginTransaction(TransactionInfo txInfo) throws ExecutionException { + GlobalTransaction globalTransaction = new MockGlobalTransaction(); + try { + globalTransaction.begin(); + } catch (TransactionException e) { + e.printStackTrace(); + } + return globalTransaction; + } + + @Override + public GlobalTransaction reloadTransaction(String xid) throws ExecutionException, TransactionException { + return new MockGlobalTransaction(xid, GlobalStatus.UnKnown); + } + + @Override + public void reportTransaction(GlobalTransaction tx, GlobalStatus globalStatus) throws ExecutionException { + + } + + @Override + public long branchRegister(String resourceId, String clientId, String xid, String applicationData, String lockKeys) + throws TransactionException { + return new Random().nextLong(); + } + + @Override + public void branchReport(String xid, long branchId, BranchStatus status, String applicationData) throws TransactionException { + + } + + @Override + public void triggerAfterCompletion(GlobalTransaction tx) { + + } + + @Override + public void cleanUp(GlobalTransaction tx) { + + } +} diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java index 917b4374376..5bafdbef2de 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java @@ -16,19 +16,23 @@ */ package org.apache.seata.saga.engine.tm; -import org.apache.seata.common.exception.FrameworkException; import org.apache.seata.core.model.BranchStatus; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.model.GlobalStatus; import org.apache.seata.core.model.Resource; import org.apache.seata.core.model.ResourceManager; import org.apache.seata.rm.DefaultResourceManager; +import org.apache.seata.tm.api.GlobalTransactionContext; +import org.apache.seata.tm.api.transaction.TransactionHookManager; import org.apache.seata.tm.api.transaction.TransactionInfo; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; import org.mockito.Mockito; +import java.util.Collections; + import static org.mockito.ArgumentMatchers.any; /** @@ -50,15 +54,23 @@ public void testCommitTransaction() { @Test public void testRollbackTransaction() { + MockedStatic enhancedTransactionHookManager = Mockito.mockStatic(TransactionHookManager.class); + enhancedTransactionHookManager.when(TransactionHookManager::getHooks).thenReturn(Collections.singletonList(new MockTransactionHook())); MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.rollbackTransaction(mockGlobalTransaction, null)); + enhancedTransactionHookManager.close(); } @Test public void testBeginTransaction() { + MockedStatic enhancedServiceLoader = Mockito.mockStatic(GlobalTransactionContext.class); + enhancedServiceLoader.when(GlobalTransactionContext::getCurrentOrCreate).thenReturn(new MockGlobalTransaction()); + MockedStatic enhancedTransactionHookManager = Mockito.mockStatic(TransactionHookManager.class); + enhancedTransactionHookManager.when(TransactionHookManager::getHooks).thenReturn(Collections.singletonList(new MockTransactionHook())); TransactionInfo transactionInfo = new TransactionInfo(); - Assertions.assertThrows(FrameworkException.class, - () -> sagaTransactionalTemplate.beginTransaction(transactionInfo)); + Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.beginTransaction(transactionInfo)); + enhancedServiceLoader.close(); + enhancedTransactionHookManager.close(); } @Test @@ -95,7 +107,16 @@ public void testBranchReport() { @Test public void testTriggerAfterCompletion() { + MockedStatic enhancedTransactionHookManager = Mockito.mockStatic(TransactionHookManager.class); + enhancedTransactionHookManager.when(TransactionHookManager::getHooks).thenReturn(Collections.singletonList(new MockTransactionHook())); MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); Assertions.assertDoesNotThrow(() -> sagaTransactionalTemplate.triggerAfterCompletion(mockGlobalTransaction)); + enhancedTransactionHookManager.close(); + } + + @Test + public void testCleanUp() { + MockGlobalTransaction mockGlobalTransaction = new MockGlobalTransaction(); + sagaTransactionalTemplate.cleanUp(mockGlobalTransaction); } } \ No newline at end of file diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java index be9c68235c9..5b72dc721e5 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java @@ -111,7 +111,7 @@ public GlobalStatus getLocalStatus() { @Override public GlobalTransactionRole getGlobalTransactionRole() { - return null; + return GlobalTransactionRole.Launcher; } @Override diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java new file mode 100644 index 00000000000..6e64ff056c4 --- /dev/null +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java @@ -0,0 +1,43 @@ +package org.apache.seata.saga.engine.tm; + +import org.apache.seata.tm.api.transaction.TransactionHook; + +/** + * @author jingliu_xiong@foxmail.com + */ +public class MockTransactionHook implements TransactionHook { + @Override + public void beforeBegin() { + + } + + @Override + public void afterBegin() { + + } + + @Override + public void beforeCommit() { + + } + + @Override + public void afterCommit() { + + } + + @Override + public void beforeRollback() { + + } + + @Override + public void afterRollback() { + + } + + @Override + public void afterCompletion() { + + } +} diff --git a/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java b/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java index 45702f40f72..1e315d353c7 100644 --- a/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java +++ b/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java @@ -16,18 +16,22 @@ */ package org.apache.seata.saga.statelang.parser; -import java.io.IOException; -import java.io.InputStream; -import java.util.Date; -import java.util.Map; - +import org.apache.seata.common.util.BeanUtils; import org.apache.seata.saga.statelang.domain.StateMachine; +import org.apache.seata.saga.statelang.domain.StateMachineInstance; +import org.apache.seata.saga.statelang.domain.impl.StateMachineInstanceImpl; import org.apache.seata.saga.statelang.parser.utils.DesignerJsonTransformer; import org.apache.seata.saga.statelang.parser.utils.IOUtils; import org.apache.seata.saga.statelang.validator.ValidationException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.io.IOException; +import java.io.InputStream; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + /** * StateParser tests */ @@ -117,6 +121,22 @@ public void testRecursiveSubStateMachine() throws IOException { Assertions.assertTrue(e.getMessage().endsWith("call itself")); } + @Test + public void testGenerateTracingGraphJson() throws Exception { + InputStream inputStream = getInputStreamByPath("statelang/simple_statemachine_with_layout.json"); + String json = IOUtils.toString(inputStream, "UTF-8"); + StateMachine stateMachine = StateMachineParserFactory.getStateMachineParser(null).parse(json); + Map machineMap = BeanUtils.objectToMap(stateMachine); + StateMachineInstance instance = (StateMachineInstance) BeanUtils.mapToObject(machineMap, StateMachineInstanceImpl.class); + Map context = new HashMap<>(); + context.put("test", "test"); + stateMachine.setContent(json); + instance.setStateMachine(stateMachine); + JsonParser jsonParser = JsonParserFactory.getJsonParser("fastjson"); + String graphJson = DesignerJsonTransformer.generateTracingGraphJson(instance, jsonParser); + Assertions.assertNotNull(graphJson); + } + private InputStream getInputStreamByPath(String path) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { From ec867ea7ba205284f168af43139652546b51ec1b Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Sat, 11 May 2024 21:58:21 +0800 Subject: [PATCH 13/18] fix(6509): add license --- .../saga/engine/tm/MockTransactionHook.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java index 6e64ff056c4..6bb872a5c19 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java @@ -1,3 +1,19 @@ +/* + * 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.saga.engine.tm; import org.apache.seata.tm.api.transaction.TransactionHook; From 77cd0341376a6c3b7942011af5d0b9542a868dbb Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Sat, 25 May 2024 20:09:59 +0800 Subject: [PATCH 14/18] fix(6509): delele author mail --- .../apache/seata/saga/engine/store/db/AbstractStore.java | 6 ++---- .../saga/proctrl/handler/DefaultRouterHandlerTest.java | 2 +- .../seata/saga/proctrl/impl/ProcessContextImplTest.java | 2 +- .../process/impl/CustomizeBusinessProcessorTest.java | 2 +- .../seata/saga/engine/config/DbStateMachineConfigTest.java | 2 +- .../org/apache/seata/saga/engine/config/MockDataSource.java | 4 +++- .../expression/spel/SpringELExpressionFactoryTest.java | 2 +- .../engine/expression/spel/SpringELExpressionObject.java | 2 +- .../saga/engine/expression/spel/SpringELExpressionTest.java | 2 +- .../apache/seata/saga/engine/invoker/impl/MockService.java | 2 +- .../engine/invoker/impl/SpringBeanServiceInvokerTest.java | 2 +- .../engine/store/db/DbAndReportTcStateLogStoreTest.java | 2 +- .../saga/engine/store/db/MockSagaTransactionTemplate.java | 4 +++- .../engine/tm/DefaultSagaTransactionalTemplateTest.java | 2 +- .../apache/seata/saga/engine/tm/MockGlobalTransaction.java | 4 +++- .../apache/seata/saga/engine/tm/MockTransactionHook.java | 2 +- 16 files changed, 23 insertions(+), 19 deletions(-) diff --git a/saga/seata-saga-engine-store/src/main/java/org/apache/seata/saga/engine/store/db/AbstractStore.java b/saga/seata-saga-engine-store/src/main/java/org/apache/seata/saga/engine/store/db/AbstractStore.java index 638a47345a7..9541144f825 100644 --- a/saga/seata-saga-engine-store/src/main/java/org/apache/seata/saga/engine/store/db/AbstractStore.java +++ b/saga/seata-saga-engine-store/src/main/java/org/apache/seata/saga/engine/store/db/AbstractStore.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.engine.store.db; -import org.apache.seata.common.util.BeanUtils; +import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -25,12 +25,10 @@ import java.util.Arrays; import java.util.List; -import javax.sql.DataSource; - import org.apache.seata.common.exception.StoreException; +import org.apache.seata.common.util.BeanUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /** * Abstract store * diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java index 30ca65eccc8..cd319da6832 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java @@ -28,7 +28,7 @@ import java.util.Map; /** - * @author jingliu_xiong@foxmail.com + * DefaultRouterHandlerTest */ public class DefaultRouterHandlerTest { @Test diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java index 26d6bbd3419..c9df539c872 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java @@ -23,7 +23,7 @@ import java.util.Map; /** - * @author jingliu_xiong@foxmail.com + * ProcessContextImplTest */ public class ProcessContextImplTest { diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java index bb27e869a06..e8eacae6796 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java @@ -29,7 +29,7 @@ import java.util.Map; /** - * @author jingliu_xiong@foxmail.com + * CustomizeBusinessProcessorTest */ public class CustomizeBusinessProcessorTest { diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java index 6db3f278ea2..0bc082395fb 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java @@ -28,7 +28,7 @@ import static org.mockito.Mockito.when; /** - * @author jingliu_xiong@foxmail.com + * DbStateMachineConfigTest */ public class DbStateMachineConfigTest { @Test diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/MockDataSource.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/MockDataSource.java index ed734cc2ecd..480337df8c8 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/MockDataSource.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/MockDataSource.java @@ -23,7 +23,9 @@ import java.sql.SQLFeatureNotSupportedException; import java.util.logging.Logger; - +/** + * MockDataSource + */ public class MockDataSource implements DataSource { private Connection connection; diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java index 67d42b47812..3db7d54e76e 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java @@ -19,7 +19,7 @@ import org.junit.jupiter.api.Test; /** - * @author jingliu_xiong@foxmail.com + * SpringELExpressionFactoryTest */ public class SpringELExpressionFactoryTest { @Test diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java index 68f4fcdbf8f..b8bb541e35f 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionObject.java @@ -17,7 +17,7 @@ package org.apache.seata.saga.engine.expression.spel; /** - * @author jingliu_xiong@foxmail.com + * SpringELExpressionObject */ public class SpringELExpressionObject { private String name; diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java index 8c6db198e6c..68e09e9e462 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionTest.java @@ -23,7 +23,7 @@ import org.springframework.expression.spel.standard.SpelExpressionParser; /** - * @author jingliu_xiong@foxmail.com + * SpringELExpressionTest */ public class SpringELExpressionTest { @Test diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/MockService.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/MockService.java index 8ab967bf37a..030933b499c 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/MockService.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/MockService.java @@ -18,7 +18,7 @@ package org.apache.seata.saga.engine.invoker.impl; /** - * @author jingliu_xiong@foxmail.com + * MockService */ public class MockService { private int times; diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java index bf0aa02e676..fb51a425c26 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java @@ -33,7 +33,7 @@ import static org.mockito.Mockito.when; /** - * @author jingliu_xiong@foxmail.com + * SpringBeanServiceInvokerTest */ public class SpringBeanServiceInvokerTest { @Test diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java index 54e783f5152..9a69212dea0 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java @@ -34,7 +34,7 @@ import static org.mockito.ArgumentMatchers.any; /** - * @author jingliu_xiong@foxmail.com + * DbAndReportTcStateLogStoreTest */ public class DbAndReportTcStateLogStoreTest { private DbAndReportTcStateLogStore dbAndReportTcStateLogStore; diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java index 233b9a65a96..c7cb4f5c0ea 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java @@ -27,7 +27,9 @@ import java.util.Random; - +/** + * MockSagaTransactionTemplate + */ public class MockSagaTransactionTemplate implements SagaTransactionalTemplate { @Override diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java index 5bafdbef2de..3d00284295d 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java @@ -36,7 +36,7 @@ import static org.mockito.ArgumentMatchers.any; /** - * @author jingliu_xiong@foxmail.com + * DefaultSagaTransactionalTemplateTest */ public class DefaultSagaTransactionalTemplateTest { private static SagaTransactionalTemplate sagaTransactionalTemplate; diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java index 5b72dc721e5..90a7c8a4076 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockGlobalTransaction.java @@ -24,7 +24,9 @@ import org.apache.seata.tm.api.GlobalTransactionRole; import org.apache.seata.tm.api.transaction.SuspendedResourcesHolder; - +/** + * MockGlobalTransaction + */ public class MockGlobalTransaction implements GlobalTransaction { private String xid; diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java index 6bb872a5c19..c57b73ea6ce 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/MockTransactionHook.java @@ -19,7 +19,7 @@ import org.apache.seata.tm.api.transaction.TransactionHook; /** - * @author jingliu_xiong@foxmail.com + * MockTransactionHook */ public class MockTransactionHook implements TransactionHook { @Override From 6ccff413d3a47d75007c1ae9def37c15dd079275 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Sat, 25 May 2024 20:43:17 +0800 Subject: [PATCH 15/18] fix(6509): adjust rely --- .../saga/engine/pcext/utils/LoopTaskUtils.java | 15 ++++++++------- .../saga/proctrl/ProcessControllerTests.java | 12 ++++++------ .../proctrl/handler/DefaultRouterHandlerTest.java | 6 +++--- .../saga/proctrl/impl/ProcessContextImplTest.java | 6 +++--- .../impl/CustomizeBusinessProcessorTest.java | 6 +++--- .../engine/config/DbStateMachineConfigTest.java | 7 ++++--- .../impl/SpringBeanServiceInvokerTest.java | 10 +++++----- .../store/db/DbAndReportTcStateLogStoreTest.java | 8 ++++---- .../store/db/MockSagaTransactionTemplate.java | 4 ++-- .../tm/DefaultSagaTransactionalTemplateTest.java | 4 ++-- .../saga/statelang/parser/StateParserTests.java | 12 ++++++------ 11 files changed, 46 insertions(+), 44 deletions(-) diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java index 75db9e53bb5..e71818f1f1a 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java @@ -16,6 +16,14 @@ */ package org.apache.seata.saga.engine.pcext.utils; +import java.util.ArrayList; +import java.util.Collection; +import java.util.EmptyStackException; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + import org.apache.seata.common.exception.FrameworkErrorCode; import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.common.util.NumberUtils; @@ -37,13 +45,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.Collection; -import java.util.EmptyStackException; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; /** * Loop Task Util diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java index 200a856f59c..95a6e76535d 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/ProcessControllerTests.java @@ -16,6 +16,12 @@ */ package org.apache.seata.saga.proctrl; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + import org.apache.seata.saga.proctrl.eventing.impl.AsyncEventBus; import org.apache.seata.saga.proctrl.eventing.impl.DirectEventBus; import org.apache.seata.saga.proctrl.eventing.impl.ProcessCtrlEventConsumer; @@ -32,12 +38,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - /** * ProcessController Tests * diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java index cd319da6832..47f2b91e025 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/handler/DefaultRouterHandlerTest.java @@ -16,6 +16,9 @@ */ package org.apache.seata.saga.proctrl.handler; +import java.util.HashMap; +import java.util.Map; + import org.apache.seata.common.exception.FrameworkException; import org.apache.seata.saga.proctrl.ProcessRouter; import org.apache.seata.saga.proctrl.ProcessType; @@ -24,9 +27,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.util.HashMap; -import java.util.Map; - /** * DefaultRouterHandlerTest */ diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java index c9df539c872..1d457e5b9e6 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/impl/ProcessContextImplTest.java @@ -16,12 +16,12 @@ */ package org.apache.seata.saga.proctrl.impl; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + /** * ProcessContextImplTest */ diff --git a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java index e8eacae6796..b4b42bb034b 100644 --- a/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java +++ b/saga/seata-saga-processctrl/src/test/java/org/apache/seata/saga/proctrl/process/impl/CustomizeBusinessProcessorTest.java @@ -16,6 +16,9 @@ */ package org.apache.seata.saga.proctrl.process.impl; +import java.util.HashMap; +import java.util.Map; + import org.apache.seata.common.exception.FrameworkException; import org.apache.seata.saga.proctrl.ProcessContext; import org.apache.seata.saga.proctrl.ProcessType; @@ -25,9 +28,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.util.HashMap; -import java.util.Map; - /** * CustomizeBusinessProcessorTest */ diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java index 0bc082395fb..979676f6f6c 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/config/DbStateMachineConfigTest.java @@ -17,13 +17,14 @@ package org.apache.seata.saga.engine.config; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.SQLException; import static org.mockito.Mockito.when; diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java index fb51a425c26..3d0b4879099 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/invoker/impl/SpringBeanServiceInvokerTest.java @@ -17,6 +17,11 @@ package org.apache.seata.saga.engine.invoker.impl; +import java.util.Collections; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + import org.apache.seata.saga.statelang.domain.impl.AbstractTaskState; import org.apache.seata.saga.statelang.domain.impl.ServiceTaskStateImpl; import org.junit.jupiter.api.Assertions; @@ -24,11 +29,6 @@ import org.mockito.Mockito; import org.springframework.context.ApplicationContext; -import java.util.Collections; -import java.util.concurrent.LinkedBlockingDeque; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.when; diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java index 9a69212dea0..db6b2dc7754 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStoreTest.java @@ -16,6 +16,10 @@ */ package org.apache.seata.saga.engine.store.db; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; + import org.apache.seata.saga.engine.config.DbStateMachineConfig; import org.apache.seata.saga.engine.sequence.UUIDSeqGenerator; import org.apache.seata.saga.proctrl.impl.ProcessContextImpl; @@ -27,10 +31,6 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; - import static org.mockito.ArgumentMatchers.any; /** diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java index c7cb4f5c0ea..75a27a7b809 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/store/db/MockSagaTransactionTemplate.java @@ -16,6 +16,8 @@ */ package org.apache.seata.saga.engine.store.db; +import java.util.Random; + import org.apache.seata.core.exception.TransactionException; import org.apache.seata.core.model.BranchStatus; import org.apache.seata.core.model.GlobalStatus; @@ -25,8 +27,6 @@ import org.apache.seata.tm.api.TransactionalExecutor.ExecutionException; import org.apache.seata.tm.api.transaction.TransactionInfo; -import java.util.Random; - /** * MockSagaTransactionTemplate */ diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java index 3d00284295d..4c06b4b9069 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/tm/DefaultSagaTransactionalTemplateTest.java @@ -16,6 +16,8 @@ */ package org.apache.seata.saga.engine.tm; +import java.util.Collections; + import org.apache.seata.core.model.BranchStatus; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.model.GlobalStatus; @@ -31,8 +33,6 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; -import java.util.Collections; - import static org.mockito.ArgumentMatchers.any; /** diff --git a/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java b/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java index 1e315d353c7..358785d1634 100644 --- a/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java +++ b/saga/seata-saga-statelang/src/test/java/org/apache/seata/saga/statelang/parser/StateParserTests.java @@ -16,6 +16,12 @@ */ package org.apache.seata.saga.statelang.parser; +import java.io.IOException; +import java.io.InputStream; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + import org.apache.seata.common.util.BeanUtils; import org.apache.seata.saga.statelang.domain.StateMachine; import org.apache.seata.saga.statelang.domain.StateMachineInstance; @@ -26,12 +32,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.io.IOException; -import java.io.InputStream; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - /** * StateParser tests */ From 832df211af5a1b47bc3bc8d1acb05f563c86ea71 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Mon, 27 May 2024 12:11:16 +0800 Subject: [PATCH 16/18] fix(6509): adjust cocecov token --- .github/workflows/build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d57645eb419..4d87946ee3e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -53,6 +53,8 @@ jobs: - name: "Codecov" if: matrix.java == '8' uses: codecov/codecov-action@v3.1.4 + with: + token: ${{ secrets.CODECOV_TOKEN }} # job 2: Build on 'arm64v8/ubuntu' OS (Skip tests). build_arm64-binary: From ac7407c35d9f055014dc7a944c8242dae7e2d6af Mon Sep 17 00:00:00 2001 From: funkye <364176773@qq.com> Date: Mon, 27 May 2024 15:34:23 +0800 Subject: [PATCH 17/18] Update .github/workflows/build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4d87946ee3e..b034d20a558 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -52,7 +52,7 @@ jobs: # step 5 - name: "Codecov" if: matrix.java == '8' - uses: codecov/codecov-action@v3.1.4 + uses: codecov/codecov-action@v4.0.1 with: token: ${{ secrets.CODECOV_TOKEN }} From 51f95f945855741a77f4bb9e9de45a16460dbcc4 Mon Sep 17 00:00:00 2001 From: Jingliu Xiong <928124786@qq.com> Date: Sat, 8 Jun 2024 18:00:38 +0800 Subject: [PATCH 18/18] fix(6509): adjust by review --- changes/en-us/2.x.md | 2 +- changes/zh-cn/2.x.md | 2 +- .../engine/expression/spel/SpringELExpressionFactoryTest.java | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 01ccc4b3b23..712deb8d8a9 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -158,7 +158,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6456](https://github.com/apache/incubator-seata/pull/6456)] adjust the test cases related to dynamic configuration - [[#6466](https://github.com/apache/incubator-seata/pull/6466)] support redis integration testing - [[#6484](https://github.com/apache/incubator-seata/pull/6484)] fix FileConfigurationTest and MockServerTest fail -- [[#6519](https://github.com/apache/incubator-seata/pull/6484)] 增加saga模块的测试用例覆盖率 +- [[#6519](https://github.com/apache/incubator-seata/pull/6519)] 增加saga模块的测试用例覆盖率 - [[#6545](https://github.com/apache/incubator-seata/pull/6545)] fix TestConfigCustomSPI compatibility test fail ### refactor: diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 375d86a334d..760115e61e2 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -155,7 +155,7 @@ - [[#6456](https://github.com/apache/incubator-seata/pull/6456)] 调整动态配置监听测试用例 - [[#6466](https://github.com/apache/incubator-seata/pull/6466)] 支持redis的集成测试 - [[#6484](https://github.com/apache/incubator-seata/pull/6484)] 修复FileConfigurationTest和MockServerTest失败 -- [[#6519](https://github.com/apache/incubator-seata/pull/6484)] 增加saga模块的测试用例覆盖率 +- [[#6519](https://github.com/apache/incubator-seata/pull/6519)] 增加saga模块的测试用例覆盖率 - [[#6545](https://github.com/apache/incubator-seata/pull/6545)] 修复 TestConfigCustomSPI 兼容性测试失败 ### refactor: diff --git a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java index 3db7d54e76e..c299095e13f 100644 --- a/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java +++ b/saga/seata-saga-spring/src/test/java/org/apache/seata/saga/engine/expression/spel/SpringELExpressionFactoryTest.java @@ -16,6 +16,7 @@ */ package org.apache.seata.saga.engine.expression.spel; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** @@ -25,6 +26,6 @@ public class SpringELExpressionFactoryTest { @Test public void testCreateExpression() { SpringELExpressionFactory factory = new SpringELExpressionFactory(null); - factory.createExpression("'Hello World'.concat('!')"); + Assertions.assertNotNull(factory.createExpression("'Hello World'.concat('!')")); } } \ No newline at end of file