blob: e7d94c483f4aeb3fab812322fb123be79eeeb96f (
plain)
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
|
package org.junit.rules;
import static org.hamcrest.CoreMatchers.allOf;
import static org.junit.matchers.JUnitMatchers.isThrowable;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.Matcher;
/**
* Builds special matcher used by {@link ExpectedException}.
*/
class ExpectedExceptionMatcherBuilder {
private final List<Matcher<?>> matchers = new ArrayList<Matcher<?>>();
void add(Matcher<?> matcher) {
matchers.add(matcher);
}
boolean expectsThrowable() {
return !matchers.isEmpty();
}
Matcher<Throwable> build() {
return isThrowable(allOfTheMatchers());
}
private Matcher<Throwable> allOfTheMatchers() {
if (matchers.size() == 1) {
return cast(matchers.get(0));
}
return allOf(castedMatchers());
}
@SuppressWarnings({"unchecked", "rawtypes"})
private List<Matcher<? super Throwable>> castedMatchers() {
return new ArrayList<Matcher<? super Throwable>>((List) matchers);
}
@SuppressWarnings("unchecked")
private Matcher<Throwable> cast(Matcher<?> singleMatcher) {
return (Matcher<Throwable>) singleMatcher;
}
}
|