1package com.bluecatcode.hamcrest.matchers;2import org.hamcrest.CustomTypeSafeMatcher;3import org.hamcrest.Description;4import org.hamcrest.Factory;5import org.hamcrest.Matcher;6import static org.hamcrest.Matchers.allOf;7import static org.hamcrest.Matchers.is;8import static org.hamcrest.core.IsInstanceOf.instanceOf;9public class IsThrowable<T extends Throwable> extends CustomTypeSafeMatcher<T> {10 private final Matcher<T> matcher;11 public IsThrowable(Matcher<T> matcher) {12 super(CustomMatchers.fixedDescription(matcher));13 this.matcher = matcher;14 }15 @Override16 protected boolean matchesSafely(T item) {17 return matcher.matches(item);18 }19 public void describeMismatchSafely(T item, Description mismatchDescription) {20 matcher.describeMismatch(item, mismatchDescription);21 }22 /**23 * Matches if value is a Throwable of type <tt>type</tt>24 *25 * @param type throwable type26 * @param <T> the throwable type27 * @return the matcher28 */29 @Factory30 public static <T extends Throwable> Matcher<T> isThrowable(Class<?> type) {31 final Matcher<T> typeMatcher = instanceOf(type);32 return new IsThrowable<>(typeMatcher);33 }34 /**35 * Matches if value is a Throwable of type <tt>type</tt> that matches the <tt>matcher</tt>36 *37 * @param type throwable type38 * @param matcher throwable matcher39 * @param <T> the throwable type40 * @return the matcher41 */42 @Factory43 public static <T extends Throwable> Matcher<T> isThrowable(Class<?> type, Matcher<? super T> matcher) {44 final Matcher<T> typeMatcher = instanceOf(type);45 return new IsThrowable<>(allOf(typeMatcher, matcher));46 }47 /**48 * Matches if value is a throwable with the <tt>message</tt>49 *50 * @param message message to match51 * @param <T> the throwable type52 * @return the matcher53 */54 @Factory55 public static <T extends Throwable> Matcher<T> withMessage(String message) {56 return withMessage(is(message));57 }58 /**59 * Matches if value is a throwable with a message that matches the <tt>matcher</tt>60 *61 * @param matcher message matcher62 * @param <T> the throwable type63 * @return the matcher64 */65 @Factory66 public static <T extends Throwable> Matcher<T> withMessage(final Matcher<String> matcher) {67 return new CustomTypeSafeMatcher<T>(CustomMatchers.fixedDescription("message ", matcher)) {68 @Override69 protected boolean matchesSafely(T item) {70 return matcher.matches(item.getMessage());71 }72 public void describeMismatchSafely(T item, Description mismatchDescription) {73 matcher.describeMismatch(item.getMessage(), mismatchDescription);74 }75 };76 }77 /**78 * Matches if value is a throwable with a cause that matches the <tt>matcher</tt>79 *80 * @param matcher cause matcher81 * @param <T> the throwable type82 * @param <C> the cause throwable type83 * @return the matcher84 */85 @Factory86 public static <T extends Throwable, C extends Throwable> Matcher<T> withCause(final Matcher<C> matcher) {87 return new CustomTypeSafeMatcher<T>(CustomMatchers.fixedDescription("cause ", matcher)) {88 @Override89 protected boolean matchesSafely(T item) {90 return matcher.matches(item.getCause());91 }92 public void describeMismatchSafely(T item, Description mismatchDescription) {93 matcher.describeMismatch(item.getCause(), mismatchDescription);94 }95 };96 }97}...