View Javadoc

1   package com.atlassian.webdriver.matchers;
2   
3   import org.hamcrest.Description;
4   import org.hamcrest.Matcher;
5   import org.hamcrest.TypeSafeMatcher;
6   import org.junit.runners.model.InitializationError;
7   
8   import static org.hamcrest.Matchers.instanceOf;
9   
10  /**
11   * Matchers for Java errors.
12   *
13   * @since 2.1
14   */
15  public final class ErrorMatchers
16  {
17      private ErrorMatchers()
18      {
19          throw new AssertionError("Don't instantiate me");
20      }
21  
22  
23      // based off ExpectedException internals
24  
25      public static Matcher<Throwable> withMessage(final String... expectedSubstrings)
26      {
27          return withMessage(LangMatchers.containsInOrder(expectedSubstrings));
28      }
29  
30      public static Matcher<Throwable> withMessage(final Matcher<String> messageMatcher)
31      {
32          return new TypeSafeMatcher<Throwable>()
33          {
34              @Override
35              public boolean matchesSafely(Throwable throwable)
36              {
37                  return messageMatcher.matches(throwable.getMessage());
38              }
39  
40              @Override
41              public void describeTo(Description description)
42              {
43                  description.appendText("a Throwable with message ").appendDescriptionOf(messageMatcher);
44              }
45          };
46      }
47  
48      public static Matcher<Throwable> withCause(Class<? extends Throwable> causeType)
49      {
50          // can be fixed when moved to Hamcrest 1.2
51          return withCause(LangMatchers.isInstance(causeType));
52      }
53  
54      public static Matcher<Throwable> withCause(final Matcher<? extends Throwable> causeMatcher)
55      {
56          return new TypeSafeMatcher<Throwable>()
57          {
58              @Override
59              public boolean matchesSafely(Throwable throwable)
60              {
61                  return causeMatcher.matches(throwable.getCause());
62              }
63  
64              @Override
65              public void describeTo(Description description)
66              {
67                  description.appendText("a Throwable with cause (").appendDescriptionOf(causeMatcher).appendText(")");
68              }
69          };
70      }
71  
72      public static <T extends Throwable> Matcher<Throwable> specificError(final Class<T> errorClass, final Matcher<T> specificMatcher)
73      {
74          return new TypeSafeMatcher<Throwable>()
75          {
76              @Override
77              public boolean matchesSafely(Throwable throwable)
78              {
79                  return instanceOf(errorClass).matches(throwable) && specificMatcher.matches(errorClass.cast(throwable));
80              }
81  
82              @Override
83              public void describeTo(Description description)
84              {
85                  description.appendText("an instance of ").appendValue(errorClass)
86                          .appendText(" that is (").appendDescriptionOf(specificMatcher).appendText(")");
87              }
88          };
89      }
90  
91      public static Matcher<InitializationError> withCauses(final Matcher<Iterable<Throwable>> causesMatcher)
92      {
93          return new TypeSafeMatcher<InitializationError>()
94          {
95              @Override
96              public boolean matchesSafely(InitializationError initializationError)
97              {
98                  return causesMatcher.matches(initializationError.getCauses());
99              }
100 
101             @Override
102             public void describeTo(Description description)
103             {
104                 description.appendText("a JUnit InitializationError with causes ").appendDescriptionOf(causesMatcher);
105             }
106         };
107     }
108 
109 }