• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 final String TEST_MESSAGE_2 = "This is monotonous";
44     protected static final String TEST_MESSAGE = "Test Message";
45     protected T exceptionContext;
46 
47     protected static class ObjectWithFaultyToString {
48         @Override
toString()49         public String toString() {
50             throw new RuntimeException("Crap");
51         }
52     }
53 
54 
55     @BeforeEach
setUp()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
testAddContextValue()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
testSetContextValue()94     public void testSetContextValue() {
95         exceptionContext.addContextValue("test2", "different value");
96         exceptionContext.setContextValue("test3", "3");
97 
98         final String message = exceptionContext.getFormattedExceptionMessage(TEST_MESSAGE);
99         assertTrue(message.contains(TEST_MESSAGE));
100         assertTrue(message.contains("test Poorly written obj"));
101         assertTrue(message.contains("Crap"));
102 
103         assertNull(exceptionContext.getFirstContextValue("crap"));
104         assertTrue(exceptionContext.getFirstContextValue("test Poorly written obj") instanceof ObjectWithFaultyToString);
105 
106         assertEquals(7, exceptionContext.getContextEntries().size());
107         assertEquals(6, exceptionContext.getContextLabels().size());
108 
109         assertTrue(exceptionContext.getContextLabels().contains("test Poorly written obj"));
110         assertFalse(exceptionContext.getContextLabels().contains("crap"));
111 
112         exceptionContext.setContextValue("test Poorly written obj", "replacement");
113 
114         assertEquals(7, exceptionContext.getContextEntries().size());
115         assertEquals(6, exceptionContext.getContextLabels().size());
116 
117         exceptionContext.setContextValue("test2", "another");
118 
119         assertEquals(6, exceptionContext.getContextEntries().size());
120         assertEquals(6, exceptionContext.getContextLabels().size());
121 
122         final String contextMessage = exceptionContext.getFormattedExceptionMessage(null);
123         assertFalse(contextMessage.contains(TEST_MESSAGE));
124     }
125 
126     @Test
testGetFirstContextValue()127     public void testGetFirstContextValue() {
128         exceptionContext.addContextValue("test2", "different value");
129 
130         assertNull(exceptionContext.getFirstContextValue("test1"));
131         assertEquals("some value", exceptionContext.getFirstContextValue("test2"));
132         assertNull(exceptionContext.getFirstContextValue("crap"));
133 
134         exceptionContext.setContextValue("test2", "another");
135 
136         assertEquals("another", exceptionContext.getFirstContextValue("test2"));
137     }
138 
139     @Test
testGetContextValues()140     public void testGetContextValues() {
141         exceptionContext.addContextValue("test2", "different value");
142 
143         assertEquals(exceptionContext.getContextValues("test1"), Collections.singletonList(null));
144         assertEquals(exceptionContext.getContextValues("test2"), Arrays.asList("some value", "different value"));
145 
146         exceptionContext.setContextValue("test2", "another");
147 
148         assertEquals("another", exceptionContext.getFirstContextValue("test2"));
149     }
150 
151     @Test
testGetContextLabels()152     public void testGetContextLabels() {
153         assertEquals(5, exceptionContext.getContextEntries().size());
154 
155         exceptionContext.addContextValue("test2", "different value");
156 
157         final Set<String> labels = exceptionContext.getContextLabels();
158         assertEquals(6, exceptionContext.getContextEntries().size());
159         assertEquals(5, labels.size());
160         assertTrue(labels.contains("test1"));
161         assertTrue(labels.contains("test2"));
162         assertTrue(labels.contains("test Date"));
163         assertTrue(labels.contains("test Nbr"));
164     }
165 
166     @Test
testGetContextEntries()167     public void testGetContextEntries() {
168         assertEquals(5, exceptionContext.getContextEntries().size());
169 
170         exceptionContext.addContextValue("test2", "different value");
171 
172         final List<Pair<String, Object>> entries = exceptionContext.getContextEntries();
173         assertEquals(6, entries.size());
174         assertEquals("test1", entries.get(0).getKey());
175         assertEquals("test2", entries.get(1).getKey());
176         assertEquals("test Date", entries.get(2).getKey());
177         assertEquals("test Nbr", entries.get(3).getKey());
178         assertEquals("test Poorly written obj", entries.get(4).getKey());
179         assertEquals("test2", entries.get(5).getKey());
180     }
181 
182     @Test
testJavaSerialization()183     public void testJavaSerialization() {
184         exceptionContext.setContextValue("test Poorly written obj", "serializable replacement");
185 
186         final T clone = SerializationUtils.deserialize(SerializationUtils.serialize(exceptionContext));
187 
188         assertEquals(exceptionContext.getFormattedExceptionMessage(null), clone.getFormattedExceptionMessage(null));
189     }
190 }
191