Skip to content
Browse files

Extract matchers to separate classes (refactoring)

  • Loading branch information...
1 parent 5e9467c commit a7c4d033c831765b5e651f2c7c4ce69c2b60ce4a @marcphilipp marcphilipp committed
View
37 src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java
@@ -0,0 +1,37 @@
+package org.junit.internal.matchers;
+
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+public class ThrowableCauseMatcher<T extends Throwable> extends
+ TypeSafeMatcher<T> {
+
+ private final Matcher<T> fMatcher;
+
+ public ThrowableCauseMatcher(Matcher<T> matcher) {
+ fMatcher= matcher;
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("exception with cause ");
+ description.appendDescriptionOf(fMatcher);
+ }
+
+ @Override
+ protected boolean matchesSafely(T item) {
+ return fMatcher.matches(item.getCause());
+ }
+
+ @Override
+ protected void describeMismatchSafely(T item, Description description) {
+ description.appendText("cause ");
+ fMatcher.describeMismatch(item.getCause(), description);
+ }
+
+ @Factory
+ public static <T extends Throwable> Matcher<T> hasCause(final Matcher<T> matcher) {
+ return new ThrowableCauseMatcher<T>(matcher);
+ }
+}
View
37 src/main/java/org/junit/internal/matchers/ThrowableMessageMatcher.java
@@ -0,0 +1,37 @@
+package org.junit.internal.matchers;
+
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+public class ThrowableMessageMatcher<T extends Throwable> extends
+ TypeSafeMatcher<T> {
+
+ private final Matcher<String> fMatcher;
+
+ public ThrowableMessageMatcher(Matcher<String> matcher) {
+ fMatcher= matcher;
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("exception with message ");
+ description.appendDescriptionOf(fMatcher);
+ }
+
+ @Override
+ protected boolean matchesSafely(T item) {
+ return fMatcher.matches(item.getMessage());
+ }
+
+ @Override
+ protected void describeMismatchSafely(T item, Description description) {
+ description.appendText("message ");
+ fMatcher.describeMismatch(item.getMessage(), description);
+ }
+
+ @Factory
+ public static <T extends Throwable> Matcher<T> hasMessage(final Matcher<String> matcher) {
+ return new ThrowableMessageMatcher<T>(matcher);
+ }
+}
View
45 src/main/java/org/junit/rules/ExpectedExceptionMatcher.java
@@ -1,6 +1,8 @@
package org.junit.rules;
import static org.hamcrest.CoreMatchers.allOf;
+import static org.junit.internal.matchers.ThrowableCauseMatcher.hasCause;
+import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage;
import static org.junit.matchers.JUnitMatchers.isThrowable;
import java.util.ArrayList;
@@ -9,7 +11,6 @@
import org.hamcrest.Description;
import org.hamcrest.Matcher;
-import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.TypeSafeMatcher;
/**
@@ -84,46 +85,4 @@ public void describeTo(Description description) {
private Matcher<Throwable> cast(Matcher<?> singleMatcher) {
return (Matcher<Throwable>) singleMatcher;
}
-
- private Matcher<Throwable> hasMessage(final Matcher<String> matcher) {
- return new TypeSafeMatcher<Throwable>() {
- public void describeTo(Description description) {
- description.appendText("exception with message ");
- description.appendDescriptionOf(matcher);
- }
-
- @Override
- protected boolean matchesSafely(Throwable item) {
- return matcher.matches(item.getMessage());
- }
-
- @Override
- protected void describeMismatchSafely(Throwable item,
- Description mismatchDescription) {
- mismatchDescription.appendText("message ");
- matcher.describeMismatch(item.getMessage(), mismatchDescription);
- }
- };
- }
-
- private Matcher<Throwable> hasCause(final Matcher<? extends Throwable> matcher) {
- return new TypeSafeMatcher<Throwable>() {
- public void describeTo(Description description) {
- description.appendText("exception with cause ");
- description.appendDescriptionOf(matcher);
- }
-
- @Override
- protected boolean matchesSafely(Throwable item) {
- return matcher.matches(item.getCause());
- }
-
- @Override
- protected void describeMismatchSafely(Throwable item,
- Description mismatchDescription) {
- mismatchDescription.appendText("cause ");
- matcher.describeMismatch(item.getCause(), mismatchDescription);
- }
- };
- }
}

0 comments on commit a7c4d03

Please sign in to comment.
Something went wrong with that request. Please try again.