From 7d3cf99caea7ce46e77d9248827d0fde5537551e Mon Sep 17 00:00:00 2001 From: azatyamanaev <49018424+azatyamanaev@users.noreply.github.com> Date: Mon, 29 Jul 2024 12:40:25 +0300 Subject: [PATCH] test: Increase [integration-tx-api] module test case coverage (#6533) --- changes/en-us/2.x.md | 4 +- changes/zh-cn/2.x.md | 5 + .../tx/api/remoting/TwoPhaseResult.java | 2 +- .../TwoPhaseBusinessActionParamTest.java | 75 ++++++++++ .../parser/IfNeedEnhanceBeanTest.java | 46 ++++++ .../tx/api/json/JsonParserImpl.java | 41 ++++++ .../tx/api/json/JsonParserWrapTest.java | 77 ++++++++++ .../tx/api/remoting/TwoPhaseResultTest.java | 56 ++++++++ .../parser/DefaultRemotingParserTest.java | 136 ++++++++++++++++++ .../tx/api/remoting/parser/RemoteBean.java | 31 ++++ .../tx/api/remoting/parser/SimpleBean.java | 20 +++ .../api/remoting/parser/SimpleRemoteBean.java | 21 +++ .../remoting/parser/SimpleRemotingParser.java | 69 +++++++++ .../tx/api/util/ClassUtilsTest.java | 87 +++++++++++ .../tx/api/util/DubboUtilTest.java | 55 +++++++ .../tx/api/util/SimpleDubboProxy.java | 21 +++ .../rm/tcc/json/JsonParserFactoryTest.java | 37 +++++ 17 files changed, 781 insertions(+), 2 deletions(-) create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/TwoPhaseBusinessActionParamTest.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/parser/IfNeedEnhanceBeanTest.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/json/JsonParserImpl.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/json/JsonParserWrapTest.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/TwoPhaseResultTest.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/DefaultRemotingParserTest.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/RemoteBean.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleBean.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleRemoteBean.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleRemotingParser.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/ClassUtilsTest.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/DubboUtilTest.java create mode 100644 integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/SimpleDubboProxy.java create mode 100644 tcc/src/test/java/org/apache/seata/rm/tcc/json/JsonParserFactoryTest.java diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 8deec047e5f..d10b32ef63d 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -39,10 +39,10 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6702](https://github.com/apache/incubator-seata/pull/6702)] fix REC security vulnerability during Raft deserialization ### test: +- [[#6533](https://github.com/apache/incubator-seata/pull/6533)] increase integration-tx-api module unit test coverage - [[#6608](https://github.com/apache/incubator-seata/pull/6608)] add unit test for sql-parser-core - [[#6647](https://github.com/apache/incubator-seata/pull/6647)] improve the test case coverage of saga module to 70% - Thanks to these contributors for their code commits. Please report an unintended omission. @@ -57,6 +57,7 @@ Thanks to these contributors for their code commits. Please report an unintended - [traitsisgiorgos](https://github.com/traitsisgiorgos) - [wanghongzhou](https://github.com/wanghongzhou) - [ggbocoder](https://github.com/ggbocoder) +- [azatyamanaev](https://github.com/azatyamanaev) - [xjlgod](https://github.com/xjlgod) - [xingfudeshi](https://github.com/xingfudeshi) - [wuwen5](https://github.com/wuwen5) @@ -64,4 +65,5 @@ Thanks to these contributors for their code commits. Please report an unintended - [GoodBoyCoder](https://github.com/GoodBoyCoder) - [liuqiufeng](https://github.com/liuqiufeng) + Also, we receive many valuable issues, questions and advices from our community. Thanks for you all. diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index d1118bb3d46..bbdaa44f153 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -41,6 +41,7 @@ - [[#6702](https://github.com/apache/incubator-seata/pull/6702)] 修复Raft反序列化时存在RCE安全漏洞 ### test: +- [[#6533](https://github.com/apache/incubator-seata/pull/6533)] 增加 Integration-TX-API 模块单元测试覆盖范围 - [[#6608](https://github.com/apache/incubator-seata/pull/6608)] 添加sql-parser-core模块测试用例 - [[#6647](https://github.com/apache/incubator-seata/pull/6647)] 增加saga模块的测试用例覆盖率 @@ -60,6 +61,7 @@ - [traitsisgiorgos](https://github.com/traitsisgiorgos) - [wanghongzhou](https://github.com/wanghongzhou) - [ggbocoder](https://github.com/ggbocoder) +- [azatyamanaev](https://github.com/azatyamanaev) - [xjlgod](https://github.com/xjlgod) - [xingfudeshi](https://github.com/xingfudeshi) - [wuwen5](https://github.com/wuwen5) @@ -67,4 +69,7 @@ - [GoodBoyCoder](https://github.com/GoodBoyCoder) - [liuqiufeng](https://github.com/liuqiufeng) + + 同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。 + diff --git a/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/remoting/TwoPhaseResult.java b/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/remoting/TwoPhaseResult.java index 55a8433c4e5..e21680e7be4 100644 --- a/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/remoting/TwoPhaseResult.java +++ b/integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/remoting/TwoPhaseResult.java @@ -87,7 +87,7 @@ public String toString() { sb.append("["); sb.append("isSuccess:").append(isSuccess); if (StringUtils.isNotBlank(message)) { - sb.append(", msg").append(message); + sb.append(", msg:").append(message); } sb.append("]"); return sb.toString(); diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/TwoPhaseBusinessActionParamTest.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/TwoPhaseBusinessActionParamTest.java new file mode 100644 index 00000000000..ae136a60b61 --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/TwoPhaseBusinessActionParamTest.java @@ -0,0 +1,75 @@ +/* + * 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.integration.tx.api.interceptor; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.seata.common.Constants; +import org.apache.seata.core.model.BranchType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TwoPhaseBusinessActionParamTest { + + private TwoPhaseBusinessActionParam actionParam; + + @BeforeEach + public void setUp() { + actionParam = new TwoPhaseBusinessActionParam(); + } + + @Test + public void testGetActionName() { + actionParam.setActionName("business_action"); + assertEquals("business_action", actionParam.getActionName()); + } + + @Test + public void testIsReportDelayed() { + actionParam.setDelayReport(true); + assertTrue(actionParam.getDelayReport()); + } + + @Test + public void testIsCommonFenceUsed() { + actionParam.setUseCommonFence(true); + assertTrue(actionParam.getUseCommonFence()); + } + + @Test + public void testFillBusinessActionContext() { + Map businessActionContextMap = new HashMap<>(2); + businessActionContextMap.put(Constants.COMMIT_METHOD, "commit"); + businessActionContextMap.put(Constants.USE_COMMON_FENCE, false); + + actionParam.setBusinessActionContext(businessActionContextMap); + + assertEquals("commit", actionParam.getBusinessActionContext().get(Constants.COMMIT_METHOD)); + assertFalse((Boolean) actionParam.getBusinessActionContext().get(Constants.USE_COMMON_FENCE)); + } + + @Test + public void testGetBranchType() { + actionParam.setBranchType(BranchType.TCC); + assertEquals(BranchType.TCC, actionParam.getBranchType()); + } +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/parser/IfNeedEnhanceBeanTest.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/parser/IfNeedEnhanceBeanTest.java new file mode 100644 index 00000000000..cc8843301d6 --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/parser/IfNeedEnhanceBeanTest.java @@ -0,0 +1,46 @@ +/* + * 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.integration.tx.api.interceptor.parser; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class IfNeedEnhanceBeanTest { + + private IfNeedEnhanceBean enhanceBean; + + + @BeforeEach + public void setUp() { + enhanceBean = new IfNeedEnhanceBean(); + } + + @Test + public void testIsEnhanceNeeded() { + enhanceBean.setIfNeed(true); + assertTrue(enhanceBean.isIfNeed()); + } + + @Test + public void testGetNeedEnhanceEnum() { + enhanceBean.setNeedEnhanceEnum(NeedEnhanceEnum.SERVICE_BEAN); + assertEquals(NeedEnhanceEnum.SERVICE_BEAN, enhanceBean.getNeedEnhanceEnum()); + } +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/json/JsonParserImpl.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/json/JsonParserImpl.java new file mode 100644 index 00000000000..64ee066af6a --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/json/JsonParserImpl.java @@ -0,0 +1,41 @@ +/* + * 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.integration.tx.api.json; + +import java.io.IOException; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonParserImpl implements JsonParser { + + private final ObjectMapper mapper = new ObjectMapper(); + + @Override + public String toJSONString(Object object) throws IOException { + return mapper.writeValueAsString(object); + } + + @Override + public T parseObject(String text, Class clazz) throws IOException { + return mapper.readValue(text, clazz); + } + + @Override + public String getName() { + return "customParser"; + } +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/json/JsonParserWrapTest.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/json/JsonParserWrapTest.java new file mode 100644 index 00000000000..50073bdc32a --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/json/JsonParserWrapTest.java @@ -0,0 +1,77 @@ +/* + * 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.integration.tx.api.json; + +import org.apache.seata.common.exception.JsonParseException; +import org.apache.seata.core.model.BranchType; +import org.apache.seata.integration.tx.api.interceptor.TwoPhaseBusinessActionParam; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class JsonParserWrapTest { + + private JsonParserWrap parserWrap; + private final String jsonString = "{\"actionName\":\"business_action\",\"useCommonFence\":null,\"businessActionContext\":null," + + "\"branchType\":\"TCC\",\"delayReport\":null}"; + + + @BeforeEach + public void setUp() { + parserWrap = new JsonParserWrap(new JsonParserImpl()); + } + + @Test + public void testToJSONString() { + TwoPhaseBusinessActionParam actionParam = new TwoPhaseBusinessActionParam(); + actionParam.setActionName("business_action"); + actionParam.setBranchType(BranchType.TCC); + + String resultString = parserWrap.toJSONString(actionParam); + + assertEquals(jsonString, resultString); + } + + @Test + public void testToJSONStringThrowsException() { + Object mockItem = mock(Object.class); + when(mockItem.toString()).thenReturn(mockItem.getClass().getName()); + assertThrows(JsonParseException.class, () -> parserWrap.toJSONString(mockItem)); + } + + @Test + public void testParseObject() { + TwoPhaseBusinessActionParam actionParam = parserWrap.parseObject(jsonString, TwoPhaseBusinessActionParam.class); + + assertEquals("business_action", actionParam.getActionName()); + assertEquals(BranchType.TCC, actionParam.getBranchType()); + } + + @Test + public void testParseObjectThrowsException() { + assertThrows(JsonParseException.class, () -> parserWrap.parseObject(jsonString, Integer.class)); + } + + @Test + public void testGetName() { + assertEquals("customParser", parserWrap.getName()); + } +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/TwoPhaseResultTest.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/TwoPhaseResultTest.java new file mode 100644 index 00000000000..c5d67afd5b7 --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/TwoPhaseResultTest.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.integration.tx.api.remoting; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TwoPhaseResultTest { + + private TwoPhaseResult result; + + @BeforeEach + public void setUp() { + result = new TwoPhaseResult(false, ""); + } + + @Test + public void testGetMessage() { + result.setMessage("message"); + assertEquals("message", result.getMessage()); + } + + @Test + public void testIsSuccess() { + result.setSuccess(true); + assertTrue(result.isSuccess()); + } + + @Test + public void testToStringEmptyMessage() { + assertEquals("[isSuccess:false]", result.toString()); + } + + @Test + public void testToStringNotEmptyMessage() { + result.setMessage("test"); + assertEquals("[isSuccess:false, msg:test]", result.toString()); + } +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/DefaultRemotingParserTest.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/DefaultRemotingParserTest.java new file mode 100644 index 00000000000..7cdc2456ee4 --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/DefaultRemotingParserTest.java @@ -0,0 +1,136 @@ +/* + * 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.integration.tx.api.remoting.parser; + +import org.apache.seata.integration.tx.api.remoting.Protocols; +import org.apache.seata.integration.tx.api.remoting.RemotingDesc; +import org.apache.seata.integration.tx.api.remoting.RemotingParser; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DefaultRemotingParserTest { + + private static final DefaultRemotingParser remotingParser = DefaultRemotingParser.get(); + + @BeforeAll + public static void setUp() { + remotingParser.registerRemotingParser(new SimpleRemotingParser()); + } + + @Test + public void testIsRemoting() { + SimpleRemoteBean remoteBean = new SimpleRemoteBean(); + RemotingParser parser = remotingParser.isRemoting(remoteBean, remoteBean.getClass().getName()); + assertInstanceOf(SimpleRemotingParser.class, parser); + } + + @Test + public void testIsRemotingFail() { + SimpleBean remoteBean = new SimpleBean(); + assertNull(remotingParser.isRemoting(remoteBean, remoteBean.getClass().getName())); + } + + @Test + public void testIsReference() { + SimpleRemoteBean remoteBean = new SimpleRemoteBean(); + assertTrue(remotingParser.isReference(remoteBean, remoteBean.getClass().getName())); + } + + @Test + public void testIsReferenceFail() { + SimpleBean remoteBean = new SimpleBean(); + assertFalse(remotingParser.isReference(remoteBean, remoteBean.getClass().getName())); + } + + @Test + public void testIsServiceFromObject() { + SimpleRemoteBean remoteBean = new SimpleRemoteBean(); + assertTrue(remotingParser.isService(remoteBean, remoteBean.getClass().getName())); + } + + @Test + public void testIsServiceFromObjectFail() { + SimpleBean remoteBean = new SimpleBean(); + assertFalse(remotingParser.isService(remoteBean, remoteBean.getClass().getName())); + } + + @Test + public void testIsServiceFromClass() { + assertTrue(remotingParser.isService(SimpleRemoteBean.class)); + } + + @Test + public void testIsServiceFromClassFail() { + assertFalse(remotingParser.isService(SimpleBean.class)); + } + + @Test + public void testGetServiceDesc() { + SimpleRemoteBean remoteBean = new SimpleRemoteBean(); + RemotingDesc desc = remotingParser.getServiceDesc(remoteBean, remoteBean.getClass().getName()); + assertEquals(Protocols.IN_JVM, desc.getProtocol()); + assertEquals(SimpleRemoteBean.class, desc.getServiceClass()); + } + + @Test + public void testGetServiceDescFail() { + SimpleBean simpleBean = new SimpleBean(); + assertNull(remotingParser.getServiceDesc(simpleBean, simpleBean.getClass().getName())); + } + + @Test + public void testParserRemotingServiceInfo() { + SimpleRemoteBean remoteBean = new SimpleRemoteBean(); + SimpleRemotingParser parser = new SimpleRemotingParser(); + RemotingDesc desc = remotingParser.parserRemotingServiceInfo(remoteBean, remoteBean.getClass().getName(), + parser); + + assertEquals(desc, remotingParser.parserRemotingServiceInfo(remoteBean, remoteBean.getClass().getName(), + parser)); + assertEquals(Protocols.IN_JVM, desc.getProtocol()); + assertEquals(SimpleRemoteBean.class, desc.getServiceClass()); + } + + @Test + public void testParserRemotingServiceInfoFail() { + SimpleBean simpleBean = new SimpleBean(); + assertNull(remotingParser.parserRemotingServiceInfo(simpleBean, simpleBean.getClass().getName(), + new SimpleRemotingParser())); + } + + @Test + public void testGetRemotingBeanDesc() { + SimpleRemoteBean remoteBean = new SimpleRemoteBean(); + remotingParser.parserRemotingServiceInfo(remoteBean, remoteBean.getClass().getName(), + new SimpleRemotingParser()); + + assertNotNull(remotingParser.getRemotingBeanDesc(remoteBean)); + } + + @Test + public void testGetRemotingDeanDescFail() { + SimpleBean simpleBean = new SimpleBean(); + assertNull(remotingParser.getRemotingBeanDesc(simpleBean)); + } +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/RemoteBean.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/RemoteBean.java new file mode 100644 index 00000000000..df0edd37701 --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/RemoteBean.java @@ -0,0 +1,31 @@ +/* + * 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.integration.tx.api.remoting.parser; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * annotation for DefaultRemotingParser test + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface RemoteBean { +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleBean.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleBean.java new file mode 100644 index 00000000000..7d672afc7f6 --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleBean.java @@ -0,0 +1,20 @@ +/* + * 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.integration.tx.api.remoting.parser; + +public class SimpleBean { +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleRemoteBean.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleRemoteBean.java new file mode 100644 index 00000000000..164457aad7c --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleRemoteBean.java @@ -0,0 +1,21 @@ +/* + * 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.integration.tx.api.remoting.parser; + +@RemoteBean +public class SimpleRemoteBean { +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleRemotingParser.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleRemotingParser.java new file mode 100644 index 00000000000..8a8454eb488 --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/parser/SimpleRemotingParser.java @@ -0,0 +1,69 @@ +/* + * 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.integration.tx.api.remoting.parser; + +import org.apache.seata.common.exception.FrameworkException; +import org.apache.seata.integration.tx.api.remoting.Protocols; +import org.apache.seata.integration.tx.api.remoting.RemotingDesc; + +public class SimpleRemotingParser extends AbstractedRemotingParser { + + @Override + public boolean isReference(Object bean, String beanName) throws FrameworkException { + return isRemoteBean(bean); + } + + @Override + public boolean isService(Object bean, String beanName) throws FrameworkException { + return isRemoteBean(bean); + } + + @Override + public boolean isService(Class beanClass) throws FrameworkException { + return isRemoteBean(beanClass); + } + + @Override + public RemotingDesc getServiceDesc(Object bean, String beanName) throws FrameworkException { + if (!this.isRemoting(bean, beanName)) { + return null; + } + + RemotingDesc remotingDesc = new RemotingDesc(); + remotingDesc.setReference(this.isReference(bean, beanName)); + remotingDesc.setService(this.isService(bean, beanName)); + remotingDesc.setProtocol(Protocols.IN_JVM); + remotingDesc.setServiceClass(bean.getClass()); + remotingDesc.setServiceClassName(remotingDesc.getServiceClass().getName()); + remotingDesc.setTargetBean(bean); + + return remotingDesc; + } + + @Override + public short getProtocol() { + return Protocols.IN_JVM; + } + + private boolean isRemoteBean(Object bean) { + return isRemoteBean(bean.getClass()); + } + + private boolean isRemoteBean(Class clazz) { + return clazz.isAnnotationPresent(RemoteBean.class); + } +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/ClassUtilsTest.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/ClassUtilsTest.java new file mode 100644 index 00000000000..583bc397f3c --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/ClassUtilsTest.java @@ -0,0 +1,87 @@ +/* + * 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.integration.tx.api.util; + +import java.lang.reflect.Method; +import java.util.AbstractList; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +public class ClassUtilsTest { + + @Test + public void testGetPackageName() { + String packageName = ClassUtils.getPackageName(AbstractMap.class); + assertEquals("java.util", packageName); + } + + @Test + public void testGetPackageNameForSimpleName() { + String packageName = ClassUtils.getPackageName(AbstractMap.class.getSimpleName()); + assertEquals("", packageName); + } + + @Test + public void testGetMostSpecificMethodWhenClassIsNull() throws NoSuchMethodException { + Method method = AbstractMap.class.getDeclaredMethod("clone"); + Method specificMethod = ClassUtils.getMostSpecificMethod(method, null); + assertEquals(AbstractMap.class, specificMethod.getDeclaringClass()); + } + + @Test + public void testGetMostSpecificMethodFromSameClass() throws NoSuchMethodException { + Method method = AbstractMap.class.getDeclaredMethod("clone"); + Method specificMethod = ClassUtils.getMostSpecificMethod(method, AbstractMap.class); + assertEquals(AbstractMap.class, specificMethod.getDeclaringClass()); + } + + @Test + public void testGetMostSpecificNotPublicMethod() throws NoSuchMethodException { + Method method = AbstractMap.class.getDeclaredMethod("clone"); + Method specificMethod = ClassUtils.getMostSpecificMethod(method, HashMap.class); + assertNotEquals(HashMap.class.getDeclaredMethod("clone"), specificMethod); + } + + @Test + public void testGetMostSpecificPublicMethod() throws NoSuchMethodException { + Method method = Map.class.getDeclaredMethod("remove", Object.class, Object.class); + Method specificMethod = ClassUtils.getMostSpecificMethod(method, HashMap.class); + assertEquals(HashMap.class.getDeclaredMethod("remove", Object.class, Object.class), specificMethod); + } + + @Test + public void testGetMostSpecificPrivateMethod() throws NoSuchMethodException { + Method method = AbstractList.class.getDeclaredMethod("rangeCheckForAdd", int.class); + Method specificMethod = ClassUtils.getMostSpecificMethod(method, ArrayList.class); + assertNotEquals(ArrayList.class.getDeclaredMethod("rangeCheckForAdd", int.class), specificMethod); + } + + @Test + public void testGetMostSpecificMethodWhichNotExistsInTargetClass() throws NoSuchMethodException { + Method method = ArrayList.class.getDeclaredMethod("sort", Comparator.class); + Method specificMethod = ClassUtils.getMostSpecificMethod(method, Map.class); + assertEquals(ArrayList.class.getDeclaredMethod("sort", Comparator.class), specificMethod); + } +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/DubboUtilTest.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/DubboUtilTest.java new file mode 100644 index 00000000000..1b093ce1111 --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/DubboUtilTest.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.integration.tx.api.util; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DubboUtilTest { + + @Test + public void testIsDubbo3XPartialProxyName() { + assertTrue(DubboUtil.isDubboProxyName(SimpleDubboProxy.class.getName())); + } + + @Test + public void testIsNotDubboProxyName() { + assertFalse(DubboUtil.isDubboProxyName(ArrayList.class.getName())); + } + + @Test + public void testGetAssistInterfaceForNull() throws NoSuchFieldException, InvocationTargetException, IllegalAccessException, NoSuchMethodException { + assertNull(DubboUtil.getAssistInterface(null)); + } + + @Test + public void testGetAssistInterfaceForNotDubboProxy() throws NoSuchFieldException, InvocationTargetException, IllegalAccessException, NoSuchMethodException { + assertNull(DubboUtil.getAssistInterface(new ArrayList<>())); + } + + @Test + public void testGetAssistInterfaceThrowsException() { + assertThrows(NoSuchFieldException.class, () -> DubboUtil.getAssistInterface(new SimpleDubboProxy())); + } +} diff --git a/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/SimpleDubboProxy.java b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/SimpleDubboProxy.java new file mode 100644 index 00000000000..867f91699d6 --- /dev/null +++ b/integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/util/SimpleDubboProxy.java @@ -0,0 +1,21 @@ +/* + * 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.integration.tx.api.util; + +public class SimpleDubboProxy { + +} diff --git a/tcc/src/test/java/org/apache/seata/rm/tcc/json/JsonParserFactoryTest.java b/tcc/src/test/java/org/apache/seata/rm/tcc/json/JsonParserFactoryTest.java new file mode 100644 index 00000000000..baaabc41125 --- /dev/null +++ b/tcc/src/test/java/org/apache/seata/rm/tcc/json/JsonParserFactoryTest.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.rm.tcc.json; + +import org.apache.seata.common.loader.EnhancedServiceNotFoundException; +import org.apache.seata.integration.tx.api.json.JsonParserFactory; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class JsonParserFactoryTest { + + @Test + public void testGetInstance() { + assertNotNull(JsonParserFactory.getInstance("jackson")); + } + + @Test + public void testGetInstanceThrowsException() { + assertThrows(EnhancedServiceNotFoundException.class, () -> JsonParserFactory.getInstance("jsonParser")); + } +}