-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Increase [integration-tx-api] module test case coverage (#6533)
- Loading branch information
1 parent
20cd962
commit 7d3cf99
Showing
17 changed files
with
781 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...java/org/apache/seata/integration/tx/api/interceptor/TwoPhaseBusinessActionParamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> 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()); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...st/java/org/apache/seata/integration/tx/api/interceptor/parser/IfNeedEnhanceBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...gration-tx-api/src/test/java/org/apache/seata/integration/tx/api/json/JsonParserImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> T parseObject(String text, Class<T> clazz) throws IOException { | ||
return mapper.readValue(text, clazz); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "customParser"; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...ion-tx-api/src/test/java/org/apache/seata/integration/tx/api/json/JsonParserWrapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...tx-api/src/test/java/org/apache/seata/integration/tx/api/remoting/TwoPhaseResultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
Oops, something went wrong.