harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lvj...@apache.org
Subject svn commit: r647681 - 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, 14 Apr 2008 07:37:07 GMT
Author: lvjing
Date: Mon Apr 14 00:37:02 2008
New Revision: 647681

URL: http://svn.apache.org/viewvc?rev=647681&view=rev
Log:
Apply patch for HARMONY-5720,[classlib][sql][rowset] - implement getRowSets(), getWhereClause(), execute(), populate(ResultSet, int) and toCachedRowSet() methods in JoinRowSetImpl

Added:
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetCachedRowSetTest.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/main/java/org/apache/harmony/sql/internal/rowset/JoinRowSetImpl.java
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/WebRowSetImpl.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/FilteredRowSetTest.java
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetOtherTest.java
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetTest.java
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/WebRowSetTest.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=647681&r1=647680&r2=647681&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 Apr 14 00:37:02 2008
@@ -24,7 +24,6 @@
 import java.io.StringBufferInputStream;
 import java.io.StringReader;
 import java.io.StringWriter;
-import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
 import java.sql.Array;
@@ -67,6 +66,7 @@
 import javax.sql.rowset.CachedRowSet;
 import javax.sql.rowset.RowSetMetaDataImpl;
 import javax.sql.rowset.RowSetWarning;
+import javax.sql.rowset.WebRowSet;
 import javax.sql.rowset.serial.SerialArray;
 import javax.sql.rowset.serial.SerialBlob;
 import javax.sql.rowset.serial.SerialClob;
@@ -394,12 +394,11 @@
              */
             output = (CachedRowSetImpl) super.clone();
             // BaseRowSet.params
-            Field paramsField = output.getClass().getSuperclass()
-                    .getDeclaredField("params"); //$NON-NLS-1$
+            Field paramsField = BaseRowSet.class.getDeclaredField("params"); //$NON-NLS-1$
             paramsField.setAccessible(true);
             paramsField.set(output, paramsHashtable);
             // BaseRowSet.listeners
-            Field listenersField = output.getClass().getSuperclass()
+            Field listenersField = BaseRowSet.class
                     .getDeclaredField("listeners"); //$NON-NLS-1$
             listenersField.setAccessible(true);
             listenersField.set(output, listeners);
@@ -566,6 +565,10 @@
     }
 
     public ResultSet getOriginal() throws SQLException {
+        if (originalResultSet == null)
+        {
+            throw new NullPointerException();
+        }
         return originalResultSet;
     }
 
@@ -1437,6 +1440,10 @@
      * @return row index include delted rows
      */
     private int getIndexIncludeDeletedRows(int index) throws SQLException {
+        if (rows == null || rows.size() == 0) {
+            return -1;
+        }
+
         if (getShowDeleted()) {
             return index;
         }
@@ -2424,6 +2431,7 @@
             ex.initCause(e);
             throw ex;
         }
+
         updateString(columnIndex, new String(out.toByteArray()));
     }
 
@@ -2664,6 +2672,10 @@
     }
 
     public void updateRow() throws SQLException {
+        if (currentRow == null) {
+            // rowset.7=Not a valid cursor
+            throw new SQLException(Messages.getString("rowset.7")); //$NON-NLS-1$
+        }
         if (isCursorOnInsert) {
             // rowset.11=Illegal operation on an insert row
             throw new SQLException(Messages.getString("rowset.11")); //$NON-NLS-1$

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/JoinRowSetImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/JoinRowSetImpl.java?rev=647681&r1=647680&r2=647681&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/JoinRowSetImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/JoinRowSetImpl.java Mon Apr 14 00:37:02 2008
@@ -16,6 +16,8 @@
  */
 package org.apache.harmony.sql.internal.rowset;
 
+import java.sql.Connection;
+import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.sql.Types;
@@ -34,13 +36,14 @@
 import javax.sql.rowset.RowSetMetaDataImpl;
 import javax.sql.rowset.spi.SyncFactoryException;
 
-import org.apache.harmony.luni.util.NotImplementedException;
 import org.apache.harmony.sql.internal.nls.Messages;
 
 public class JoinRowSetImpl extends WebRowSetImpl implements JoinRowSet {
 
     private List<RowSet> rsList;
 
+    private CachedRowSet copyFirstRs;
+
     private List<Integer> matchColIndexs;
 
     private List<String> matchColNames;
@@ -61,6 +64,7 @@
         super(providerID);
         initProperties();
     }
+    
 
     private void initProperties() {
         rsList = new ArrayList<RowSet>();
@@ -216,6 +220,7 @@
         }
 
         if (rsList.size() == 1) {
+            copyFirstRs = ((CachedRowSet) rsList.get(0)).createCopy();
             setMatchColumn(columnIdx);
         }
     }
@@ -229,7 +234,12 @@
             throw new SQLException(Messages.getString("rowset.32")); //$NON-NLS-1$
         }
 
-        int columnIdx = rowset.findColumn(columnName);
+        int columnIdx = -1;
+        try {
+            columnIdx = rowset.findColumn(columnName);
+        } catch (SQLException e) {
+            throw e;
+        }
         addRowSet(rowset, columnIdx);
     }
 
@@ -288,11 +298,113 @@
     }
 
     public Collection<?> getRowSets() throws SQLException {
-        throw new NotImplementedException();
+        return rsList;
     }
 
+    /**
+     * Gets a sql clause which specify the join action.
+     */
     public String getWhereClause() throws SQLException {
-        throw new NotImplementedException();
+        int size = rsList.size();
+
+        // If 0 rowSets in it, return "".
+        if (size == 0) {
+            return ""; //$NON-NLS-1$
+        }
+
+        String whereClause;
+        String tableName;
+        int metaColumnCount;
+        int matchIndex;
+        if (size == 1) {
+            tableName = ((CachedRowSet) rsList.get(0)).getTableName();
+            if (tableName == null) {
+                throw new SQLException(Messages.getString("rowset.39")); //$NON-NLS-1$
+            }
+            metaColumnCount = meta.getColumnCount();
+            whereClause = "Select"; //$NON-NLS-1$
+            for (int i = 1; i <= metaColumnCount; i++) {
+                whereClause += " " + tableName + "." + meta.getColumnName(i); //$NON-NLS-1$ //$NON-NLS-2$
+                if (i < metaColumnCount) {
+                    whereClause += ","; //$NON-NLS-1$
+                } else {
+                    whereClause += " from " + tableName; //$NON-NLS-1$
+                }
+            }
+        } else {
+            whereClause = "Select"; //$NON-NLS-1$
+            CachedRowSet rowSet;
+            String matchName;
+
+            for (int i = 0; i < size; i++) {
+                rowSet = (CachedRowSet) rsList.get(i);
+                tableName = rowSet.getTableName();
+                if (tableName == null) {
+                    throw new SQLException(Messages.getString("rowset.39")); //$NON-NLS-1$
+                }
+
+                matchIndex = matchColIndexs.get(0);
+                if (i == 0) {
+                    matchName = rowSet.getMetaData().getColumnName(matchIndex);
+                    whereClause += " " + tableName + "." + matchName + ",";
+                }
+
+                metaColumnCount = rowSet.getMetaData().getColumnCount();
+                for (int j = 1; j < matchIndex; j++) {
+                    matchName = rowSet.getMetaData().getColumnName(j);
+                    whereClause += " " + tableName + "." + matchName;
+
+                    if (j != metaColumnCount - 1 || i != size - 1) {
+                        whereClause += ",";
+                    } else {
+                        whereClause += " ";
+                    }
+                }
+
+                for (int j = matchIndex + 1; j <= metaColumnCount; j++) {
+                    matchName = rowSet.getMetaData().getColumnName(j);
+                    whereClause += " " + tableName + "." + matchName;
+
+                    if (j != metaColumnCount || i != size - 1) {
+                        whereClause += ",";
+                    } else {
+                        whereClause += " ";
+                    }
+                }
+            }
+            whereClause += "from ";
+            for (int i = 0; i < size; i++) {
+                rowSet = (CachedRowSet) rsList.get(i);
+                tableName = rowSet.getTableName();
+
+                whereClause += tableName;
+                if (i != size - 1) {
+                    whereClause += ", ";
+                } else {
+                    whereClause += " ";
+                }
+            }
+
+            whereClause += "where ";
+            CachedRowSet firstRowSet = (CachedRowSet) rsList.get(0);
+            String firstTableName = firstRowSet.getTableName();
+            String firstMatchName = firstRowSet.getMetaData().getColumnName(
+                    matchColIndexs.get(0));
+            for (int i = 1; i < size; i++) {
+                rowSet = (CachedRowSet) rsList.get(i);
+                tableName = rowSet.getTableName();
+                matchIndex = matchColIndexs.get(i);
+                matchName = rowSet.getMetaData().getColumnName(matchIndex);
+
+                whereClause += firstTableName + "." + firstMatchName + " = ";
+                whereClause += tableName + "." + matchName;
+                if (i != size - 1) {
+                    whereClause += " and ";
+                }
+            }
+        }
+
+        return whereClause;
     }
 
     public void setJoinType(int joinType) throws SQLException {
@@ -332,7 +444,77 @@
     }
 
     public CachedRowSet toCachedRowSet() throws SQLException {
-        throw new NotImplementedException();
+        if (rsList.size() == 0) {
+            CachedRowSetImpl toCrset = new CachedRowSetImpl();
+            toCrset.setRows(new ArrayList<CachedRow>(), 0);
+            return toCrset;
+        } else if (rsList.size() == 1) {
+            CachedRowSet toCrset = ((CachedRowSet) rsList.get(0)).createCopy();
+            toCrset.setMetaData(meta);
+            return toCrset;
+        } else {
+            CachedRowSetImpl toCrset = new CachedRowSetImpl();
+            toCrset.setRows(rows, meta.getColumnCount());
+            toCrset.setMetaData(meta);
+            toCrset.columnTypes = columnTypes;
+            toCrset.setTypeMap(rsList.get(0).getTypeMap());
+            if (rsList.get(0).getUrl() != null) {
+                toCrset.setUrl(rsList.get(0).getUrl());
+                toCrset.setUsername(rsList.get(0).getUsername());
+                toCrset.setPassword(rsList.get(0).getPassword());
+            } else if (rsList.get(0).getDataSourceName() != null) {
+                toCrset.setDataSourceName(rsList.get(0).getDataSourceName());
+            }
+            return toCrset;
+        }
+    }
+
+    @Override
+    public void populate(ResultSet rs) throws SQLException {
+        // do nothing
+    }
+
+    @Override
+    public void populate(ResultSet rs, int startRow) throws SQLException {
+        // do nothing
+    }
+
+    @Override
+    public void execute() throws SQLException {
+        if (rsList.size() == 0) {
+            throw new SQLException();
+        } else if (rsList.size() == 1) {
+            try {
+                copyFirstRs.execute();
+                super.populate(copyFirstRs);
+            } catch (SQLException e) {
+                setRows(new ArrayList<CachedRow>(), getMetaData()
+                        .getColumnCount());
+                throw e;
+            }
+        } else {
+            setRows(new ArrayList<CachedRow>(), getMetaData().getColumnCount());
+            throw new SQLException();
+        }
+    }
+
+    @Override
+    public void execute(Connection connection) throws SQLException {
+        if (rsList.size() == 0) {
+            throw new SQLException();
+        } else if (rsList.size() == 1) {
+            try {
+                copyFirstRs.execute(connection);
+                super.populate(copyFirstRs);
+            } catch (SQLException e) {
+                setRows(new ArrayList<CachedRow>(), getMetaData()
+                        .getColumnCount());
+                throw e;
+            }
+        } else {
+            setRows(new ArrayList<CachedRow>(), getMetaData().getColumnCount());
+            throw new SQLException();
+        }
     }
 
     /**
@@ -706,6 +888,5 @@
             return -1;
 
         }
-
     }
 }

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/WebRowSetImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/WebRowSetImpl.java?rev=647681&r1=647680&r2=647681&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/WebRowSetImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/WebRowSetImpl.java Mon Apr 14 00:37:02 2008
@@ -26,6 +26,8 @@
 import java.sql.ResultSet;
 import java.sql.SQLException;
 
+import javax.sql.rowset.BaseRowSet;
+import javax.sql.rowset.CachedRowSet;
 import javax.sql.rowset.WebRowSet;
 import javax.sql.rowset.spi.SyncFactoryException;
 
@@ -50,14 +52,14 @@
     }
 
     public void writeXml(ResultSet rs, Writer writer) throws SQLException {
-        populate(rs);
+        super.populate(rs);
         writeXml(writer);
         beforeFirst();
     }
 
     public void writeXml(ResultSet rs, OutputStream oStream)
             throws SQLException, IOException {
-        populate(rs);
+        super.populate(rs);
         writeXml(oStream);
         beforeFirst();
     }
@@ -70,4 +72,45 @@
         new XmlWriterImpl().writeXML(this, new OutputStreamWriter(oStream));
     }
 
+    @Override
+    public CachedRowSet createCopy() throws SQLException {
+        WebRowSetImpl webRs = new WebRowSetImpl();
+        CachedRowSet copyCrset = super.createCopy();
+        copyCrset.beforeFirst();
+        webRs.populate(copyCrset);
+        webRs.setCommand(copyCrset.getCommand());
+        Object[] params = ((CachedRowSetImpl) copyCrset).getParams();
+        for (int i = 0; i < params.length; i++) {
+            if (params[i] instanceof Object[]) {
+                Object[] objs = (Object[]) params[i];
+                // character stream
+                if (objs.length == 2) {
+                    webRs.setCharacterStream(i + 1, (Reader) objs[0],
+                            ((Integer) objs[1]).intValue());
+                } else {
+                    int type = ((Integer) objs[2]).intValue();
+                    switch (type) {
+                    case BaseRowSet.ASCII_STREAM_PARAM:
+                        webRs.setAsciiStream(i + 1, (InputStream) objs[0],
+                                ((Integer) objs[1]).intValue());
+                        break;
+                    case BaseRowSet.BINARY_STREAM_PARAM:
+                        webRs.setBinaryStream(i + 1, (InputStream) objs[0],
+                                ((Integer) objs[1]).intValue());
+                        break;
+                    }
+                }
+            } else {
+                webRs.setObject(i + 1, params[i]);
+            }
+        }
+        if (copyCrset.getUrl() != null) {
+            webRs.setUrl(copyCrset.getUrl());
+            webRs.setUsername(copyCrset.getUsername());
+            webRs.setPassword(copyCrset.getPassword());
+        } else if (copyCrset.getDataSourceName() != null) {
+            webRs.setDataSourceName(copyCrset.getDataSourceName());
+        }
+        return webRs;
+    }
 }

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=647681&r1=647680&r2=647681&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 Apr 14 00:37:02 2008
@@ -880,6 +880,62 @@
         assertFalse(crset.next());
     }
 
+    public void testCreateCopy4() throws Exception {
+        crset.setCommand("SELECT * FROM USER_INFO WHERE ID = ?");
+        crset.setInt(1, 3);
+        crset.execute();
+        // check data
+        assertTrue(crset.next());
+        assertEquals(3, crset.getInt(1));
+        assertFalse(crset.next());
+
+        // deep copy
+        CachedRowSet copyCrset = crset.createCopy();
+        copyCrset.beforeFirst();
+        assertTrue(copyCrset.next());
+        assertEquals(3, copyCrset.getInt(1));
+        assertFalse(copyCrset.next());
+        copyCrset.execute();
+        assertTrue(copyCrset.next());
+        assertEquals(3, copyCrset.getInt(1));
+        assertFalse(copyCrset.next());
+
+        crset.setInt(1, 4);
+        crset.execute();
+        crset.beforeFirst();
+        assertTrue(crset.next());
+        assertEquals(4, crset.getInt(1));
+        assertFalse(crset.next());
+
+        copyCrset.beforeFirst();
+        assertTrue(copyCrset.next());
+        assertEquals(3, copyCrset.getInt(1));
+        assertFalse(copyCrset.next());
+
+        copyCrset.execute();
+        copyCrset.beforeFirst();
+        assertTrue(copyCrset.next());
+        assertEquals(3, copyCrset.getInt(1));
+        assertFalse(copyCrset.next());
+
+        copyCrset.setInt(1, 1);
+        copyCrset.execute();
+        assertTrue(copyCrset.next());
+        assertEquals(1, copyCrset.getInt(1));
+        assertFalse(copyCrset.next());
+
+        crset.beforeFirst();
+        assertTrue(crset.next());
+        assertEquals(4, crset.getInt(1));
+        assertFalse(crset.next());
+
+        crset.execute();
+        crset.beforeFirst();
+        assertTrue(crset.next());
+        assertEquals(4, crset.getInt(1));
+        assertFalse(crset.next());
+    }
+
     public void testAfterLast() throws Exception {
         try {
             rs.afterLast();
@@ -2886,6 +2942,18 @@
             // expected
         }
     }
+
+    public void testGetOriginal() throws Exception {
+        crset = newNoInitialInstance();
+        try {
+            crset.getOriginal();
+            fail("Should throw NullPointerException.");
+        }
+        catch (NullPointerException e) {
+            // Expected.
+        }
+    }
+
 }
 
 class Listener implements RowSetListener, Cloneable {

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/FilteredRowSetTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/FilteredRowSetTest.java?rev=647681&r1=647680&r2=647681&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/FilteredRowSetTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/FilteredRowSetTest.java Mon Apr 14 00:37:02 2008
@@ -53,11 +53,15 @@
         Predicate range = new RangeOne();
         filteredRowSet.setFilter(range);
 
-        try {
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
             filteredRowSet.createCopy();
-            fail("should throw SQLException");
-        } catch (SQLException e) {
-            // expected
+        } else {
+            try {
+                filteredRowSet.createCopy();
+                fail("should throw SQLException");
+            } catch (SQLException e) {
+                // expected
+            }
         }
     }
 

Added: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetCachedRowSetTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetCachedRowSetTest.java?rev=647681&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetCachedRowSetTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetCachedRowSetTest.java Mon Apr 14 00:37:02 2008
@@ -0,0 +1,516 @@
+/* 
+ * 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;
+import java.util.Collection;
+import java.util.Iterator;
+
+public class JoinRowSetCachedRowSetTest extends JoinRowSetTestCase {
+
+    public void testGetCommandAndUrl() throws Exception {
+        assertNull(jrs.getCommand());
+        assertNull(jrs.getUrl());
+        assertNull(jrs.getUsername());
+        assertNull(jrs.getPassword());
+
+        String sqlCommand = "select * from USER_INFO";
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.setCommand(sqlCommand);
+        noInitialCrset.setUrl(DERBY_URL);
+        noInitialCrset.execute();
+        assertEquals(sqlCommand, noInitialCrset.getCommand());
+        assertEquals(DERBY_URL, noInitialCrset.getUrl());
+        assertTrue(noInitialCrset.first());
+        assertEquals(1, noInitialCrset.getInt(1));
+
+        // add CachedRowSet to JoinRowSet
+        jrs.addRowSet(noInitialCrset, 1);
+        assertTrue(jrs.first());
+        assertEquals(1, jrs.getInt(1));
+
+        // check command and url
+        assertNull(jrs.getCommand());
+        assertNull(jrs.getUrl());
+        assertNull(jrs.getUsername());
+        assertNull(jrs.getPassword());
+    }
+
+    public void testSetCommandAndUrl() throws Exception {
+        // Test empty JoinRowSet
+        String sqlCommand = "select * from CUSTOMER_INFO";
+        jrs.setCommand(sqlCommand);
+        jrs.setUrl(DERBY_URL);
+        try {
+            jrs.execute();
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+        assertEquals(sqlCommand, jrs.getCommand());
+        assertEquals(DERBY_URL, jrs.getUrl());
+
+        // Test JoinRowSet filled with CachedRowSet
+        jrs = newJoinRowSet();
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.setCommand(sqlCommand);
+        noInitialCrset.setUrl(DERBY_URL);
+        noInitialCrset.execute();
+        jrs.addRowSet(noInitialCrset, 1);
+        assertTrue(jrs.first());
+        assertEquals(1111, jrs.getInt(1));
+        assertNull(jrs.getCommand());
+        assertNull(jrs.getUrl());
+        // call JoinRowSet.execute()
+        jrs.beforeFirst();
+        jrs.execute();
+        // check data
+        int index = 0;
+        noInitialCrset.beforeFirst();
+        while (jrs.next() && noInitialCrset.next()) {
+            index++;
+            for (int i = 1; i <= jrs.getMetaData().getColumnCount(); i++) {
+                assertEquals(jrs.getObject(i), noInitialCrset.getObject(i));
+            }
+        }
+
+        // set command and url
+        sqlCommand = "select * from USER_INFO";
+        jrs.setCommand(sqlCommand);
+        jrs.setUrl(DERBY_URL);
+        assertEquals(sqlCommand, jrs.getCommand());
+        assertEquals(DERBY_URL, jrs.getUrl());
+        // call JoinRowSet.execute() after set cmd and url
+        jrs.execute();
+        /*
+         * Check data. Though the sql command is changed, the data of JoinRowSet
+         * remains the same.
+         */
+        jrs.beforeFirst();
+        noInitialCrset.beforeFirst();
+        while (jrs.next() && noInitialCrset.next()) {
+            for (int i = 1; i <= jrs.getMetaData().getColumnCount(); i++) {
+                assertEquals(jrs.getObject(i), noInitialCrset.getObject(i));
+            }
+        }
+        assertEquals(sqlCommand, jrs.getCommand());
+        assertEquals(DERBY_URL, jrs.getUrl());
+    }
+
+    public void testPopulate() throws Exception {
+        /*
+         * JoinRowSet.populate(ResultSet) won't throw exception. However,
+         * JoinRowSet is still empty.
+         */
+        rs = st.executeQuery("select * from USER_INFO");
+        jrs.populate(rs);
+        assertNull(jrs.getMetaData());
+        jrs.beforeFirst();
+        assertFalse(jrs.next());
+        assertFalse(jrs.first());
+
+        /*
+         * Test JoinRowSet which has added a CachedRowSet
+         */
+        jrs = newJoinRowSet();
+        jrs.addRowSet(crset, 1);
+        assertTrue(jrs.first());
+        assertEquals(1, jrs.getInt(1));
+
+        rs = st.executeQuery("select * from BOOKS");
+        // populate() still has no effect here
+        jrs.populate(rs);
+        jrs.beforeFirst();
+        rs = st.executeQuery("select * from USER_INFO");
+        while (jrs.next() && rs.next()) {
+            for (int i = 1; i <= jrs.getMetaData().getColumnCount(); i++) {
+                assertEquals(rs.getObject(i), jrs.getObject(i));
+            }
+        }
+    }
+
+    public void testExecuteWithoutConn_Normal() throws Exception {
+        /*
+         * Add a CachedRowSet which has set command and url but not call execute
+         * to JoinRowSet
+         */
+        rs = st.executeQuery("select * from USER_INFO");
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.setCommand("select * from BOOKS");
+        noInitialCrset.setUrl(DERBY_URL);
+        noInitialCrset.populate(rs);
+        assertTrue(noInitialCrset.first());
+        assertEquals("hermit", noInitialCrset.getString(2));
+        assertEquals("select * from BOOKS", noInitialCrset.getCommand());
+        assertEquals(DERBY_URL, noInitialCrset.getUrl());
+        // add to JoinRowSet
+        jrs = newJoinRowSet();
+        jrs.addRowSet(noInitialCrset, 1);
+        assertNull(jrs.getStatement());
+        // check data
+        assertSame(noInitialCrset.getMetaData(), jrs.getMetaData());
+        rs = st.executeQuery("select * from USER_INFO");
+        jrs.beforeFirst();
+        int index = 0;
+        while (jrs.next() && rs.next()) {
+            index++;
+            for (int i = 1; i <= jrs.getMetaData().getColumnCount(); i++) {
+                assertEquals(jrs.getObject(i), rs.getObject(i));
+            }
+        }
+        assertEquals(4, index);
+        // call JoinRowSet.execute()
+        assertNull(jrs.getCommand());
+        jrs.execute();
+        assertNull(jrs.getStatement());
+        assertNull(jrs.getCommand());
+        // check JoinRowSet's data
+        assertNotSame(noInitialCrset.getMetaData(), jrs.getMetaData());
+        rs = st.executeQuery("select * from BOOKS");
+        jrs.beforeFirst();
+        index = 0;
+        while (jrs.next() && rs.next()) {
+            index++;
+            for (int i = 1; i <= jrs.getMetaData().getColumnCount(); i++) {
+                assertEquals(jrs.getObject(i), rs.getObject(i));
+            }
+        }
+        assertEquals(7, index);
+        // check noInitialCrset's data
+        rs = st.executeQuery("select * from USER_INFO");
+        noInitialCrset.beforeFirst();
+        index = 0;
+        while (noInitialCrset.next() && rs.next()) {
+            index++;
+            for (int i = 1; i <= noInitialCrset.getMetaData().getColumnCount(); i++) {
+                assertEquals(noInitialCrset.getObject(i), rs.getObject(i));
+            }
+        }
+        assertEquals(4, index);
+        // call noInitialCrset's execute()
+        assertEquals("select * from BOOKS", noInitialCrset.getCommand());
+        noInitialCrset.execute();
+        // check noInitialCrset's data
+        jrs.beforeFirst();
+        noInitialCrset.beforeFirst();
+        index = 0;
+        while (noInitialCrset.next() && jrs.next()) {
+            index++;
+            for (int i = 1; i <= noInitialCrset.getMetaData().getColumnCount(); i++) {
+                assertEquals(noInitialCrset.getObject(i), jrs.getObject(i));
+            }
+        }
+        assertEquals(7, index);
+
+        // change noInitialCrset's command
+        noInitialCrset.setCommand("select * from CUSTOMER_INFO");
+        // call JoinRowSet.execute()
+        jrs.execute();
+        // check data
+        jrs.beforeFirst();
+        noInitialCrset.beforeFirst();
+        index = 0;
+        while (noInitialCrset.next() && jrs.next()) {
+            index++;
+            for (int i = 1; i <= noInitialCrset.getMetaData().getColumnCount(); i++) {
+                assertEquals(noInitialCrset.getObject(i), jrs.getObject(i));
+            }
+        }
+        assertEquals(7, index);
+
+        /*
+         * Add a CachedRowSet which has set command and url to JoinRowSet
+         */
+        crset = newNoInitialInstance();
+        crset.setCommand("select * from USER_INFO");
+        crset.setUrl(DERBY_URL);
+        crset.execute();
+        assertTrue(crset.absolute(1));
+        assertEquals("hermit", crset.getString(2));
+        // add to JoinRowSet
+        jrs = newJoinRowSet();
+        jrs.addRowSet(crset, 1);
+        // check data
+        assertSame(crset.getMetaData(), jrs.getMetaData());
+        assertTrue(jrs.first());
+        assertEquals("hermit", jrs.getString(2));
+        // call JoinRowSet.execute()
+        jrs.execute();
+        // check data
+        assertNotSame(crset.getMetaData(), jrs.getMetaData());
+        isMetaDataEquals(crset.getMetaData(), jrs.getMetaData());
+        assertTrue(jrs.absolute(1));
+        assertEquals("hermit", jrs.getString(2));
+
+        // set command and url for JoinRowSet, then call execute() again
+        jrs.setCommand("select * from BOOKS");
+        jrs.setUrl(DERBY_URL);
+        jrs.execute();
+        // the data remains the same as crset's
+        assertNotSame(crset.getMetaData(), jrs.getMetaData());
+        isMetaDataEquals(crset.getMetaData(), jrs.getMetaData());
+        assertTrue(jrs.absolute(1));
+        assertEquals("hermit", jrs.getString(2));
+
+        // change command for CachedRowSet, then call JoinRowSet.execute()
+        crset.setCommand("select * from BOOKS");
+        jrs.execute();
+        assertNotSame(crset.getMetaData(), jrs.getMetaData());
+        isMetaDataEquals(crset.getMetaData(), jrs.getMetaData());
+        assertTrue(jrs.absolute(1));
+        assertEquals("hermit", jrs.getString(2));
+        // call CachedRowSet.execute()
+        crset.execute();
+        assertTrue(crset.first());
+        assertEquals("sn1-1", crset.getString(2));
+        assertTrue(jrs.absolute(1));
+        assertEquals("hermit", jrs.getString(2));
+
+        /*
+         * Add another CachedRowSet which also can call execute() sucessfully to
+         * JoinRowSet
+         */
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.setCommand("select * from BOOKS");
+        noInitialCrset.setUrl(DERBY_URL);
+        noInitialCrset.execute();
+        /*
+         * TODO The match column will lost after call execute(). Therefore, it
+         * would throw exception and prompts that no match column. It's need to
+         * test Joinable.
+         */
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            // TODO
+        } else {
+            try {
+                jrs.addRowSet(noInitialCrset, 1);
+                fail("should throw SQLException");
+            } catch (SQLException e) {
+                // expected
+            }
+        }
+    }
+
+    public void testExecuteWithoutConn_Exception() throws Exception {
+        /*
+         * An empty JoinRowSet call execute() would throw SQLException even when
+         * command and url are set.
+         */
+        try {
+            jrs.execute();
+            fail("should throw SQLException.");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        String sqlCommand = "select * from USER_INFO";
+        jrs.setCommand(sqlCommand);
+        jrs.setUrl(DERBY_URL);
+        try {
+            jrs.execute();
+            fail("should throw SQLException.");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        /*
+         * Add a CachedRowSet without set command and url to JoinRowSet
+         */
+        rs = st.executeQuery("select * from USER_INFO");
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.populate(rs);
+        assertTrue(noInitialCrset.first());
+        assertEquals(1, noInitialCrset.getInt(1));
+        // add to JoinRowSet
+        jrs = newJoinRowSet();
+        jrs.addRowSet(noInitialCrset, 1);
+        assertTrue(jrs.last());
+        assertEquals(4, jrs.getInt(1));
+        // call execute()
+        try {
+            jrs.execute();
+            fail("should throw SQLException.");
+        } catch (SQLException e) {
+            // expected
+        }
+        // if execute() fail, then the JoinRowSet's data would lose
+        assertNotNull(jrs.getMetaData());
+        assertSame(noInitialCrset.getMetaData(), jrs.getMetaData());
+        assertFalse(jrs.first());
+        assertFalse(jrs.absolute(3));
+        assertTrue(noInitialCrset.first());
+        assertEquals("hermit", noInitialCrset.getString(2));
+
+        /*
+         * Add two CachedRowSets which both can call execute()
+         */
+        crset = newNoInitialInstance();
+        crset.setCommand("select * from USER_INFO");
+        crset.setUrl(DERBY_URL);
+        crset.execute();
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.setCommand("select * from BOOKS");
+        noInitialCrset.setUrl(DERBY_URL);
+        noInitialCrset.execute();
+        // add to JoinRowSet
+        jrs = newJoinRowSet();
+        jrs.addRowSet(crset, 1);
+        jrs.addRowSet(noInitialCrset, 1);
+        // it would throw SQLException when more than one RowSet is added
+        try {
+            jrs.execute();
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+        assertNotNull(jrs.getMetaData());
+        assertFalse(jrs.first());
+    }
+
+    public void testExecuteWithConn_Normal() throws Exception {
+        assertNull(jrs.getStatement());
+        /*
+         * Add a CachedRowSet which has set command and call execute() to
+         * JoinRowSet
+         */
+        crset = newNoInitialInstance();
+        crset.setCommand("select * from USER_INFO");
+        crset.execute(conn);
+        assertTrue(crset.absolute(1));
+        assertEquals("hermit", crset.getString(2));
+        assertEquals(1, crset.getRow());
+        // add to JoinRowSet
+        jrs = newJoinRowSet();
+        jrs.addRowSet(crset, 1);
+        assertNull(jrs.getStatement());
+        // check data
+        assertSame(crset.getMetaData(), jrs.getMetaData());
+        assertTrue(jrs.absolute(1));
+        assertEquals("hermit", jrs.getString(2));
+        assertEquals(1, jrs.getRow());
+        assertTrue(jrs.last());
+        assertEquals(4, jrs.getRow());
+        // call JoinRowSet.execute()
+        jrs.execute(conn);
+        assertNull(jrs.getStatement());
+        // check data
+        assertNotSame(crset.getMetaData(), jrs.getMetaData());
+        isMetaDataEquals(crset.getMetaData(), jrs.getMetaData());
+        rs = st.executeQuery("select * from USER_INFO");
+        jrs.beforeFirst();
+        int index = 0;
+        while (jrs.next() && rs.next()) {
+            index++;
+            for (int i = 1; i <= jrs.getMetaData().getColumnCount(); i++) {
+                assertEquals(jrs.getObject(i), rs.getObject(i));
+            }
+            assertEquals(index, jrs.getRow());
+        }
+        assertEquals(4, index);
+
+        // set command and url for JoinRowSet, then call execute() again
+        jrs.setCommand("select * from BOOKS");
+        jrs.execute(conn);
+        assertNull(jrs.getStatement());
+        // the data remains the same as crset's
+        assertNotSame(crset.getMetaData(), jrs.getMetaData());
+        isMetaDataEquals(crset.getMetaData(), jrs.getMetaData());
+        rs = st.executeQuery("select * from USER_INFO");
+        jrs.beforeFirst();
+        index = 0;
+        while (jrs.next() && rs.next()) {
+            index++;
+            for (int i = 1; i <= jrs.getMetaData().getColumnCount(); i++) {
+                assertEquals(jrs.getObject(i), rs.getObject(i));
+            }
+        }
+        assertEquals(4, index);
+    }
+
+    public void testExecuteWithConn_Exception() throws Exception {
+        /*
+         * Test empty JoinRowSet
+         */
+        try {
+            jrs.execute(conn);
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        jrs.setCommand("select * from USER_INFO");
+        try {
+            jrs.execute(conn);
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        /*
+         * Add a CachedRowSet without set command and url to JoinRowSet
+         */
+        rs = st.executeQuery("select * from USER_INFO");
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.populate(rs);
+        assertTrue(noInitialCrset.first());
+        assertEquals("hermit", noInitialCrset.getString(2));
+        // add to JoinRowSet
+        jrs = newJoinRowSet();
+        jrs.addRowSet(noInitialCrset, 1);
+        // check data
+        assertSame(noInitialCrset.getMetaData(), jrs.getMetaData());
+        assertTrue(jrs.last());
+        assertEquals("test4", jrs.getString(2));
+        // call execute(conn)
+        jrs.setCommand("select * from USER_INFO");
+        try {
+            jrs.execute(conn);
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+        // check data
+        assertSame(noInitialCrset.getMetaData(), jrs.getMetaData());
+        assertFalse(jrs.first());
+        assertTrue(noInitialCrset.first());
+        assertEquals("hermit", noInitialCrset.getString(2));
+
+        /*
+         * Add two CachedRowSets which both can call execute(conn)
+         */
+        crset = newNoInitialInstance();
+        crset.setCommand("select * from USER_INFO");
+        crset.execute(conn);
+        noInitialCrset = newNoInitialInstance();
+        noInitialCrset.setCommand("select * from BOOKS");
+        noInitialCrset.execute(conn);
+        // add to JoinRowSet
+        jrs = newJoinRowSet();
+        jrs.addRowSet(crset, 1);
+        jrs.addRowSet(noInitialCrset, 1);
+        // it would throw SQLException when more than one RowSet is added
+        try {
+            jrs.execute(conn);
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+        // check data
+        assertNotNull(jrs.getMetaData());
+        assertFalse(jrs.first());
+    }
+}

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

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetOtherTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetOtherTest.java?rev=647681&r1=647680&r2=647681&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetOtherTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetOtherTest.java Mon Apr 14 00:37:02 2008
@@ -16,8 +16,13 @@
  */
 package org.apache.harmony.sql.tests.internal.rowset;
 
+import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
+import java.util.Collection;
 
+import javax.sql.RowSetMetaData;
+import javax.sql.rowset.CachedRowSet;
+import javax.sql.rowset.JdbcRowSet;
 import javax.sql.rowset.JoinRowSet;
 
 public class JoinRowSetOtherTest extends JoinRowSetTestCase {
@@ -204,4 +209,324 @@
         }
     }
 
+    public void testGetRowSets_Empty() throws Exception {
+        Collection rowsets = jrs.getRowSets();
+        assertEquals(0, rowsets.size());
+    }
+
+    /**
+     * @throws SQLException
+     * @tests java.sql.rowset.joinRowSet#getRowSets()
+     */
+    public void testGetRowSets_SingleCachedRowSet() throws Exception {
+        crset.setMatchColumn(1);
+        jrs.addRowSet(crset);
+        CachedRowSet returnRowset = (CachedRowSet) jrs.getRowSets().iterator()
+                .next();
+        // Since the returnRowset is the same with the original one,
+        // so we no need to test FilteredRowSet, JoinRowSet, WebRowSet
+        // because they are all subInterface of CachedRowSet.
+        assertSame(returnRowset, crset);
+
+        jrs.absolute(4);
+        jrs.updateString(2, "Updated 4");
+        jrs.updateRow();
+
+        jrs.absolute(4);
+        assertEquals("Updated 4", jrs.getString(2));
+
+        // TODO It is Strange. According to spec, the returned rowset should
+        // maintain
+        // the update occured in the joinrowset.
+        int pos = findValue("Updated 4", returnRowset, 1);
+        assertEquals(-1, pos);
+
+        assertSame(returnRowset, jrs.getRowSets().iterator().next());
+        returnRowset = (CachedRowSet) jrs.getRowSets().iterator().next();
+        assertSame(returnRowset, crset);
+        pos = findValue("Updated 4", returnRowset, 1);
+        assertEquals(-1, pos);
+
+        crset.absolute(3);
+        crset.updateString(2, "Updated 3");
+        crset.updateRow();
+        crset.acceptChanges(conn);
+
+        crset.absolute(3);
+        assertEquals("Updated 3", crset.getString(2));
+        crset.absolute(4);
+        assertEquals("test4", crset.getString(2));
+
+        assertSame(returnRowset, jrs.getRowSets().iterator().next());
+        assertSame(returnRowset, crset);
+    }
+
+    /**
+     * @throws SQLException
+     * @tests java.sql.rowset.joinRowSet#getRowSets()
+     */
+    public void testGetRowSets_SingleJdbcRowSet() throws Exception {
+        // Creates jdbc rowset.
+        JdbcRowSet jdbcRs;
+        jdbcRs = newJdbcRowSet();
+        jdbcRs.setCommand("SELECT * FROM USER_INFO");
+        jdbcRs.setUrl(DERBY_URL);
+        jdbcRs.execute();
+
+        jdbcRs.setMatchColumn(1);
+        jrs.addRowSet(jdbcRs);
+
+        CachedRowSet returnRowset = (CachedRowSet) jrs.getRowSets().iterator()
+                .next();
+        assertNotSame(returnRowset, jrs);
+
+        jrs.absolute(4);
+        jrs.updateString(2, "Updated 4");
+        jrs.updateRow();
+
+        jrs.absolute(4);
+        assertEquals("Updated 4", jrs.getString(2));
+
+        // TODO It is Strange. According to spec, the returned rowset should
+        // maintain
+        // the update occured in the joinrowset.
+        returnRowset.absolute(4);
+        assertEquals("test4", returnRowset.getString(2));
+
+        jdbcRs.absolute(3);
+        jdbcRs.updateString(2, "Updated 3");
+        jdbcRs.updateRow();
+
+        returnRowset.absolute(3);
+        assertEquals("test3", returnRowset.getString(2));
+
+        jdbcRs.close();
+    }
+
+    /**
+     * @throws Exception
+     * @throws SQLException
+     * @tests java.sql.rowset.joinRowSet#getRowSets()
+     */
+    public void testGetRowSets_NoInitialCachedRowSet() throws Exception {
+        Collection collection;
+
+        CachedRowSet crs = newNoInitialInstance();
+        try {
+
+            jrs.addRowSet(crs, 1);
+            if ("true".equals(System.getProperty("Testing Harmony"))) {
+                fail("Should throw SQLException in harmony.");
+            }
+        } catch (SQLException e) {
+            // Expected.
+        }
+    }
+
+    public void testGetRowSets_MultipleCachedRowSet() throws Exception {
+        Collection collection;
+
+        rs = st.executeQuery("select * from USER_INFO");
+        CachedRowSet crset2 = newNoInitialInstance();
+        crset2.populate(rs);
+
+        jrs.addRowSet(crset, 1);
+        jrs.addRowSet(crset2, 1);
+
+        collection = jrs.getRowSets();
+        assertEquals(2, collection.size());
+        assertTrue(collection.contains(crset2));
+        assertTrue(collection.contains(crset));
+
+        crset.absolute(1);
+        crset.updateString(2, "Updated");
+        crset.updateRow();
+        int pos = findValue("Updated", jrs, 2);
+        assertEquals(-1, pos);
+    }
+
+    /**
+     * @throws Exception
+     * @tests java.sql.rowset.joinRowSet#getWhereClause()
+     */
+    public void testGetWhereClause_Empty() throws Exception {
+        String whereClause;
+
+        if (System.getProperty("Testing Harmony") == "true") {
+            whereClause = jrs.getWhereClause();
+            assertEquals("", whereClause);
+        } else {
+            try {
+                whereClause = jrs.getWhereClause();
+                fail("Should throw StringIndexOutOfBoundsException.");
+            } catch (StringIndexOutOfBoundsException e) {
+                // Expected.
+            }
+        }
+    }
+
+    /**
+     * @throws Exception
+     * @tests java.sql.rowset.joinRowSet#getWhereClause()
+     */
+    public void testGetWhereClause_SingleCachedRowSet() throws Exception {
+        String whereClause;
+
+        jrs.addRowSet(crset, 1);
+        if (System.getProperty("Testing Harmony") == "true") {
+
+            whereClause = jrs.getWhereClause();
+            assertNotNull(whereClause);
+        }
+
+        else {
+            try {
+                whereClause = jrs.getWhereClause();
+                fail("Should throw NullPointerException.");
+            } catch (NullPointerException e) {
+                // Expected.
+            }
+        }
+
+        crset.setTableName("table1");
+
+        if (System.getProperty("Testing Harmony") == "true") {
+            whereClause = jrs.getWhereClause();
+            assertNotNull(whereClause);
+        } else {
+            try {
+                whereClause = jrs.getWhereClause();
+                fail("Should throw SQLException.");
+            } catch (SQLException e) {
+                // Expected.
+            }
+        }
+
+        crset.setMatchColumn("ID");
+
+        whereClause = jrs.getWhereClause();
+        assertNotNull(whereClause);
+    }
+
+    /**
+     * @throws Exception
+     * @tests java.sql.rowset.joinRowSet#getWhereClause()
+     */
+    public void testGetWhereClause_SingleJdbcRowSet() throws Exception {
+        String whereClause;
+
+        // Creates jdbc rowset.
+        JdbcRowSet jdbcRs;
+        jdbcRs = newJdbcRowSet();
+        jdbcRs.setCommand("SELECT * FROM USER_INFO");
+        jdbcRs.setUrl(DERBY_URL);
+        jdbcRs.execute();
+
+        jdbcRs.setMatchColumn("ID");
+        jrs.addRowSet(jdbcRs);
+
+        if (System.getProperty("Testing Harmony") == "true") {
+            whereClause = jrs.getWhereClause();
+            assertNotNull(whereClause);
+        } else {
+            try {
+                whereClause = jrs.getWhereClause();
+                fail("Should throw NullPointerException.");
+            } catch (NullPointerException e) {
+                // Expected.
+            }
+        }
+
+        CachedRowSet rowSetInJrs = (CachedRowSet) jrs.getRowSets().iterator()
+                .next();
+        rowSetInJrs.setTableName("Table1");
+
+        whereClause = jrs.getWhereClause();
+        assertNotNull(whereClause);
+    }
+
+    /**
+     * @throws Exception
+     * @tests java.sql.rowset.joinRowSet#getWhereClause()
+     */
+    public void testGetWhereClause_MultipleCachedRowSet() throws Exception {
+        String whereClause;
+
+        // Creates another cached rowset.
+        CachedRowSet crset2;
+        crset2 = newNoInitialInstance();
+        crset2.setCommand("SELECT * FROM BOOKS");
+        crset2.setUrl(DERBY_URL);
+        crset2.execute();
+
+        // Tests when there are on jdbcRowSet and one CachedRowSet.
+        jrs.addRowSet(crset2, 1);
+        jrs.addRowSet(crset, 1);
+
+        if (System.getProperty("Testing Harmony") == "true") {
+            whereClause = jrs.getWhereClause();
+            assertNotNull(whereClause);
+        } else {
+            try {
+                whereClause = jrs.getWhereClause();
+                fail("Should throw NullPointerException.");
+            } catch (NullPointerException e) {
+                // Expected.
+            }
+        }
+        crset.setTableName("Table1");
+        crset2.setTableName("Table2");
+        if (System.getProperty("Testing Harmony") == "true") {
+            whereClause = jrs.getWhereClause();
+            assertNotNull(whereClause);
+        } else {
+            try {
+                whereClause = jrs.getWhereClause();
+                fail("Should throw SQLException.");
+            } catch (SQLException e) {
+                // Expected.
+            }
+        }
+
+        crset.setMatchColumn("ID");
+        crset2.setMatchColumn("AUTHORID");
+        whereClause = jrs.getWhereClause();
+        assertNotNull(whereClause);
+
+        jrs = newJoinRowSet();
+        jrs.addRowSet(crset2, "AUTHORID");
+        jrs.addRowSet(crset, "ID");
+        whereClause = jrs.getWhereClause();
+        assertNotNull(whereClause);
+
+        crset2.close();
+    }
+
+    /**
+     * @throws Exception
+     * @tests java.sql.rowset.joinRowSet#getWhereClause()
+     */
+    public void testGetWhereClause_MoreRowSets() throws Exception {
+        crset.setTableName("Table1");
+        jrs.addRowSet(crset, "ID");
+        
+        // Creates another cached rowset.
+        CachedRowSet crset2;
+        crset2 = newNoInitialInstance();
+        crset2.setCommand("SELECT * FROM BOOKS");
+        crset2.setUrl(DERBY_URL);
+        crset2.execute();
+        crset2.setTableName("Table2");
+        jrs.addRowSet(crset2, "AUTHORID");
+        
+        crset2 = newNoInitialInstance();
+        crset2.setCommand("SELECT * FROM BOOKS");
+        crset2.setUrl(DERBY_URL);
+        crset2.execute();
+        crset2.setTableName("Table3");
+        jrs.addRowSet(crset2, "AUTHORID");
+      
+        String whereClause = jrs.getWhereClause();
+        assertNotNull(whereClause);
+    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetTest.java?rev=647681&r1=647680&r2=647681&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/JoinRowSetTest.java Mon Apr 14 00:37:02 2008
@@ -312,7 +312,6 @@
          * Test empty WebRowSet
          */
         WebRowSet webRs = newWebRowSet();
-
         /*
          * Though WebRowSet implements Joinable, addRowSet(WebRowSet) would
          * throw ClassCastException when run on RI.
@@ -335,28 +334,40 @@
         } else {
             try {
                 jrs.addRowSet(webRs);
-                fail("should throw NullPointerException");
-            } catch (SQLException e) {
-                // Expected
+                fail("should throw ClassCastException");
+            } catch (ClassCastException e) {
+                // expected
             }
 
             webRs.setMatchColumn(1);
-            jrs.addRowSet(webRs);
+            try {
+                jrs.addRowSet(webRs);
+                fail("should throw ClassCastException");
+            } catch (ClassCastException e) {
+                // expected
+            }
         }
         jrs.close();
 
         /*
          * Test WebRowSet filled with data
          */
-        // TODO uncomment it after implementing addRowSet(RowSet, int)
-        // jrs = newJoinRowSet();
-        // webRs = newWebRowSet();
-        // webRs.populate(st.executeQuery("SELECT * FROM USER_INFO"));
-        // webRs.setMatchColumn(1);
-        // jrs.addRowSet(webRs);
-        // assertTrue(jrs.next());
-        // assertEquals(1, jrs.getInt(1));
-        // jrs.close();
+        jrs = newJoinRowSet();
+        webRs = newWebRowSet();
+        webRs.populate(st.executeQuery("SELECT * FROM USER_INFO"));
+        webRs.setMatchColumn(1);
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            jrs.addRowSet(webRs);
+        } else {
+            try {
+                // The same problem as the empty
+                jrs.addRowSet(webRs);
+                fail("should throw ClassCastException");
+            } catch (ClassCastException e) {
+                // expected
+            }
+        }
+        jrs.close();
     }
 
     public void testAddJoinableRowSet_WebRowSet_Int_Exception()
@@ -374,21 +385,32 @@
                 // expected
             }
         } else {
-            jrs.addRowSet(webRs, 1);
+            try {
+                jrs.addRowSet(webRs, 1);
+                fail("should throw ClassCastException");
+            } catch (ClassCastException e) {
+                // expected
+            }
         }
         jrs.close();
 
         /*
          * Test non-empty WebRowSet.
          */
-        // TODO uncomment it after implementing addRowSet(RowSet, int)
-        // jrs = newJoinRowSet();
-        // webRs = newWebRowSet();
-        // webRs.populate(st.executeQuery("SELECT * FROM USER_INFO"));
-        // jrs.addRowSet(webRs, 1);
-        // assertTrue(jrs.next());
-        // assertEquals(1, jrs.getInt(1));
-        // jrs.close();
+        jrs = newJoinRowSet();
+        webRs = newWebRowSet();
+        webRs.populate(st.executeQuery("SELECT * FROM USER_INFO"));
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            jrs.addRowSet(webRs, 1);
+        } else {
+            try {
+                jrs.addRowSet(webRs, 1);
+                fail("should throw ClassCastException");
+            } catch (ClassCastException e) {
+                // expected
+            }
+        }
+        jrs.close();
     }
 
     public void testAddJoinableRowSet_FilteredRowSet_Exception()
@@ -604,6 +626,7 @@
                 }
             }
         }
+
     }
 
     private boolean isTypeMatch(int first, int second) throws Exception {
@@ -750,5 +773,92 @@
                 // expected
             }
         }
+    }
+
+    public void testToCachedRowSet_CachedRowSet() throws Exception {
+        /*
+         * Test empty JoinRowSet
+         */
+        CachedRowSet emptyToCrset = jrs.toCachedRowSet();
+        assertNull(emptyToCrset.getMetaData());
+        assertFalse(emptyToCrset.first());
+
+        /*
+         * The first CachedRowSet
+         */
+        jrs = newJoinRowSet();
+        jrs.addRowSet(crset, 1);
+        CachedRowSet toCrset = jrs.toCachedRowSet();
+        // check metadata
+        assertSame(crset.getMetaData(), jrs.getMetaData());
+        assertSame(crset.getMetaData(), toCrset.getMetaData());
+        assertNotSame(crset, toCrset);
+        // check data
+        int index = 0;
+        toCrset.beforeFirst();
+        crset.beforeFirst();
+        while (toCrset.next() && crset.next()) {
+            for (int i = 1; i <= crset.getMetaData().getColumnCount(); i++) {
+                assertEquals(toCrset.getObject(i), crset.getObject(i));
+            }
+            index++;
+        }
+        assertEquals(4, index);
+
+        /*
+         * The second CachedRowSet
+         */
+        noInitialCrset.populate(st.executeQuery("SELECT * FROM BOOKS"));
+        jrs.addRowSet(noInitialCrset, 1);
+        toCrset = jrs.toCachedRowSet();
+        // check metadata
+        assertSame(jrs.getMetaData(), toCrset.getMetaData());
+        /*
+         * check data. The data order is not the same between RI and HY.
+         */
+        index = 0;
+        rs = st.executeQuery("SELECT * FROM BOOKS");
+        if ("true".equals(System.getProperty("Testing Harmony"))) {
+            toCrset.beforeFirst();
+            while (toCrset.next() && rs.next()) {
+                index++;
+                assertEquals(toCrset.getObject(14), rs.getObject(3));
+            }
+        } else {
+            toCrset.afterLast();
+            while (toCrset.previous() && rs.next()) {
+                index++;
+                assertEquals(toCrset.getObject(14), rs.getObject(3));
+            }
+        }
+        assertEquals(6, index);
+
+        /*
+         * The third CachedRowSet
+         */
+        insertDataToCustomerTable();
+        CachedRowSet thirdCrset = newNoInitialInstance();
+        thirdCrset.populate(st.executeQuery("SELECT * FROM CUSTOMER_INFO"));
+        jrs.addRowSet(thirdCrset, 1);
+        toCrset = jrs.toCachedRowSet();
+        // check metadata
+        assertSame(jrs.getMetaData(), toCrset.getMetaData());
+        // check data
+        toCrset.beforeFirst();
+        index = 1;
+        while (toCrset.next()) {
+            if (index == 1) {
+                assertEquals(3, toCrset.getInt(1));
+                index++;
+            } else if (index == 2) {
+                assertEquals(3, toCrset.getInt(1));
+                index++;
+            } else if (index == 3) {
+                assertEquals(4, toCrset.getInt(1));
+            } else {
+                index = -1;
+            }
+        }
+        assertEquals(3, index);
     }
 }

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/WebRowSetTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/WebRowSetTest.java?rev=647681&r1=647680&r2=647681&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/WebRowSetTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/WebRowSetTest.java Mon Apr 14 00:37:02 2008
@@ -29,6 +29,81 @@
 
     protected WebRowSet another;
 
+    public void testCreateCopy() throws Exception {
+        webRs = newWebRowSet();
+        webRs.setUrl(DERBY_URL);
+        webRs.setCommand("SELECT * FROM USER_INFO WHERE ID = ?");
+        webRs.setInt(1, 3);
+        webRs.execute();
+
+        CachedRowSet copyWebRs = webRs.createCopy();
+        assertTrue(copyWebRs instanceof WebRowSet);
+        assertEquals("SELECT * FROM USER_INFO WHERE ID = ?", copyWebRs
+                .getCommand());
+        assertEquals(DERBY_URL, copyWebRs.getUrl());
+    }
+
+    public void testCreateCopy2() throws Exception {
+        webRs = newWebRowSet();
+        webRs.setUrl(DERBY_URL);
+        webRs.setCommand("SELECT * FROM USER_INFO WHERE ID = ? AND NAME = ?");
+        webRs.setInt(1, 3);
+        webRs.setString(2, "test3");
+        webRs.execute();
+        // check data
+        assertTrue(webRs.next());
+        assertEquals(3, webRs.getInt(1));
+        assertFalse(webRs.next());
+
+        // deep copy
+        CachedRowSet copyWebRs = webRs.createCopy();
+        copyWebRs.beforeFirst();
+        assertTrue(copyWebRs.next());
+        assertEquals(3, copyWebRs.getInt(1));
+        assertFalse(copyWebRs.next());
+        copyWebRs.execute();
+        assertTrue(copyWebRs.next());
+        assertEquals(3, copyWebRs.getInt(1));
+        assertFalse(copyWebRs.next());
+
+        webRs.setInt(1, 4);
+        webRs.setString(2, "test4");
+        webRs.execute();
+        webRs.beforeFirst();
+        assertTrue(webRs.next());
+        assertEquals(4, webRs.getInt(1));
+        assertFalse(webRs.next());
+
+        copyWebRs.beforeFirst();
+        assertTrue(copyWebRs.next());
+        assertEquals(3, copyWebRs.getInt(1));
+        assertFalse(copyWebRs.next());
+
+        copyWebRs.execute();
+        copyWebRs.beforeFirst();
+        assertTrue(copyWebRs.next());
+        assertEquals(3, copyWebRs.getInt(1));
+        assertFalse(copyWebRs.next());
+
+        copyWebRs.setInt(1, 1);
+        copyWebRs.setString(2, "hermit");
+        copyWebRs.execute();
+        assertTrue(copyWebRs.next());
+        assertEquals(1, copyWebRs.getInt(1));
+        assertFalse(copyWebRs.next());
+
+        webRs.beforeFirst();
+        assertTrue(webRs.next());
+        assertEquals(4, webRs.getInt(1));
+        assertFalse(webRs.next());
+
+        webRs.execute();
+        webRs.beforeFirst();
+        assertTrue(webRs.next());
+        assertEquals(4, webRs.getInt(1));
+        assertFalse(webRs.next());
+    }
+
     public void testWriteAndRead() throws Exception {
         rs = st.executeQuery("SELECT * FROM USER_INFO");
         webRs = newWebRowSet();



Mime
View raw message