aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/matchers/JUnitMatchers.java
blob: 5bb48d70870de20d0d16b42409d5083a271795ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package org.junit.matchers;

import org.hamcrest.CoreMatchers;
import org.hamcrest.Matcher;
import org.hamcrest.core.CombinableMatcher.CombinableBothMatcher;
import org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher;
import org.junit.internal.matchers.StacktracePrintingMatcher;

/**
 * Convenience import class: these are useful matchers for use with the assertThat method, but they are
 * not currently included in the basic CoreMatchers class from hamcrest.
 *
 * @since 4.4
 */
public class JUnitMatchers {
    /**
     * @return A matcher matching any collection containing element
     * @deprecated Please use {@link CoreMatchers#hasItem(Object)} instead.
     */
    @Deprecated
    public static <T> Matcher<Iterable<? super T>> hasItem(T element) {
        return CoreMatchers.hasItem(element);
    }

    /**
     * @return A matcher matching any collection containing an element matching elementMatcher
     * @deprecated Please use {@link CoreMatchers#hasItem(Matcher)} instead.
     */
    @Deprecated
    public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> elementMatcher) {
        return CoreMatchers.<T>hasItem(elementMatcher);
    }

    /**
     * @return A matcher matching any collection containing every element in elements
     * @deprecated Please use {@link CoreMatchers#hasItems(Object...)} instead.
     */
    @Deprecated
    public static <T> Matcher<Iterable<T>> hasItems(T... elements) {
        return CoreMatchers.hasItems(elements);
    }

    /**
     * @return A matcher matching any collection containing at least one element that matches
     *         each matcher in elementMatcher (this may be one element matching all matchers,
     *         or different elements matching each matcher)
     * @deprecated Please use {@link CoreMatchers#hasItems(Matcher...)} instead.
     */
    @Deprecated
    public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMatchers) {
        return CoreMatchers.<T>hasItems(elementMatchers);
    }

    /**
     * @return A matcher matching any collection in which every element matches elementMatcher
     * @deprecated Please use {@link CoreMatchers#everyItem(Matcher)} instead.
     */
    @Deprecated
    public static <T> Matcher<Iterable<T>> everyItem(final Matcher<T> elementMatcher) {
        return CoreMatchers.everyItem((Matcher) elementMatcher);
    }

    /**
     * @return a matcher matching any string that contains substring
     * @deprecated Please use {@link CoreMatchers#containsString(String)} instead.
     */
    @Deprecated
    public static Matcher<java.lang.String> containsString(java.lang.String substring) {
        return CoreMatchers.containsString(substring);
    }

    /**
     * This is useful for fluently combining matchers that must both pass.  For example:
     * <pre>
     *   assertThat(string, both(containsString("a")).and(containsString("b")));
     * </pre>
     *
     * @deprecated Please use {@link CoreMatchers#both(Matcher)} instead.
     */
    @Deprecated
    public static <T> CombinableBothMatcher<T> both(Matcher<? super T> matcher) {
        return CoreMatchers.both(matcher);
    }

    /**
     * This is useful for fluently combining matchers where either may pass, for example:
     * <pre>
     *   assertThat(string, either(containsString("a")).or(containsString("b")));
     * </pre>
     *
     * @deprecated Please use {@link CoreMatchers#either(Matcher)} instead.
     */
    @Deprecated
    public static <T> CombinableEitherMatcher<T> either(Matcher<? super T> matcher) {
        return CoreMatchers.either(matcher);
    }

    /**
     * @return A matcher that delegates to throwableMatcher and in addition
     *         appends the stacktrace of the actual Throwable in case of a mismatch.
     */
    public static <T extends Throwable> Matcher<T> isThrowable(Matcher<T> throwableMatcher) {
        return StacktracePrintingMatcher.isThrowable(throwableMatcher);
    }

    /**
     * @return A matcher that delegates to exceptionMatcher and in addition
     *         appends the stacktrace of the actual Exception in case of a mismatch.
     */
    public static <T extends Exception> Matcher<T> isException(Matcher<T> exceptionMatcher) {
        return StacktracePrintingMatcher.isException(exceptionMatcher);
    }
}