AbstractMatcher.java revision 47d431f63a66505a645f282416659a9758a91f1c
1/*
2 * Copyright 2001-2009 OFFIS, Tammo Freese
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.easymock;
17
18import java.io.Serializable;
19
20import org.easymock.internal.ArgumentToString;
21
22/**
23 * A convenience implementation of {@link ArgumentsMatcher}. A subclass that
24 * does not redefine any method will behave like
25 * {@link MockControl#EQUALS_MATCHER}.
26 *
27 * @deprecated Since EasyMock 2.0, <code>ArgumentsMatcher</code>s are only supported
28 * for the legacy <code>MockControl</code>. For mock objects generated by the methods
29 * on <code>EasyMock</code>, there are per-argument matchers available. For more
30 * information, see the EasyMock documentation.
31 */
32@Deprecated
33public abstract class AbstractMatcher implements ArgumentsMatcher, Serializable {
34
35    private static final long serialVersionUID = -5463061331694985383L;
36
37    /**
38     * Checks whether an expected argument matches an actual argument; the method
39     * is used by
40     * {@link AbstractMatcher#matches(Object[], Object[])}. The arguments
41     * provided to this method are always not <code>null</code>.
42     *
43     * @param expected
44     *            the expected argument.
45     * @param actual
46     *            the actual argument.
47     * @return true if the arguments match, false otherwise.
48     */
49    protected boolean argumentMatches(Object expected, Object actual) {
50        return expected.equals(actual);
51    }
52
53    /**
54     * Converts an argument to a String, used by
55     * {@link AbstractMatcher#toString(Object[])}.
56     *
57     * @param argument
58     *            the argument to convert to a String.
59     * @return a <code>String</code> representation of the argument.
60     */
61    protected String argumentToString(Object argument) {
62        StringBuffer result = new StringBuffer();
63        ArgumentToString.appendArgument(argument, result);
64        return result.toString();
65    }
66
67    /**
68     * Checks whether an expected argument array matches an actual argument array.
69     * This convenience implementation uses
70     * <code>argumentMatches(Object, Object)</code> to check whether arguments
71     * pairs match. If all the arguments match, true is returned, otherwise
72     * false. In two cases, <code>argumentMatches(Object, Object)</code> is
73     * not called: If both argument arrays are null, they match; if one and only
74     * one is null, they do not match.
75     *
76     * @param expected
77     *            the expected arguments.
78     * @param actual
79     *            the actual arguments.
80     * @return true if the arguments match, false otherwise.
81     */
82    public boolean matches(Object[] expected, Object[] actual) {
83        if (expected == actual) {
84            return true;
85        }
86        if (expected == null || actual == null) {
87            return false;
88        }
89        if (expected.length != actual.length) {
90            return false;
91        }
92        for (int i = 0; i < expected.length; i++) {
93            Object expectedObject = expected[i];
94            Object actualObject = actual[i];
95
96            if (expectedObject == null && actualObject == null) {
97                continue;
98            }
99
100            if (expectedObject == null && actualObject != null) {
101                return false;
102            }
103
104            if (expectedObject != null && actualObject == null) {
105                return false;
106            }
107
108            if (!argumentMatches(expectedObject, actualObject)) {
109                return false;
110            }
111        }
112        return true;
113    }
114
115    /**
116     * Returns a string representation of the matcher. This convenience
117     * implementation calls {@link AbstractMatcher#argumentToString(Object)}
118     * for every argument in the given array and returns the string representations
119     * of the arguments separated by commas.
120     *
121     * @param arguments
122     *            the arguments to be used in the string representation.
123     * @return a string representation of the matcher.
124     */
125    public String toString(Object[] arguments) {
126        if (arguments == null)
127            arguments = new Object[0];
128
129        StringBuilder result = new StringBuilder();
130
131        for (int i = 0; i < arguments.length; i++) {
132            if (i > 0) {
133                result.append(", ");
134            }
135            result.append(argumentToString(arguments[i]));
136        }
137        return result.toString();
138    }
139}
140