harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r633787 - in /harmony/enhanced/classlib/trunk/modules/sql/src: main/java/org/apache/harmony/sql/internal/rowset/ test/java/org/apache/harmony/sql/tests/internal/rowset/
Date Wed, 05 Mar 2008 08:31:04 GMT
Author: tonywu
Date: Wed Mar  5 00:31:03 2008
New Revision: 633787

URL: http://svn.apache.org/viewvc?rev=633787&view=rev
Log:
Apply patch for HARMONY-5574 ([classlib][sql][rowset]implements deleteRow, undoDelete and restoreOriginal methods of CachedRowSet)

Added:
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetDeleteTest.java   (with props)
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
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.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=633787&r1=633786&r2=633787&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 Wed Mar  5 00:31:03 2008
@@ -43,6 +43,7 @@
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.List;
 import java.util.Map;
 import java.util.Vector;
 
@@ -74,7 +75,9 @@
 
     private CachedRow currentRow;
 
-    // start from : 1.
+    /**
+     * current row index include deleted rows, start from 1
+     */
     private int currentRowIndex;
 
     // the number of the rows in one "page"
@@ -93,6 +96,8 @@
 
     private int columnCount;
 
+    private int deletedRowCount;
+
     private SyncProvider syncProvider;
 
     // TODO where is it initialized
@@ -197,8 +202,22 @@
                     .getRowSetWriter();
             rowSetWriter.setConnection(con);
             int beforeWriteIndex = currentRowIndex;
+            boolean isShowDeleted = getShowDeleted();
+            setShowDeleted(true);
+
             rowSetWriter.writeData(this);
+
             absolute(beforeWriteIndex);
+            setShowDeleted(isShowDeleted);
+
+            int index = getRow();
+            if (index == 0) {
+                next();
+                index = getRow();
+                if (index == 0) {
+                    index = rows.size() + 1;
+                }
+            }
 
             boolean isChanged = false;
             /*
@@ -230,12 +249,12 @@
                 }
             }
 
-            if (currentRowIndex > rows.size()) {
+            if (index > rows.size()) {
                 afterLast();
-            } else if (currentRowIndex <= 0) {
+            } else if (index <= 0) {
                 beforeFirst();
             } else {
-                absolute(currentRowIndex);
+                absolute(index);
             }
 
             notifyRowSetChanged();
@@ -263,6 +282,10 @@
     }
 
     private int getIndexByName(String columnName) throws SQLException {
+        if (meta == null || columnName == null) {
+            throw new NullPointerException();
+        }
+
         for (int i = 1; i <= meta.getColumnCount(); i++) {
             if (columnName.equalsIgnoreCase(meta.getColumnName(i))) {
                 return i;
@@ -573,7 +596,24 @@
     }
 
     public void restoreOriginal() throws SQLException {
-        throw new NotImplementedException();
+        if (rows == null) {
+            return;
+        }
+
+        List<CachedRow> insertedRows = new ArrayList<CachedRow>();
+        for (CachedRow row : rows) {
+            if (row.isInsert()) {
+                insertedRows.add(row);
+            } else if (row.isDelete() || row.isUpdate()) {
+                row.restoreOriginal();
+            }
+        }
+        rows.removeAll(insertedRows);
+        insertRow = null;
+        
+        first();
+
+        // TODO fire rowSetChanged event
     }
 
     public void rollback() throws SQLException {
@@ -605,6 +645,7 @@
 
         if (rowDeleted()) {
             rows.remove(currentRow);
+            deletedRowCount--;
         } else if (rowUpdated() || rowInserted()) {
             currentRow.setOriginal();
         }
@@ -654,12 +695,19 @@
     }
 
     public void undoDelete() throws SQLException {
-        if (currentRow == null) {
+        if (isAfterLast() || isBeforeFirst()) {
             // TODO add error messages
             throw new SQLException();
         }
-        if (currentRow.isDelete()) {
+
+        if (currentRow != null && !currentRow.isDelete()) {
+            // TODO add error messages
+            throw new SQLException();
+        }
+
+        if (currentRow != null && currentRow.isDelete()) {
             currentRow.undoDelete();
+            deletedRowCount--;
         }
     }
 
@@ -721,21 +769,20 @@
     }
 
     public boolean absolute(int row) throws SQLException {
-        return doAbsolute(row, true);
+        return doAbsolute(getIndexIncludeDeletedRows(row), true);
     }
 
     /**
      * internal implement of absolute
      * 
      * @param row
-     *            index of row cursor to move
+     *            index of row cursor to move, include deleted rows
      * @param checkType
      *            whether to check property ResultSet.TYPE_FORWARD_ONLY
      * @return whether the cursor is on result set
      * @throws SQLException
      */
     private boolean doAbsolute(int row, boolean checkType) throws SQLException {
-        // FIXME need to consider getShowDeleted()
         if (rows == null || rows.size() == 0) {
             return false;
         }
@@ -812,21 +859,93 @@
             throw new SQLException();
         }
         currentRow.setDelete();
+        deletedRowCount++;
     }
 
     private void checkValidRow() throws SQLException {
         if (currentRow == null) {
-            // rowset.0 = Not a valid position
-            throw new SQLException(Messages.getString("rowset.0"));
+            // rowset.7=Not a valid cursor
+            throw new SQLException(Messages.getString("rowset.7")); //$NON-NLS-1$
+        }
+    }
+
+    /**
+     * convert <code>index</code> consider property <code>showDeleted</code>.
+     * If <code>showDeleted</code> is true, do nothing, otherwise, re-compute
+     * <code>index</code> add deleted rows.
+     * 
+     * @param index
+     *            maybe negative, indicate the row number counting from the end
+     *            of the result set
+     * @return row index include delted rows
+     */
+    private int getIndexIncludeDeletedRows(int index) throws SQLException {
+        if (getShowDeleted()) {
+            return index;
+        }
+
+        if (index == 0) {
+            return 0;
+        }
+
+        if (index > 0) {
+            int indexIncludeDeletedRows = 0;
+            for (; index > 0; ++indexIncludeDeletedRows) {
+                if (indexIncludeDeletedRows == rows.size()) {
+                    indexIncludeDeletedRows += index;
+                    break;
+                }
+
+                if (!rows.get(indexIncludeDeletedRows).isDelete()) {
+                    index--;
+                }
+            }
+            return indexIncludeDeletedRows;
+        }
+
+        // index < 0
+        int indexIncludeDeletedRows = rows.size();
+        for (; index < 0; --indexIncludeDeletedRows) {
+            if (indexIncludeDeletedRows == 0) {
+                break;
+            }
+
+            if (!rows.get(indexIncludeDeletedRows - 1).isDelete()) {
+                index++;
+            }
         }
+        if (indexIncludeDeletedRows != 0) {
+            indexIncludeDeletedRows++;
+        }
+
+        return indexIncludeDeletedRows;
+    }
+
+    /**
+     * If <code>showDeleted</code> property is true, return the rows size
+     * include deleted rows. Otherwise not include deleted rows.
+     * 
+     * @return
+     * @throws SQLException
+     */
+    private int getValidRowSize() throws SQLException {
+        if (rows == null) {
+            return 0;
+        }
+
+        if (getShowDeleted()) {
+            return rows.size();
+        }
+
+        return rows.size() - deletedRowCount;
     }
 
     public int findColumn(String columnName) throws SQLException {
-        throw new NotImplementedException();
+        return getIndexByName(columnName);
     }
 
     public boolean first() throws SQLException {
-        return doAbsolute(1, true);
+        return doAbsolute(getIndexIncludeDeletedRows(1), true);
     }
 
     public Array getArray(int columnIndex) throws SQLException {
@@ -1030,11 +1149,27 @@
 
     public int getRow() throws SQLException {
         // FIXME need more tests
-        if (currentRow == null) {
+        if (currentRow == null || rows == null) {
             return 0;
         }
 
-        return currentRowIndex;
+        if (!getShowDeleted() && currentRow.isDelete()) {
+            return 0;
+        }
+
+        if (getShowDeleted() || currentRowIndex == 0) {
+            return currentRowIndex;
+        }
+
+        // doesn't show deleted rows, skip them
+        int index = 0;
+        for (int i = 0; i < currentRowIndex; ++i) {
+            if (!rows.get(i).isDelete()) {
+                index++;
+            }
+        }
+        return index;
+
     }
 
     public short getShort(int columnIndex) throws SQLException {
@@ -1067,13 +1202,6 @@
         }
     }
 
-    private void checkCursorValid() throws SQLException {
-        if ((currentRowIndex <= 0) || (currentRowIndex > rows.size())) {
-            // rowset.7=Not a valid cursor
-            throw new SQLException(Messages.getString("rowset.7")); //$NON-NLS-1$
-        }
-    }
-
     public String getString(String columnName) throws SQLException {
         return getString(getIndexByName(columnName));
     }
@@ -1160,7 +1288,7 @@
     }
 
     public boolean isFirst() throws SQLException {
-        return currentRowIndex == 1;
+        return getRow() == 1;
     }
 
     public boolean isLast() throws SQLException {
@@ -1168,11 +1296,11 @@
             return false;
         }
 
-        return currentRowIndex == rows.size();
+        return getRow() == getValidRowSize();
     }
 
     public boolean last() throws SQLException {
-        return doAbsolute(-1, true);
+        return doAbsolute(getIndexIncludeDeletedRows(-1), true);
     }
 
     public void moveToCurrentRow() throws SQLException {
@@ -1200,11 +1328,67 @@
          * 
          * next() doesn't check TYPE_FORWARD_ONLY property, relative(1) does.
          */
-        return doAbsolute(currentRowIndex + 1, false);
+        return doAbsolute(findNextValidRow(), false);
+    }
+
+    /**
+     * Valid row is row which is visible to users. If <code>showDeleted</code>
+     * is true, deleted rows are valid rows, otherwise deleted rows are invalid.
+     * 
+     * @return next valid row
+     * @throws SQLException
+     */
+    private int findNextValidRow() throws SQLException {
+        int index = currentRowIndex + 1;
+
+        if (getShowDeleted()) {
+            return index;
+        }
+
+        if (index > rows.size()) {
+            return rows.size() + 1;
+        }
+
+        while (index <= rows.size()) {
+            if (!rows.get(index - 1).isDelete()) {
+                break;
+            }
+            index++;
+        }
+
+        return index;
     }
 
     public boolean previous() throws SQLException {
-        return doAbsolute(currentRowIndex - 1, true);
+        return doAbsolute(findPreviousValidRow(), true);
+    }
+
+    /**
+     * Valid row is row which is visible to users. If <code>showDeleted</code>
+     * is true, deleted rows are valid rows, otherwise deleted rows are invalid.
+     * 
+     * @return previous valid row
+     * @throws SQLException
+     */
+    private int findPreviousValidRow() throws SQLException {
+        int index = currentRowIndex - 1;
+
+        if (index <= 0) {
+            return 0;
+        }
+
+        if (getShowDeleted()) {
+            return index;
+        }
+
+        while (index > 0) {
+            if (!rows.get(index - 1).isDelete()) {
+                break;
+            }
+            index--;
+        }
+
+        return index;
     }
 
     public void refreshRow() throws SQLException {
@@ -1218,6 +1402,7 @@
         }
 
         int index = getRow() + moveRows;
+
         if (index <= 0) {
             beforeFirst();
             return false;
@@ -1228,7 +1413,7 @@
             return false;
         }
 
-        return doAbsolute(index, true);
+        return doAbsolute(getIndexIncludeDeletedRows(index), true);
     }
 
     public boolean rowDeleted() throws SQLException {
@@ -1395,7 +1580,7 @@
     }
 
     public void updateNull(int columnIndex) throws SQLException {
-        checkCursorValid();
+        checkValidRow();
         checkColumnValid(columnIndex);
         currentRow.updateObject(columnIndex, null);
     }

Added: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetDeleteTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetDeleteTest.java?rev=633787&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetDeleteTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetDeleteTest.java Wed Mar  5 00:31:03 2008
@@ -0,0 +1,676 @@
+/* 
+ * 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 org.apache.harmony.sql.tests.internal.rowset;
+
+import java.sql.SQLException;
+
+public class CachedRowSetDeleteTest extends CachedRowSetTestCase {
+    public void testDeleteRow_CursorPos() throws Exception {
+        /*
+         * This method is mainly used to test cursor position after delete
+         */
+        insertMoreData(5);
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.populate(rs);
+
+        assertFalse(noInitialCrset.getShowDeleted());
+
+        /*
+         * move to the fifth row, then delete it, check the cursor position
+         */
+        assertTrue(noInitialCrset.absolute(5));
+        assertEquals(5, noInitialCrset.getInt(1));
+        noInitialCrset.deleteRow();
+        assertEquals(5, noInitialCrset.getInt(1));
+        assertEquals(0, noInitialCrset.getRow());
+
+        noInitialCrset.acceptChanges(conn);
+        // the cursor is on the sixth row now, that is move to the next row
+        assertEquals(6, noInitialCrset.getInt(1));
+
+        // cursor index is no change
+        assertTrue(noInitialCrset.absolute(5));
+        assertEquals(6, noInitialCrset.getInt(1));
+
+        /*
+         * Delete the sixth row. Then move the cursor to the seventh row. Check
+         * the cursor position after delete.
+         */
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            // TODO RI move cursor incorrectly, we follow spec
+            assertEquals(5, noInitialCrset.getRow());
+            noInitialCrset.deleteRow();
+            assertTrue(noInitialCrset.next());
+
+            assertEquals(5, noInitialCrset.getRow());
+            assertEquals(7, noInitialCrset.getInt(1));
+
+            noInitialCrset.acceptChanges(conn);
+
+            // cursor index is no change
+            assertEquals(5, noInitialCrset.getRow());
+            assertEquals(7, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.absolute(5));
+            assertEquals(7, noInitialCrset.getInt(1));
+        } else {
+            noInitialCrset.deleteRow();
+            assertTrue(noInitialCrset.next());
+            assertEquals(7, noInitialCrset.getInt(1));
+            noInitialCrset.acceptChanges(conn);
+            /*
+             * We can see the cursor is on the eighth row now.
+             */
+            assertEquals(8, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.absolute(6));
+            assertEquals(8, noInitialCrset.getInt(1));
+        }
+        /*
+         * Delete the row before the last row. Then move the cursor to the last
+         * row before call acceptChanges(). Check the cursor position after
+         * delete.
+         */
+        assertTrue(noInitialCrset.last());
+        assertTrue(noInitialCrset.previous());
+        assertEquals(9, noInitialCrset.getInt(1));
+        noInitialCrset.deleteRow();
+        assertTrue(noInitialCrset.last());
+        assertEquals(10, noInitialCrset.getInt(1));
+        noInitialCrset.acceptChanges(conn);
+
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            // TODO RI move cursor incorrectly, we follow spec
+            assertTrue(noInitialCrset.isLast());
+
+            assertEquals(10, noInitialCrset.getInt(1));
+            noInitialCrset.acceptChanges(conn);
+        } else {
+            assertTrue(noInitialCrset.isAfterLast());
+        }
+    }
+
+    public void testDeleteRow_MoveCursor() throws Exception {
+        insertMoreData(5);
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.populate(rs);
+
+        assertFalse(noInitialCrset.getShowDeleted());
+
+        assertTrue(noInitialCrset.absolute(3));
+        assertEquals(3, noInitialCrset.getInt(1));
+
+        assertEquals(3, noInitialCrset.getRow());
+        noInitialCrset.deleteRow(); // delete the third row
+        assertEquals(0, noInitialCrset.getRow());
+
+        assertTrue(noInitialCrset.next());
+
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            // TODO RI move cursor incorrectly, we follow spec
+            assertEquals(3, noInitialCrset.getRow());
+            assertEquals(4, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.absolute(4));
+            assertEquals(4, noInitialCrset.getRow());
+            assertEquals(5, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.previous());
+            assertEquals(3, noInitialCrset.getRow());
+            assertEquals(4, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.previous());
+            assertEquals(2, noInitialCrset.getRow());
+            assertEquals(2, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.previous());
+            assertEquals(1, noInitialCrset.getRow());
+            assertEquals(1, noInitialCrset.getInt(1));
+
+        } else {
+            /*
+             * TODO seems RI doesn't adjust cursor index after delete row
+             */
+            assertEquals(4, noInitialCrset.getRow());
+            assertEquals(4, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.absolute(4));
+            assertEquals(4, noInitialCrset.getRow());
+            assertEquals(4, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.previous());
+            assertEquals(3, noInitialCrset.getRow());
+            assertEquals(2, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.previous());
+            assertEquals(2, noInitialCrset.getRow());
+            assertEquals(1, noInitialCrset.getInt(1));
+
+            assertFalse(noInitialCrset.previous());
+            assertEquals(0, noInitialCrset.getRow());
+
+            try {
+                noInitialCrset.getInt(1);
+                fail("Should throw SQLException");
+            } catch (SQLException e) {
+                // expected, Invalid cursor position
+            }
+        }
+    }
+
+    public void testDeleteRow_MoveCursor2() throws Exception {
+        insertMoreData(5);
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.populate(rs);
+
+        assertFalse(noInitialCrset.getShowDeleted());
+
+        assertTrue(noInitialCrset.absolute(3));
+        assertEquals(3, noInitialCrset.getInt(1));
+
+        assertEquals(3, noInitialCrset.getRow());
+        noInitialCrset.deleteRow(); // delete the third row
+        assertEquals(0, noInitialCrset.getRow());
+
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            assertTrue(noInitialCrset.absolute(3));
+            assertEquals(3, noInitialCrset.getRow());
+            assertEquals(4, noInitialCrset.getInt(1));
+        } else {
+            /*
+             * TODO RI's behavior is weird, we can't understand, so Harmony will
+             * follow spec
+             */
+            assertFalse(noInitialCrset.getShowDeleted());
+            assertTrue(noInitialCrset.absolute(3));
+            assertEquals(0, noInitialCrset.getRow());
+            // i can see the deleted row!!
+            assertEquals(3, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.next());
+            assertEquals(4, noInitialCrset.getRow());
+            assertEquals(4, noInitialCrset.getInt(1));
+
+            assertTrue(noInitialCrset.previous());
+            assertEquals(3, noInitialCrset.getRow());
+            // deleted row disappeared
+            assertEquals(2, noInitialCrset.getInt(1));
+
+        }
+    }
+
+    public void testDeleteRow_MultiDel() throws Exception {
+        insertMoreData(5);
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.populate(rs);
+
+        assertFalse(noInitialCrset.getShowDeleted());
+
+        assertTrue(noInitialCrset.absolute(3));
+        assertEquals(3, noInitialCrset.getInt(1));
+        noInitialCrset.deleteRow(); // delete the third row
+        assertEquals(0, noInitialCrset.getRow());
+
+        assertTrue(noInitialCrset.next());
+        noInitialCrset.deleteRow(); // delete the fourth row
+        assertEquals(0, noInitialCrset.getRow());
+
+        assertTrue(noInitialCrset.next());
+        noInitialCrset.deleteRow(); // delete the fifth row
+        assertEquals(0, noInitialCrset.getRow());
+        noInitialCrset.acceptChanges(conn);
+
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            // TODO RI move cursor incorrectly, we follow spec
+            assertEquals(6, noInitialCrset.getInt(1));
+        } else {
+            assertEquals(8, noInitialCrset.getInt(1));
+        }
+    }
+
+    public void testRowDeleted() throws Exception {
+        noInitialCrset = newNoInitialInstance();
+        try {
+            noInitialCrset.rowDeleted();
+            fail("should throw SQLException");
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // RI would throw ArrayIndexOutOfBoundsException
+        } catch (SQLException e) {
+            // according to spec, it's supposed to throw SQLException
+        }
+
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        noInitialCrset.populate(rs);
+
+        try {
+            noInitialCrset.rowDeleted();
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        assertTrue(noInitialCrset.next());
+        assertFalse(noInitialCrset.rowDeleted());
+        noInitialCrset.deleteRow();
+        assertTrue(noInitialCrset.rowDeleted());
+    }
+
+    public void testDeleteRow_Exception() throws Exception {
+        /*
+         * This method is mainly used to test exception
+         */
+        noInitialCrset = newNoInitialInstance();
+        try {
+            noInitialCrset.deleteRow();
+            fail("should throw SQLException");
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // RI would throw ArrayIndexOutOfBoundsException
+        } catch (SQLException e) {
+            // according to spec, it's supposed to throw SQLException
+        }
+
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        noInitialCrset.populate(rs);
+
+        try {
+            noInitialCrset.deleteRow();
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        noInitialCrset.moveToInsertRow();
+        try {
+            noInitialCrset.deleteRow();
+            fail("should throw SQLException");
+        } catch (ClassCastException e) {
+            // RI would throw ClassCastException
+        } catch (SQLException e) {
+            // expected
+        }
+
+        noInitialCrset.moveToCurrentRow();
+        assertTrue(noInitialCrset.absolute(4));
+        assertEquals(4, noInitialCrset.getInt(1));
+        noInitialCrset.deleteRow();
+        noInitialCrset.acceptChanges(conn);
+        // check the cursor position after delete
+        assertTrue(noInitialCrset.isAfterLast());
+
+        // check DB
+        rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 4");
+        assertTrue(rs.next());
+        assertEquals(0, rs.getInt(1));
+
+        // check CachedRowSet
+        noInitialCrset.beforeFirst();
+        int index = 0;
+        while (noInitialCrset.next()) {
+            index++;
+            assertEquals(index, noInitialCrset.getInt(1));
+        }
+        assertEquals(3, index);
+    }
+
+    public void testUndoDeleted() throws Exception {
+        noInitialCrset = newNoInitialInstance();
+        // if no data, nothing would happen when calling undoDelete()
+        noInitialCrset.undoDelete();
+
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        noInitialCrset.populate(rs);
+
+        /*
+         * Test exception. According to spec, it would throw SQLException when
+         * calling undoDelete() in such conditions:
+         * 
+         * 1) the current row has not been deleted.
+         * 
+         * 2) the cursor is on the insert row.
+         * 
+         * 3) the curosr is before the first row, or after the last row.
+         */
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            // TODO RI doesn't follow the spec, we follow spec
+            noInitialCrset.beforeFirst();
+            assertTrue(noInitialCrset.isBeforeFirst());
+            try {
+                noInitialCrset.undoDelete();
+                fail("Should throw SQLException");
+            } catch (SQLException e) {
+                // expected, invalid cursor
+            }
+
+            noInitialCrset.afterLast();
+            assertTrue(noInitialCrset.isAfterLast());
+            try {
+                noInitialCrset.undoDelete();
+                fail("Should throw SQLException");
+            } catch (SQLException e) {
+                // expected, invalid cursor
+            }
+
+            noInitialCrset.moveToInsertRow();
+            try {
+                noInitialCrset.undoDelete();
+                fail("Should throw SQLException");
+            } catch (SQLException e) {
+                // expected, invalid cursor
+            }
+
+            noInitialCrset.moveToCurrentRow();
+            assertTrue(noInitialCrset.absolute(1));
+            assertFalse(noInitialCrset.rowDeleted());
+            try {
+                noInitialCrset.undoDelete();
+                fail("Should throw SQLException");
+            } catch (SQLException e) {
+                // expected, invalid cursor
+            }
+
+        } else {
+            // when run on RI, it won't throw SQLException
+            noInitialCrset.beforeFirst();
+            assertTrue(noInitialCrset.isBeforeFirst());
+            noInitialCrset.undoDelete();
+
+            noInitialCrset.afterLast();
+            assertTrue(noInitialCrset.isAfterLast());
+            noInitialCrset.undoDelete();
+
+            noInitialCrset.moveToInsertRow();
+            noInitialCrset.undoDelete();
+
+            noInitialCrset.moveToCurrentRow();
+            assertTrue(noInitialCrset.absolute(1));
+            assertFalse(noInitialCrset.rowDeleted());
+            noInitialCrset.undoDelete();
+        }
+
+        // delete the fourth row
+        assertTrue(noInitialCrset.absolute(4));
+        assertEquals(4, noInitialCrset.getInt(1));
+        assertFalse(noInitialCrset.rowDeleted());
+        noInitialCrset.deleteRow();
+        assertTrue(noInitialCrset.rowDeleted());
+
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            noInitialCrset.undoDelete();
+            assertFalse(noInitialCrset.rowDeleted());
+            noInitialCrset.acceptChanges(conn);
+
+            // check DB
+            rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 4");
+            assertTrue(rs.next());
+            assertEquals(1, rs.getInt(1));
+
+            // check CachedRowSet
+            noInitialCrset.beforeFirst();
+            int index = 0;
+            while (noInitialCrset.next()) {
+                index++;
+                assertEquals(index, noInitialCrset.getInt(1));
+            }
+            assertEquals(4, index);
+
+        } else {
+            // TODO undoDelete() still makes no difference in RI
+            noInitialCrset.undoDelete();
+            assertTrue(noInitialCrset.rowDeleted());
+            noInitialCrset.acceptChanges(conn);
+
+            // check DB
+            rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 4");
+            assertTrue(rs.next());
+            assertEquals(0, rs.getInt(1));
+
+            // check CachedRowSet
+            noInitialCrset.beforeFirst();
+            int index = 0;
+            while (noInitialCrset.next()) {
+                index++;
+                assertEquals(index, noInitialCrset.getInt(1));
+            }
+            assertEquals(3, index);
+        }
+
+    }
+
+    public void testShowDeleted_False() throws Exception {
+        /*
+         * when getShowDeleted() is false
+         */
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        noInitialCrset.populate(rs);
+        assertFalse(noInitialCrset.getShowDeleted());
+
+        // delete the third row
+        assertTrue(noInitialCrset.absolute(3));
+        assertFalse(noInitialCrset.rowDeleted());
+        noInitialCrset.deleteRow();
+        assertTrue(noInitialCrset.rowDeleted());
+        // call next(), then call previous, to check which row the curosr is on
+        assertTrue(noInitialCrset.next());
+        assertEquals(4, noInitialCrset.getInt(1));
+        assertTrue(noInitialCrset.previous());
+        // NOTICE: The cursor is on the second row, not the third row
+        assertEquals(2, noInitialCrset.getInt(1));
+
+        // assertTrue(noInitialCrset.absolute(1));
+        // System.out.println("first: ");
+        // for (int i = 1; i <= DEFAULT_COLUMN_COUNT; i++) {
+        // System.out.print(noInitialCrset.getObject(i) + " ");
+        // }
+        // retrieve the data in CachedRowSet after calling deleteRow()
+        noInitialCrset.beforeFirst();
+        int index = 0;
+        while (noInitialCrset.next()) {
+            index++;
+            if (index == 3) {
+                assertEquals(4, noInitialCrset.getInt(1));
+            } else {
+                assertEquals(index, noInitialCrset.getInt(1));
+            }
+        }
+        assertEquals(3, index);
+
+        // commit to DB
+        noInitialCrset.acceptChanges(conn);
+
+        // check DB
+        rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 3");
+        assertTrue(rs.next());
+        assertEquals(0, rs.getInt(1));
+
+        // check CachedRowSet
+        noInitialCrset.beforeFirst();
+        index = 0;
+        while (noInitialCrset.next()) {
+            index++;
+            if (index == 3) {
+                assertEquals(4, noInitialCrset.getInt(1));
+            } else {
+                assertEquals(index, noInitialCrset.getInt(1));
+            }
+        }
+        assertEquals(3, index);
+    }
+
+    public void testShowDeleted_True() throws Exception {
+        /*
+         * when getShowDeleted() is true
+         */
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.populate(rs);
+        assertFalse(noInitialCrset.getShowDeleted());
+        noInitialCrset.setShowDeleted(true);
+        assertTrue(noInitialCrset.getShowDeleted());
+
+        // delete the third row
+        assertTrue(noInitialCrset.absolute(3));
+        assertFalse(noInitialCrset.rowDeleted());
+        noInitialCrset.deleteRow();
+        assertTrue(noInitialCrset.rowDeleted());
+        // call next(), then call previous, to check which row the curosr is on
+        assertTrue(noInitialCrset.next());
+        assertEquals(4, noInitialCrset.getInt(1));
+        assertTrue(noInitialCrset.previous());
+        // NOTICE: The cursor is on the third row. There is the difference
+        // between getShowDeleted() is TRUE and FALSE
+        assertEquals(3, noInitialCrset.getInt(1));
+
+        // retrieve the data in CachedRowSet after calling deleteRow()
+        noInitialCrset.beforeFirst();
+        int index = 0;
+        while (noInitialCrset.next()) {
+            index++;
+            assertEquals(index, noInitialCrset.getInt(1));
+            if (index == 3) {
+                assertTrue(noInitialCrset.rowDeleted());
+            } else {
+                assertFalse(noInitialCrset.rowDeleted());
+            }
+        }
+        assertEquals(4, index);
+
+        /*
+         * move to the third row which is marked as deleted, then call
+         * undoDelete(), see what will happen
+         */
+        assertTrue(noInitialCrset.absolute(3));
+        assertEquals(3, noInitialCrset.getInt(1));
+        assertTrue(noInitialCrset.rowDeleted());
+        noInitialCrset.undoDelete();
+        assertFalse(noInitialCrset.rowDeleted());
+
+        // commit to DB
+        noInitialCrset.acceptChanges(conn);
+
+        // check DB
+        rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 3");
+        assertTrue(rs.next());
+        assertEquals(1, rs.getInt(1));
+
+        // check CachedRowSet
+        noInitialCrset.beforeFirst();
+        index = 0;
+        while (noInitialCrset.next()) {
+            index++;
+            assertEquals(index, noInitialCrset.getInt(1));
+            assertFalse(noInitialCrset.rowDeleted());
+        }
+        assertEquals(4, index);
+    }
+
+    public void testShowDeleted_StateChange() throws Exception {
+        /*
+         * getShowDeleted() is false in default. First, call deleteRow() on the
+         * third row; then setShowDeleted(true), see what will happen; at last,
+         * setShowDeleted(false) back.
+         */
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        noInitialCrset.populate(rs);
+        assertFalse(noInitialCrset.getShowDeleted());
+
+        /*
+         * Delete the third row when getShowDeleted() is false
+         */
+        assertTrue(noInitialCrset.absolute(3));
+        assertFalse(noInitialCrset.rowDeleted());
+        noInitialCrset.deleteRow();
+        assertTrue(noInitialCrset.rowDeleted());
+        // call next(), then call previous, to check which row the curosr is on
+        assertTrue(noInitialCrset.next());
+        assertEquals(4, noInitialCrset.getInt(1));
+        assertTrue(noInitialCrset.previous());
+        // NOTICE: The cursor is on the second row, not the third row
+        assertEquals(2, noInitialCrset.getInt(1));
+        // retrieve the data in CachedRowSet
+        noInitialCrset.beforeFirst();
+        int index = 0;
+        while (noInitialCrset.next()) {
+            index++;
+            if (index == 3) {
+                assertEquals(4, noInitialCrset.getInt(1));
+            } else {
+                assertEquals(index, noInitialCrset.getInt(1));
+            }
+        }
+        assertEquals(3, index);
+
+        /*
+         * setShowDeleted(true), see what's happen
+         */
+        noInitialCrset.setShowDeleted(true);
+        noInitialCrset.beforeFirst();
+        index = 0;
+        while (noInitialCrset.next()) {
+            index++;
+            assertEquals(index, noInitialCrset.getInt(1));
+            if (index == 3) {
+                assertTrue(noInitialCrset.rowDeleted());
+            } else {
+                assertFalse(noInitialCrset.rowDeleted());
+            }
+        }
+        assertEquals(4, index);
+
+        /*
+         * setShowDeleted(false), see what's happen
+         */
+        noInitialCrset.setShowDeleted(false);
+        noInitialCrset.beforeFirst();
+        index = 0;
+        while (noInitialCrset.next()) {
+            index++;
+            if (index == 3) {
+                assertEquals(4, noInitialCrset.getInt(1));
+            } else {
+                assertEquals(index, noInitialCrset.getInt(1));
+            }
+        }
+        assertEquals(3, index);
+
+        /*
+         * setShowDeleted(true), then commit to DB
+         */
+        noInitialCrset.acceptChanges(conn);
+        // check DB
+        rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 3");
+        assertTrue(rs.next());
+        assertEquals(0, rs.getInt(1));
+
+        // check CachedRowSet
+        noInitialCrset.beforeFirst();
+        index = 0;
+        while (noInitialCrset.next()) {
+            index++;
+            if (index == 3) {
+                assertEquals(4, noInitialCrset.getInt(1));
+            } else {
+                assertEquals(index, noInitialCrset.getInt(1));
+            }
+        }
+        assertEquals(3, index);
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetDeleteTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=633787&r1=633786&r2=633787&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 Wed Mar  5 00:31:03 2008
@@ -180,156 +180,6 @@
         assertEquals(0, noInitialCrset.size());
     }
 
-    public void testDeleteRow_Exception() throws Exception {
-        /*
-         * This method is mainly used to test exception
-         */
-        noInitialCrset = newNoInitialInstance();
-        try {
-            noInitialCrset.deleteRow();
-            fail("should throw SQLException");
-        } catch (ArrayIndexOutOfBoundsException e) {
-            // RI would throw ArrayIndexOutOfBoundsException
-        } catch (SQLException e) {
-            // according to spec, it's supposed to throw SQLException
-        }
-
-        rs = st.executeQuery("SELECT * FROM USER_INFO");
-        noInitialCrset.populate(rs);
-
-        try {
-            noInitialCrset.deleteRow();
-            fail("should throw SQLException");
-        } catch (SQLException e) {
-            // expected
-        }
-
-        noInitialCrset.moveToInsertRow();
-        try {
-            noInitialCrset.deleteRow();
-            fail("should throw SQLException");
-        } catch (ClassCastException e) {
-            // RI would throw ClassCastException
-        } catch (SQLException e) {
-            // expected
-        }
-
-        noInitialCrset.moveToCurrentRow();
-        assertTrue(noInitialCrset.absolute(4));
-        assertEquals(4, noInitialCrset.getInt(1));
-        noInitialCrset.deleteRow();
-        noInitialCrset.acceptChanges(conn);
-        // check the cursor position after delete
-        assertTrue(noInitialCrset.isAfterLast());
-
-        // check DB
-        rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 4");
-        assertTrue(rs.next());
-        assertEquals(0, rs.getInt(1));
-
-        // check CachedRowSet
-        noInitialCrset.beforeFirst();
-        int index = 0;
-        while (noInitialCrset.next()) {
-            index++;
-            assertEquals(index, noInitialCrset.getInt(1));
-        }
-        assertEquals(3, index);
-    }
-
-    public void testDeleteRow_CursorPos() throws Exception {
-        /*
-         * This method is mainly used to test cursor position after delete
-         */
-        insertMoreData(5);
-        rs = st.executeQuery("SELECT * FROM USER_INFO");
-        noInitialCrset = newNoInitialInstance();
-        noInitialCrset.populate(rs);
-
-        /*
-         * move to the fifth row, then delete it, check the cursor position
-         */
-        assertTrue(noInitialCrset.absolute(5));
-        assertEquals(5, noInitialCrset.getInt(1));
-        noInitialCrset.deleteRow();
-        noInitialCrset.acceptChanges(conn);
-        // the cursor is on the sixth row now, that is move to the next row
-        assertEquals(6, noInitialCrset.getInt(1));
-        assertTrue(noInitialCrset.absolute(5));
-        assertEquals(6, noInitialCrset.getInt(1));
-
-        /*
-         * Delete the sixth row. Then move the cursor to the seventh row. Check
-         * the cursor position after delete.
-         */
-        noInitialCrset.deleteRow();
-        assertTrue(noInitialCrset.next());
-        assertEquals(7, noInitialCrset.getInt(1));
-        noInitialCrset.acceptChanges(conn);
-        /*
-         * We can see the cursor is on the eighth row now.
-         */
-        assertEquals(8, noInitialCrset.getInt(1));
-
-        /*
-         * Delete the row before the last row. Then move the cursor to the last
-         * row before call acceptChanges(). Check the cursor position after
-         * delete.
-         */
-        assertTrue(noInitialCrset.last());
-        assertTrue(noInitialCrset.previous());
-        assertEquals(9, noInitialCrset.getInt(1));
-        noInitialCrset.deleteRow();
-        assertTrue(noInitialCrset.last());
-        assertEquals(10, noInitialCrset.getInt(1));
-        noInitialCrset.acceptChanges(conn);
-        assertTrue(noInitialCrset.isAfterLast());
-    }
-
-    public void testDeleteRow_MultiDel() throws Exception {
-        insertMoreData(5);
-        rs = st.executeQuery("SELECT * FROM USER_INFO");
-        noInitialCrset = newNoInitialInstance();
-        noInitialCrset.populate(rs);
-
-        assertTrue(noInitialCrset.absolute(3));
-        noInitialCrset.deleteRow(); // delete the third row
-        assertTrue(noInitialCrset.next());
-        noInitialCrset.deleteRow(); // delete the fourth row
-        assertTrue(noInitialCrset.next());
-        noInitialCrset.deleteRow(); // delete the fifth row
-        assertTrue(noInitialCrset.first());
-        noInitialCrset.acceptChanges(conn);
-        assertEquals(1, noInitialCrset.getInt(1));
-    }
-
-    public void testRowDeleted() throws Exception {
-        noInitialCrset = newNoInitialInstance();
-        try {
-            noInitialCrset.rowDeleted();
-            fail("should throw SQLException");
-        } catch (ArrayIndexOutOfBoundsException e) {
-            // RI would throw ArrayIndexOutOfBoundsException
-        } catch (SQLException e) {
-            // according to spec, it's supposed to throw SQLException
-        }
-
-        rs = st.executeQuery("SELECT * FROM USER_INFO");
-        noInitialCrset.populate(rs);
-
-        try {
-            noInitialCrset.rowDeleted();
-            fail("should throw SQLException");
-        } catch (SQLException e) {
-            // expected
-        }
-
-        assertTrue(noInitialCrset.next());
-        assertFalse(noInitialCrset.rowDeleted());
-        noInitialCrset.deleteRow();
-        assertTrue(noInitialCrset.rowDeleted());
-    }
-
     public void testAcceptChanges() throws SQLException {
         crset.setTableName("USER_INFO");
         // FIXME: if the value of column is null, it would go wrong when
@@ -1363,17 +1213,31 @@
         assertTrue(crset.isAfterLast());
         assertTrue(crset.previous());
 
-        // non-bug different
+        // TODO RI's bug
         if ("true".equals(System.getProperty("Testing Harmony"))) {
             assertEquals(DEFAULT_ROW_COUNT, crset.getRow());
             assertEquals("test4", crset.getString(2));
+        } else {
+            assertEquals(-1, crset.getRow());
+            assertEquals("test4", crset.getString(2));
+
+            assertTrue(crset.previous());
+            assertEquals(-2, crset.getRow());
+            assertEquals("test3", crset.getString(2));
         }
     }
 
     public void testAbsolute() throws Exception {
-        // non-bug different
+        // TODO non-bug different
         if ("true".equals(System.getProperty("Testing Harmony"))) {
             assertFalse(crset.absolute(0));
+        } else {
+            try {
+                assertTrue(crset.absolute(0));
+                fail("Should throw SQLException");
+            } catch (SQLException e) {
+                // invalid cursor position
+            }
         }
 
         assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, crset.getType());
@@ -1393,9 +1257,9 @@
         assertTrue(crset.next());
 
         /*
-         * when the given row number is negative, spec says absolute(-1) equals
-         * last(). However, the return value of absolute(negative) is false when
-         * run on RI. The Harmony follows the spec.
+         * TODO when the given row number is negative, spec says absolute(-1)
+         * equals last(). However, the return value of absolute(negative) is
+         * false when run on RI. The Harmony follows the spec.
          */
         if (System.getProperty("Testing Harmony") == "true") {
             assertTrue(crset.absolute(-1));
@@ -1403,6 +1267,10 @@
             assertTrue(crset.absolute(-3));
             assertEquals(2, crset.getInt(1));
             assertFalse(crset.absolute(-5));
+        } else {
+            assertFalse(crset.absolute(-1));
+            assertEquals(0, crset.getRow());
+            assertTrue(crset.isBeforeFirst());
         }
 
         crset.setType(ResultSet.TYPE_FORWARD_ONLY);
@@ -2143,6 +2011,257 @@
             }
 
         }
+    }
+
+    public void testFindColumn() throws SQLException {
+        try {
+            noInitialCrset.findColumn(null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            noInitialCrset.findColumn("ID");
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            noInitialCrset.findColumn("not exist name");
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            crset.findColumn("not exist name");
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected, Invalid column name
+        }
+
+        try {
+            crset.findColumn(null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        assertEquals(1, crset.findColumn("ID"));
+
+        assertEquals(1, crset.findColumn("id"));
+
+        assertEquals(7, crset.findColumn("FLOAT_T"));
+
+        assertEquals(7, crset.findColumn("FloaT_T"));
+    }
+
+    /**
+     * TODO test event
+     * 
+     * @throws Exception
+     */
+    public void testRestoreOriginal() throws Exception {
+        // update
+        assertTrue(crset.absolute(3));
+        assertEquals(3, crset.getInt(1));
+        assertEquals("test3", crset.getString(2));
+
+        crset.updateString(2, "update3");
+        assertEquals("update3", crset.getString(2));
+
+        crset.updateRow();
+        crset.restoreOriginal();
+
+        /*
+         * TODO seems RI put the cursor out of rowset after restoreOriginal, RI'
+         * bug
+         */
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            assertTrue(crset.isFirst());
+            assertEquals(1, crset.getRow());
+            assertTrue(crset.absolute(3));
+        } else {
+            assertEquals(0, crset.getRow());
+            crset.beforeFirst();
+            assertTrue(crset.next());
+            assertTrue(crset.next());
+            assertTrue(crset.next());
+        }
+
+        assertEquals(3, crset.getInt(1));
+        assertEquals("test3", crset.getString(2));
+
+        // insert
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        crset = newNoInitialInstance();
+        crset.populate(rs);
+        crset.next();
+
+        crset.moveToInsertRow();
+        crset.updateInt(1, 5);
+        crset.updateString(2, "test5");
+        crset.updateLong(3, 555555L);
+        crset.updateInt(4, 200); // 200000 exceeds the NUMERIC's range
+        crset.updateBigDecimal(5, new BigDecimal(23));
+        crset.updateFloat(8, 4.888F);
+        crset.insertRow();
+
+        crset.restoreOriginal();
+
+        /*
+         * TODO seems RI put the cursor out of rowset after restoreOriginal, RI'
+         * bug
+         */
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            assertTrue(crset.isFirst());
+            assertEquals(1, crset.getRow());
+
+            int count = 0;
+            crset.beforeFirst();
+            while (crset.next()) {
+                count++;
+            }
+            assertEquals(4, count);
+        } else {
+            assertEquals(0, crset.getRow());
+
+            // assertTrue(crset.next()); throws SQLException
+            crset.beforeFirst();
+            assertTrue(crset.isBeforeFirst());
+            // assertTrue(crset.next()); throws SQLException
+            /*
+             * I can't move cursor to a valid position, so can't do any check
+             */
+        }
+
+        // delete
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        crset = newNoInitialInstance();
+        crset.populate(rs);
+
+        crset.absolute(3);
+        crset.deleteRow();
+
+        crset.restoreOriginal();
+
+        /*
+         * TODO seems RI put the cursor out of rowset after restoreOriginal, RI'
+         * bug
+         */
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            assertTrue(crset.isFirst());
+            assertEquals(1, crset.getRow());
+            assertTrue(crset.absolute(3));
+            assertEquals(3, crset.getInt(1));
+        } else {
+            assertEquals(0, crset.getRow());
+            crset.beforeFirst();
+            assertTrue(crset.absolute(3));
+            assertEquals(3, crset.getInt(1));
+        }
+
+        crset = newNoInitialInstance();
+
+        crset.restoreOriginal();
+
+        // invoke restoreOriginal cursor out of rowset
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        crset = newNoInitialInstance();
+        crset.populate(rs);
+
+        assertTrue(crset.absolute(3));
+        assertEquals(3, crset.getInt(1));
+        assertEquals("test3", crset.getString(2));
+
+        crset.updateString(2, "update3");
+        assertEquals("update3", crset.getString(2));
+
+        crset.updateRow();
+
+        crset.beforeFirst();
+
+        crset.restoreOriginal();
+
+        /*
+         * TODO seems RI put the cursor out of rowset after restoreOriginal, RI'
+         * bug
+         */
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            assertTrue(crset.isFirst());
+            assertEquals(1, crset.getRow());
+            assertTrue(crset.absolute(3));
+        } else {
+            assertEquals(0, crset.getRow());
+            crset.beforeFirst();
+            assertTrue(crset.next());
+            assertTrue(crset.next());
+            assertTrue(crset.next());
+        }
+
+        assertEquals(3, crset.getInt(1));
+        assertEquals("test3", crset.getString(2));
+    }
+
+    /**
+     * TODO test event
+     * 
+     * @throws Exception
+     */
+    public void testRestoreOriginal_MultiChanges() throws Exception {
+        insertMoreData(5);
+
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        crset = newNoInitialInstance();
+        crset.populate(rs);
+
+        // row 3 not call updateRow
+        assertTrue(crset.absolute(3));
+        assertEquals(3, crset.getInt(1));
+        assertEquals("test3", crset.getString(2));
+
+        crset.updateString(2, "update3");
+        assertEquals("update3", crset.getString(2));
+
+        // row 4 call updateRow
+        assertTrue(crset.next());
+        assertEquals(4, crset.getInt(1));
+        assertEquals("test4", crset.getString(2));
+
+        crset.updateString(2, "update4");
+        assertEquals("update4", crset.getString(2));
+        crset.updateRow();
+
+        // delete row 5
+        assertTrue(crset.next());
+        assertEquals(5, crset.getInt(1));
+        crset.deleteRow();
+
+        crset.restoreOriginal();
+
+        /*
+         * TODO seems RI put the cursor out of rowset after restoreOriginal, RI'
+         * bug
+         */
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            assertTrue(crset.isFirst());
+            assertEquals(1, crset.getRow());
+        } else {
+            assertEquals(0, crset.getRow());
+            crset.beforeFirst();
+        }
+
+        assertTrue(crset.absolute(3));
+        assertEquals(3, crset.getInt(1));
+        assertEquals("update3", crset.getString(2));
+
+        assertTrue(crset.next());
+        assertEquals("test4", crset.getString(2));
+
+        assertTrue(crset.next());
+        assertEquals(5, crset.getInt(1));
     }
 
     public class Listener implements RowSetListener, Cloneable {

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java?rev=633787&r1=633786&r2=633787&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java Wed Mar  5 00:31:03 2008
@@ -750,7 +750,7 @@
             assertEquals(scaled, crset.getObject(5));
         } else {
             /*
-             * seems ri doesn't do scale
+             * TODO seems ri doesn't do scale
              */
             assertEquals(bigDecimal, crset.getBigDecimal(5));
             assertEquals(bigDecimal, crset.getObject(5));



Mime
View raw message