001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.lang;
019:
020: import java.util.Arrays;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025: import junit.textui.TestRunner;
026:
027: /**
028: * Tests ArrayUtils add methods.
029: *
030: * @author Gary D. Gregory
031: * @version $Id: ArrayUtilsAddTest.java 437554 2006-08-28 06:21:41Z bayard $
032: */
033: public class ArrayUtilsAddTest extends TestCase {
034: public static void main(String[] args) {
035: TestRunner.run(suite());
036: }
037:
038: public static Test suite() {
039: TestSuite suite = new TestSuite(ArrayUtilsAddTest.class);
040: suite.setName("ArrayUtils add Tests");
041: return suite;
042: }
043:
044: public void testAddObjectArrayBoolean() {
045: boolean[] newArray;
046: newArray = ArrayUtils.add((boolean[]) null, false);
047: assertTrue(Arrays.equals(new boolean[] { false }, newArray));
048: assertEquals(Boolean.TYPE, newArray.getClass()
049: .getComponentType());
050: newArray = ArrayUtils.add((boolean[]) null, true);
051: assertTrue(Arrays.equals(new boolean[] { true }, newArray));
052: assertEquals(Boolean.TYPE, newArray.getClass()
053: .getComponentType());
054: boolean[] array1 = new boolean[] { true, false, true };
055: newArray = ArrayUtils.add(array1, false);
056: assertTrue(Arrays.equals(new boolean[] { true, false, true,
057: false }, newArray));
058: assertEquals(Boolean.TYPE, newArray.getClass()
059: .getComponentType());
060: }
061:
062: public void testAddObjectArrayByte() {
063: byte[] newArray;
064: newArray = ArrayUtils.add((byte[]) null, (byte) 0);
065: assertTrue(Arrays.equals(new byte[] { 0 }, newArray));
066: assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
067: newArray = ArrayUtils.add((byte[]) null, (byte) 1);
068: assertTrue(Arrays.equals(new byte[] { 1 }, newArray));
069: assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
070: byte[] array1 = new byte[] { 1, 2, 3 };
071: newArray = ArrayUtils.add(array1, (byte) 0);
072: assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 0 }, newArray));
073: assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
074: newArray = ArrayUtils.add(array1, (byte) 4);
075: assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 4 }, newArray));
076: assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
077: }
078:
079: public void testAddObjectArrayChar() {
080: char[] newArray;
081: newArray = ArrayUtils.add((char[]) null, (char) 0);
082: assertTrue(Arrays.equals(new char[] { 0 }, newArray));
083: assertEquals(Character.TYPE, newArray.getClass()
084: .getComponentType());
085: newArray = ArrayUtils.add((char[]) null, (char) 1);
086: assertTrue(Arrays.equals(new char[] { 1 }, newArray));
087: assertEquals(Character.TYPE, newArray.getClass()
088: .getComponentType());
089: char[] array1 = new char[] { 1, 2, 3 };
090: newArray = ArrayUtils.add(array1, (char) 0);
091: assertTrue(Arrays.equals(new char[] { 1, 2, 3, 0 }, newArray));
092: assertEquals(Character.TYPE, newArray.getClass()
093: .getComponentType());
094: newArray = ArrayUtils.add(array1, (char) 4);
095: assertTrue(Arrays.equals(new char[] { 1, 2, 3, 4 }, newArray));
096: assertEquals(Character.TYPE, newArray.getClass()
097: .getComponentType());
098: }
099:
100: public void testAddObjectArrayDouble() {
101: double[] newArray;
102: newArray = ArrayUtils.add((double[]) null, 0);
103: assertTrue(Arrays.equals(new double[] { 0 }, newArray));
104: assertEquals(Double.TYPE, newArray.getClass()
105: .getComponentType());
106: newArray = ArrayUtils.add((double[]) null, 1);
107: assertTrue(Arrays.equals(new double[] { 1 }, newArray));
108: assertEquals(Double.TYPE, newArray.getClass()
109: .getComponentType());
110: double[] array1 = new double[] { 1, 2, 3 };
111: newArray = ArrayUtils.add(array1, 0);
112: assertTrue(Arrays.equals(new double[] { 1, 2, 3, 0 }, newArray));
113: assertEquals(Double.TYPE, newArray.getClass()
114: .getComponentType());
115: newArray = ArrayUtils.add(array1, 4);
116: assertTrue(Arrays.equals(new double[] { 1, 2, 3, 4 }, newArray));
117: assertEquals(Double.TYPE, newArray.getClass()
118: .getComponentType());
119: }
120:
121: public void testAddObjectArrayFloat() {
122: float[] newArray;
123: newArray = ArrayUtils.add((float[]) null, 0);
124: assertTrue(Arrays.equals(new float[] { 0 }, newArray));
125: assertEquals(Float.TYPE, newArray.getClass().getComponentType());
126: newArray = ArrayUtils.add((float[]) null, 1);
127: assertTrue(Arrays.equals(new float[] { 1 }, newArray));
128: assertEquals(Float.TYPE, newArray.getClass().getComponentType());
129: float[] array1 = new float[] { 1, 2, 3 };
130: newArray = ArrayUtils.add(array1, 0);
131: assertTrue(Arrays.equals(new float[] { 1, 2, 3, 0 }, newArray));
132: assertEquals(Float.TYPE, newArray.getClass().getComponentType());
133: newArray = ArrayUtils.add(array1, 4);
134: assertTrue(Arrays.equals(new float[] { 1, 2, 3, 4 }, newArray));
135: assertEquals(Float.TYPE, newArray.getClass().getComponentType());
136: }
137:
138: public void testAddObjectArrayInt() {
139: int[] newArray;
140: newArray = ArrayUtils.add((int[]) null, 0);
141: assertTrue(Arrays.equals(new int[] { 0 }, newArray));
142: assertEquals(Integer.TYPE, newArray.getClass()
143: .getComponentType());
144: newArray = ArrayUtils.add((int[]) null, 1);
145: assertTrue(Arrays.equals(new int[] { 1 }, newArray));
146: assertEquals(Integer.TYPE, newArray.getClass()
147: .getComponentType());
148: int[] array1 = new int[] { 1, 2, 3 };
149: newArray = ArrayUtils.add(array1, 0);
150: assertTrue(Arrays.equals(new int[] { 1, 2, 3, 0 }, newArray));
151: assertEquals(Integer.TYPE, newArray.getClass()
152: .getComponentType());
153: newArray = ArrayUtils.add(array1, 4);
154: assertTrue(Arrays.equals(new int[] { 1, 2, 3, 4 }, newArray));
155: assertEquals(Integer.TYPE, newArray.getClass()
156: .getComponentType());
157: }
158:
159: public void testAddObjectArrayLong() {
160: long[] newArray;
161: newArray = ArrayUtils.add((long[]) null, 0);
162: assertTrue(Arrays.equals(new long[] { 0 }, newArray));
163: assertEquals(Long.TYPE, newArray.getClass().getComponentType());
164: newArray = ArrayUtils.add((long[]) null, 1);
165: assertTrue(Arrays.equals(new long[] { 1 }, newArray));
166: assertEquals(Long.TYPE, newArray.getClass().getComponentType());
167: long[] array1 = new long[] { 1, 2, 3 };
168: newArray = ArrayUtils.add(array1, 0);
169: assertTrue(Arrays.equals(new long[] { 1, 2, 3, 0 }, newArray));
170: assertEquals(Long.TYPE, newArray.getClass().getComponentType());
171: newArray = ArrayUtils.add(array1, 4);
172: assertTrue(Arrays.equals(new long[] { 1, 2, 3, 4 }, newArray));
173: assertEquals(Long.TYPE, newArray.getClass().getComponentType());
174: }
175:
176: public void testAddObjectArrayShort() {
177: short[] newArray;
178: newArray = ArrayUtils.add((short[]) null, (short) 0);
179: assertTrue(Arrays.equals(new short[] { 0 }, newArray));
180: assertEquals(Short.TYPE, newArray.getClass().getComponentType());
181: newArray = ArrayUtils.add((short[]) null, (short) 1);
182: assertTrue(Arrays.equals(new short[] { 1 }, newArray));
183: assertEquals(Short.TYPE, newArray.getClass().getComponentType());
184: short[] array1 = new short[] { 1, 2, 3 };
185: newArray = ArrayUtils.add(array1, (short) 0);
186: assertTrue(Arrays.equals(new short[] { 1, 2, 3, 0 }, newArray));
187: assertEquals(Short.TYPE, newArray.getClass().getComponentType());
188: newArray = ArrayUtils.add(array1, (short) 4);
189: assertTrue(Arrays.equals(new short[] { 1, 2, 3, 4 }, newArray));
190: assertEquals(Short.TYPE, newArray.getClass().getComponentType());
191: }
192:
193: public void testAddObjectArrayObject() {
194: Object[] newArray;
195: newArray = ArrayUtils.add((Object[]) null, null);
196: assertTrue(Arrays.equals((new Object[] { null }), newArray));
197: assertEquals(Object.class, newArray.getClass()
198: .getComponentType());
199:
200: newArray = ArrayUtils.add((Object[]) null, "a");
201: assertTrue(Arrays.equals((new String[] { "a" }), newArray));
202: assertTrue(Arrays.equals((new Object[] { "a" }), newArray));
203: assertEquals(String.class, newArray.getClass()
204: .getComponentType());
205:
206: String[] stringArray1 = new String[] { "a", "b", "c" };
207: newArray = ArrayUtils.add(stringArray1, null);
208: assertTrue(Arrays.equals(
209: (new String[] { "a", "b", "c", null }), newArray));
210: assertEquals(String.class, newArray.getClass()
211: .getComponentType());
212:
213: newArray = ArrayUtils.add(stringArray1, "d");
214: assertTrue(Arrays.equals((new String[] { "a", "b", "c", "d" }),
215: newArray));
216: assertEquals(String.class, newArray.getClass()
217: .getComponentType());
218:
219: Number[] numberArray1 = new Number[] { new Integer(1),
220: new Double(2) };
221: newArray = ArrayUtils.add(numberArray1, new Float(3));
222: assertTrue(Arrays.equals((new Number[] { new Integer(1),
223: new Double(2), new Float(3) }), newArray));
224: assertEquals(Number.class, newArray.getClass()
225: .getComponentType());
226:
227: numberArray1 = null;
228: newArray = ArrayUtils.add(numberArray1, new Float(3));
229: assertTrue(Arrays.equals((new Float[] { new Float(3) }),
230: newArray));
231: assertEquals(Float.class, newArray.getClass()
232: .getComponentType());
233:
234: numberArray1 = null;
235: newArray = ArrayUtils.add(numberArray1, null);
236: assertTrue(Arrays.equals((new Object[] { null }), newArray));
237: assertEquals(Object.class, newArray.getClass()
238: .getComponentType());
239: }
240:
241: public void testAddObjectArrayToObjectArray() {
242: assertNull(ArrayUtils.addAll((Object[]) null, (Object[]) null));
243: Object[] newArray;
244: String[] stringArray1 = new String[] { "a", "b", "c" };
245: String[] stringArray2 = new String[] { "1", "2", "3" };
246: newArray = ArrayUtils.addAll(stringArray1, null);
247: assertNotSame(stringArray1, newArray);
248: assertTrue(Arrays.equals(stringArray1, newArray));
249: assertTrue(Arrays.equals((new String[] { "a", "b", "c" }),
250: newArray));
251: assertEquals(String.class, newArray.getClass()
252: .getComponentType());
253: newArray = ArrayUtils.addAll(null, stringArray2);
254: assertNotSame(stringArray2, newArray);
255: assertTrue(Arrays.equals(stringArray2, newArray));
256: assertTrue(Arrays.equals((new String[] { "1", "2", "3" }),
257: newArray));
258: assertEquals(String.class, newArray.getClass()
259: .getComponentType());
260: newArray = ArrayUtils.addAll(stringArray1, stringArray2);
261: assertTrue(Arrays.equals((new String[] { "a", "b", "c", "1",
262: "2", "3" }), newArray));
263: assertEquals(String.class, newArray.getClass()
264: .getComponentType());
265: newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY,
266: null);
267: assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY,
268: newArray));
269: assertTrue(Arrays.equals((new String[] {}), newArray));
270: assertEquals(String.class, newArray.getClass()
271: .getComponentType());
272: newArray = ArrayUtils.addAll(null,
273: ArrayUtils.EMPTY_STRING_ARRAY);
274: assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY,
275: newArray));
276: assertTrue(Arrays.equals((new String[] {}), newArray));
277: assertEquals(String.class, newArray.getClass()
278: .getComponentType());
279: newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY,
280: ArrayUtils.EMPTY_STRING_ARRAY);
281: assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY,
282: newArray));
283: assertTrue(Arrays.equals((new String[] {}), newArray));
284: assertEquals(String.class, newArray.getClass()
285: .getComponentType());
286: String[] stringArrayNull = new String[] { null };
287: newArray = ArrayUtils.addAll(stringArrayNull, stringArrayNull);
288: assertTrue(Arrays.equals((new String[] { null, null }),
289: newArray));
290: assertEquals(String.class, newArray.getClass()
291: .getComponentType());
292:
293: // boolean
294: assertTrue(Arrays.equals(new boolean[] { true, false, false,
295: true }, ArrayUtils.addAll(
296: new boolean[] { true, false }, new boolean[] { false,
297: true })));
298:
299: assertTrue(Arrays.equals(new boolean[] { false, true },
300: ArrayUtils.addAll(null, new boolean[] { false, true })));
301:
302: assertTrue(Arrays.equals(new boolean[] { true, false },
303: ArrayUtils.addAll(new boolean[] { true, false }, null)));
304:
305: // char
306: assertTrue(Arrays.equals(new char[] { 'a', 'b', 'c', 'd' },
307: ArrayUtils.addAll(new char[] { 'a', 'b' }, new char[] {
308: 'c', 'd' })));
309:
310: assertTrue(Arrays.equals(new char[] { 'c', 'd' }, ArrayUtils
311: .addAll(null, new char[] { 'c', 'd' })));
312:
313: assertTrue(Arrays.equals(new char[] { 'a', 'b' }, ArrayUtils
314: .addAll(new char[] { 'a', 'b' }, null)));
315:
316: // byte
317: assertTrue(Arrays.equals(new byte[] { (byte) 0, (byte) 1,
318: (byte) 2, (byte) 3 }, ArrayUtils
319: .addAll(new byte[] { (byte) 0, (byte) 1 }, new byte[] {
320: (byte) 2, (byte) 3 })));
321:
322: assertTrue(Arrays.equals(new byte[] { (byte) 2, (byte) 3 },
323: ArrayUtils.addAll(null,
324: new byte[] { (byte) 2, (byte) 3 })));
325:
326: assertTrue(Arrays.equals(new byte[] { (byte) 0, (byte) 1 },
327: ArrayUtils.addAll(new byte[] { (byte) 0, (byte) 1 },
328: null)));
329:
330: // short
331: assertTrue(Arrays.equals(new short[] { (short) 10, (short) 20,
332: (short) 30, (short) 40 }, ArrayUtils.addAll(
333: new short[] { (short) 10, (short) 20 }, new short[] {
334: (short) 30, (short) 40 })));
335:
336: assertTrue(Arrays.equals(
337: new short[] { (short) 30, (short) 40 }, ArrayUtils
338: .addAll(null, new short[] { (short) 30,
339: (short) 40 })));
340:
341: assertTrue(Arrays.equals(
342: new short[] { (short) 10, (short) 20 }, ArrayUtils
343: .addAll(new short[] { (short) 10, (short) 20 },
344: null)));
345:
346: // int
347: assertTrue(Arrays.equals(new int[] { 1, 1000, -1000, -1 },
348: ArrayUtils.addAll(new int[] { 1, 1000 }, new int[] {
349: -1000, -1 })));
350:
351: assertTrue(Arrays.equals(new int[] { -1000, -1 }, ArrayUtils
352: .addAll(null, new int[] { -1000, -1 })));
353:
354: assertTrue(Arrays.equals(new int[] { 1, 1000 }, ArrayUtils
355: .addAll(new int[] { 1, 1000 }, null)));
356:
357: // long
358: assertTrue(Arrays.equals(new long[] { 1L, -1L, 1000L, -1000L },
359: ArrayUtils.addAll(new long[] { 1L, -1L }, new long[] {
360: 1000L, -1000L })));
361:
362: assertTrue(Arrays.equals(new long[] { 1000L, -1000L },
363: ArrayUtils.addAll(null, new long[] { 1000L, -1000L })));
364:
365: assertTrue(Arrays.equals(new long[] { 1L, -1L }, ArrayUtils
366: .addAll(new long[] { 1L, -1L }, null)));
367:
368: // float
369: assertTrue(Arrays.equals(new float[] { 10.5f, 10.1f, 1.6f,
370: 0.01f }, ArrayUtils.addAll(
371: new float[] { 10.5f, 10.1f },
372: new float[] { 1.6f, 0.01f })));
373:
374: assertTrue(Arrays.equals(new float[] { 1.6f, 0.01f },
375: ArrayUtils.addAll(null, new float[] { 1.6f, 0.01f })));
376:
377: assertTrue(Arrays.equals(new float[] { 10.5f, 10.1f },
378: ArrayUtils.addAll(new float[] { 10.5f, 10.1f }, null)));
379:
380: // double
381: assertTrue(Arrays.equals(new double[] { Math.PI, -Math.PI, 0,
382: 9.99 }, ArrayUtils.addAll(new double[] { Math.PI,
383: -Math.PI }, new double[] { 0, 9.99 })));
384:
385: assertTrue(Arrays.equals(new double[] { 0, 9.99 }, ArrayUtils
386: .addAll(null, new double[] { 0, 9.99 })));
387:
388: assertTrue(Arrays.equals(new double[] { Math.PI, -Math.PI },
389: ArrayUtils.addAll(new double[] { Math.PI, -Math.PI },
390: null)));
391:
392: }
393:
394: public void testAddObjectAtIndex() {
395: Object[] newArray;
396: newArray = ArrayUtils.add((Object[]) null, 0, null);
397: assertTrue(Arrays.equals((new Object[] { null }), newArray));
398: assertEquals(Object.class, newArray.getClass()
399: .getComponentType());
400: newArray = ArrayUtils.add((Object[]) null, 0, "a");
401: assertTrue(Arrays.equals((new String[] { "a" }), newArray));
402: assertTrue(Arrays.equals((new Object[] { "a" }), newArray));
403: assertEquals(String.class, newArray.getClass()
404: .getComponentType());
405: String[] stringArray1 = new String[] { "a", "b", "c" };
406: newArray = ArrayUtils.add(stringArray1, 0, null);
407: assertTrue(Arrays.equals(
408: (new String[] { null, "a", "b", "c" }), newArray));
409: assertEquals(String.class, newArray.getClass()
410: .getComponentType());
411: newArray = ArrayUtils.add(stringArray1, 1, null);
412: assertTrue(Arrays.equals(
413: (new String[] { "a", null, "b", "c" }), newArray));
414: assertEquals(String.class, newArray.getClass()
415: .getComponentType());
416: newArray = ArrayUtils.add(stringArray1, 3, null);
417: assertTrue(Arrays.equals(
418: (new String[] { "a", "b", "c", null }), newArray));
419: assertEquals(String.class, newArray.getClass()
420: .getComponentType());
421: newArray = ArrayUtils.add(stringArray1, 3, "d");
422: assertTrue(Arrays.equals((new String[] { "a", "b", "c", "d" }),
423: newArray));
424: assertEquals(String.class, newArray.getClass()
425: .getComponentType());
426: assertEquals(String.class, newArray.getClass()
427: .getComponentType());
428:
429: Object[] o = new Object[] { "1", "2", "4" };
430: Object[] result = ArrayUtils.add(o, 2, "3");
431: Object[] result2 = ArrayUtils.add(o, 3, "5");
432:
433: assertNotNull(result);
434: assertEquals(4, result.length);
435: assertEquals("1", result[0]);
436: assertEquals("2", result[1]);
437: assertEquals("3", result[2]);
438: assertEquals("4", result[3]);
439: assertNotNull(result2);
440: assertEquals(4, result2.length);
441: assertEquals("1", result2[0]);
442: assertEquals("2", result2[1]);
443: assertEquals("4", result2[2]);
444: assertEquals("5", result2[3]);
445:
446: // boolean tests
447: boolean[] booleanArray = ArrayUtils.add(null, 0, true);
448: assertTrue(Arrays.equals(new boolean[] { true }, booleanArray));
449: try {
450: booleanArray = ArrayUtils.add(null, -1, true);
451: } catch (IndexOutOfBoundsException e) {
452: assertEquals("Index: -1, Length: 0", e.getMessage());
453: }
454: booleanArray = ArrayUtils.add(new boolean[] { true }, 0, false);
455: assertTrue(Arrays.equals(new boolean[] { false, true },
456: booleanArray));
457: booleanArray = ArrayUtils.add(new boolean[] { false }, 1, true);
458: assertTrue(Arrays.equals(new boolean[] { false, true },
459: booleanArray));
460: booleanArray = ArrayUtils.add(new boolean[] { true, false }, 1,
461: true);
462: assertTrue(Arrays.equals(new boolean[] { true, true, false },
463: booleanArray));
464: try {
465: booleanArray = ArrayUtils.add(
466: new boolean[] { true, false }, 4, true);
467: } catch (IndexOutOfBoundsException e) {
468: assertEquals("Index: 4, Length: 2", e.getMessage());
469: }
470: try {
471: booleanArray = ArrayUtils.add(
472: new boolean[] { true, false }, -1, true);
473: } catch (IndexOutOfBoundsException e) {
474: assertEquals("Index: -1, Length: 2", e.getMessage());
475: }
476:
477: // char tests
478: char[] charArray = ArrayUtils.add((char[]) null, 0, 'a');
479: assertTrue(Arrays.equals(new char[] { 'a' }, charArray));
480: try {
481: charArray = ArrayUtils.add((char[]) null, -1, 'a');
482: } catch (IndexOutOfBoundsException e) {
483: assertEquals("Index: -1, Length: 0", e.getMessage());
484: }
485: charArray = ArrayUtils.add(new char[] { 'a' }, 0, 'b');
486: assertTrue(Arrays.equals(new char[] { 'b', 'a' }, charArray));
487: charArray = ArrayUtils.add(new char[] { 'a', 'b' }, 0, 'c');
488: assertTrue(Arrays.equals(new char[] { 'c', 'a', 'b' },
489: charArray));
490: charArray = ArrayUtils.add(new char[] { 'a', 'b' }, 1, 'k');
491: assertTrue(Arrays.equals(new char[] { 'a', 'k', 'b' },
492: charArray));
493: charArray = ArrayUtils
494: .add(new char[] { 'a', 'b', 'c' }, 1, 't');
495: assertTrue(Arrays.equals(new char[] { 'a', 't', 'b', 'c' },
496: charArray));
497: try {
498: charArray = ArrayUtils.add(new char[] { 'a', 'b' }, 4, 'c');
499: } catch (IndexOutOfBoundsException e) {
500: assertEquals("Index: 4, Length: 2", e.getMessage());
501: }
502: try {
503: charArray = ArrayUtils
504: .add(new char[] { 'a', 'b' }, -1, 'c');
505: } catch (IndexOutOfBoundsException e) {
506: assertEquals("Index: -1, Length: 2", e.getMessage());
507: }
508:
509: // short tests
510: short[] shortArray = ArrayUtils.add(new short[] { 1 }, 0,
511: (short) 2);
512: assertTrue(Arrays.equals(new short[] { 2, 1 }, shortArray));
513: try {
514: shortArray = ArrayUtils.add((short[]) null, -1, (short) 2);
515: } catch (IndexOutOfBoundsException e) {
516: assertEquals("Index: -1, Length: 0", e.getMessage());
517: }
518: shortArray = ArrayUtils
519: .add(new short[] { 2, 6 }, 2, (short) 10);
520: assertTrue(Arrays.equals(new short[] { 2, 6, 10 }, shortArray));
521: shortArray = ArrayUtils
522: .add(new short[] { 2, 6 }, 0, (short) -4);
523: assertTrue(Arrays.equals(new short[] { -4, 2, 6 }, shortArray));
524: shortArray = ArrayUtils.add(new short[] { 2, 6, 3 }, 2,
525: (short) 1);
526: assertTrue(Arrays
527: .equals(new short[] { 2, 6, 1, 3 }, shortArray));
528: try {
529: shortArray = ArrayUtils.add(new short[] { 2, 6 }, 4,
530: (short) 10);
531: } catch (IndexOutOfBoundsException e) {
532: assertEquals("Index: 4, Length: 2", e.getMessage());
533: }
534: try {
535: shortArray = ArrayUtils.add(new short[] { 2, 6 }, -1,
536: (short) 10);
537: } catch (IndexOutOfBoundsException e) {
538: assertEquals("Index: -1, Length: 2", e.getMessage());
539: }
540:
541: // byte tests
542: byte[] byteArray = ArrayUtils
543: .add(new byte[] { 1 }, 0, (byte) 2);
544: assertTrue(Arrays.equals(new byte[] { 2, 1 }, byteArray));
545: try {
546: byteArray = ArrayUtils.add((byte[]) null, -1, (byte) 2);
547: } catch (IndexOutOfBoundsException e) {
548: assertEquals("Index: -1, Length: 0", e.getMessage());
549: }
550: byteArray = ArrayUtils.add(new byte[] { 2, 6 }, 2, (byte) 3);
551: assertTrue(Arrays.equals(new byte[] { 2, 6, 3 }, byteArray));
552: byteArray = ArrayUtils.add(new byte[] { 2, 6 }, 0, (byte) 1);
553: assertTrue(Arrays.equals(new byte[] { 1, 2, 6 }, byteArray));
554: byteArray = ArrayUtils.add(new byte[] { 2, 6, 3 }, 2, (byte) 1);
555: assertTrue(Arrays.equals(new byte[] { 2, 6, 1, 3 }, byteArray));
556: try {
557: byteArray = ArrayUtils
558: .add(new byte[] { 2, 6 }, 4, (byte) 3);
559: } catch (IndexOutOfBoundsException e) {
560: assertEquals("Index: 4, Length: 2", e.getMessage());
561: }
562: try {
563: byteArray = ArrayUtils.add(new byte[] { 2, 6 }, -1,
564: (byte) 3);
565: } catch (IndexOutOfBoundsException e) {
566: assertEquals("Index: -1, Length: 2", e.getMessage());
567: }
568:
569: // int tests
570: int[] intArray = ArrayUtils.add(new int[] { 1 }, 0, 2);
571: assertTrue(Arrays.equals(new int[] { 2, 1 }, intArray));
572: try {
573: intArray = ArrayUtils.add((int[]) null, -1, 2);
574: } catch (IndexOutOfBoundsException e) {
575: assertEquals("Index: -1, Length: 0", e.getMessage());
576: }
577: intArray = ArrayUtils.add(new int[] { 2, 6 }, 2, 10);
578: assertTrue(Arrays.equals(new int[] { 2, 6, 10 }, intArray));
579: intArray = ArrayUtils.add(new int[] { 2, 6 }, 0, -4);
580: assertTrue(Arrays.equals(new int[] { -4, 2, 6 }, intArray));
581: intArray = ArrayUtils.add(new int[] { 2, 6, 3 }, 2, 1);
582: assertTrue(Arrays.equals(new int[] { 2, 6, 1, 3 }, intArray));
583: try {
584: intArray = ArrayUtils.add(new int[] { 2, 6 }, 4, 10);
585: } catch (IndexOutOfBoundsException e) {
586: assertEquals("Index: 4, Length: 2", e.getMessage());
587: }
588: try {
589: intArray = ArrayUtils.add(new int[] { 2, 6 }, -1, 10);
590: } catch (IndexOutOfBoundsException e) {
591: assertEquals("Index: -1, Length: 2", e.getMessage());
592: }
593:
594: // long tests
595: long[] longArray = ArrayUtils.add(new long[] { 1L }, 0, 2L);
596: assertTrue(Arrays.equals(new long[] { 2L, 1L }, longArray));
597: try {
598: longArray = ArrayUtils.add((long[]) null, -1, 2L);
599: } catch (IndexOutOfBoundsException e) {
600: assertEquals("Index: -1, Length: 0", e.getMessage());
601: }
602: longArray = ArrayUtils.add(new long[] { 2L, 6L }, 2, 10L);
603: assertTrue(Arrays.equals(new long[] { 2L, 6L, 10L }, longArray));
604: longArray = ArrayUtils.add(new long[] { 2L, 6L }, 0, -4L);
605: assertTrue(Arrays.equals(new long[] { -4L, 2L, 6L }, longArray));
606: longArray = ArrayUtils.add(new long[] { 2L, 6L, 3L }, 2, 1L);
607: assertTrue(Arrays.equals(new long[] { 2L, 6L, 1L, 3L },
608: longArray));
609: try {
610: longArray = ArrayUtils.add(new long[] { 2L, 6L }, 4, 10L);
611: } catch (IndexOutOfBoundsException e) {
612: assertEquals("Index: 4, Length: 2", e.getMessage());
613: }
614: try {
615: longArray = ArrayUtils.add(new long[] { 2L, 6L }, -1, 10L);
616: } catch (IndexOutOfBoundsException e) {
617: assertEquals("Index: -1, Length: 2", e.getMessage());
618: }
619:
620: // float tests
621: float[] floatArray = ArrayUtils.add(new float[] { 1.1f }, 0,
622: 2.2f);
623: assertTrue(Arrays
624: .equals(new float[] { 2.2f, 1.1f }, floatArray));
625: try {
626: floatArray = ArrayUtils.add((float[]) null, -1, 2.2f);
627: } catch (IndexOutOfBoundsException e) {
628: assertEquals("Index: -1, Length: 0", e.getMessage());
629: }
630: floatArray = ArrayUtils.add(new float[] { 2.3f, 6.4f }, 2,
631: 10.5f);
632: assertTrue(Arrays.equals(new float[] { 2.3f, 6.4f, 10.5f },
633: floatArray));
634: floatArray = ArrayUtils.add(new float[] { 2.6f, 6.7f }, 0,
635: -4.8f);
636: assertTrue(Arrays.equals(new float[] { -4.8f, 2.6f, 6.7f },
637: floatArray));
638: floatArray = ArrayUtils.add(new float[] { 2.9f, 6.0f, 0.3f },
639: 2, 1.0f);
640: assertTrue(Arrays.equals(
641: new float[] { 2.9f, 6.0f, 1.0f, 0.3f }, floatArray));
642: try {
643: floatArray = ArrayUtils.add(new float[] { 2.3f, 6.4f }, 4,
644: 10.5f);
645: } catch (IndexOutOfBoundsException e) {
646: assertEquals("Index: 4, Length: 2", e.getMessage());
647: }
648: try {
649: floatArray = ArrayUtils.add(new float[] { 2.3f, 6.4f }, -1,
650: 10.5f);
651: } catch (IndexOutOfBoundsException e) {
652: assertEquals("Index: -1, Length: 2", e.getMessage());
653: }
654:
655: // double tests
656: double[] doubleArray = ArrayUtils.add(new double[] { 1.1 }, 0,
657: 2.2);
658: assertTrue(Arrays
659: .equals(new double[] { 2.2, 1.1 }, doubleArray));
660: try {
661: doubleArray = ArrayUtils.add((double[]) null, -1, 2.2);
662: } catch (IndexOutOfBoundsException e) {
663: assertEquals("Index: -1, Length: 0", e.getMessage());
664: }
665: doubleArray = ArrayUtils
666: .add(new double[] { 2.3, 6.4 }, 2, 10.5);
667: assertTrue(Arrays.equals(new double[] { 2.3, 6.4, 10.5 },
668: doubleArray));
669: doubleArray = ArrayUtils
670: .add(new double[] { 2.6, 6.7 }, 0, -4.8);
671: assertTrue(Arrays.equals(new double[] { -4.8, 2.6, 6.7 },
672: doubleArray));
673: doubleArray = ArrayUtils.add(new double[] { 2.9, 6.0, 0.3 }, 2,
674: 1.0);
675: assertTrue(Arrays.equals(new double[] { 2.9, 6.0, 1.0, 0.3 },
676: doubleArray));
677: try {
678: doubleArray = ArrayUtils.add(new double[] { 2.3, 6.4 }, 4,
679: 10.5);
680: } catch (IndexOutOfBoundsException e) {
681: assertEquals("Index: 4, Length: 2", e.getMessage());
682: }
683: try {
684: doubleArray = ArrayUtils.add(new double[] { 2.3, 6.4 }, -1,
685: 10.5);
686: } catch (IndexOutOfBoundsException e) {
687: assertEquals("Index: -1, Length: 2", e.getMessage());
688: }
689: }
690:
691: }
|