001: package com.mockrunner.test.util;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.List;
006:
007: import junit.framework.TestCase;
008:
009: import com.mockrunner.util.common.ArrayUtil;
010:
011: public class ArrayUtilTest extends TestCase {
012: public void testGetListFromByteArray() {
013: byte[] testArray = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
014: List list = ArrayUtil.getListFromByteArray(testArray);
015: assertTrue(list.size() == 9);
016: for (int ii = 0; ii < testArray.length; ii++) {
017: byte nextByteFromList = ((Byte) list.get(ii)).byteValue();
018: assertEquals(testArray[ii], nextByteFromList);
019: }
020: }
021:
022: public void testGetByteArrayFromList() {
023: ArrayList list = new ArrayList();
024: for (int ii = 0; ii < 9; ii++) {
025: list.add(new Byte((byte) ii));
026: }
027: list.add(null);
028: list.add(new Byte((byte) 0));
029: byte[] data = ArrayUtil.getByteArrayFromList(list);
030: assertTrue(data.length == 11);
031: for (int ii = 0; ii < 9; ii++) {
032: assertEquals(ii, data[ii]);
033: }
034: assertEquals(0, data[9]);
035: assertEquals(0, data[10]);
036: data = ArrayUtil.getByteArrayFromList(list, 5);
037: assertTrue(data.length == 6);
038: assertEquals(5, data[0]);
039: assertEquals(6, data[1]);
040: assertEquals(7, data[2]);
041: assertEquals(8, data[3]);
042: assertEquals(0, data[4]);
043: assertEquals(0, data[5]);
044: data = ArrayUtil.getByteArrayFromList(list, 5, 4);
045: assertTrue(data.length == 4);
046: assertEquals(5, data[0]);
047: assertEquals(6, data[1]);
048: assertEquals(7, data[2]);
049: assertEquals(8, data[3]);
050: data = ArrayUtil.getByteArrayFromList(list, 0, 0);
051: assertTrue(data.length == 0);
052: data = ArrayUtil.getByteArrayFromList(list, 0, 1);
053: assertTrue(data.length == 1);
054: assertEquals(0, data[0]);
055: data = ArrayUtil.getByteArrayFromList(new ArrayList(), 0, 0);
056: assertNotNull(data);
057: assertTrue(data.length == 0);
058: }
059:
060: public void testAddBytesToArrayList() {
061: ArrayList list = new ArrayList();
062: byte[] testArray = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
063: ArrayUtil.addBytesToList(testArray, list, 0);
064: assertTrue(list.size() == 9);
065: for (int ii = 0; ii < testArray.length; ii++) {
066: byte nextByteFromList = ((Byte) list.get(ii)).byteValue();
067: assertEquals(testArray[ii], nextByteFromList);
068: }
069: ArrayUtil.addBytesToList(testArray, 1, 3, list, 9);
070: assertTrue(list.size() == 12);
071: for (int ii = 0; ii < testArray.length; ii++) {
072: byte nextByteFromList = ((Byte) list.get(ii)).byteValue();
073: assertEquals(testArray[ii], nextByteFromList);
074: }
075: assertEquals(1, ((Byte) list.get(9)).byteValue());
076: assertEquals(2, ((Byte) list.get(10)).byteValue());
077: assertEquals(3, ((Byte) list.get(11)).byteValue());
078: ArrayUtil.addBytesToList(testArray, 1, 3, list, 0);
079: assertTrue(list.size() == 12);
080: assertEquals(1, ((Byte) list.get(0)).byteValue());
081: assertEquals(2, ((Byte) list.get(1)).byteValue());
082: assertEquals(3, ((Byte) list.get(2)).byteValue());
083: assertEquals(3, ((Byte) list.get(3)).byteValue());
084: ArrayUtil.addBytesToList(testArray, list, 100);
085: assertTrue(list.size() == 109);
086: for (int ii = 12; ii < 100; ii++) {
087: assertNull(list.get(ii));
088: }
089: for (int ii = 100; ii < 109; ii++) {
090: byte nextByteFromList = ((Byte) list.get(ii)).byteValue();
091: assertEquals(testArray[ii - 100], nextByteFromList);
092: }
093: }
094:
095: public void testTruncateArray() {
096: String[] stringArray = new String[] { "This", "is", "an",
097: "array", "of", "strings" };
098: stringArray = (String[]) ArrayUtil
099: .truncateArray(stringArray, 3);
100: assertTrue(stringArray.length == 3);
101: assertEquals("This", stringArray[0]);
102: assertEquals("is", stringArray[1]);
103: assertEquals("an", stringArray[2]);
104: boolean[] booleanArray = new boolean[] { true, true, false,
105: true, false, false };
106: booleanArray = (boolean[]) ArrayUtil.truncateArray(
107: booleanArray, 2, 3);
108: assertTrue(booleanArray.length == 3);
109: assertEquals(false, booleanArray[0]);
110: assertEquals(true, booleanArray[1]);
111: assertEquals(false, booleanArray[2]);
112: }
113:
114: public void testIndexOf() {
115: byte[] testArray = new byte[] { 1, 1, 5, 3, 4, 5, 7, 7, 8, 3, 2 };
116: assertEquals(-1, ArrayUtil.indexOf(testArray, new byte[] { 0 }));
117: assertEquals(-1, ArrayUtil.indexOf(testArray,
118: new byte[] { 0, 2 }));
119: assertEquals(0, ArrayUtil.indexOf(testArray, new byte[] { 1 }));
120: assertEquals(1, ArrayUtil.indexOf(testArray,
121: new byte[] { 1, 5 }));
122: assertEquals(6, ArrayUtil.indexOf(testArray, new byte[] { 7, 7,
123: 8 }));
124: assertEquals(0, ArrayUtil.indexOf(testArray, new byte[] { 1, 1,
125: 5, 3, 4, 5, 7, 7, 8, 3 }));
126: assertEquals(0, ArrayUtil.indexOf(testArray, testArray));
127: assertEquals(-1, ArrayUtil.indexOf(testArray, new byte[] { 1,
128: 1, 5, 3, 4, 5, 7, 7, 8, 3, 0 }));
129: assertEquals(10, ArrayUtil.indexOf(testArray, new byte[] { 2 }));
130: assertEquals(2, ArrayUtil.indexOf(testArray,
131: new byte[] { 5, 3 }));
132: assertEquals(0, ArrayUtil.indexOf(testArray, new byte[] {}));
133:
134: assertEquals(2, ArrayUtil.indexOf(testArray,
135: new byte[] { 5, 3 }), 1);
136: assertEquals(-1, ArrayUtil.indexOf(testArray,
137: new byte[] { 5, 3 }), 5);
138: assertEquals(-1, ArrayUtil.indexOf(testArray, testArray), 1);
139: assertEquals(1, ArrayUtil.indexOf(testArray, new byte[] { 1 }),
140: 1);
141: assertEquals(6, ArrayUtil.indexOf(testArray, new byte[] { 7, 7,
142: 8 }), 6);
143: }
144:
145: public void testConvertToObjectArray() {
146: int[] intArray = new int[] { 1, 2, 3 };
147: Object[] integerWrappedArray = ArrayUtil
148: .convertToObjectArray(intArray);
149: assertTrue(integerWrappedArray instanceof Integer[]);
150: assertEquals(new Integer(1), integerWrappedArray[0]);
151: assertEquals(new Integer(2), integerWrappedArray[1]);
152: assertEquals(new Integer(3), integerWrappedArray[2]);
153: double[] doubleArray = new double[] { 1.3 };
154: Object[] doubleWrappedArray = ArrayUtil
155: .convertToObjectArray(doubleArray);
156: assertTrue(doubleWrappedArray instanceof Double[]);
157: assertEquals(new Double(1.3), doubleWrappedArray[0]);
158: String[] stringArray = new String[] { "This", "is", "an",
159: "array", "of", "strings" };
160: Object[] stringTargetArray = ArrayUtil
161: .convertToObjectArray(stringArray);
162: assertTrue(stringArray == stringTargetArray);
163: boolean[] booleanArray = new boolean[] { true, true, false };
164: Object[] booleanWrappedArray = ArrayUtil
165: .convertToObjectArray(booleanArray);
166: assertTrue(booleanWrappedArray instanceof Boolean[]);
167: assertEquals(Boolean.TRUE, booleanWrappedArray[0]);
168: assertEquals(Boolean.TRUE, booleanWrappedArray[1]);
169: assertEquals(Boolean.FALSE, booleanWrappedArray[2]);
170: }
171:
172: public void testConvertToPrimitiveArray() {
173: Integer[] integerArray = new Integer[] { new Integer(1),
174: new Integer(2), new Integer(3) };
175: int[] intUnwrappedArray = (int[]) ArrayUtil
176: .convertToPrimitiveArray(integerArray);
177: assertEquals(1, intUnwrappedArray[0]);
178: assertEquals(2, intUnwrappedArray[1]);
179: assertEquals(3, intUnwrappedArray[2]);
180: Float[] floatArray = new Float[] { new Float(1) };
181: float[] floatUnwrappedArray = (float[]) ArrayUtil
182: .convertToPrimitiveArray(floatArray);
183: assertEquals(1.0, floatUnwrappedArray[0], 0);
184: Boolean[] booleanArray = new Boolean[] { Boolean.TRUE,
185: Boolean.FALSE };
186: boolean[] booleanUnwrappedArray = (boolean[]) ArrayUtil
187: .convertToPrimitiveArray(booleanArray);
188: assertTrue(booleanUnwrappedArray[0]);
189: assertFalse(booleanUnwrappedArray[1]);
190: intUnwrappedArray = (int[]) ArrayUtil
191: .convertToPrimitiveArray(new Integer[0]);
192: assertEquals(0, intUnwrappedArray.length);
193: try {
194: ArrayUtil.convertToPrimitiveArray(new String[] { "1" });
195: fail();
196: } catch (IllegalArgumentException exc) {
197: //should throw exception
198: }
199: try {
200: ArrayUtil.convertToPrimitiveArray(new Integer[] {
201: new Integer(1), null });
202: fail();
203: } catch (IllegalArgumentException exc) {
204: //should throw exception
205: }
206: }
207:
208: public void testConvertToArray() {
209: int[] intArray = new int[] { 1, 2, 3 };
210: Object array = ArrayUtil.convertToArray(intArray);
211: assertSame(intArray, array);
212: String[] stringArray = new String[] { "This", "is", "an",
213: "array", "of", "strings" };
214: array = ArrayUtil.convertToArray(stringArray);
215: assertTrue(stringArray == array);
216: array = ArrayUtil.convertToArray("Test");
217: assertTrue(array instanceof String[]);
218: assertTrue(((String[]) array).length == 1);
219: assertEquals("Test", ((String[]) array)[0]);
220: }
221:
222: public void testCopyArray() {
223: byte[] byteArray = new byte[] { 1, 2, 3, 4, 5 };
224: byte[] copyByteArray = (byte[]) ArrayUtil.copyArray(byteArray);
225: assertNotSame(byteArray, copyByteArray);
226: assertTrue(Arrays.equals(byteArray, copyByteArray));
227: String testWrong = (String) ArrayUtil.copyArray("testWrong");
228: assertEquals(testWrong, "testWrong");
229: String[] stringArray = new String[] { "This", "is", "an",
230: "array", "of", "strings" };
231: String[] copyStringArray = (String[]) ArrayUtil
232: .copyArray(stringArray);
233: assertNotSame(stringArray, copyStringArray);
234: assertTrue(Arrays.equals(stringArray, copyStringArray));
235: Object myObject = new Object();
236: Object[] myArray = new Object[] { myObject };
237: Object[] myCopy = (Object[]) ArrayUtil.copyArray(myArray);
238: assertNotSame(myArray, myCopy);
239: assertSame(myArray[0], myCopy[0]);
240: String[] firstStringArray = new String[] { "1", "2", "3" };
241: String[] secondStringArray = new String[] { "4", "5", "6" };
242: String[][] dim2StringArray = new String[][] { firstStringArray,
243: secondStringArray };
244: String[][] copy2Dim = (String[][]) ArrayUtil
245: .copyArray(dim2StringArray);
246: assertNotSame(dim2StringArray, copy2Dim);
247: assertSame(firstStringArray, copy2Dim[0]);
248: assertSame(secondStringArray, copy2Dim[1]);
249: }
250:
251: public void testAreArraysEqual() {
252: assertTrue(ArrayUtil.areArraysEqual(null, null));
253: assertTrue(ArrayUtil.areArraysEqual("1", "1"));
254: assertTrue(ArrayUtil.areArraysEqual(new Long(1), new Long(1)));
255: assertFalse(ArrayUtil.areArraysEqual(null, "1"));
256: assertFalse(ArrayUtil.areArraysEqual("1", null));
257: assertFalse(ArrayUtil.areArraysEqual("1", "2"));
258: assertFalse(ArrayUtil.areArraysEqual("1", new String[] { "1" }));
259: assertFalse(ArrayUtil.areArraysEqual(new String[] { "1", "2" },
260: new Integer(2)));
261: assertFalse(ArrayUtil.areArraysEqual(new String[] {},
262: new String[] { "1" }));
263: assertTrue(ArrayUtil.areArraysEqual(new String[] { "1" },
264: new String[] { "1" }));
265: assertTrue(ArrayUtil.areArraysEqual(new int[] { 1, 2, 3 },
266: new int[] { 1, 2, 3 }));
267: assertFalse(ArrayUtil.areArraysEqual(new int[] { 1, 2 },
268: new int[] { 1, 2, 3 }));
269: assertFalse(ArrayUtil.areArraysEqual(
270: new Object[] { new Object() },
271: new Object[] { new Object() }));
272: assertFalse(ArrayUtil.areArraysEqual(
273: new Object[] { new Object() },
274: new Object[] { new Object() }));
275: assertTrue(ArrayUtil.areArraysEqual(new Object[] { "1" },
276: new String[] { "1" }));
277: assertFalse(ArrayUtil.areArraysEqual(new String[] { "1" },
278: new String[][] { { "1" } }));
279: assertFalse(ArrayUtil.areArraysEqual(
280: new String[][] { { "1" } }, new String[][] { { "1" } }));
281: String[][][] dim3Array = new String[][][] { { { "1" } },
282: { { "2" } }, { { "3" } } };
283: assertTrue(ArrayUtil.areArraysEqual(dim3Array, dim3Array));
284: }
285:
286: public void testComputeHashCode() {
287: assertEquals(0, ArrayUtil.computeHashCode(null));
288: assertEquals("123".hashCode(), ArrayUtil.computeHashCode("123"));
289: assertEquals(ArrayUtil
290: .computeHashCode(new String[] { "1", null }), ArrayUtil
291: .computeHashCode(new String[] { null, "1" }));
292: assertEquals(16369, ArrayUtil
293: .computeHashCode(new byte[] { 1, 1 }));
294: assertFalse(ArrayUtil
295: .computeHashCode(new String[] { "1", "2" }) == ArrayUtil
296: .computeHashCode(new String[] { "1", "2", "3" }));
297: }
298:
299: public void testEnsureUnique() {
300: String[] testArray = new String[] { "test1", "test2" };
301: ArrayUtil.ensureUnique(testArray);
302: assertTrue(Arrays.equals(new String[] { "test1", "test2" },
303: testArray));
304: testArray = new String[] { "test", "test", "xyz" };
305: ArrayUtil.ensureUnique(testArray);
306: assertTrue(Arrays.equals(
307: new String[] { "test1", "test2", "xyz" }, testArray));
308: testArray = new String[] { "x", "y", "z", "z", "x" };
309: ArrayUtil.ensureUnique(testArray);
310: assertTrue(Arrays.equals(new String[] { "x1", "y", "z1", "z2",
311: "x2" }, testArray));
312: }
313: }
|