    001    package org.junit.experimental.results;
    002
    003    import org.hamcrest.BaseMatcher;
    004    import org.hamcrest.Description;
    005    import org.hamcrest.Matcher;
    006    import org.hamcrest.TypeSafeMatcher;
    007    import org.junit.runner.notification.Failure;
    008
    009    /**
    010     * Matchers on a PrintableResult, to enable JUnit self-tests.
    011     * For example:
    012     *
    013     * <pre>
    014     * assertThat(testResult(HasExpectedException.class), isSuccessful());
    015     * </pre>
    016     */
    017    public class ResultMatchers {
    018
    019        /**
    020         * Do not instantiate.
    021         * @deprecated will be private soon.
    022         */
    023        @Deprecated
    024        public ResultMatchers() {
    025        }
    026
    027        /**
    028         * Matches if the tests are all successful
    029         */
    030        public static Matcher<PrintableResult> isSuccessful() {
    031            return failureCountIs(0);
    032        }
    033
    034        /**
    035         * Matches if there are {@code count} failures
    036         */
    037        public static Matcher<PrintableResult> failureCountIs(final int count) {
    038            return new TypeSafeMatcher<PrintableResult>() {
    039                public void describeTo(Description description) {
    040                    description.appendText("has " + count + " failures");
    041                }
    042
    043                @Override
    044                public boolean matchesSafely(PrintableResult item) {
    045                    return item.failureCount() == count;
    046                }
    047            };
    048        }
    049
    050        /**
    051         * Matches if the result has exactly one failure, and it contains {@code string}
    052         */
    053        public static Matcher<Object> hasSingleFailureContaining(final String string) {
    054            return new BaseMatcher<Object>() {
    055                public boolean matches(Object item) {
    056                    return item.toString().contains(string) && failureCountIs(1).matches(item);
    057                }
    058
    059                public void describeTo(Description description) {
    060                    description.appendText("has single failure containing " + string);
    061                }
    062            };
    063        }
    064
    065        /**
    066         * Matches if the result has exactly one failure matching the given matcher.
    067         *
    068         * @since 4.13
    069         */
    070        public static Matcher<PrintableResult> hasSingleFailureMatching(final Matcher<Throwable> matcher) {
    071            return new TypeSafeMatcher<PrintableResult>() {
    072                @Override
    073                public boolean matchesSafely(PrintableResult item) {
    074                    return item.failureCount() == 1 && matcher.matches(item.failures().get(0).getException());
    075                }
    076
    077                public void describeTo(Description description) {
    078                    description.appendText("has failure with exception matching ");
    079                    matcher.describeTo(description);
    080                }
    081            };
    082        }
    083
    084        /**
    085         * Matches if the result has one or more failures, and at least one of them
    086         * contains {@code string}
    087         */
    088        public static Matcher<PrintableResult> hasFailureContaining(final String string) {
    089            return new TypeSafeMatcher<PrintableResult>() {
    090                @Override
    091                public boolean matchesSafely(PrintableResult item) {
    092                    return item.failureCount() > 0 && item.toString().contains(string);
    093                }
    094
    095                public void describeTo(Description description) {
    096                    description.appendText("has failure containing " + string);
    097                }
    098            };
    099        }
    100    }