harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r596263 - 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, 19 Nov 2007 10:54:55 GMT
Author: tonywu
Date: Mon Nov 19 02:54:50 2007
New Revision: 596263

URL: http://svn.apache.org/viewvc?rev=596263&view=rev
Log:
Apply patch HARMONY-5147 ([classlib][sql] refactor CachedRowSetImplTest, add more columns
in test table)

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/CachedRowSetWriter.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=596263&r1=596262&r2=596263&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 19 02:54:50 2007
@@ -360,12 +360,25 @@
         RowSetMetaDataImpl rowSetMetaData = new RowSetMetaDataImpl();
         rowSetMetaData.setColumnCount(metaData.getColumnCount());
         for (int i = 1; i <= metaData.getColumnCount(); i++) {
+            rowSetMetaData.setAutoIncrement(i, metaData.isAutoIncrement(i));
+            rowSetMetaData.setCaseSensitive(i, metaData.isCaseSensitive(i));
+            rowSetMetaData.setCatalogName(i, metaData.getCatalogName(i));
+            rowSetMetaData.setColumnDisplaySize(i, metaData
+                    .getColumnDisplaySize(i));
+            rowSetMetaData.setColumnLabel(i, metaData.getColumnLabel(i));
             rowSetMetaData.setColumnName(i, metaData.getColumnName(i));
-            rowSetMetaData.setTableName(i, metaData.getTableName(i));
+            rowSetMetaData.setColumnType(i, metaData.getColumnType(i));
+            rowSetMetaData.setColumnTypeName(i, metaData.getColumnTypeName(i));
+            rowSetMetaData.setCurrency(i, metaData.isCurrency(i));
+            rowSetMetaData.setNullable(i, metaData.isNullable(i));
+            rowSetMetaData.setPrecision(i, metaData.getPrecision(i));
+            rowSetMetaData.setScale(i, metaData.getScale(i));
             rowSetMetaData.setSchemaName(i, metaData.getSchemaName(i));
-            rowSetMetaData.setCatalogName(i, metaData.getCatalogName(i));
+            rowSetMetaData.setSearchable(i, metaData.isSearchable(i));
+            rowSetMetaData.setSigned(i, metaData.isSigned(i));
+            rowSetMetaData.setTableName(i, metaData.getTableName(i));
+
         }
-        // TODO set other meta info when necessary
         this.meta = rowSetMetaData;
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetWriter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetWriter.java?rev=596263&r1=596262&r2=596263&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetWriter.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/rowset/CachedRowSetWriter.java
Mon Nov 19 02:54:50 2007
@@ -32,7 +32,6 @@
 
 public class CachedRowSetWriter implements RowSetWriter {
 
-
     private ResultSet primaryKeys;
 
     private CachedRowSet originalRowSet;
@@ -56,7 +55,6 @@
     private String keyColumnName, whereStatementForOriginal,
             whereStatementForCurrent;
 
-
     public boolean writeData(RowSetInternal theRowSet) throws SQLException {
         // use an optimistic concurrency control mechanism
 
@@ -64,14 +62,16 @@
         // analyse every row and do responsible task.
         currentRowSet.first();
         originalRowSet.first();
-        do{
+        do {
             // rolling with currentRowSet
             if (originalRowSet.next()) {
                 // deal with updated or deleted row which need do conflict check
                 if (checkConflictNotExist(originalRowSet)) {
-                    //If all of the values in the data source are already the values to be
persisted, 
-                    //the method acceptChanges does nothing. 
-                    if(!checkConflictNotExist(currentRowSet))  writeRowData();
+                    // If all of the values in the data source are already the
+                    // values to be persisted,
+                    // the method acceptChanges does nothing.
+                    if (!checkConflictNotExist(currentRowSet))
+                        writeRowData();
                 } else {
                     cleanEnvironment();
                     throw new SyncProviderException(Messages
@@ -84,14 +84,14 @@
                 // FIXME: need pre-check before insert into database?
                 writeRowData();
             }
-        }while (currentRowSet.next());
+        } while (currentRowSet.next());
 
         cleanEnvironment();
 
         return true;
     }
-    
-    private void initial(RowSetInternal theRowSet) throws SQLException {      
+
+    private void initial(RowSetInternal theRowSet) throws SQLException {
         currentRowSet = (CachedRowSetImpl) theRowSet;
         // initial environment
         originalRowSet = (CachedRowSet) currentRowSet.getOriginal();
@@ -104,7 +104,6 @@
         cachedKeySet.populate(primaryKeys);
     }
 
-
     private void writeRowData() throws SQLException {
         try {
             createScriptForWriteBack();
@@ -131,27 +130,26 @@
 
     }
 
-
     private void createScriptForWriteBack() throws SQLException {
         cachedKeySet.first();
         whereStatementForCurrent = "";
         String insertCollector = "", updateCollector = "";
         // FIXME:uses getUpdateMask()
 
-         do{
+        do {
             keyColumnName = cachedKeySet.getString("COLUMN_NAME");
             whereStatementForCurrent = whereStatementForCurrent + keyColumnName
                     + " = ? " + " and ";
-        }while (cachedKeySet.next());
+        } while (cachedKeySet.next());
 
         whereStatementForCurrent = subStringN(whereStatementForCurrent, 5);
 
         // insertCollector: all column
         for (int i = 1; i <= columnCount; i++) {
-            insertCollector = insertCollector + " ? "+" , ";            
+            insertCollector = insertCollector + " ? " + " , ";
         }
         insertCollector = subStringN(insertCollector, 3);
-        
+
         // update: all column
         ResultSetMetaData tempRSMD = currentRowSet.getMetaData();
         for (int i = 1; i <= columnCount; i++) {
@@ -206,18 +204,25 @@
     private void fillParasOfKeys(CachedRowSet inputRS, int from)
             throws SQLException {
         cachedKeySet.first();
-        int i = from+1;        
+        int i = from + 1;
         do {
             keyColumnName = cachedKeySet.getString("COLUMN_NAME");
             ((PreparedStatement) statement).setObject(i++, inputRS
                     .getObject(keyColumnName));
-        } while (cachedKeySet.next()); 
+        } while (cachedKeySet.next());
     }
 
     private void fillParasOfAllColumn() throws SQLException {
-        for (int i = 1; i <= columnCount; i++)
-            ((PreparedStatement) statement).setObject(i, currentRowSet
-                    .getObject(i));
+        for (int i = 1; i <= columnCount; i++) {
+            ResultSetMetaData rsmd = currentRowSet.getMetaData();
+            if (currentRowSet.getObject(i) == null) {
+                ((PreparedStatement) statement).setNull(i, rsmd
+                        .getColumnType(i));
+            } else {
+                ((PreparedStatement) statement).setObject(i, currentRowSet
+                        .getObject(i));
+            }
+        }
     }
 
     private boolean checkConflictNotExist(CachedRowSet crs) {
@@ -230,8 +235,12 @@
             // compare line by line, column by column
             if (dataInDB.next()) {
                 for (int i = 1; i <= dataInDB.getMetaData().getColumnCount(); i++) {
-                    if (!(dataInDB.getObject(i).equals(crs.getObject(i))))
+                    if (dataInDB.getObject(i) == crs.getObject(i)) {
+                        continue;
+                    }
+                    if (!(dataInDB.getObject(i).equals(crs.getObject(i)))) {
                         return false;
+                    }
                 }
             }
         } catch (Exception e) {
@@ -241,7 +250,7 @@
     }
 
     private void cleanEnvironment() {
-        try {           
+        try {
             originalRowSet.close();
             originalConnection.close();
             cachedKeySet.close();

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=596263&r1=596262&r2=596263&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 19 02:54:50 2007
@@ -16,11 +16,16 @@
  */
 package org.apache.harmony.sql.tests.internal.rowset;
 
+import java.math.BigDecimal;
 import java.sql.Connection;
+import java.sql.Date;
 import java.sql.DriverManager;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.sql.Time;
+import java.sql.Timestamp;
 
 import javax.sql.RowSetMetaData;
 import javax.sql.rowset.CachedRowSet;
@@ -33,7 +38,7 @@
 
     private static final String DERBY_URL = "jdbc:derby:src/test/resources/TESTDB";
 
-    private Connection conn;
+    private Connection conn = null;
 
     private Statement st;
 
@@ -41,8 +46,11 @@
 
     private CachedRowSet crset;
 
-    public void setUp() throws IllegalAccessException, InstantiationException,
-            ClassNotFoundException, SQLException {
+    private final static int DEFAULT_COLUMN_COUNT = 12;
+
+    private final static int DEFAULT_ROW_COUNT = 4;
+
+    public void setUp() throws Exception {
         Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
 
         try {
@@ -57,20 +65,18 @@
         ;
 
         st = conn.createStatement();
-
         rs = conn.getMetaData().getTables(null, "APP", "USER_INFO", null);
-        // careful: Integer, rather than int!
+        String createTableSQL = "create table USER_INFO (ID INTEGER NOT NULL,NAME VARCHAR(10)
NOT NULL, BIGINT_T BIGINT, "
+                + "NUMERIC_T NUMERIC, DECIMAL_T DECIMAL, SMALLINT_T SMALLINT, FLOAT_T FLOAT,
REAL_T REAL, DOUBLE_T DOUBLE,"
+                + "DATE_T DATE, TIME_T TIME, TIMESTAMP_T TIMESTAMP)";
+        String alterTableSQL = "ALTER TABLE USER_INFO  ADD CONSTRAINT USER_INFO_PK Primary
Key (ID)";
 
         if (!rs.next()) {
-            st
-                    .execute("create table USER_INFO (ID INTEGER NOT NULL,NAME VARCHAR(10)
NOT NULL)");
-            st
-                    .execute("ALTER TABLE USER_INFO  ADD CONSTRAINT USER_INFO_PK Primary
Key (ID)");
+            st.execute(createTableSQL);
+            st.execute(alterTableSQL);
         }
 
-        st.executeUpdate("delete from USER_INFO");
-        st.executeUpdate("insert into USER_INFO(ID,NAME) values (1,'hermit')");
-        st.executeUpdate("insert into USER_INFO(ID,NAME) values (2,'test')");
+        insertData();
         rs = st.executeQuery("select * from USER_INFO");
         try {
             crset = (CachedRowSet) Class.forName(
@@ -93,12 +99,19 @@
         crset.setUrl(DERBY_URL);
     }
 
-    public void tearDown() throws SQLException {
+    public void tearDown() throws Exception {
         if (rs != null) {
             rs.close();
         }
-        if (crset != null)
+        if (crset != null) {
             crset.close();
+        }
+        if (st != null) {
+            st.close();
+        }
+        if (conn != null) {
+            conn.close();
+        }
     }
 
     public void testSetSyncProvider() throws Exception {
@@ -186,7 +199,7 @@
     }
 
     public void testSize() {
-        assertEquals(2, crset.size());
+        assertEquals(DEFAULT_ROW_COUNT, crset.size());
     }
 
     public void testDeleteRow() throws SQLException {
@@ -200,7 +213,7 @@
         crset.next();
         assertFalse(crset.rowDeleted());
         crset.deleteRow();
-        assertEquals(2, crset.size());
+        assertEquals(DEFAULT_ROW_COUNT, crset.size());
         assertTrue(crset.rowDeleted());
     }
 
@@ -243,15 +256,16 @@
     }
 
     public void testAcceptChanges() throws SQLException {
+        // FIXME: if the value of column is null, it would go wrong when
+        // call acceptChanges(). And if one method in TestCase throws
+        // SQLException, the following method will be affected.
         rs.next();
         assertEquals(1, rs.getInt(1));
-
         assertEquals("hermit", rs.getString(2));
-        crset.first();
 
-        assertEquals(1, crset.getInt(1));
-
-        assertEquals("hermit", crset.getString(2));
+        crset.absolute(3);
+        assertEquals(3, crset.getInt(1));
+        assertEquals("test3", crset.getString(2));
         crset.updateString(2, "HarmonY");
 
         crset.moveToInsertRow();
@@ -268,9 +282,10 @@
 
         rs = st.executeQuery("select * from USER_INFO");
         rs.next();
-        assertEquals(rs.getString(2), "test");
+        assertEquals(rs.getString(2), "hermit");
         rs.next();
-        assertEquals(rs.getString(2), "Apache");
+        rs.next();
+        assertEquals(rs.getString(2), "test4");
 
     }
 
@@ -346,7 +361,7 @@
         RowSetMetaData rsmCopySchema = (RowSetMetaData) crsetCopySchema
                 .getMetaData();
         assertEquals("USER_INFO", rsmCopySchema.getTableName(1));
-        assertEquals(2, rsmCopySchema.getColumnCount());
+        assertEquals(DEFAULT_COLUMN_COUNT, rsmCopySchema.getColumnCount());
 
         RowSetMetaData rsm = (RowSetMetaData) crset.getMetaData();
         rsm.setTableName(1, "newBorn");
@@ -357,32 +372,35 @@
     }
 
     public void testCreateCopy() throws Exception {
-        crset.first();
-        assertEquals(crset.getString(2), "hermit");
-        crset.updateString(2, "copyTest");
+        // crset.first();
+        crset.absolute(3);
+        assertEquals(crset.getString(2), "test3");
+        crset.updateString(2, "copyTest3");
         crset.updateRow();
         crset.acceptChanges();
 
         rs = st.executeQuery("select * from USER_INFO");
         rs.next();
-        assertEquals(rs.getString(2), "copyTest");
+        assertEquals(rs.getString(2), "hermit");
 
         CachedRowSet crsetCopy = (CachedRowSet) crset.createCopy();
 
-        crsetCopy.first();
-        crsetCopy.updateString(2, "copyTest2");
+        // crsetCopy.first();
+        crsetCopy.absolute(3);
+        crsetCopy.updateString(2, "copyTest3");
         crsetCopy.updateRow();
         crsetCopy.acceptChanges();
 
-        assertEquals(crsetCopy.getString(2), "copyTest2");
-        assertEquals(crset.getString(2), "copyTest");
+        assertEquals(crsetCopy.getString(2), "copyTest3");
+        assertEquals(crset.getString(2), "copyTest3");
 
         rs = st.executeQuery("select * from USER_INFO");
         rs.next();
-        assertEquals(rs.getString(2), "copyTest2");
+        assertEquals(rs.getString(2), "hermit");
 
-        crset.first();
-        assertEquals(crset.getString(2), "copyTest");
+        // crset.first();
+        crset.absolute(3);
+        assertEquals(crset.getString(2), "copyTest3");
     }
 
     public void testAfterLast() throws Exception {
@@ -395,7 +413,7 @@
 
         crset.afterLast();
         crset.previous();
-        assertEquals(2, crset.getInt(1));
+        assertEquals(4, crset.getInt(1));
     }
 
     public void testNextandPreviousPage() throws Exception {
@@ -460,5 +478,52 @@
         crset.populate(cc, 1);
         crset.first();
         assertEquals("hermit", crset.getString(2));
+    }
+
+    private void insertData() throws Exception {
+
+        st.executeUpdate("delete from USER_INFO");
+
+        // first row
+        st.executeUpdate("insert into USER_INFO(ID,NAME) values (1,'hermit')");
+        // second row
+        st.executeUpdate("insert into USER_INFO(ID,NAME) values (2,'test')");
+
+        String insertSQL = "INSERT INTO USER_INFO(ID, NAME, BIGINT_T, NUMERIC_T, DECIMAL_T,
SMALLINT_T, "
+                + "FLOAT_T, REAL_T, DOUBLE_T, DATE_T, TIME_T, TIMESTAMP_T) VALUES(?, ?, ?,
?, ?, ?,"
+                + "?, ?, ?, ?, ?, ? )";
+        PreparedStatement preStmt = conn.prepareStatement(insertSQL);
+        // third row
+        preStmt.setInt(1, 3);
+        preStmt.setString(2, "test3");
+        preStmt.setLong(3, 3333L);
+        preStmt.setBigDecimal(4, new BigDecimal(123));
+        preStmt.setBigDecimal(5, new BigDecimal(23));
+        preStmt.setInt(6, 13);
+        preStmt.setFloat(7, 3.7F);
+        preStmt.setFloat(8, 3.888F);
+        preStmt.setDouble(9, 3.9999);
+        preStmt.setDate(10, new Date(523654123));
+        preStmt.setTime(11, new Time(966554221));
+        preStmt.setTimestamp(12, new Timestamp(521342100));
+        preStmt.executeUpdate();
+        // fourth row
+        preStmt.setInt(1, 4);
+        preStmt.setString(2, "test4");
+        preStmt.setLong(3, 444423L);
+        preStmt.setBigDecimal(4, new BigDecimal(12));
+        preStmt.setBigDecimal(5, new BigDecimal(23));
+        preStmt.setInt(6, 41);
+        preStmt.setFloat(7, 4.8F);
+        preStmt.setFloat(8, 4.888F);
+        preStmt.setDouble(9, 4.9999);
+        preStmt.setDate(10, new Date(965324512));
+        preStmt.setTime(11, new Time(452368512));
+        preStmt.setTimestamp(12, new Timestamp(874532105));
+        preStmt.executeUpdate();
+
+        if (preStmt != null) {
+            preStmt.close();
+        }
     }
 }



Mime
View raw message