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    *      https://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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.Serializable;
26  import java.util.Arrays;
27  import java.util.Collections;
28  import java.util.Date;
29  import java.util.List;
30  import java.util.Set;
31  
32  import org.apache.commons.lang3.AbstractLangTest;
33  import org.apache.commons.lang3.SerializationUtils;
34  import org.apache.commons.lang3.tuple.Pair;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Abstract test of an ExceptionContext implementation.
40   */
41  public abstract class AbstractExceptionContextTest<T extends ExceptionContext & Serializable> extends AbstractLangTest {
42  
43      protected static class ObjectWithFaultyToString {
44          @Override
45          public String toString() {
46              throw new RuntimeException("Crap");
47          }
48      }
49      protected static final String TEST_MESSAGE_2 = "This is monotonous";
50      protected static final String TEST_MESSAGE = "Test Message";
51  
52      protected T exceptionContext;
53  
54      @BeforeEach
55      public void setUp() throws Exception {
56          exceptionContext
57              .addContextValue("test1", null)
58              .addContextValue("test2", "some value")
59              .addContextValue("test Date", new Date())
60              .addContextValue("test Nbr", Integer.valueOf(5))
61              .addContextValue("test Poorly written obj", new ObjectWithFaultyToString());
62      }
63  
64      @Test
65      void testAddContextValue() {
66          final String message = exceptionContext.getFormattedExceptionMessage(TEST_MESSAGE);
67          assertTrue(message.contains(TEST_MESSAGE));
68          assertTrue(message.contains("test1"));
69          assertTrue(message.contains("test2"));
70          assertTrue(message.contains("test Date"));
71          assertTrue(message.contains("test Nbr"));
72          assertTrue(message.contains("some value"));
73          assertTrue(message.contains("5"));
74  
75          assertNull(exceptionContext.getFirstContextValue("test1"));
76          assertEquals("some value", exceptionContext.getFirstContextValue("test2"));
77  
78          assertEquals(5, exceptionContext.getContextLabels().size());
79          assertTrue(exceptionContext.getContextLabels().contains("test1"));
80          assertTrue(exceptionContext.getContextLabels().contains("test2"));
81          assertTrue(exceptionContext.getContextLabels().contains("test Date"));
82          assertTrue(exceptionContext.getContextLabels().contains("test Nbr"));
83  
84          exceptionContext.addContextValue("test2", "different value");
85          assertEquals(5, exceptionContext.getContextLabels().size());
86          assertTrue(exceptionContext.getContextLabels().contains("test2"));
87  
88          final String contextMessage = exceptionContext.getFormattedExceptionMessage(null);
89          assertFalse(contextMessage.contains(TEST_MESSAGE));
90      }
91  
92      @Test
93      void testGetContextEntries() {
94          assertEquals(5, exceptionContext.getContextEntries().size());
95  
96          exceptionContext.addContextValue("test2", "different value");
97  
98          final List<Pair<String, Object>> entries = exceptionContext.getContextEntries();
99          assertEquals(6, entries.size());
100         assertEquals("test1", entries.get(0).getKey());
101         assertEquals("test2", entries.get(1).getKey());
102         assertEquals("test Date", entries.get(2).getKey());
103         assertEquals("test Nbr", entries.get(3).getKey());
104         assertEquals("test Poorly written obj", entries.get(4).getKey());
105         assertEquals("test2", entries.get(5).getKey());
106     }
107 
108     @Test
109     void testGetContextLabels() {
110         assertEquals(5, exceptionContext.getContextEntries().size());
111 
112         exceptionContext.addContextValue("test2", "different value");
113 
114         final Set<String> labels = exceptionContext.getContextLabels();
115         assertEquals(6, exceptionContext.getContextEntries().size());
116         assertEquals(5, labels.size());
117         assertTrue(labels.contains("test1"));
118         assertTrue(labels.contains("test2"));
119         assertTrue(labels.contains("test Date"));
120         assertTrue(labels.contains("test Nbr"));
121     }
122 
123     @Test
124     void testGetContextValues() {
125         exceptionContext.addContextValue("test2", "different value");
126 
127         assertEquals(Collections.singletonList(null), exceptionContext.getContextValues("test1"));
128         assertEquals(Arrays.asList("some value", "different value"), exceptionContext.getContextValues("test2"));
129 
130         exceptionContext.setContextValue("test2", "another");
131 
132         assertEquals("another", exceptionContext.getFirstContextValue("test2"));
133     }
134 
135     @Test
136     void testGetFirstContextValue() {
137         exceptionContext.addContextValue("test2", "different value");
138 
139         assertNull(exceptionContext.getFirstContextValue("test1"));
140         assertEquals("some value", exceptionContext.getFirstContextValue("test2"));
141         assertNull(exceptionContext.getFirstContextValue("crap"));
142 
143         exceptionContext.setContextValue("test2", "another");
144 
145         assertEquals("another", exceptionContext.getFirstContextValue("test2"));
146     }
147 
148     @Test
149     void testJavaSerialization() {
150         exceptionContext.setContextValue("test Poorly written obj", "serializable replacement");
151 
152         final T clone = SerializationUtils.deserialize(SerializationUtils.serialize(exceptionContext));
153 
154         assertEquals(exceptionContext.getFormattedExceptionMessage(null), clone.getFormattedExceptionMessage(null));
155     }
156 
157     @Test
158     void testSetContextValue() {
159         exceptionContext.addContextValue("test2", "different value");
160         exceptionContext.setContextValue("test3", "3");
161 
162         final String message = exceptionContext.getFormattedExceptionMessage(TEST_MESSAGE);
163         assertTrue(message.contains(TEST_MESSAGE));
164         assertTrue(message.contains("test Poorly written obj"));
165         assertTrue(message.contains("Crap"));
166 
167         assertNull(exceptionContext.getFirstContextValue("crap"));
168         assertInstanceOf(ObjectWithFaultyToString.class, exceptionContext.getFirstContextValue("test Poorly written obj"));
169 
170         assertEquals(7, exceptionContext.getContextEntries().size());
171         assertEquals(6, exceptionContext.getContextLabels().size());
172 
173         assertTrue(exceptionContext.getContextLabels().contains("test Poorly written obj"));
174         assertFalse(exceptionContext.getContextLabels().contains("crap"));
175 
176         exceptionContext.setContextValue("test Poorly written obj", "replacement");
177 
178         assertEquals(7, exceptionContext.getContextEntries().size());
179         assertEquals(6, exceptionContext.getContextLabels().size());
180 
181         exceptionContext.setContextValue("test2", "another");
182 
183         assertEquals(6, exceptionContext.getContextEntries().size());
184         assertEquals(6, exceptionContext.getContextLabels().size());
185 
186         final String contextMessage = exceptionContext.getFormattedExceptionMessage(null);
187         assertFalse(contextMessage.contains(TEST_MESSAGE));
188     }
189 }