Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ExpectedException uses builder instead of custom matcher

  • Loading branch information...
commit ec1a5c3ffcc4b9b0dda6e9a683ea51103f55853e 1 parent a7c4d03
Marc Philipp marcphilipp authored
18 src/main/java/org/junit/rules/ExpectedException.java
View
@@ -3,6 +3,8 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
+import static org.junit.internal.matchers.ThrowableCauseMatcher.hasCause;
+import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.junit.internal.AssumptionViolatedException;
@@ -86,7 +88,7 @@ public static ExpectedException none() {
return new ExpectedException();
}
- private final ExpectedExceptionMatcher fMatcher= new ExpectedExceptionMatcher();
+ private final ExpectedExceptionMatcherBuilder fMatcherBuilder= new ExpectedExceptionMatcherBuilder();
private boolean handleAssumptionViolatedExceptions= false;
@@ -115,7 +117,7 @@ public Statement apply(Statement base,
* exception.
*/
public void expect(Matcher<?> matcher) {
- fMatcher.andAlso(matcher);
+ fMatcherBuilder.add(matcher);
}
/**
@@ -139,7 +141,7 @@ public void expectMessage(String substring) {
* from any thrown exception.
*/
public void expectMessage(Matcher<String> matcher) {
- fMatcher.andAlsoHasMessage(matcher);
+ expect(hasMessage(matcher));
}
/**
@@ -147,7 +149,7 @@ public void expectMessage(Matcher<String> matcher) {
* any thrown exception.
*/
public void expectCause(Matcher<? extends Throwable> expectedCause) {
- fMatcher.andAlsoHasCause(expectedCause);
+ expect(hasCause(expectedCause));
}
private class ExpectedExceptionStatement extends Statement {
@@ -171,9 +173,9 @@ public void evaluate() throws Throwable {
handleException(e);
return;
}
- if (fMatcher.expectsThrowable())
+ if (fMatcherBuilder.expectsThrowable())
throw new AssertionError("Expected test to throw "
- + StringDescription.toString(fMatcher));
+ + StringDescription.toString(fMatcherBuilder.build()));
}
}
@@ -186,8 +188,8 @@ private void optionallyHandleException(Throwable e, boolean handleException)
}
private void handleException(Throwable e) throws Throwable {
- if (fMatcher.expectsThrowable())
- assertThat(e, fMatcher);
+ if (fMatcherBuilder.expectsThrowable())
+ assertThat(e, fMatcherBuilder.build());
else
throw e;
}
88 src/main/java/org/junit/rules/ExpectedExceptionMatcher.java
View
@@ -1,88 +0,0 @@
-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;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.hamcrest.Description;
-import org.hamcrest.Matcher;
-import org.hamcrest.TypeSafeMatcher;
-
-/**
- * Special matcher used by {@link ExpectedException}.
- */
-class ExpectedExceptionMatcher extends TypeSafeMatcher<Throwable> {
-
- private final List<Matcher<?>> fMatchers= new ArrayList<Matcher<?>>();
-
- private Matcher<Throwable> fCompositeMatcher;
-
- void andAlso(Matcher<?> matcher) {
- fMatchers.add(matcher);
- fCompositeMatcher= null;
- }
-
- void andAlsoHasMessage(Matcher<String> matcher) {
- andAlso(hasMessage(matcher));
- }
-
- void andAlsoHasCause(Matcher<? extends Throwable> matcher) {
- andAlso(hasCause(matcher));
- }
-
- boolean expectsThrowable() {
- return !fMatchers.isEmpty();
- }
-
- @Override
- protected boolean matchesSafely(Throwable item) {
- return getCompositeMatcher().matches(item);
- }
-
- @Override
- protected void describeMismatchSafely(Throwable item,
- Description mismatchDescription) {
- getCompositeMatcher().describeMismatch(item, mismatchDescription);
- }
-
- public void describeTo(Description description) {
- getCompositeMatcher().describeTo(description);
- }
-
- private Matcher<Throwable> getCompositeMatcher() {
- if (fCompositeMatcher == null)
- fCompositeMatcher= createCompositeMatcher();
- return fCompositeMatcher;
- }
-
- private Matcher<Throwable> createCompositeMatcher() {
- return isThrowable(allOfTheMatchers());
- }
-
- private Matcher<Throwable> allOfTheMatchers() {
- if (fMatchers.size() == 1) {
- return cast(fMatchers.get(0));
- }
- return allOf(castedMatchers());
- }
-
- private List<Matcher<? super Throwable>> castedMatchers() {
- List<Matcher<? super Throwable>> castedMatchers= new LinkedList<Matcher<? super Throwable>>();
- for (Matcher<?> matcher : fMatchers) {
- castedMatchers.add(cast(matcher));
- }
- return castedMatchers;
- }
-
- // Should be able to remove this suppression in some brave new hamcrest
- // world.
- @SuppressWarnings("unchecked")
- private Matcher<Throwable> cast(Matcher<?> singleMatcher) {
- return (Matcher<Throwable>) singleMatcher;
- }
-}
46 src/main/java/org/junit/rules/ExpectedExceptionMatcherBuilder.java
View
@@ -0,0 +1,46 @@
+package org.junit.rules;
+
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.junit.matchers.JUnitMatchers.isThrowable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.hamcrest.Matcher;
+
+/**
+ * Builds special matcher used by {@link ExpectedException}.
+ */
+class ExpectedExceptionMatcherBuilder {
+
+ private final List<Matcher<?>> fMatchers= new ArrayList<Matcher<?>>();
+
+ void add(Matcher<?> matcher) {
+ fMatchers.add(matcher);
+ }
+
+ boolean expectsThrowable() {
+ return !fMatchers.isEmpty();
+ }
+
+ Matcher<Throwable> build() {
+ return isThrowable(allOfTheMatchers());
+ }
+
+ private Matcher<Throwable> allOfTheMatchers() {
+ if (fMatchers.size() == 1) {
+ return cast(fMatchers.get(0));
+ }
+ return allOf(castedMatchers());
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ private List<Matcher<? super Throwable>> castedMatchers() {
+ return new ArrayList<Matcher<? super Throwable>>((List) fMatchers);
+ }
+
+ @SuppressWarnings("unchecked")
+ private Matcher<Throwable> cast(Matcher<?> singleMatcher) {
+ return (Matcher<Throwable>) singleMatcher;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.