harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r598204 - in /harmony/enhanced/classlib/trunk/modules/sql/src: main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java
Date Mon, 26 Nov 2007 10:17:51 GMT
Author: tonywu
Date: Mon Nov 26 02:17:49 2007
New Revision: 598204

URL: http://svn.apache.org/viewvc?rev=598204&view=rev
Log:
Apply patch HARMONY-5187 ([classlib][sql] implement cursor moving relative methods for CachedRowSetImpl)

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

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetImpl.java?rev=598204&r1=598203&r2=598204&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 Nov 26 02:17:49 2007
@@ -597,42 +597,60 @@
     }
 
     public boolean absolute(int row) throws SQLException {
-        if (rows.size() == 0) {
+        return doAbsolute(row, true);
+    }
+
+    /**
+     * internal implement of absolute
+     * 
+     * @param row
+     *            index of row cursor to move
+     * @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 {
+        if (rows == null || rows.size() == 0) {
             return false;
         }
-        if (getType() == ResultSet.TYPE_FORWARD_ONLY) {
-            // TODO add error messages
-            throw new SQLException();
+
+        if (checkType && getType() == ResultSet.TYPE_FORWARD_ONLY) {
+            // rowset.8=The Result Set Type is TYPE_FORWARD_ONLY
+            throw new SQLException(Messages.getString("rowset.8"));
         }
+
         if (row < 0) {
             row = rows.size() + row + 1;
-        } else if (row == 0) {
-            currentRowIndex = row;
+        }
+
+        if (row <= 0) {
+            currentRowIndex = 0;
             currentRow = null;
-            return true;
+            return false;
+        }
+
+        if (row > rows.size()) {
+            currentRowIndex = rows.size() + 1;
+            currentRow = null;
+            return false;
         }
 
         currentRowIndex = row;
-        currentRow = (CachedRow) rows.get(currentRowIndex - 1);
+        currentRow = rows.get(currentRowIndex - 1);
         return true;
     }
 
     public void afterLast() throws SQLException {
-        if (getType() == TYPE_FORWARD_ONLY) {
-            // rowset.8=The Result Set Type is TYPE_FORWARD_ONLY
-            throw new SQLException(Messages.getString("rowset.8"));
+        if (rows == null) {
+            return;
         }
-        currentRowIndex = rows.size() + 1;
-        currentRow = null;
+
+        doAbsolute(rows.size() + 1, true);
     }
 
     public void beforeFirst() throws SQLException {
-        if (getType() == TYPE_FORWARD_ONLY) {
-            // rowset.8=The Result Set Type is TYPE_FORWARD_ONLY
-            throw new SQLException(Messages.getString("rowset.8"));
-        }
-        currentRowIndex = 0;
-        currentRow = null;
+        doAbsolute(0, true);
     }
 
     public void cancelRowUpdates() throws SQLException {
@@ -672,7 +690,7 @@
     }
 
     public boolean first() throws SQLException {
-        return absolute(1);
+        return doAbsolute(1, true);
     }
 
     public Array getArray(int columnIndex) throws SQLException {
@@ -984,23 +1002,35 @@
     }
 
     public boolean isAfterLast() throws SQLException {
-        throw new NotImplementedException();
+        if (rows == null || rows.size() == 0) {
+            return false;
+        }
+
+        return currentRowIndex > rows.size();
     }
 
     public boolean isBeforeFirst() throws SQLException {
-        throw new NotImplementedException();
+        if (rows == null || rows.size() == 0) {
+            return false;
+        }
+
+        return currentRowIndex == 0;
     }
 
     public boolean isFirst() throws SQLException {
-        throw new NotImplementedException();
+        return currentRowIndex == 1;
     }
 
     public boolean isLast() throws SQLException {
-        throw new NotImplementedException();
+        if (rows == null || rows.size() == 0) {
+            return false;
+        }
+
+        return currentRowIndex == rows.size();
     }
 
     public boolean last() throws SQLException {
-        return absolute(-1);
+        return doAbsolute(-1, true);
     }
 
     public void moveToCurrentRow() throws SQLException {
@@ -1020,29 +1050,40 @@
     }
 
     public boolean next() throws SQLException {
-        currentRowIndex++;
-        if (rows.size() < currentRowIndex) {
-            return false;
-        }
-        currentRow = rows.get(currentRowIndex - 1);
-        return true;
+        /*
+         * spec next() is identical with relative(1), but they can't:
+         * 
+         * next() doesn't check TYPE_FORWARD_ONLY property, relative(1) does.
+         */
+        return doAbsolute(currentRowIndex + 1, false);
     }
 
     public boolean previous() throws SQLException {
-        currentRowIndex--;
-        if (rows.size() < currentRowIndex) {
-            return false;
-        }
-        currentRow = rows.get(currentRowIndex - 1);
-        return true;
+        return doAbsolute(currentRowIndex - 1, true);
     }
 
     public void refreshRow() throws SQLException {
         throw new NotImplementedException();
     }
 
-    public boolean relative(int rows) throws SQLException {
-        throw new NotImplementedException();
+    public boolean relative(int moveRows) throws SQLException {
+        if (currentRow == null) {
+            // TODO add error message
+            throw new SQLException();
+        }
+
+        int index = getRow() + moveRows;
+        if (index <= 0) {
+            beforeFirst();
+            return false;
+        }
+
+        if (index > rows.size()) {
+            afterLast();
+            return false;
+        }
+
+        return doAbsolute(index, true);
     }
 
     public boolean rowDeleted() throws SQLException {

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.java?rev=598204&r1=598203&r2=598204&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 Nov 26 02:17:49 2007
@@ -1144,6 +1144,199 @@
 
     }
 
+    public void testRelative() throws Exception {
+        /*
+         * ri throw SQLException, but spec say relative(1) is identical to next
+         */
+        try {
+            crset.relative(1);
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        assertTrue(crset.next());
+        assertEquals("hermit", crset.getString(2));
+
+        assertTrue(crset.relative(2));
+        assertEquals("test3", crset.getString(2));
+
+        assertTrue(crset.relative(-1));
+        assertEquals("test", crset.getString(2));
+
+        assertTrue(crset.relative(0));
+        assertEquals("test", crset.getString(2));
+
+        assertFalse(crset.relative(-5));
+        assertEquals(0, crset.getRow());
+
+        assertTrue(crset.next());
+        assertEquals("hermit", crset.getString(2));
+        assertTrue(crset.relative(3));
+        assertEquals("test4", crset.getString(2));
+
+        assertFalse(crset.relative(3));
+        assertEquals(0, crset.getRow());
+
+        assertTrue(crset.isAfterLast());
+        assertTrue(crset.previous());
+
+        // non-bug different
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            assertEquals(DEFAULT_ROW_COUNT, crset.getRow());
+            assertEquals("test4", crset.getString(2));
+        }
+    }
+
+    public void testAbsolute() throws Exception {
+        // non-bug different
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            assertFalse(crset.absolute(0));
+        }
+        
+        assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, crset.getType());
+        assertTrue(crset.absolute(1));
+        assertEquals(1, crset.getInt(1));
+        assertTrue(crset.absolute(4));
+        assertEquals(4, crset.getInt(1));
+        assertTrue(crset.absolute(3));
+        assertEquals(3, crset.getInt(1));
+
+        // when position the cursor beyond the first/last row in the result set
+        assertFalse(crset.absolute(10));
+        assertTrue(crset.isAfterLast());
+        assertTrue(crset.previous());
+        assertFalse(crset.absolute(-10));
+        assertTrue(crset.isBeforeFirst());
+        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.
+         */
+        if (System.getProperty("Testing Harmony") == "true") {
+            assertTrue(crset.absolute(-1));
+            assertEquals(4, crset.getInt(1));
+            assertTrue(crset.absolute(-3));
+            assertEquals(2, crset.getInt(1));
+            assertFalse(crset.absolute(-5));
+        }
+
+        crset.setType(ResultSet.TYPE_FORWARD_ONLY);
+        try {
+            crset.absolute(1);
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        try {
+            crset.absolute(-1);
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+    }
+
+    public void testNextAndPrevious() throws Exception {
+        /*
+         * This method is also used to test isBeforeFirst(), isAfterLast(),
+         * isFirst(),isLast()
+         */
+        // Test for next()
+        assertTrue(crset.isBeforeFirst());
+        assertFalse(crset.isAfterLast());
+        assertFalse(crset.isFirst());
+        assertFalse(crset.isLast());
+        assertTrue(crset.next());
+        assertTrue(crset.isFirst());
+        assertEquals(1, crset.getInt(1));
+
+        assertTrue(crset.next());
+        assertFalse(crset.isFirst());
+        assertTrue(crset.next());
+        assertTrue(crset.next());
+        assertTrue(crset.isLast());
+        assertEquals(4, crset.getInt(1));
+        assertFalse(crset.next());
+        // assertFalse(crset.next());
+        assertFalse(crset.isBeforeFirst());
+        assertTrue(crset.isAfterLast());
+
+        // Test for previous()
+        assertTrue(crset.previous());
+        assertEquals(4, crset.getInt(1));
+        assertTrue(crset.isLast());
+        assertTrue(crset.previous());
+        assertTrue(crset.previous());
+        assertTrue(crset.previous());
+        assertEquals(1, crset.getInt(1));
+        assertTrue(crset.isFirst());
+        assertFalse(crset.previous());
+        assertTrue(crset.isBeforeFirst());
+        // assertFalse(crset.previous());
+
+        assertTrue(crset.next());
+        assertTrue(crset.next());
+        assertEquals(2, crset.getInt(1));
+
+        // Test for previous()'s Exception
+        assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, crset.getType());
+        crset.setType(ResultSet.TYPE_FORWARD_ONLY);
+        assertEquals(ResultSet.TYPE_FORWARD_ONLY, crset.getType());
+        try {
+            crset.previous();
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+    }
+
+    public void testFirstAndLast() throws Exception {
+        /*
+         * This method is used to test afterLast(), beforeFist(), first(),
+         * last()
+         */
+        assertTrue(crset.isBeforeFirst());
+        assertTrue(crset.first());
+        assertTrue(crset.isFirst());
+        assertFalse(crset.isBeforeFirst());
+        crset.beforeFirst();
+        assertTrue(crset.isBeforeFirst());
+        assertTrue(crset.last());
+        assertTrue(crset.isLast());
+
+        assertTrue(crset.first());
+        assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, crset.getType());
+        crset.setType(ResultSet.TYPE_FORWARD_ONLY);
+        assertEquals(ResultSet.TYPE_FORWARD_ONLY, crset.getType());
+
+        try {
+            crset.beforeFirst();
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+
+
+        try {
+            crset.first();
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        try {
+            crset.last();
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        assertTrue(crset.isFirst());
+    }
+
     public class Listener implements RowSetListener, Cloneable {
 
         private String tag = null;



Mime
View raw message