1b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpackage org.junit.experimental.results; 2b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 3b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.hamcrest.BaseMatcher; 4b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.hamcrest.Description; 5b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.hamcrest.Matcher; 6b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.internal.matchers.TypeSafeMatcher; 7b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 8b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot/** 9b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Matchers on a PrintableResult, to enable JUnit self-tests. 10b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * For example: 11b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * 12b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * <pre> 13b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * assertThat(testResult(HasExpectedException.class), isSuccessful()); 14b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * </pre> 15b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 16b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpublic class ResultMatchers { 17b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 18b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Matches if the tests are all successful 19b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 20b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public static Matcher<PrintableResult> isSuccessful() { 21b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return failureCountIs(0); 22b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 23b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 24b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 25b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Matches if there are {@code count} failures 26b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 27b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public static Matcher<PrintableResult> failureCountIs(final int count) { 28b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return new TypeSafeMatcher<PrintableResult>() { 29b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public void describeTo(Description description) { 30b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot description.appendText("has " + count + " failures"); 31b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 32b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 33b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot @Override 34b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public boolean matchesSafely(PrintableResult item) { 35b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return item.failureCount() == count; 36b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 37b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot }; 38b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 39b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 40b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 41b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Matches if the result has exactly one failure, and it contains {@code string} 42b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 43b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public static Matcher<Object> hasSingleFailureContaining(final String string) { 44b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return new BaseMatcher<Object>() { 45b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public boolean matches(Object item) { 46b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return item.toString().contains(string) && failureCountIs(1).matches(item); 47b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 48b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 49b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public void describeTo(Description description) { 50b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot description.appendText("has single failure containing " + string); 51b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 52b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot }; 53b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 54b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 55b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 56b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Matches if the result has one or more failures, and at least one of them 57b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * contains {@code string} 58b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 59b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public static Matcher<PrintableResult> hasFailureContaining(final String string) { 60b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return new BaseMatcher<PrintableResult>() { 61b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public boolean matches(Object item) { 62b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return item.toString().contains(string); 63b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 64b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 65b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public void describeTo(Description description) { 66b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot description.appendText("has failure containing " + string); 67b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 68b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot }; 69b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 70b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot} 71