0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.harmony.sql.tests.internal.rowset;
0019:
0020: import java.math.BigDecimal;
0021: import java.sql.Connection;
0022: import java.sql.Date;
0023: import java.sql.DriverManager;
0024: import java.sql.PreparedStatement;
0025: import java.sql.ResultSet;
0026: import java.sql.SQLException;
0027: import java.sql.Time;
0028: import java.sql.Timestamp;
0029: import java.util.Arrays;
0030: import java.util.HashMap;
0031:
0032: import javax.sql.RowSetEvent;
0033: import javax.sql.RowSetListener;
0034: import javax.sql.RowSetMetaData;
0035: import javax.sql.rowset.CachedRowSet;
0036: import javax.sql.rowset.spi.SyncProviderException;
0037:
0038: public class CachedRowSetImplTest extends CachedRowSetTestCase {
0039:
0040: @Override
0041: public void setUp() throws Exception {
0042: super .setUp();
0043: }
0044:
0045: @Override
0046: public void tearDown() throws Exception {
0047: super .tearDown();
0048: }
0049:
0050: public void testGetOriginalRow() throws Exception {
0051: try {
0052: crset.getOriginalRow();
0053: fail("should throw SQLException");
0054: } catch (SQLException e) {
0055: // expected: spec throw SQLException
0056: } catch (ArrayIndexOutOfBoundsException e) {
0057: // RI throw ArrayIndexOutOfBoundsException
0058: }
0059:
0060: assertTrue(crset.absolute(3));
0061: assertNotSame(crset.getOriginalRow(), crset.getOriginalRow());
0062:
0063: crset.updateString(2, "update3");
0064: ResultSet originalRow = crset.getOriginalRow();
0065: assertTrue(originalRow.next());
0066: assertEquals("test3", originalRow.getString(2));
0067:
0068: // after call acceptChanges()
0069: crset.updateRow();
0070: crset.acceptChanges();
0071: assertTrue(crset.absolute(3));
0072: assertEquals("update3", crset.getString(2));
0073: originalRow = crset.getOriginalRow();
0074: assertTrue(originalRow.next());
0075: // TODO uncomment it after implement Writer
0076: // assertEquals("update3", originalRow.getString(2));
0077: }
0078:
0079: public void testSetSyncProvider() throws Exception {
0080: if (System.getProperty("Testing Harmony") == "true") {
0081: String mySyncProvider = "org.apache.harmony.sql.internal.rowset.HYOptimisticProvider";
0082: crset.setSyncProvider(mySyncProvider);
0083: assertEquals(crset.getSyncProvider().getClass()
0084: .getCanonicalName(), mySyncProvider);
0085: }
0086: }
0087:
0088: public void testColumnUpdatedInt() throws SQLException {
0089: crset.first();
0090: // try {
0091: // assertFalse(crset.columnUpdated(1));
0092: // fail("should throw SQLException");
0093: // } catch (SQLException e) {
0094: // // expected;
0095: // }
0096: crset.next();
0097: try {
0098: crset.columnUpdated(-1);
0099: fail("should throw IndexOutOfBoundsException");
0100: } catch (IndexOutOfBoundsException e) {
0101: // expected;
0102: }
0103: try {
0104: crset.columnUpdated(0);
0105: fail("should throw IndexOutOfBoundsException");
0106: } catch (IndexOutOfBoundsException e) {
0107: // expected;
0108: }
0109: assertFalse(crset.columnUpdated(1));
0110: }
0111:
0112: public void testColumnUpdatedString() throws SQLException {
0113: crset.first();
0114: // try {
0115: // assertFalse(crset.columnUpdated("ID"));
0116: // fail("should throw SQLException");
0117: // } catch (SQLException e) {
0118: // // expected;
0119: // }
0120: crset.next();
0121: try {
0122: assertFalse(crset.columnUpdated("Incorrect"));
0123: fail("should throw SQLException");
0124: } catch (SQLException e) {
0125: // expected;
0126: }
0127: assertFalse(crset.columnUpdated("NAME"));
0128: }
0129:
0130: public void testGetPageSize() throws SQLException {
0131: assertEquals(0, crset.getPageSize());
0132: crset.setPageSize(1);
0133: assertEquals(1, crset.getPageSize());
0134: }
0135:
0136: public void testSetPageSize() throws SQLException {
0137: try {
0138: crset.setPageSize(-1);
0139: fail("should throw SQLException");
0140: } catch (SQLException e) {
0141: // expected;
0142: }
0143: crset.setPageSize(0);
0144: crset.setPageSize(Integer.MAX_VALUE);
0145: assertEquals(Integer.MAX_VALUE, crset.getPageSize());
0146: }
0147:
0148: public void testGetTableName() throws SQLException {
0149:
0150: crset.setTableName("USER");
0151: assertEquals("USER", crset.getTableName());
0152: }
0153:
0154: public void testSetTableName() throws SQLException {
0155: try {
0156: crset.setTableName(null);
0157: fail("should throw SQLException");
0158: } catch (SQLException e) {
0159: // expected;
0160: }
0161: }
0162:
0163: public void testSize() throws Exception {
0164: assertEquals(DEFAULT_ROW_COUNT, crset.size());
0165: // before populate should return 0
0166: assertEquals(0, noInitialCrset.size());
0167: }
0168:
0169: public void testDeleteRow() throws SQLException {
0170: crset.first();
0171: // try {
0172: // crset.deleteRow();
0173: // fail("should throw SQLException");
0174: // } catch (SQLException e) {
0175: // // expected;
0176: // }
0177: crset.next();
0178: assertFalse(crset.rowDeleted());
0179: crset.deleteRow();
0180: assertEquals(DEFAULT_ROW_COUNT, crset.size());
0181: assertTrue(crset.rowDeleted());
0182: }
0183:
0184: public void testRowDeleted() throws SQLException {
0185: // try {
0186: // crset.rowDeleted();
0187: // fail("should throw SQLException");
0188: // } catch (SQLException e) {
0189: // // expected;
0190: // }
0191: }
0192:
0193: public void testInsertRow() throws SQLException {
0194: crset.first();
0195: try {
0196: crset.insertRow();
0197: fail("should throw SQLException");
0198: } catch (SQLException e) {
0199: // expected;
0200: }
0201: crset.next();
0202: try {
0203: crset.insertRow();
0204: fail("should throw SQLException");
0205: } catch (SQLException e) {
0206: // expected;
0207: }
0208: crset.moveToInsertRow();
0209: crset.updateString("Name", "TonyWu");
0210: crset.updateInt("ID", 3);
0211: crset.insertRow();
0212: assertEquals("TonyWu", crset.getString(2));
0213: assertEquals("TonyWu", crset.getString("Name"));
0214: assertEquals(3, crset.getInt(1));
0215: assertEquals(3, crset.getInt("ID"));
0216:
0217: crset.moveToCurrentRow();
0218: assertFalse(crset.rowInserted());
0219:
0220: }
0221:
0222: public void testAcceptChanges() throws SQLException {
0223: crset.setTableName("USER_INFO");
0224: // FIXME: if the value of column is null, it would go wrong when
0225: // call acceptChanges(). And if one method in TestCase throws
0226: // SQLException, the following method will be affected.
0227: rs.next();
0228: assertEquals(1, rs.getInt(1));
0229: assertEquals("hermit", rs.getString(2));
0230:
0231: crset.absolute(3);
0232: assertEquals(3, crset.getInt(1));
0233: assertEquals("test3", crset.getString(2));
0234: crset.updateString(2, "HarmonY");
0235:
0236: crset.moveToInsertRow();
0237: crset.updateInt(1, 16);
0238: crset.updateString(2, "Apache");
0239: crset.insertRow();
0240: crset.moveToCurrentRow();
0241:
0242: crset.deleteRow();
0243: /*
0244: * RI will print the change result
0245: */
0246: crset.acceptChanges();
0247:
0248: rs = st
0249: .executeQuery("select * from USER_INFO where NAME = 'hermit'");
0250: rs.next();
0251: assertEquals("hermit", rs.getString(2));
0252: rs = st
0253: .executeQuery("select * from USER_INFO where NAME = 'test4'");
0254: rs.next();
0255: assertEquals("test4", rs.getString(2));
0256:
0257: }
0258:
0259: public void testExecute() throws SQLException {
0260: crset.setCommand("Update User_INFO set Name= ? Where ID= ? ");
0261: crset.setString(1, "executed!");
0262: crset.setInt(2, 1);
0263: crset.execute();
0264:
0265: crset.setCommand("SELECT ID, NAME FROM USER_INFO"
0266: + " WHERE ID = ? ");
0267: crset.setInt(1, 1);
0268: crset.execute();
0269:
0270: crset.first();
0271: assertEquals("executed!", crset.getString(2));
0272:
0273: crset.setCommand("Update User_INFO set Name= ? Where ID= ? ");
0274: crset.setString(1, "executed!");
0275: crset.setInt(2, 1);
0276: crset.execute(DriverManager.getConnection(DERBY_URL));
0277:
0278: crset.setCommand("SELECT ID, NAME FROM USER_INFO"
0279: + " WHERE ID = ? ");
0280: crset.setInt(1, 1);
0281: crset.execute(DriverManager.getConnection(DERBY_URL));
0282: }
0283:
0284: public void testExecute2() throws Exception {
0285: crset.setCommand("SELECT ID, NAME FROM USER_INFO"
0286: + " WHERE ID = ? ");
0287: crset.setInt(1, 1);
0288: crset.execute();
0289:
0290: crset.first();
0291: assertEquals("hermit", crset.getString(2));
0292: }
0293:
0294: public void testExecute3() throws Exception {
0295: // insert 15 more rows for test
0296: insertMoreData(15);
0297:
0298: rs = st.executeQuery("select * from USER_INFO");
0299: noInitialCrset.setUrl(DERBY_URL);
0300: noInitialCrset.setPageSize(5);
0301: noInitialCrset.setCommand("select * from USER_INFO");
0302: noInitialCrset.execute();
0303: rs = st.executeQuery("select * from USER_INFO");
0304: int cursorIndex = 0;
0305: while (noInitialCrset.next() && rs.next()) {
0306: cursorIndex++;
0307: for (int i = 1; i <= DEFAULT_COLUMN_COUNT; i++) {
0308: assertEquals(rs.getObject(i), noInitialCrset
0309: .getObject(i));
0310: }
0311: }
0312: // The pageSize works here. CachedRowSet only get 5 rows from ResultSet.
0313: assertEquals(5, cursorIndex);
0314:
0315: // change a command
0316: noInitialCrset = newNoInitialInstance();
0317: noInitialCrset.setUrl(null);
0318: // The pageSize still work here
0319: noInitialCrset.setPageSize(5);
0320: assertEquals(5, noInitialCrset.getPageSize());
0321: noInitialCrset
0322: .setCommand("select * from USER_INFO where NAME like ?");
0323: noInitialCrset.setString(1, "test%");
0324: Connection aConn = DriverManager.getConnection(DERBY_URL);
0325: noInitialCrset.execute(aConn);
0326: aConn.close();
0327: cursorIndex = 1;
0328: while (noInitialCrset.next()) {
0329: cursorIndex++;
0330: assertEquals(cursorIndex, noInitialCrset.getInt(1));
0331: }
0332: assertEquals(6, cursorIndex);
0333:
0334: noInitialCrset = newNoInitialInstance();
0335: rs = st.executeQuery("select * from USER_INFO");
0336:
0337: noInitialCrset.setUrl(DERBY_URL);
0338: noInitialCrset.setPageSize(5);
0339: noInitialCrset.setMaxRows(2);
0340: noInitialCrset.setCommand("select * from USER_INFO");
0341:
0342: noInitialCrset.execute();
0343:
0344: rs = st.executeQuery("select * from USER_INFO");
0345: cursorIndex = 0;
0346: while (noInitialCrset.next() && rs.next()) {
0347: cursorIndex++;
0348: }
0349: // maxRows works here
0350: assertEquals(2, cursorIndex);
0351: }
0352:
0353: public void testCreateShared() throws Exception {
0354: crset.setUsername("testUsername");
0355: crset.setPassword("testPassword");
0356: crset.setPageSize(5);
0357: Listener listener = new Listener(); // a class implements RowSetListener
0358: crset.addRowSetListener(listener);
0359: crset.absolute(3); // move to the third row for testing
0360: // TODO: when the cursor moved, notifyCursorMoved() should be called
0361: // assertEquals("cursorMoved", listener.getTag());
0362:
0363: CachedRowSet crsetShared = (CachedRowSet) crset.createShared();
0364: assertEquals("testUsername", crsetShared.getUsername());
0365: assertEquals("testPassword", crsetShared.getPassword());
0366: assertEquals(5, crsetShared.getPageSize());
0367: // check whether modify the attribute of the original is visible to the
0368: // duplicate
0369: crset.setUsername("modifyUsername");
0370: crset.setPageSize(10);
0371: assertEquals("modifyUsername", crset.getUsername());
0372: assertEquals("testUsername", crsetShared.getUsername());
0373: assertEquals(10, crset.getPageSize());
0374: assertEquals(5, crsetShared.getPageSize());
0375:
0376: // compare the current row, that is the third row
0377: assertEquals(3, crset.getInt(1));
0378: assertEquals("test3", crset.getString(2));
0379: assertEquals(3, crsetShared.getInt(1));
0380: assertEquals("test3", crsetShared.getString(2));
0381: // check whether update the duplicate is visible to the original
0382: crsetShared.updateString(2, "modify3");
0383: assertEquals("modify3", crsetShared.getString(2));
0384: assertEquals("modify3", crset.getString(2));
0385: crsetShared.updateRow();
0386: crsetShared.acceptChanges();
0387: assertEquals("rowSetChanged", listener.getTag());
0388:
0389: // when move the duplicate's cursor, the original shouldn't be affected
0390: crsetShared.absolute(1);
0391: assertEquals(1, crsetShared.getInt(1));
0392: assertEquals(3, crset.getInt(1));
0393: }
0394:
0395: public void testcreateCopyNoConstraints() throws Exception {
0396:
0397: crset.setConcurrency(ResultSet.CONCUR_READ_ONLY);
0398: crset.setType(ResultSet.TYPE_SCROLL_SENSITIVE);
0399: crset.setEscapeProcessing(false);
0400: crset.setMaxRows(10);
0401: crset.setTransactionIsolation(Connection.TRANSACTION_NONE);
0402: crset.setQueryTimeout(10);
0403: crset.setPageSize(10);
0404: crset.setShowDeleted(true);
0405: crset.setUsername("username");
0406: crset.setPassword("password");
0407: crset.setTypeMap(new HashMap<String, Class<?>>());
0408: crset.setMaxFieldSize(10);
0409: crset.setFetchDirection(ResultSet.FETCH_UNKNOWN);
0410:
0411: CachedRowSet copy = crset.createCopyNoConstraints();
0412:
0413: // default is ResultSet.CONCUR_UPDATABLE
0414: assertEquals(ResultSet.CONCUR_UPDATABLE, copy.getConcurrency());
0415: // default is ResultSet.TYPE_SCROLL_INSENSITIVE
0416: assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, copy.getType());
0417: // default is true
0418: assertTrue(copy.getEscapeProcessing());
0419: // default is 0
0420: assertEquals(0, copy.getMaxRows());
0421: // default is Connection.TRANSACTION_READ_COMMITTED
0422: assertEquals(Connection.TRANSACTION_READ_COMMITTED, copy
0423: .getTransactionIsolation());
0424: // default is 0
0425: assertEquals(0, copy.getQueryTimeout());
0426: // default is false
0427: assertFalse(copy.getShowDeleted());
0428: // default is 0
0429: assertEquals(0, copy.getMaxFieldSize());
0430: // default is null
0431: assertNull(copy.getPassword());
0432: // default is null
0433: assertNull(copy.getUsername());
0434: // default is null
0435: assertNull(copy.getTypeMap());
0436:
0437: if (crset.getKeyColumns() == null) {
0438: assertNull(copy.getKeyColumns());
0439: } else {
0440: int[] keyColumns = crset.getKeyColumns();
0441: int[] copyKeyColumns = copy.getKeyColumns();
0442:
0443: assertEquals(keyColumns.length, copyKeyColumns.length);
0444: for (int i = 0; i < keyColumns.length; i++) {
0445: assertEquals(keyColumns[i], copyKeyColumns[i]);
0446: }
0447: assertEquals(crset.getKeyColumns(), copy.getKeyColumns());
0448: }
0449:
0450: assertEquals(crset.getFetchDirection(), copy
0451: .getFetchDirection());
0452: assertEquals(crset.getPageSize(), copy.getPageSize());
0453:
0454: // TODO uncomment them after implemented
0455: // assertEquals(crset.isBeforeFirst(), crsetCopy.isBeforeFirst());
0456: // assertEquals(crset.isAfterLast(), crsetCopy.isAfterLast());
0457: // assertEquals(crset.isFirst(), crsetCopy.isFirst());
0458: // assertEquals(crset.isLast(), crsetCopy.isLast());
0459: // assertEquals(crset.getRow(), copy.getRow());
0460: // assertNotSame(crset.getWarnings(), copy.getWarnings());
0461: // assertEquals(crset.getStatement(), copy.getStatement());
0462: // try {
0463: // assertEquals(crset.getCursorName(), copy.getCursorName());
0464: // fail("Should throw SQLException");
0465: // } catch (SQLException e) {
0466: // // expected
0467: // }
0468: //
0469: // try {
0470: // assertEquals(crset.getMatchColumnIndexes(), copy
0471: // .getMatchColumnIndexes());
0472: // fail("Should throw SQLException");
0473: // } catch (SQLException e) {
0474: // // expected
0475: // }
0476: //
0477: // try {
0478: // assertEquals(crset.getMatchColumnNames(), copy
0479: // .getMatchColumnNames());
0480: // } catch (SQLException e) {
0481: // // expected
0482: // }
0483:
0484: assertEquals(crset.isReadOnly(), copy.isReadOnly());
0485: assertEquals(crset.size(), copy.size());
0486:
0487: // different metaData object
0488: assertNotSame(crset.getMetaData(), copy.getMetaData());
0489:
0490: isMetaDataEquals(crset.getMetaData(), copy.getMetaData());
0491:
0492: assertEquals(crset.getCommand(), copy.getCommand());
0493:
0494: // check SyncProvider
0495: assertEquals(crset.getSyncProvider().getProviderID(), copy
0496: .getSyncProvider().getProviderID());
0497: assertEquals(crset.getSyncProvider().getProviderGrade(), copy
0498: .getSyncProvider().getProviderGrade());
0499: assertEquals(crset.getSyncProvider().getDataSourceLock(), copy
0500: .getSyncProvider().getDataSourceLock());
0501: assertEquals(crset.getSyncProvider().getVendor(), copy
0502: .getSyncProvider().getVendor());
0503: assertEquals(crset.getSyncProvider().getVersion(), copy
0504: .getSyncProvider().getVersion());
0505:
0506: assertEquals(crset.getTableName(), copy.getTableName());
0507: assertEquals(crset.getUrl(), copy.getUrl());
0508:
0509: }
0510:
0511: public void testcreateCopyNoConstraints2() throws Exception {
0512:
0513: // the default value
0514: assertNull(crset.getCommand());
0515: assertEquals(ResultSet.CONCUR_UPDATABLE, crset.getConcurrency());
0516: assertNull(crset.getDataSourceName());
0517: assertEquals(ResultSet.FETCH_FORWARD, crset.getFetchDirection());
0518: assertEquals(0, crset.getFetchSize());
0519: assertEquals(0, crset.getMaxFieldSize());
0520: assertEquals(0, crset.getMaxRows());
0521: assertEquals(0, crset.getPageSize());
0522: assertNull(crset.getPassword());
0523: assertEquals(0, crset.getQueryTimeout());
0524: assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, crset
0525: .getTransactionIsolation());
0526: assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, crset.getType());
0527: assertNull(crset.getTypeMap());
0528: assertEquals(DERBY_URL, crset.getUrl());
0529: assertNull(crset.getUsername());
0530: assertTrue(crset.getEscapeProcessing());
0531: assertNull(crset.getKeyColumns());
0532:
0533: // set value
0534: crset.setCommand("testCommand");
0535: crset.setConcurrency(ResultSet.CONCUR_READ_ONLY);
0536: crset.setDataSourceName("testDataSourceName");
0537: crset.setFetchDirection(ResultSet.FETCH_REVERSE);
0538: crset.setMaxFieldSize(100);
0539: crset.setMaxRows(10);
0540: crset.setPageSize(10);
0541: crset.setPassword("passwo");
0542: crset.setQueryTimeout(100);
0543: crset.setTableName("testTable");
0544: crset
0545: .setTransactionIsolation(ResultSet.HOLD_CURSORS_OVER_COMMIT);
0546: crset.setType(ResultSet.TYPE_SCROLL_SENSITIVE);
0547: crset.setTypeMap(new HashMap<String, Class<?>>());
0548: crset.setUsername("testUserName");
0549: crset.setEscapeProcessing(false);
0550: crset.setKeyColumns(new int[] { 1 });
0551:
0552: // check the changed value
0553: assertEquals("testCommand", crset.getCommand());
0554: assertEquals(ResultSet.CONCUR_READ_ONLY, crset.getConcurrency());
0555: assertEquals("testDataSourceName", crset.getDataSourceName());
0556: assertEquals(ResultSet.FETCH_REVERSE, crset.getFetchDirection());
0557: assertEquals(0, crset.getFetchSize());
0558: assertEquals(100, crset.getMaxFieldSize());
0559: assertEquals(10, crset.getMaxRows());
0560: assertEquals(10, crset.getPageSize());
0561: assertEquals("passwo", crset.getPassword());
0562: assertEquals(100, crset.getQueryTimeout());
0563: assertEquals("testTable", crset.getTableName());
0564: assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, crset
0565: .getTransactionIsolation());
0566: assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, crset.getType());
0567: assertNotNull(crset.getTypeMap());
0568: assertNull(crset.getUrl());
0569: assertEquals("testUserName", crset.getUsername());
0570: assertFalse(crset.getEscapeProcessing());
0571: assertTrue(Arrays
0572: .equals(new int[] { 1 }, crset.getKeyColumns()));
0573:
0574: // after call createCopyNoConstraints
0575: CachedRowSet copy = crset.createCopyNoConstraints();
0576: assertEquals("testCommand", copy.getCommand());
0577: assertEquals(ResultSet.CONCUR_UPDATABLE, copy.getConcurrency());
0578: assertEquals("testDataSourceName", copy.getDataSourceName());
0579: assertEquals(ResultSet.FETCH_REVERSE, copy.getFetchDirection());
0580: assertEquals(0, copy.getFetchSize());
0581: assertEquals(0, copy.getMaxFieldSize());
0582: assertEquals(0, copy.getMaxRows());
0583: assertEquals(10, copy.getPageSize());
0584: assertNull(copy.getPassword());
0585: assertEquals(0, copy.getQueryTimeout());
0586: assertEquals("testTable", copy.getTableName());
0587: assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, copy
0588: .getTransactionIsolation());
0589: assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, copy.getType());
0590: assertNull(copy.getTypeMap());
0591: assertNull(copy.getUrl());
0592: assertNull(copy.getUsername());
0593: assertTrue(copy.getEscapeProcessing());
0594: assertTrue(Arrays.equals(new int[] { 1 }, copy.getKeyColumns()));
0595: }
0596:
0597: public void testCopySchema() throws Exception {
0598: // the original's addtribute and meta data
0599: crset.setCommand("testCommand");
0600: crset.setConcurrency(ResultSet.CONCUR_UPDATABLE);
0601: crset.setDataSourceName("testDataSource");
0602: crset.setFetchDirection(ResultSet.FETCH_UNKNOWN);
0603: crset.setPageSize(20);
0604: crset.setMaxRows(20);
0605: crset.setTableName("USER_INFO");
0606: /*
0607: * NOTICE: spec say copy must not has any content, but when run on RI,
0608: * if call next() before call createCopySchema(), the copy can get the
0609: * current row's data
0610: */
0611:
0612: /*
0613: * NOTICE: when run on RI, if add the listener first, then it will go
0614: * wrong when call createCopySchema().It's said that clone failed.
0615: */
0616: // Listener listener = new Listener();
0617: // crset.addRowSetListener(listener);
0618: RowSetMetaData rsmd = (RowSetMetaData) crset.getMetaData();
0619: // the copy
0620: CachedRowSet crsetCopySchema = crset.createCopySchema();
0621: RowSetMetaData rsmdCopySchema = (RowSetMetaData) crsetCopySchema
0622: .getMetaData();
0623:
0624: // compare the meta data between the duplicate and the original
0625: assertNotSame(crset.getMetaData(), crsetCopySchema
0626: .getMetaData());
0627: assertNotSame(crset.getOriginal(), crsetCopySchema
0628: .getOriginal());
0629: // assertNotSame(crset.getSyncProvider(), crsetCopySchema
0630: // .getSyncProvider());
0631:
0632: assertEquals("USER_INFO", crset.getTableName());
0633: assertEquals("USER_INFO", rsmdCopySchema.getTableName(1));
0634: assertEquals(DEFAULT_COLUMN_COUNT, rsmdCopySchema
0635: .getColumnCount());
0636: assertEquals(rsmd.getColumnName(1), rsmdCopySchema
0637: .getColumnName(1));
0638: // check the primary key
0639: // TODO: RI doesn't evalute the keyColumns. The value of
0640: // crset.getKeyColumns() is null.
0641: assertEquals(crset.getKeyColumns(), crsetCopySchema
0642: .getKeyColumns());
0643:
0644: // check the attributes in the duplicate. These are supposed to be the
0645: // same as the original
0646: // System.out.println("crsetCopySchema: " + crsetCopySchema.getInt(1));
0647: assertFalse(crsetCopySchema.next());
0648: assertEquals("testCommand", crsetCopySchema.getCommand());
0649: assertEquals(ResultSet.CONCUR_UPDATABLE, crsetCopySchema
0650: .getConcurrency());
0651: assertEquals("testDataSource", crsetCopySchema
0652: .getDataSourceName());
0653: assertEquals(ResultSet.FETCH_UNKNOWN, crsetCopySchema
0654: .getFetchDirection());
0655: assertEquals(20, crsetCopySchema.getPageSize());
0656: assertEquals(20, crsetCopySchema.getMaxRows());
0657:
0658: // fill the duplicate CachedRowSet with data, check the listener
0659: Listener listener = new Listener();
0660: crsetCopySchema.addRowSetListener(listener);
0661: assertNull(listener.getTag());
0662: rs = st.executeQuery("select * from USER_INFO");
0663: crsetCopySchema.populate(rs);
0664: // TODO: in the Harmony implementation, need to call notifyRowSetChanged
0665: // at the suitable place
0666: // assertEquals("rowSetChanged", listener.getTag());
0667: listener.setTag(null);
0668: // the move of the original's cursor shouldn't affect the duplicate
0669: crset.next();
0670: assertNull(listener.getTag());
0671: }
0672:
0673: public void testCopySchema2() throws Exception {
0674:
0675: // set value
0676: crset.setCommand("testCommand");
0677: crset.setConcurrency(ResultSet.CONCUR_READ_ONLY);
0678: crset.setDataSourceName("testDataSourceName");
0679: crset.setFetchDirection(ResultSet.FETCH_REVERSE);
0680: crset.setMaxFieldSize(100);
0681: crset.setMaxRows(10);
0682: crset.setPageSize(10);
0683: crset.setPassword("passwo");
0684: crset.setQueryTimeout(100);
0685: crset.setTableName("testTable");
0686: crset
0687: .setTransactionIsolation(ResultSet.HOLD_CURSORS_OVER_COMMIT);
0688: crset.setType(ResultSet.TYPE_SCROLL_SENSITIVE);
0689: crset.setTypeMap(new HashMap<String, Class<?>>());
0690: crset.setEscapeProcessing(false);
0691: crset.setKeyColumns(new int[] { 1 });
0692:
0693: // call createCopySchema()
0694: CachedRowSet copy = crset.createCopySchema();
0695: assertFalse(copy.next());
0696: assertEquals(crset.getCommand(), copy.getCommand());
0697: assertEquals(crset.getConcurrency(), copy.getConcurrency());
0698: assertEquals(crset.getDataSourceName(), copy
0699: .getDataSourceName());
0700: assertEquals(crset.getFetchDirection(), copy
0701: .getFetchDirection());
0702: assertEquals(crset.getMaxFieldSize(), copy.getMaxFieldSize());
0703: assertEquals(crset.getMaxRows(), copy.getMaxRows());
0704: assertEquals(crset.getPageSize(), copy.getPageSize());
0705: assertEquals(crset.getQueryTimeout(), copy.getQueryTimeout());
0706: assertEquals(crset.getTableName(), copy.getTableName());
0707: assertEquals(crset.getTransactionIsolation(), copy
0708: .getTransactionIsolation());
0709: assertEquals(crset.getType(), copy.getType());
0710: assertEquals(crset.getUrl(), copy.getUrl());
0711: assertEquals(crset.getEscapeProcessing(), copy
0712: .getEscapeProcessing());
0713: assertTrue(Arrays.equals(crset.getKeyColumns(), copy
0714: .getKeyColumns()));
0715:
0716: // compare the object reference
0717: assertNotSame(crset.getKeyColumns(), copy.getKeyColumns());
0718: assertNotSame(crset.getMetaData(), copy.getMetaData());
0719: assertNotSame(crset.getOriginal(), copy.getOriginal());
0720: assertNotSame(crset.getTypeMap(), copy.getTypeMap());
0721: }
0722:
0723: public void testCreateCopy() throws Exception {
0724:
0725: // TODO: lack of the test for CachedRowSet.getOriginal() and
0726: // CachedRowSet.getOriginalRow()
0727:
0728: crset.absolute(3);
0729:
0730: CachedRowSet crsetCopy = crset.createCopy();
0731:
0732: crsetCopy.updateString(2, "copyTest3");
0733: crsetCopy.updateRow();
0734: crsetCopy.acceptChanges();
0735:
0736: assertEquals("copyTest3", crsetCopy.getString(2));
0737:
0738: assertEquals("test3", crset.getString(2));
0739:
0740: rs = st.executeQuery("select * from USER_INFO");
0741: rs.next();
0742: rs.next();
0743: rs.next();
0744: // TODO: Uncomment it when Writer is implemented fully.
0745: // assertEquals("copyTest3", rs.getString(2));
0746:
0747: reloadCachedRowSet();
0748: crset.absolute(2);
0749:
0750: crsetCopy = crset.createCopy();
0751:
0752: assertEquals(crset.isReadOnly(), crsetCopy.isReadOnly());
0753: // TODO uncomment when isBeforeFirst is implemented
0754: // assertEquals(crset.isBeforeFirst(), crsetCopy.isBeforeFirst());
0755: // TODO uncomment when isAfterLast is implemented
0756: // assertEquals(crset.isAfterLast(), crsetCopy.isAfterLast());
0757: // TODO uncomment when isFirst is implemented
0758: // assertEquals(crset.isFirst(), crsetCopy.isFirst());
0759: // TODO uncomment when isLast is implemented
0760: // assertEquals(crset.isLast(), crsetCopy.isLast());
0761:
0762: assertEquals(crset.size(), crsetCopy.size());
0763: // different metaData object
0764: assertNotSame(crset.getMetaData(), crsetCopy.getMetaData());
0765:
0766: isMetaDataEquals(crset.getMetaData(), crsetCopy.getMetaData());
0767:
0768: assertEquals(crset.getCommand(), crsetCopy.getCommand());
0769: assertEquals(crset.getConcurrency(), crsetCopy.getConcurrency());
0770:
0771: // uncomment after implemented
0772: // try {
0773: // assertEquals(crset.getCursorName(), crsetCopy.getCursorName());
0774: // fail("Should throw SQLException");
0775: // } catch (SQLException e) {
0776: // // expected
0777: // }
0778: // try {
0779: // assertEquals(crset.getMatchColumnIndexes(), crsetCopy
0780: // .getMatchColumnIndexes());
0781: // fail("Should throw SQLException");
0782: // } catch (SQLException e) {
0783: // // expected
0784: // }
0785: //
0786: // try {
0787: // assertEquals(crset.getMatchColumnNames(), crsetCopy
0788: // .getMatchColumnNames());
0789: // } catch (SQLException e) {
0790: // // expected
0791: // }
0792: // assertEquals(crset.getRow(), crsetCopy.getRow());
0793: // assertEquals(crset.getStatement(), crsetCopy.getStatement());
0794: // assertNotSame(crset.getWarnings(), crsetCopy.getWarnings());
0795:
0796: assertEquals(crset.getEscapeProcessing(), crsetCopy
0797: .getEscapeProcessing());
0798: assertEquals(crset.getFetchDirection(), crsetCopy
0799: .getFetchDirection());
0800: assertEquals(crset.getFetchSize(), crsetCopy.getFetchSize());
0801: if (crset.getKeyColumns() == null) {
0802: assertNull(crsetCopy.getKeyColumns());
0803: } else {
0804: int[] keyColumns = crset.getKeyColumns();
0805: int[] copyKeyColumns = crsetCopy.getKeyColumns();
0806:
0807: assertEquals(keyColumns.length, copyKeyColumns.length);
0808: for (int i = 0; i < keyColumns.length; i++) {
0809: assertEquals(keyColumns[i], copyKeyColumns[i]);
0810: }
0811: assertEquals(crset.getKeyColumns(), crsetCopy
0812: .getKeyColumns());
0813: }
0814:
0815: assertEquals(crset.getMaxFieldSize(), crsetCopy
0816: .getMaxFieldSize());
0817: assertEquals(crset.getMaxRows(), crsetCopy.getMaxRows());
0818:
0819: assertEquals(crset.getPageSize(), crsetCopy.getPageSize());
0820: assertEquals(crset.getPassword(), crsetCopy.getPassword());
0821: assertEquals(crset.getQueryTimeout(), crsetCopy
0822: .getQueryTimeout());
0823: assertEquals(crset.getShowDeleted(), crsetCopy.getShowDeleted());
0824:
0825: assertEquals(crset.getSyncProvider().getProviderID(), crsetCopy
0826: .getSyncProvider().getProviderID());
0827: assertEquals(crset.getSyncProvider().getProviderGrade(),
0828: crsetCopy.getSyncProvider().getProviderGrade());
0829: assertEquals(crset.getSyncProvider().getDataSourceLock(),
0830: crsetCopy.getSyncProvider().getDataSourceLock());
0831: assertEquals(crset.getSyncProvider().getVendor(), crsetCopy
0832: .getSyncProvider().getVendor());
0833: assertEquals(crset.getSyncProvider().getVersion(), crsetCopy
0834: .getSyncProvider().getVersion());
0835:
0836: assertEquals(crset.getTableName(), crsetCopy.getTableName());
0837: assertEquals(crset.getTransactionIsolation(), crsetCopy
0838: .getTransactionIsolation());
0839: assertEquals(crset.getType(), crsetCopy.getType());
0840:
0841: assertEquals(crset.getUrl(), crsetCopy.getUrl());
0842: assertEquals(crset.getUsername(), crsetCopy.getUsername());
0843:
0844: }
0845:
0846: public void testCreateCopy2() throws Exception {
0847:
0848: CachedRowSet copy = crset.createCopy();
0849:
0850: copy.absolute(3);
0851: crset.absolute(3);
0852:
0853: copy.updateString(2, "updated");
0854: assertEquals("updated", copy.getString(2));
0855: assertEquals("test3", crset.getString(2));
0856: copy.updateRow();
0857: copy.acceptChanges();
0858:
0859: assertEquals("updated", copy.getString(2));
0860: assertEquals("test3", crset.getString(2));
0861:
0862: crset.updateString(2, "again");
0863:
0864: assertEquals("updated", copy.getString(2));
0865: assertEquals("again", crset.getString(2));
0866:
0867: crset.updateRow();
0868: try {
0869: /*
0870: * seems ri doesn't release lock when expception throw from
0871: * acceptChanges(), which will cause test case block at insertData()
0872: * when next test case setUp, so we must pass current connection to
0873: * it, and all resource would be released after connection closed.
0874: */
0875: crset.acceptChanges(conn);
0876: // TODO: wait the implementation of Writer
0877: // fail("Should throw SyncProviderException");
0878: } catch (SyncProviderException e) {
0879: // expected
0880: }
0881:
0882: assertEquals("updated", copy.getString(2));
0883:
0884: crset.absolute(3);
0885: // data doesn't change
0886: assertEquals("again", crset.getString(2));
0887: }
0888:
0889: public void testCreateCopy3() throws Exception {
0890: crset.setCommand("SELECT * FROM USER_INFO WHERE ID = ?");
0891: crset.setInt(1, 3);
0892: crset.execute();
0893:
0894: assertEquals(12, crset.getMetaData().getColumnCount());
0895: assertTrue(crset.next());
0896: assertEquals("test3", crset.getString(2));
0897: assertFalse(crset.next());
0898:
0899: CachedRowSet crsetCopy = crset.createCopy();
0900: crsetCopy.execute();
0901: assertEquals(12, crsetCopy.getMetaData().getColumnCount());
0902: assertTrue(crsetCopy.next());
0903: assertEquals("test3", crsetCopy.getString(2));
0904: assertFalse(crsetCopy.next());
0905:
0906: crsetCopy.setCommand("SELECT * FROM USER_INFO WHERE NAME = ?");
0907: crsetCopy.setString(1, "test4");
0908: crsetCopy.execute();
0909: assertTrue(crsetCopy.next());
0910: assertEquals(4, crsetCopy.getInt(1));
0911: assertFalse(crsetCopy.next());
0912:
0913: crset.execute();
0914: assertTrue(crset.next());
0915: assertEquals("test3", crset.getString(2));
0916: assertFalse(crset.next());
0917: }
0918:
0919: public void testAfterLast() throws Exception {
0920: try {
0921: rs.afterLast();
0922: fail("Should throw SQLException");
0923: } catch (SQLException e) {
0924: // expected
0925: }
0926:
0927: crset.afterLast();
0928: crset.previous();
0929: assertEquals(4, crset.getInt(1));
0930: }
0931:
0932: public void testNextandPreviousPage() throws Exception {
0933:
0934: st.executeUpdate("delete from USER_INFO");
0935: st
0936: .executeUpdate("insert into USER_INFO(ID,NAME) values (1,'1')");
0937: st
0938: .executeUpdate("insert into USER_INFO(ID,NAME) values (2,'2')");
0939: st
0940: .executeUpdate("insert into USER_INFO(ID,NAME) values (3,'3')");
0941: st
0942: .executeUpdate("insert into USER_INFO(ID,NAME) values (4,'4')");
0943: rs = st.executeQuery("select * from USER_INFO");
0944:
0945: crset.setPageSize(2);
0946: crset.setCommand("SELECT ID FROM USER_INFO");
0947: crset.execute();
0948:
0949: for (int j = 0; j < 2; j++)
0950: crset.next();
0951: assertFalse(crset.next());
0952:
0953: int i = 0;
0954:
0955: crset.beforeFirst();
0956: while (crset.nextPage()) {
0957: while (crset.next()) {
0958: assertEquals(++i, crset.getInt(1));
0959: }
0960: }
0961:
0962: while (crset.previousPage()) {
0963: crset.afterLast();
0964: while (crset.previous()) {
0965: assertEquals(i--, crset.getInt(1));
0966: }
0967: }
0968:
0969: while (crset.previousPage()) {
0970: i = i - crset.getPageSize();
0971: int j = i;
0972: while (crset.next()) {
0973: assertEquals(++j, crset.getInt(1));
0974: }
0975: }
0976: }
0977:
0978: public void testPopulate_LResultSet() throws Exception {
0979: // insert 15 more rows for test
0980: insertMoreData(15);
0981:
0982: rs = st.executeQuery("select * from USER_INFO");
0983: noInitialCrset.setMaxRows(15);
0984: assertEquals(15, noInitialCrset.getMaxRows());
0985:
0986: noInitialCrset.populate(rs);
0987:
0988: assertTrue(noInitialCrset.isBeforeFirst());
0989: int cursorIndex = 0;
0990: while (noInitialCrset.next()) {
0991: cursorIndex++;
0992: }
0993: // setMaxRows no effect, we follow ri
0994: assertEquals(20, cursorIndex);
0995:
0996: /*
0997: * The pageSize won't work when call method populate(ResultSet) without
0998: * second parameter
0999: */
1000: noInitialCrset = newNoInitialInstance();
1001: rs = st.executeQuery("select * from USER_INFO");
1002:
1003: noInitialCrset.setMaxRows(15);
1004: assertEquals(15, noInitialCrset.getMaxRows());
1005:
1006: noInitialCrset.setPageSize(5);
1007: assertEquals(5, noInitialCrset.getPageSize());
1008:
1009: noInitialCrset.populate(rs);
1010:
1011: assertTrue(noInitialCrset.isBeforeFirst());
1012: rs = st.executeQuery("select * from USER_INFO");
1013: cursorIndex = 0;
1014: while (noInitialCrset.next() && rs.next()) {
1015: cursorIndex++;
1016: }
1017: /*
1018: * It's supposed to only get five rows in CachedRowSet as the
1019: * CachedRowSet's pageSize is 5. However, the pageSize doesn't work in
1020: * RI. The CachedRowSet gets all the data from ResultSet. We follow ri.
1021: */
1022: assertEquals(20, cursorIndex);
1023:
1024: noInitialCrset = newNoInitialInstance();
1025: rs = st.executeQuery("select * from USER_INFO");
1026: // cursor move two rows
1027: rs.next();
1028: rs.next();
1029:
1030: noInitialCrset.populate(rs);
1031: assertTrue(noInitialCrset.isBeforeFirst());
1032: cursorIndex = 0;
1033: while (noInitialCrset.next()) {
1034: cursorIndex++;
1035: }
1036: assertEquals(18, cursorIndex);
1037: }
1038:
1039: public void testPopulate_LResultSet_I() throws Exception {
1040: // insert 15 more rows for test
1041: insertMoreData(15);
1042:
1043: rs = st.executeQuery("select * from USER_INFO");
1044: noInitialCrset.setPageSize(5);
1045: try {
1046: noInitialCrset.populate(rs, 1);
1047: fail("Should throw SQLException");
1048: } catch (SQLException e) {
1049: // expected
1050: }
1051:
1052: // create a scrollable and updatable ResultSet
1053: st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1054: ResultSet.CONCUR_UPDATABLE);
1055: rs = st.executeQuery("select * from USER_INFO");
1056:
1057: noInitialCrset = newNoInitialInstance();
1058: noInitialCrset.setPageSize(6);
1059: noInitialCrset.populate(rs, 6);
1060: int cursorIndex = 5;
1061: while (noInitialCrset.next()) {
1062: cursorIndex++;
1063: assertEquals(cursorIndex, noInitialCrset.getInt(1));
1064: }
1065: assertEquals(11, cursorIndex);
1066:
1067: st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1068: ResultSet.CONCUR_UPDATABLE);
1069: rs = st.executeQuery("select * from USER_INFO");
1070:
1071: noInitialCrset = newNoInitialInstance();
1072:
1073: noInitialCrset.setPageSize(6);
1074: noInitialCrset.setMaxRows(5);
1075:
1076: noInitialCrset.populate(rs, 6);
1077: cursorIndex = 0;
1078: while (noInitialCrset.next()) {
1079: cursorIndex++;
1080: assertEquals(cursorIndex + 5, noInitialCrset.getInt(1));
1081: }
1082: // only get MaxRows
1083: assertEquals(5, cursorIndex);
1084:
1085: st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1086: ResultSet.CONCUR_UPDATABLE);
1087: rs = st.executeQuery("select * from USER_INFO");
1088:
1089: noInitialCrset = newNoInitialInstance();
1090:
1091: noInitialCrset.setMaxRows(5);
1092: try {
1093: noInitialCrset.setPageSize(6);
1094: fail("Should throw SQLException");
1095: } catch (SQLException e) {
1096: // expected, Page size cannot be greater than maxRows
1097: }
1098:
1099: }
1100:
1101: public void testPopulate_use_copy() throws Exception {
1102: // insert 15 more rows for test
1103: insertMoreData(15);
1104:
1105: rs = st.executeQuery("select * from USER_INFO");
1106: crset.close();
1107: crset.populate(rs);
1108:
1109: CachedRowSet crsetCopy = crset.createCopy();
1110: assertEquals(0, crsetCopy.getPageSize());
1111: noInitialCrset.setPageSize(5);
1112: // if it doesn't specify the startRow for method populate(), then the
1113: // pageSize wouldn't work.
1114: assertTrue(crsetCopy.isBeforeFirst());
1115: noInitialCrset.populate(crsetCopy);
1116: assertTrue(crsetCopy.isAfterLast());
1117: int cursorIndex = 0;
1118: while (noInitialCrset.next()) {
1119: cursorIndex++;
1120: for (int i = 1; i <= DEFAULT_COLUMN_COUNT; i++) {
1121: assertEquals(cursorIndex, noInitialCrset.getInt(1));
1122: }
1123: }
1124: assertEquals(20, cursorIndex);
1125:
1126: try {
1127: noInitialCrset.populate(crsetCopy, 0);
1128: fail("Should throw SQLException");
1129: } catch (SQLException e) {
1130: // invalid cursor position
1131: }
1132:
1133: try {
1134: noInitialCrset.populate(crsetCopy, -1);
1135: fail("Should throw SQLException");
1136: } catch (SQLException e) {
1137: // invalid cursor position
1138: }
1139:
1140: try {
1141: noInitialCrset.populate(crsetCopy, 100);
1142: fail("Should throw SQLException");
1143: } catch (SQLException e) {
1144: // invalid cursor position
1145: }
1146:
1147: // specify the startRow, then the noInitialCrset will get only 5 rows
1148: noInitialCrset.populate(crsetCopy, 1);
1149: assertEquals(5, noInitialCrset.getPageSize());
1150: assertTrue(noInitialCrset.isBeforeFirst());
1151: cursorIndex = 0;
1152: rs = st.executeQuery("select * from USER_INFO");
1153: while (noInitialCrset.next() && rs.next()) {
1154: cursorIndex++;
1155: for (int i = 1; i <= DEFAULT_COLUMN_COUNT; i++) {
1156: assertEquals(cursorIndex, noInitialCrset.getInt(1));
1157: assertEquals(rs.getObject(i), noInitialCrset
1158: .getObject(i));
1159: }
1160: }
1161: // the pageSize works here
1162: assertEquals(5, cursorIndex);
1163:
1164: // the noInitialCrset would fetch data from the eleventh row
1165: noInitialCrset.populate(crsetCopy, 11);
1166: cursorIndex = 10;
1167: while (noInitialCrset.next()) {
1168: cursorIndex++;
1169: assertEquals(cursorIndex, noInitialCrset.getInt(1));
1170: }
1171: assertEquals(15, cursorIndex);
1172: }
1173:
1174: public void testConstructor() throws Exception {
1175:
1176: assertTrue(noInitialCrset.isReadOnly());
1177: assertEquals(0, noInitialCrset.size());
1178: assertNull(noInitialCrset.getMetaData());
1179:
1180: assertNull(noInitialCrset.getCommand());
1181: assertEquals(ResultSet.CONCUR_UPDATABLE, noInitialCrset
1182: .getConcurrency());
1183: // TODO uncomment after impelemented
1184: // try {
1185: // crset.getCursorName();
1186: // fail("Should throw SQLException");
1187: // } catch (SQLException e) {
1188: // // expected
1189: // }
1190: // try {
1191: // crset.getMatchColumnIndexes();
1192: // fail("Should throw SQLException");
1193: // } catch (SQLException e) {
1194: // // expected
1195: // }
1196: //
1197: // try {
1198: // crset.getMatchColumnNames();
1199: // } catch (SQLException e) {
1200: // // expected
1201: // }
1202: // assertEquals(0, crset.getRow());
1203: // assertNull(crset.getStatement());
1204:
1205: assertEquals(true, noInitialCrset.getEscapeProcessing());
1206: assertEquals(Connection.TRANSACTION_READ_COMMITTED,
1207: noInitialCrset.getTransactionIsolation());
1208:
1209: assertEquals(ResultSet.FETCH_FORWARD, noInitialCrset
1210: .getFetchDirection());
1211: assertEquals(0, noInitialCrset.getFetchSize());
1212: assertNull(noInitialCrset.getKeyColumns());
1213:
1214: assertEquals(0, noInitialCrset.getMaxFieldSize());
1215: assertEquals(0, noInitialCrset.getMaxRows());
1216:
1217: assertEquals(0, noInitialCrset.getPageSize());
1218: assertEquals(null, noInitialCrset.getPassword());
1219: assertEquals(0, noInitialCrset.getQueryTimeout());
1220: assertEquals(false, noInitialCrset.getShowDeleted());
1221:
1222: assertNull(noInitialCrset.getTableName());
1223: assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, noInitialCrset
1224: .getType());
1225:
1226: assertNull(noInitialCrset.getUrl());
1227: assertNull(noInitialCrset.getUsername());
1228:
1229: }
1230:
1231: public void testRelative() throws Exception {
1232: /*
1233: * ri throw SQLException, but spec say relative(1) is identical to next
1234: */
1235: try {
1236: crset.relative(1);
1237: fail("Should throw SQLException");
1238: } catch (SQLException e) {
1239: // expected
1240: }
1241:
1242: assertTrue(crset.next());
1243: assertEquals("hermit", crset.getString(2));
1244:
1245: assertTrue(crset.relative(2));
1246: assertEquals("test3", crset.getString(2));
1247:
1248: assertTrue(crset.relative(-1));
1249: assertEquals("test", crset.getString(2));
1250:
1251: assertTrue(crset.relative(0));
1252: assertEquals("test", crset.getString(2));
1253:
1254: assertFalse(crset.relative(-5));
1255: assertEquals(0, crset.getRow());
1256:
1257: assertTrue(crset.next());
1258: assertEquals("hermit", crset.getString(2));
1259: assertTrue(crset.relative(3));
1260: assertEquals("test4", crset.getString(2));
1261:
1262: assertFalse(crset.relative(3));
1263: assertEquals(0, crset.getRow());
1264:
1265: assertTrue(crset.isAfterLast());
1266: assertTrue(crset.previous());
1267:
1268: // non-bug different
1269: if ("true".equals(System.getProperty("Testing Harmony"))) {
1270: assertEquals(DEFAULT_ROW_COUNT, crset.getRow());
1271: assertEquals("test4", crset.getString(2));
1272: }
1273: }
1274:
1275: public void testAbsolute() throws Exception {
1276: // non-bug different
1277: if ("true".equals(System.getProperty("Testing Harmony"))) {
1278: assertFalse(crset.absolute(0));
1279: }
1280:
1281: assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, crset.getType());
1282: assertTrue(crset.absolute(1));
1283: assertEquals(1, crset.getInt(1));
1284: assertTrue(crset.absolute(4));
1285: assertEquals(4, crset.getInt(1));
1286: assertTrue(crset.absolute(3));
1287: assertEquals(3, crset.getInt(1));
1288:
1289: // when position the cursor beyond the first/last row in the result set
1290: assertFalse(crset.absolute(10));
1291: assertTrue(crset.isAfterLast());
1292: assertTrue(crset.previous());
1293: assertFalse(crset.absolute(-10));
1294: assertTrue(crset.isBeforeFirst());
1295: assertTrue(crset.next());
1296:
1297: /*
1298: * when the given row number is negative, spec says absolute(-1) equals
1299: * last(). However, the return value of absolute(negative) is false when
1300: * run on RI. The Harmony follows the spec.
1301: */
1302: if (System.getProperty("Testing Harmony") == "true") {
1303: assertTrue(crset.absolute(-1));
1304: assertEquals(4, crset.getInt(1));
1305: assertTrue(crset.absolute(-3));
1306: assertEquals(2, crset.getInt(1));
1307: assertFalse(crset.absolute(-5));
1308: }
1309:
1310: crset.setType(ResultSet.TYPE_FORWARD_ONLY);
1311: try {
1312: crset.absolute(1);
1313: fail("should throw SQLException");
1314: } catch (SQLException e) {
1315: // expected
1316: }
1317:
1318: try {
1319: crset.absolute(-1);
1320: fail("should throw SQLException");
1321: } catch (SQLException e) {
1322: // expected
1323: }
1324: }
1325:
1326: public void testNextAndPrevious() throws Exception {
1327: /*
1328: * This method is also used to test isBeforeFirst(), isAfterLast(),
1329: * isFirst(),isLast()
1330: */
1331: // Test for next()
1332: assertTrue(crset.isBeforeFirst());
1333: assertFalse(crset.isAfterLast());
1334: assertFalse(crset.isFirst());
1335: assertFalse(crset.isLast());
1336: assertTrue(crset.next());
1337: assertTrue(crset.isFirst());
1338: assertEquals(1, crset.getInt(1));
1339:
1340: assertTrue(crset.next());
1341: assertFalse(crset.isFirst());
1342: assertTrue(crset.next());
1343: assertTrue(crset.next());
1344: assertTrue(crset.isLast());
1345: assertEquals(4, crset.getInt(1));
1346: assertFalse(crset.next());
1347: // assertFalse(crset.next());
1348: assertFalse(crset.isBeforeFirst());
1349: assertTrue(crset.isAfterLast());
1350:
1351: // Test for previous()
1352: assertTrue(crset.previous());
1353: assertEquals(4, crset.getInt(1));
1354: assertTrue(crset.isLast());
1355: assertTrue(crset.previous());
1356: assertTrue(crset.previous());
1357: assertTrue(crset.previous());
1358: assertEquals(1, crset.getInt(1));
1359: assertTrue(crset.isFirst());
1360: assertFalse(crset.previous());
1361: assertTrue(crset.isBeforeFirst());
1362: // assertFalse(crset.previous());
1363:
1364: assertTrue(crset.next());
1365: assertTrue(crset.next());
1366: assertEquals(2, crset.getInt(1));
1367:
1368: // Test for previous()'s Exception
1369: assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, crset.getType());
1370: crset.setType(ResultSet.TYPE_FORWARD_ONLY);
1371: assertEquals(ResultSet.TYPE_FORWARD_ONLY, crset.getType());
1372: try {
1373: crset.previous();
1374: fail("should throw SQLException");
1375: } catch (SQLException e) {
1376: // expected
1377: }
1378: }
1379:
1380: public void testFirstAndLast() throws Exception {
1381: /*
1382: * This method is used to test afterLast(), beforeFist(), first(),
1383: * last()
1384: */
1385: assertTrue(crset.isBeforeFirst());
1386: assertTrue(crset.first());
1387: assertTrue(crset.isFirst());
1388: assertFalse(crset.isBeforeFirst());
1389: crset.beforeFirst();
1390: assertTrue(crset.isBeforeFirst());
1391: assertTrue(crset.last());
1392: assertTrue(crset.isLast());
1393:
1394: assertTrue(crset.first());
1395: assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, crset.getType());
1396: crset.setType(ResultSet.TYPE_FORWARD_ONLY);
1397: assertEquals(ResultSet.TYPE_FORWARD_ONLY, crset.getType());
1398:
1399: try {
1400: crset.beforeFirst();
1401: fail("should throw SQLException");
1402: } catch (SQLException e) {
1403: // expected
1404: }
1405:
1406: try {
1407: crset.first();
1408: fail("should throw SQLException");
1409: } catch (SQLException e) {
1410: // expected
1411: }
1412:
1413: try {
1414: crset.last();
1415: fail("should throw SQLException");
1416: } catch (SQLException e) {
1417: // expected
1418: }
1419:
1420: assertTrue(crset.isFirst());
1421: }
1422:
1423: public void testAcceptChanges_Insert() throws Exception {
1424: /*
1425: * Insert a new row one time
1426: */
1427: crset.moveToInsertRow();
1428: crset.updateInt(1, 5);
1429: crset.updateString(2, "test5");
1430: crset.updateLong(3, 444423L);
1431: crset.updateBigDecimal(4, new BigDecimal(12));
1432: crset.updateBigDecimal(5, new BigDecimal(23));
1433: crset.updateInt(6, 41);
1434: crset.updateFloat(7, 4.8F);
1435: crset.updateFloat(8, 4.888F);
1436: crset.updateDouble(9, 4.9999);
1437: crset.updateDate(10, new Date(965324512));
1438: crset.updateTime(11, new Time(452368512));
1439: crset.updateTimestamp(12, new Timestamp(874532105));
1440: crset.insertRow();
1441: crset.moveToCurrentRow();
1442: crset.acceptChanges(conn);
1443: // check the new row in CachedRowSet
1444: crset.beforeFirst();
1445: String newRowValue = "";
1446: while (crset.next()) {
1447: if (crset.getInt(1) == 5) {
1448: newRowValue = "test5";
1449: }
1450: }
1451: assertEquals("test5", newRowValue);
1452: // check the new row in DB
1453: rs = st.executeQuery("select * from USER_INFO where ID = 5");
1454: assertTrue(rs.next());
1455: assertEquals(5, rs.getInt(1));
1456:
1457: /*
1458: * TODO Insert multiple rows one time, uncomment after implemented
1459: */
1460: // TODO uncomment it after insert methods are implemented
1461: // noInitialCrset = newNoInitialInstance();
1462: // rs = st.executeQuery("select * from USER_INFO");
1463: // noInitialCrset.populate(rs);
1464: // noInitialCrset.setReadOnly(false);
1465: // noInitialCrset.moveToInsertRow();
1466: // for (int i = 6; i <= 20; i++) {
1467: // noInitialCrset.updateInt(1, i);
1468: // noInitialCrset.updateString(2, "test" + i);
1469: // noInitialCrset.insertRow();
1470: // }
1471: // noInitialCrset.moveToCurrentRow();
1472: // noInitialCrset.acceptChanges(conn);
1473: // // check the new rows in CachedRowSet
1474: // assertEquals(20, noInitialCrset.size());
1475: // // check the new rows in DB
1476: // rs = st.executeQuery("select * from USER_INFO");
1477: // int cursorIndex = 0;
1478: // while (rs.next()) {
1479: // cursorIndex++;
1480: // }
1481: // assertEquals(20, cursorIndex);
1482: }
1483:
1484: public void testAcceptChanges_InsertException() throws Exception {
1485: /*
1486: * Insert a new row. One given column's value exceeds the max range.
1487: * Therefore, it should throw SyncProviderException.
1488: */
1489: crset.moveToInsertRow();
1490: crset.updateInt(1, 4);
1491: crset.updateString(2, "test5");
1492: crset.updateLong(3, 555555L);
1493: crset.updateInt(4, 200000); // 200000 exceeds the NUMERIC's range
1494: crset.updateBigDecimal(5, new BigDecimal(23));
1495: crset.updateFloat(8, 4.888F);
1496: crset.insertRow();
1497: crset.moveToCurrentRow();
1498: try {
1499: crset.acceptChanges(conn);
1500: fail("should throw SyncProviderException");
1501: } catch (SyncProviderException e) {
1502: // TODO analysis SyncProviderException
1503: }
1504:
1505: /*
1506: * Insert a new row. The new row's primary key has existed. Therefore,
1507: * it should throw SyncProviderException.
1508: */
1509: crset = newNoInitialInstance();
1510: rs = st.executeQuery("select * from USER_INFO");
1511: crset.populate(rs);
1512: crset.moveToInsertRow();
1513: crset.updateInt(1, 4); // The ID valued 4 has existed in db.
1514: crset.updateString(2, "test5");
1515: crset.updateBigDecimal(4, new BigDecimal(12));
1516: crset.updateTimestamp(12, new Timestamp(874532105));
1517: crset.insertRow();
1518: crset.moveToCurrentRow();
1519: try {
1520: crset.acceptChanges(conn);
1521: fail("should throw SyncProviderException");
1522: } catch (SyncProviderException e) {
1523: // TODO analysis SyncProviderException
1524: }
1525:
1526: /*
1527: * Insert a new row. Before inserting the new row, another new row which
1528: * has the same data is inserted into the DB. However, the current
1529: * CachedRowSet doesn't know it. In this situation, it should throw
1530: * SyncProviderException.
1531: */
1532: crset = newNoInitialInstance();
1533: rs = st.executeQuery("select * from USER_INFO");
1534: crset.populate(rs);
1535: String insertSQL = "INSERT INTO USER_INFO(ID, NAME, BIGINT_T, NUMERIC_T,DECIMAL_T, SMALLINT_T, "
1536: + "FLOAT_T, REAL_T, DOUBLE_T, DATE_T, TIME_T, TIMESTAMP_T) VALUES(?, ?, ?, ?, ?, ?,"
1537: + "?, ?, ?, ?, ?, ? )";
1538: PreparedStatement preStmt = conn.prepareStatement(insertSQL);
1539: preStmt.setInt(1, 80);
1540: preStmt.setString(2, "test" + 80);
1541: preStmt.setLong(3, 444423L);
1542: preStmt.setBigDecimal(4, new BigDecimal(12));
1543: preStmt.setBigDecimal(5, new BigDecimal(23));
1544: preStmt.setInt(6, 41);
1545: preStmt.setFloat(7, 4.8F);
1546: preStmt.setFloat(8, 4.888F);
1547: preStmt.setDouble(9, 4.9999);
1548: preStmt.setDate(10, new Date(965324512));
1549: preStmt.setTime(11, new Time(452368512));
1550: preStmt.setTimestamp(12, new Timestamp(874532105));
1551: preStmt.executeUpdate();
1552: if (preStmt != null) {
1553: preStmt.close();
1554: }
1555: // check the new row in DB
1556: rs = st.executeQuery("select * from USER_INFO where ID = 80");
1557: assertTrue(rs.next());
1558: assertEquals(80, rs.getInt(1));
1559: assertEquals("test80", rs.getString(2));
1560:
1561: // now call CachedRowSet.insertRow()
1562: crset.moveToInsertRow();
1563: crset.updateInt(1, 80);
1564: crset.updateString(2, "test" + 80);
1565: crset.updateLong(3, 444423L);
1566: crset.updateBigDecimal(4, new BigDecimal(12));
1567: crset.updateBigDecimal(5, new BigDecimal(23));
1568: crset.updateInt(6, 41);
1569: crset.updateFloat(7, 4.8F);
1570: crset.updateFloat(8, 4.888F);
1571: crset.updateDouble(9, 4.9999);
1572: crset.updateDate(10, new Date(965324512));
1573: crset.updateTime(11, new Time(452368512));
1574: crset.updateTimestamp(12, new Timestamp(874532105));
1575: crset.insertRow();
1576: crset.moveToCurrentRow();
1577: try {
1578: crset.acceptChanges(conn);
1579: fail("should throw SyncProviderException");
1580: } catch (SyncProviderException e) {
1581: // TODO analysis SyncProviderException
1582: }
1583: }
1584:
1585: public void testAcceptChanges_Delete() throws Exception {
1586: /*
1587: * Delete all the row. On the first and second row, only two columns
1588: * have value, all the others are NULL. When run on RI, deleteRow() will
1589: * go wrong and throw Exception. According to the spec, deleteRow() is
1590: * supposed to ok.
1591: */
1592: crset.beforeFirst();
1593: while (crset.next()) {
1594: crset.deleteRow();
1595: }
1596: if ("true".equals(System.getProperty("Testing Harmony"))) {
1597: crset.acceptChanges(conn);
1598: } else {
1599: try {
1600: crset.acceptChanges(conn);
1601: } catch (NullPointerException e) {
1602: // RI would throw NullPointerException when deleting a row in
1603: // which some columns' value are null
1604: }
1605: }
1606: // check DB
1607: rs = st.executeQuery("select * from USER_INFO");
1608: int rowCount = 0;
1609: while (rs.next()) {
1610: rowCount++;
1611: }
1612: if ("true".equals(System.getProperty("Testing Harmony"))) {
1613: assertEquals(0, rowCount);
1614: } else {
1615: assertEquals(4, rowCount);
1616: }
1617: }
1618:
1619: public void testAcceptChanges_DeleteException() throws Exception {
1620: /*
1621: * Delete a row which has been deleted from database
1622: */
1623: int result = st
1624: .executeUpdate("delete from USER_INFO where ID = 3");
1625: assertEquals(1, result);
1626: // move to the third row which doesn't exist in database
1627: assertTrue(crset.absolute(3));
1628: assertEquals(3, crset.getInt(1));
1629: crset.deleteRow();
1630: try {
1631: crset.acceptChanges(conn);
1632: fail("should throw SyncProviderException");
1633: } catch (SyncProviderException e) {
1634: // TODO analysis SyncProviderException
1635: }
1636:
1637: /*
1638: * Delete a row which has been updated in database
1639: */
1640: crset = newNoInitialInstance();
1641: rs = st.executeQuery("select * from USER_INFO");
1642: crset.populate(rs);
1643: result = st
1644: .executeUpdate("update USER_INFO set NAME = 'update44' where ID = 4");
1645: assertEquals(1, result);
1646: // move to the updated row
1647: crset.absolute(3);
1648: assertEquals(4, crset.getInt(1));
1649: assertEquals("test4", crset.getString(2));
1650: crset.deleteRow();
1651: try {
1652: crset.acceptChanges(conn);
1653: fail("should throw SyncProviderException");
1654: } catch (SyncProviderException e) {
1655: // TODO analysis SyncProviderException
1656: }
1657: }
1658:
1659: public void testAcceptChanges_Update() throws Exception {
1660: // update the first row
1661: assertTrue(crset.absolute(1));
1662: crset.updateInt(1, 11);
1663: crset.updateString(2, "test11");
1664: crset.updateRow();
1665: crset.acceptChanges(conn);
1666: // check DB
1667: rs = st.executeQuery("select * from USER_INFO where ID = 11");
1668: assertTrue(rs.next());
1669: assertEquals(11, rs.getInt(1));
1670: assertEquals("test11", rs.getString(2));
1671:
1672: // update the third row
1673: noInitialCrset = newNoInitialInstance();
1674: rs = st.executeQuery("select * from USER_INFO");
1675: noInitialCrset.populate(rs);
1676: assertTrue(noInitialCrset.absolute(1));
1677: noInitialCrset.updateInt(1, 111);
1678: noInitialCrset.updateString(2, "update111");
1679: noInitialCrset.updateRow();
1680: assertTrue(noInitialCrset.absolute(3));
1681: noInitialCrset.updateInt(1, 333);
1682: noInitialCrset.updateString(2, "update333");
1683: noInitialCrset.updateLong(3, 33333L);
1684: noInitialCrset.updateRow();
1685: noInitialCrset.acceptChanges(conn);
1686: // check DB
1687: rs = st.executeQuery("select * from USER_INFO where ID = 111");
1688: assertTrue(rs.next());
1689: assertEquals(111, rs.getInt(1));
1690: assertEquals("update111", rs.getString(2));
1691: rs = st.executeQuery("select * from USER_INFO where ID = 333");
1692: assertTrue(rs.next());
1693: assertEquals(333, rs.getInt(1));
1694: assertEquals("update333", rs.getString(2));
1695: assertEquals(33333L, rs.getLong(3));
1696: }
1697:
1698: public void testAcceptChanges_UpdateException() throws Exception {
1699: /*
1700: * Update a row which has been deleted from database
1701: */
1702: int result = st
1703: .executeUpdate("delete from USER_INFO where ID = 3");
1704: assertEquals(1, result);
1705: // move to the third row which doesn't exist in database
1706: assertTrue(crset.absolute(3));
1707: assertEquals(3, crset.getInt(1));
1708: crset.updateString(2, "update33");
1709: crset.updateRow();
1710: try {
1711: crset.acceptChanges(conn);
1712: fail("should throw SyncProviderException");
1713: } catch (SyncProviderException e) {
1714: // TODO analysis SyncProviderException
1715: }
1716:
1717: /*
1718: * Update a row which has been updated in database
1719: */
1720: crset = newNoInitialInstance();
1721: rs = st.executeQuery("select * from USER_INFO");
1722: crset.populate(rs);
1723: result = st
1724: .executeUpdate("update USER_INFO set NAME = 'update44' where ID = 4");
1725: assertEquals(1, result);
1726: // move to the updated row
1727: assertTrue(crset.absolute(3));
1728: assertEquals(4, crset.getInt(1));
1729: assertEquals("test4", crset.getString(2));
1730: crset.updateString(2, "change4");
1731: crset.updateRow();
1732: try {
1733: crset.acceptChanges(conn);
1734: fail("should throw SyncProviderException");
1735: } catch (SyncProviderException e) {
1736: // TODO analysis SyncProviderException
1737: }
1738:
1739: /*
1740: * Update a row in which one column's value is out of range
1741: */
1742: crset = newNoInitialInstance();
1743: rs = st.executeQuery("select * from USER_INFO");
1744: crset.populate(rs);
1745: assertEquals(3, crset.size());
1746: assertTrue(crset.absolute(3));
1747: assertEquals(4, crset.getInt(1));
1748: crset.updateString(2, "update4");
1749: crset.updateLong(3, 555555L);
1750: crset.updateInt(4, 200000); // 200000 exceeds the NUMERIC's range
1751: crset.updateBigDecimal(5, new BigDecimal(23));
1752: crset.updateFloat(8, 4.888F);
1753: crset.updateRow();
1754: try {
1755: crset.acceptChanges(conn);
1756: fail("should throw SyncProviderException");
1757: } catch (SyncProviderException e) {
1758: // TODO analysis SyncProviderException
1759: }
1760: }
1761:
1762: public class Listener implements RowSetListener, Cloneable {
1763:
1764: private String tag = null;
1765:
1766: public void cursorMoved(RowSetEvent theEvent) {
1767: tag = "cursorMoved";
1768: }
1769:
1770: public void rowChanged(RowSetEvent theEvent) {
1771: tag = "rowChanged";
1772: }
1773:
1774: public void rowSetChanged(RowSetEvent theEvent) {
1775: tag = "rowSetChanged";
1776: }
1777:
1778: public String getTag() {
1779: return tag;
1780: }
1781:
1782: public void setTag(String tag) {
1783: this .tag = tag;
1784: }
1785:
1786: public Listener clone() throws CloneNotSupportedException {
1787: Listener listener = (Listener) super.clone();
1788: listener.tag = tag;
1789: return listener;
1790: }
1791: }
1792: }
|