1 5 package org.easymock.internal; 6 7 import java.lang.reflect.Array ; 8 import java.util.Arrays ; 9 10 import org.easymock.AbstractMatcher; 11 12 public class ArrayMatcher extends AbstractMatcher { 13 public String argumentToString(Object argument) { 14 if (argument != null && argument.getClass().isArray()) { 15 return arrayToString(createObjectArray(argument)); 16 } else { 17 return super.argumentToString(argument); 18 } 19 } 20 21 private String arrayToString(Object [] array) { 22 StringBuffer result = new StringBuffer (); 23 for (int i = 0; i < array.length; i++) { 24 result.append(super.argumentToString(array[i])); 25 if (i != array.length - 1) 26 result.append(", "); 27 } 28 return "[" + result.toString() + "]"; 29 } 30 31 public boolean argumentMatches(Object expected, Object actual) { 32 if (expected instanceof boolean[]) { 33 return Arrays.equals((boolean[]) expected, (boolean[]) actual); 34 } else if (expected instanceof byte[]) { 35 return Arrays.equals((byte[]) expected, (byte[]) actual); 36 } else if (expected instanceof char[]) { 37 return Arrays.equals((char[]) expected, (char[]) actual); 38 } else if (expected instanceof double[]) { 39 return Arrays.equals((double[]) expected, (double[]) actual); 40 } else if (expected instanceof float[]) { 41 return Arrays.equals((float[]) expected, (float[]) actual); 42 } else if (expected instanceof int[]) { 43 return Arrays.equals((int[]) expected, (int[]) actual); 44 } else if (expected instanceof long[]) { 45 return Arrays.equals((long[]) expected, (long[]) actual); 46 } else if (expected instanceof short[]) { 47 return Arrays.equals((short[]) expected, (short[]) actual); 48 } else if (expected instanceof Object []) { 49 return Arrays.equals((Object []) expected, (Object []) actual); 50 } else { 51 return super.argumentMatches(expected, actual); 52 } 53 } 54 55 public static Object [] createObjectArray(Object array) { 56 if (array instanceof Object []) { 57 return (Object []) array; 58 } 59 Object [] result = new Object [Array.getLength(array)]; 60 for (int i = 0; i < Array.getLength(array); i++) { 61 result[i] = Array.get(array, i); 62 } 63 return result; 64 } 65 } | Popular Tags |