You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by lv...@apache.org on 2008/03/07 08:25:24 UTC
svn commit: r634570 - in /harmony/enhanced/classlib/trunk/modules/sql/src:
main/java/org/apache/harmony/sql/internal/nls/
main/java/org/apache/harmony/sql/internal/rowset/
test/java/org/apache/harmony/sql/tests/internal/rowset/
Author: lvjing
Date: Thu Mar 6 23:25:23 2008
New Revision: 634570
URL: http://svn.apache.org/viewvc?rev=634570&view=rev
Log:
Apply patch for HARMONY-5582, [classlib][sql][rowset] refine insert, update, delete related methods in CachedRowSet
Added:
harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetInsertTest.java (with props)
Modified:
harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties
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
harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java
Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties?rev=634570&r1=634569&r2=634570&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties Thu Mar 6 23:25:23 2008
@@ -71,3 +71,4 @@
rowset.8=The ResultSet type is TYPE_FORWARD_ONLY
rowset.9=PageSize can not be larger than MaxRows
rowset.10=Data type mismatch
+rowset.11=Illegal operation on an insert row
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=634570&r1=634569&r2=634570&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 Thu Mar 6 23:25:23 2008
@@ -91,6 +91,8 @@
private CachedRow insertRow;
+ private boolean isCursorOnInsert;
+
// TODO remember to evalute it in CachedRowSetReader
private int[] keyCols;
@@ -177,24 +179,36 @@
}
public void acceptChanges() throws SyncProviderException {
- if (currentRow == insertRow && currentRow != null) {
- // TODO add error messages
- throw new SyncProviderException();
+ if (isCursorOnInsert) {
+ // rowset.11=Illegal operation on an insert row
+ throw new SyncProviderException(Messages.getString("rowset.11"));
}
+ Connection currentConn = null;
try {
- acceptChanges(getConnection());
+ currentConn = getConnection();
+ acceptChanges(currentConn);
} catch (SQLException e) {
SyncProviderException ex = new SyncProviderException();
ex.initCause(e);
throw ex;
+ } finally {
+ if (currentConn != null) {
+ try {
+ currentConn.close();
+ } catch (SQLException ex) {
+ SyncProviderException spe = new SyncProviderException();
+ spe.initCause(ex);
+ throw spe;
+ }
+ }
}
}
public void acceptChanges(Connection con) throws SyncProviderException {
- if (currentRow == insertRow && currentRow != null) {
- // TODO add error messages
- throw new SyncProviderException();
+ if (isCursorOnInsert) {
+ // rowset.11=Illegal operation on an insert row
+ throw new SyncProviderException(Messages.getString("rowset.11"));
}
try {
@@ -220,10 +234,6 @@
}
boolean isChanged = false;
- /*
- * FIXME: if no conflicts happen when writeData, then call
- * setOriginalRow()
- */
for (int i = rows.size() - 1; i >= 0; i--) {
currentRow = rows.get(i);
if (rowDeleted()) {
@@ -244,7 +254,6 @@
}
originalResultSet.setRows(nowRows, columnCount);
} catch (CloneNotSupportedException cloneE) {
- // TODO how to deal with the CloneNotSupportedException
throw new SyncProviderException(cloneE.getMessage());
}
}
@@ -610,7 +619,8 @@
}
rows.removeAll(insertedRows);
insertRow = null;
-
+ isCursorOnInsert = false;
+
first();
// TODO fire rowSetChanged event
@@ -712,15 +722,26 @@
}
public void undoInsert() throws SQLException {
- throw new NotImplementedException();
+ // TODO notify listener
+ checkValidRow();
+ if (isCursorOnInsert) {
+ // rowset.11=Illegal operation on an insert row
+ throw new SQLException(Messages.getString("rowset.11"));
+ }
+ if (!rowInserted()) {
+ // rowset.4=Not an insert row
+ throw new SQLException(Messages.getString("rowset.4"));
+ }
+ rows.remove(currentRow);
+ next();
}
public void undoUpdate() throws SQLException {
- if (currentRow == null) {
- // TODO add error message
- throw new SQLException();
+ checkValidRow();
+ if (isCursorOnInsert && insertRow == null) {
+ // rowset.11=Illegal operation on an insert row
+ throw new SQLException(Messages.getString("rowset.11"));
}
-
if (currentRow == insertRow) {
currentRow = new CachedRow(new Object[columnCount]);
} else if (rowUpdated()) {
@@ -783,6 +804,10 @@
* @throws SQLException
*/
private boolean doAbsolute(int row, boolean checkType) throws SQLException {
+ if (isCursorOnInsert) {
+ // rowset.0=Not a valid position
+ throw new SQLException(Messages.getString("rowset.0")); //$NON-NLS-1$
+ }
if (rows == null || rows.size() == 0) {
return false;
}
@@ -949,11 +974,14 @@
}
public Array getArray(int columnIndex) throws SQLException {
- throw new NotImplementedException();
+ Object obj = getObject(columnIndex);
+ if (obj == null)
+ return null;
+ return (Array) obj;
}
public Array getArray(String colName) throws SQLException {
- throw new NotImplementedException();
+ return getArray(getIndexByName(colName));
}
public InputStream getAsciiStream(int columnIndex) throws SQLException {
@@ -965,8 +993,10 @@
}
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
- // TODO re-implement it
- return (BigDecimal) getObject(columnIndex);
+ Object obj = getObject(columnIndex);
+ if (obj == null)
+ return null;
+ return (BigDecimal) obj;
}
public BigDecimal getBigDecimal(int columnIndex, int scale)
@@ -975,12 +1005,12 @@
}
public BigDecimal getBigDecimal(String columnName) throws SQLException {
- throw new NotImplementedException();
+ return getBigDecimal(getIndexByName(columnName));
}
public BigDecimal getBigDecimal(String columnName, int scale)
throws SQLException {
- throw new NotImplementedException();
+ return getBigDecimal(getIndexByName(columnName), scale);
}
public InputStream getBinaryStream(int columnIndex) throws SQLException {
@@ -992,12 +1022,10 @@
}
public Blob getBlob(int columnIndex) throws SQLException {
- checkValidRow();
- Object value = currentRow.getObject(columnIndex);
- if (value == null) {
+ Object obj = getObject(columnIndex);
+ if (obj == null)
return null;
- }
- return (Blob) value;
+ return (Blob) obj;
}
public Blob getBlob(String columnName) throws SQLException {
@@ -1005,12 +1033,10 @@
}
public boolean getBoolean(int columnIndex) throws SQLException {
- checkValidRow();
- Object value = currentRow.getObject(columnIndex);
- if (value == null) {
+ Object obj = getObject(columnIndex);
+ if (obj == null)
return false;
- }
- return ((Boolean) value).booleanValue();
+ return (Boolean) obj;
}
public boolean getBoolean(String columnName) throws SQLException {
@@ -1018,12 +1044,10 @@
}
public byte getByte(int columnIndex) throws SQLException {
- checkValidRow();
- Object value = currentRow.getObject(columnIndex);
- if (value == null) {
+ Object obj = getObject(columnIndex);
+ if (obj == null)
return 0;
- }
- return ((Byte) value).byteValue();
+ return (Byte) obj;
}
public byte getByte(String columnName) throws SQLException {
@@ -1031,12 +1055,10 @@
}
public byte[] getBytes(int columnIndex) throws SQLException {
- checkValidRow();
- Object value = currentRow.getObject(columnIndex);
- if (value == null) {
+ Object obj = getObject(columnIndex);
+ if (obj == null)
return null;
- }
- return (byte[]) value;
+ return (byte[]) obj;
}
public byte[] getBytes(String columnName) throws SQLException {
@@ -1052,11 +1074,14 @@
}
public Clob getClob(int columnIndex) throws SQLException {
- throw new NotImplementedException();
+ Object obj = getObject(columnIndex);
+ if (obj == null)
+ return null;
+ return (Clob) obj;
}
public Clob getClob(String colName) throws SQLException {
- throw new NotImplementedException();
+ return getClob(getIndexByName(colName));
}
public String getCursorName() throws SQLException {
@@ -1064,7 +1089,10 @@
}
public Date getDate(int columnIndex) throws SQLException {
- throw new NotImplementedException();
+ Object obj = getObject(columnIndex);
+ if (obj == null)
+ return null;
+ return (Date) obj;
}
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
@@ -1072,36 +1100,40 @@
}
public Date getDate(String columnName) throws SQLException {
- throw new NotImplementedException();
+ return getDate(getIndexByName(columnName));
}
public Date getDate(String columnName, Calendar cal) throws SQLException {
- throw new NotImplementedException();
+ return getDate(getIndexByName(columnName), cal);
}
public double getDouble(int columnIndex) throws SQLException {
- throw new NotImplementedException();
+ Object obj = getObject(columnIndex);
+ if (obj == null)
+ return 0;
+ return (Double) obj;
}
public double getDouble(String columnName) throws SQLException {
- throw new NotImplementedException();
+ return getDouble(getIndexByName(columnName));
}
public float getFloat(int columnIndex) throws SQLException {
- throw new NotImplementedException();
+ Object obj = getObject(columnIndex);
+ if (obj == null)
+ return 0;
+ return (Float) obj;
}
public float getFloat(String columnName) throws SQLException {
- throw new NotImplementedException();
+ return getFloat(getIndexByName(columnName));
}
public int getInt(int columnIndex) throws SQLException {
- checkValidRow();
- Object value = currentRow.getObject(columnIndex);
- if (value == null) {
+ Object obj = getObject(columnIndex);
+ if (obj == null)
return 0;
- }
- return ((Integer) value).intValue();
+ return (Integer) obj;
}
public int getInt(String columnName) throws SQLException {
@@ -1109,11 +1141,14 @@
}
public long getLong(int columnIndex) throws SQLException {
- throw new NotImplementedException();
+ Object obj = getObject(columnIndex);
+ if (obj == null)
+ return 0;
+ return (Long) obj;
}
public long getLong(String columnName) throws SQLException {
- throw new NotImplementedException();
+ return getLong(getIndexByName(columnName));
}
public ResultSetMetaData getMetaData() throws SQLException {
@@ -1121,7 +1156,14 @@
}
public Object getObject(int columnIndex) throws SQLException {
- // TODO re-implement it
+ if (meta == null || currentRow == null) {
+ // rowset.7=Not a valid cursor
+ throw new SQLException(Messages.getString("rowset.7"));
+ }
+ if (columnIndex <= 0 || columnIndex > columnCount) {
+ // sql.27=Invalid column index :{0}
+ throw new SQLException(Messages.getString("sql.27", columnIndex));
+ }
return currentRow.getObject(columnIndex);
}
@@ -1131,12 +1173,12 @@
}
public Object getObject(String columnName) throws SQLException {
- return currentRow.getObject(getIndexByName(columnName));
+ return getObject(getIndexByName(columnName));
}
public Object getObject(String columnName, Map<String, Class<?>> map)
throws SQLException {
- throw new NotImplementedException();
+ return getObject(getIndexByName(columnName), map);
}
public Ref getRef(int columnIndex) throws SQLException {
@@ -1266,6 +1308,17 @@
// rowset.4=Not an insert row
throw new SQLException(Messages.getString("rowset.4")); //$NON-NLS-1$
}
+ boolean isValueSet = false;
+ for (int i = 1; i <= columnCount; i++) {
+ if (currentRow.getUpdateMask(i)) {
+ isValueSet = true;
+ break;
+ }
+ }
+ if (!isValueSet) {
+ // TODO add error message
+ throw new SQLException();
+ }
insertRow.setInsert();
rows.add(insertRow);
insertRow = null;
@@ -1304,22 +1357,28 @@
}
public void moveToCurrentRow() throws SQLException {
- if (rememberedCursorPosition != -1) {
+ if (isCursorOnInsert) {
currentRowIndex = rememberedCursorPosition;
- if (currentRowIndex >= 1 && currentRowIndex <= size()) {
+ if (currentRowIndex >= 1 && currentRowIndex <= rows.size()) {
currentRow = rows.get(currentRowIndex - 1);
} else {
currentRow = null;
}
rememberedCursorPosition = -1;
+ isCursorOnInsert = false;
}
}
public void moveToInsertRow() throws SQLException {
+ if (meta == null) {
+ // TODO add error message
+ throw new SQLException();
+ }
insertRow = new CachedRow(new Object[columnCount]);
currentRow = insertRow;
rememberedCursorPosition = currentRowIndex;
currentRowIndex = -1;
+ isCursorOnInsert = true;
}
public boolean next() throws SQLException {
@@ -1392,7 +1451,11 @@
}
public void refreshRow() throws SQLException {
- throw new NotImplementedException();
+ checkValidRow();
+ if (isCursorOnInsert) {
+ // rowset.0=Not a valid position
+ throw new SQLException(Messages.getString("rowset.0"));
+ }
}
public boolean relative(int moveRows) throws SQLException {
@@ -1422,7 +1485,7 @@
}
public boolean rowInserted() throws SQLException {
- if (currentRow == null || currentRow == insertRow) {
+ if (currentRow == null || isCursorOnInsert) {
// TODO add error message
throw new SQLException();
}
@@ -1430,7 +1493,6 @@
}
public boolean rowUpdated() throws SQLException {
-
if (currentRow == null || currentRow == insertRow) {
// TODO add error message
throw new SQLException();
@@ -1595,6 +1657,10 @@
public void updateObject(int columnIndex, Object x) throws SQLException {
checkValidRow();
checkColumnValid(columnIndex);
+ if (isCursorOnInsert && insertRow == null) {
+ insertRow = new CachedRow(new Object[columnCount]);
+ currentRow = insertRow;
+ }
currentRow.updateObject(columnIndex, x);
}
@@ -1620,7 +1686,10 @@
// x = ((BigDecimal) x).setScale(scale);
// }
}
-
+ if (isCursorOnInsert && insertRow == null) {
+ insertRow = new CachedRow(new Object[columnCount]);
+ currentRow = insertRow;
+ }
currentRow.updateObject(columnIndex, x);
}
@@ -1674,6 +1743,10 @@
throws SQLException {
checkValidRow();
checkColumnValid(columnIndex);
+ if (isCursorOnInsert && insertRow == null) {
+ insertRow = new CachedRow(new Object[columnCount]);
+ currentRow = insertRow;
+ }
currentRow.updateObject(columnIndex, convertUpdateValue(columnIndex,
value));
}
@@ -1728,8 +1801,8 @@
return bigDecimal.intValueExact();
} catch (ArithmeticException e) {
- // TODO load from resource file
- throw new SQLException("Data Type Mismatch"); //$NON-NLS-1$
+ // rowset.10=Data Type Mismatch
+ throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
}
}
@@ -1760,8 +1833,8 @@
return bigDecimal.intValueExact();
} catch (ArithmeticException e) {
- // TODO load from resource file
- throw new SQLException("Data Type Mismatch"); //$NON-NLS-1$
+ // rowset.10=Data Type Mismatch
+ throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
}
}
if (value instanceof String) {
@@ -1797,8 +1870,8 @@
return bigDecimal.byteValueExact();
} catch (ArithmeticException e) {
- // TODO load from resource file
- throw new SQLException("Data Type Mismatch"); //$NON-NLS-1$
+ // rowset.10=Data Type Mismatch
+ throw new SQLException(Messages.getString("rowset.10")); //$NON-NLS-1$
}
}
if (value instanceof String) {
@@ -1879,6 +1952,7 @@
}
public Connection getConnection() throws SQLException {
+ // TODO consider user name, password and datasource
return DriverManager.getConnection(getUrl());
}
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=634570&r1=634569&r2=634570&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 Thu Mar 6 23:25:23 2008
@@ -142,10 +142,10 @@
*/
PreparedStatement preSt = getConnection().prepareStatement(
insertSQL.toString());
- for (int i = 0; i < updateCount; i++) {
- preSt.setObject(i + 1, insertColValues[i]);
- }
try {
+ for (int i = 0; i < updateCount; i++) {
+ preSt.setObject(i + 1, insertColValues[i]);
+ }
preSt.executeUpdate();
} catch (SQLException e) {
throw new SyncProviderException();
@@ -172,8 +172,12 @@
*/
PreparedStatement preSt = getConnection().prepareStatement(
deleteSQL.toString());
- fillParamInPreStatement(preSt, 1);
- preSt.executeUpdate();
+ try {
+ fillParamInPreStatement(preSt, 1);
+ preSt.executeUpdate();
+ } finally {
+ preSt.close();
+ }
}
/**
@@ -214,18 +218,18 @@
*/
PreparedStatement preSt = getConnection().prepareStatement(
updateSQL.toString());
- // the SET part of SQL
- for (int i = 0; i < updateCount; i++) {
- if (updateColValues[i] == null) {
- preSt.setNull(i + 1, currentRowSet.getMetaData().getColumnType(
- updateColIndexs[i]));
- } else {
- preSt.setObject(i + 1, updateColValues[i]);
- }
- }
- // the WHERE part of SQL
- fillParamInPreStatement(preSt, updateCount + 1);
try {
+ // the SET part of SQL
+ for (int i = 0; i < updateCount; i++) {
+ if (updateColValues[i] == null) {
+ preSt.setNull(i + 1, currentRowSet.getMetaData()
+ .getColumnType(updateColIndexs[i]));
+ } else {
+ preSt.setObject(i + 1, updateColValues[i]);
+ }
+ }
+ // the WHERE part of SQL
+ fillParamInPreStatement(preSt, updateCount + 1);
preSt.executeUpdate();
} catch (SQLException e) {
throw new SyncProviderException();
@@ -264,15 +268,21 @@
PreparedStatement preSt = getConnection().prepareStatement(
querySQL.toString());
- fillParamInPreStatement(preSt, 1);
- ResultSet queryRs = preSt.executeQuery();
- if (queryRs.next()) {
- if (queryRs.getInt(1) == 1) {
- isExist = false;
+ ResultSet queryRs = null;
+ try {
+ fillParamInPreStatement(preSt, 1);
+ queryRs = preSt.executeQuery();
+ if (queryRs.next()) {
+ if (queryRs.getInt(1) == 1) {
+ isExist = false;
+ }
}
+ } finally {
+ if (queryRs != null) {
+ queryRs.close();
+ }
+ preSt.close();
}
- queryRs.close();
- preSt.close();
return isExist;
}
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=634570&r1=634569&r2=634570&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 Thu Mar 6 23:25:23 2008
@@ -1273,6 +1273,25 @@
assertTrue(crset.isBeforeFirst());
}
+ crset.moveToInsertRow();
+ try {
+ crset.first();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ crset.updateInt(1, 60);
+ crset.updateString(2, "abc");
+ crset.insertRow();
+ try {
+ crset.absolute(3);
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ crset.moveToCurrentRow();
+ assertTrue(crset.absolute(2));
+
crset.setType(ResultSet.TYPE_FORWARD_ONLY);
try {
crset.absolute(1);
@@ -2262,6 +2281,55 @@
assertTrue(crset.next());
assertEquals(5, crset.getInt(1));
+ }
+
+ public void testRefreshRow() throws Exception {
+ noInitialCrset = newNoInitialInstance();
+ try {
+ noInitialCrset.refreshRow();
+ fail("should throw exception");
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // RI throw ArrayIndexOutOfBoundsException
+ } catch (SQLException e) {
+ // expected
+ }
+
+ rs = st.executeQuery("SELECT * FROM USER_INFO");
+ noInitialCrset.populate(rs);
+ assertTrue(noInitialCrset.isBeforeFirst());
+ try {
+ noInitialCrset.refreshRow();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected: Invalid cursor position
+ }
+
+ // when the cursor is on the insert row
+ noInitialCrset.moveToInsertRow();
+ try {
+ noInitialCrset.refreshRow();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected: Invalid cursor position
+ }
+
+ // move the cursor to the third row
+ noInitialCrset.moveToCurrentRow();
+ assertTrue(noInitialCrset.absolute(3));
+ // no effect
+ noInitialCrset.refreshRow();
+
+ /*
+ * Update the third row in database. Then call refreshRow().
+ */
+ int result = st
+ .executeUpdate("UPDATE USER_INFO SET NAME = 'update33' WHERE ID = 3");
+ assertEquals(1, result);
+
+ // still no effect.
+ noInitialCrset.refreshRow();
+ assertEquals(3, noInitialCrset.getInt(1));
+ assertEquals("test3", noInitialCrset.getString(2));
}
public class Listener implements RowSetListener, Cloneable {
Added: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetInsertTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetInsertTest.java?rev=634570&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetInsertTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetInsertTest.java Thu Mar 6 23:25:23 2008
@@ -0,0 +1,391 @@
+/*
+ * 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;
+
+public class CachedRowSetInsertTest extends CachedRowSetTestCase {
+
+ public void testRowInserted() throws Exception {
+ noInitialCrset = newNoInitialInstance();
+ try {
+ noInitialCrset.rowInserted();
+ fail("should throw exception");
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // RI throw exception here
+ } catch (SQLException e) {
+ // expected
+ }
+
+ rs = st.executeQuery("SELECT * FROM USER_INFO");
+ noInitialCrset.populate(rs);
+
+ assertTrue(noInitialCrset.isBeforeFirst());
+ try {
+ noInitialCrset.rowInserted();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ noInitialCrset.afterLast();
+ assertTrue(noInitialCrset.isAfterLast());
+ try {
+ noInitialCrset.rowInserted();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+
+ assertTrue(noInitialCrset.absolute(1));
+ assertFalse(noInitialCrset.rowInserted());
+
+ noInitialCrset.moveToInsertRow();
+ try {
+ noInitialCrset.rowInserted();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ noInitialCrset.updateInt(1, 10);
+ noInitialCrset.updateString(2, "insert10");
+ try {
+ noInitialCrset.rowInserted();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ noInitialCrset.insertRow();
+ try {
+ noInitialCrset.rowInserted();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ noInitialCrset.moveToCurrentRow();
+
+ noInitialCrset.beforeFirst();
+ boolean isInserted = false;
+ while (noInitialCrset.next()) {
+ if (noInitialCrset.getInt(1) == 10) {
+ isInserted = true;
+ assertTrue(noInitialCrset.rowInserted());
+ } else {
+ assertFalse(noInitialCrset.rowInserted());
+ }
+ }
+ assertTrue(isInserted);
+ }
+
+ public void testInsertRow_Single() throws Exception {
+ noInitialCrset = newNoInitialInstance();
+ try {
+ noInitialCrset.moveToInsertRow();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ try {
+ noInitialCrset.insertRow();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ // if the cursor is not on the insert row, no effect
+ noInitialCrset.moveToCurrentRow();
+
+ rs = st.executeQuery("SELECT * FROM USER_INFO");
+ noInitialCrset.populate(rs);
+ assertTrue(noInitialCrset.absolute(3));
+ noInitialCrset.moveToInsertRow();
+ try {
+ // call insertRow() without call any update method
+ noInitialCrset.insertRow();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ noInitialCrset.updateInt(1, 10);
+ assertEquals(10, noInitialCrset.getInt(1));
+ noInitialCrset.updateString(2, "insert10");
+ assertEquals("insert10", noInitialCrset.getString(2));
+ noInitialCrset.insertRow();
+ noInitialCrset.moveToCurrentRow();
+ assertEquals(3, noInitialCrset.getInt(1));
+ }
+
+ public void testInsertRow_Multi() throws Exception {
+ noInitialCrset = newNoInitialInstance();
+ // only when the cursor is on the insert row, it will have effect
+ noInitialCrset.moveToCurrentRow();
+ rs = st.executeQuery("SELECT * FROM USER_INFO");
+ noInitialCrset.populate(rs);
+ // move the cursor to the third row
+ assertTrue(noInitialCrset.absolute(3));
+
+ /*
+ * Insert one row
+ */
+ noInitialCrset.moveToInsertRow();
+ noInitialCrset.updateInt(1, 10);
+ assertEquals(10, noInitialCrset.getInt(1));
+ noInitialCrset.updateString(2, "update10");
+ assertEquals("update10", noInitialCrset.getString(2));
+ noInitialCrset.insertRow();
+ // the cursor is still on the insert row after call insertRow()
+ assertEquals(10, noInitialCrset.getInt(1));
+ assertEquals("update10", noInitialCrset.getString(2));
+
+ /*
+ * Insert another row. Here call moveToInsertRow() causes the original
+ * current row index lose.
+ */
+ noInitialCrset.moveToInsertRow();
+ noInitialCrset.updateInt(1, 11);
+ assertEquals(11, noInitialCrset.getInt(1));
+ noInitialCrset.updateString(2, "update11");
+ assertEquals("update11", noInitialCrset.getString(2));
+ noInitialCrset.insertRow();
+ assertEquals(11, noInitialCrset.getInt(1));
+ assertEquals("update11", noInitialCrset.getString(2));
+ noInitialCrset.moveToCurrentRow();
+
+ // commit to database
+ noInitialCrset.acceptChanges(conn);
+
+ // check db
+ rs = st.executeQuery("SELECT * FROM USER_INFO");
+ int index = 0;
+ while (rs.next()) {
+ index++;
+ }
+ assertEquals(6, index);
+
+ // check CahcedRowSet
+ noInitialCrset.beforeFirst();
+ index = 0;
+ while (noInitialCrset.next()) {
+ index++;
+ assertFalse(noInitialCrset.rowInserted());
+ }
+ assertEquals(6, index);
+
+ // move the cursor to the third row
+ assertTrue(noInitialCrset.absolute(3));
+ assertEquals(3, noInitialCrset.getInt(1));
+ // insert a row
+ noInitialCrset.moveToInsertRow();
+ noInitialCrset.updateInt(1, 12);
+ noInitialCrset.updateString(2, "update12");
+ noInitialCrset.insertRow();
+ assertEquals(12, noInitialCrset.getInt(1));
+ assertEquals("update12", noInitialCrset.getString(2));
+ noInitialCrset.moveToCurrentRow(); // notice here
+ // check current row
+ assertEquals(3, noInitialCrset.getInt(1));
+
+ // insert another row
+ noInitialCrset.moveToInsertRow();
+ noInitialCrset.updateInt(1, 13);
+ noInitialCrset.updateString(2, "update13");
+ noInitialCrset.insertRow();
+ assertEquals(13, noInitialCrset.getInt(1));
+ assertEquals("update13", noInitialCrset.getString(2));
+ noInitialCrset.moveToCurrentRow(); // notice here
+ // check current row
+ assertEquals(3, noInitialCrset.getInt(1));
+
+ // commit to database
+ noInitialCrset.acceptChanges(conn);
+ assertEquals(3, noInitialCrset.getInt(1));
+
+ // check db
+ rs = st.executeQuery("SELECT * FROM USER_INFO");
+ index = 0;
+ while (rs.next()) {
+ index++;
+ }
+ assertEquals(8, index);
+
+ // check CahcedRowSet
+ noInitialCrset.beforeFirst();
+ index = 0;
+ while (noInitialCrset.next()) {
+ index++;
+ assertFalse(noInitialCrset.rowInserted());
+ }
+ assertEquals(8, index);
+
+ // move the cursor to the third row
+ assertTrue(noInitialCrset.absolute(3));
+ assertEquals(3, noInitialCrset.getInt(1));
+ noInitialCrset.moveToInsertRow();
+ noInitialCrset.updateInt(1, 14);
+ assertEquals(14, noInitialCrset.getInt(1));
+ noInitialCrset.updateString(2, "update14");
+ noInitialCrset.insertRow();
+ assertEquals("update14", noInitialCrset.getString(2));
+ noInitialCrset.updateInt(1, 15);
+ assertEquals(15, noInitialCrset.getInt(1));
+ noInitialCrset.updateString(2, "update15");
+ noInitialCrset.insertRow();
+ assertEquals("update15", noInitialCrset.getString(2));
+ noInitialCrset.moveToCurrentRow();
+ assertEquals(3, noInitialCrset.getInt(1));
+ // commit to database
+ noInitialCrset.acceptChanges(conn);
+ assertEquals(3, noInitialCrset.getInt(1));
+
+ // check db
+ rs = st.executeQuery("SELECT * FROM USER_INFO");
+ index = 0;
+ while (rs.next()) {
+ index++;
+ }
+ assertEquals(10, index);
+
+ // check CahcedRowSet
+ noInitialCrset.beforeFirst();
+ index = 0;
+ while (noInitialCrset.next()) {
+ index++;
+ assertFalse(noInitialCrset.rowInserted());
+ }
+ assertEquals(10, index);
+ }
+
+ public void testUndoInsert() throws Exception {
+ noInitialCrset = newNoInitialInstance();
+ try {
+ noInitialCrset.undoInsert();
+ fail("should throw exception");
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // RI throw ArrayIndexOutOfBoundsException
+ } catch (SQLException e) {
+ // expected
+ }
+
+ rs = st.executeQuery("SELECT * FROM USER_INFO");
+ noInitialCrset.populate(rs);
+
+ // the cursor is before the first row
+ assertTrue(noInitialCrset.isBeforeFirst());
+ try {
+ noInitialCrset.undoInsert();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+
+ // the cursor is after the last row
+ noInitialCrset.afterLast();
+ assertTrue(noInitialCrset.isAfterLast());
+ try {
+ noInitialCrset.undoInsert();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+
+ // the cursor is on the insert row
+ assertTrue(noInitialCrset.absolute(3));
+ try {
+ noInitialCrset.undoInsert();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ assertEquals(3, noInitialCrset.getInt(1));
+ noInitialCrset.moveToInsertRow();
+ try {
+ noInitialCrset.undoInsert();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ noInitialCrset.updateInt(1, 10);
+ noInitialCrset.updateString(2, "update10");
+ try {
+ noInitialCrset.undoInsert();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ noInitialCrset.insertRow();
+ try {
+ noInitialCrset.undoInsert();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // expected
+ }
+ noInitialCrset.moveToCurrentRow();
+ noInitialCrset.acceptChanges(conn);
+ assertEquals(3, noInitialCrset.getInt(1));
+
+ // check db
+ rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 10");
+ assertTrue(rs.next());
+ assertEquals(1, rs.getInt(1));
+
+ // check CachedRowSet
+ boolean isInserted = false;
+ noInitialCrset.beforeFirst();
+ while (noInitialCrset.next()) {
+ if (noInitialCrset.getInt(1) == 10) {
+ isInserted = true;
+ }
+ }
+ assertTrue(isInserted);
+
+ /*
+ * call undoInsert() successfully
+ */
+ assertTrue(noInitialCrset.absolute(3));
+ assertEquals(3, noInitialCrset.getInt(1));
+ noInitialCrset.moveToInsertRow();
+ noInitialCrset.updateInt(1, 20);
+ noInitialCrset.updateString(2, "update20");
+ noInitialCrset.insertRow();
+ noInitialCrset.moveToCurrentRow();
+ // move to the inserted row
+ noInitialCrset.beforeFirst();
+ while (noInitialCrset.next()) {
+ if (noInitialCrset.rowInserted()) {
+ break;
+ }
+ }
+ assertEquals(20, noInitialCrset.getInt(1));
+ assertTrue(noInitialCrset.rowInserted());
+ /*
+ * When run on RI, undoInsert() has no effect.
+ */
+ noInitialCrset.undoInsert();
+
+ // commit to database
+ noInitialCrset.acceptChanges(conn);
+
+ // check db
+ rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 20");
+ assertTrue(rs.next());
+ if ("true".equals(System.getProperty("Testing Harmony"))) {
+ assertEquals(0, rs.getInt(1));
+ } else {
+ // undoInsert() has no effect when run RI.
+ assertEquals(1, rs.getInt(1));
+ }
+ }
+}
Propchange: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetInsertTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java?rev=634570&r1=634569&r2=634570&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetUpdateTest.java Thu Mar 6 23:25:23 2008
@@ -332,14 +332,7 @@
noInitialCrset.updateInt(1, 10);
noInitialCrset.updateString(2, "update10");
noInitialCrset.insertRow();
- try {
- noInitialCrset.undoUpdate();
- fail("should throw SQLException");
- } catch (SQLException e) {
- // RI throw SQLException here.
- }
noInitialCrset.moveToCurrentRow();
-
noInitialCrset.absolute(4);
assertEquals(10, noInitialCrset.getInt(1));
noInitialCrset.updateString(2, "abc");
@@ -362,6 +355,18 @@
// RI throw SQLException here.
}
}
+
+ noInitialCrset.moveToInsertRow();
+ noInitialCrset.updateInt(1, 10);
+ noInitialCrset.updateString(2, "update10");
+ noInitialCrset.insertRow();
+ try {
+ noInitialCrset.undoUpdate();
+ fail("should throw SQLException");
+ } catch (SQLException e) {
+ // RI throw SQLException here.
+ }
+ noInitialCrset.moveToCurrentRow();
}
public void testUpdateValue() throws Exception {