View Javadoc
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
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 }