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.assertTrue;
21  
22  import org.apache.commons.lang3.ObjectToStringRuntimeException;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * JUnit tests for DefaultExceptionContext.
28   */
29  public class DefaultExceptionContextTest extends AbstractExceptionContextTest<DefaultExceptionContext> {
30  
31      @Override
32      @BeforeEach
33      public void setUp() throws Exception {
34          exceptionContext = new DefaultExceptionContext();
35          super.setUp();
36      }
37  
38      @Test
39      public void testFormattedExceptionMessageExceptionHandling() {
40          exceptionContext = new DefaultExceptionContext();
41          final String label1 = "throws 1";
42          final String label2 = "throws 2";
43          exceptionContext.addContextValue(label1, new ObjectToStringRuntimeException(label1));
44          exceptionContext.addContextValue(label2, new ObjectToStringRuntimeException(label2));
45          final String message = exceptionContext.getFormattedExceptionMessage(TEST_MESSAGE);
46          assertTrue(message.startsWith(TEST_MESSAGE));
47          assertTrue(message.contains(label1));
48          assertTrue(message.contains(label2));
49      }
50  
51      @Test
52      public void testFormattedExceptionMessageNull() {
53          exceptionContext = new DefaultExceptionContext();
54          assertEquals("", exceptionContext.getFormattedExceptionMessage(null));
55      }
56  
57      @Test
58      public void testFormattedExceptionMessageNullValue() {
59          exceptionContext = new DefaultExceptionContext();
60          final String label1 = "throws 1";
61          final String label2 = "throws 2";
62          exceptionContext.addContextValue(label1, null);
63          exceptionContext.addContextValue(label2, null);
64          final String message = exceptionContext.getFormattedExceptionMessage(TEST_MESSAGE);
65          assertTrue(message.startsWith(TEST_MESSAGE));
66          assertTrue(message.contains(label1));
67          assertTrue(message.contains(label2));
68      }
69  
70  }