source: josm/trunk/test/unit/org/openstreetmap/josm/testutils/ThrowableRootCauseMatcher.java@ 18332

Last change on this file since 18332 was 18332, checked in by Don-vip, 3 years ago

see #21596 - Update dependencies in ivy.xml and tools/ivy.xml (patch by taylor.smock)

  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.testutils;
3
4import org.hamcrest.Description;
5import org.hamcrest.Matcher;
6import org.hamcrest.TypeSafeMatcher;
7
8/**
9 * Matches the root cause of a {@code Throwable}.
10 * This has been rejected from JUnit developers (see https://github.com/junit-team/junit4/pull/778),
11 * But this is really useful, thanks to pimterry for implementing it.
12 *
13 * @param <T> Throwable type
14 * @see <a href="https://github.com/junit-team/junit4/pull/778">Github pull request</a>
15 */
16public class ThrowableRootCauseMatcher<T extends Throwable> extends TypeSafeMatcher<T> {
17
18 private final Matcher<T> fMatcher;
19
20 /**
21 * Constructs a new {@code ThrowableRootCauseMatcher}.
22 * @param matcher matcher
23 */
24 public ThrowableRootCauseMatcher(Matcher<T> matcher) {
25 fMatcher = matcher;
26 }
27
28 @Override
29 public void describeTo(Description description) {
30 description.appendText("exception with cause ");
31 description.appendDescriptionOf(fMatcher);
32 }
33
34 @Override
35 protected boolean matchesSafely(T item) {
36 Throwable exception = item;
37 while (exception.getCause() != null) {
38 exception = exception.getCause();
39 }
40 return fMatcher.matches(exception);
41 }
42
43 @Override
44 protected void describeMismatchSafely(T item, Description description) {
45 description.appendText("cause ");
46 fMatcher.describeMismatch(item.getCause(), description);
47 }
48
49 /**
50 * Returns a new {@code ThrowableRootCauseMatcher} instance.
51 * @param <T> Throwable type
52 * @param matcher matcher
53 * @return new {@code ThrowableRootCauseMatcher} instance
54 */
55 public static <T extends Throwable> Matcher<T> hasRootCause(final Matcher<T> matcher) {
56 return new ThrowableRootCauseMatcher<>(matcher);
57 }
58}
Note: See TracBrowser for help on using the repository browser.