• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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  * JUnit tests for ContextedException.
32  */
33 public class ContextedExceptionTest extends AbstractExceptionContextTest<ContextedException> {
34 
35     @BeforeEach
36     @Override
setUp()37     public void setUp() throws Exception {
38         exceptionContext = new ContextedException(new Exception(TEST_MESSAGE));
39         super.setUp();
40     }
41 
42     @Test
testContextedException()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
testNullException()52     public void testNullException() {
53         assertEquals("", ExceptionUtils.getStackTrace(null), "Empty response.");
54     }
55 
56     @Test
testContextedExceptionString()57     public void testContextedExceptionString() {
58         exceptionContext = new ContextedException(TEST_MESSAGE);
59         assertEquals(TEST_MESSAGE, exceptionContext.getMessage());
60 
61         final String trace = ExceptionUtils.getStackTrace(exceptionContext);
62         assertTrue(trace.contains(TEST_MESSAGE));
63     }
64 
65     @Test
testContextedExceptionThrowable()66     public void testContextedExceptionThrowable() {
67         exceptionContext = new ContextedException(new Exception(TEST_MESSAGE));
68         final String message = exceptionContext.getMessage();
69         final String trace = ExceptionUtils.getStackTrace(exceptionContext);
70         assertTrue(trace.contains("ContextedException"));
71         assertTrue(trace.contains(TEST_MESSAGE));
72         assertTrue(message.contains(TEST_MESSAGE));
73     }
74 
75     @Test
testContextedExceptionStringThrowable()76     public void testContextedExceptionStringThrowable() {
77         exceptionContext = new ContextedException(TEST_MESSAGE_2, new Exception(TEST_MESSAGE));
78         final String message = exceptionContext.getMessage();
79         final String trace = ExceptionUtils.getStackTrace(exceptionContext);
80         assertTrue(trace.contains("ContextedException"));
81         assertTrue(trace.contains(TEST_MESSAGE));
82         assertTrue(trace.contains(TEST_MESSAGE_2));
83         assertTrue(message.contains(TEST_MESSAGE_2));
84     }
85 
86     @Test
testContextedExceptionStringThrowableContext()87     public void testContextedExceptionStringThrowableContext() {
88         exceptionContext = new ContextedException(TEST_MESSAGE_2, new Exception(TEST_MESSAGE), new DefaultExceptionContext());
89         final String message = exceptionContext.getMessage();
90         final String trace = ExceptionUtils.getStackTrace(exceptionContext);
91         assertTrue(trace.contains("ContextedException"));
92         assertTrue(trace.contains(TEST_MESSAGE));
93         assertTrue(trace.contains(TEST_MESSAGE_2));
94         assertTrue(message.contains(TEST_MESSAGE_2));
95     }
96 
97     @Test
testNullExceptionPassing()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
testRawMessage()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 }
119