001package org.junit.matchers; 002 003import org.hamcrest.CoreMatchers; 004import org.hamcrest.Matcher; 005import org.hamcrest.core.CombinableMatcher.CombinableBothMatcher; 006import org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher; 007import org.junit.internal.matchers.StacktracePrintingMatcher; 008 009/** 010 * Convenience import class: these are useful matchers for use with the assertThat method, but they are 011 * not currently included in the basic CoreMatchers class from hamcrest. 012 * 013 * @since 4.4 014 */ 015public class JUnitMatchers { 016 /** 017 * @return A matcher matching any collection containing element 018 * @deprecated Please use {@link CoreMatchers#hasItem(Object)} instead. 019 */ 020 @Deprecated 021 public static <T> Matcher<Iterable<? super T>> hasItem(T element) { 022 return CoreMatchers.hasItem(element); 023 } 024 025 /** 026 * @return A matcher matching any collection containing an element matching elementMatcher 027 * @deprecated Please use {@link CoreMatchers#hasItem(Matcher)} instead. 028 */ 029 @Deprecated 030 public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> elementMatcher) { 031 return CoreMatchers.<T>hasItem(elementMatcher); 032 } 033 034 /** 035 * @return A matcher matching any collection containing every element in elements 036 * @deprecated Please use {@link CoreMatchers#hasItems(Object...)} instead. 037 */ 038 @Deprecated 039 public static <T> Matcher<Iterable<T>> hasItems(T... elements) { 040 return CoreMatchers.hasItems(elements); 041 } 042 043 /** 044 * @return A matcher matching any collection containing at least one element that matches 045 * each matcher in elementMatcher (this may be one element matching all matchers, 046 * or different elements matching each matcher) 047 * @deprecated Please use {@link CoreMatchers#hasItems(Matcher...)} instead. 048 */ 049 @Deprecated 050 public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMatchers) { 051 return CoreMatchers.hasItems(elementMatchers); 052 } 053 054 /** 055 * @return A matcher matching any collection in which every element matches elementMatcher 056 * @deprecated Please use {@link CoreMatchers#everyItem(Matcher)} instead. 057 */ 058 @Deprecated 059 public static <T> Matcher<Iterable<T>> everyItem(final Matcher<T> elementMatcher) { 060 return CoreMatchers.everyItem((Matcher) elementMatcher); 061 } 062 063 /** 064 * @return a matcher matching any string that contains substring 065 * @deprecated Please use {@link CoreMatchers#containsString(String)} instead. 066 */ 067 @Deprecated 068 public static Matcher<java.lang.String> containsString(java.lang.String substring) { 069 return CoreMatchers.containsString(substring); 070 } 071 072 /** 073 * This is useful for fluently combining matchers that must both pass. For example: 074 * <pre> 075 * assertThat(string, both(containsString("a")).and(containsString("b"))); 076 * </pre> 077 * 078 * @deprecated Please use {@link CoreMatchers#both(Matcher)} instead. 079 */ 080 @Deprecated 081 public static <T> CombinableBothMatcher<T> both(Matcher<? super T> matcher) { 082 return CoreMatchers.both(matcher); 083 } 084 085 /** 086 * This is useful for fluently combining matchers where either may pass, for example: 087 * <pre> 088 * assertThat(string, either(containsString("a")).or(containsString("b"))); 089 * </pre> 090 * 091 * @deprecated Please use {@link CoreMatchers#either(Matcher)} instead. 092 */ 093 @Deprecated 094 public static <T> CombinableEitherMatcher<T> either(Matcher<? super T> matcher) { 095 return CoreMatchers.either(matcher); 096 } 097 098 /** 099 * @return A matcher that delegates to throwableMatcher and in addition 100 * appends the stacktrace of the actual Throwable in case of a mismatch. 101 */ 102 public static <T extends Throwable> Matcher<T> isThrowable(Matcher<T> throwableMatcher) { 103 return StacktracePrintingMatcher.isThrowable(throwableMatcher); 104 } 105 106 /** 107 * @return A matcher that delegates to exceptionMatcher and in addition 108 * appends the stacktrace of the actual Exception in case of a mismatch. 109 */ 110 public static <T extends Exception> Matcher<T> isException(Matcher<T> exceptionMatcher) { 111 return StacktracePrintingMatcher.isException(exceptionMatcher); 112 } 113}