org.junit.rules.ExpectedExceptionMatcherBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of virtdata-lib-realer Show documentation
Show all versions of virtdata-lib-realer Show documentation
With inspiration from other libraries
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> matchers = new ArrayList>();
void add(Matcher> matcher) {
matchers.add(matcher);
}
boolean expectsThrowable() {
return !matchers.isEmpty();
}
Matcher build() {
return isThrowable(allOfTheMatchers());
}
private Matcher allOfTheMatchers() {
if (matchers.size() == 1) {
return cast(matchers.get(0));
}
return allOf(castedMatchers());
}
@SuppressWarnings({"unchecked", "rawtypes"})
private List> castedMatchers() {
return new ArrayList>((List) matchers);
}
@SuppressWarnings("unchecked")
private Matcher cast(Matcher> singleMatcher) {
return (Matcher) singleMatcher;
}
}