1b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpackage org.junit.matchers;
2b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
3b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.hamcrest.Matcher;
4b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.internal.matchers.CombinableMatcher;
5b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.internal.matchers.Each;
6b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.internal.matchers.IsCollectionContaining;
7b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.internal.matchers.StringContains;
8b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
9b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot/**
10b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Convenience import class: these are useful matchers for use with the assertThat method, but they are
11b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * not currently included in the basic CoreMatchers class from hamcrest.
12b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */
13b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpublic class JUnitMatchers {
14b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	/**
15b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @param element
16b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @return A matcher matching any collection containing element
17b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 */
18b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItem(T element) {
19b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return IsCollectionContaining.hasItem(element);
20b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
21b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
22b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	/**
23b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @param elementMatcher
24b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @return A matcher matching any collection containing an element matching elementMatcher
25b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 */
26b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItem(org.hamcrest.Matcher<? extends T> elementMatcher) {
27b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return IsCollectionContaining.hasItem(elementMatcher);
28b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
29b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
30b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	/**
31b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @param elements
32b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @return A matcher matching any collection containing every element in elements
33b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 */
34b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... elements) {
35b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return IsCollectionContaining.hasItems(elements);
36b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
37b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
38b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	/**
39b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @param elementMatchers
40b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @return A matcher matching any collection containing at least one element that matches
41b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 *         each matcher in elementMatcher (this may be one element matching all matchers,
42b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 *         or different elements matching each matcher)
43b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 */
44b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? extends T>... elementMatchers) {
45b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return IsCollectionContaining.hasItems(elementMatchers);
46b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
47b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
48b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	/**
49b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @param elementMatcher
50b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @return A matcher matching any collection in which every element matches elementMatcher
51b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 */
52b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public static <T> Matcher<Iterable<T>> everyItem(final Matcher<T> elementMatcher) {
53b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return Each.each(elementMatcher);
54b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
55b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
56b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	/**
57b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @param substring
58b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * @return a matcher matching any string that contains substring
59b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 */
60b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public static org.hamcrest.Matcher<java.lang.String> containsString(java.lang.String substring) {
61b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return StringContains.containsString(substring);
62b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
63b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
64b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	/**
65b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * This is useful for fluently combining matchers that must both pass.  For example:
66b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * <pre>
67b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 *   assertThat(string, both(containsString("a")).and(containsString("b")));
68b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * </pre>
69b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 */
70b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public static <T> CombinableMatcher<T> both(Matcher<T> matcher) {
71b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return new CombinableMatcher<T>(matcher);
72b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
73b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
74b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	/**
75b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * This is useful for fluently combining matchers where either may pass, for example:
76b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * <pre>
77b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 *   assertThat(string, either(containsString("a")).or(containsString("b")));
78b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 * </pre>
79b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	 */
80b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public static <T> CombinableMatcher<T> either(Matcher<T> matcher) {
81b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return new CombinableMatcher<T>(matcher);
82b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
83b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot}
84