diff options
author | Paul Duffin <paulduffin@google.com> | 2016-12-14 11:49:43 +0000 |
---|---|---|
committer | Paul Duffin <paulduffin@google.com> | 2016-12-20 15:52:52 +0000 |
commit | aeb93fc33cae3aadbb9b46083350ad2dc9aea645 (patch) | |
tree | b316db7dee11d1aeee3510562e036fd41705b8b5 /src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java | |
parent | 26401927b83770db45f00706ccc589955644c6c2 (diff) | |
download | junit-aeb93fc33cae3aadbb9b46083350ad2dc9aea645.tar.gz |
Upgrade to JUnit 4.12
The license has changed from Common Public License v1.0 to
Eclipse Public License v1.0.
This will not compile as it is because it is intended to be
built against Hamcrest 1.3 or later but it is being built
against Hamcrest 1.1.
A follow on patch will fix the compilation errors so that it
builds against Hamcrest 1.1. That allows Hamcrest to be upgraded
separately. The patch can be reverted once Hamcrest has been
upgraded.
There are also some Android specific issues that will also be
fixed in follow on patches.
Bug: 33613916
Test: make checkbuild
Change-Id: Ic2c983a030399e3ace1a14927cb143fbd8307b4f
Diffstat (limited to 'src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java')
-rw-r--r-- | src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java | 50 |
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 |