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