harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r548575 - in /harmony/enhanced/classlib/trunk/modules/sql/src: main/java/javax/sql/rowset/serial/ test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/
Date Tue, 19 Jun 2007 04:07:04 GMT
Author: tonywu
Date: Mon Jun 18 21:07:03 2007
New Revision: 548575

URL: http://svn.apache.org/viewvc?view=rev&rev=548575
Log:
Apply patch HARMONY-4210 [classlib][sql] javax.sql.SerialArray(Array) throws NullPointerException
when one of the Array member is downcasted to Object)

Modified:
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/DefaultUDTMap.java
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialArray.java
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialArrayTest.java

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/DefaultUDTMap.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/DefaultUDTMap.java?view=diff&rev=548575&r1=548574&r2=548575
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/DefaultUDTMap.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/DefaultUDTMap.java
Mon Jun 18 21:07:03 2007
@@ -21,6 +21,7 @@
 import java.sql.Array;
 import java.sql.Blob;
 import java.sql.Clob;
+import java.sql.SQLException;
 import java.sql.Struct;
 import java.sql.Types;
 import java.util.HashMap;
@@ -34,45 +35,45 @@
                 || type == Types.DATALINK || type == Types.STRUCT || type == Types.JAVA_OBJECT);
     }
 
-    public static SerialDatalink[] processObject(URL[] elements)
+    public static SerialDatalink[] processDatalink(Object[] elements)
             throws SerialException {
         SerialDatalink[] ret = new SerialDatalink[elements.length];
         for (int i = 0; i < elements.length; i++) {
-            ret[i] = new SerialDatalink(elements[i]);
+            ret[i] = new SerialDatalink((URL)elements[i]);
         }
         return ret;
     }
 
-    public static Struct[] processObject(Struct[] elements)
+    public static Struct[] processStruct(Object[] elements)
             throws SerialException {
         Struct[] ret = new Struct[elements.length];
         for (int i = 0; i < elements.length; i++) {
-            ret[i] = elements[i];
+            ret[i] = (Struct)elements[i];
         }
         return ret;
     }
 
-    public static Array[] processObject(Array[] elements)
+    public static Array[] processArray(Object[] elements)
             throws SerialException {
         Array[] ret = new Array[elements.length];
         for (int i = 0; i < elements.length; i++) {
-            ret[i] = elements[i];
+            ret[i] = (Array)elements[i];
         }
         return ret;
     }
 
-    public static Clob[] processObject(Clob[] elements) throws SerialException {
+    public static Clob[] processClob(Object[] elements) throws SQLException {
         Clob[] ret = new Clob[elements.length];
         for (int i = 0; i < elements.length; i++) {
-            ret[i] = elements[i];
+            ret[i] = new SerialClob((Clob)elements[i]);
         }
         return ret;
     }
 
-    public static Blob[] processObject(Blob[] elements) throws SerialException {
+    public static Blob[] processBlob(Object[] elements) throws SQLException {
         Blob[] ret = new Blob[elements.length];
         for (int i = 0; i < elements.length; i++) {
-            ret[i] = elements[i];
+            ret[i] = new SerialBlob((Blob)elements[i]);
         }
         return ret;
     }
@@ -89,3 +90,4 @@
         return ret;
     }
 }
+

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialArray.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialArray.java?view=diff&rev=548575&r1=548574&r2=548575
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialArray.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialArray.java
Mon Jun 18 21:07:03 2007
@@ -18,13 +18,9 @@
 package javax.sql.rowset.serial;
 
 import java.io.Serializable;
-import java.net.URL;
 import java.sql.Array;
-import java.sql.Blob;
-import java.sql.Clob;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.sql.Struct;
 import java.sql.Types;
 import java.util.Map;
 
@@ -62,51 +58,74 @@
     public SerialArray(Array array, Map<String, Class<?>> map)
             throws SerialException, SQLException {
         if (null == array || null == array.getArray() || null == map) {
-            throw new SQLException(Messages.getString("sql.39"));
+            throw new SQLException(Messages.getString("sql.39")); //$NON-NLS-1$
         }
         baseType = array.getBaseType();
         baseTypeName = array.getBaseTypeName();
         Object[] element = (Object[]) array.getArray(map);
-
-        switch (baseType) {
-        case Types.STRUCT:
-            elements = DefaultUDTMap.processObject((Struct[])element);
-            break;
-        case Types.ARRAY:
-            elements = DefaultUDTMap.processObject((Array[])element);
-            break;
-        case Types.CLOB:
-            elements = DefaultUDTMap.processObject((Clob[])element);
-            break;
-        case Types.BLOB:
-            elements = DefaultUDTMap.processObject((Blob[])element);
-            break;
-        case Types.DATALINK:
-            elements = DefaultUDTMap.processObject((URL[])element);
-            break;
-        case Types.JAVA_OBJECT:
-            elements = DefaultUDTMap.processObject(element);
-            break;
-        default:
-            elements = new Object[element.length];
-            for (int i = 0; i < element.length; i++) {
-                elements[i] = element[i];
-            }
+        if (element.length == 0) {
+            elements = new Object[0];
+        } else {
+            transferElements(baseType, element);
         }
     }
+    
+    /**
+	 * Transfers primitive objects to SerialXXX objects according to the given
+	 * type.
+     * @throws SQLException 
+	 */
+    private void transferElements(int type, Object[] element) throws SQLException {
+    	switch (type) {
+		case Types.STRUCT:
+			elements = DefaultUDTMap.processStruct(element);
+			break;
+		case Types.ARRAY:
+			elements = DefaultUDTMap.processArray(element);
+			break;
+		case Types.CLOB:
+			elements = DefaultUDTMap.processClob(element);
+			break;
+		case Types.BLOB:
+			elements = DefaultUDTMap.processBlob(element);
+			break;
+		case Types.DATALINK:
+			elements = DefaultUDTMap.processDatalink(element);
+			break;
+		case Types.JAVA_OBJECT:
+			elements = DefaultUDTMap.processObject(element);
+			break;
+		default:
+			elements = new Object[element.length];
+			for (int i = 0; i < element.length; i++) {
+				elements[i] = element[i];
+			}
+		}
+    }
 
     /**
-     * The constructor
-     * 
-     * @param array
-     *            array to be serializated
-     * @throws SerialException
-     *             when any error occurs during serializing
-     * @throws SQLException
-     *             if array is null
-     */
+	 * The constructor
+	 * 
+	 * @param array
+	 *            array to be serializated
+	 * @throws SerialException
+	 *             when any error occurs during serializing
+	 * @throws SQLException
+	 *             if array is null
+	 */
     public SerialArray(Array array) throws SerialException, SQLException {
-        this(array, DefaultUDTMap.DEFAULTMAP);
+    	if (null == array || null == array.getArray()) {
+            throw new SQLException(Messages.getString("sql.39")); //$NON-NLS-1$
+        }
+        baseType = array.getBaseType();
+        baseTypeName = array.getBaseTypeName();
+        
+        Object[] element = (Object[]) array.getArray();
+        if (element.length == 0) {
+            elements = new Object[0];
+        } else {
+            transferElements(baseType, element);
+        }
     }
 
     /**
@@ -136,7 +155,7 @@
      */
     public Object getArray(long index, int count) throws SerialException {
         if (index < 0 || count + index > elements.length) {
-            throw new SerialException("Illegal Argument");
+            throw new SerialException(Messages.getString("sql.42")); //$NON-NLS-1$
         }
         Object[] ret = new Object[count];
         System.arraycopy(elements, (int) index, ret, 0, count);
@@ -160,7 +179,7 @@
     public Object getArray(long index, int count, Map<String, Class<?>> map)
             throws SerialException {
         if (index < 0 || count + index > elements.length ) {
-            throw new SerialException(Messages.getString("sql.40"));
+            throw new SerialException(Messages.getString("sql.40")); //$NON-NLS-1$
         }
         Object[] ret = new Object[count];
         System.arraycopy(elements, (int) index, ret, 0, count);
@@ -264,5 +283,4 @@
             throws SerialException {
         throw new UnsupportedOperationException();
     }
-
 }

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialArrayTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialArrayTest.java?view=diff&rev=548575&r1=548574&r2=548575
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialArrayTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialArrayTest.java
Mon Jun 18 21:07:03 2007
@@ -27,6 +27,7 @@
 import java.util.Map;
 
 import javax.sql.rowset.serial.SerialArray;
+import javax.sql.rowset.serial.SerialClob;
 import javax.sql.rowset.serial.SerialException;
 
 import junit.framework.TestCase;
@@ -60,6 +61,8 @@
     }
 
     public void testConstructor_ObjectArray() throws SQLException {
+        assertNotNull(new SerialArray(new SQLArray()));
+
         // OK
         sa = new SerialArray(mock);
         // array.getArray should not return null
@@ -382,6 +385,64 @@
 
         public void writeSQL(SQLOutput stream) throws SQLException {
             return;
+        }
+    }
+    
+    private static class SQLArray implements java.sql.Array {
+
+        Object[] array;
+
+        SQLArray() throws SQLException {
+
+            char[] chars = { 'a', 'b', 'c', 'd' };
+            array = new Object[1];
+
+            array[0] = (Object) new SerialClob(chars);
+        }
+
+        public Object getArray() {
+            return array;
+        }
+
+        public int getBaseType() {
+            return java.sql.Types.CLOB;
+        }
+
+        /** Everything below here is just supplied to satisfy
+         the interface and is not part of this testcase.
+         **/
+
+        public Object getArray(long index, int count) {
+            return null;
+        }
+
+        public Object getArray(long index, int count, Map<String, Class<?>> map)
{
+            return null;
+        }
+
+        public Object getArray(Map<String, Class<?>> map) {
+            return null;
+        }
+
+        public String getBaseTypeName() {
+            return null;
+        }
+
+        public ResultSet getResultSet() {
+            return null;
+        }
+
+        public ResultSet getResultSet(long index, int count) {
+            return null;
+        }
+
+        public ResultSet getResultSet(Map<String, Class<?>> map) {
+            return null;
+        }
+
+        public ResultSet getResultSet(long index, int count,
+                Map<String, Class<?>> map) {
+            return null;
         }
     }
 }



Mime
View raw message