harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r599764 - in /harmony/enhanced/classlib/trunk/modules/sql/src: main/java/org/apache/harmony/sql/internal/rowset/SyncResolverImpl.java test/java/org/apache/harmony/sql/tests/internal/rowset/SyncResolverTest.java
Date Fri, 30 Nov 2007 10:01:32 GMT
Author: tonywu
Date: Fri Nov 30 02:01:30 2007
New Revision: 599764

URL: http://svn.apache.org/viewvc?rev=599764&view=rev
Log:
Apply patch HARMONY-5228 ([classlib][sql] implement SyncResolverImpl)

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

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/SyncResolverImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/SyncResolverImpl.java?rev=599764&r1=599763&r2=599764&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/SyncResolverImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/SyncResolverImpl.java
Fri Nov 30 02:01:30 2007
@@ -31,13 +31,17 @@
 import java.sql.Statement;
 import java.sql.Time;
 import java.sql.Timestamp;
+import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.List;
 import java.util.Map;
 
+import javax.sql.RowSetMetaData;
 import javax.sql.rowset.BaseRowSet;
 import javax.sql.rowset.spi.SyncResolver;
 
 import org.apache.harmony.luni.util.NotImplementedException;
+import org.apache.harmony.sql.internal.nls.Messages;
 
 /**
  * TODO seems RI's implementation is not complete, now we follow RI throw
@@ -50,29 +54,88 @@
 
     private static final long serialVersionUID = 4964648528867743289L;
 
+    private List<ConflictedRow> conflictRows;
+
+    private int currentIndex;
+
+    private RowSetMetaData metadata;
+
+    private static class ConflictedRow {
+        CachedRow row;
+
+        int index;
+
+        int status;
+
+        public ConflictedRow(CachedRow row, int index, int status) {
+            this.row = row;
+            this.index = index;
+            this.status = status;
+        }
+    }
+
+    public SyncResolverImpl(RowSetMetaData metadata) {
+        super();
+        this.metadata = metadata;
+        conflictRows = new ArrayList<ConflictedRow>();
+        currentIndex = -1;
+    }
+
+    public void addConflictRow(CachedRow row, int rowIndex, int status) {
+        conflictRows.add(new ConflictedRow(row, rowIndex, status));
+    }
+
     public Object getConflictValue(int index) throws SQLException {
-        // TODO not yet implemented
-        throw new NotImplementedException();
+        if (index <= 0 || index > metadata.getColumnCount()) {
+            // sql.27=Invalid column index :{0}
+            throw new SQLException(Messages.getString("sql.27", Integer //$NON-NLS-1$
+                    .valueOf(index)));
+        }
+
+        if (currentIndex < 0 || currentIndex >= conflictRows.size()) {
+            // rowset.7=Not a valid cursor
+            throw new SQLException(Messages.getString("rowset.7")); //$NON-NLS-1$
+        }
+
+        return conflictRows.get(currentIndex).row.getObject(index);
     }
 
     public Object getConflictValue(String columnName) throws SQLException {
-        // TODO not yet implemented
-        throw new NotImplementedException();
+        return getConflictValue(getIndexByName(columnName));
     }
 
     public int getStatus() {
-        // TODO not yet implemented
-        throw new NotImplementedException();
+        if (currentIndex < 0 || currentIndex >= conflictRows.size()) {
+            /*
+             * invalid cursor, can't throw SQLException, we throw
+             * NullPointerException instead
+             */
+            // rowset.7=Not a valid cursor
+            throw new NullPointerException(Messages.getString("rowset.7")); //$NON-NLS-1$
+        }
+
+        return conflictRows.get(currentIndex).status;
     }
 
+    /**
+     * TODO close input stream and clear warning chain as spec say
+     */
     public boolean nextConflict() throws SQLException {
-        // TODO not yet implemented
-        throw new NotImplementedException();
+        if (currentIndex == conflictRows.size()) {
+            return false;
+        }
+
+        currentIndex++;
+        return currentIndex >= 0 && currentIndex < conflictRows.size();
     }
 
     public boolean previousConflict() throws SQLException {
-        // TODO not yet implemented
-        throw new NotImplementedException();
+        if (currentIndex == -1) {
+            return false;
+        }
+
+        currentIndex--;
+        return currentIndex >= 0 && currentIndex < conflictRows.size();
     }
 
     public void setResolvedValue(int index, Object obj) throws SQLException {
@@ -83,14 +146,24 @@
 
     public void setResolvedValue(String columnName, Object obj)
             throws SQLException {
-        // TODO not yet implemented
-        throw new NotImplementedException();
-
+        setResolvedValue(getIndexByName(columnName), obj);
     }
 
     public int getRow() throws SQLException {
-        // TODO not yet implemented
-        throw new NotImplementedException();
+        if (currentIndex < 0 || currentIndex >= conflictRows.size()) {
+            return 0;
+        }
+        return conflictRows.get(currentIndex).index;
+    }
+
+    private int getIndexByName(String columnName) throws SQLException {
+        for (int i = 1; i <= metadata.getColumnCount(); i++) {
+            if (columnName.equalsIgnoreCase(metadata.getColumnName(i))) {
+                return i;
+            }
+        }
+        // rowset.1=Not a valid column name
+        throw new SQLException(Messages.getString("rowset.1"));
     }
 
     public void execute() throws SQLException {

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/SyncResolverTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/SyncResolverTest.java?rev=599764&r1=599763&r2=599764&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/SyncResolverTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/SyncResolverTest.java
Fri Nov 30 02:01:30 2007
@@ -17,10 +17,16 @@
 
 package org.apache.harmony.sql.tests.internal.rowset;
 
+import java.sql.SQLException;
+
+import javax.sql.RowSetMetaData;
+import javax.sql.rowset.RowSetMetaDataImpl;
 import javax.sql.rowset.spi.SyncResolver;
 
+import org.apache.harmony.sql.internal.rowset.CachedRow;
 import org.apache.harmony.sql.internal.rowset.SyncResolverImpl;
 
+
 public class SyncResolverTest extends CachedRowSetTestCase {
     @Override
     public void setUp() throws Exception {
@@ -67,7 +73,7 @@
         // }
         //
         // SyncResolver resolver = ex.getSyncResolver();
-        SyncResolver resolver = new SyncResolverImpl();
+        SyncResolver resolver = new SyncResolverImpl(null);
 
         try {
             resolver.absolute(1);
@@ -263,5 +269,129 @@
         } catch (UnsupportedOperationException e) {
             // expected
         }
+    }
+
+    public void testGetConflictValue() throws Exception {
+
+        RowSetMetaData metadata = new RowSetMetaDataImpl();
+        metadata.setColumnCount(DEFAULT_COLUMN_COUNT);
+
+        SyncResolverImpl resolver = new SyncResolverImpl(metadata);
+        resolver.addConflictRow(
+                new CachedRow(new Object[DEFAULT_COLUMN_COUNT]), 1,
+                SyncResolver.INSERT_ROW_CONFLICT);
+
+        resolver.addConflictRow(
+                new CachedRow(new Object[DEFAULT_COLUMN_COUNT]), 2,
+                SyncResolver.INSERT_ROW_CONFLICT);
+
+        // before call nextConflict
+        try {
+            resolver.getConflictValue(1);
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected, Invalid cursor position
+        }
+
+        try {
+            resolver.getConflictValue(-1);
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected, Invalid column index
+        }
+        try {
+            resolver.getConflictValue("not exist");
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected, Invalid column name
+        }
+
+        assertTrue(resolver.nextConflict());
+
+        for (int i = 1; i <= DEFAULT_COLUMN_COUNT; ++i) {
+            assertNull(resolver.getConflictValue(i));
+        }
+
+        assertTrue(resolver.nextConflict());
+
+        for (int i = 1; i <= DEFAULT_COLUMN_COUNT; ++i) {
+            assertNull(resolver.getConflictValue(i));
+        }
+
+        assertFalse(resolver.nextConflict());
+        assertEquals(0, resolver.getRow());
+
+        /*
+         * ri throw SQLException after call nextConflict again, it's not
+         * reasonable
+         */
+        try {
+            resolver.getConflictValue(1);
+            fail("Should throw SQLException");
+        } catch (SQLException e) {
+            // expected Invalid cursor position
+        }
+
+    }
+
+    public void testNextPreviousConflict() throws Exception {
+
+        RowSetMetaData metadata = new RowSetMetaDataImpl();
+        metadata.setColumnCount(DEFAULT_COLUMN_COUNT);
+
+        SyncResolverImpl resolver = new SyncResolverImpl(metadata);
+        resolver.addConflictRow(
+                new CachedRow(new Object[DEFAULT_COLUMN_COUNT]), 1,
+                SyncResolver.INSERT_ROW_CONFLICT);
+
+        resolver.addConflictRow(
+                new CachedRow(new Object[DEFAULT_COLUMN_COUNT]), 2,
+                SyncResolver.INSERT_ROW_CONFLICT);
+
+        assertTrue(resolver.nextConflict());
+        assertTrue(resolver.nextConflict());
+        assertFalse(resolver.nextConflict());
+        assertFalse(resolver.nextConflict());
+
+        assertTrue(resolver.previousConflict());
+        assertTrue(resolver.previousConflict());
+        assertFalse(resolver.previousConflict());
+        assertFalse(resolver.previousConflict());
+    }
+
+    public void testGetStatus() throws Exception {
+
+        RowSetMetaData metadata = new RowSetMetaDataImpl();
+        metadata.setColumnCount(DEFAULT_COLUMN_COUNT);
+
+        SyncResolverImpl resolver = new SyncResolverImpl(metadata);
+        resolver.addConflictRow(
+                new CachedRow(new Object[DEFAULT_COLUMN_COUNT]), 1,
+                SyncResolver.INSERT_ROW_CONFLICT);
+
+        resolver.addConflictRow(
+                new CachedRow(new Object[DEFAULT_COLUMN_COUNT]), 2,
+                SyncResolver.INSERT_ROW_CONFLICT);
+        
+        try {
+            resolver.getStatus();
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        assertTrue(resolver.nextConflict());
+        assertEquals(SyncResolver.INSERT_ROW_CONFLICT, resolver.getStatus());
+        assertTrue(resolver.nextConflict());
+        assertEquals(SyncResolver.INSERT_ROW_CONFLICT, resolver.getStatus());
+        assertFalse(resolver.nextConflict());
+
+        try {
+            resolver.getStatus();
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
     }
 }



Mime
View raw message