-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathExceptionHelperTest.java
More file actions
204 lines (180 loc) · 7.11 KB
/
ExceptionHelperTest.java
File metadata and controls
204 lines (180 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.datadog.debugger.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
import datadog.logging.RatelimitedLogger;
import java.util.ArrayDeque;
import java.util.Deque;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.Logger;
@ExtendWith(MockitoExtension.class)
public class ExceptionHelperTest {
@Mock Logger logger;
@Mock RatelimitedLogger ratelimitedLogger;
@Test
public void testWarn() {
when(logger.isDebugEnabled()).thenReturn(false);
doAnswer(this::logWarn).when(logger).warn(anyString(), ArgumentMatchers.<Object[]>any());
ExceptionHelper.logException(
logger, new RuntimeException("this is an exception"), "Error {} {}", "param1", "param2");
}
@Test
public void testDebug() {
when(logger.isDebugEnabled()).thenReturn(true);
doAnswer(this::logDebug).when(logger).debug(anyString(), ArgumentMatchers.<Object[]>any());
ExceptionHelper.logException(
logger, new RuntimeException("this is an exception"), "Error {} {}", "param1", "param2");
}
@Test
public void rateLimitWarn() {
when(logger.isDebugEnabled()).thenReturn(false);
doAnswer(this::logWarn)
.when(ratelimitedLogger)
.warn(anyString(), ArgumentMatchers.<Object[]>any());
ExceptionHelper.rateLimitedLogException(
ratelimitedLogger,
logger,
new RuntimeException("this is an exception"),
"Error {} {}",
"param1",
"param2");
}
@Test
public void rateLimitDebug() {
when(logger.isDebugEnabled()).thenReturn(true);
doAnswer(this::logDebug).when(logger).debug(anyString(), ArgumentMatchers.<Object[]>any());
ExceptionHelper.rateLimitedLogException(
ratelimitedLogger,
logger,
new RuntimeException("this is an exception"),
"Error {} {}",
"param1",
"param2");
}
@Test
public void foldExceptionStackTrace() {
String strStackTrace = ExceptionHelper.foldExceptionStackTrace(new Exception());
assertTrue(
strStackTrace.startsWith(
"java.lang.Exception at com.datadog.debugger.util.ExceptionHelperTest.foldExceptionStackTrace(ExceptionHelperTest.java:74) at "),
strStackTrace);
assertFalse(strStackTrace.contains("\n"));
assertFalse(strStackTrace.contains("\t"));
assertFalse(strStackTrace.contains("\r"));
}
@Test
public void innerMostException() {
Exception ex = new RuntimeException("test1");
Throwable innerMost = ExceptionHelper.getInnerMostThrowable(ex);
assertEquals(ex, innerMost);
Exception nested = new RuntimeException("test3", new RuntimeException("test2", ex));
innerMost = ExceptionHelper.getInnerMostThrowable(nested);
assertEquals(ex, innerMost);
Deque<Throwable> exceptions = new ArrayDeque<>();
innerMost = ExceptionHelper.getInnerMostThrowable(nested, exceptions);
assertEquals(ex, innerMost);
assertEquals(3, exceptions.size());
assertEquals(nested, exceptions.pollLast());
assertEquals(nested.getCause(), exceptions.pollLast());
assertEquals(nested.getCause().getCause(), exceptions.pollLast());
}
@Test
public void flattenStackTrace() {
Throwable simpleException =
new MockException(
"oops!",
new StackTraceElement[] {
new StackTraceElement("MyClass1", "myMethod1", "file1.java", 1)
});
Throwable nestedException =
new MockException(
"oops!",
new StackTraceElement[] {
new StackTraceElement("MyClass2", "myMethod2", "file2.java", 2)
},
simpleException);
StackTraceElement[] stack = ExceptionHelper.flattenStackTrace(simpleException);
assertEquals(1, stack.length);
stack = ExceptionHelper.flattenStackTrace(nestedException);
assertEquals(2, stack.length);
}
@Test
public void createThrowableMapping() {
Throwable nestedException = createNestException();
Throwable innerMostThrowable = ExceptionHelper.getInnerMostThrowable(nestedException);
int[] mapping = ExceptionHelper.createThrowableMapping(innerMostThrowable, nestedException);
StackTraceElement[] flattenedTrace = ExceptionHelper.flattenStackTrace(nestedException);
for (int i = 0; i < mapping.length; i++) {
assertEquals(
flattenedTrace[mapping[i]].getClassName(),
innerMostThrowable.getStackTrace()[i].getClassName());
}
}
@Test
public void createThrowableMappingRecursive() {
Throwable nestedException = createNestExceptionRecursive(4);
Throwable innerMostThrowable = ExceptionHelper.getInnerMostThrowable(nestedException);
int[] mapping = ExceptionHelper.createThrowableMapping(innerMostThrowable, nestedException);
StackTraceElement[] flattenedTrace = ExceptionHelper.flattenStackTrace(nestedException);
for (int i = 0; i < mapping.length; i++) {
assertEquals(
flattenedTrace[mapping[i]].getClassName(),
innerMostThrowable.getStackTrace()[i].getClassName());
}
}
private RuntimeException createNestExceptionRecursive(int depth) {
if (depth == 0) {
return createNestException();
}
return createNestExceptionRecursive(depth - 1);
}
private RuntimeException createNestException() {
return new RuntimeException("test3", createTest2Exception(createTest1Exception()));
}
private RuntimeException createTest1Exception() {
return new RuntimeException("test1");
}
private RuntimeException createTest2Exception(Throwable cause) {
return new RuntimeException("test2", cause);
}
private static class MockException extends Exception {
private final StackTraceElement[] stackTrace;
public MockException(String message, StackTraceElement[] stackTrace) {
this(message, stackTrace, null);
}
public MockException(String message, StackTraceElement[] stackTrace, Throwable cause) {
super(message, cause);
this.stackTrace = stackTrace;
}
@Override
public StackTraceElement[] getStackTrace() {
return stackTrace;
}
}
private Object logDebug(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
assertEquals(4, args.length);
assertEquals("Error {} {}", args[0]);
assertEquals("param1", args[1]);
assertEquals("param2", args[2]);
assertTrue(args[3] instanceof RuntimeException);
assertEquals("this is an exception", ((RuntimeException) args[3]).getMessage());
return null;
}
private Object logWarn(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
assertEquals(3, args.length);
assertEquals("Error {} {} java.lang.RuntimeException: this is an exception", args[0]);
assertEquals("param1", args[1]);
assertEquals("param2", args[2]);
return null;
}
}