0001: package com.mockrunner.test.jdbc;
0002:
0003: import java.io.ByteArrayInputStream;
0004: import java.io.InputStream;
0005: import java.io.Reader;
0006: import java.io.StringReader;
0007: import java.math.BigDecimal;
0008: import java.math.BigInteger;
0009: import java.sql.Blob;
0010: import java.sql.Clob;
0011: import java.sql.Ref;
0012: import java.sql.ResultSet;
0013: import java.sql.SQLException;
0014: import java.util.ArrayList;
0015: import java.util.Arrays;
0016: import java.util.List;
0017:
0018: import junit.framework.TestCase;
0019:
0020: import com.mockrunner.mock.jdbc.MockBlob;
0021: import com.mockrunner.mock.jdbc.MockClob;
0022: import com.mockrunner.mock.jdbc.MockNClob;
0023: import com.mockrunner.mock.jdbc.MockRef;
0024: import com.mockrunner.mock.jdbc.MockResultSet;
0025: import com.mockrunner.mock.jdbc.MockResultSetMetaData;
0026: import com.mockrunner.mock.jdbc.MockRowId;
0027: import com.mockrunner.mock.jdbc.MockSQLXML;
0028: import com.mockrunner.mock.jdbc.MockStruct;
0029:
0030: public class MockResultSetTest extends TestCase {
0031: private MockResultSet resultSet;
0032:
0033: protected void setUp() throws Exception {
0034: super .setUp();
0035: resultSet = new MockResultSet("");
0036: }
0037:
0038: public void testGetColumnCountMetaData() throws Exception {
0039: assertEquals(0, resultSet.getMetaData().getColumnCount());
0040: resultSet.addColumn("firstColumn");
0041: resultSet.addColumn("secondColumn");
0042: resultSet.addColumn("thirdColumn");
0043: assertEquals(3, resultSet.getMetaData().getColumnCount());
0044: assertEquals("firstColumn", resultSet.getMetaData()
0045: .getColumnName(1));
0046: assertEquals("secondColumn", resultSet.getMetaData()
0047: .getColumnName(2));
0048: assertEquals("thirdColumn", resultSet.getMetaData()
0049: .getColumnName(3));
0050: }
0051:
0052: public void testAddRow() throws Exception {
0053: resultSet.addColumn("firstColumn");
0054: resultSet.addColumn("secondColumn");
0055: resultSet.addColumn("thirdColumn");
0056: List row = new ArrayList();
0057: row.add("value1");
0058: row.add("value2");
0059: row.add("value3");
0060: row.add("value4");
0061: resultSet.addRow(row);
0062: resultSet.addRow(new String[] { "test1", "test2" });
0063: assertTrue(resultSet.getRowCount() == 2);
0064: resultSet.next();
0065: assertFalse(resultSet.rowUpdated());
0066: assertFalse(resultSet.rowDeleted());
0067: assertFalse(resultSet.rowInserted());
0068: assertEquals("value1", resultSet.getString(1));
0069: assertEquals("value2", resultSet.getString(2));
0070: assertEquals("value3", resultSet.getString(3));
0071: assertEquals("value4", resultSet.getString(4));
0072: assertEquals("value1", resultSet.getString("firstColumn"));
0073: assertEquals("value2", resultSet.getString("secondColumn"));
0074: assertEquals("value3", resultSet.getString("thirdColumn"));
0075: assertEquals("value4", resultSet.getString("Column4"));
0076: resultSet.next();
0077: assertFalse(resultSet.rowUpdated());
0078: assertFalse(resultSet.rowDeleted());
0079: assertFalse(resultSet.rowInserted());
0080: assertEquals("test1", resultSet.getString(1));
0081: assertEquals("test2", resultSet.getString(2));
0082: assertEquals(null, resultSet.getString(3));
0083: assertEquals(null, resultSet.getString(4));
0084: assertEquals("test1", resultSet.getString("firstColumn"));
0085: assertEquals("test2", resultSet.getString("secondColumn"));
0086: assertEquals(null, resultSet.getString("thirdColumn"));
0087: assertEquals(null, resultSet.getString("Column4"));
0088: }
0089:
0090: public void testAddColumn() throws Exception {
0091: resultSet.addColumn("intColumn", new Integer[] {
0092: new Integer(1), new Integer(2), new Integer(3) });
0093: List column = new ArrayList();
0094: column.add("value1");
0095: column.add("value2");
0096: column.add("value3");
0097: column.add("value4");
0098: resultSet.addColumn("stringColumn", column);
0099: resultSet.addColumn();
0100: assertTrue(resultSet.getRowCount() == 4);
0101: resultSet.next();
0102: assertFalse(resultSet.rowUpdated());
0103: assertFalse(resultSet.rowDeleted());
0104: assertFalse(resultSet.rowInserted());
0105: assertEquals(1, resultSet.getInt(1));
0106: assertEquals("value1", resultSet.getString(2));
0107: assertEquals(null, resultSet.getObject(3));
0108: assertEquals(1, resultSet.getInt("intColumn"));
0109: assertEquals("value1", resultSet.getString("stringColumn"));
0110: assertEquals(null, resultSet.getString("Column3"));
0111: resultSet.next();
0112: assertFalse(resultSet.rowUpdated());
0113: assertFalse(resultSet.rowDeleted());
0114: assertFalse(resultSet.rowInserted());
0115: assertEquals(2, resultSet.getInt(1));
0116: assertEquals("value2", resultSet.getString(2));
0117: assertEquals(null, resultSet.getObject(3));
0118: assertEquals(2, resultSet.getInt("intColumn"));
0119: assertEquals("value2", resultSet.getString("stringColumn"));
0120: assertEquals(null, resultSet.getString("Column3"));
0121: resultSet.next();
0122: assertFalse(resultSet.rowUpdated());
0123: assertFalse(resultSet.rowDeleted());
0124: assertFalse(resultSet.rowInserted());
0125: assertEquals(3, resultSet.getInt(1));
0126: assertEquals("value3", resultSet.getString(2));
0127: assertEquals(null, resultSet.getObject(3));
0128: assertEquals(3, resultSet.getInt("intColumn"));
0129: assertEquals("value3", resultSet.getString("stringColumn"));
0130: assertEquals(null, resultSet.getString("Column3"));
0131: resultSet.next();
0132: assertFalse(resultSet.rowUpdated());
0133: assertFalse(resultSet.rowDeleted());
0134: assertFalse(resultSet.rowInserted());
0135: assertEquals(0, resultSet.getInt(1));
0136: assertEquals("value4", resultSet.getString(2));
0137: assertEquals(null, resultSet.getObject(3));
0138: assertEquals(0, resultSet.getInt("intColumn"));
0139: assertEquals("value4", resultSet.getString("stringColumn"));
0140: assertEquals(null, resultSet.getString("Column3"));
0141: }
0142:
0143: public void testGetValues() throws Exception {
0144: List column = new ArrayList();
0145: column.add("1.2");
0146: column.add(new Double(3.4));
0147: column.add("1900-12-12");
0148: column.add("value");
0149: column.add(null);
0150: column.add("value");
0151: resultSet.addColumn("column", column);
0152: try {
0153: resultSet.getObject(1);
0154: fail();
0155: } catch (SQLException exc) {
0156: //should throw SQLException
0157: }
0158: resultSet.next();
0159: assertEquals(1.2f, resultSet.getFloat(1), 0.0);
0160: assertEquals(new BigDecimal("1.2"), resultSet.getBigDecimal(1));
0161: assertEquals(new BigDecimal(new BigInteger("120000"), 5),
0162: resultSet.getBigDecimal(1, 5));
0163: assertEquals(new BigDecimal("1.2"), resultSet
0164: .getBigDecimal("column"));
0165: assertEquals(new BigDecimal(new BigInteger("120000"), 5),
0166: resultSet.getBigDecimal("column", 5));
0167: assertFalse(resultSet.wasNull());
0168: assertEquals(1.2, resultSet.getDouble("column"), 0.0);
0169: assertTrue(Arrays.equals(new byte[] { 49, 46, 50 }, resultSet
0170: .getBytes(1)));
0171: assertFalse(resultSet.wasNull());
0172: resultSet.next();
0173: assertEquals("3.4", resultSet.getNString("column"));
0174: assertEquals(3.4, resultSet.getDouble(1), 0.0);
0175: assertEquals(3, resultSet.getInt(1));
0176: resultSet.next();
0177: assertEquals("1900-12-12", resultSet.getDate(1).toString());
0178: char[] charData = new char[10];
0179: resultSet.getCharacterStream(1).read(charData);
0180: assertEquals("1900-12-12", new String(charData));
0181: assertFalse(resultSet.wasNull());
0182: resultSet.next();
0183: byte[] byteData = new byte[5];
0184: resultSet.getBinaryStream("column").read(byteData);
0185: assertEquals("value", new String(byteData, "ISO-8859-1"));
0186: assertFalse(resultSet.wasNull());
0187: resultSet.next();
0188: assertEquals(0, resultSet.getShort(1));
0189: assertTrue(resultSet.wasNull());
0190: assertEquals(null, resultSet.getString(1));
0191: assertTrue(resultSet.wasNull());
0192: resultSet.next();
0193: assertEquals("value", resultSet.getObject("column"));
0194: assertEquals("value", resultSet.getNString("column"));
0195: Clob clob = resultSet.getClob("column");
0196: assertEquals("value", clob.getSubString(1, 5));
0197: assertFalse(resultSet.wasNull());
0198: resultSet.next();
0199: try {
0200: resultSet.getObject(1);
0201: fail();
0202: } catch (SQLException exc) {
0203: //should throw SQLException
0204: }
0205: }
0206:
0207: public void testUpdateValues() throws Exception {
0208: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0209: List column = new ArrayList();
0210: column.add(null);
0211: column.add(new Double(3.4));
0212: column.add("value");
0213: resultSet.addColumn("column1", column);
0214: column = new ArrayList();
0215: column.add(new Integer(2));
0216: column.add("test");
0217: resultSet.addColumn("column2", column);
0218: try {
0219: resultSet.updateObject(1, "value");
0220: fail();
0221: } catch (SQLException exc) {
0222: //should throw SQLException
0223: }
0224: resultSet.next();
0225: resultSet.updateNull(2);
0226: assertNull(resultSet.getObject(1));
0227: assertNull(resultSet.getObject(2));
0228: resultSet.updateInt(1, 3);
0229: assertEquals(new Integer(3), resultSet.getObject(1));
0230: assertNull(resultSet.getObject(2));
0231: resultSet.next();
0232: resultSet.updateBytes(2, new byte[] { 1, 2, 3 });
0233: assertEquals(new Double(3.4), resultSet.getObject(1));
0234: assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, resultSet
0235: .getBlob("column2").getBytes(1, 3)));
0236: resultSet.next();
0237: resultSet.updateObject(1, "3");
0238: assertEquals(3, resultSet.getLong(1));
0239: resultSet.updateNString(1, "1");
0240: assertEquals(1, resultSet.getLong(1));
0241: ByteArrayInputStream stream = new ByteArrayInputStream(
0242: new byte[] { 1, 2, 3, 4, 5 });
0243: resultSet.updateBinaryStream(1, stream, 3);
0244: InputStream inputStream = resultSet.getBinaryStream(1);
0245: assertEquals(1, inputStream.read());
0246: assertEquals(2, inputStream.read());
0247: assertEquals(3, inputStream.read());
0248: assertEquals(-1, inputStream.read());
0249: resultSet.updateRowId(1, new MockRowId(new byte[] { 1, 2, 3 }));
0250: assertEquals(new MockRowId(new byte[] { 1, 2, 3 }), resultSet
0251: .getRowId(1));
0252: }
0253:
0254: public void testUpdateStreams() throws Exception {
0255: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0256: resultSet.addColumn("column", new Object[] { "value" });
0257: resultSet.next();
0258: ByteArrayInputStream updateStream = new ByteArrayInputStream(
0259: new byte[] { 1, 2, 3, 4, 5 });
0260: resultSet.updateAsciiStream(1, updateStream, (long) 2);
0261: InputStream inputStream = resultSet.getAsciiStream("column");
0262: assertEquals(1, inputStream.read());
0263: assertEquals(2, inputStream.read());
0264: assertEquals(-1, inputStream.read());
0265: updateStream = new ByteArrayInputStream(new byte[] { 1, 2, 3,
0266: 4, 5 });
0267: resultSet.updateAsciiStream(1, updateStream);
0268: inputStream = resultSet.getAsciiStream(1);
0269: assertEquals(1, inputStream.read());
0270: assertEquals(2, inputStream.read());
0271: assertEquals(3, inputStream.read());
0272: assertEquals(4, inputStream.read());
0273: assertEquals(5, inputStream.read());
0274: assertEquals(-1, inputStream.read());
0275: updateStream = new ByteArrayInputStream(new byte[] { 1, 2, 3,
0276: 4, 5 });
0277: resultSet.updateBinaryStream("column", updateStream, (long) 3);
0278: inputStream = resultSet.getBinaryStream("column");
0279: assertEquals(1, inputStream.read());
0280: assertEquals(2, inputStream.read());
0281: assertEquals(3, inputStream.read());
0282: assertEquals(-1, inputStream.read());
0283: StringReader updateReader = new StringReader("test");
0284: resultSet.updateCharacterStream(1, updateReader);
0285: Reader inputReader = resultSet.getCharacterStream("column");
0286: assertEquals('t', (char) inputReader.read());
0287: assertEquals('e', (char) inputReader.read());
0288: assertEquals('s', (char) inputReader.read());
0289: assertEquals('t', (char) inputReader.read());
0290: assertEquals(-1, inputReader.read());
0291: updateReader = new StringReader("test");
0292: resultSet.updateCharacterStream(1, updateReader, 1);
0293: inputReader = resultSet.getNCharacterStream(1);
0294: assertEquals('t', (char) inputReader.read());
0295: assertEquals(-1, inputReader.read());
0296: updateReader = new StringReader("test");
0297: resultSet.updateNCharacterStream(1, updateReader, 2);
0298: inputReader = resultSet.getCharacterStream(1);
0299: assertEquals('t', (char) inputReader.read());
0300: assertEquals('e', (char) inputReader.read());
0301: assertEquals(-1, inputReader.read());
0302: }
0303:
0304: public void testUpdateBlobAndClob() throws Exception {
0305: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0306: resultSet.addColumn("column", new Object[] { "value" });
0307: resultSet.next();
0308: resultSet.updateBlob(1, new MockBlob(new byte[] { 1, 2, 3 }));
0309: assertEquals(new MockBlob(new byte[] { 1, 2, 3 }), resultSet
0310: .getBlob("column"));
0311: resultSet.updateBlob("column", new ByteArrayInputStream(
0312: new byte[] { 1, 2, 3 }));
0313: assertEquals(new MockBlob(new byte[] { 1, 2, 3 }), resultSet
0314: .getBlob(1));
0315: resultSet.updateBlob("column", new ByteArrayInputStream(
0316: new byte[] { 1, 2, 3, 4, 5 }), 3);
0317: assertEquals(new MockBlob(new byte[] { 1, 2, 3 }), resultSet
0318: .getBlob(1));
0319: resultSet.updateClob(1, new MockClob("test"));
0320: assertEquals(new MockClob("test"), resultSet.getClob("column"));
0321: resultSet.updateClob("column", new StringReader("test"));
0322: assertEquals(new MockClob("test"), resultSet.getClob(1));
0323: resultSet.updateClob("column", new StringReader("testxyz"), 4);
0324: assertEquals(new MockClob("test"), resultSet.getClob(1));
0325: resultSet.updateNClob("column", new MockNClob("test"));
0326: assertEquals(new MockNClob("test"), resultSet
0327: .getNClob("column"));
0328: resultSet.updateClob(1, new MockClob("test"));
0329: assertEquals(new MockNClob("test"), resultSet.getNClob(1));
0330: resultSet.updateNClob("column", new StringReader("test"));
0331: assertEquals(new MockNClob("test"), resultSet
0332: .getNClob("column"));
0333: resultSet.updateNClob(1, new StringReader("test"), 4);
0334: assertEquals(new MockNClob("test"), resultSet.getNClob(1));
0335: }
0336:
0337: public void testUpdateSQLXML() throws Exception {
0338: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0339: resultSet.addColumn("column", new Object[] { "value" });
0340: resultSet.next();
0341: resultSet.updateSQLXML(1, new MockSQLXML("<abc></abc>"));
0342: assertEquals(new MockSQLXML("<abc></abc>"), resultSet
0343: .getSQLXML("column"));
0344: resultSet.updateSQLXML("column", new MockSQLXML(
0345: "<abc>abc</abc>"));
0346: assertEquals(new MockSQLXML("<abc>abc</abc>"), resultSet
0347: .getSQLXML(1));
0348: }
0349:
0350: public void testError() throws Exception {
0351: resultSet.setResultSetType(ResultSet.TYPE_FORWARD_ONLY);
0352: List column = new ArrayList();
0353: column.add("test");
0354: resultSet.addColumn("column1", column);
0355: try {
0356: resultSet.first();
0357: fail("not scrollable");
0358: } catch (SQLException exc) {
0359: //should throw SQLException
0360: }
0361: try {
0362: resultSet.next();
0363: resultSet.updateInt(1, 1);
0364: fail("not updatable");
0365: } catch (SQLException exc) {
0366: //should throw SQLException
0367: }
0368: try {
0369: resultSet.updateInt("column2", 1);
0370: fail("invalid columnname");
0371: } catch (SQLException exc) {
0372: //should throw SQLException
0373: }
0374: try {
0375: resultSet.updateInt(2, 1);
0376: fail("invalid columnindex");
0377: } catch (SQLException exc) {
0378: //should throw SQLException
0379: }
0380: try {
0381: resultSet.next();
0382: resultSet.updateInt(1, 1);
0383: fail("row invalid");
0384: } catch (SQLException exc) {
0385: //should throw SQLException
0386: }
0387: }
0388:
0389: public void testFindColumn() throws Exception {
0390: resultSet.addColumn("test");
0391: resultSet.addColumn("testxy");
0392: assertEquals(1, resultSet.findColumn("test"));
0393: assertEquals(2, resultSet.findColumn("testxy"));
0394: try {
0395: resultSet.findColumn("test1");
0396: fail("column invalid");
0397: } catch (SQLException exc) {
0398: //should throw SQLException
0399: }
0400: }
0401:
0402: public void testEmptyResultSet() throws Exception {
0403: resultSet.addColumn("test");
0404:
0405: assertFalse(resultSet.isFirst());
0406: assertFalse(resultSet.isAfterLast());
0407: assertFalse(resultSet.isBeforeFirst());
0408: assertFalse(resultSet.isLast());
0409:
0410: assertFalse(resultSet.next());
0411: assertFalse(resultSet.first());
0412: assertFalse(resultSet.last());
0413: assertFalse(resultSet.previous());
0414: assertFalse(resultSet.absolute(1));
0415: assertFalse(resultSet.relative(1));
0416:
0417: assertFalse(resultSet.isFirst());
0418: assertFalse(resultSet.isAfterLast());
0419: assertFalse(resultSet.isBeforeFirst());
0420: assertFalse(resultSet.isLast());
0421:
0422: try {
0423: resultSet.getString("test");
0424: fail();
0425: } catch (SQLException exc) {
0426: //should throw SQLException
0427: }
0428: try {
0429: resultSet.getString(1);
0430: fail();
0431: } catch (SQLException exc) {
0432: //should throw SQLException
0433: }
0434: }
0435:
0436: public void testInsertOnEmptyResultSet() throws Exception {
0437: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0438: resultSet.addColumn("test");
0439: resultSet.moveToInsertRow();
0440: resultSet.updateString("test", "value");
0441: resultSet.insertRow();
0442: resultSet.moveToCurrentRow();
0443: assertEquals("value", resultSet.getString("test"));
0444: assertEquals(1, resultSet.getRowCount());
0445: assertTrue(resultSet.isFirst());
0446: assertTrue(resultSet.isLast());
0447: }
0448:
0449: public void testCursorPosition() throws Exception {
0450: resultSet.setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
0451: resultSet.addRow(new String[] { "1", "2", "3" });
0452: resultSet.addRow(new String[] { "4", "5", "6" });
0453: resultSet.addRow(new String[] { "7", "8", "9" });
0454: assertTrue(resultSet.isBeforeFirst());
0455: assertFalse(resultSet.isAfterLast());
0456: assertTrue(resultSet.last());
0457: assertTrue(resultSet.isLast());
0458: assertTrue(resultSet.first());
0459: assertTrue(resultSet.isFirst());
0460: resultSet.afterLast();
0461: assertTrue(resultSet.isAfterLast());
0462: assertFalse(resultSet.isBeforeFirst());
0463: resultSet.beforeFirst();
0464: assertTrue(resultSet.isBeforeFirst());
0465: assertFalse(resultSet.isAfterLast());
0466: assertTrue(resultSet.next());
0467: assertTrue(resultSet.isFirst());
0468: assertFalse(resultSet.previous());
0469: assertTrue(resultSet.isBeforeFirst());
0470: assertTrue(resultSet.absolute(3));
0471: assertTrue(resultSet.isLast());
0472: assertTrue(resultSet.absolute(1));
0473: assertTrue(resultSet.isFirst());
0474: assertEquals(1, resultSet.getInt(1));
0475: assertTrue(resultSet.relative(1));
0476: assertEquals(4, resultSet.getInt(1));
0477: assertTrue(resultSet.relative(1));
0478: assertTrue(resultSet.isLast());
0479: assertEquals(7, resultSet.getInt(1));
0480: assertTrue(resultSet.relative(-2));
0481: assertTrue(resultSet.isFirst());
0482: assertTrue(resultSet.last());
0483: assertFalse(resultSet.next());
0484: }
0485:
0486: public void testCursorNotAllowed() throws Exception {
0487: resultSet.setResultSetType(ResultSet.TYPE_FORWARD_ONLY);
0488: resultSet.addRow(new String[] { "1", "2", "3" });
0489: resultSet.addRow(new String[] { "4", "5", "6" });
0490: assertTrue(resultSet.isBeforeFirst());
0491: try {
0492: resultSet.first();
0493: fail();
0494: } catch (SQLException exc) {
0495: //should throw SQLException
0496: }
0497: assertTrue(resultSet.isBeforeFirst());
0498: resultSet.next();
0499: resultSet.next();
0500: try {
0501: resultSet.previous();
0502: fail();
0503: } catch (SQLException exc) {
0504: //should throw SQLException
0505: }
0506: try {
0507: resultSet.last();
0508: fail();
0509: } catch (SQLException exc) {
0510: //should throw SQLException
0511: }
0512: try {
0513: resultSet.relative(-1);
0514: fail();
0515: } catch (SQLException exc) {
0516: //should throw SQLException
0517: }
0518: }
0519:
0520: public void testSetFetchDirection() throws Exception {
0521: try {
0522: resultSet.setFetchDirection(ResultSet.FETCH_REVERSE + 1000);
0523: fail();
0524: } catch (SQLException exc) {
0525: //should throw exception
0526: }
0527: resultSet.setResultSetType(ResultSet.TYPE_FORWARD_ONLY);
0528: try {
0529: resultSet.setFetchDirection(ResultSet.FETCH_REVERSE);
0530: fail();
0531: } catch (SQLException exc) {
0532: //should throw exception
0533: }
0534: resultSet.setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
0535: resultSet.addRow(new String[] { "1", "2", "3" });
0536: resultSet.addRow(new String[] { "4", "5", "6" });
0537: resultSet.addRow(new String[] { "7", "8", "9" });
0538: resultSet.addRow(new String[] { "10", "11", "12" });
0539: resultSet.last();
0540: resultSet.setFetchDirection(ResultSet.FETCH_REVERSE);
0541: assertEquals(ResultSet.FETCH_REVERSE, resultSet
0542: .getFetchDirection());
0543: assertTrue(resultSet.isFirst());
0544: resultSet.setFetchDirection(ResultSet.FETCH_FORWARD);
0545: assertEquals(ResultSet.FETCH_FORWARD, resultSet
0546: .getFetchDirection());
0547: resultSet.absolute(3);
0548: assertEquals(7, resultSet.getInt(1));
0549: resultSet.setFetchDirection(ResultSet.FETCH_REVERSE);
0550: assertEquals(ResultSet.FETCH_REVERSE, resultSet
0551: .getFetchDirection());
0552: assertEquals(7, resultSet.getInt(1));
0553: resultSet.previous();
0554: assertEquals(10, resultSet.getInt(1));
0555: assertTrue(resultSet.isFirst());
0556: resultSet.setFetchDirection(ResultSet.FETCH_REVERSE);
0557: assertEquals(ResultSet.FETCH_REVERSE, resultSet
0558: .getFetchDirection());
0559: resultSet.absolute(3);
0560: assertEquals(4, resultSet.getInt(1));
0561: resultSet.setFetchDirection(ResultSet.FETCH_UNKNOWN);
0562: assertEquals(ResultSet.FETCH_UNKNOWN, resultSet
0563: .getFetchDirection());
0564: resultSet.absolute(3);
0565: assertEquals(4, resultSet.getInt(1));
0566: resultSet.setFetchDirection(ResultSet.FETCH_REVERSE);
0567: assertEquals(ResultSet.FETCH_REVERSE, resultSet
0568: .getFetchDirection());
0569: resultSet.absolute(3);
0570: assertEquals(4, resultSet.getInt(1));
0571: resultSet.setFetchDirection(ResultSet.FETCH_FORWARD);
0572: assertEquals(ResultSet.FETCH_FORWARD, resultSet
0573: .getFetchDirection());
0574: resultSet.absolute(3);
0575: assertEquals(7, resultSet.getInt(1));
0576: }
0577:
0578: public void testInsertDeleteRows() throws Exception {
0579: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0580: resultSet.setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
0581: resultSet.addRow(new String[] { "1", "2", "3" });
0582: resultSet.addRow(new String[] { "4", "5", "6" });
0583: resultSet.addRow(new String[] { "7", "8", "9" });
0584: resultSet.addRow(new String[] { "10", "11", "12" });
0585: resultSet.absolute(3);
0586: resultSet.moveToInsertRow();
0587: resultSet.updateString(1, "x");
0588: resultSet.updateString(2, "y");
0589: resultSet.updateString(3, "z");
0590: resultSet.insertRow();
0591: resultSet.moveToCurrentRow();
0592: assertEquals("x", resultSet.getString(1));
0593: assertEquals("y", resultSet.getString(2));
0594: assertEquals("z", resultSet.getString(3));
0595: resultSet.refreshRow();
0596: assertEquals("x", resultSet.getString(1));
0597: assertEquals("y", resultSet.getString(2));
0598: assertEquals("z", resultSet.getString(3));
0599: resultSet.next();
0600: assertEquals("7", resultSet.getString(1));
0601: resultSet.previous();
0602: resultSet.previous();
0603: assertEquals("4", resultSet.getString(1));
0604: resultSet.deleteRow();
0605: try {
0606: resultSet.getString(1);
0607: fail("was deleted");
0608: } catch (SQLException exc) {
0609: //should throw SQLException
0610: }
0611: resultSet.first();
0612: assertFalse(resultSet.rowInserted());
0613: assertFalse(resultSet.rowDeleted());
0614: resultSet.next();
0615: assertFalse(resultSet.rowInserted());
0616: assertTrue(resultSet.rowDeleted());
0617: resultSet.next();
0618: assertTrue(resultSet.rowInserted());
0619: assertFalse(resultSet.rowDeleted());
0620: resultSet.next();
0621: assertFalse(resultSet.rowInserted());
0622: assertFalse(resultSet.rowDeleted());
0623: }
0624:
0625: public void testInsertRowFailure() throws Exception {
0626: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0627: resultSet.setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
0628: resultSet.addRow(new String[] { "1", "2", "3" });
0629: resultSet.next();
0630: try {
0631: resultSet.insertRow();
0632: fail("cursor not in insert row");
0633: } catch (SQLException exc) {
0634: //should throw SQLException
0635: }
0636: resultSet.moveToInsertRow();
0637: try {
0638: resultSet.deleteRow();
0639: fail("cursor is in insert row");
0640: } catch (SQLException exc) {
0641: //should throw SQLException
0642: }
0643: try {
0644: resultSet.updateRow();
0645: fail("cursor is in insert row");
0646: } catch (SQLException exc) {
0647: //should throw SQLException
0648: }
0649: try {
0650: resultSet.cancelRowUpdates();
0651: fail("cursor is in insert row");
0652: } catch (SQLException exc) {
0653: //should throw SQLException
0654: }
0655: }
0656:
0657: public void testUpdateDeleteInvalidRow() throws Exception {
0658: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0659: resultSet.setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
0660: resultSet.addRow(new String[] { "1", "2", "3" });
0661: try {
0662: resultSet.updateRow();
0663: fail();
0664: } catch (SQLException exc) {
0665: //should throw SQLException
0666: }
0667: try {
0668: resultSet.rowUpdated();
0669: fail();
0670: } catch (SQLException exc) {
0671: //should throw SQLException
0672: }
0673: resultSet.next();
0674: resultSet.next();
0675: try {
0676: resultSet.deleteRow();
0677: fail();
0678: } catch (SQLException exc) {
0679: //should throw SQLException
0680: }
0681: try {
0682: resultSet.rowDeleted();
0683: fail();
0684: } catch (SQLException exc) {
0685: //should throw SQLException
0686: }
0687: }
0688:
0689: public void testCloneOnUpdateRow() throws Exception {
0690: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0691: resultSet.setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
0692: resultSet.addRow(new Object[] {
0693: new MockBlob(new byte[] { 1, 2, 3 }),
0694: new MockClob("test") });
0695: resultSet.next();
0696: Blob blob = resultSet.getBlob(1);
0697: Clob clob = resultSet.getClob(2);
0698: blob.setBytes(1, new byte[] { 4, 5, 6 });
0699: clob.setString(1, "anothertest");
0700: assertEquals(new MockBlob(new byte[] { 4, 5, 6 }), resultSet
0701: .getBlob(1));
0702: assertEquals(new MockClob("anothertest"), resultSet.getClob(2));
0703: resultSet.setDatabaseView(true);
0704: assertEquals(new MockBlob(new byte[] { 1, 2, 3 }), resultSet
0705: .getBlob(1));
0706: assertEquals(new MockClob("test"), resultSet.getClob(2));
0707: resultSet.updateRow();
0708: assertEquals(new MockBlob(new byte[] { 4, 5, 6 }), resultSet
0709: .getBlob(1));
0710: assertEquals(new MockClob("anothertest"), resultSet.getClob(2));
0711: resultSet.setDatabaseView(false);
0712: assertEquals(new MockBlob(new byte[] { 4, 5, 6 }), resultSet
0713: .getBlob(1));
0714: assertEquals(new MockClob("anothertest"), resultSet.getClob(2));
0715: }
0716:
0717: public void testCloneOnInsertRow() throws Exception {
0718: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0719: resultSet.setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
0720: resultSet.addColumn("column", new Object[] { "1" });
0721: resultSet.next();
0722: resultSet.moveToInsertRow();
0723: resultSet.updateRef("column", new MockRef("test"));
0724: resultSet.insertRow();
0725: resultSet.moveToCurrentRow();
0726: assertEquals(new MockRef("test"), resultSet.getRef("column"));
0727: resultSet.setDatabaseView(true);
0728: assertEquals(new MockRef("test"), resultSet.getRef("column"));
0729: resultSet.setDatabaseView(false);
0730: Ref ref = resultSet.getRef("column");
0731: ref.setObject("anothertest");
0732: assertEquals(new MockRef("anothertest"), resultSet
0733: .getRef("column"));
0734: resultSet.setDatabaseView(true);
0735: assertEquals(new MockRef("test"), resultSet.getRef("column"));
0736: }
0737:
0738: public void testDatabaseView() throws Exception {
0739: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0740: resultSet.setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
0741: resultSet.addRow(new String[] { "1", "2", "3" });
0742: resultSet.addRow(new String[] { "4", "5", "6" });
0743: resultSet.addRow(new String[] { "7", "8", "9" });
0744: resultSet.addRow(new String[] { "10", "11", "12" });
0745: resultSet.absolute(3);
0746: resultSet.updateInt(1, 3);
0747: assertEquals(3, resultSet.getInt(1));
0748: assertEquals(8, resultSet.getInt(2));
0749: assertEquals(9, resultSet.getInt(3));
0750: resultSet.setDatabaseView(true);
0751: assertEquals(7, resultSet.getInt(1));
0752: assertEquals(8, resultSet.getInt(2));
0753: assertEquals(9, resultSet.getInt(3));
0754: resultSet.updateRow();
0755: assertEquals(3, resultSet.getInt(1));
0756: assertEquals(8, resultSet.getInt(2));
0757: assertEquals(9, resultSet.getInt(3));
0758: resultSet.setDatabaseView(false);
0759: resultSet.first();
0760: assertFalse(resultSet.rowUpdated());
0761: resultSet.next();
0762: assertFalse(resultSet.rowUpdated());
0763: resultSet.next();
0764: assertTrue(resultSet.rowUpdated());
0765: resultSet.next();
0766: assertFalse(resultSet.rowUpdated());
0767: resultSet.updateInt(2, 4);
0768: resultSet.cancelRowUpdates();
0769: assertEquals(10, resultSet.getInt(1));
0770: assertEquals(11, resultSet.getInt(2));
0771: assertEquals(12, resultSet.getInt(3));
0772: resultSet.setDatabaseView(true);
0773: assertEquals(10, resultSet.getInt(1));
0774: assertEquals(11, resultSet.getInt(2));
0775: assertEquals(12, resultSet.getInt(3));
0776: assertFalse(resultSet.rowUpdated());
0777: }
0778:
0779: public void testIsEqual() throws Exception {
0780: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0781: resultSet.addColumn("col1");
0782: resultSet.addRow(new String[] { "test1", "test2" });
0783: resultSet.addRow(new String[] { "test3", "test4" });
0784: resultSet.addRow(new String[] { "test5", "test6" });
0785: List row1 = resultSet.getRow(1);
0786: assertEquals("test1", row1.get(0));
0787: assertEquals("test2", row1.get(1));
0788: List row3 = resultSet.getRow(3);
0789: assertEquals("test5", row3.get(0));
0790: assertEquals("test6", row3.get(1));
0791: List column2 = resultSet.getColumn(2);
0792: assertEquals("test2", column2.get(0));
0793: assertEquals("test4", column2.get(1));
0794: assertEquals("test6", column2.get(2));
0795: List column1 = resultSet.getColumn("col1");
0796: assertEquals("test1", column1.get(0));
0797: assertEquals("test3", column1.get(1));
0798: assertEquals("test5", column1.get(2));
0799: MockResultSet otherResult = new MockResultSet("");
0800: otherResult.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0801: otherResult.addRow(new String[] { "test1", "test2" });
0802: otherResult.addRow(new String[] { "test3", "test4" });
0803: otherResult.addRow(new String[] { "test5", "test6" });
0804: assertFalse(resultSet.isEqual(otherResult));
0805: assertFalse(otherResult.isEqual(resultSet));
0806: List testList = new ArrayList();
0807: testList.add("test3");
0808: testList.add("test4");
0809: assertTrue(otherResult.isRowEqual(2, testList));
0810: assertFalse(otherResult.isRowEqual(1, testList));
0811: assertFalse(otherResult.isColumnEqual(1, testList));
0812: otherResult.addColumn("col1");
0813: otherResult = new MockResultSet("");
0814: otherResult.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0815: otherResult.addColumn("col1");
0816: otherResult.addRow(new String[] { "test1", "test2" });
0817: otherResult.addRow(new String[] { "test3", "test4" });
0818: otherResult.addRow(new String[] { "test5", "test6" });
0819: assertTrue(resultSet.isEqual(otherResult));
0820: assertTrue(otherResult.isEqual(resultSet));
0821: testList = new ArrayList();
0822: testList.add("test1");
0823: testList.add("test3");
0824: testList.add("test5");
0825: assertTrue(otherResult.isColumnEqual(1, testList));
0826: assertTrue(otherResult.isColumnEqual("col1", testList));
0827: resultSet.next();
0828: resultSet.next();
0829: resultSet.updateClob(1, new MockClob("Test"));
0830: testList = new ArrayList();
0831: testList.add("test1");
0832: testList.add(new MockClob("Test"));
0833: testList.add("test5");
0834: assertTrue(resultSet.isColumnEqual("col1", testList));
0835: resultSet.setDatabaseView(true);
0836: assertFalse(resultSet.isColumnEqual("col1", testList));
0837: resultSet.updateRow();
0838: assertTrue(resultSet.isColumnEqual("col1", testList));
0839: otherResult.next();
0840: otherResult.next();
0841: otherResult.updateClob(1, new MockClob("Test"));
0842: assertTrue(resultSet.isEqual(otherResult));
0843: assertTrue(otherResult.isEqual(resultSet));
0844: otherResult.setDatabaseView(true);
0845: assertFalse(resultSet.isEqual(otherResult));
0846: assertFalse(otherResult.isEqual(resultSet));
0847: otherResult.updateRow();
0848: assertTrue(resultSet.isEqual(otherResult));
0849: assertTrue(otherResult.isEqual(resultSet));
0850: otherResult = new MockResultSet("");
0851: otherResult.addRow(new Integer[] { new Integer(1),
0852: new Integer(2), new Integer(3) });
0853: otherResult.addRow(new Integer[] { new Integer(4),
0854: new Integer(5), new Integer(6) });
0855: otherResult.addRow(new Integer[] { new Integer(7),
0856: new Integer(8), new Integer(9) });
0857: testList = new ArrayList();
0858: testList.add("1");
0859: testList.add("4");
0860: testList.add("7");
0861: assertTrue(otherResult.isColumnEqual(1, testList));
0862: testList = new ArrayList();
0863: testList.add("7");
0864: testList.add("8");
0865: testList.add("9");
0866: assertTrue(otherResult.isRowEqual(3, testList));
0867: resultSet.addRow(new String[] { "test1", "test2" });
0868: resultSet.addRow(new String[] { "test3", "test4" });
0869: resultSet.addRow(new Object[] { new MockClob("test5"),
0870: new MockStruct("test6") });
0871: }
0872:
0873: public void testIsEqualNotAssignableAndNull() throws Exception {
0874: resultSet.addColumn("col1");
0875: resultSet.addColumn("col2");
0876: resultSet.addRow(new Object[] { new Integer(1), null });
0877: List rowList = new ArrayList();
0878: rowList.add("1");
0879: rowList.add(null);
0880: assertTrue(resultSet.isRowEqual(1, rowList));
0881: List columnList1 = new ArrayList();
0882: columnList1.add("1");
0883: List columnList2 = new ArrayList();
0884: columnList2.add(null);
0885: assertTrue(resultSet.isColumnEqual(1, columnList1));
0886: assertTrue(resultSet.isColumnEqual(2, columnList2));
0887: assertTrue(resultSet.isColumnEqual("col1", columnList1));
0888: assertTrue(resultSet.isColumnEqual("col2", columnList2));
0889: MockResultSet otherResult = new MockResultSet("");
0890: otherResult.addColumn("col1");
0891: otherResult.addColumn("col2");
0892: otherResult.addRow(new String[] { "1", null });
0893: assertTrue(resultSet.isEqual(otherResult));
0894: }
0895:
0896: public void testRowsInsertedDeletedUpdated() throws Exception {
0897: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0898: resultSet.addRow(new String[] { "test1", "test2" });
0899: resultSet.addRow(new String[] { "test3", "test4" });
0900: resultSet.addRow(new String[] { "test5", "test6" });
0901: assertFalse(resultSet.rowInserted(1));
0902: assertFalse(resultSet.rowInserted(2));
0903: assertFalse(resultSet.rowInserted(3));
0904: assertFalse(resultSet.rowDeleted(1));
0905: assertFalse(resultSet.rowDeleted(2));
0906: assertFalse(resultSet.rowDeleted(3));
0907: assertFalse(resultSet.rowUpdated(1));
0908: assertFalse(resultSet.rowUpdated(2));
0909: assertFalse(resultSet.rowUpdated(3));
0910: resultSet.next();
0911: resultSet.next();
0912: resultSet.deleteRow();
0913: assertFalse(resultSet.rowInserted(1));
0914: assertFalse(resultSet.rowInserted(2));
0915: assertFalse(resultSet.rowInserted(3));
0916: assertFalse(resultSet.rowDeleted(1));
0917: assertTrue(resultSet.rowDeleted(2));
0918: assertFalse(resultSet.rowDeleted(3));
0919: assertFalse(resultSet.rowUpdated(1));
0920: assertFalse(resultSet.rowUpdated(2));
0921: assertFalse(resultSet.rowUpdated(3));
0922: resultSet.next();
0923: resultSet.updateRow();
0924: assertFalse(resultSet.rowInserted(1));
0925: assertFalse(resultSet.rowInserted(2));
0926: assertFalse(resultSet.rowInserted(3));
0927: assertFalse(resultSet.rowDeleted(1));
0928: assertTrue(resultSet.rowDeleted(2));
0929: assertFalse(resultSet.rowDeleted(3));
0930: assertFalse(resultSet.rowUpdated(1));
0931: assertFalse(resultSet.rowUpdated(2));
0932: assertTrue(resultSet.rowUpdated(3));
0933: resultSet.moveToInsertRow();
0934: resultSet.updateString(1, "xyz");
0935: resultSet.updateString(2, "xyz");
0936: resultSet.insertRow();
0937: resultSet.moveToCurrentRow();
0938: assertFalse(resultSet.rowInserted(1));
0939: assertFalse(resultSet.rowInserted(2));
0940: assertTrue(resultSet.rowInserted(3));
0941: assertFalse(resultSet.rowInserted(4));
0942: assertFalse(resultSet.rowDeleted(1));
0943: assertTrue(resultSet.rowDeleted(2));
0944: assertFalse(resultSet.rowDeleted(3));
0945: assertFalse(resultSet.rowDeleted(4));
0946: assertFalse(resultSet.rowUpdated(1));
0947: assertFalse(resultSet.rowUpdated(2));
0948: assertFalse(resultSet.rowUpdated(3));
0949: assertTrue(resultSet.rowUpdated(4));
0950: }
0951:
0952: public void testRowsInsertDeleteUpdateNotAllowed() throws Exception {
0953: resultSet.addRow(new String[] { "test1", "test2" });
0954: assertFalse(resultSet.rowInserted(1));
0955: assertFalse(resultSet.rowDeleted(1));
0956: assertFalse(resultSet.rowUpdated(1));
0957: resultSet.next();
0958: try {
0959: resultSet.deleteRow();
0960: fail();
0961: } catch (SQLException exc) {
0962: //should throw SQLException
0963: }
0964: assertFalse(resultSet.rowInserted(1));
0965: assertFalse(resultSet.rowDeleted(1));
0966: assertFalse(resultSet.rowUpdated(1));
0967: try {
0968: resultSet.updateRow();
0969: fail();
0970: } catch (SQLException exc) {
0971: //should throw SQLException
0972: }
0973: assertFalse(resultSet.rowInserted(1));
0974: assertFalse(resultSet.rowDeleted(1));
0975: assertFalse(resultSet.rowUpdated(1));
0976: resultSet.moveToInsertRow();
0977: try {
0978: resultSet.insertRow();
0979: fail();
0980: } catch (SQLException exc) {
0981: //should throw SQLException
0982: }
0983: assertFalse(resultSet.rowInserted(1));
0984: assertFalse(resultSet.rowDeleted(1));
0985: assertFalse(resultSet.rowUpdated(1));
0986: }
0987:
0988: public void testCloneAndIsEqual() throws Exception {
0989: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
0990: resultSet.addRow(new String[] { "test1", "test2" });
0991: resultSet.addRow(new String[] { "test3", "test4" });
0992: resultSet.addRow(new Object[] { new MockClob("test5"),
0993: new MockStruct("test6") });
0994: MockResultSet cloneResult = (MockResultSet) resultSet.clone();
0995: assertTrue(resultSet.isEqual(cloneResult));
0996: resultSet.next();
0997: resultSet.next();
0998: resultSet.next();
0999: resultSet.updateClob(1, new MockClob("test"));
1000: assertFalse(resultSet.isEqual(cloneResult));
1001: resultSet.setDatabaseView(true);
1002: assertTrue(resultSet.isEqual(cloneResult));
1003: resultSet.updateRow();
1004: assertFalse(resultSet.isEqual(cloneResult));
1005: resultSet.setDatabaseView(false);
1006: MockClob clob = (MockClob) resultSet.getClob(1);
1007: assertEquals("test", clob.getSubString(1, 4));
1008: clob.setString(1, "xyzx");
1009: assertEquals("xyzx", clob.getSubString(1, 4));
1010: List list = new ArrayList();
1011: list.add(new MockClob("xyzx"));
1012: list.add(new MockStruct("test6"));
1013: assertTrue(resultSet.isRowEqual(3, list));
1014: assertFalse(cloneResult.isRowEqual(3, list));
1015: list = new ArrayList();
1016: list.add(new MockClob("test5"));
1017: list.add(new MockStruct("test6"));
1018: assertTrue(cloneResult.isRowEqual(3, list));
1019: }
1020:
1021: public void testCloneDeepCopyMetaData() throws Exception {
1022: MockResultSetMetaData metaData1 = new MockResultSetMetaData();
1023: resultSet.setResultSetMetaData(metaData1);
1024: MockResultSet cloneResult = (MockResultSet) resultSet.clone();
1025: assertTrue(resultSet.isEqual(cloneResult));
1026: assertNotSame(metaData1, cloneResult.getMetaData());
1027: }
1028:
1029: public void testCaseInsensitiveColumns() throws Exception {
1030: resultSet.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
1031: resultSet.addColumn("first", new String[] { "1", "2", "3" });
1032: resultSet.addColumn("second", new String[] { "4", "5", "6" });
1033: resultSet.addColumn("third", new String[] { "7", "true", "9" });
1034: resultSet.next();
1035: assertEquals("1", resultSet.getString("FIRST"));
1036: assertEquals("4", resultSet.getString("second"));
1037: assertEquals(7, resultSet.getInt("Third"));
1038: resultSet.next();
1039: assertEquals("2", resultSet.getObject("FirsT"));
1040: assertEquals("5", resultSet.getString("sEcond"));
1041: assertEquals(true, resultSet.getBoolean("THIRD"));
1042: resultSet.next();
1043: resultSet.setDatabaseView(true);
1044: assertEquals("3", resultSet.getString("FIrST"));
1045: assertEquals(6.0, resultSet.getDouble("second"), 0);
1046: assertEquals(9, resultSet.getInt("ThiRd"));
1047: resultSet.moveToInsertRow();
1048: resultSet.updateString("FIRST", "x");
1049: resultSet.insertRow();
1050: resultSet.moveToCurrentRow();
1051: assertEquals("x", resultSet.getString("first"));
1052: }
1053:
1054: public void testCaseSensitiveColumns() throws Exception {
1055: resultSet.setColumnsCaseSensitive(true);
1056: resultSet.addColumn("first", new String[] { "1" });
1057: resultSet.next();
1058: assertEquals("1", resultSet.getString("first"));
1059: try {
1060: resultSet.getString("FIRST");
1061: fail();
1062: } catch (SQLException e) {
1063: //expected exception
1064: }
1065: }
1066: }
|