001: package org.uispec4j.utils;
002:
003: import junit.framework.Assert;
004:
005: import java.util.*;
006:
007: public class ArrayUtils {
008:
009: public static String toString(Object[] objects) {
010: StringBuffer buffer = new StringBuffer();
011: appendLine(buffer, objects, ",");
012: return buffer.toString();
013: }
014:
015: public static String toVerticalString(Object[] array) {
016: StringBuffer buffer = new StringBuffer();
017: for (int i = 0; i < array.length; i++) {
018: buffer.append(array[i]);
019: if (i < array.length - 1) {
020: buffer.append(Utils.LINE_SEPARATOR);
021: }
022: }
023: return buffer.toString();
024: }
025:
026: private static Integer[] toArray(int[] actual) {
027: Integer[] result = new Integer[actual.length];
028: for (int i = 0; i < actual.length; i++) {
029: result[i] = new Integer(actual[i]);
030: }
031: return result;
032: }
033:
034: public static String toString(Object[][] objects) {
035: StringBuffer buffer = new StringBuffer();
036: buffer.append('[');
037: for (int i = 0; i < objects.length; i++) {
038: if (i > 0) {
039: buffer.append('\n');
040: buffer.append(' ');
041: }
042: appendLine(buffer, objects[i], ",\t");
043: }
044: buffer.append(']');
045: return buffer.toString();
046: }
047:
048: private static void appendLine(StringBuffer buffer,
049: Object[] objects, String separator) {
050: buffer.append('[');
051: for (int i = 0; i < objects.length; i++) {
052: if (objects[i] == null) {
053: buffer.append("null");
054: } else if (objects[i].getClass().isArray()) {
055: buffer.append(toString((Object[]) objects[i]));
056: } else {
057: buffer.append(objects[i]);
058: }
059: if (i < (objects.length - 1)) {
060: buffer.append(separator);
061: }
062: }
063: buffer.append(']');
064: }
065:
066: public static String toString(int[] ints) {
067: StringBuffer buffer = new StringBuffer();
068: buffer.append('[');
069: for (int i = 0; i < ints.length; i++) {
070: buffer.append(ints[i]);
071: if (i < (ints.length - 1)) {
072: buffer.append(',');
073: }
074: }
075: buffer.append(']');
076: return buffer.toString();
077: }
078:
079: public static String toString(List list) {
080: StringBuffer buffer = new StringBuffer();
081: buffer.append('[');
082: for (Iterator iterator = list.iterator(); iterator.hasNext();) {
083: buffer.append(iterator.next());
084: if (iterator.hasNext()) {
085: buffer.append(',');
086: }
087: }
088: buffer.append(']');
089: return buffer.toString();
090: }
091:
092: public static Boolean[][] toBooleanObjects(boolean[][] source) {
093: Boolean[][] result = new Boolean[source.length][];
094: for (int i = 0; i < result.length; i++) {
095: result[i] = new Boolean[source[i].length];
096: for (int j = 0; j < result[i].length; j++) {
097: result[i][j] = Boolean.valueOf(source[i][j]);
098: }
099: }
100: return result;
101: }
102:
103: public static void assertEquals(String message, Object[] expected,
104: Object[] actual) {
105: if (!Arrays.equals(expected, actual)) {
106: fail(message, expected, actual);
107: }
108: }
109:
110: private static void fail(String message, Object[] expected,
111: Object[] actual) {
112: boolean verticalDisplay = expected.length > 5
113: || actual.length > 5;
114: if (verticalDisplay) {
115: Assert.assertEquals(message, toVerticalString(expected),
116: toVerticalString(actual));
117: } else {
118: String prefix = message != null ? message + "\n" : "";
119: Assert.fail(prefix + "Expected: " + toString(expected)
120: + "\nActual: " + toString(actual));
121: }
122: }
123:
124: public static void assertEquals(Object[] expected, Object[] actual) {
125: if (actual == null) {
126: Assert.assertNull("Actual array is not null", expected);
127: }
128: if (!Arrays.equals(expected, actual)) {
129: fail(null, expected, actual);
130: }
131: }
132:
133: public static void assertEquals(Object[][] expected,
134: Object[][] actual) {
135: Assert.assertEquals(expected.length, actual.length);
136: for (int i = 0; i < expected.length; i++) {
137: assertEquals("Error at row " + i + ":", expected[i],
138: actual[i]);
139: }
140: }
141:
142: public static void assertEquals(int[] expected, int[] actual) {
143: if (!Arrays.equals(expected, actual)) {
144: fail(null, toArray(expected), toArray(actual));
145: }
146: }
147:
148: public static void assertEquals(Object[] expectedArray, List list) {
149: assertEquals(expectedArray, list.iterator());
150: }
151:
152: public static void assertEquals(Object[] expectedArray,
153: Iterator actualIterator) {
154: int index = 0;
155: List actualList = new ArrayList();
156: while (actualIterator.hasNext()) {
157: if (index >= expectedArray.length) {
158: for (Iterator iterator = actualIterator; iterator
159: .hasNext();) {
160: actualList.add(iterator.next());
161: }
162: Assert
163: .fail("The iterator contains too many elements: expected: "
164: + toString(expectedArray)
165: + " but was: " + actualList);
166: }
167: Object obj = actualIterator.next();
168: actualList.add(obj);
169: if (!obj.equals(expectedArray[index])) {
170: Assert.fail("Mismatch at index " + index
171: + ". expected: " + expectedArray[index]
172: + " but was: " + obj);
173: }
174: index++;
175: }
176: if (index < expectedArray.length) {
177: fail("Several elements are missing from the iterator",
178: expectedArray, actualList.toArray());
179: }
180: }
181:
182: public static void orderedCompare(Object[][] expectedData,
183: Object[][] actualData) {
184: compareCollection(actualData, expectedData, ArrayList.class);
185: }
186:
187: private static void compareCollection(Object[][] actualData,
188: Object[][] expectedData, Class collectionClass) {
189: Collection actual;
190: Collection expected;
191: try {
192: actual = (Collection) collectionClass.newInstance();
193: expected = (Collection) collectionClass.newInstance();
194: } catch (InstantiationException e) {
195: throw new RuntimeException(e);
196: } catch (IllegalAccessException e) {
197: throw new RuntimeException(e);
198: }
199: for (int i = 0; i < actualData.length; i++) {
200: actual.add(toString(actualData[i]));
201: }
202: for (int i = 0; i < expectedData.length; i++) {
203: expected.add(toString(expectedData[i]));
204: }
205: Assert.assertEquals(expected, actual);
206: }
207:
208: public static void assertEmpty(Object[] array) {
209: if ((array != null) && (array.length > 0)) {
210: Assert.fail("Array should be empty but is "
211: + toString(array));
212: }
213: }
214:
215: public static void assertEmpty(List list) {
216: if ((list != null) && (!list.isEmpty())) {
217: Assert
218: .fail("List should be empty but is "
219: + toString(list));
220: }
221: }
222: }
|