Skip to content

Commit bd5afab

Browse files
authored
test: add unit tests for saga-engine low coverage components (#7915)
1 parent 9b2158b commit bd5afab

File tree

13 files changed

+1996
-0
lines changed

13 files changed

+1996
-0
lines changed

changes/en-us/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Add changes here for all PR submitted to the 2.x branch.
5252

5353
- [[#7962](https://github.com/apache/incubator-seata/pull/7962)] add unit tests for NacosRegistryProvider and NacosRegistryServiceImpl
5454
- [[#8003](https://github.com/apache/incubator-seata/pull/8003)] Enhance NacosRegistryServiceImplTest with additional mocks for service name group and cluster
55+
- [[#7915](https://github.com/apache/incubator-seata/pull/7915)] add unit tests for saga-engine low coverage components
5556

5657
### refactor:
5758

changes/zh-cn/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
- [[#7962](https://github.com/apache/incubator-seata/pull/7962)] 为 NacosRegistryProvider 和 NacosRegistryServiceImpl 添加单元测试用例
5959
- [[#8003](https://github.com/apache/incubator-seata/pull/8003)] 为 NacosRegistryServiceImplTest 增加服务名称、分组和集群的额外模拟
60+
- [[#7915](https://github.com/apache/incubator-seata/pull/7915)] 为 saga-engine 添加单元测试用例
6061

6162

6263
### refactor:
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.saga.engine.exception;
18+
19+
import org.apache.seata.common.exception.FrameworkErrorCode;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
import static org.junit.jupiter.api.Assertions.assertSame;
25+
26+
/**
27+
* Test class for {@link EngineExecutionException}
28+
*/
29+
public class EngineExecutionExceptionTest {
30+
31+
@Test
32+
public void defaultConstructorTest() {
33+
EngineExecutionException e = new EngineExecutionException();
34+
assertNotNull(e);
35+
// Parent class FrameworkException default constructor sets the default message
36+
assertNotNull(e.getMessage());
37+
}
38+
39+
@Test
40+
public void constructorWithErrorCodeTest() {
41+
EngineExecutionException e = new EngineExecutionException(FrameworkErrorCode.UnknownAppError);
42+
assertNotNull(e);
43+
assertEquals(FrameworkErrorCode.UnknownAppError, e.getErrcode());
44+
}
45+
46+
@Test
47+
public void constructorWithMessageTest() {
48+
EngineExecutionException e = new EngineExecutionException("Test error message");
49+
assertEquals("Test error message", e.getMessage());
50+
}
51+
52+
@Test
53+
public void constructorWithMessageAndErrorCodeTest() {
54+
EngineExecutionException e = new EngineExecutionException("Test error", FrameworkErrorCode.UnknownAppError);
55+
assertEquals("Test error", e.getMessage());
56+
assertEquals(FrameworkErrorCode.UnknownAppError, e.getErrcode());
57+
}
58+
59+
@Test
60+
public void constructorWithCauseTest() {
61+
Throwable cause = new RuntimeException("root cause");
62+
EngineExecutionException e = new EngineExecutionException(cause);
63+
assertSame(cause, e.getCause());
64+
}
65+
66+
@Test
67+
public void constructorWithCauseAndMessageTest() {
68+
Throwable cause = new RuntimeException("root cause");
69+
EngineExecutionException e = new EngineExecutionException(cause, "wrapper message");
70+
assertEquals("wrapper message", e.getMessage());
71+
assertSame(cause, e.getCause());
72+
}
73+
74+
@Test
75+
public void constructorWithCauseMessageAndErrorCodeTest() {
76+
Throwable cause = new RuntimeException("root cause");
77+
EngineExecutionException e =
78+
new EngineExecutionException(cause, "wrapper message", FrameworkErrorCode.UnknownAppError);
79+
assertEquals("wrapper message", e.getMessage());
80+
assertSame(cause, e.getCause());
81+
assertEquals(FrameworkErrorCode.UnknownAppError, e.getErrcode());
82+
}
83+
84+
@Test
85+
public void stateNameGetterSetterTest() {
86+
EngineExecutionException e = new EngineExecutionException();
87+
e.setStateName("TestState");
88+
assertEquals("TestState", e.getStateName());
89+
}
90+
91+
@Test
92+
public void stateMachineNameGetterSetterTest() {
93+
EngineExecutionException e = new EngineExecutionException();
94+
e.setStateMachineName("TestMachine");
95+
assertEquals("TestMachine", e.getStateMachineName());
96+
}
97+
98+
@Test
99+
public void stateMachineInstanceIdGetterSetterTest() {
100+
EngineExecutionException e = new EngineExecutionException();
101+
e.setStateMachineInstanceId("instance-123");
102+
assertEquals("instance-123", e.getStateMachineInstanceId());
103+
}
104+
105+
@Test
106+
public void stateInstanceIdGetterSetterTest() {
107+
EngineExecutionException e = new EngineExecutionException();
108+
e.setStateInstanceId("state-456");
109+
assertEquals("state-456", e.getStateInstanceId());
110+
}
111+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.saga.engine.exception;
18+
19+
import org.apache.seata.common.exception.FrameworkErrorCode;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
import static org.junit.jupiter.api.Assertions.assertSame;
25+
26+
/**
27+
* Test class for {@link ForwardInvalidException}
28+
*/
29+
public class ForwardInvalidExceptionTest {
30+
31+
@Test
32+
public void defaultConstructorTest() {
33+
ForwardInvalidException e = new ForwardInvalidException();
34+
assertNotNull(e);
35+
}
36+
37+
@Test
38+
public void constructorWithErrorCodeTest() {
39+
ForwardInvalidException e = new ForwardInvalidException(FrameworkErrorCode.UnknownAppError);
40+
assertEquals(FrameworkErrorCode.UnknownAppError, e.getErrcode());
41+
}
42+
43+
@Test
44+
public void constructorWithMessageTest() {
45+
ForwardInvalidException e = new ForwardInvalidException("forward invalid");
46+
assertEquals("forward invalid", e.getMessage());
47+
}
48+
49+
@Test
50+
public void constructorWithMessageAndErrorCodeTest() {
51+
ForwardInvalidException e = new ForwardInvalidException("forward invalid", FrameworkErrorCode.UnknownAppError);
52+
assertEquals("forward invalid", e.getMessage());
53+
assertEquals(FrameworkErrorCode.UnknownAppError, e.getErrcode());
54+
}
55+
56+
@Test
57+
public void constructorWithCauseMessageAndErrorCodeTest() {
58+
Throwable cause = new RuntimeException("root");
59+
ForwardInvalidException e =
60+
new ForwardInvalidException(cause, "forward invalid", FrameworkErrorCode.UnknownAppError);
61+
assertSame(cause, e.getCause());
62+
assertEquals("forward invalid", e.getMessage());
63+
assertEquals(FrameworkErrorCode.UnknownAppError, e.getErrcode());
64+
}
65+
66+
@Test
67+
public void constructorWithCauseTest() {
68+
Throwable cause = new RuntimeException("root");
69+
ForwardInvalidException e = new ForwardInvalidException(cause);
70+
assertSame(cause, e.getCause());
71+
}
72+
73+
@Test
74+
public void constructorWithCauseAndMessageTest() {
75+
Throwable cause = new RuntimeException("root");
76+
ForwardInvalidException e = new ForwardInvalidException(cause, "forward invalid");
77+
assertEquals("forward invalid", e.getMessage());
78+
assertSame(cause, e.getCause());
79+
}
80+
81+
@Test
82+
public void inheritanceFromEngineExecutionExceptionTest() {
83+
ForwardInvalidException e = new ForwardInvalidException("test");
84+
e.setStateName("TestState");
85+
e.setStateMachineName("TestMachine");
86+
assertEquals("TestState", e.getStateName());
87+
assertEquals("TestMachine", e.getStateMachineName());
88+
}
89+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.saga.engine.expression.exception;
18+
19+
import org.apache.seata.saga.engine.exception.EngineExecutionException;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.io.IOException;
23+
24+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertNull;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
28+
29+
/**
30+
* Test class for {@link ExceptionMatchExpression}
31+
*/
32+
public class ExceptionMatchExpressionTest {
33+
34+
@Test
35+
public void getValueWhenExactClassNameMatchReturnTrueTest() {
36+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
37+
expr.setExpressionString("java.lang.RuntimeException");
38+
Object result = expr.getValue(new RuntimeException("test"));
39+
assertEquals(true, result);
40+
}
41+
42+
@Test
43+
public void getValueWhenSubclassMatchReturnTrueTest() {
44+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
45+
expr.setExpressionString("java.lang.Exception");
46+
Object result = expr.getValue(new RuntimeException("test"));
47+
assertEquals(true, result);
48+
}
49+
50+
@Test
51+
public void getValueWhenNoMatchReturnFalseTest() {
52+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
53+
expr.setExpressionString("java.io.IOException");
54+
Object result = expr.getValue(new RuntimeException("test"));
55+
assertEquals(false, result);
56+
}
57+
58+
@Test
59+
public void getValueWhenContextIsNotExceptionReturnFalseTest() {
60+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
61+
expr.setExpressionString("java.lang.RuntimeException");
62+
Object result = expr.getValue("not an exception");
63+
assertEquals(false, result);
64+
}
65+
66+
@Test
67+
public void getValueWhenExpressionStringEmptyReturnFalseTest() {
68+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
69+
// expressionString is null by default
70+
Object result = expr.getValue(new RuntimeException());
71+
assertEquals(false, result);
72+
}
73+
74+
@Test
75+
public void getValueWhenContextIsNullReturnFalseTest() {
76+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
77+
expr.setExpressionString("java.lang.RuntimeException");
78+
Object result = expr.getValue(null);
79+
assertEquals(false, result);
80+
}
81+
82+
@Test
83+
public void setExpressionStringWithValidExceptionClassSetClassTest() {
84+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
85+
expr.setExpressionString("java.lang.RuntimeException");
86+
assertEquals("java.lang.RuntimeException", expr.getExpressionString());
87+
}
88+
89+
@Test
90+
public void setExpressionStringWithIOExceptionClassTest() {
91+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
92+
expr.setExpressionString("java.io.IOException");
93+
assertEquals("java.io.IOException", expr.getExpressionString());
94+
95+
// Test matching
96+
Object result = expr.getValue(new IOException("test"));
97+
assertEquals(true, result);
98+
}
99+
100+
@Test
101+
public void setExpressionStringWithInvalidClassThrowEngineExecutionExceptionTest() {
102+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
103+
assertThrows(EngineExecutionException.class, () -> expr.setExpressionString("com.nonexistent.NotAnException"));
104+
}
105+
106+
@Test
107+
public void setValueDoNothingTest() {
108+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
109+
// setValue is a no-op, just verify it doesn't throw
110+
assertDoesNotThrow(() -> expr.setValue("value", "context"));
111+
}
112+
113+
@Test
114+
public void getExpressionStringWhenNotSetReturnNullTest() {
115+
ExceptionMatchExpression expr = new ExceptionMatchExpression();
116+
assertNull(expr.getExpressionString());
117+
}
118+
}

0 commit comments

Comments
 (0)