|
23 | 23 | import org.junit.jupiter.api.AfterEach; |
24 | 24 | import org.junit.jupiter.api.BeforeEach; |
25 | 25 | import org.junit.jupiter.api.Test; |
26 | | -import org.slf4j.MDC; |
27 | 26 | import org.slf4j.Marker; |
28 | 27 | import org.slf4j.MarkerFactory; |
29 | 28 |
|
|
34 | 33 | import java.io.ObjectInputStream; |
35 | 34 | import java.io.ObjectOutputStream; |
36 | 35 | import java.io.Serializable; |
37 | | -import java.lang.reflect.Field; |
38 | 36 | import java.util.Arrays; |
39 | 37 | import java.util.Map; |
40 | 38 |
|
41 | 39 | import static org.junit.jupiter.api.Assertions.assertEquals; |
42 | 40 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
43 | 41 | import static org.junit.jupiter.api.Assertions.assertNull; |
44 | 42 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
45 | 44 |
|
46 | 45 | public class LoggingEventSerializationTest { |
47 | 46 |
|
@@ -251,11 +250,47 @@ public void sharedSuppressedThrowableProxyIsRejected() throws Exception { |
251 | 250 | assertThrows(InvalidObjectException.class, () -> writeAndReadWithThrowableProxy(tp)); |
252 | 251 | } |
253 | 252 |
|
| 253 | + // ThrowableProxy replaces the repeated occurrence of a throwable by a distinct proxy |
| 254 | + // marked as cyclic, so genuinely circular exceptions must survive the round trip |
| 255 | + @Test |
| 256 | + public void circularExceptionRoundTrip() throws Exception { |
| 257 | + Exception root = new Exception("root"); |
| 258 | + Exception cause = new Exception("cause"); |
| 259 | + root.initCause(cause); |
| 260 | + // root -> cause -> suppressed root |
| 261 | + cause.addSuppressed(root); |
| 262 | + |
| 263 | + LoggingEvent event = createLoggingEventWithThrowable(root); |
| 264 | + ILoggingEvent remoteEvent = writeAndRead(event); |
| 265 | + checkForEquality(event, remoteEvent); |
| 266 | + |
| 267 | + IThrowableProxy remoteCause = remoteEvent.getThrowableProxy().getCause(); |
| 268 | + assertTrue(remoteCause.getSuppressed()[0].isCyclic()); |
| 269 | + String rendered = ThrowableProxyUtil.asString(remoteEvent.getThrowableProxy()); |
| 270 | + assertTrue(rendered.contains("[CIRCULAR REFERENCE: java.lang.Exception: root]"), rendered); |
| 271 | + } |
| 272 | + |
| 273 | + // the same throwable reachable twice without forming a cycle |
| 274 | + @Test |
| 275 | + public void exceptionWithSameCauseAndSuppressedRoundTrip() throws Exception { |
| 276 | + Exception root = new Exception("root"); |
| 277 | + Exception other = new Exception("other"); |
| 278 | + root.initCause(other); |
| 279 | + root.addSuppressed(other); |
| 280 | + |
| 281 | + LoggingEvent event = createLoggingEventWithThrowable(root); |
| 282 | + ILoggingEvent remoteEvent = writeAndRead(event); |
| 283 | + checkForEquality(event, remoteEvent); |
| 284 | + |
| 285 | + IThrowableProxy remoteProxy = remoteEvent.getThrowableProxy(); |
| 286 | + assertTrue(remoteProxy.getSuppressed()[0].isCyclic()); |
| 287 | + String rendered = ThrowableProxyUtil.asString(remoteProxy); |
| 288 | + assertTrue(rendered.contains("[CIRCULAR REFERENCE: java.lang.Exception: other]"), rendered); |
| 289 | + } |
| 290 | + |
254 | 291 | private void writeAndReadWithThrowableProxy(ThrowableProxyVO tp) throws Exception { |
255 | 292 | LoggingEventVO vo = LoggingEventVO.build(createLoggingEvent()); |
256 | | - Field field = LoggingEventVO.class.getDeclaredField("throwableProxy"); |
257 | | - field.setAccessible(true); |
258 | | - field.set(vo, tp); |
| 293 | + vo.setThrowableProxyVO(tp); |
259 | 294 | oos.writeObject(vo); |
260 | 295 | ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
261 | 296 | inputStream = new HardenedLoggingEventInputStream(loggerContext, bis); |
|
0 commit comments