|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AbstractMatcher.java | 100% | 100% | 100% | 100% |
|
1 | /* | |
2 | * Copyright (c) 2001-2006 OFFIS, Tammo Freese. | |
3 | * This program is made available under the terms of 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 | * @deprecated Since EasyMock 2.0, <code>ArgumentsMatcher</code>s are only supported | |
13 | * for the legacy <code>MockControl</code>. For mock objects generated by the methods | |
14 | * on <code>EasyMock</code>, there are per-argument matchers available. For more | |
15 | * information, see the EasyMock documentation. | |
16 | */ | |
17 | public abstract class AbstractMatcher implements ArgumentsMatcher { | |
18 | ||
19 | /** | |
20 | * Checks whether an expected argument matches an actual argument; the method | |
21 | * is used by | |
22 | * {@link AbstractMatcher#matches(Object[], Object[])}. The arguments | |
23 | * provided to this method are always not <code>null</code>. | |
24 | * | |
25 | * @param expected | |
26 | * the expected argument. | |
27 | * @param actual | |
28 | * the actual argument. | |
29 | * @return true if the arguments match, false otherwise. | |
30 | */ | |
31 | 1 | protected boolean argumentMatches(Object expected, Object actual) { |
32 | 1 | return expected.equals(actual); |
33 | } | |
34 | ||
35 | /** | |
36 | * Converts an argument to a String, used by | |
37 | * {@link AbstractMatcher#toString(Object[])}. | |
38 | * | |
39 | * @param argument | |
40 | * the argument to convert to a String. | |
41 | * @return a <code>String</code> representation of the argument. | |
42 | */ | |
43 | 50 | protected String argumentToString(Object argument) { |
44 | 50 | if (argument instanceof String) { |
45 | 26 | return "\"" + argument + "\""; |
46 | } | |
47 | 24 | return "" + argument; |
48 | } | |
49 | ||
50 | /** | |
51 | * Checks whether an expected argument array matches an actual argument array. | |
52 | * This convenience implementation uses | |
53 | * <code>argumentMatches(Object, Object)</code> to check whether arguments | |
54 | * pairs match. If all the arguments match, true is returned, otherwise | |
55 | * false. In two cases, <code>argumentMatches(Object, Object)</code> is | |
56 | * not called: If both argument arrays are null, they match; if one and only | |
57 | * one is null, they do not match. | |
58 | * | |
59 | * @param expected | |
60 | * the expected arguments. | |
61 | * @param actual | |
62 | * the actual arguments. | |
63 | * @return true if the arguments match, false otherwise. | |
64 | */ | |
65 | 38 | public boolean matches(Object[] expected, Object[] actual) { |
66 | 38 | if (expected == actual) { |
67 | 1 | return true; |
68 | } | |
69 | 37 | if (expected == null || actual == null) { |
70 | 2 | return false; |
71 | } | |
72 | 35 | if (expected.length != actual.length) { |
73 | 1 | return false; |
74 | } | |
75 | 34 | for (int i = 0; i < expected.length; i++) { |
76 | 33 | Object expectedObject = expected[i]; |
77 | 33 | Object actualObject = actual[i]; |
78 | ||
79 | 33 | if (expectedObject == null && actualObject == null) { |
80 | 1 | continue; |
81 | } | |
82 | ||
83 | 32 | if (expectedObject == null && actualObject != null) { |
84 | 2 | return false; |
85 | } | |
86 | ||
87 | 30 | if (expectedObject != null && actualObject == null) { |
88 | 2 | return false; |
89 | } | |
90 | ||
91 | 28 | if (!argumentMatches(expectedObject, actualObject)) { |
92 | 14 | return false; |
93 | } | |
94 | } | |
95 | 16 | return true; |
96 | } | |
97 | ||
98 | /** | |
99 | * Returns a string representation of the matcher. This convenience | |
100 | * implementation calls {@link AbstractMatcher#argumentToString(Object)} | |
101 | * for every argument in the given array and returns the string representations | |
102 | * of the arguments separated by commas. | |
103 | * | |
104 | * @param arguments | |
105 | * the arguments to be used in the string representation. | |
106 | * @return a string representation of the matcher. | |
107 | */ | |
108 | 59 | public String toString(Object[] arguments) { |
109 | 59 | if (arguments == null) |
110 | 1 | arguments = new Object[0]; |
111 | ||
112 | 59 | StringBuffer result = new StringBuffer(); |
113 | ||
114 | 59 | for (int i = 0; i < arguments.length; i++) { |
115 | 67 | if (i > 0) |
116 | 16 | result.append(", "); |
117 | 67 | result.append(argumentToString(arguments[i])); |
118 | } | |
119 | 58 | return result.toString(); |
120 | } | |
121 | } |
|