harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r596627 - in /harmony/enhanced/classlib/trunk/modules/sql/src: main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java
Date Tue, 20 Nov 2007 11:25:06 GMT
Author: tellison
Date: Tue Nov 20 03:25:05 2007
New Revision: 596627

URL: http://svn.apache.org/viewvc?rev=596627&view=rev
Log:
Apply patch HARMONY-5155 ([classlib][sql] implement CachedRowSet.createCopySchema and createShared
methods)

Modified:
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java?rev=596627&r1=596626&r2=596627&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java
Tue Nov 20 03:25:05 2007
@@ -84,12 +84,14 @@
 
     private CachedRow insertRow;
 
+    // TODO remember to evalute it in CachedRowSetReader
     private int[] keyCols;
 
     private int columnCount;
 
     private SyncProvider syncProvider;
 
+    // TODO where is it initialized
     private CachedRowSetImpl originalResultSet;
 
     private Object currentColumn;
@@ -262,28 +264,43 @@
         // For webRowSet: represent the table structure: Columns
         CachedRowSetImpl result;
         try {
+            // copy data from BaseRowSet
             result = (CachedRowSetImpl) super.clone();
-            result.meta = (RowSetMetaDataImpl) meta;
+            // deep copy meta data
+            result.meta = copyMetaData(meta);
+            result.columnCount = columnCount;
             result.keyCols = keyCols == null ? null : keyCols.clone();
+            result.originalResultSet = new CachedRowSetImpl(syncProvider
+                    .getProviderID());
+            result.pageSize = pageSize;
+            result.setSyncProvider(syncProvider.getProviderID());
+            result.setTableName(getTableName());
+
+            // clean up rows data
+            result.currentColumn = null;
+            result.currentRowIndex = 0;
+            result.insertRow = null;
+            result.originalRow = null;
+            result.pageNumber = 1;
+            result.rememberedCursorPosition = 0;
+            result.rows = new ArrayList<CachedRow>();
+            result.sqlwarn = null;
             return result;
-        } catch (CloneNotSupportedException e1) {
-            // FIXME deal with the exception
-            return null;
+        } catch (CloneNotSupportedException e) {
+            // TODO add error message
+            throw new SQLException();
         }
     }
 
     public RowSet createShared() throws SQLException {
         // shallow copy
-        CachedRowSetImpl result = new CachedRowSetImpl();
-        result.rows = rows;
-
-        result.currentRowIndex = currentRowIndex;
-        result.currentRow = currentRow;
-        result.meta = meta;
-
-        result.setReadOnly(isReadOnly());
-        result.setConcurrency(super.getConcurrency());
-        result.setType(super.getType());
+        RowSet result = null;
+        try {
+            result = (RowSet) super.clone();
+        } catch (CloneNotSupportedException e) {
+            // TODO add error message
+            throw new SQLException();
+        }
 
         return result;
     }
@@ -307,7 +324,7 @@
     }
 
     public int[] getKeyColumns() throws SQLException {
-        return keyCols.clone();
+        return keyCols == null ? null : keyCols.clone();
     }
 
     public ResultSet getOriginal() throws SQLException {
@@ -358,7 +375,7 @@
 
     public void populate(ResultSet rs, int startRow) throws SQLException {
         initParams();
-        composeMetaData(rs.getMetaData());
+        meta = copyMetaData(rs.getMetaData());
 
         new CachedRowSetReader(rs, startRow).readData(this);
 
@@ -374,31 +391,31 @@
 
     }
 
-    private void composeMetaData(ResultSetMetaData metaData)
+    // deep copy of ResultSetMetaData
+    private RowSetMetaData copyMetaData(ResultSetMetaData metaData)
             throws SQLException {
         RowSetMetaDataImpl rowSetMetaData = new RowSetMetaDataImpl();
         rowSetMetaData.setColumnCount(metaData.getColumnCount());
-        for (int i = 1; i <= metaData.getColumnCount(); i++) {
-            rowSetMetaData.setAutoIncrement(i, metaData.isAutoIncrement(i));
-            rowSetMetaData.setCaseSensitive(i, metaData.isCaseSensitive(i));
-            rowSetMetaData.setCatalogName(i, metaData.getCatalogName(i));
-            rowSetMetaData.setColumnDisplaySize(i, metaData
-                    .getColumnDisplaySize(i));
-            rowSetMetaData.setColumnLabel(i, metaData.getColumnLabel(i));
-            rowSetMetaData.setColumnName(i, metaData.getColumnName(i));
-            rowSetMetaData.setColumnType(i, metaData.getColumnType(i));
-            rowSetMetaData.setColumnTypeName(i, metaData.getColumnTypeName(i));
-            rowSetMetaData.setCurrency(i, metaData.isCurrency(i));
-            rowSetMetaData.setNullable(i, metaData.isNullable(i));
-            rowSetMetaData.setPrecision(i, metaData.getPrecision(i));
-            rowSetMetaData.setScale(i, metaData.getScale(i));
-            rowSetMetaData.setSchemaName(i, metaData.getSchemaName(i));
-            rowSetMetaData.setSearchable(i, metaData.isSearchable(i));
-            rowSetMetaData.setSigned(i, metaData.isSigned(i));
-            rowSetMetaData.setTableName(i, metaData.getTableName(i));
-
+        for (int columnIndex = 1; columnIndex <= metaData.getColumnCount(); columnIndex++)
{
+            rowSetMetaData.setAutoIncrement(columnIndex, metaData.isAutoIncrement(columnIndex));
+            rowSetMetaData.setCaseSensitive(columnIndex, metaData.isCaseSensitive(columnIndex));
+            rowSetMetaData.setCatalogName(columnIndex, metaData.getCatalogName(columnIndex));
+            rowSetMetaData.setColumnDisplaySize(columnIndex, metaData
+                    .getColumnDisplaySize(columnIndex));
+            rowSetMetaData.setColumnLabel(columnIndex, metaData.getColumnLabel(columnIndex));
+            rowSetMetaData.setColumnName(columnIndex, metaData.getColumnName(columnIndex));
+            rowSetMetaData.setColumnType(columnIndex, metaData.getColumnType(columnIndex));
+            rowSetMetaData.setColumnTypeName(columnIndex, metaData.getColumnTypeName(columnIndex));
+            rowSetMetaData.setCurrency(columnIndex, metaData.isCurrency(columnIndex));
+            rowSetMetaData.setNullable(columnIndex, metaData.isNullable(columnIndex));
+            rowSetMetaData.setPrecision(columnIndex, metaData.getPrecision(columnIndex));
+            rowSetMetaData.setScale(columnIndex, metaData.getScale(columnIndex));
+            rowSetMetaData.setSchemaName(columnIndex, metaData.getSchemaName(columnIndex));
+            rowSetMetaData.setSearchable(columnIndex, metaData.isSearchable(columnIndex));
+            rowSetMetaData.setSigned(columnIndex, metaData.isSigned(columnIndex));
+            rowSetMetaData.setTableName(columnIndex, metaData.getTableName(columnIndex));
         }
-        meta = rowSetMetaData;
+        return rowSetMetaData;
     }
 
     public boolean previousPage() throws SQLException {

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java?rev=596627&r1=596626&r2=596627&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java
Tue Nov 20 03:25:05 2007
@@ -27,6 +27,8 @@
 import java.sql.Time;
 import java.sql.Timestamp;
 
+import javax.sql.RowSetEvent;
+import javax.sql.RowSetListener;
 import javax.sql.RowSetMetaData;
 import javax.sql.rowset.CachedRowSet;
 
@@ -322,12 +324,45 @@
     }
 
     public void testCreateShared() throws Exception {
+        crset.setUsername("testUsername");
+        crset.setPassword("testPassword");
+        crset.setPageSize(5);
+        Listener listener = new Listener(); // a class implements RowSetListener
+        crset.addRowSetListener(listener);
+        crset.absolute(3); // move to the third row for testing
+        // TODO: when the cursor moved, notifyCursorMoved() should be called
+        // assertEquals("cursorMoved", listener.getTag());
+
         CachedRowSet crsetShared = (CachedRowSet) crset.createShared();
-        crsetShared.first();
-        crset.first();
-        crsetShared.updateString(2, "copyTest2");
-        assertEquals(crsetShared.getString(2), "copyTest2");
-        assertEquals(crset.getString(2), "copyTest2");
+        assertEquals("testUsername", crsetShared.getUsername());
+        assertEquals("testPassword", crsetShared.getPassword());
+        assertEquals(5, crsetShared.getPageSize());
+        // check whether modify the attribute of the original is visible to the
+        // duplicate
+        crset.setUsername("modifyUsername");
+        crset.setPageSize(10);
+        assertEquals("modifyUsername", crset.getUsername());
+        assertEquals("testUsername", crsetShared.getUsername());
+        assertEquals(10, crset.getPageSize());
+        assertEquals(5, crsetShared.getPageSize());
+
+        // compare the current row, that is the third row
+        assertEquals(3, crset.getInt(1));
+        assertEquals("test3", crset.getString(2));
+        assertEquals(3, crsetShared.getInt(1));
+        assertEquals("test3", crsetShared.getString(2));
+        // check whether update the duplicate is visible to the original
+        crsetShared.updateString(2, "modify3");
+        assertEquals("modify3", crsetShared.getString(2));
+        assertEquals("modify3", crset.getString(2));
+        crsetShared.updateRow();
+        crsetShared.acceptChanges();
+        assertEquals("rowSetChanged", listener.getTag());
+
+        // when move the duplicate's cursor, the original shouldn't be affected
+        crsetShared.absolute(1);
+        assertEquals(1, crsetShared.getInt(1));
+        assertEquals(3, crset.getInt(1));
     }
 
     public void testcreateCopyNoConstraints() throws Exception {
@@ -355,20 +390,73 @@
     }
 
     public void testCopySchema() throws Exception {
-        crset.first();
-        CachedRowSet crsetCopySchema = (CachedRowSet) crset.createCopySchema();
+        // the original's addtribute and meta data
+        crset.setCommand("testCommand");
+        crset.setConcurrency(ResultSet.CONCUR_UPDATABLE);
+        crset.setDataSourceName("testDataSource");
+        crset.setFetchDirection(ResultSet.FETCH_UNKNOWN);
+        crset.setPageSize(20);
+        crset.setMaxRows(20);
+        crset.setTableName("USER_INFO");
+        /*
+         * NOTICE: spec say copy must not has any content, but when run on RI,
+         * if call next() before call createCopySchema(), the copy can get the
+         * current row's data
+         */
 
-        RowSetMetaData rsmCopySchema = (RowSetMetaData) crsetCopySchema
+        /*
+         * NOTICE: when run on RI, if add the listener first, then it will go
+         * wrong when call createCopySchema().It's said that clone failed.
+         */
+        // Listener listener = new Listener();
+        // crset.addRowSetListener(listener);
+        RowSetMetaData rsmd = (RowSetMetaData) crset.getMetaData();
+        // the copy
+        CachedRowSet crsetCopySchema = crset.createCopySchema();
+        RowSetMetaData rsmdCopySchema = (RowSetMetaData) crsetCopySchema
                 .getMetaData();
-        assertEquals("USER_INFO", rsmCopySchema.getTableName(1));
-        assertEquals(DEFAULT_COLUMN_COUNT, rsmCopySchema.getColumnCount());
-
-        RowSetMetaData rsm = (RowSetMetaData) crset.getMetaData();
-        rsm.setTableName(1, "newBorn");
-        assertEquals("newBorn", rsm.getTableName(1));
 
-        crset.setTableName("test");
-        assertEquals("test", crset.getTableName());
+        // compare the meta data between the duplicate and the original
+        assertNotSame(crset.getMetaData(), crsetCopySchema.getMetaData());
+        assertNotSame(crset.getOriginal(), crsetCopySchema.getOriginal());
+        // assertNotSame(crset.getSyncProvider(), crsetCopySchema
+        // .getSyncProvider());
+
+        assertEquals("USER_INFO", crset.getTableName());
+        assertEquals("USER_INFO", rsmdCopySchema.getTableName(1));
+        assertEquals(DEFAULT_COLUMN_COUNT, rsmdCopySchema.getColumnCount());
+        assertEquals(rsmd.getColumnName(1), rsmdCopySchema.getColumnName(1));
+        // check the primary key
+        // TODO: RI doesn't evalute the keyColumns. The value of
+        // crset.getKeyColumns() is null.
+        assertEquals(crset.getKeyColumns(), crsetCopySchema.getKeyColumns());
+
+        // check the attributes in the duplicate. These are supposed to be the
+        // same as the original
+        // System.out.println("crsetCopySchema: " + crsetCopySchema.getInt(1));
+        assertFalse(crsetCopySchema.next());
+        assertEquals("testCommand", crsetCopySchema.getCommand());
+        assertEquals(ResultSet.CONCUR_UPDATABLE, crsetCopySchema
+                .getConcurrency());
+        assertEquals("testDataSource", crsetCopySchema.getDataSourceName());
+        assertEquals(ResultSet.FETCH_UNKNOWN, crsetCopySchema
+                .getFetchDirection());
+        assertEquals(20, crsetCopySchema.getPageSize());
+        assertEquals(20, crsetCopySchema.getMaxRows());
+
+        // fill the duplicate CachedRowSet with data, check the listener
+        Listener listener = new Listener();
+        crsetCopySchema.addRowSetListener(listener);
+        assertNull(listener.getTag());
+        rs = st.executeQuery("select * from USER_INFO");
+        crsetCopySchema.populate(rs);
+        // TODO: in the Harmony implementation, need to call notifyRowSetChanged
+        // at the suitable place
+        // assertEquals("rowSetChanged", listener.getTag());
+        listener.setTag(null);
+        // the move of the original's cursor shouldn't affect the duplicate
+        crset.next();
+        assertNull(listener.getTag());
     }
 
     public void testCreateCopy() throws Exception {
@@ -524,6 +612,37 @@
 
         if (preStmt != null) {
             preStmt.close();
+        }
+    }
+
+    public class Listener implements RowSetListener, Cloneable {
+
+        private String tag = null;
+
+        public void cursorMoved(RowSetEvent theEvent) {
+            tag = "cursorMoved";
+        }
+
+        public void rowChanged(RowSetEvent theEvent) {
+            tag = "rowChanged";
+        }
+
+        public void rowSetChanged(RowSetEvent theEvent) {
+            tag = "rowSetChanged";
+        }
+
+        public String getTag() {
+            return tag;
+        }
+
+        public void setTag(String tag) {
+            this.tag = tag;
+        }
+
+        public Listener clone() throws CloneNotSupportedException {
+            Listener listener = (Listener) super.clone();
+            listener.tag = tag;
+            return listener;
         }
     }
 }



Mime
View raw message