KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > AbstractMatcher


1 /*
2  * Copyright (c) 2001-2005 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

5 package org.easymock;
6
7 /**
8  * A convenience implementation of {@link ArgumentsMatcher}. A subclass that
9  * does not redefine any method will behave like
10  * {@link MockControl#EQUALS_MATCHER}.
11  */

12 public abstract class AbstractMatcher implements ArgumentsMatcher {
13
14     /**
15      * Compares two arguments; used by
16      * {@link AbstractMatcher#matches(Object[], Object[])}. The arguments
17      * provided to this method are always not <code>null</code>.
18      *
19      * @param expected
20      * the expected argument.
21      * @param actual
22      * the actual argument.
23      * @return true if the arguments match, false otherwise.
24      */

25     protected boolean argumentMatches(Object JavaDoc expected, Object JavaDoc actual) {
26         return expected.equals(actual);
27     }
28
29     /**
30      * Converts an argument to a String, used by
31      * {@link AbstractMatcher#toString(Object[])}.
32      *
33      * @param argument
34      * the argument to convert to a String.
35      * @return a <code>String</code> representation of the argument.
36      */

37     protected String JavaDoc argumentToString(Object JavaDoc argument) {
38         if (argument instanceof String JavaDoc) {
39             return "\"" + argument + "\"";
40         }
41         return "" + argument;
42     }
43
44     /**
45      * Matches two arrays of arguments. This convenience implementation uses
46      * <code>argumentMatches(Object, Object)</code> to check whether arguments
47      * pairs match. If all the arguments match, true is returned, otherwise
48      * false. In two cases, <code>argumentMatches(Object, Object)</code> is
49      * not called: If both argument arrays are null, they match; if one and only
50      * one is null, they do not match.
51      *
52      * @param expected
53      * the expected arguments.
54      * @param actual
55      * the actual arguments.
56      * @return true if the arguments match, false otherwise.
57      */

58     public boolean matches(Object JavaDoc[] expected, Object JavaDoc[] actual) {
59         if (expected == actual) {
60             return true;
61         }
62         if (expected == null || actual == null) {
63             return false;
64         }
65         if (expected.length != actual.length) {
66             return false;
67         }
68         for (int i = 0; i < expected.length; i++) {
69             Object JavaDoc expectedObject = expected[i];
70             Object JavaDoc actualObject = actual[i];
71
72             if (expectedObject == null && actualObject == null) {
73                 continue;
74             }
75
76             if (expectedObject == null && actualObject != null) {
77                 return false;
78             }
79
80             if (expectedObject != null && actualObject == null) {
81                 return false;
82             }
83
84             if (!argumentMatches(expectedObject, actualObject)) {
85                 return false;
86             }
87         }
88         return true;
89     }
90
91     /**
92      * Returns a string representation of the matcher. This convenience
93      * implementation calls {@link AbstractMatcher#argumentToString(Object)}for
94      * every argument in the given array and returns the string representations
95      * of the arguments separated by commas.
96      *
97      * @param arguments
98      * the arguments to be used in the string representation.
99      * @return a string representation of the matcher.
100      */

101     public String JavaDoc toString(Object JavaDoc[] arguments) {
102         if (arguments == null)
103             arguments = new Object JavaDoc[0];
104
105         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
106
107         for (int i = 0; i < arguments.length; i++) {
108             if (i > 0)
109                 result.append(", ");
110             result.append(argumentToString(arguments[i]));
111         }
112         return result.toString();
113     }
114 }
115
Popular Tags