-
-
Notifications
You must be signed in to change notification settings - Fork 683
/
AbstractThrowableAssert.java
437 lines (412 loc) · 19 KB
/
AbstractThrowableAssert.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2018 the original author or authors.
*/
package org.assertj.core.api;
import static org.assertj.core.error.ShouldNotHaveThrown.shouldNotHaveThrown;
import java.util.IllegalFormatException;
import java.util.regex.Pattern;
import org.assertj.core.error.BasicErrorMessageFactory;
import org.assertj.core.internal.Failures;
import org.assertj.core.internal.Throwables;
import org.assertj.core.util.VisibleForTesting;
/**
* Base class for all implementations of assertions for {@link Throwable}s.
*
* @param <SELF> the "self" type of this assertion class. Please read "<a href="http://bit.ly/1IZIRcY"
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>"
* for more details.
* @param <ACTUAL> the type of the "actual" value.
*
* @author David DIDIER
* @author Alex Ruiz
* @author Joel Costigliola
* @author Mikhail Mazursky
*/
public abstract class AbstractThrowableAssert<SELF extends AbstractThrowableAssert<SELF, ACTUAL>, ACTUAL extends Throwable>
extends AbstractObjectAssert<SELF, ACTUAL> {
@VisibleForTesting
Throwables throwables = Throwables.instance();
public AbstractThrowableAssert(ACTUAL actual, Class<?> selfType) {
super(actual, selfType);
}
protected SELF hasBeenThrown() {
if (actual == null) {
throw Failures.instance().failure(info, new BasicErrorMessageFactory("%nExpecting code to raise a throwable."));
}
return myself;
}
/**
* Verifies that the message of the actual {@code Throwable} is equal to the given one.
*
* @param message the expected message.
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} is not equal to the given one.
*/
public SELF hasMessage(String message) {
throwables.assertHasMessage(info, actual, message);
return myself;
}
/**
* Verifies that the message of the actual (@code Throwable) is equal to the given one, after being formatted using
* the {@link String#format} method.
* <p>
* Example:
* <pre><code class='java'> Throwable invalidArgException = new IllegalArgumentException("foo is not a valid input");
* Throwable throwable = new Throwable(invalidArgException);
*
* // This assertion succeeds:
* assertThat(throwable).hasMessage("%s is not a valid input", "foo");
*
* // These assertions fail:
* assertThat(throwable).hasMessage("%s is not a valid input", "bar");
* assertThat(throwable).hasMessage("%s is not a valid input", 12);
* assertThat(null).hasMessage("%s is not a valid input", "foo");</code></pre>
*
* @param message a format string representing the expected message
* @param parameters argument referenced by the format specifiers in the format string
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} is not equal to the given one.
* @throws IllegalFormatException if the message contains an illegal syntax according to {@link String#format(String, Object...)}.
*/
public SELF hasMessage(String message, Object... parameters) {
return hasMessage(String.format(message, parameters));
}
/**
* Verifies that the actual {@code Throwable} has a cause similar to the given one, that is with the same type and message
* (it does not use the {@link Throwable#equals(Object) equals} method for comparison).
*
* Example:
* <pre><code class='java'> Throwable invalidArgException = new IllegalArgumentException("invalid arg");
* Throwable throwable = new Throwable(invalidArgException);
*
* // This assertion succeeds:
* assertThat(throwable).hasCause(invalidArgException);
*
* // These assertions fail:
* assertThat(throwable).hasCause(new IllegalArgumentException("bad arg"));
* assertThat(throwable).hasCause(new NullPointerException());
* assertThat(throwable).hasCause(null); // prefer hasNoCause()</code></pre>
*
* @param cause the expected cause
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the actual {@code Throwable} has not the given cause.
*/
public SELF hasCause(Throwable cause) {
throwables.assertHasCause(info, actual, cause);
return myself;
}
/**
* Verifies that the actual {@code Throwable} does not have a cause.
*
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the actual {@code Throwable} has a cause.
*/
public SELF hasNoCause() {
throwables.assertHasNoCause(info, actual);
return myself;
}
/**
* Verifies that the message of the actual {@code Throwable} starts with the given description.
*
* @param description the description expected to start the actual {@code Throwable}'s message.
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} does not start with the given description.
*/
public SELF hasMessageStartingWith(String description) {
throwables.assertHasMessageStartingWith(info, actual, description);
return myself;
}
/**
* Verifies that the message of the actual {@code Throwable} contains the given description.
* <p>
* Examples:
* <pre><code class='java'> Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123");
* Throwable throwableWithoutMessage = new IllegalArgumentException();
*
* // assertion will pass:
* assertThat(throwableWithMessage).hasMessageContaining("123");
*
* // assertions will fail:
* assertThat(throwableWithoutMessage).hasMessageContaining("123");
* assertThat(throwableWithMessage).hasMessageContaining("234"); </code></pre>
*
* @param description the description expected to be contained in the actual {@code Throwable}'s message.
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} does not contain the given description.
*/
public SELF hasMessageContaining(String description) {
throwables.assertHasMessageContaining(info, actual, description);
return myself;
}
/**
* Verifies that the message of the actual {@code Throwable} does not contain the given content or is {@code null}.
* <p>
* Examples:
* <pre><code class='java'> Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123");
* Throwable throwableWithoutMessage = new IllegalArgumentException();
*
* // assertions will pass:
* assertThat(throwableWithMessage).hasMessageNotContaining("234");
* assertThat(throwableWithoutMessage).hasMessageNotContaining("foo");
*
* // assertion will fail:
* assertThat(throwableWithMessage).hasMessageNotContaining("amount");</code></pre>
*
* @param content the content expected not to be contained in the actual {@code Throwable}'s message.
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} contains the given content.
*/
public SELF hasMessageNotContaining(String content) {
throwables.assertHasMessageNotContaining(info, actual, content);
return myself;
}
/**
* Verifies that the stack trace of the actual {@code Throwable} contains the given description.
*
* @param description the description expected to be contained in the actual {@code Throwable}'s stack trace.
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the stack trace of the actual {@code Throwable} does not contain the given description.
*/
public SELF hasStackTraceContaining(String description) {
throwables.assertHasStackTraceContaining(info, actual, description);
return myself;
}
/**
* Verifies that the message of the actual {@code Throwable} matches the given regular expression.
* <p>
* Examples:
* <pre><code class='java'> Throwable throwable = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass
* assertThat(throwable).hasMessageMatching("wrong amount [0-9]*");
*
* // assertion will fail
* assertThat(throwable).hasMessageMatching("wrong amount [0-9]* euros");</code></pre>
*
* @param regex the regular expression of value expected to be matched the actual {@code Throwable}'s message.
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} does not match the given regular expression.
* @throws NullPointerException if the regex is null
*/
public SELF hasMessageMatching(String regex) {
throwables.assertHasMessageMatching(info, actual, regex);
return myself;
}
/**
* Verifies that a sequence of the message of the actual {@code Throwable} matches with
* the given regular expression (see {@link java.util.regex.Matcher#find()}).
* The Pattern used under the hood enables the {@link Pattern#DOTALL} mode.
*
* <p>
* Examples:
* <pre><code class='java'>
* Throwable throwable = new IllegalArgumentException("Dear John,\n" +
* "it' s a wrong amount");
* // assertion will pass
* assertThat(throwable).hasMessageFindingMatch("wrong amount");
* assertThat(throwable).hasMessageFindingMatch("Dear John");
* assertThat(throwable).hasMessageFindingMatch("wrong amount$");
* // assertion will fail
* assertThat(throwable).hasMessageFindingMatch("Dear John$");
*
* </code></pre>
*
* @param regex the regular expression expected to be found in the actual {@code Throwable}'s message.
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} doesn't contain any sequence matching with the given regular expression
* @throws NullPointerException if the regex is null
*/
public SELF hasMessageFindingMatch(String regex) {
throwables.assertHasMessageFindingMatch(info, actual, regex);
return myself;
}
/**
* Verifies that the message of the actual {@code Throwable} ends with the given description.
*
* @param description the description expected to end the actual {@code Throwable}'s message.
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} does not end with the given description.
*/
public SELF hasMessageEndingWith(String description) {
throwables.assertHasMessageEndingWith(info, actual, description);
return myself;
}
/**
* Verifies that the cause of the actual {@code Throwable} is an instance of the given type.
* <p>
* Example:
* <pre><code class='java'> Throwable throwable = new Throwable(new NullPointerException());
*
* // assertions will pass
* assertThat(throwable).hasCauseInstanceOf(NullPointerException.class);
* assertThat(throwable).hasCauseInstanceOf(RuntimeException.class);
*
* // assertion will fail
* assertThat(throwable).hasCauseInstanceOf(IllegalArgumentException.class);</code></pre>
*
* @param type the expected cause type.
* @return this assertion object.
* @throws NullPointerException if given type is {@code null}.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the actual {@code Throwable} has no cause.
* @throws AssertionError if the cause of the actual {@code Throwable} is not an instance of the given type.
*/
public SELF hasCauseInstanceOf(Class<? extends Throwable> type) {
throwables.assertHasCauseInstanceOf(info, actual, type);
return myself;
}
/**
* Verifies that the cause of the actual {@code Throwable} is <b>exactly</b> an instance of the given type.
* <p>
* Example:
* <pre><code class='java'> Throwable throwable = new Throwable(new NullPointerException());
*
* // assertion will pass
* assertThat(throwable).hasCauseExactlyInstanceOf(NullPointerException.class);
*
* // assertions will fail (even if NullPointerException is a RuntimeException since we want an exact match)
* assertThat(throwable).hasCauseExactlyInstanceOf(RuntimeException.class);
* assertThat(throwable).hasCauseExactlyInstanceOf(IllegalArgumentException.class);</code></pre>
*
* @param type the expected cause type.
* @return this assertion object.
* @throws NullPointerException if given type is {@code null}.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the actual {@code Throwable} has no cause.
* @throws AssertionError if the cause of the actual {@code Throwable} is not <b>exactly</b> an instance of the given
* type.
*/
public SELF hasCauseExactlyInstanceOf(Class<? extends Throwable> type) {
throwables.assertHasCauseExactlyInstanceOf(info, actual, type);
return myself;
}
/**
* Verifies that the root cause of the actual {@code Throwable} is an instance of the given type.
* <p>
* Example:
* <pre><code class='java'> Throwable throwable = new Throwable(new IllegalStateException(new NullPointerException()));
*
* // assertions will pass
* assertThat(throwable).hasRootCauseInstanceOf(NullPointerException.class);
* assertThat(throwable).hasRootCauseInstanceOf(RuntimeException.class);
*
* // assertion will fail
* assertThat(throwable).hasRootCauseInstanceOf(IllegalStateException.class);</code></pre>
*
* @param type the expected cause type.
* @return this assertion object.
* @throws NullPointerException if given type is {@code null}.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the actual {@code Throwable} has no cause.
* @throws AssertionError if the cause of the actual {@code Throwable} is not an instance of the given type.
*/
public SELF hasRootCauseInstanceOf(Class<? extends Throwable> type) {
throwables.assertHasRootCauseInstanceOf(info, actual, type);
return myself;
}
/**
* Verifies that the root cause of the actual {@code Throwable} is <b>exactly</b> an instance of the given type.
* <p>
* Example:
* <pre><code class='java'> Throwable throwable = new Throwable(new IllegalStateException(new NullPointerException()));
*
* // assertion will pass
* assertThat(throwable).hasRootCauseExactlyInstanceOf(NullPointerException.class);
*
* // assertions will fail (even if NullPointerException is a RuntimeException since we want an exact match)
* assertThat(throwable).hasRootCauseExactlyInstanceOf(RuntimeException.class);
* assertThat(throwable).hasRootCauseExactlyInstanceOf(IllegalStateException.class);</code></pre>
*
* @param type the expected cause type.
* @return this assertion object.
* @throws NullPointerException if given type is {@code null}.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the actual {@code Throwable} has no cause.
* @throws AssertionError if the root cause of the actual {@code Throwable} is not <b>exactly</b> an instance of the
* given type.
*/
public SELF hasRootCauseExactlyInstanceOf(Class<? extends Throwable> type) {
throwables.assertHasRootCauseExactlyInstanceOf(info, actual, type);
return myself;
}
/**
* Verifies that the actual {@code Throwable} has no suppressed exceptions.
* <p>
* Example:
* <pre><code class='java'> // assertion will pass
* assertThat(new Throwable()).hasNoSuppressedExceptions();
*
* // assertion will fail
* Throwable throwableWithSuppressedException = new Throwable();
* throwableWithSuppressedException.addSuppressed(new IllegalArgumentException());
* assertThat(throwableWithSuppressedException).hasNoSuppressedExceptions();</code></pre>
*
* @return this assertion object.
* @throws NullPointerException if given type is {@code null}.
* @throws AssertionError if the actual {@code Throwable} has any suppressed exceptions.
* @since 2.6.0 / 3.6.0
*/
public SELF hasNoSuppressedExceptions() {
throwables.assertHasNoSuppressedExceptions(info, actual);
return myself;
}
/**
* Verifies that the actual {@code Throwable} has a suppressed exception similar to the given one, that is with the same type and message
* (it does not use the {@link Throwable#equals(Object) equals} method for comparison).
*
* Example:
* <pre><code class='java'> Throwable throwable = new Throwable();
* Throwable invalidArgException = new IllegalArgumentException("invalid argument");
* throwable.addSuppressed(invalidArgException);
*
* // These assertions succeed:
* assertThat(throwable).hasSuppressedException(invalidArgException);
* assertThat(throwable).hasSuppressedException(new IllegalArgumentException("invalid argument"));
*
* // These assertions fail:
* assertThat(throwable).hasSuppressedException(new IllegalArgumentException("invalid parameter"));
* assertThat(throwable).hasSuppressedException(new NullPointerException());</code></pre>
*
* @param suppressedException the expected suppressed exception
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the actual {@code Throwable} does not have the given suppressed exception.
* @since 2.6.0 / 3.6.0
*/
public SELF hasSuppressedException(Throwable suppressedException) {
throwables.assertHasSuppressedException(info, actual, suppressedException);
return myself;
}
/**
* Verifies that the {@link org.assertj.core.api.ThrowableAssert.ThrowingCallable} didn't raise a throwable.
* <p>
* Example :
* <pre><code class='java'> assertThatCode(() -> foo.bar()).doesNotThrowAnyException();</code></pre>
*
* @throws AssertionError if the actual statement raised a {@code Throwable}.
* @since 3.7.0
*/
public void doesNotThrowAnyException() {
if (actual != null) throw Failures.instance().failure(info, shouldNotHaveThrown(actual));
}
}