harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r542480 - in /harmony/enhanced/classlib/trunk/modules/sql/src: main/java/javax/sql/rowset/serial/ main/java/org/apache/harmony/sql/internal/nls/ test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/
Date Tue, 29 May 2007 10:32:21 GMT
Author: tellison
Date: Tue May 29 03:32:17 2007
New Revision: 542480

URL: http://svn.apache.org/viewvc?view=rev&rev=542480
Log:
Apply patch HARMONY-3839 ([classlib][sql]SeralArray improvement)

Added:
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/DefaultUDTMap.java
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialArray.java
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialJavaObject.java
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialArrayTest.java
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialJavaObjectTest.java

Added: 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=auto&rev=542480
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/DefaultUDTMap.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/DefaultUDTMap.java
Tue May 29 03:32:17 2007
@@ -0,0 +1,91 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package javax.sql.rowset.serial;
+
+import java.net.URL;
+import java.sql.Array;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.sql.Struct;
+import java.sql.Types;
+import java.util.HashMap;
+
+public class DefaultUDTMap<T> {
+
+    public static HashMap<String, Class<?>> DEFAULTMAP = new HashMap<String,
Class<?>>();
+
+    public static boolean isDefault(int type) {
+        return (type == Types.ARRAY || type == Types.BLOB || type == Types.CLOB
+                || type == Types.DATALINK || type == Types.STRUCT || type == Types.JAVA_OBJECT);
+    }
+
+    public static SerialDatalink[] processObject(URL[] elements)
+            throws SerialException {
+        SerialDatalink[] ret = new SerialDatalink[elements.length];
+        for (int i = 0; i < elements.length; i++) {
+            ret[i] = new SerialDatalink(elements[i]);
+        }
+        return ret;
+    }
+
+    public static Struct[] processObject(Struct[] elements)
+            throws SerialException {
+        Struct[] ret = new Struct[elements.length];
+        for (int i = 0; i < elements.length; i++) {
+            ret[i] = elements[i];
+        }
+        return ret;
+    }
+
+    public static Array[] processObject(Array[] elements)
+            throws SerialException {
+        Array[] ret = new Array[elements.length];
+        for (int i = 0; i < elements.length; i++) {
+            ret[i] = elements[i];
+        }
+        return ret;
+    }
+
+    public static Clob[] processObject(Clob[] elements) throws SerialException {
+        Clob[] ret = new Clob[elements.length];
+        for (int i = 0; i < elements.length; i++) {
+            ret[i] = elements[i];
+        }
+        return ret;
+    }
+
+    public static Blob[] processObject(Blob[] elements) throws SerialException {
+        Blob[] ret = new Blob[elements.length];
+        for (int i = 0; i < elements.length; i++) {
+            ret[i] = elements[i];
+        }
+        return ret;
+    }
+
+    public static Object[] processObject(Object[] elements)
+            throws SerialException {
+        Object[] ret = new Object[elements.length];
+        for (int i = 0; i < elements.length; i++) {
+            ret[i] = new SerialJavaObject(elements[i]);
+            // TODO according to RI, should do like this, but does it make
+            // sense?
+            elements[i] = ret[i];
+        }
+        return ret;
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/DefaultUDTMap.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=542480&r1=542479&r2=542480
==============================================================================
--- 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
Tue May 29 03:32:17 2007
@@ -18,11 +18,18 @@
 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;
 
+import org.apache.harmony.sql.internal.nls.Messages;
+
 /**
  * 
  * A array that can be serialized
@@ -54,14 +61,38 @@
      */
     public SerialArray(Array array, Map<String, Class<?>> map)
             throws SerialException, SQLException {
-        // TODO add check for map
-        if (null == array || null == array.getArray()) {
-            throw new SQLException(
-                    "Cannot instantiate a SerialArray object with a null Array object.");
+        if (null == array || null == array.getArray() || null == map) {
+            throw new SQLException(Messages.getString("sql.39"));
         }
         baseType = array.getBaseType();
         baseTypeName = array.getBaseTypeName();
-        elements = (Object[]) array.getArray();
+        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];
+            }
+        }
     }
 
     /**
@@ -75,13 +106,7 @@
      *             if array is null
      */
     public SerialArray(Array array) throws SerialException, SQLException {
-        if (null == array || null == array.getArray()) {
-            throw new SQLException(
-                    "Cannot instantiate a SerialArray object with a null Array object.");
-        }
-        baseType = array.getBaseType();
-        baseTypeName = array.getBaseTypeName();
-        elements = (Object[]) array.getArray();
+        this(array, DefaultUDTMap.DEFAULTMAP);
     }
 
     /**
@@ -110,11 +135,10 @@
      *             if any error occurs when copy the array
      */
     public Object getArray(long index, int count) throws SerialException {
-        if (index < 0 || count >= index + elements.length) {
+        if (index < 0 || count + index > elements.length) {
             throw new SerialException("Illegal Argument");
         }
         Object[] ret = new Object[count];
-        // TODO check if casting long to int is legal
         System.arraycopy(elements, (int) index, ret, 0, count);
         return ret;
     }
@@ -135,12 +159,10 @@
      */
     public Object getArray(long index, int count, Map<String, Class<?>> map)
             throws SerialException {
-        // TODO replace raw string to Msg.getString 
-        if (index < 0 || count >= index + elements.length) {
-            throw new SerialException("Illegal Argument");
+        if (index < 0 || count + index > elements.length ) {
+            throw new SerialException(Messages.getString("sql.40"));
         }
         Object[] ret = new Object[count];
-        // TODO check if casting long to int is legal
         System.arraycopy(elements, (int) index, ret, 0, count);
         return ret;
     }
@@ -155,10 +177,8 @@
      *             if any error occurs when copy the array
      */
     public Object getArray(Map<String, Class<?>> map) throws SerialException
{
-        // TODO add check for map
-        Object[] ret = new Object[elements.length];
-        System.arraycopy(elements, 0, ret, 0, elements.length);
-        return elements;
+        return getArray(0, elements.length,
+                null == map ? DefaultUDTMap.DEFAULTMAP : map);
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialJavaObject.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialJavaObject.java?view=diff&rev=542480&r1=542479&r2=542480
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialJavaObject.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialJavaObject.java
Tue May 29 03:32:17 2007
@@ -21,6 +21,8 @@
 import java.lang.reflect.Field;
 import java.util.Vector;
 
+import org.apache.harmony.sql.internal.nls.Messages;
+
 public class SerialJavaObject implements Serializable, Cloneable {
 
 	private static final long serialVersionUID = -1465795139032831023L;
@@ -36,6 +38,12 @@
 	private transient Field[] fields;
 
 	public SerialJavaObject(Object obj) throws SerialException {
+        if (null == obj) {
+            throw new NullPointerException();
+        }
+        if (!(obj instanceof Serializable)) {
+            throw new SerialException(Messages.getString("sql.41"));
+        }
         this.obj = obj;
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties?view=diff&rev=542480&r1=542479&r2=542480
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties
Tue May 29 03:32:17 2007
@@ -47,9 +47,15 @@
 sql.30=Invalid column display size. Cannot be less than zero
 sql.31=Invalid precision value. Cannot be less than zero
 sql.32=Invalid scale size. Cannot be less than zero
-sql.40=ClassNotFoundException: {0}
-sql.41=Invalid JNDI context supplied
 sql.33=Cannot instantiate a SQLOutputImpl instance with null parameters
 sql.34=Cannot instantiate a SQLInputImpl instance with null parameters
 sql.35=SQLInputImpl exception: Invalid read position
+sql.36=No more attributes
 sql.37=Operation not supported
+sql.38=Object is invalid
+sql.39=Cannot instantiate a SerialArray object with a null Array object
+sql.40=ClassNotFoundException: {0}
+sql.41=Invalid JNDI context supplied
+sql.42=Illegal Argument
+sql.43=The object is not serializable
+sql.44=No logger has been set

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=542480&r1=542479&r2=542480
==============================================================================
--- 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
Tue May 29 03:32:17 2007
@@ -33,25 +33,65 @@
 
 public class SerialArrayTest extends TestCase {
 
-    private Array mock = new MockArray();
+    private MockArray mock = new MockArray();
 
     private SerialArray sa;
 
-    String[] strs = new String[2];
+    Object[] testElements = new Object[4];
 
     Map<String, Class<?>> map = new HashMap<String, Class<?>>();
+    
+    Map<String, Class<?>> badmap = new HashMap<String, Class<?>>();
 
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-        sa = new SerialArray(mock);
-        strs[0] = "test1";
-        strs[1] = "test2";
+        testElements = new Object[4];
+        testElements[0] = "test1";
+        testElements[1] = "test2";
+        testElements[2] = new SQLException();
+        testElements[3] = new HashMap();       
+        sa = new SerialArray(mock);     
+        map = new HashMap<String, Class<?>>();
         map.put("String", MockStringSQLData.class);
-        map.put("Object", MockObjectSQLData.class);
+        map.put("Object", null);
+        badmap = new HashMap<String, Class<?>>();
+        badmap.put("Something", HashMap.class);
     }
 
-    public void testConstructor() throws SQLException {
+    public void testConstructor_ObjectArray() throws SQLException {
+        // OK
+        sa = new SerialArray(mock);
+        // array.getArray should not return null
+        try {
+            sa = new SerialArray(new MockNullArray());
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }        
+        testElements = new Object[5];
+        testElements[0] = "test1";
+        testElements[1] = "test2";
+        testElements[2] = new SQLException();
+        testElements[3] = new HashMap();  
+        try {
+            sa = new SerialArray(mock);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        testElements[4] = new Object();
+        try {
+            sa = new SerialArray(mock);
+            // RI fail here
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+    }
+    
+    public void testConstructor_IntArray() throws SQLException {
+        mock.basetype = Types.INTEGER;
         // OK
         sa = new SerialArray(mock);
         // array.getArray should not return null
@@ -61,11 +101,22 @@
         } catch (SQLException e) {
             // expected
         }
+        testElements = new Object[5];
+        testElements[0] = "test1";
+        testElements[1] = "test2";
+        testElements[2] = new SQLException();
+        testElements[3] = new HashMap();
+        // OK for integer
+        sa = new SerialArray(mock);
+
+        testElements[4] = new Object();
+        sa = new SerialArray(mock);
     }
 
     public void testConstructor_map() throws SQLException {
         try {
             sa = new SerialArray(mock, null);
+            fail("should throw SQLException");
         } catch (SQLException e) {
             // expected
         }
@@ -75,33 +126,70 @@
             fail("should throw SQLException");
         } catch (SQLException e) {
             // expected
-        }        
+        }     
+        //OK        
+        sa = new SerialArray(mock,map);
+        sa = new SerialArray(mock,badmap);
     }
 
     public void testGetArray() throws SerialException {
-        for (int i = 0; i < strs.length; i++) {
-            assertEquals(strs[i], ((Object[]) sa.getArray())[i]);
+        for (int i = 0; i < testElements.length; i++) {
+            assertEquals(testElements[i], ((Object[]) sa.getArray())[i]);
         }
     }
 
+    public void testGetArrayMap() throws SerialException {
+        for (int i = 0; i < testElements.length; i++) {
+            assertSame(testElements[i], ((Object[]) sa.getArray(null))[i]);
+        }
+        for (int i = 0; i < testElements.length; i++) {
+            assertSame(testElements[i], ((Object[]) sa.getArray(map))[i]);
+        }
+        for (int i = 0; i < testElements.length; i++) {
+            assertSame(testElements[i], ((Object[]) sa.getArray(badmap))[i]);
+        }
+        
+    }
+    
     public void testGetArrayLongInt() throws SerialException {
-        for (int i = 0; i < strs.length; i++) {
-            assertEquals(strs[i], ((Object[]) sa.getArray(i, 1))[0]);
+        for (int i = 0; i < testElements.length; i++) {
+            assertEquals(testElements[i], ((Object[]) sa.getArray(i, 1))[0]);
         }
     }
 
-    public void testGetArrayLongIntMapOfStringClassOfQ() {
-
+    public void testGetArrayLongIntMap() throws SerialException {
+        for (int i = 0; i < testElements.length; i++) {
+            assertSame(testElements[i], ((Object[]) sa.getArray(i, 1, null))[0]);
+        }
+        for (int i = 0; i < testElements.length; i++) {
+            assertSame(testElements[i], ((Object[]) sa.getArray(i, 1, map))[0]);
+        }
+        for (int i = 0; i < testElements.length; i++) {
+            assertSame(testElements[i], ((Object[]) sa.getArray(i, 1, badmap))[0]);
+        }
+        mock.returnNull = true;
+        // elements force deeper copy
+        for (int i = 0; i < testElements.length; i++) {
+            assertSame(testElements[i], ((Object[]) sa.getArray(i, 1, map))[0]);
+        }
     }
 
     public void testGetArrayMapOfStringClassOfQ() throws SerialException {
-        for (int i = 0; i < strs.length; i++) {
-            assertEquals(strs[i], ((Object[]) sa.getArray())[i]);
+        for (int i = 0; i < testElements.length; i++) {
+            assertSame(testElements[i], ((Object[]) sa.getArray(badmap))[i]);
+        }
+        for (int i = 0; i < testElements.length; i++) {
+            assertSame(testElements[i], ((Object[]) sa.getArray(map))[i]);
+        }
+        mock.returnNull = true;
+        // elements force deeper copy
+        for (int i = 0; i < testElements.length; i++) {
+            assertSame(testElements[i], ((Object[]) sa.getArray(map))[i]);
         }
     }
 
     public void testGetBaseType() throws SerialException {
-        assertEquals(Types.ARRAY, sa.getBaseType());
+        assertEquals(Types.JAVA_OBJECT, sa.getBaseType());
     }
 
     public void testGetBaseTypeName() throws SQLException {
@@ -146,34 +234,46 @@
     }
 
     class MockArray implements Array {
+        
+        public boolean returnNull = false;
+        
+        public int basetype = Types.JAVA_OBJECT;
 
         public Object getArray() throws SQLException {
-            return strs;
+            return testElements;
         }
 
         public Object getArray(long index, int count) throws SQLException {
-            String[] ret = new String[count];
+            Object[] ret = new Object[count];
             for (int i = 0; i < ret.length; i++) {
-                ret[i] = strs[(int) index + i];
+                ret[i] = testElements[(int) index + i];
             }
             return ret;
         }
 
         public Object getArray(long index, int count, Map<String, Class<?>> map)
                 throws SQLException {
-            String[] ret = new String[count];
-            for (int i = 0; i < ret.length; i++) {
-                ret[i] = strs[(int) index + i];
+            if (!returnNull) {
+                Object[] ret = new Object[count];
+                for (int i = 0; i < ret.length; i++) {
+                    ret[i] = testElements[(int) index + i];
+                }
+                return ret;
+            } else {
+                return null;
             }
-            return ret;
         }
 
         public Object getArray(Map<String, Class<?>> map) throws SQLException
{
-            return strs;
+            if (!returnNull){
+                return testElements;
+            } else {
+                return null;
+            }
         }
 
         public int getBaseType() throws SQLException {
-            return Types.ARRAY;
+            return basetype;
         }
 
         public String getBaseTypeName() throws SQLException {
@@ -217,7 +317,7 @@
         }
 
         public Object getArray(Map<String, Class<?>> map) throws SQLException
{
-            return strs;
+            return testElements;
         }
 
         public int getBaseType() throws SQLException {

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialJavaObjectTest.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/SerialJavaObjectTest.java?view=diff&rev=542480&r1=542479&r2=542480
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialJavaObjectTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialJavaObjectTest.java
Tue May 29 03:32:17 2007
@@ -20,6 +20,7 @@
 import java.io.Serializable;
 import java.util.Arrays;
 
+import javax.sql.rowset.serial.SerialException;
 import javax.sql.rowset.serial.SerialJavaObject;
 
 import junit.framework.TestCase;
@@ -27,24 +28,32 @@
 public class SerialJavaObjectTest extends TestCase {
 	public void test_Constructor() throws Exception {
 		TransientFieldClass tfc = new TransientFieldClass();
-		SerialJavaObject sjo = new SerialJavaObject(tfc);
-		assertSame(tfc, sjo.getObject());
-		Arrays.equals(sjo.getFields(), tfc.getClass().getFields());
-		
-		NonSerialiableClass nsc = new NonSerialiableClass();
-		sjo = new SerialJavaObject(nsc);
-		assertSame(nsc, sjo.getObject());
-		Arrays.equals(sjo.getFields(), nsc.getClass().getFields());
-		
-		StaticFieldClass sfc = new StaticFieldClass();
-		sjo = new SerialJavaObject(sfc);
-		assertSame(sfc, sjo.getObject());
-		Arrays.equals(sjo.getFields(), sfc.getClass().getFields());
+        SerialJavaObject sjo;
+        try {
+            sjo = new SerialJavaObject(tfc);
+            fail("should throw SerialException");
+        } catch (SerialException e) {
+            // excepted
+        }
+        try {
+            NonSerialiableClass nsc = new NonSerialiableClass();
+            sjo = new SerialJavaObject(nsc);
+            fail("should throw SerialException");
+        } catch (SerialException e) {
+            // excepted
+        }		
 		
 		SerializableClass sc = new SerializableClass();
 		sjo = new SerialJavaObject(sc);
 		assertSame(sc, sjo.getObject());
 		Arrays.equals(sjo.getFields(), sc.getClass().getFields());
+        
+        try {
+            new SerialJavaObject(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
 	}
 	
 	static class TransientFieldClass {



Mime
View raw message