commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r892114 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang3/ArrayUtils.java test/org/apache/commons/lang3/ArrayUtilsAddTest.java
Date Fri, 18 Dec 2009 02:50:22 GMT
Author: sebb
Date: Fri Dec 18 02:50:21 2009
New Revision: 892114

URL: http://svn.apache.org/viewvc?rev=892114&view=rev
Log:
LANG-567 - ArrayUtils.addAll(T[] array1, T... array2) does not handle mixed types very well
Also remove unnecessary main() and suite() from test class

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang3/ArrayUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang3/ArrayUtilsAddTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang3/ArrayUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang3/ArrayUtils.java?rev=892114&r1=892113&r2=892114&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang3/ArrayUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang3/ArrayUtils.java Fri Dec 18
02:50:21 2009
@@ -2956,10 +2956,19 @@
         } else if (array2 == null) {
             return clone(array1);
         }
-        T[] joinedArray = (T[]) Array.newInstance(array1.getClass().getComponentType(),
-                                                            array1.length + array2.length);
+        final Class<?> type1 = array1.getClass().getComponentType();
+        T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
         System.arraycopy(array1, 0, joinedArray, 0, array1.length);
-        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+        try {
+            System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
+        } catch (ArrayStoreException ase) {
+            // Check if problem is incompatible types
+            final Class<?> type2 = array2.getClass().getComponentType();
+            if (!type1.isAssignableFrom(type2)){
+                throw new IllegalArgumentException("Cannot store "+type2.getName()+" in an
array of "+type1.getName());
+            }
+            throw ase; // No, so rethrow original
+        }
         return joinedArray;
     }
 

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang3/ArrayUtilsAddTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang3/ArrayUtilsAddTest.java?rev=892114&r1=892113&r2=892114&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang3/ArrayUtilsAddTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang3/ArrayUtilsAddTest.java Fri
Dec 18 02:50:21 2009
@@ -19,10 +19,7 @@
 
 import java.util.Arrays;
 
-import junit.framework.Test;
 import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import junit.textui.TestRunner;
 
 /**
  * Tests ArrayUtils add methods.
@@ -31,14 +28,19 @@
  * @version $Id$
  */
 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 testJira567(){
+        Number[] n;
+        // Valid array construction
+        n = ArrayUtils.addAll(new Number[]{Integer.valueOf(1)}, new Long[]{Long.valueOf(2)});
+        assertEquals(2,n.length);
+        assertEquals(Number.class,n.getClass().getComponentType());
+        try {
+            // Invalid - can't store Long in Integer array
+               n = ArrayUtils.addAll(new Integer[]{Integer.valueOf(1)}, new Long[]{Long.valueOf(2)});
+               fail("Should have generated IllegalArgumentException");
+        } catch (IllegalArgumentException expected) {
+        }
     }
 
     public void testAddObjectArrayBoolean() {



Mime
View raw message