Permalink
Browse files

Lazy init for composite matcher

  • Loading branch information...
1 parent 2d886e2 commit 6bea82df32f687d636f1b843a1918a3a1ff22a6e @marcphilipp marcphilipp committed Jul 19, 2012
Showing with 20 additions and 9 deletions.
  1. +20 −9 src/main/java/org/junit/rules/ExpectedExceptionMatcher.java
@@ -15,13 +15,14 @@
* Special matcher used by {@link ExpectedException}.
*/
class ExpectedExceptionMatcher extends TypeSafeMatcher<Throwable> {
-
- private final List<Matcher<?>> fMatchers = new ArrayList<Matcher<?>>();
+
+ private final List<Matcher<?>> fMatchers= new ArrayList<Matcher<?>>();
+
private Matcher<Throwable> fCompositeMatcher;
-
+
void and(Matcher<?> matcher) {
fMatchers.add(matcher);
- fCompositeMatcher= withStacktrace(createCompositeMatcher());
+ fCompositeMatcher= null;
}
void andHasMessage(Matcher<String> matcher) {
@@ -38,28 +39,38 @@ boolean expectsThrowable() {
@Override
protected boolean matchesSafely(Throwable item) {
- return fCompositeMatcher.matches(item);
+ return getCompositeMatcher().matches(item);
}
-
+
@Override
protected void describeMismatchSafely(Throwable item,
Description mismatchDescription) {
- fCompositeMatcher.describeMismatch(item, mismatchDescription);
+ getCompositeMatcher().describeMismatch(item, mismatchDescription);
}
public void describeTo(Description description) {
- fCompositeMatcher.describeTo(description);
+ getCompositeMatcher().describeTo(description);
+ }
+
+ private Matcher<Throwable> getCompositeMatcher() {
+ if (fCompositeMatcher == null)
+ fCompositeMatcher= createCompositeMatcher();
+ return fCompositeMatcher;
}
private Matcher<Throwable> createCompositeMatcher() {
+ return withStacktrace(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>>();
+ List<Matcher<? super Throwable>> castedMatchers= new LinkedList<Matcher<? super Throwable>>();
for (Matcher<?> matcher : fMatchers) {
castedMatchers.add(cast(matcher));
}

0 comments on commit 6bea82d

Please sign in to comment.