• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit.internal.matchers;
2 
3 import org.hamcrest.Description;
4 import org.hamcrest.Factory;
5 import org.hamcrest.Matcher;
6 import org.hamcrest.TypeSafeMatcher;
7 
8 public class ThrowableMessageMatcher<T extends Throwable> extends
9         TypeSafeMatcher<T> {
10 
11     private final Matcher<String> matcher;
12 
ThrowableMessageMatcher(Matcher<String> matcher)13     public ThrowableMessageMatcher(Matcher<String> matcher) {
14         this.matcher = matcher;
15     }
16 
describeTo(Description description)17     public void describeTo(Description description) {
18         description.appendText("exception with message ");
19         description.appendDescriptionOf(matcher);
20     }
21 
22     @Override
matchesSafely(T item)23     protected boolean matchesSafely(T item) {
24         return matcher.matches(item.getMessage());
25     }
26 
27     @Override
describeMismatchSafely(T item, Description description)28     protected void describeMismatchSafely(T item, Description description) {
29         description.appendText("message ");
30         matcher.describeMismatch(item.getMessage(), description);
31     }
32 
33     @Factory
hasMessage(final Matcher<String> matcher)34     public static <T extends Throwable> Matcher<T> hasMessage(final Matcher<String> matcher) {
35         return new ThrowableMessageMatcher<T>(matcher);
36     }
37 }