From 11bd2237bf4e50799e091f3bfa9aba5e50c8de70 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Tue, 3 Feb 2004 22:14:24 +0000 Subject: [PATCH] Bugzilla Bug 26056 [lang] Add methods to ArrayUtils: add at end and insert-like ops Added methods: primitiveType[] add(primitiveType[], primitiveType) git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137769 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/ArrayUtils.java | 275 ++++++++++++++-- .../commons/lang/ArrayUtilsAddTest.java | 308 ++++++++++++++++++ .../apache/commons/lang/ArrayUtilsTest.java | 99 +----- .../apache/commons/lang/LangTestSuite.java | 3 +- 4 files changed, 567 insertions(+), 118 deletions(-) create mode 100644 src/test/org/apache/commons/lang/ArrayUtilsAddTest.java diff --git a/src/java/org/apache/commons/lang/ArrayUtils.java b/src/java/org/apache/commons/lang/ArrayUtils.java index 43def04a6..a3818cd40 100644 --- a/src/java/org/apache/commons/lang/ArrayUtils.java +++ b/src/java/org/apache/commons/lang/ArrayUtils.java @@ -81,7 +81,7 @@ import org.apache.commons.lang.builder.ToStringStyle; * @author Gary Gregory * @author Ashwin S * @since 2.0 - * @version $Id: ArrayUtils.java,v 1.39 2004/01/31 20:12:16 ggregory Exp $ + * @version $Id: ArrayUtils.java,v 1.40 2004/02/03 22:14:24 ggregory Exp $ */ public class ArrayUtils { @@ -2787,11 +2787,11 @@ public class ArrayUtils { } /** - *

Adds the element to the end of the array.

+ *

Copies the given array and adds the given element at the end of the new array.

* *

The new array contains the same elements of the input * array plus the given element in the last position. The component type of - * the new array is the same as that of the input array.

+ * the new array is the same as that of the input array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

@@ -2806,25 +2806,262 @@ public class ArrayUtils { * * @param array the array to "add" the element to, may be null * @param element the object to add - * @return A new array containing the existing elements and the new element + * @return A new array containing the existing elements plus the new element * @since 2.1 */ - public static Object[] add(Object[] array, Object element) { - Object joinedArray; - int elementPos; - if (array != null) { - joinedArray = Array.newInstance(array.getClass().getComponentType(), array.length + 1); - System.arraycopy(array, 0, joinedArray, 0, array.length); - elementPos = array.length; - } else { - // null input array, use the element type - joinedArray = Array.newInstance(element != null ? element.getClass() : Object.class, 1); - elementPos = 0; - } - Array.set(joinedArray, elementPos, element); - return (Object[]) joinedArray; + public static Object[] add(final Object[] array, final Object element) { + Object newArray = copyArrayGrow1(array, element != null ? element.getClass() : Object.class); + Array.set(newArray, lastIndex(newArray), element); + return (Object[]) newArray; } - + + /** + *

Copies the given array and adds the given element at the end of the new array.

+ * + *

The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array.

+ * + *

If the input array is null, a new one element array is returned + * whose component type is the same as the element.

+ * + *
+     * ArrayUtils.add(null, true)          = [true]
+     * ArrayUtils.add([true], false)       = [true, false]
+     * ArrayUtils.add([true, false], true) = [true, false, true]
+     * 
+ * + * @param array the array to copy and add the element to, may be null + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static boolean[] add(final boolean[] array, final boolean element) { + boolean[] newArray = (boolean[])copyArrayGrow1(array, Boolean.TYPE); + newArray[lastIndex(newArray)] = element; + return newArray; + } + + /** + *

Copies the given array and adds the given element at the end of the new array.

+ * + *

The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array.

+ * + *

If the input array is null, a new one element array is returned + * whose component type is the same as the element.

+ * + *
+     * ArrayUtils.add(null, 0)   = [0]
+     * ArrayUtils.add([1], 0)    = [1, 0]
+     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+     * 
+ * + * @param array the array to copy and add the element to, may be null + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static byte[] add(final byte[] array, final byte element) { + byte[] newArray = (byte[])copyArrayGrow1(array, Byte.TYPE); + newArray[lastIndex(newArray)] = element; + return newArray; + } + + /** + *

Copies the given array and adds the given element at the end of the new array.

+ * + *

The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array.

+ * + *

If the input array is null, a new one element array is returned + * whose component type is the same as the element.

+ * + *
+     * ArrayUtils.add(null, '0')       = ['0']
+     * ArrayUtils.add(['1'], '0')      = ['1', '0']
+     * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
+     * 
+ * + * @param array the array to copy and add the element to, may be null + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static char[] add(final char[] array, final char element) { + char[] newArray = (char[])copyArrayGrow1(array, Character.TYPE); + newArray[lastIndex(newArray)] = element; + return newArray; + } + + /** + *

Copies the given array and adds the given element at the end of the new array.

+ * + *

The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array.

+ * + *

If the input array is null, a new one element array is returned + * whose component type is the same as the element.

+ * + *
+     * ArrayUtils.add(null, 0)   = [0]
+     * ArrayUtils.add([1], 0)    = [1, 0]
+     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+     * 
+ * + * @param array the array to copy and add the element to, may be null + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static double[] add(final double[] array, final double element) { + double[] newArray = (double[])copyArrayGrow1(array, Double.TYPE); + newArray[lastIndex(newArray)] = element; + return newArray; + } + + /** + *

Copies the given array and adds the given element at the end of the new array.

+ * + *

The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array.

+ * + *

If the input array is null, a new one element array is returned + * whose component type is the same as the element.

+ * + *
+     * ArrayUtils.add(null, 0)   = [0]
+     * ArrayUtils.add([1], 0)    = [1, 0]
+     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+     * 
+ * + * @param array the array to copy and add the element to, may be null + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static float[] add(final float[] array, final float element) { + float[] newArray = (float[])copyArrayGrow1(array, Float.TYPE); + newArray[lastIndex(newArray)] = element; + return newArray; + } + + /** + *

Copies the given array and adds the given element at the end of the new array.

+ * + *

The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array.

+ * + *

If the input array is null, a new one element array is returned + * whose component type is the same as the element.

+ * + *
+     * ArrayUtils.add(null, 0)   = [0]
+     * ArrayUtils.add([1], 0)    = [1, 0]
+     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+     * 
+ * + * @param array the array to copy and add the element to, may be null + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static int[] add(final int[] array, final int element) { + int[] newArray = (int[])copyArrayGrow1(array, Integer.TYPE); + newArray[lastIndex(newArray)] = element; + return newArray; + } + + /** + *

Copies the given array and adds the given element at the end of the new array.

+ * + *

The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array.

+ * + *

If the input array is null, a new one element array is returned + * whose component type is the same as the element.

+ * + *
+     * ArrayUtils.add(null, 0)   = [0]
+     * ArrayUtils.add([1], 0)    = [1, 0]
+     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+     * 
+ * + * @param array the array to copy and add the element to, may be null + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static long[] add(final long[] array, final long element) { + long[] newArray = (long[])copyArrayGrow1(array, Long.TYPE); + newArray[lastIndex(newArray)] = element; + return newArray; + } + + /** + *

Copies the given array and adds the given element at the end of the new array.

+ * + *

The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array.

+ * + *

If the input array is null, a new one element array is returned + * whose component type is the same as the element.

+ * + *
+     * ArrayUtils.add(null, 0)   = [0]
+     * ArrayUtils.add([1], 0)    = [1, 0]
+     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
+     * 
+ * + * @param array the array to copy and add the element to, may be null + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static short[] add(final short[] array, final short element) { + short[] newArray = (short[])copyArrayGrow1(array, Short.TYPE); + newArray[lastIndex(newArray)] = element; + return newArray; + } + + /** + * Returns a copy of the given array of size 1 greater than the argument. + * The last value of the array is left to the default value. + * + * @param array The array to copy, must not be null. + * @param newArrayComponentType If array is null, create a + * size 1 array of this type. + * @return A new copy of the array of size 1 greater than the input. + */ + private static Object copyArrayGrow1(final Object array, Class newArrayComponentType) { + if (array != null) { + int arrayLength = Array.getLength(array); + Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1); + System.arraycopy(array, 0, newArray, 0, arrayLength); + return newArray; + } else { + return Array.newInstance(newArrayComponentType, 1); + } + } + + /** + * Returns the last index of the given array. This value is one less than the size since + * arrays indices are 0-based. + * + * @param array The array to return the last index for, must not be null. + * @return The last index + */ + public static int lastIndex(final Object array) { + return Array.getLength(array) - 1; + } + /** *

Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent diff --git a/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java b/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java new file mode 100644 index 000000000..b0a54c6d6 --- /dev/null +++ b/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java @@ -0,0 +1,308 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.commons.lang; + +import java.util.Arrays; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.textui.TestRunner; + +/** + * Tests ArrayUtils add methods. + * + * @author Gary D. Gregory + * @version $Id: ArrayUtilsAddTest.java,v 1.1 2004/02/03 22:14:24 ggregory Exp $ + */ +public class ArrayUtilsAddTest extends TestCase { + public static void main(String[] args) { + TestRunner.run(suite()); + } + + public static Test suite() { + TestSuite suite = new TestSuite(ArrayUtilsAddTest.class); + suite.setName("ArrayUtils add Tests"); + return suite; + } + + public void testAddObjectArrayBoolean() { + boolean[] newArray; + newArray = ArrayUtils.add((boolean[])null, false); + assertTrue(Arrays.equals(new boolean[]{false}, newArray)); + assertEquals(Boolean.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((boolean[])null, true); + assertTrue(Arrays.equals(new boolean[]{true}, newArray)); + assertEquals(Boolean.TYPE, newArray.getClass().getComponentType()); + boolean[] array1 = new boolean[]{true, false, true}; + newArray = ArrayUtils.add(array1, false); + assertTrue(Arrays.equals(new boolean[]{true, false, true, false}, newArray)); + assertEquals(Boolean.TYPE, newArray.getClass().getComponentType()); + } + + public void testAddObjectArrayByte() { + byte[] newArray; + newArray = ArrayUtils.add((byte[])null, (byte)0); + assertTrue(Arrays.equals(new byte[]{0}, newArray)); + assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((byte[])null, (byte)1); + assertTrue(Arrays.equals(new byte[]{1}, newArray)); + assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); + byte[] array1 = new byte[]{1, 2, 3}; + newArray = ArrayUtils.add(array1, (byte)0); + assertTrue(Arrays.equals(new byte[]{1, 2, 3, 0}, newArray)); + assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(array1, (byte)4); + assertTrue(Arrays.equals(new byte[]{1, 2, 3, 4}, newArray)); + assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); + } + + public void testAddObjectArrayChar() { + char[] newArray; + newArray = ArrayUtils.add((char[])null, (char)0); + assertTrue(Arrays.equals(new char[]{0}, newArray)); + assertEquals(Character.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((char[])null, (char)1); + assertTrue(Arrays.equals(new char[]{1}, newArray)); + assertEquals(Character.TYPE, newArray.getClass().getComponentType()); + char[] array1 = new char[]{1, 2, 3}; + newArray = ArrayUtils.add(array1, (char)0); + assertTrue(Arrays.equals(new char[]{1, 2, 3, 0}, newArray)); + assertEquals(Character.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(array1, (char)4); + assertTrue(Arrays.equals(new char[]{1, 2, 3, 4}, newArray)); + assertEquals(Character.TYPE, newArray.getClass().getComponentType()); + } + + public void testAddObjectArrayDouble() { + double[] newArray; + newArray = ArrayUtils.add((double[])null, 0); + assertTrue(Arrays.equals(new double[]{0}, newArray)); + assertEquals(Double.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((double[])null, 1); + assertTrue(Arrays.equals(new double[]{1}, newArray)); + assertEquals(Double.TYPE, newArray.getClass().getComponentType()); + double[] array1 = new double[]{1, 2, 3}; + newArray = ArrayUtils.add(array1, 0); + assertTrue(Arrays.equals(new double[]{1, 2, 3, 0}, newArray)); + assertEquals(Double.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(array1, 4); + assertTrue(Arrays.equals(new double[]{1, 2, 3, 4}, newArray)); + assertEquals(Double.TYPE, newArray.getClass().getComponentType()); + } + + public void testAddObjectArrayFloat() { + float[] newArray; + newArray = ArrayUtils.add((float[])null, 0); + assertTrue(Arrays.equals(new float[]{0}, newArray)); + assertEquals(Float.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((float[])null, 1); + assertTrue(Arrays.equals(new float[]{1}, newArray)); + assertEquals(Float.TYPE, newArray.getClass().getComponentType()); + float[] array1 = new float[]{1, 2, 3}; + newArray = ArrayUtils.add(array1, 0); + assertTrue(Arrays.equals(new float[]{1, 2, 3, 0}, newArray)); + assertEquals(Float.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(array1, 4); + assertTrue(Arrays.equals(new float[]{1, 2, 3, 4}, newArray)); + assertEquals(Float.TYPE, newArray.getClass().getComponentType()); + } + + public void testAddObjectArrayInt() { + int[] newArray; + newArray = ArrayUtils.add((int[])null, 0); + assertTrue(Arrays.equals(new int[]{0}, newArray)); + assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((int[])null, 1); + assertTrue(Arrays.equals(new int[]{1}, newArray)); + assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); + int[] array1 = new int[]{1, 2, 3}; + newArray = ArrayUtils.add(array1, 0); + assertTrue(Arrays.equals(new int[]{1, 2, 3, 0}, newArray)); + assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(array1, 4); + assertTrue(Arrays.equals(new int[]{1, 2, 3, 4}, newArray)); + assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); + } + + public void testAddObjectArrayLong() { + long[] newArray; + newArray = ArrayUtils.add((long[])null, 0); + assertTrue(Arrays.equals(new long[]{0}, newArray)); + assertEquals(Long.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((long[])null, 1); + assertTrue(Arrays.equals(new long[]{1}, newArray)); + assertEquals(Long.TYPE, newArray.getClass().getComponentType()); + long[] array1 = new long[]{1, 2, 3}; + newArray = ArrayUtils.add(array1, 0); + assertTrue(Arrays.equals(new long[]{1, 2, 3, 0}, newArray)); + assertEquals(Long.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(array1, 4); + assertTrue(Arrays.equals(new long[]{1, 2, 3, 4}, newArray)); + assertEquals(Long.TYPE, newArray.getClass().getComponentType()); + } + + public void testAddObjectArrayShort() { + short[] newArray; + newArray = ArrayUtils.add((short[])null, (short)0); + assertTrue(Arrays.equals(new short[]{0}, newArray)); + assertEquals(Short.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((short[])null, (short)1); + assertTrue(Arrays.equals(new short[]{1}, newArray)); + assertEquals(Short.TYPE, newArray.getClass().getComponentType()); + short[] array1 = new short[]{1, 2, 3}; + newArray = ArrayUtils.add(array1, (short)0); + assertTrue(Arrays.equals(new short[]{1, 2, 3, 0}, newArray)); + assertEquals(Short.TYPE, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(array1, (short)4); + assertTrue(Arrays.equals(new short[]{1, 2, 3, 4}, newArray)); + assertEquals(Short.TYPE, newArray.getClass().getComponentType()); + } + + public void testAddObjectArrayObject() { + Object[] newArray; + newArray = ArrayUtils.add((Object[])null, null); + assertTrue(Arrays.equals((new Object[]{null}), newArray)); + assertEquals(Object.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((Object[])null, "a"); + assertTrue(Arrays.equals((new String[]{"a"}), newArray)); + assertTrue(Arrays.equals((new Object[]{"a"}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + String[] stringArray1 = new String[]{"a", "b", "c"}; + newArray = ArrayUtils.add(stringArray1, null); + assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(stringArray1, "d"); + assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + } + + public void testAddObjectArrayToObjectArray() { + assertNull(ArrayUtils.addAll(null, null)); + Object[] newArray; + String[] stringArray1 = new String[]{"a", "b", "c"}; + String[] stringArray2 = new String[]{"1", "2", "3"}; + newArray = ArrayUtils.addAll(stringArray1, null); + assertTrue(Arrays.equals(stringArray1, newArray)); + assertTrue(Arrays.equals((new String[]{"a", "b", "c"}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.addAll(null, stringArray2); + assertTrue(Arrays.equals(stringArray2, newArray)); + assertTrue(Arrays.equals((new String[]{"1", "2", "3"}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.addAll(stringArray1, stringArray2); + assertTrue(Arrays.equals((new String[]{"a", "b", "c", "1", "2", "3"}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, null); + assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray)); + assertTrue(Arrays.equals((new String[]{}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.addAll(null, ArrayUtils.EMPTY_STRING_ARRAY); + assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray)); + assertTrue(Arrays.equals((new String[]{}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.EMPTY_STRING_ARRAY); + assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray)); + assertTrue(Arrays.equals((new String[]{}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + String[] stringArrayNull = new String []{null}; + newArray = ArrayUtils.addAll(stringArrayNull, stringArrayNull); + assertTrue(Arrays.equals((new String[]{null, null}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + } + + public void testAddObjectAtIndex() { + Object[] newArray; + newArray = ArrayUtils.add((Object[])null, 0, null); + assertTrue(Arrays.equals((new Object[]{null}), newArray)); + assertEquals(Object.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((Object[])null, 0, "a"); + assertTrue(Arrays.equals((new String[]{"a"}), newArray)); + assertTrue(Arrays.equals((new Object[]{"a"}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + String[] stringArray1 = new String[]{"a", "b", "c"}; + newArray = ArrayUtils.add(stringArray1, 0, null); + assertTrue(Arrays.equals((new String[]{null, "a", "b", "c"}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(stringArray1, 1, null); + assertTrue(Arrays.equals((new String[]{"a", null, "b", "c"}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(stringArray1, 3, null); + assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add(stringArray1, 3, "d"); + assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray)); + assertEquals(String.class, newArray.getClass().getComponentType()); + assertEquals(String.class, newArray.getClass().getComponentType()); + + Object[] o = new Object[] {"1", "2", "4"}; + Object[] result = ArrayUtils.add(o, 2, "3"); + Object[] result2 = ArrayUtils.add(o, 3, "5"); + + assertNotNull(result); + assertEquals(4, result.length); + assertEquals("1", result[0]); + assertEquals("2", result[1]); + assertEquals("3", result[2]); + assertEquals("4", result[3]); + assertNotNull(result2); + assertEquals(4, result2.length); + assertEquals("1", result2[0]); + assertEquals("2", result2[1]); + assertEquals("4", result2[2]); + assertEquals("5", result2[3]); + } + +} diff --git a/src/test/org/apache/commons/lang/ArrayUtilsTest.java b/src/test/org/apache/commons/lang/ArrayUtilsTest.java index 97141ce32..7f383d1ad 100644 --- a/src/test/org/apache/commons/lang/ArrayUtilsTest.java +++ b/src/test/org/apache/commons/lang/ArrayUtilsTest.java @@ -75,7 +75,7 @@ import junit.textui.TestRunner; * @author Ashwin S * @author Fredrik Westermarck * @author Gary Gregory - * @version $Id: ArrayUtilsTest.java,v 1.23 2004/01/31 20:12:15 ggregory Exp $ + * @version $Id: ArrayUtilsTest.java,v 1.24 2004/02/03 22:14:24 ggregory Exp $ */ public class ArrayUtilsTest extends TestCase { @@ -111,103 +111,6 @@ public class ArrayUtilsTest extends TestCase { assertEquals(false, Modifier.isFinal(ArrayUtils.class.getModifiers())); } - void assertArraysEquals(Object[] array1, Object[] array2) { - assertTrue(Arrays.equals(array1, array2)); - } - - public void testJoin() { - assertNull(ArrayUtils.addAll(null, null)); - Object[] joinedArray; - String[] stringArray1 = new String[]{"a", "b", "c"}; - String[] stringArray2 = new String[]{"1", "2", "3"}; - joinedArray = ArrayUtils.addAll(stringArray1, null); - assertArraysEquals(stringArray1, joinedArray); - assertArraysEquals(new String[]{"a", "b", "c"}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.addAll(null, stringArray2); - assertArraysEquals(stringArray2, joinedArray); - assertArraysEquals(new String[]{"1", "2", "3"}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.addAll(stringArray1, stringArray2); - assertArraysEquals(new String[]{"a", "b", "c", "1", "2", "3"}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, null); - assertArraysEquals(ArrayUtils.EMPTY_STRING_ARRAY, joinedArray); - assertArraysEquals(new String[]{}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.addAll(null, ArrayUtils.EMPTY_STRING_ARRAY); - assertArraysEquals(ArrayUtils.EMPTY_STRING_ARRAY, joinedArray); - assertArraysEquals(new String[]{}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.EMPTY_STRING_ARRAY); - assertArraysEquals(ArrayUtils.EMPTY_STRING_ARRAY, joinedArray); - assertArraysEquals(new String[]{}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - String[] stringArrayNull = new String []{null}; - joinedArray = ArrayUtils.addAll(stringArrayNull, stringArrayNull); - assertArraysEquals(new String[]{null, null}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - } - - public void testAddObjectArrayObject() { - Object[] joinedArray; - joinedArray = ArrayUtils.add((Object[])null, null); - assertArraysEquals(new Object[]{null}, joinedArray); - assertEquals(Object.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.add((Object[])null, "a"); - assertArraysEquals(new String[]{"a"}, joinedArray); - assertArraysEquals(new Object[]{"a"}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - String[] stringArray1 = new String[]{"a", "b", "c"}; - joinedArray = ArrayUtils.add(stringArray1, null); - assertArraysEquals(new String[]{"a", "b", "c", null}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.add(stringArray1, "d"); - assertArraysEquals(new String[]{"a", "b", "c", "d"}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - } - - public void testAddAtIndex() { - Object[] joinedArray; - joinedArray = ArrayUtils.add((Object[])null, 0, null); - assertArraysEquals(new Object[]{null}, joinedArray); - assertEquals(Object.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.add((Object[])null, 0, "a"); - assertArraysEquals(new String[]{"a"}, joinedArray); - assertArraysEquals(new Object[]{"a"}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - String[] stringArray1 = new String[]{"a", "b", "c"}; - joinedArray = ArrayUtils.add(stringArray1, 0, null); - assertArraysEquals(new String[]{null, "a", "b", "c"}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.add(stringArray1, 1, null); - assertArraysEquals(new String[]{"a", null, "b", "c"}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.add(stringArray1, 3, null); - assertArraysEquals(new String[]{"a", "b", "c", null}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - joinedArray = ArrayUtils.add(stringArray1, 3, "d"); - assertArraysEquals(new String[]{"a", "b", "c", "d"}, joinedArray); - assertEquals(String.class, joinedArray.getClass().getComponentType()); - - Object[] o = new Object[] {"1", "2", "4"}; - Object[] result = ArrayUtils.add(o, 2, "3"); - Object[] result2 = ArrayUtils.add(o, 3, "5"); - - assertNotNull(result); - assertEquals(4, result.length); - assertEquals("1", result[0]); - assertEquals("2", result[1]); - assertEquals("3", result[2]); - assertEquals("4", result[3]); - assertNotNull(result2); - assertEquals(4, result2.length); - assertEquals("1", result2[0]); - assertEquals("2", result2[1]); - assertEquals("4", result2[2]); - assertEquals("5", result2[3]); - } - //----------------------------------------------------------------------- public void testToString() { assertEquals("{}", ArrayUtils.toString(null)); diff --git a/src/test/org/apache/commons/lang/LangTestSuite.java b/src/test/org/apache/commons/lang/LangTestSuite.java index 9ab1efc7e..932bbd249 100644 --- a/src/test/org/apache/commons/lang/LangTestSuite.java +++ b/src/test/org/apache/commons/lang/LangTestSuite.java @@ -64,7 +64,7 @@ import junit.textui.TestRunner; * @author Stephen Colebourne * @author Ringo De Smet * @author Matthew Hawthorne - * @version $Id: LangTestSuite.java,v 1.24 2004/01/31 13:00:07 scolebourne Exp $ + * @version $Id: LangTestSuite.java,v 1.25 2004/02/03 22:14:24 ggregory Exp $ */ public class LangTestSuite extends TestCase { @@ -89,6 +89,7 @@ public class LangTestSuite extends TestCase { TestSuite suite = new TestSuite(); suite.setName("Commons-Lang Tests"); suite.addTest(ArrayUtilsTest.suite()); + suite.addTest(ArrayUtilsAddTest.suite()); suite.addTest(BitFieldTest.suite()); suite.addTest(BooleanUtilsTest.suite()); suite.addTest(CharRangeTest.suite());