View Javadoc
1   /*
2    * Copyright 2011-2024 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.googlecode.catchexception.throwable;
17  
18  import static com.googlecode.catchexception.throwable.CatchThrowable.catchThrowable;
19  import static com.googlecode.catchexception.throwable.CatchThrowable.caughtThrowable;
20  import static com.googlecode.catchexception.throwable.CatchThrowable.verifyThrowable;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.junit.jupiter.api.Assertions.fail;
25  
26  import java.net.HttpRetryException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests {@link CatchThrowable}.
35   */
36  class CatchThrowableTest {
37  
38      /** The list. */
39      private final List<String> list = new ArrayList<>();
40  
41      /**
42       * The message of the exception thrown by {@code new ArrayList<String>().get(0) }.
43       */
44      private final String expectedMessage = "Index: 0, Size: 0";
45  
46      /**
47       * The message of the exception thrown by {@code new ArrayList<String>().get(0) } for jdk9on.
48       */
49      private final String expectedMessageJdk9on = "Index 0 out of bounds for length 0";
50  
51      /**
52       * Sets the up.
53       */
54      @BeforeEach
55      void setUp() {
56          // set any exception so that we have clear state before the test
57          ThrowableHolder.set(new HttpRetryException("detail", 0));
58      }
59  
60      /**
61       * Catch exception obj exc no exception thrown.
62       */
63      @Test
64      void catchExceptionObjExcNoExceptionThrown() {
65  
66          catchThrowable(list::size, IndexOutOfBoundsException.class);
67          assertNull(caughtThrowable());
68      }
69  
70      /**
71       * Catch exception throw error.
72       */
73      @Test
74      void catchExceptionThrowError() {
75  
76          catchThrowable(() -> {
77              throw new Error("ddd");
78          });
79          assertEquals(Error.class, caughtThrowable().getClass());
80      }
81  
82      /**
83       * Catch exception obj exc actual class thrown.
84       */
85      @Test
86      void catchExceptionObjExcActualClassThrown() {
87  
88          // test for actual class
89          catchThrowable(() -> list.get(0), IndexOutOfBoundsException.class);
90          if (!expectedMessage.equals(caughtThrowable().getMessage())) {
91              assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
92          }
93      }
94  
95      /**
96       * Catch exception obj exc sub class of expected thrown.
97       */
98      @Test
99      void catchExceptionObjExcSubClassOfExpectedThrown() {
100 
101         // test for super class
102         catchThrowable(() -> list.get(0), RuntimeException.class);
103         if (!expectedMessage.equals(caughtThrowable().getMessage())) {
104             assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
105         }
106     }
107 
108     /**
109      * Catch exception obj exc super class of expected thrown.
110      */
111     @Test
112     void catchExceptionObjExcSuperClassOfExpectedThrown() {
113 
114         try {
115             catchThrowable(() -> list.get(0), ArrayIndexOutOfBoundsException.class);
116             fail("IndexOutOfBoundsException is expected (shall not be caught)");
117         } catch (IndexOutOfBoundsException e) {
118             assertNull(caughtThrowable());
119         }
120     }
121 
122     /**
123      * Catch exception obj exc other class than expected thrown.
124      */
125     @Test
126     void catchExceptionObjExcOtherClassThanExpectedThrown() {
127 
128         try {
129             catchThrowable(() -> list.get(0), IllegalArgumentException.class);
130             fail("IndexOutOfBoundsException is expected (shall not be caught)");
131         } catch (IndexOutOfBoundsException e) {
132             assertNull(caughtThrowable());
133         }
134     }
135 
136     /**
137      * Catch exception obj exc missing argument exception.
138      */
139     @Test
140     void catchExceptionObjExcMissingArgumentException() {
141 
142         // test validation of the arguments
143         try {
144             catchThrowable(list::size, null);
145             fail("IllegalArgumentException is expected");
146         } catch (IllegalArgumentException e) {
147             assertEquals("throwableClazz must not be null", e.getMessage());
148         }
149     }
150 
151     /**
152      * Catch exception obj exc missing argument object.
153      */
154     @Test
155     void catchExceptionObjExcMissingArgumentObject() {
156 
157         try {
158             catchThrowable(null, IllegalArgumentException.class);
159             fail("IllegalArgumentException is expected");
160         } catch (IllegalArgumentException e) {
161             assertEquals("obj must not be null", e.getMessage());
162         }
163     }
164 
165     /**
166      * Testverify throwable obj exc no exception thrown.
167      */
168     @Test
169     void testverifyThrowable_ObjExc_noExceptionThrown() {
170 
171         try {
172             verifyThrowable(list::size, IndexOutOfBoundsException.class);
173             fail("ThrowableNotThrownAssertionError is expected");
174         } catch (ThrowableNotThrownAssertionError e) {
175             assertNull(caughtThrowable());
176             assertEquals("Neither a throwable of type " + IndexOutOfBoundsException.class.getName()
177                     + " nor another throwable was thrown", e.getMessage());
178         }
179 
180     }
181 
182     /**
183      * Testverify throwable obj exc actual class thrown.
184      */
185     @Test
186     void testverifyThrowable_ObjExc_actualClassThrown() {
187 
188         // test for actual class
189         verifyThrowable(() -> list.get(0), IndexOutOfBoundsException.class);
190         if (!expectedMessage.equals(caughtThrowable().getMessage())) {
191             assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
192         }
193     }
194 
195     /**
196      * Testverify throwable obj exc sub class of expected thrown.
197      */
198     @Test
199     void testverifyThrowable_ObjExc_subClassOfExpectedThrown() {
200 
201         // test for super class
202         verifyThrowable(() -> list.get(0), RuntimeException.class);
203         if (!expectedMessage.equals(caughtThrowable().getMessage())) {
204             assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
205         }
206     }
207 
208     /**
209      * Testverify throwable obj exc super class of expected thrown.
210      */
211     @Test
212     void testverifyThrowable_ObjExc_superClassOfExpectedThrown() {
213 
214         // test for sub class
215         try {
216             verifyThrowable(() -> list.get(0), ArrayIndexOutOfBoundsException.class);
217             fail("ThrowableNotThrownAssertionError is expected");
218         } catch (ThrowableNotThrownAssertionError e) {
219             assertNull(caughtThrowable());
220             if (!e.getMessage().contains(expectedMessageJdk9on)) {
221                 assertEquals("Throwable of type " + ArrayIndexOutOfBoundsException.class.getName()
222                         + " expected but was not thrown." + " Instead a throwable of type "
223                         + IndexOutOfBoundsException.class + " with message '" + expectedMessage + "' was thrown.",
224                         e.getMessage());
225             }
226         }
227     }
228 
229     /**
230      * Testverify throwable obj exc other class than expected thrown.
231      */
232     @Test
233     void testverifyThrowable_ObjExc_otherClassThanExpectedThrown() {
234 
235         // test for other exception type
236         try {
237             verifyThrowable(() -> list.get(0), IllegalArgumentException.class);
238             fail("ThrowableNotThrownAssertionError is expected");
239         } catch (ThrowableNotThrownAssertionError e) {
240             assertNull(caughtThrowable());
241             if (!e.getMessage().contains(expectedMessageJdk9on)) {
242                 assertEquals("Throwable of type " + IllegalArgumentException.class.getName()
243                         + " expected but was not thrown." + " Instead a throwable of type "
244                         + IndexOutOfBoundsException.class + " with message '" + expectedMessage + "' was thrown.",
245                         e.getMessage());
246             }
247         }
248 
249     }
250 
251     /**
252      * Testverify throwable obj exc missing argument exception.
253      */
254     @Test
255     void testverifyThrowable_ObjExc_missingArgument_Exception() {
256 
257         // test validation of the arguments
258         try {
259             verifyThrowable(list::size, null);
260             fail("IllegalArgumentException is expected");
261         } catch (IllegalArgumentException e) {
262             assertEquals("throwableClazz must not be null", e.getMessage());
263         }
264     }
265 
266     /**
267      * Testverify throwable obj exc missing argument object.
268      */
269     @Test
270     void testverifyThrowable_ObjExc_missingArgument_Object() {
271 
272         try {
273             verifyThrowable(null, IllegalArgumentException.class);
274             fail("IllegalArgumentException is expected");
275         } catch (IllegalArgumentException e) {
276             assertEquals("obj must not be null", e.getMessage());
277         }
278     }
279 
280     /**
281      * Testverify throwable obj no exception thrown.
282      */
283     @Test
284     void testverifyThrowable_Obj_noExceptionThrown() {
285 
286         List<String> myList = new ArrayList<>();
287 
288         // no exception thrown by size()
289         try {
290             verifyThrowable(myList::size);
291             fail("ThrowableNotThrownAssertionError is expected");
292         } catch (ThrowableNotThrownAssertionError e) {
293             assertNull(caughtThrowable());
294             assertEquals("Throwable expected but not thrown", e.getMessage());
295         }
296     }
297 
298     /**
299      * Testverify throwable obj exception thrown.
300      */
301     @Test
302     void testverifyThrowable_Obj_exceptionThrown() {
303 
304         List<String> myList = new ArrayList<>();
305 
306         verifyThrowable(() -> myList.get(0));
307         if (!expectedMessage.equals(caughtThrowable().getMessage())) {
308             assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
309         }
310     }
311 
312     /**
313      * Testverify throwable obj missing argument object.
314      */
315     @Test
316     void testverifyThrowable_Obj_missingArgument_Object() {
317 
318         // test validation of the arguments
319         try {
320             verifyThrowable(null);
321             fail("IllegalArgumentException is expected");
322         } catch (IllegalArgumentException e) {
323             assertEquals("obj must not be null", e.getMessage());
324         }
325     }
326 
327     /**
328      * Catch exception obj no exception thrown.
329      */
330     @Test
331     void catchExceptionObjNoExceptionThrown() {
332 
333         List<String> myList = new ArrayList<>();
334 
335         // no exception thrown by size()
336         catchThrowable(myList::size);
337         assertNull(caughtThrowable());
338     }
339 
340     /**
341      * Catch exception obj exception thrown.
342      */
343     @Test
344     void catchExceptionObjExceptionThrown() {
345 
346         List<String> myList = new ArrayList<>();
347 
348         catchThrowable(() -> myList.get(0));
349         if (!expectedMessage.equals(caughtThrowable().getMessage())) {
350             assertEquals(expectedMessageJdk9on, caughtThrowable().getMessage());
351         }
352     }
353 
354     /**
355      * Catch exception obj missing argument object.
356      */
357     @Test
358     void catchExceptionObjMissingArgumentObject() {
359 
360         // test validation of the arguments
361         try {
362             catchThrowable(null);
363             fail("IllegalArgumentException is expected");
364         } catch (IllegalArgumentException e) {
365             assertEquals("obj must not be null", e.getMessage());
366         }
367     }
368 
369     /**
370      * Test protected.
371      */
372     @Test
373     void testProtected() {
374         var obj = new PublicSomethingImpl();
375         catchThrowable(obj::dooo);
376         assertTrue(caughtThrowable() instanceof MyThrowable);
377     }
378 
379 }