|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractMatcher.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 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 | 375 | protected boolean argumentMatches(Object expected, Object actual) { |
| 26 | 375 | 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 | 193 | protected String argumentToString(Object argument) { |
| 38 | 193 | if (argument instanceof String) { |
| 39 | 46 | return "\"" + argument + "\""; |
| 40 | } | |
| 41 | 147 | 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 | 398 | public boolean matches(Object[] expected, Object[] actual) { |
| 59 | 398 | if (expected == actual) { |
| 60 | 34 | return true; |
| 61 | } | |
| 62 | 364 | if (expected == null || actual == null) { |
| 63 | 2 | return false; |
| 64 | } | |
| 65 | 362 | if (expected.length != actual.length) { |
| 66 | 11 | return false; |
| 67 | } | |
| 68 | 351 | for (int i = 0; i < expected.length; i++) { |
| 69 | 404 | Object expectedObject = expected[i]; |
| 70 | 404 | Object actualObject = actual[i]; |
| 71 | ||
| 72 | 404 | if (expectedObject == null && actualObject == null) { |
| 73 | 4 | continue; |
| 74 | } | |
| 75 | ||
| 76 | 400 | if (expectedObject == null && actualObject != null) { |
| 77 | 2 | return false; |
| 78 | } | |
| 79 | ||
| 80 | 398 | if (expectedObject != null && actualObject == null) { |
| 81 | 2 | return false; |
| 82 | } | |
| 83 | ||
| 84 | 396 | if (!argumentMatches(expectedObject, actualObject)) { |
| 85 | 119 | return false; |
| 86 | } | |
| 87 | } | |
| 88 | 228 | 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 | 165 | public String toString(Object[] arguments) { |
| 102 | 165 | if (arguments == null) |
| 103 | 21 | arguments = new Object[0]; |
| 104 | ||
| 105 | 165 | StringBuffer result = new StringBuffer(); |
| 106 | ||
| 107 | 165 | for (int i = 0; i < arguments.length; i++) { |
| 108 | 184 | if (i > 0) |
| 109 | 40 | result.append(", "); |
| 110 | 184 | result.append(argumentToString(arguments[i])); |
| 111 | } | |
| 112 | 161 | return result.toString(); |
| 113 | } | |
| 114 | } |
|
||||||||||