harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r635493 - 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 Mon, 10 Mar 2008 09:04:14 GMT
Author: tonywu
Date: Mon Mar 10 02:04:01 2008
New Revision: 635493

URL: http://svn.apache.org/viewvc?rev=635493&view=rev
Log:
Apply patch for HARMONY-5588 ([classlib][sql] CachedRowSetImpl add paging support)

Added:
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetPagingTest.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/CachedRowSetDeleteTest.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/CachedRowSetInsertTest.java
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetTestCase.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=635493&r1=635492&r2=635493&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
Mon Mar 10 02:04:01 2008
@@ -83,7 +83,22 @@
     // the number of the rows in one "page"
     private int pageSize;
 
-    private int pageNumber = 1;
+    /**
+     * used for paging, record row index for next page in ResultSet, start from
+     * 1
+     */
+    private int nextPageRowIndex = -1;
+
+    /**
+     * used for paging, record row index for previous page in ResultSet, start
+     * from 1
+     */
+    private int previousPageRowIndex = -1;
+
+    /**
+     * cached ResultSet for paging in memory
+     */
+    private CachedRowSet cachedResultSet = null;
 
     private String tableName;
 
@@ -208,7 +223,7 @@
     public void acceptChanges(Connection con) throws SyncProviderException {
         if (isCursorOnInsert) {
             // rowset.11=Illegal operation on an insert row
-            throw new SyncProviderException(Messages.getString("rowset.11"));
+            throw new SyncProviderException(Messages.getString("rowset.11")); //$NON-NLS-1$
         }
 
         try {
@@ -217,13 +232,16 @@
             rowSetWriter.setConnection(con);
             int beforeWriteIndex = currentRowIndex;
             boolean isShowDeleted = getShowDeleted();
+            // writer use next navigate rowset, so must make all rows visible
             setShowDeleted(true);
 
             rowSetWriter.writeData(this);
 
+            // must reset curosr before reset showDeleted
             absolute(beforeWriteIndex);
             setShowDeleted(isShowDeleted);
 
+            // record to the next visible row index
             int index = getRow();
             if (index == 0) {
                 next();
@@ -258,6 +276,9 @@
                 }
             }
 
+            deletedRowCount = 0;
+
+            // move cursor
             if (index > rows.size()) {
                 afterLast();
             } else if (index <= 0) {
@@ -410,7 +431,7 @@
         output.currentRow = null;
         output.currentRowIndex = 0;
         output.insertRow = null;
-        output.pageNumber = 1;
+        output.nextPageRowIndex = -1;
         output.rememberedCursorPosition = 0;
         output.rows = new ArrayList<CachedRow>();
         output.sqlwarn = null;
@@ -432,7 +453,7 @@
     }
 
     public void execute(Connection conn) throws SQLException {
-        // ensure the getConnection can works!
+        // TODO ensure the getConnection can works!
         String localCommand = getCommand();
         if (localCommand == null || getParams() == null) {
             // TODO add error messages
@@ -447,6 +468,16 @@
         if (ps.execute()) {
             doPopulate(ps.getResultSet(), true);
         }
+
+        if (getPageSize() != 0) {
+            nextPageRowIndex = rows.size() + 1;
+            previousPageRowIndex = 0;
+            cachedResultSet = null;
+        } else {
+            previousPageRowIndex = -1;
+            nextPageRowIndex = -1;
+            cachedResultSet = null;
+        }
     }
 
     public int[] getKeyColumns() throws SQLException {
@@ -490,27 +521,102 @@
     }
 
     /**
-     * TODO refill the cachedrowset with pagesize, and the previous rowset was
-     * replaced
+     * TODO refactor paging
      */
     public boolean nextPage() throws SQLException {
-        pageNumber++;
+        if (rows == null || nextPageRowIndex == -1 || getPageSize() == 0) {
+            // TODO load message from resource file
+            throw new SQLException(
+                    "Using execute() method populate data before calling");
+        }
+
+        if (cachedResultSet == null) {
+            // TODO ensure the getConnection can works!
+            String localCommand = getCommand();
+            if (localCommand == null || getParams() == null) {
+                // TODO add error messages
+                throw new SQLException();
+            }
+
+            PreparedStatement ps = getConnection().prepareStatement(
+                    localCommand);
+            Object[] params = getParams();
+            for (int i = 0; i < params.length; i++) {
+                ps.setObject(i + 1, params[i]);
+            }
+            if (ps.execute()) {
+                ResultSet rs = ps.getResultSet();
+                int index = 1;
+                while (rs.next() && index < nextPageRowIndex - 1) {
+                    index++;
+                }
+
+                doPopulate(rs, true);
+
+                if (rows.size() == 0) {
+                    return false;
+                }
+                previousPageRowIndex = nextPageRowIndex - 1;
+                nextPageRowIndex += rows.size();
+                return true;
+
+            }
+            return false;
+
+        }
+
+        if (cachedResultSet.absolute(nextPageRowIndex)) {
+            cachedResultSet.previous();
+            doPopulate(cachedResultSet, true);
+
+            if (rows.size() == 0) {
+                return false;
+            }
+            previousPageRowIndex = nextPageRowIndex - 1;
+            nextPageRowIndex += rows.size();
+            return true;
+        }
         return false;
+
     }
 
     public void populate(ResultSet rs) throws SQLException {
         doPopulate(rs, false);
+        previousPageRowIndex = -1;
+        nextPageRowIndex = -1;
+        cachedResultSet = null;
     }
 
     public void populate(ResultSet rs, int startRow) throws SQLException {
+        if (rs == null) {
+            // TODO add error messages
+            throw new SQLException();
+        }
+
         if (startRow == 1) {
             rs.beforeFirst();
+            // TODO use next move
         } else if (startRow <= 0 || !rs.absolute(startRow - 1)) {
             // rowset.7=Not a valid cursor
             throw new SQLException(Messages.getString("rowset.7")); //$NON-NLS-1$
         }
 
-        doPopulate(rs, true);
+        // paging in memory
+        if (getPageSize() != 0) {
+            cachedResultSet = new CachedRowSetImpl();
+            cachedResultSet.setMaxRows(getMaxRows());
+            cachedResultSet.populate(rs, startRow);
+            doPopulate(cachedResultSet, true);
+
+            nextPageRowIndex = rows.size() + 1;
+            previousPageRowIndex = 0;
+
+        } else {
+            doPopulate(rs, true);
+            previousPageRowIndex = -1;
+            nextPageRowIndex = -1;
+            cachedResultSet = null;
+        }
     }
 
     private void doPopulate(ResultSet rs, boolean isPaging) throws SQLException {
@@ -596,8 +702,94 @@
     }
 
     public boolean previousPage() throws SQLException {
-        // TODO implement it
-        return false;
+        if (rows == null || previousPageRowIndex == -1 || getPageSize() == 0) {
+            // TODO load message from resource file
+            throw new SQLException(
+                    "Using execute() method populate data before calling");
+        }
+
+        if (previousPageRowIndex == 0) {
+            return false;
+        }
+
+        if (cachedResultSet == null) {
+            // TODO ensure the getConnection can works!
+            String localCommand = getCommand();
+            if (localCommand == null || getParams() == null) {
+                // TODO add error messages
+                throw new SQLException();
+            }
+
+            PreparedStatement ps = getConnection().prepareStatement(
+                    localCommand);
+            Object[] params = getParams();
+            for (int i = 0; i < params.length; i++)
+                ps.setObject(i + 1, params[i]);
+
+            if (ps.execute()) {
+                ResultSet rs = ps.getResultSet();
+                int startIndex = previousPageRowIndex - getPageSize() + 1;
+
+                if (startIndex <= 0) {
+                    startIndex = 1;
+                }
+
+                int index = 0;
+                while (index < startIndex - 1) {
+                    if (!rs.next()) {
+                        break;
+                    }
+                    index++;
+                }
+
+                int prePageSize = getPageSize();
+                if (prePageSize != 0
+                        && previousPageRowIndex - startIndex + 1 != prePageSize)
{
+                    setPageSize(previousPageRowIndex - startIndex + 1);
+                }
+                doPopulate(rs, true);
+
+                setPageSize(prePageSize);
+
+                if (rows.size() == 0) {
+                    return false;
+                }
+                nextPageRowIndex = previousPageRowIndex + 1;
+                previousPageRowIndex = startIndex - 1;
+                return true;
+            }
+
+            return false;
+        }
+
+        int startIndex = previousPageRowIndex - getPageSize() + 1;
+
+        if (startIndex <= 0) {
+            startIndex = 1;
+        }
+
+        if (!cachedResultSet.absolute(startIndex)) {
+            return false;
+        }
+
+        cachedResultSet.previous();
+
+        int prePageSize = getPageSize();
+        if (prePageSize != 0
+                && previousPageRowIndex - startIndex + 1 != prePageSize) {
+            setPageSize(previousPageRowIndex - startIndex + 1);
+        }
+
+        doPopulate(cachedResultSet, true);
+
+        setPageSize(prePageSize);
+
+        if (rows.size() == 0) {
+            return false;
+        }
+        nextPageRowIndex = previousPageRowIndex + 1;
+        previousPageRowIndex = startIndex - 1;
+        return true;
     }
 
     public void release() throws SQLException {
@@ -620,6 +812,7 @@
         rows.removeAll(insertedRows);
         insertRow = null;
         isCursorOnInsert = false;
+        deletedRowCount = 0;
 
         first();
 
@@ -1191,7 +1384,7 @@
 
     public int getRow() throws SQLException {
         // FIXME need more tests
-        if (currentRow == null || rows == null) {
+        if (currentRow == null || rows == null || isCursorOnInsert) {
             return 0;
         }
 
@@ -1459,12 +1652,27 @@
     }
 
     public boolean relative(int moveRows) throws SQLException {
-        if (currentRow == null) {
-            // TODO add error message
-            throw new SQLException();
-        }
-
+        checkValidRow();
+        // TODO use more effective way to move cursor
         int index = getRow() + moveRows;
+
+        if (isCursorOnInsert || currentRow.isDelete()) {
+            if (moveRows > 0) {
+                if (next()) {
+                    index = getRow() + moveRows - 1;
+                } else {
+                    return false;
+                }
+            }
+
+            if (moveRows < 0) {
+                if (previous()) {
+                    index = getRow() + moveRows + 1;
+                } else {
+                    return false;
+                }
+            }
+        }
 
         if (index <= 0) {
             beforeFirst();

Modified: 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=635493&r1=635492&r2=635493&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetDeleteTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetDeleteTest.java
Mon Mar 10 02:04:01 2008
@@ -89,17 +89,17 @@
          */
         assertTrue(noInitialCrset.last());
         assertTrue(noInitialCrset.previous());
-        assertEquals(9, noInitialCrset.getInt(1));
+        assertEquals(8, noInitialCrset.getInt(1));
         noInitialCrset.deleteRow();
         assertTrue(noInitialCrset.last());
-        assertEquals(10, noInitialCrset.getInt(1));
+        assertEquals(9, 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));
+            assertEquals(9, noInitialCrset.getInt(1));
             noInitialCrset.acceptChanges(conn);
         } else {
             assertTrue(noInitialCrset.isAfterLast());
@@ -513,6 +513,66 @@
             }
         }
         assertEquals(3, index);
+    }
+
+    public void testAbsolute() throws Exception {
+        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());
+
+        assertTrue(noInitialCrset.previous());
+        assertTrue(noInitialCrset.previous());
+
+        assertEquals(1, noInitialCrset.getInt(1));
+
+        noInitialCrset.absolute(3);
+        assertEquals(4, noInitialCrset.getInt(1));
+    }
+
+    public void testRelative() throws Exception {
+        insertMoreData(10);
+        
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        crset = newNoInitialInstance();
+        crset.populate(rs);
+        assertFalse(crset.getShowDeleted());
+
+        // delete row 6
+        assertTrue(crset.absolute(6));
+        assertFalse(crset.rowDeleted());
+        crset.deleteRow();
+        assertTrue(crset.rowDeleted());
+
+        // delete the third row
+        assertTrue(crset.absolute(3));
+        assertFalse(crset.rowDeleted());
+        assertEquals(3, crset.getInt(1));
+        crset.deleteRow();
+        assertTrue(crset.rowDeleted());
+
+        assertTrue(crset.relative(3));
+        assertEquals(7, crset.getInt(1));
+
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+        crset = newNoInitialInstance();
+        crset.populate(rs);
+        assertFalse(crset.getShowDeleted());
+
+        // delete the third row
+        assertTrue(crset.absolute(1));
+        assertEquals(1, crset.getInt(1));
+        assertFalse(crset.rowDeleted());
+        crset.deleteRow();
+        assertTrue(crset.rowDeleted());
+
+        assertFalse(crset.relative(-1));
+        assertTrue(crset.isBeforeFirst());
     }
 
     public void testShowDeleted_True() throws Exception {

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=635493&r1=635492&r2=635493&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
Mon Mar 10 02:04:01 2008
@@ -900,6 +900,14 @@
 
         int i = 0;
 
+        /*
+         * TODO In RI there are before first page and after last page, according
+         * spec, there shouldn't be, Harmony follow spec
+         */
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            i = 2;
+        }
+
         crset.beforeFirst();
         while (crset.nextPage()) {
             while (crset.next()) {
@@ -907,6 +915,14 @@
             }
         }
 
+        /*
+         * TODO In RI there are before first page and after last page, according
+         * spec, there shouldn't be, Harmony follow spec
+         */
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            i = 2;
+        }
+
         while (crset.previousPage()) {
             crset.afterLast();
             while (crset.previous()) {
@@ -939,7 +955,7 @@
             cursorIndex++;
         }
         // setMaxRows no effect, we follow ri
-        assertEquals(20, cursorIndex);
+        assertEquals(19, cursorIndex);
 
         /*
          * The pageSize won't work when call method populate(ResultSet) without
@@ -967,7 +983,7 @@
          * CachedRowSet's pageSize is 5. However, the pageSize doesn't work in
          * RI. The CachedRowSet gets all the data from ResultSet. We follow ri.
          */
-        assertEquals(20, cursorIndex);
+        assertEquals(19, cursorIndex);
 
         noInitialCrset = newNoInitialInstance();
         rs = st.executeQuery("select * from USER_INFO");
@@ -981,7 +997,7 @@
         while (noInitialCrset.next()) {
             cursorIndex++;
         }
-        assertEquals(18, cursorIndex);
+        assertEquals(17, cursorIndex);
     }
 
     public void testPopulate_LResultSet_I() throws Exception {
@@ -997,6 +1013,15 @@
             // expected
         }
 
+        try {
+            noInitialCrset.populate(null, 1);
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected, we follow spec
+        } catch (NullPointerException e) {
+            // ri throw NullPointerException
+        }
+
         // create a scrollable and updatable ResultSet
         st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                 ResultSet.CONCUR_UPDATABLE);
@@ -1069,7 +1094,7 @@
                 assertEquals(cursorIndex, noInitialCrset.getInt(1));
             }
         }
-        assertEquals(20, cursorIndex);
+        assertEquals(19, cursorIndex);
 
         try {
             noInitialCrset.populate(crsetCopy, 0);

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetInsertTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetInsertTest.java?rev=635493&r1=635492&r2=635493&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetInsertTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetInsertTest.java
Mon Mar 10 02:04:01 2008
@@ -112,6 +112,9 @@
         noInitialCrset.populate(rs);
         assertTrue(noInitialCrset.absolute(3));
         noInitialCrset.moveToInsertRow();
+        
+        assertEquals(0, noInitialCrset.getRow());
+        
         try {
             // call insertRow() without call any update method
             noInitialCrset.insertRow();
@@ -123,7 +126,11 @@
         assertEquals(10, noInitialCrset.getInt(1));
         noInitialCrset.updateString(2, "insert10");
         assertEquals("insert10", noInitialCrset.getString(2));
+
+        assertEquals(0, noInitialCrset.getRow());
         noInitialCrset.insertRow();
+        assertEquals(0, noInitialCrset.getRow());
+        
         noInitialCrset.moveToCurrentRow();
         assertEquals(3, noInitialCrset.getInt(1));
     }

Added: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetPagingTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetPagingTest.java?rev=635493&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetPagingTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetPagingTest.java
Mon Mar 10 02:04:01 2008
@@ -0,0 +1,537 @@
+/* 
+ * 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.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * TODO In RI, nextPage is similar next, first call nextPage move to first page,
+ * while in spec, first call nextPage should move to second page, Harmony follow
+ * spec.
+ * 
+ */
+public class CachedRowSetPagingTest extends CachedRowSetTestCase {
+
+    public void testPagingInMemory() throws Exception {
+        insertMoreData(10);
+        crset = newNoInitialInstance();
+
+        st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
+                ResultSet.CONCUR_UPDATABLE);
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+
+        // the max rows load into memory
+        crset.setMaxRows(5);
+        crset.setPageSize(3);
+
+        crset.populate(rs, 1);
+
+        if (!"true".equals(System.getProperty("Testing Harmony"))) {
+            // RI need nextPage one more time
+            assertTrue(crset.nextPage());
+        }
+
+        for (int i = 1; i <= 3; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertTrue(crset.nextPage());
+
+        crset.beforeFirst();
+
+        for (int i = 4; i <= 5; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+
+        assertFalse(crset.nextPage());
+
+    }
+
+    public void testPagingInMemory2() throws Exception {
+        insertMoreData(4);
+
+        crset = newNoInitialInstance();
+
+        st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
+                ResultSet.CONCUR_UPDATABLE);
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+
+        crset.setPageSize(3);
+
+        crset.populate(rs, 1);
+
+        if (!"true".equals(System.getProperty("Testing Harmony"))) {
+            // RI need nextPage one more time
+            assertTrue(crset.nextPage());
+        }
+
+        for (int i = 1; i <= 3; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+
+        crset.setPageSize(2);
+        assertTrue(crset.nextPage());
+
+        assertTrue(crset.isBeforeFirst());
+
+        for (int i = 4; i <= 5; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+
+        crset.setPageSize(5);
+        assertTrue(crset.nextPage());
+
+        assertTrue(crset.isBeforeFirst());
+
+        for (int i = 6; i <= 8; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+        assertFalse(crset.nextPage());
+
+        assertTrue(crset.previousPage());
+
+        if (!"true".equals(System.getProperty("Testing Harmony"))) {
+            // RI need previousPage one more time
+            assertTrue(crset.previousPage());
+        }
+
+        assertTrue(crset.isBeforeFirst());
+        for (int i = 1; i <= 5; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+
+        assertFalse(crset.previousPage());
+
+    }
+
+    public void testPagingInMemory3() throws Exception {
+        insertMoreData(10);
+
+        crset = newNoInitialInstance();
+
+        st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
+                ResultSet.CONCUR_UPDATABLE);
+        rs = st.executeQuery("SELECT * FROM USER_INFO");
+
+        crset.setPageSize(4);
+
+        crset.populate(rs, 1);
+
+        if (!"true".equals(System.getProperty("Testing Harmony"))) {
+            // RI need nextPage one more time
+            assertTrue(crset.nextPage());
+        }
+
+        for (int i = 1; i <= 4; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+        assertFalse(crset.previousPage());
+        assertFalse(crset.previousPage());
+
+        assertTrue(crset.nextPage());
+
+        assertTrue(crset.isBeforeFirst());
+        for (int i = 5; i <= 8; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+
+        crset.setPageSize(3);
+        assertTrue(crset.previousPage());
+
+        assertTrue(crset.isBeforeFirst());
+
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            for (int i = 2; i <= 4; ++i) {
+                assertTrue(crset.next());
+                assertEquals(i, crset.getInt(1));
+            }
+
+            assertFalse(crset.next());
+            assertTrue(crset.isAfterLast());
+
+            assertTrue(crset.previousPage());
+
+            assertTrue(crset.isBeforeFirst());
+
+            assertTrue(crset.next());
+            assertEquals(1, crset.getInt(1));
+            assertFalse(crset.next());
+            assertTrue(crset.isAfterLast());
+
+            assertFalse(crset.previousPage());
+            assertFalse(crset.previousPage());
+
+            assertTrue(crset.nextPage());
+
+            assertTrue(crset.isBeforeFirst());
+            for (int i = 2; i <= 4; ++i) {
+                assertTrue(crset.next());
+                assertEquals(i, crset.getInt(1));
+            }
+
+            assertFalse(crset.next());
+            assertTrue(crset.isAfterLast());
+
+            assertTrue(crset.nextPage());
+
+            assertTrue(crset.isBeforeFirst());
+            for (int i = 5; i <= 7; ++i) {
+                assertTrue(crset.next());
+                assertEquals(i, crset.getInt(1));
+            }
+
+            assertFalse(crset.next());
+            assertTrue(crset.isAfterLast());
+
+            crset.setPageSize(4);
+            assertEquals(4, crset.getPageSize());
+
+            assertTrue(crset.previousPage());
+
+            assertTrue(crset.isBeforeFirst());
+            for (int i = 1; i <= 4; ++i) {
+                assertTrue(crset.next());
+                assertEquals(i, crset.getInt(1));
+            }
+
+            assertFalse(crset.next());
+            assertTrue(crset.isAfterLast());
+
+            assertFalse(crset.previousPage());
+            assertFalse(crset.previousPage());
+
+        } else {
+            // seems RI lost one row
+            assertTrue(crset.next());
+            assertEquals(1, crset.getInt(1));
+            assertTrue(crset.next());
+            assertEquals(2, crset.getInt(1));
+            assertTrue(crset.next());
+            assertEquals(3, crset.getInt(1));
+            assertFalse(crset.next());
+        }
+
+    }
+
+    public void testException() throws Exception {
+        insertMoreData(4);
+
+        crset = newNoInitialInstance();
+
+        try {
+            crset.nextPage();
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected, Populate data before calling
+        }
+
+        crset.setCommand("select * from USER_INFO");
+        crset.setUrl(DERBY_URL);
+
+        crset.setPageSize(0);
+        crset.execute();
+
+        try {
+            crset.nextPage();
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected, Populate data before calling
+        }
+    }
+
+    public void testTravel() throws Exception {
+        insertMoreData(4);
+
+        crset = newNoInitialInstance();
+
+        crset.setCommand("select * from USER_INFO");
+        crset.setUrl(DERBY_URL);
+
+        crset.setPageSize(3);
+        crset.execute();
+
+        assertEquals("select * from USER_INFO", crset.getCommand());
+
+        assertFalse(crset.previousPage());
+
+        for (int i = 1; i <= 3; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+
+        if (!"true".equals(System.getProperty("Testing Harmony"))) {
+            // RI need nextPage one more time
+            assertTrue(crset.nextPage());
+        }
+
+        int index = 4;
+        while (crset.nextPage()) {
+            while (crset.next()) {
+                assertEquals(index, crset.getInt(1));
+                index++;
+            }
+        }
+        assertEquals(9, index);
+
+        crset = newNoInitialInstance();
+        crset.setCommand("select * from USER_INFO");
+        crset.setUrl(DERBY_URL);
+
+        crset.setPageSize(3);
+        crset.execute();
+
+        assertTrue(crset.next());
+        assertEquals(1, crset.getInt(1));
+
+        assertTrue(crset.nextPage());
+        if (!"true".equals(System.getProperty("Testing Harmony"))) {
+            // RI need nextPage one more time
+            assertTrue(crset.nextPage());
+        }
+
+        assertTrue(crset.next());
+
+        assertEquals(4, crset.getInt(1));
+
+        assertTrue(crset.nextPage());
+
+        assertTrue(crset.next());
+        assertEquals(7, crset.getInt(1));
+
+        assertFalse(crset.nextPage());
+
+    }
+
+    public void testTravel2() throws Exception {
+        insertMoreData(4);
+
+        crset = newNoInitialInstance();
+
+        crset.setCommand("select * from USER_INFO");
+        crset.setUrl(DERBY_URL);
+
+        crset.setPageSize(3);
+        crset.execute();
+
+        if (!"true".equals(System.getProperty("Testing Harmony"))) {
+            // RI need nextPage one more time
+            assertTrue(crset.nextPage());
+        }
+
+        for (int i = 1; i <= 3; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+
+        crset.setPageSize(2);
+        assertTrue(crset.nextPage());
+
+        assertTrue(crset.isBeforeFirst());
+
+        for (int i = 4; i <= 5; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+
+        crset.setPageSize(5);
+        assertTrue(crset.nextPage());
+
+        assertTrue(crset.isBeforeFirst());
+
+        for (int i = 6; i <= 8; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+        assertFalse(crset.nextPage());
+
+        assertTrue(crset.previousPage());
+
+        if (!"true".equals(System.getProperty("Testing Harmony"))) {
+            // RI need previousPage one more time
+            assertTrue(crset.previousPage());
+        }
+
+        assertTrue(crset.isBeforeFirst());
+        for (int i = 1; i <= 5; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+
+        assertFalse(crset.previousPage());
+
+    }
+
+    public void testTravel3() throws Exception {
+        insertMoreData(10);
+
+        crset = newNoInitialInstance();
+
+        crset.setCommand("select * from USER_INFO");
+        crset.setUrl(DERBY_URL);
+
+        crset.setPageSize(4);
+        crset.execute();
+
+        if (!"true".equals(System.getProperty("Testing Harmony"))) {
+            // RI need nextPage one more time
+            assertTrue(crset.nextPage());
+        }
+
+        for (int i = 1; i <= 4; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+        assertFalse(crset.previousPage());
+        assertFalse(crset.previousPage());
+
+        assertTrue(crset.nextPage());
+
+        assertTrue(crset.isBeforeFirst());
+        for (int i = 5; i <= 8; ++i) {
+            assertTrue(crset.next());
+            assertEquals(i, crset.getInt(1));
+        }
+
+        assertFalse(crset.next());
+        assertTrue(crset.isAfterLast());
+
+        crset.setPageSize(3);
+        assertTrue(crset.previousPage());
+
+        assertTrue(crset.isBeforeFirst());
+
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            for (int i = 2; i <= 4; ++i) {
+                assertTrue(crset.next());
+                assertEquals(i, crset.getInt(1));
+            }
+
+            assertFalse(crset.next());
+            assertTrue(crset.isAfterLast());
+
+            assertTrue(crset.previousPage());
+
+            assertTrue(crset.isBeforeFirst());
+
+            assertTrue(crset.next());
+            assertEquals(1, crset.getInt(1));
+            assertFalse(crset.next());
+            assertTrue(crset.isAfterLast());
+
+            assertFalse(crset.previousPage());
+            assertFalse(crset.previousPage());
+
+            assertTrue(crset.nextPage());
+
+            assertTrue(crset.isBeforeFirst());
+            for (int i = 2; i <= 4; ++i) {
+                assertTrue(crset.next());
+                assertEquals(i, crset.getInt(1));
+            }
+
+            assertFalse(crset.next());
+            assertTrue(crset.isAfterLast());
+
+            assertTrue(crset.nextPage());
+
+            assertTrue(crset.isBeforeFirst());
+            for (int i = 5; i <= 7; ++i) {
+                assertTrue(crset.next());
+                assertEquals(i, crset.getInt(1));
+            }
+
+            assertFalse(crset.next());
+            assertTrue(crset.isAfterLast());
+
+            crset.setPageSize(4);
+            assertEquals(4, crset.getPageSize());
+
+            assertTrue(crset.previousPage());
+
+            assertTrue(crset.isBeforeFirst());
+            for (int i = 1; i <= 4; ++i) {
+                assertTrue(crset.next());
+                assertEquals(i, crset.getInt(1));
+            }
+
+            assertFalse(crset.next());
+            assertTrue(crset.isAfterLast());
+
+            assertFalse(crset.previousPage());
+            assertFalse(crset.previousPage());
+
+        } else {
+            // TODO seems RI lost one row
+            assertTrue(crset.next());
+            assertEquals(1, crset.getInt(1));
+            assertTrue(crset.next());
+            assertEquals(2, crset.getInt(1));
+            assertTrue(crset.next());
+            assertEquals(3, crset.getInt(1));
+            assertFalse(crset.next());
+        }
+
+    }
+}

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

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetTestCase.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetTestCase.java?rev=635493&r1=635492&r2=635493&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetTestCase.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetTestCase.java
Mon Mar 10 02:04:01 2008
@@ -174,7 +174,7 @@
         PreparedStatement preStmt = conn.prepareStatement(insertSQL);
 
         // insert 15 rows
-        for (int i = DEFAULT_ROW_COUNT + 1; i <= DEFAULT_ROW_COUNT + rows + 1; i++) {
+        for (int i = DEFAULT_ROW_COUNT + 1; i < DEFAULT_ROW_COUNT + rows + 1; i++) {
             preStmt.setInt(1, i);
             preStmt.setString(2, "test" + i);
             preStmt.setLong(3, 444423L);



Mime
View raw message