1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.exception;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.Date;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 public class ContextedExceptionTest extends AbstractExceptionContextTest<ContextedException> {
34
35 @BeforeEach
36 @Override
37 public void setUp() throws Exception {
38 exceptionContext = new ContextedException(new Exception(TEST_MESSAGE));
39 super.setUp();
40 }
41
42 @Test
43 public void testContextedException() {
44 exceptionContext = new ContextedException();
45 final String message = exceptionContext.getMessage();
46 final String trace = ExceptionUtils.getStackTrace(exceptionContext);
47 assertTrue(trace.contains("ContextedException"));
48 assertTrue(StringUtils.isEmpty(message));
49 }
50
51 @Test
52 public void testContextedExceptionString() {
53 exceptionContext = new ContextedException(TEST_MESSAGE);
54 assertEquals(TEST_MESSAGE, exceptionContext.getMessage());
55
56 final String trace = ExceptionUtils.getStackTrace(exceptionContext);
57 assertTrue(trace.contains(TEST_MESSAGE));
58 }
59
60 @Test
61 public void testContextedExceptionStringThrowable() {
62 exceptionContext = new ContextedException(TEST_MESSAGE_2, new Exception(TEST_MESSAGE));
63 final String message = exceptionContext.getMessage();
64 final String trace = ExceptionUtils.getStackTrace(exceptionContext);
65 assertTrue(trace.contains("ContextedException"));
66 assertTrue(trace.contains(TEST_MESSAGE));
67 assertTrue(trace.contains(TEST_MESSAGE_2));
68 assertTrue(message.contains(TEST_MESSAGE_2));
69 }
70
71 @Test
72 public void testContextedExceptionStringThrowableContext() {
73 exceptionContext = new ContextedException(TEST_MESSAGE_2, new Exception(TEST_MESSAGE), new DefaultExceptionContext());
74 final String message = exceptionContext.getMessage();
75 final String trace = ExceptionUtils.getStackTrace(exceptionContext);
76 assertTrue(trace.contains("ContextedException"));
77 assertTrue(trace.contains(TEST_MESSAGE));
78 assertTrue(trace.contains(TEST_MESSAGE_2));
79 assertTrue(message.contains(TEST_MESSAGE_2));
80 }
81
82 @Test
83 public void testContextedExceptionThrowable() {
84 exceptionContext = new ContextedException(new Exception(TEST_MESSAGE));
85 final String message = exceptionContext.getMessage();
86 final String trace = ExceptionUtils.getStackTrace(exceptionContext);
87 assertTrue(trace.contains("ContextedException"));
88 assertTrue(trace.contains(TEST_MESSAGE));
89 assertTrue(message.contains(TEST_MESSAGE));
90 }
91
92 @Test
93 public void testNullException() {
94 assertEquals("", ExceptionUtils.getStackTrace(null), "Empty response.");
95 }
96
97 @Test
98 public void testNullExceptionPassing() {
99 exceptionContext = new ContextedException(TEST_MESSAGE_2, new Exception(TEST_MESSAGE), null)
100 .addContextValue("test1", null)
101 .addContextValue("test2", "some value")
102 .addContextValue("test Date", new Date())
103 .addContextValue("test Nbr", Integer.valueOf(5))
104 .addContextValue("test Poorly written obj", new ObjectWithFaultyToString());
105
106 final String message = exceptionContext.getMessage();
107 assertNotNull(message);
108 }
109
110 @Test
111 public void testRawMessage() {
112 assertEquals(Exception.class.getName() + ": " + TEST_MESSAGE, exceptionContext.getRawMessage());
113 exceptionContext = new ContextedException(TEST_MESSAGE_2, new Exception(TEST_MESSAGE), new DefaultExceptionContext());
114 assertEquals(TEST_MESSAGE_2, exceptionContext.getRawMessage());
115 exceptionContext = new ContextedException(null, new Exception(TEST_MESSAGE), new DefaultExceptionContext());
116 assertNull(exceptionContext.getRawMessage());
117 }
118 }