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.juneau.commons.reflect;
18
19 import static org.junit.jupiter.api.Assertions.*;
20
21 import java.lang.reflect.*;
22
23 import org.apache.juneau.*;
24 import org.junit.jupiter.api.*;
25
26 class ExecutableException_Test extends TestBase {
27
28 public static class TestClass {
29 public void method() throws Exception {
30 throw new RuntimeException("test");
31 }
32 }
33
34 //====================================================================================================
35 // ExecutableException(String, Object...)
36 //====================================================================================================
37 @Test
38 void a001_constructor_message() {
39 ExecutableException e = new ExecutableException("Test message");
40 assertEquals("Test message", e.getMessage());
41 assertNull(e.getCause());
42 }
43
44 //====================================================================================================
45 // ExecutableException(String, Object...) with args
46 //====================================================================================================
47 @Test
48 void a002_constructor_messageWithArgs() {
49 ExecutableException e = new ExecutableException("Test {0} message {1}", "arg1", "arg2");
50 assertTrue(e.getMessage().contains("arg1"));
51 assertTrue(e.getMessage().contains("arg2"));
52 assertNull(e.getCause());
53 }
54
55 //====================================================================================================
56 // ExecutableException(Throwable)
57 //====================================================================================================
58 @Test
59 void a003_constructor_cause() {
60 Throwable cause = new RuntimeException("cause");
61 ExecutableException e = new ExecutableException(cause);
62 assertSame(cause, e.getCause());
63 }
64
65 //====================================================================================================
66 // ExecutableException(Throwable, String, Object...)
67 //====================================================================================================
68 @Test
69 void a004_constructor_causeAndMessage() {
70 Throwable cause = new RuntimeException("cause");
71 ExecutableException e = new ExecutableException(cause, "Test {0}", "arg");
72 assertSame(cause, e.getCause());
73 assertTrue(e.getMessage().contains("arg"));
74 }
75
76 //====================================================================================================
77 // getTargetException()
78 //====================================================================================================
79 @Test
80 void a005_getTargetException() throws Exception {
81 // With InvocationTargetException
82 RuntimeException targetException = new RuntimeException("target");
83 InvocationTargetException ite = new InvocationTargetException(targetException);
84 ExecutableException e = new ExecutableException(ite);
85 assertSame(targetException, e.getTargetException());
86
87 // With other exception
88 IllegalArgumentException iae = new IllegalArgumentException("test");
89 ExecutableException e2 = new ExecutableException(iae);
90 assertSame(iae, e2.getTargetException());
91
92 // With no cause
93 ExecutableException e3 = new ExecutableException("message");
94 assertNull(e3.getTargetException());
95 }
96
97 //====================================================================================================
98 // unwrap()
99 //====================================================================================================
100 @Test
101 void a006_unwrap() {
102 // With cause
103 Throwable cause = new RuntimeException("cause");
104 ExecutableException e = new ExecutableException(cause);
105 assertSame(cause, e.unwrap());
106
107 // Without cause
108 ExecutableException e2 = new ExecutableException("message");
109 assertSame(e2, e2.unwrap());
110 }
111 }
112
113