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