1 package org.djutils.exceptions;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.fail;
6
7 import java.io.FileInputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10
11 import org.junit.Test;
12
13
14
15
16
17
18
19
20
21
22
23 public class ThrowTest
24 {
25
26
27
28 @SuppressWarnings({"checkstyle:methodlength", "cast"})
29 @Test
30 public void testThrow()
31 {
32 Object object = new Object();
33 Object objectNull = null;
34 int i = 10;
35 Double d = Double.valueOf(20.0);
36 String s = "argument";
37 int hex = 26;
38
39
40
41
42
43 String message = "Throw error has occurred. Correct";
44 try
45 {
46 Throw.whenNull(objectNull, message);
47 }
48 catch (Exception e)
49 {
50 assertTrue(e.getMessage() + " / " + message, e.getMessage().endsWith(message));
51 }
52
53 String message1arg = "Throw error has occurred for %s. Correct";
54 String message1 = "Throw error has occurred for argument. Correct";
55 try
56 {
57 Throw.whenNull(objectNull, message1arg, s);
58 }
59 catch (Exception e)
60 {
61 assertTrue(e.getMessage() + " / " + message1, e.getMessage().endsWith(message1));
62 }
63
64 String message2arg = "Throw error %d has occurred for %s. Correct";
65 String message2 = "Throw error 10 has occurred for argument. Correct";
66 try
67 {
68 Throw.whenNull(objectNull, message2arg, i, s);
69 }
70 catch (Exception e)
71 {
72 assertTrue(e.getMessage() + " / " + message2, e.getMessage().endsWith(message2));
73 }
74
75 String message3arg = "Throw error %4.1f has occurred for %s, value %d. Correct";
76 String message3 = "Throw error 20.0 has occurred for argument, value 10. Correct";
77 try
78 {
79 Throw.whenNull(objectNull, message3arg, d, s, i);
80 }
81 catch (Exception e)
82 {
83 assertTrue(e.getMessage() + " / " + message3, e.getMessage().endsWith(message3));
84 }
85
86 String message4arg = "Throw error %4.1f has occurred for %s, hex=%h, value %d. Correct";
87 String message4 = "Throw error 20.0 has occurred for argument, hex=1a, value 10. Correct";
88 try
89 {
90 Throw.whenNull(objectNull, message4arg, d, s, hex, i);
91 }
92 catch (Exception e)
93 {
94 assertTrue(e.getMessage() + " / " + message4, e.getMessage().endsWith(message4));
95 }
96
97
98 Throw.whenNull(object, "object is not null -- this should not be triggered");
99 Throw.whenNull(object, message1arg, s);
100 Throw.whenNull(object, message2arg, i, s);
101 Throw.whenNull(object, message3arg, d, s, i);
102 Throw.whenNull(object, message4arg, d, s, hex, i);
103
104
105
106
107
108 try
109 {
110 Throw.when(i == 10, Exception.class, message);
111 }
112 catch (Exception e)
113 {
114 assertTrue(e.getMessage() + " / " + message, e.getMessage().endsWith(message));
115 }
116
117 try
118 {
119 Throw.when(i == 10, Exception.class, message1arg, s);
120 }
121 catch (Exception e)
122 {
123 assertTrue(e.getMessage() + " / " + message1, e.getMessage().endsWith(message1));
124 }
125
126 try
127 {
128 Throw.when(i == 10, Exception.class, message2arg, i, s);
129 }
130 catch (Exception e)
131 {
132 assertTrue(e.getMessage() + " / " + message2, e.getMessage().endsWith(message2));
133 }
134
135 try
136 {
137 Throw.when(i == 10, Exception.class, message3arg, d, s, i);
138 }
139 catch (Exception e)
140 {
141 assertTrue(e.getMessage() + " / " + message3, e.getMessage().endsWith(message3));
142 }
143
144 try
145 {
146 Throw.when(i == 10, Exception.class, message4arg, d, s, hex, i);
147 }
148 catch (Exception e)
149 {
150 assertTrue(e.getMessage() + " / " + message4, e.getMessage().endsWith(message4));
151 }
152
153
154 Throw.when(false, RuntimeException.class, "object is not null -- this should not be triggered");
155 Throw.when(false, RuntimeException.class, message1arg, s);
156 Throw.when(false, RuntimeException.class, message2arg, i, s);
157 Throw.when(false, RuntimeException.class, message3arg, d, s, i);
158 Throw.when(false, RuntimeException.class, message4arg, d, s, hex, i);
159
160 assertEquals(d, Throw.when(d, false, RuntimeException.class, "object is not null -- this should not be triggered"));
161 assertEquals(d, Throw.when(d, false, RuntimeException.class, message1arg, s));
162 assertEquals(d, Throw.when(d, false, RuntimeException.class, message2arg, i, s));
163 assertEquals(d, Throw.when(d, false, RuntimeException.class, message3arg, d, s, i));
164 assertEquals(d, Throw.when(d, false, RuntimeException.class, message4arg, d, s, hex, i));
165
166 try
167 {
168 Throw.when(true, RuntimeException.class, message);
169 fail("Throw.when(true, ...) should have thrown an exception");
170 }
171 catch (RuntimeException rte)
172 {
173 assertTrue("exception is a RuntimeException", rte instanceof RuntimeException);
174 assertTrue("message is present", rte.getMessage().contains(message));
175 }
176
177 String badFormatString = "FormatString with bad placeholder %f";
178 String notAFloatOrDouble = "this is not a float or double";
179 try
180 {
181 Throw.when(true, RuntimeException.class, badFormatString, notAFloatOrDouble);
182 fail("Throw.when(true, ...) should have thrown an exception");
183 }
184 catch (RuntimeException rte)
185 {
186 assertTrue("exception is a RuntimeException", rte instanceof RuntimeException);
187 assertTrue("description is descriptive, despite error in format string",
188 rte.getMessage().contains(badFormatString));
189 assertTrue("description is descriptive, despite error in format string",
190 rte.getMessage().contains(notAFloatOrDouble));
191 }
192
193 try
194 {
195 Throw.when("result (which will not be returned because there will be no return)", true, RuntimeException.class,
196 badFormatString, notAFloatOrDouble);
197 fail("Throw.when(true, ...) should have thrown an exception");
198 }
199 catch (RuntimeException rte)
200 {
201 assertTrue("exception is a RuntimeException", rte instanceof RuntimeException);
202 assertTrue("description is descriptive, despite error in format string",
203 rte.getMessage().contains(badFormatString));
204 assertTrue("description is descriptive, despite error in format string",
205 rte.getMessage().contains(notAFloatOrDouble));
206 }
207
208 try
209 {
210 Throw.when("result (which will not be returned because there will be no return)", true, RuntimeException.class,
211 message);
212 fail("Throw.when(true, ...) should have thrown an exception");
213 }
214 catch (RuntimeException rte)
215 {
216 assertTrue("exception is a RuntimeException", rte instanceof RuntimeException);
217 assertTrue("description is descriptive", rte.getMessage().contains(message));
218 }
219
220 Object result = Double.valueOf(123 / 456);
221 String formatStringWith2PlaceHolders = "message %s %s";
222 String arg1 = "arg1";
223 String arg2 = "arg2";
224 try
225 {
226 Throw.when(result, true, RuntimeException.class, formatStringWith2PlaceHolders, arg1, arg2);
227 }
228 catch (RuntimeException rte)
229 {
230 assertTrue("exception is a RuntimeException", rte instanceof RuntimeException);
231 assertTrue("description is descriptive", rte.getMessage().contains("message"));
232 assertTrue("description is descriptive", rte.getMessage().contains(arg1));
233 assertTrue("description is descriptive", rte.getMessage().contains(arg2));
234 }
235
236 String formatStringWith3PlaceHolders = "message %s %s %s";
237 String arg3 = "arg3";
238 try
239 {
240 Throw.when(result, true, RuntimeException.class, formatStringWith3PlaceHolders, arg1, arg2, arg3);
241 }
242 catch (RuntimeException rte)
243 {
244 assertTrue("exception is a RuntimeException", rte instanceof RuntimeException);
245 assertTrue("description is descriptive", rte.getMessage().contains("message"));
246 assertTrue("description is descriptive", rte.getMessage().contains(arg1));
247 assertTrue("description is descriptive", rte.getMessage().contains(arg2));
248 assertTrue("description is descriptive", rte.getMessage().contains(arg3));
249 }
250
251 String formatStringWith4PlaceHolders = "message %s %s %s %s";
252 String arg4 = "arg4";
253 try
254 {
255 Throw.when(result, true, RuntimeException.class, formatStringWith4PlaceHolders, arg1, arg2, arg3, arg4);
256 }
257 catch (RuntimeException rte)
258 {
259 assertTrue("exception is a RuntimeException", rte instanceof RuntimeException);
260 assertTrue("description is descriptive", rte.getMessage().contains("message"));
261 assertTrue("description is descriptive", rte.getMessage().contains(arg1));
262 assertTrue("description is descriptive", rte.getMessage().contains(arg2));
263 assertTrue("description is descriptive", rte.getMessage().contains(arg3));
264 assertTrue("description is descriptive", rte.getMessage().contains(arg4));
265 }
266 }
267
268
269
270
271 @Test
272 public void testThrowUnchecked()
273 {
274 NoThrow nt = new NoThrow();
275 try
276 {
277 nt.doSomethingWithoutException();
278 fail("An exception should have been thrown by NoThrow.doSomething()");
279 }
280 catch (Exception e)
281 {
282 assertTrue("The exception thrown by NoThrow.doSomething() is not an IOException", e instanceof IOException);
283 }
284 }
285
286
287 class NoThrow implements NoThrowInterface
288 {
289
290 @Override
291 public void doSomethingWithoutException()
292 {
293 try
294 {
295 InputStream is = new FileInputStream("z:xyz/x/y/z.xyz");
296 is.read();
297 is.close();
298 }
299 catch (IOException exception)
300 {
301 Throw.throwUnchecked(exception);
302 }
303 }
304
305 }
306
307
308 interface NoThrowInterface
309 {
310
311 void doSomethingWithoutException();
312 }
313 }