1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.commons.lang;
19 
20 import java.util.Arrays;
21 
22 import junit.framework.TestCase;
23 
24 /**
25  * Tests ArrayUtils add methods.
26  *
27  * @author Gary D. Gregory
28  * @version $Id: ArrayUtilsAddTest.java 905628 2010-02-02 13:29:55Z niallp $
29  */
30 public class ArrayUtilsAddTest extends TestCase {
31 
testJira567()32     public void testJira567(){
33         Number[] n;
34         // Valid array construction
35         n = (Number[])ArrayUtils.addAll(new Number[]{new Integer(1)}, new Long[]{new Long(2)});
36         assertEquals(2,n.length);
37         assertEquals(Number.class,n.getClass().getComponentType());
38         try {
39             // Invalid - can't store Long in Integer array
40                n = (Number[])ArrayUtils.addAll(new Integer[]{new Integer(1)}, new Long[]{new Long(2)});
41                fail("Should have generated IllegalArgumentException");
42         } catch (IllegalArgumentException expected) {
43         }
44     }
45 
testAddObjectArrayBoolean()46     public void testAddObjectArrayBoolean() {
47         boolean[] newArray;
48         newArray = ArrayUtils.add((boolean[])null, false);
49         assertTrue(Arrays.equals(new boolean[]{false}, newArray));
50         assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
51         newArray = ArrayUtils.add((boolean[])null, true);
52         assertTrue(Arrays.equals(new boolean[]{true}, newArray));
53         assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
54         boolean[] array1 = new boolean[]{true, false, true};
55         newArray = ArrayUtils.add(array1, false);
56         assertTrue(Arrays.equals(new boolean[]{true, false, true, false}, newArray));
57         assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
58     }
59 
testAddObjectArrayByte()60     public void testAddObjectArrayByte() {
61         byte[] newArray;
62         newArray = ArrayUtils.add((byte[])null, (byte)0);
63         assertTrue(Arrays.equals(new byte[]{0}, newArray));
64         assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
65         newArray = ArrayUtils.add((byte[])null, (byte)1);
66         assertTrue(Arrays.equals(new byte[]{1}, newArray));
67         assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
68         byte[] array1 = new byte[]{1, 2, 3};
69         newArray = ArrayUtils.add(array1, (byte)0);
70         assertTrue(Arrays.equals(new byte[]{1, 2, 3, 0}, newArray));
71         assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
72         newArray = ArrayUtils.add(array1, (byte)4);
73         assertTrue(Arrays.equals(new byte[]{1, 2, 3, 4}, newArray));
74         assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
75     }
76 
testAddObjectArrayChar()77     public void testAddObjectArrayChar() {
78         char[] newArray;
79         newArray = ArrayUtils.add((char[])null, (char)0);
80         assertTrue(Arrays.equals(new char[]{0}, newArray));
81         assertEquals(Character.TYPE, newArray.getClass().getComponentType());
82         newArray = ArrayUtils.add((char[])null, (char)1);
83         assertTrue(Arrays.equals(new char[]{1}, newArray));
84         assertEquals(Character.TYPE, newArray.getClass().getComponentType());
85         char[] array1 = new char[]{1, 2, 3};
86         newArray = ArrayUtils.add(array1, (char)0);
87         assertTrue(Arrays.equals(new char[]{1, 2, 3, 0}, newArray));
88         assertEquals(Character.TYPE, newArray.getClass().getComponentType());
89         newArray = ArrayUtils.add(array1, (char)4);
90         assertTrue(Arrays.equals(new char[]{1, 2, 3, 4}, newArray));
91         assertEquals(Character.TYPE, newArray.getClass().getComponentType());
92     }
93 
testAddObjectArrayDouble()94     public void testAddObjectArrayDouble() {
95         double[] newArray;
96         newArray = ArrayUtils.add((double[])null, 0);
97         assertTrue(Arrays.equals(new double[]{0}, newArray));
98         assertEquals(Double.TYPE, newArray.getClass().getComponentType());
99         newArray = ArrayUtils.add((double[])null, 1);
100         assertTrue(Arrays.equals(new double[]{1}, newArray));
101         assertEquals(Double.TYPE, newArray.getClass().getComponentType());
102         double[] array1 = new double[]{1, 2, 3};
103         newArray = ArrayUtils.add(array1, 0);
104         assertTrue(Arrays.equals(new double[]{1, 2, 3, 0}, newArray));
105         assertEquals(Double.TYPE, newArray.getClass().getComponentType());
106         newArray = ArrayUtils.add(array1, 4);
107         assertTrue(Arrays.equals(new double[]{1, 2, 3, 4}, newArray));
108         assertEquals(Double.TYPE, newArray.getClass().getComponentType());
109     }
110 
testAddObjectArrayFloat()111     public void testAddObjectArrayFloat() {
112         float[] newArray;
113         newArray = ArrayUtils.add((float[])null, 0);
114         assertTrue(Arrays.equals(new float[]{0}, newArray));
115         assertEquals(Float.TYPE, newArray.getClass().getComponentType());
116         newArray = ArrayUtils.add((float[])null, 1);
117         assertTrue(Arrays.equals(new float[]{1}, newArray));
118         assertEquals(Float.TYPE, newArray.getClass().getComponentType());
119         float[] array1 = new float[]{1, 2, 3};
120         newArray = ArrayUtils.add(array1, 0);
121         assertTrue(Arrays.equals(new float[]{1, 2, 3, 0}, newArray));
122         assertEquals(Float.TYPE, newArray.getClass().getComponentType());
123         newArray = ArrayUtils.add(array1, 4);
124         assertTrue(Arrays.equals(new float[]{1, 2, 3, 4}, newArray));
125         assertEquals(Float.TYPE, newArray.getClass().getComponentType());
126     }
127 
testAddObjectArrayInt()128     public void testAddObjectArrayInt() {
129         int[] newArray;
130         newArray = ArrayUtils.add((int[])null, 0);
131         assertTrue(Arrays.equals(new int[]{0}, newArray));
132         assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
133         newArray = ArrayUtils.add((int[])null, 1);
134         assertTrue(Arrays.equals(new int[]{1}, newArray));
135         assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
136         int[] array1 = new int[]{1, 2, 3};
137         newArray = ArrayUtils.add(array1, 0);
138         assertTrue(Arrays.equals(new int[]{1, 2, 3, 0}, newArray));
139         assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
140         newArray = ArrayUtils.add(array1, 4);
141         assertTrue(Arrays.equals(new int[]{1, 2, 3, 4}, newArray));
142         assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
143     }
144 
testAddObjectArrayLong()145     public void testAddObjectArrayLong() {
146         long[] newArray;
147         newArray = ArrayUtils.add((long[])null, 0);
148         assertTrue(Arrays.equals(new long[]{0}, newArray));
149         assertEquals(Long.TYPE, newArray.getClass().getComponentType());
150         newArray = ArrayUtils.add((long[])null, 1);
151         assertTrue(Arrays.equals(new long[]{1}, newArray));
152         assertEquals(Long.TYPE, newArray.getClass().getComponentType());
153         long[] array1 = new long[]{1, 2, 3};
154         newArray = ArrayUtils.add(array1, 0);
155         assertTrue(Arrays.equals(new long[]{1, 2, 3, 0}, newArray));
156         assertEquals(Long.TYPE, newArray.getClass().getComponentType());
157         newArray = ArrayUtils.add(array1, 4);
158         assertTrue(Arrays.equals(new long[]{1, 2, 3, 4}, newArray));
159         assertEquals(Long.TYPE, newArray.getClass().getComponentType());
160     }
161 
testAddObjectArrayShort()162     public void testAddObjectArrayShort() {
163         short[] newArray;
164         newArray = ArrayUtils.add((short[])null, (short)0);
165         assertTrue(Arrays.equals(new short[]{0}, newArray));
166         assertEquals(Short.TYPE, newArray.getClass().getComponentType());
167         newArray = ArrayUtils.add((short[])null, (short)1);
168         assertTrue(Arrays.equals(new short[]{1}, newArray));
169         assertEquals(Short.TYPE, newArray.getClass().getComponentType());
170         short[] array1 = new short[]{1, 2, 3};
171         newArray = ArrayUtils.add(array1, (short)0);
172         assertTrue(Arrays.equals(new short[]{1, 2, 3, 0}, newArray));
173         assertEquals(Short.TYPE, newArray.getClass().getComponentType());
174         newArray = ArrayUtils.add(array1, (short)4);
175         assertTrue(Arrays.equals(new short[]{1, 2, 3, 4}, newArray));
176         assertEquals(Short.TYPE, newArray.getClass().getComponentType());
177     }
178 
testAddObjectArrayObject()179     public void testAddObjectArrayObject() {
180         Object[] newArray;
181         newArray = ArrayUtils.add((Object[])null, null);
182         assertTrue(Arrays.equals((new Object[]{null}), newArray));
183         assertEquals(Object.class, newArray.getClass().getComponentType());
184 
185         newArray = ArrayUtils.add((Object[])null, "a");
186         assertTrue(Arrays.equals((new String[]{"a"}), newArray));
187         assertTrue(Arrays.equals((new Object[]{"a"}), newArray));
188         assertEquals(String.class, newArray.getClass().getComponentType());
189 
190         String[] stringArray1 = new String[]{"a", "b", "c"};
191         newArray = ArrayUtils.add(stringArray1, null);
192         assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray));
193         assertEquals(String.class, newArray.getClass().getComponentType());
194 
195         newArray = ArrayUtils.add(stringArray1, "d");
196         assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray));
197         assertEquals(String.class, newArray.getClass().getComponentType());
198 
199         Number[] numberArray1 = new Number[]{new Integer(1), new Double(2)};
200         newArray = ArrayUtils.add(numberArray1, new Float(3));
201         assertTrue(Arrays.equals((new Number[]{new Integer(1), new Double(2), new Float(3)}), newArray));
202         assertEquals(Number.class, newArray.getClass().getComponentType());
203 
204         numberArray1 = null;
205         newArray = ArrayUtils.add(numberArray1, new Float(3));
206         assertTrue(Arrays.equals((new Float[]{new Float(3)}), newArray));
207         assertEquals(Float.class, newArray.getClass().getComponentType());
208 
209         numberArray1 = null;
210         newArray = ArrayUtils.add(numberArray1, null);
211         assertTrue(Arrays.equals((new Object[]{null}), newArray));
212         assertEquals(Object.class, newArray.getClass().getComponentType());
213     }
214 
testAddObjectArrayToObjectArray()215     public void testAddObjectArrayToObjectArray() {
216         assertNull(ArrayUtils.addAll((Object[]) null, (Object[]) null));
217         Object[] newArray;
218         String[] stringArray1 = new String[]{"a", "b", "c"};
219         String[] stringArray2 = new String[]{"1", "2", "3"};
220         newArray = ArrayUtils.addAll(stringArray1, (String[]) null);
221         assertNotSame(stringArray1, newArray);
222         assertTrue(Arrays.equals(stringArray1, newArray));
223         assertTrue(Arrays.equals((new String[]{"a", "b", "c"}), newArray));
224         assertEquals(String.class, newArray.getClass().getComponentType());
225         newArray = ArrayUtils.addAll(null, stringArray2);
226         assertNotSame(stringArray2, newArray);
227         assertTrue(Arrays.equals(stringArray2, newArray));
228         assertTrue(Arrays.equals((new String[]{"1", "2", "3"}), newArray));
229         assertEquals(String.class, newArray.getClass().getComponentType());
230         newArray = ArrayUtils.addAll(stringArray1, stringArray2);
231         assertTrue(Arrays.equals((new String[]{"a", "b", "c", "1", "2", "3"}), newArray));
232         assertEquals(String.class, newArray.getClass().getComponentType());
233         newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, (String[]) null);
234         assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray));
235         assertTrue(Arrays.equals((new String[]{}), newArray));
236         assertEquals(String.class, newArray.getClass().getComponentType());
237         newArray = ArrayUtils.addAll(null, ArrayUtils.EMPTY_STRING_ARRAY);
238         assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray));
239         assertTrue(Arrays.equals((new String[]{}), newArray));
240         assertEquals(String.class, newArray.getClass().getComponentType());
241         newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.EMPTY_STRING_ARRAY);
242         assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray));
243         assertTrue(Arrays.equals((new String[]{}), newArray));
244         assertEquals(String.class, newArray.getClass().getComponentType());
245         String[] stringArrayNull = new String []{null};
246         newArray = ArrayUtils.addAll(stringArrayNull, stringArrayNull);
247         assertTrue(Arrays.equals((new String[]{null, null}), newArray));
248         assertEquals(String.class, newArray.getClass().getComponentType());
249 
250         // boolean
251         assertTrue( Arrays.equals( new boolean[] { true, false, false, true },
252             ArrayUtils.addAll( new boolean[] { true, false }, new boolean[] { false, true } ) ) );
253 
254         assertTrue( Arrays.equals( new boolean[] { false, true },
255             ArrayUtils.addAll( null, new boolean[] { false, true } ) ) );
256 
257         assertTrue( Arrays.equals( new boolean[] { true, false },
258             ArrayUtils.addAll( new boolean[] { true, false }, null ) ) );
259 
260         // char
261         assertTrue( Arrays.equals( new char[] { 'a', 'b', 'c', 'd' },
262             ArrayUtils.addAll( new char[] { 'a', 'b' }, new char[] { 'c', 'd' } ) ) );
263 
264         assertTrue( Arrays.equals( new char[] { 'c', 'd' },
265             ArrayUtils.addAll( null, new char[] { 'c', 'd' } ) ) );
266 
267         assertTrue( Arrays.equals( new char[] { 'a', 'b' },
268             ArrayUtils.addAll( new char[] { 'a', 'b' }, null ) ) );
269 
270         // byte
271         assertTrue( Arrays.equals( new byte[] { (byte) 0, (byte) 1, (byte) 2, (byte) 3 },
272             ArrayUtils.addAll( new byte[] { (byte) 0, (byte) 1 }, new byte[] { (byte) 2, (byte) 3 } ) ) );
273 
274         assertTrue( Arrays.equals( new byte[] { (byte) 2, (byte) 3 },
275             ArrayUtils.addAll( null, new byte[] { (byte) 2, (byte) 3 } ) ) );
276 
277         assertTrue( Arrays.equals( new byte[] { (byte) 0, (byte) 1 },
278             ArrayUtils.addAll( new byte[] { (byte) 0, (byte) 1 }, null ) ) );
279 
280         // short
281         assertTrue( Arrays.equals( new short[] { (short) 10, (short) 20, (short) 30, (short) 40 },
282             ArrayUtils.addAll( new short[] { (short) 10, (short) 20 }, new short[] { (short) 30, (short) 40 } ) ) );
283 
284         assertTrue( Arrays.equals( new short[] { (short) 30, (short) 40 },
285             ArrayUtils.addAll( null, new short[] { (short) 30, (short) 40 } ) ) );
286 
287         assertTrue( Arrays.equals( new short[] { (short) 10, (short) 20 },
288             ArrayUtils.addAll( new short[] { (short) 10, (short) 20 }, null ) ) );
289 
290         // int
291         assertTrue( Arrays.equals( new int[] { 1, 1000, -1000, -1 },
292             ArrayUtils.addAll( new int[] { 1, 1000 }, new int[] { -1000, -1 } ) ) );
293 
294         assertTrue( Arrays.equals( new int[] { -1000, -1 },
295             ArrayUtils.addAll( null, new int[] { -1000, -1 } ) ) );
296 
297         assertTrue( Arrays.equals( new int[] { 1, 1000 },
298             ArrayUtils.addAll( new int[] { 1, 1000 }, null ) ) );
299 
300         // long
301         assertTrue( Arrays.equals( new long[] { 1L, -1L, 1000L, -1000L },
302             ArrayUtils.addAll( new long[] { 1L, -1L }, new long[] { 1000L, -1000L } ) ) );
303 
304         assertTrue( Arrays.equals( new long[] { 1000L, -1000L },
305             ArrayUtils.addAll( null, new long[] { 1000L, -1000L } ) ) );
306 
307         assertTrue( Arrays.equals( new long[] { 1L, -1L },
308             ArrayUtils.addAll( new long[] { 1L, -1L }, null ) ) );
309 
310         // float
311         assertTrue( Arrays.equals( new float[] { 10.5f, 10.1f, 1.6f, 0.01f },
312             ArrayUtils.addAll( new float[] { 10.5f, 10.1f }, new float[] { 1.6f, 0.01f } ) ) );
313 
314         assertTrue( Arrays.equals( new float[] { 1.6f, 0.01f },
315             ArrayUtils.addAll( null, new float[] { 1.6f, 0.01f } ) ) );
316 
317         assertTrue( Arrays.equals( new float[] { 10.5f, 10.1f },
318             ArrayUtils.addAll( new float[] { 10.5f, 10.1f }, null ) ) );
319 
320         // double
321         assertTrue( Arrays.equals( new double[] { Math.PI, -Math.PI, 0, 9.99 },
322             ArrayUtils.addAll( new double[] { Math.PI, -Math.PI }, new double[] { 0, 9.99 } ) ) );
323 
324         assertTrue( Arrays.equals( new double[] { 0, 9.99 },
325             ArrayUtils.addAll( null, new double[] { 0, 9.99 } ) ) );
326 
327         assertTrue( Arrays.equals( new double[] { Math.PI, -Math.PI },
328             ArrayUtils.addAll( new double[] { Math.PI, -Math.PI }, null ) ) );
329 
330     }
331 
testAddObjectAtIndex()332     public void testAddObjectAtIndex() {
333         Object[] newArray;
334         newArray = ArrayUtils.add((Object[])null, 0, null);
335         assertTrue(Arrays.equals((new Object[]{null}), newArray));
336         assertEquals(Object.class, newArray.getClass().getComponentType());
337         newArray = ArrayUtils.add((Object[])null, 0, "a");
338         assertTrue(Arrays.equals((new String[]{"a"}), newArray));
339         assertTrue(Arrays.equals((new Object[]{"a"}), newArray));
340         assertEquals(String.class, newArray.getClass().getComponentType());
341         String[] stringArray1 = new String[]{"a", "b", "c"};
342         newArray = ArrayUtils.add(stringArray1, 0, null);
343         assertTrue(Arrays.equals((new String[]{null, "a", "b", "c"}), newArray));
344         assertEquals(String.class, newArray.getClass().getComponentType());
345         newArray = ArrayUtils.add(stringArray1, 1, null);
346         assertTrue(Arrays.equals((new String[]{"a", null, "b", "c"}), newArray));
347         assertEquals(String.class, newArray.getClass().getComponentType());
348         newArray = ArrayUtils.add(stringArray1, 3, null);
349         assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray));
350         assertEquals(String.class, newArray.getClass().getComponentType());
351         newArray = ArrayUtils.add(stringArray1, 3, "d");
352         assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray));
353         assertEquals(String.class, newArray.getClass().getComponentType());
354         assertEquals(String.class, newArray.getClass().getComponentType());
355 
356         Object[] o = new Object[] {"1", "2", "4"};
357         Object[] result = ArrayUtils.add(o, 2, "3");
358         Object[] result2 = ArrayUtils.add(o, 3, "5");
359 
360         assertNotNull(result);
361         assertEquals(4, result.length);
362         assertEquals("1", result[0]);
363         assertEquals("2", result[1]);
364         assertEquals("3", result[2]);
365         assertEquals("4", result[3]);
366         assertNotNull(result2);
367         assertEquals(4, result2.length);
368         assertEquals("1", result2[0]);
369         assertEquals("2", result2[1]);
370         assertEquals("4", result2[2]);
371         assertEquals("5", result2[3]);
372 
373         // boolean tests
374         boolean[] booleanArray = ArrayUtils.add( null, 0, true );
375         assertTrue( Arrays.equals( new boolean[] { true }, booleanArray ) );
376         try {
377             booleanArray = ArrayUtils.add( null, -1, true );
378         } catch(IndexOutOfBoundsException e) {
379             assertEquals("Index: -1, Length: 0", e.getMessage());
380         }
381         booleanArray = ArrayUtils.add( new boolean[] { true }, 0, false);
382         assertTrue( Arrays.equals( new boolean[] { false, true }, booleanArray ) );
383         booleanArray = ArrayUtils.add( new boolean[] { false }, 1, true);
384         assertTrue( Arrays.equals( new boolean[] { false, true }, booleanArray ) );
385         booleanArray = ArrayUtils.add( new boolean[] { true, false }, 1, true);
386         assertTrue( Arrays.equals( new boolean[] { true, true, false }, booleanArray ) );
387         try {
388             booleanArray = ArrayUtils.add( new boolean[] { true, false }, 4, true);
389         } catch(IndexOutOfBoundsException e) {
390             assertEquals("Index: 4, Length: 2", e.getMessage());
391         }
392         try {
393             booleanArray = ArrayUtils.add( new boolean[] { true, false }, -1, true);
394         } catch(IndexOutOfBoundsException e) {
395             assertEquals("Index: -1, Length: 2", e.getMessage());
396         }
397 
398         // char tests
399         char[] charArray = ArrayUtils.add( (char[]) null, 0, 'a' );
400         assertTrue( Arrays.equals( new char[] { 'a' }, charArray ) );
401         try {
402             charArray = ArrayUtils.add( (char[]) null, -1, 'a' );
403         } catch(IndexOutOfBoundsException e) {
404             assertEquals("Index: -1, Length: 0", e.getMessage());
405         }
406         charArray = ArrayUtils.add( new char[] { 'a' }, 0, 'b');
407         assertTrue( Arrays.equals( new char[] { 'b', 'a' }, charArray ) );
408         charArray = ArrayUtils.add( new char[] { 'a', 'b' }, 0, 'c');
409         assertTrue( Arrays.equals( new char[] { 'c', 'a', 'b' }, charArray ) );
410         charArray = ArrayUtils.add( new char[] { 'a', 'b' }, 1, 'k');
411         assertTrue( Arrays.equals( new char[] { 'a', 'k', 'b' }, charArray ) );
412         charArray = ArrayUtils.add( new char[] { 'a', 'b', 'c' }, 1, 't');
413         assertTrue( Arrays.equals( new char[] { 'a', 't', 'b', 'c' }, charArray ) );
414         try {
415             charArray = ArrayUtils.add( new char[] { 'a', 'b' }, 4, 'c');
416         } catch(IndexOutOfBoundsException e) {
417             assertEquals("Index: 4, Length: 2", e.getMessage());
418         }
419         try {
420             charArray = ArrayUtils.add( new char[] { 'a', 'b' }, -1, 'c');
421         } catch(IndexOutOfBoundsException e) {
422             assertEquals("Index: -1, Length: 2", e.getMessage());
423         }
424 
425         // short tests
426         short[] shortArray = ArrayUtils.add( new short[] { 1 }, 0, (short) 2);
427         assertTrue( Arrays.equals( new short[] { 2, 1 }, shortArray ) );
428         try {
429             shortArray = ArrayUtils.add( (short[]) null, -1, (short) 2);
430         } catch(IndexOutOfBoundsException e) {
431             assertEquals("Index: -1, Length: 0", e.getMessage());
432         }
433         shortArray = ArrayUtils.add( new short[] { 2, 6 }, 2, (short) 10);
434         assertTrue( Arrays.equals( new short[] { 2, 6, 10 }, shortArray ) );
435         shortArray = ArrayUtils.add( new short[] { 2, 6 }, 0, (short) -4);
436         assertTrue( Arrays.equals( new short[] { -4, 2, 6 }, shortArray ) );
437         shortArray = ArrayUtils.add( new short[] { 2, 6, 3 }, 2, (short) 1);
438         assertTrue( Arrays.equals( new short[] { 2, 6, 1, 3 }, shortArray ) );
439         try {
440             shortArray = ArrayUtils.add( new short[] { 2, 6 }, 4, (short) 10);
441         } catch(IndexOutOfBoundsException e) {
442             assertEquals("Index: 4, Length: 2", e.getMessage());
443         }
444         try {
445             shortArray = ArrayUtils.add( new short[] { 2, 6 }, -1, (short) 10);
446         } catch(IndexOutOfBoundsException e) {
447             assertEquals("Index: -1, Length: 2", e.getMessage());
448         }
449 
450         // byte tests
451         byte[] byteArray = ArrayUtils.add( new byte[] { 1 }, 0, (byte) 2);
452         assertTrue( Arrays.equals( new byte[] { 2, 1 }, byteArray ) );
453         try {
454             byteArray = ArrayUtils.add( (byte[]) null, -1, (byte) 2);
455         } catch(IndexOutOfBoundsException e) {
456             assertEquals("Index: -1, Length: 0", e.getMessage());
457         }
458         byteArray = ArrayUtils.add( new byte[] { 2, 6 }, 2, (byte) 3);
459         assertTrue( Arrays.equals( new byte[] { 2, 6, 3 }, byteArray ) );
460         byteArray = ArrayUtils.add( new byte[] { 2, 6 }, 0, (byte) 1);
461         assertTrue( Arrays.equals( new byte[] { 1, 2, 6 }, byteArray ) );
462         byteArray = ArrayUtils.add( new byte[] { 2, 6, 3 }, 2, (byte) 1);
463         assertTrue( Arrays.equals( new byte[] { 2, 6, 1, 3 }, byteArray ) );
464         try {
465             byteArray = ArrayUtils.add( new byte[] { 2, 6 }, 4, (byte) 3);
466         } catch(IndexOutOfBoundsException e) {
467             assertEquals("Index: 4, Length: 2", e.getMessage());
468         }
469         try {
470             byteArray = ArrayUtils.add( new byte[] { 2, 6 }, -1, (byte) 3);
471         } catch(IndexOutOfBoundsException e) {
472             assertEquals("Index: -1, Length: 2", e.getMessage());
473         }
474 
475         // int tests
476         int[] intArray = ArrayUtils.add( new int[] { 1 }, 0, 2);
477         assertTrue( Arrays.equals( new int[] { 2, 1 }, intArray ) );
478         try {
479             intArray = ArrayUtils.add( (int[]) null, -1, 2);
480         } catch(IndexOutOfBoundsException e) {
481             assertEquals("Index: -1, Length: 0", e.getMessage());
482         }
483         intArray = ArrayUtils.add( new int[] { 2, 6 }, 2, 10);
484         assertTrue( Arrays.equals( new int[] { 2, 6, 10 }, intArray ) );
485         intArray = ArrayUtils.add( new int[] { 2, 6 }, 0, -4);
486         assertTrue( Arrays.equals( new int[] { -4, 2, 6 }, intArray ) );
487         intArray = ArrayUtils.add( new int[] { 2, 6, 3 }, 2, 1);
488         assertTrue( Arrays.equals( new int[] { 2, 6, 1, 3 }, intArray ) );
489         try {
490             intArray = ArrayUtils.add( new int[] { 2, 6 }, 4, 10);
491         } catch(IndexOutOfBoundsException e) {
492             assertEquals("Index: 4, Length: 2", e.getMessage());
493         }
494         try {
495             intArray = ArrayUtils.add( new int[] { 2, 6 }, -1, 10);
496         } catch(IndexOutOfBoundsException e) {
497             assertEquals("Index: -1, Length: 2", e.getMessage());
498         }
499 
500         // long tests
501         long[] longArray = ArrayUtils.add( new long[] { 1L }, 0, 2L);
502         assertTrue( Arrays.equals( new long[] { 2L, 1L }, longArray ) );
503         try {
504             longArray = ArrayUtils.add( (long[]) null, -1, 2L);
505         } catch(IndexOutOfBoundsException e) {
506             assertEquals("Index: -1, Length: 0", e.getMessage());
507         }
508         longArray = ArrayUtils.add( new long[] { 2L, 6L }, 2, 10L);
509         assertTrue( Arrays.equals( new long[] { 2L, 6L, 10L }, longArray ) );
510         longArray = ArrayUtils.add( new long[] { 2L, 6L }, 0, -4L);
511         assertTrue( Arrays.equals( new long[] { -4L, 2L, 6L }, longArray ) );
512         longArray = ArrayUtils.add( new long[] { 2L, 6L, 3L }, 2, 1L);
513         assertTrue( Arrays.equals( new long[] { 2L, 6L, 1L, 3L }, longArray ) );
514         try {
515             longArray = ArrayUtils.add( new long[] { 2L, 6L }, 4, 10L);
516         } catch(IndexOutOfBoundsException e) {
517             assertEquals("Index: 4, Length: 2", e.getMessage());
518         }
519         try {
520             longArray = ArrayUtils.add( new long[] { 2L, 6L }, -1, 10L);
521         } catch(IndexOutOfBoundsException e) {
522             assertEquals("Index: -1, Length: 2", e.getMessage());
523         }
524 
525         // float tests
526         float[] floatArray = ArrayUtils.add( new float[] { 1.1f }, 0, 2.2f);
527         assertTrue( Arrays.equals( new float[] { 2.2f, 1.1f }, floatArray ) );
528         try {
529             floatArray = ArrayUtils.add( (float[]) null, -1, 2.2f);
530         } catch(IndexOutOfBoundsException e) {
531             assertEquals("Index: -1, Length: 0", e.getMessage());
532         }
533         floatArray = ArrayUtils.add( new float[] { 2.3f, 6.4f }, 2, 10.5f);
534         assertTrue( Arrays.equals( new float[] { 2.3f, 6.4f, 10.5f }, floatArray ) );
535         floatArray = ArrayUtils.add( new float[] { 2.6f, 6.7f }, 0, -4.8f);
536         assertTrue( Arrays.equals( new float[] { -4.8f, 2.6f, 6.7f }, floatArray ) );
537         floatArray = ArrayUtils.add( new float[] { 2.9f, 6.0f, 0.3f }, 2, 1.0f);
538         assertTrue( Arrays.equals( new float[] { 2.9f, 6.0f, 1.0f, 0.3f }, floatArray ) );
539         try {
540             floatArray = ArrayUtils.add( new float[] { 2.3f, 6.4f }, 4, 10.5f);
541         } catch(IndexOutOfBoundsException e) {
542             assertEquals("Index: 4, Length: 2", e.getMessage());
543         }
544         try {
545             floatArray = ArrayUtils.add( new float[] { 2.3f, 6.4f }, -1, 10.5f);
546         } catch(IndexOutOfBoundsException e) {
547             assertEquals("Index: -1, Length: 2", e.getMessage());
548         }
549 
550         // double tests
551         double[] doubleArray = ArrayUtils.add( new double[] { 1.1 }, 0, 2.2);
552         assertTrue( Arrays.equals( new double[] { 2.2, 1.1 }, doubleArray ) );
553         try {
554           doubleArray = ArrayUtils.add( (double[]) null, -1, 2.2);
555         } catch(IndexOutOfBoundsException e) {
556             assertEquals("Index: -1, Length: 0", e.getMessage());
557         }
558         doubleArray = ArrayUtils.add( new double[] { 2.3, 6.4 }, 2, 10.5);
559         assertTrue( Arrays.equals( new double[] { 2.3, 6.4, 10.5 }, doubleArray ) );
560         doubleArray = ArrayUtils.add( new double[] { 2.6, 6.7 }, 0, -4.8);
561         assertTrue( Arrays.equals( new double[] { -4.8, 2.6, 6.7 }, doubleArray ) );
562         doubleArray = ArrayUtils.add( new double[] { 2.9, 6.0, 0.3 }, 2, 1.0);
563         assertTrue( Arrays.equals( new double[] { 2.9, 6.0, 1.0, 0.3 }, doubleArray ) );
564         try {
565             doubleArray = ArrayUtils.add( new double[] { 2.3, 6.4 }, 4, 10.5);
566         } catch(IndexOutOfBoundsException e) {
567             assertEquals("Index: 4, Length: 2", e.getMessage());
568         }
569         try {
570             doubleArray = ArrayUtils.add( new double[] { 2.3, 6.4 }, -1, 10.5);
571         } catch(IndexOutOfBoundsException e) {
572             assertEquals("Index: -1, Length: 2", e.getMessage());
573         }
574     }
575 
576 }
577