KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > internal > ArrayMatcher


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.internal;
6
7 import java.lang.reflect.Array JavaDoc;
8 import java.util.Arrays JavaDoc;
9
10 import org.easymock.AbstractMatcher;
11
12 public class ArrayMatcher extends AbstractMatcher {
13     public String JavaDoc argumentToString(Object JavaDoc 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 JavaDoc arrayToString(Object JavaDoc[] array) {
22         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc expected, Object JavaDoc 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 JavaDoc[]) {
49             return Arrays.equals((Object JavaDoc[]) expected, (Object JavaDoc[]) actual);
50         } else {
51             return super.argumentMatches(expected, actual);
52         }
53     }
54
55     public static Object JavaDoc[] createObjectArray(Object JavaDoc array) {
56         if (array instanceof Object JavaDoc[]) {
57             return (Object JavaDoc[]) array;
58         }
59         Object JavaDoc[] result = new Object JavaDoc[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