Skip to content

Commit 8722d51

Browse files
authored
test: add some UT for server module (#7750)
1 parent 61e375f commit 8722d51

31 files changed

+6482
-2
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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.server.console.aop;
18+
19+
import org.apache.seata.common.exception.FrameworkException;
20+
import org.apache.seata.common.exception.ShouldNeverHappenException;
21+
import org.apache.seata.common.result.SingleResult;
22+
import org.apache.seata.server.console.exception.ConsoleException;
23+
import org.junit.jupiter.api.Assertions;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
27+
public class GlobalExceptionHandlerAdviceTest {
28+
29+
private GlobalExceptionHandlerAdvice advice;
30+
31+
@BeforeEach
32+
public void setUp() {
33+
advice = new GlobalExceptionHandlerAdvice();
34+
}
35+
36+
@Test
37+
public void testHandlerConsoleException() {
38+
RuntimeException cause = new RuntimeException("Original cause");
39+
String logMessage = "Console error occurred";
40+
ConsoleException exception = new ConsoleException(cause, logMessage);
41+
42+
SingleResult<Void> result = advice.handlerConsoleException(exception);
43+
44+
Assertions.assertNotNull(result);
45+
Assertions.assertFalse(result.isSuccess());
46+
Assertions.assertEquals(exception.getMessage(), result.getMessage());
47+
}
48+
49+
@Test
50+
public void testHandlerConsoleException_WithNullLogMessage() {
51+
RuntimeException cause = new RuntimeException("Original cause");
52+
ConsoleException exception = new ConsoleException(cause, null);
53+
54+
SingleResult<Void> result = advice.handlerConsoleException(exception);
55+
56+
Assertions.assertNotNull(result);
57+
Assertions.assertFalse(result.isSuccess());
58+
}
59+
60+
@Test
61+
public void testHandlerIllegalArgumentException() {
62+
String errorMessage = "Invalid argument provided";
63+
IllegalArgumentException exception = new IllegalArgumentException(errorMessage);
64+
65+
SingleResult<Void> result = advice.handlerIllegalArgumentException(exception);
66+
67+
Assertions.assertNotNull(result);
68+
Assertions.assertFalse(result.isSuccess());
69+
Assertions.assertEquals(errorMessage, result.getMessage());
70+
}
71+
72+
@Test
73+
public void testHandlerIllegalArgumentException_WithNullMessage() {
74+
IllegalArgumentException exception = new IllegalArgumentException();
75+
76+
SingleResult<Void> result = advice.handlerIllegalArgumentException(exception);
77+
78+
Assertions.assertNotNull(result);
79+
Assertions.assertFalse(result.isSuccess());
80+
}
81+
82+
@Test
83+
public void testHandlerIllegalStateException() {
84+
String errorMessage = "Illegal state detected";
85+
IllegalStateException exception = new IllegalStateException(errorMessage);
86+
87+
SingleResult<Void> result = advice.handlerIllegalStateException(exception);
88+
89+
Assertions.assertNotNull(result);
90+
Assertions.assertFalse(result.isSuccess());
91+
Assertions.assertEquals(errorMessage, result.getMessage());
92+
}
93+
94+
@Test
95+
public void testHandlerIllegalStateException_WithNullMessage() {
96+
IllegalStateException exception = new IllegalStateException();
97+
98+
SingleResult<Void> result = advice.handlerIllegalStateException(exception);
99+
100+
Assertions.assertNotNull(result);
101+
Assertions.assertFalse(result.isSuccess());
102+
}
103+
104+
@Test
105+
public void testHandlerShouldNeverHappenException() {
106+
String errorMessage = "This should never happen";
107+
ShouldNeverHappenException exception = new ShouldNeverHappenException(errorMessage);
108+
109+
SingleResult<Void> result = advice.handlerShouldNeverHappenException(exception);
110+
111+
Assertions.assertNotNull(result);
112+
Assertions.assertFalse(result.isSuccess());
113+
Assertions.assertEquals(errorMessage, result.getMessage());
114+
}
115+
116+
@Test
117+
public void testHandlerShouldNeverHappenException_WithCause() {
118+
RuntimeException cause = new RuntimeException("Cause");
119+
ShouldNeverHappenException exception = new ShouldNeverHappenException(cause);
120+
121+
SingleResult<Void> result = advice.handlerShouldNeverHappenException(exception);
122+
123+
Assertions.assertNotNull(result);
124+
Assertions.assertFalse(result.isSuccess());
125+
}
126+
127+
@Test
128+
public void testHandlerFrameworkException() {
129+
String errorMessage = "Framework error occurred";
130+
FrameworkException exception = new FrameworkException(errorMessage);
131+
132+
SingleResult<Void> result = advice.handlerFrameworkException(exception);
133+
134+
Assertions.assertNotNull(result);
135+
Assertions.assertFalse(result.isSuccess());
136+
Assertions.assertEquals(errorMessage, result.getMessage());
137+
}
138+
139+
@Test
140+
public void testHandlerFrameworkException_WithCause() {
141+
RuntimeException cause = new RuntimeException("Root cause");
142+
String errorMessage = "Framework error with cause";
143+
FrameworkException exception = new FrameworkException(cause, errorMessage);
144+
145+
SingleResult<Void> result = advice.handlerFrameworkException(exception);
146+
147+
Assertions.assertNotNull(result);
148+
Assertions.assertFalse(result.isSuccess());
149+
}
150+
151+
@Test
152+
public void testHandleException() {
153+
String errorMessage = "Generic exception occurred";
154+
Exception exception = new Exception(errorMessage);
155+
156+
SingleResult<Void> result = advice.handleException(exception);
157+
158+
Assertions.assertNotNull(result);
159+
Assertions.assertFalse(result.isSuccess());
160+
Assertions.assertEquals(errorMessage, result.getMessage());
161+
}
162+
163+
@Test
164+
public void testHandleException_WithNullMessage() {
165+
Exception exception = new Exception();
166+
167+
SingleResult<Void> result = advice.handleException(exception);
168+
169+
Assertions.assertNotNull(result);
170+
Assertions.assertFalse(result.isSuccess());
171+
}
172+
173+
@Test
174+
public void testHandleException_RuntimeException() {
175+
String errorMessage = "Runtime exception";
176+
RuntimeException exception = new RuntimeException(errorMessage);
177+
178+
SingleResult<Void> result = advice.handleException(exception);
179+
180+
Assertions.assertNotNull(result);
181+
Assertions.assertFalse(result.isSuccess());
182+
Assertions.assertEquals(errorMessage, result.getMessage());
183+
}
184+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.server.console.exception;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
public class ConsoleExceptionTest {
23+
24+
@Test
25+
public void testConsoleException_WithCauseAndLogMessage() {
26+
RuntimeException cause = new RuntimeException("Test cause");
27+
String logMessage = "Test log message";
28+
29+
ConsoleException exception = new ConsoleException(cause, logMessage);
30+
31+
Assertions.assertEquals(cause, exception.getCause());
32+
Assertions.assertEquals(logMessage, exception.getLogMessage());
33+
}
34+
35+
@Test
36+
public void testConsoleException_GetAndSetLogMessage() {
37+
RuntimeException cause = new RuntimeException("Test cause");
38+
String logMessage = "Original log message";
39+
40+
ConsoleException exception = new ConsoleException(cause, logMessage);
41+
Assertions.assertEquals(logMessage, exception.getLogMessage());
42+
43+
String newLogMessage = "New log message";
44+
exception.setLogMessage(newLogMessage);
45+
Assertions.assertEquals(newLogMessage, exception.getLogMessage());
46+
}
47+
48+
@Test
49+
public void testConsoleException_WithNullCause() {
50+
String logMessage = "Test log message";
51+
52+
ConsoleException exception = new ConsoleException(null, logMessage);
53+
54+
Assertions.assertNull(exception.getCause());
55+
Assertions.assertEquals(logMessage, exception.getLogMessage());
56+
}
57+
58+
@Test
59+
public void testConsoleException_WithNullLogMessage() {
60+
RuntimeException cause = new RuntimeException("Test cause");
61+
62+
ConsoleException exception = new ConsoleException(cause, null);
63+
64+
Assertions.assertEquals(cause, exception.getCause());
65+
Assertions.assertNull(exception.getLogMessage());
66+
}
67+
68+
@Test
69+
public void testConsoleException_IsRuntimeException() {
70+
RuntimeException cause = new RuntimeException("Test cause");
71+
String logMessage = "Test log message";
72+
73+
ConsoleException exception = new ConsoleException(cause, logMessage);
74+
75+
Assertions.assertTrue(exception instanceof RuntimeException);
76+
}
77+
}

0 commit comments

Comments
 (0)