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