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 ContextedRuntimeException.
32   */
33  public class ContextedRuntimeExceptionTest extends AbstractExceptionContextTest<ContextedRuntimeException> {
34  
35      @BeforeEach
36      @Override
37      public void setUp() throws Exception {
38          exceptionContext = new ContextedRuntimeException(new Exception(TEST_MESSAGE));
39          super.setUp();
40      }
41  
42      @Test
43      public void testContextedException() {
44          exceptionContext = new ContextedRuntimeException();
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 ContextedRuntimeException(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 ContextedRuntimeException(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          // Use an anonymous subclass to make sure users can provide custom implementations
74          exceptionContext = new ContextedRuntimeException(TEST_MESSAGE_2, new Exception(TEST_MESSAGE),
75                  new DefaultExceptionContext() {
76                      private static final long serialVersionUID = 1L;
77                  });
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
87      public void testContextedExceptionThrowable() {
88          exceptionContext = new ContextedRuntimeException(new Exception(TEST_MESSAGE));
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(message.contains(TEST_MESSAGE));
94      }
95  
96      @Test
97      public void testNullExceptionPassing() {
98          exceptionContext = new ContextedRuntimeException(TEST_MESSAGE_2, new Exception(TEST_MESSAGE), null)
99          .addContextValue("test1", null)
100         .addContextValue("test2", "some value")
101         .addContextValue("test Date", new Date())
102         .addContextValue("test Nbr", Integer.valueOf(5))
103         .addContextValue("test Poorly written obj", new ObjectWithFaultyToString());
104 
105         final String message = exceptionContext.getMessage();
106         assertNotNull(message);
107     }
108 
109     @Test
110     public void testRawMessage() {
111         assertEquals(Exception.class.getName() + ": " + TEST_MESSAGE, exceptionContext.getRawMessage());
112         exceptionContext = new ContextedRuntimeException(TEST_MESSAGE_2, new Exception(TEST_MESSAGE), new DefaultExceptionContext());
113         assertEquals(TEST_MESSAGE_2, exceptionContext.getRawMessage());
114         exceptionContext = new ContextedRuntimeException(null, new Exception(TEST_MESSAGE), new DefaultExceptionContext());
115         assertNull(exceptionContext.getRawMessage());
116     }
117 }