aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java
diff options
context:
space:
mode:
authorPaul Duffin <paulduffin@google.com>2017-01-23 14:41:00 +0000
committerPaul Duffin <paulduffin@google.com>2017-01-23 15:34:33 +0000
commitfacdecc8e055d722b8c0e0c8fbf8a8644e7fec57 (patch)
tree9930cc8a22092529188aaa026b3af0681c8c3023 /src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java
parent7323bf635ef4f8c3d3f40e4ee1e4c50652e782c8 (diff)
downloadjunit-facdecc8e055d722b8c0e0c8fbf8a8644e7fec57.tar.gz
Revert "Revert matchers back to 4.10 to compile against Hamcrest 1.1"
This reverts commit 86f323b2a73c9c250fbe25b828435aa936704ab3. Bug: 30946317 Test: make checkbuild Change-Id: Ie72fec656d01f24a1724d33a35ceecdb57aaba57
Diffstat (limited to 'src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java')
-rw-r--r--src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java b/src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java
new file mode 100644
index 0000000..22ce8bd
--- /dev/null
+++ b/src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java
@@ -0,0 +1,50 @@
+package org.junit.internal.matchers;
+
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+/**
+ * A matcher that applies a delegate matcher to the cause of the current Throwable, returning the result of that
+ * match.
+ *
+ * @param <T> the type of the throwable being matched
+ */
+public class ThrowableCauseMatcher<T extends Throwable> extends
+ TypeSafeMatcher<T> {
+
+ private final Matcher<? extends Throwable> causeMatcher;
+
+ public ThrowableCauseMatcher(Matcher<? extends Throwable> causeMatcher) {
+ this.causeMatcher = causeMatcher;
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("exception with cause ");
+ description.appendDescriptionOf(causeMatcher);
+ }
+
+ @Override
+ protected boolean matchesSafely(T item) {
+ return causeMatcher.matches(item.getCause());
+ }
+
+ @Override
+ protected void describeMismatchSafely(T item, Description description) {
+ description.appendText("cause ");
+ causeMatcher.describeMismatch(item.getCause(), description);
+ }
+
+ /**
+ * Returns a matcher that verifies that the outer exception has a cause for which the supplied matcher
+ * evaluates to true.
+ *
+ * @param matcher to apply to the cause of the outer exception
+ * @param <T> type of the outer exception
+ */
+ @Factory
+ public static <T extends Throwable> Matcher<T> hasCause(final Matcher<? extends Throwable> matcher) {
+ return new ThrowableCauseMatcher<T>(matcher);
+ }
+} \ No newline at end of file