aboutsummaryrefslogtreecommitdiffstats
path: root/testutil/src/main/java/com/yahoo/test/Matchers.java
blob: f8ec26785a25bf1d2ba6641a5f759798447d32c3 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.test;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

import java.lang.reflect.Method;

/**
 * Some useful matchers.
 *
 * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
public final class Matchers {

    /**
     * A match is found if at least <em>one</em> item in the Iterable has the given method, <em>and</em> the return value
     * when calling it equals the given expected value.
     *
     * @param expected The expected value.
     * @param methodName The name of the method to call. The method must take no parameters.
     * @return A Matcher to match against an Iterable.
     */
    @SuppressWarnings("rawtypes")
    public static org.hamcrest.Matcher<java.lang.Iterable> hasItemWithMethod(final Object expected, final String methodName) {
        return hasItemWithMethod(new MethodResult(methodName, expected));
    }

    /**
     * A match is found if at least <em>one</em> item in the Iterable has <em>all</em> given methods, <em>and</em>
     * the return values when calling them equals the given expected values.
     *
     * @param results The pairs of method names and expected results to compare.
     * @return A Matcher to match against an Iterable.
     */
    @SuppressWarnings("rawtypes")
    public static org.hamcrest.Matcher<java.lang.Iterable> hasItemWithMethod(final MethodResult... results) {
        return new BaseMatcher<Iterable>() {
            @Override
            public boolean matches(Object item) {
                Iterable components = (Iterable) item;
                if (!components.iterator().hasNext()) {
                    //empty collection
                    return false;
                }
                for (Object componentInList : components) {
                    boolean allMethodsMatch = false;
                    for (MethodResult result : results) {
                        Object strToMatch;
                        try {
                            Method method = componentInList.getClass().getMethod(result.getMethodName());
                            strToMatch = method.invoke(componentInList);
                        } catch (Exception e) {
                            allMethodsMatch = false;
                            break;
                        }
                        if (result.getExpectedResult().equals(strToMatch)) {
                            allMethodsMatch = true;
                        } else {
                            allMethodsMatch = false;
                            break;
                        }
                    }
                    if (allMethodsMatch) {
                        return true;
                    }
                }
                return false;
            }

            @Override
            public void describeTo(Description description) {
                StringBuilder b = new StringBuilder();
                for (MethodResult result : results) {
                    b.append(result).append(". ");
                }
                description.appendText(b.toString());
            }
        };
    }

    public static final class MethodResult {
        private final String methodName;
        private final Object expectedResult;

        public MethodResult(String methodName, Object expectedResult) {
            this.methodName = methodName;
            this.expectedResult = expectedResult;
        }

        public Object getExpectedResult() {
            return expectedResult;
        }

        public String getMethodName() {
            return methodName;
        }

        @Override
        public String toString() {
            return "Method: '" + methodName + "\', expected result: '" + expectedResult + '\'';
        }
    }
}