001: package com.mockrunner.test.jdbc;
002:
003: import java.sql.BatchUpdateException;
004: import java.sql.ResultSet;
005: import java.sql.SQLException;
006: import java.sql.SQLWarning;
007: import java.sql.Statement;
008:
009: import com.mockrunner.base.BaseTestCase;
010: import com.mockrunner.jdbc.StatementResultSetHandler;
011: import com.mockrunner.mock.jdbc.MockCallableStatement;
012: import com.mockrunner.mock.jdbc.MockConnection;
013: import com.mockrunner.mock.jdbc.MockPreparedStatement;
014: import com.mockrunner.mock.jdbc.MockResultSet;
015: import com.mockrunner.mock.jdbc.MockStatement;
016:
017: public class MockStatementTest extends BaseTestCase {
018: private StatementResultSetHandler statementHandler;
019: private MockConnection connection;
020: private MockResultSet resultSet1;
021: private MockResultSet resultSet2;
022: private MockResultSet resultSet3;
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026: resultSet1 = new MockResultSet("");
027: resultSet1.addRow(new String[] { "a", "b", "c" });
028: resultSet2 = new MockResultSet("");
029: resultSet2.addRow(new String[] { "column11", "column21" });
030: resultSet2.addRow(new String[] { "column12", "column22" });
031: resultSet3 = new MockResultSet("");
032: resultSet3.addRow(new String[] { "test1", "test2" });
033: resultSet3.addRow(new String[] { "test3", "test4" });
034: resultSet3.addRow(new String[] { "test5", "test6" });
035: connection = getJDBCMockObjectFactory().getMockConnection();
036: statementHandler = connection.getStatementResultSetHandler();
037: }
038:
039: protected void tearDown() throws Exception {
040: super .tearDown();
041: statementHandler = null;
042: connection = null;
043: resultSet1 = null;
044: resultSet2 = null;
045: resultSet3 = null;
046: }
047:
048: private boolean isEmpty(MockResultSet resultSet) {
049: return resultSet.getRowCount() == 0;
050: }
051:
052: private boolean isResultSet1(MockResultSet resultSet) {
053: return resultSet.getRowCount() == 1;
054: }
055:
056: private boolean isResultSet2(MockResultSet resultSet) {
057: return resultSet.getRowCount() == 2;
058: }
059:
060: private boolean isResultSet3(MockResultSet resultSet) {
061: return resultSet.getRowCount() == 3;
062: }
063:
064: public void testPrepareResultSet() throws Exception {
065: statementHandler.prepareGlobalResultSet(resultSet3);
066: statementHandler.prepareResultSet("select test from",
067: resultSet1);
068: statementHandler.prepareResultSet("select test1, test2",
069: resultSet2);
070: MockStatement statement = (MockStatement) connection
071: .createStatement();
072: statement.setCursorName("cursor");
073: MockResultSet testResultSet = (MockResultSet) statement
074: .executeQuery("SELECT test1, test2 FROM y WHERE value = 1");
075: assertEquals("cursor", testResultSet.getCursorName());
076: assertTrue(isResultSet2(testResultSet));
077: statementHandler.setCaseSensitive(true);
078: testResultSet = (MockResultSet) statement
079: .executeQuery("SELECT test1, test2 FROM y WHERE value = 1");
080: assertTrue(isResultSet3(testResultSet));
081: statementHandler.setCaseSensitive(false);
082: testResultSet = (MockResultSet) statement
083: .executeQuery("select test from x where value = true");
084: assertTrue(isResultSet1(testResultSet));
085: statementHandler.setExactMatch(true);
086: testResultSet = (MockResultSet) statement
087: .executeQuery("select test from x where value = true");
088: assertTrue(isResultSet3(testResultSet));
089: statementHandler.setExactMatch(false);
090: assertTrue(statement
091: .execute("select test from x where value = true"));
092: assertTrue(isResultSet1((MockResultSet) statement
093: .getResultSet()));
094: assertEquals(-1, statement.getUpdateCount());
095: statementHandler.prepareReturnsResultSet("select test from",
096: false);
097: assertFalse(statement.execute("select test from"));
098: assertNull(statement.getResultSet());
099: assertEquals(0, statement.getUpdateCount());
100: }
101:
102: public void testPrepareMultipleResultSets() throws Exception {
103: statementHandler.prepareGlobalResultSet(resultSet3);
104: statementHandler.prepareResultSets("select test from",
105: new MockResultSet[] { resultSet1, resultSet2,
106: resultSet3 });
107: MockStatement statement = (MockStatement) connection
108: .createStatement();
109: MockResultSet testResultSet = (MockResultSet) statement
110: .executeQuery("xyz");
111: assertTrue(isResultSet3(testResultSet));
112: assertFalse(statement.getMoreResults());
113: assertFalse(statement.getMoreResults());
114: testResultSet = (MockResultSet) statement
115: .executeQuery("SELECT TEST FROM");
116: assertTrue(isResultSet1(testResultSet));
117: assertTrue(isResultSet1((MockResultSet) statement
118: .getResultSet()));
119: assertNotSame(resultSet1, (MockResultSet) statement
120: .getResultSet());
121: assertEquals(-1, statement.getUpdateCount());
122: assertTrue(statement.getMoreResults());
123: assertTrue(isResultSet2((MockResultSet) statement
124: .getResultSet()));
125: assertNotSame(resultSet2, (MockResultSet) statement
126: .getResultSet());
127: assertEquals(-1, statement.getUpdateCount());
128: assertTrue(statement.getMoreResults());
129: assertTrue(isResultSet3((MockResultSet) statement
130: .getResultSet()));
131: assertNotSame(resultSet3, (MockResultSet) statement
132: .getResultSet());
133: assertEquals(-1, statement.getUpdateCount());
134: assertFalse(statement.getMoreResults());
135: assertFalse(statement.getMoreResults());
136: assertNull(statement.getResultSet());
137: assertEquals(-1, statement.getUpdateCount());
138: }
139:
140: public void testPrepareMultipleResultSetsClose() throws Exception {
141: statementHandler.prepareResultSets("select test",
142: new MockResultSet[] { resultSet1, resultSet2,
143: resultSet3 });
144: MockStatement statement = (MockStatement) connection
145: .createStatement();
146: statement.executeQuery("select test");
147: MockResultSet testResultSet1 = (MockResultSet) statement
148: .getResultSet();
149: statement.getMoreResults();
150: MockResultSet testResultSet2 = (MockResultSet) statement
151: .getResultSet();
152: statement.getMoreResults();
153: MockResultSet testResultSet3 = (MockResultSet) statement
154: .getResultSet();
155: assertTrue(testResultSet1.isClosed());
156: assertTrue(testResultSet2.isClosed());
157: assertFalse(testResultSet3.isClosed());
158: statement.getMoreResults();
159: assertTrue(testResultSet3.isClosed());
160: statement.execute("select test");
161: testResultSet1 = (MockResultSet) statement.getResultSet();
162: statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
163: testResultSet2 = (MockResultSet) statement.getResultSet();
164: statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
165: testResultSet3 = (MockResultSet) statement.getResultSet();
166: statement.getMoreResults(Statement.CLOSE_CURRENT_RESULT);
167: assertFalse(testResultSet1.isClosed());
168: assertFalse(testResultSet2.isClosed());
169: assertTrue(testResultSet3.isClosed());
170: }
171:
172: public void testCurrentResultSetsCloseOnExecute() throws Exception {
173: statementHandler.prepareResultSets("select test",
174: new MockResultSet[] { resultSet1, resultSet2,
175: resultSet3 });
176: MockStatement statement = (MockStatement) connection
177: .createStatement();
178: statement.executeQuery("select test");
179: MockResultSet testResultSet1 = (MockResultSet) statement
180: .getResultSet();
181: statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
182: MockResultSet testResultSet2 = (MockResultSet) statement
183: .getResultSet();
184: statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
185: MockResultSet testResultSet3 = (MockResultSet) statement
186: .getResultSet();
187: statement.executeQuery("select xyz");
188: assertTrue(testResultSet1.isClosed());
189: assertTrue(testResultSet2.isClosed());
190: assertTrue(testResultSet3.isClosed());
191: }
192:
193: public void testPrepareResultSetsStatementSet() throws Exception {
194: MockResultSet resultSet = new MockResultSet("id");
195: statementHandler.prepareGlobalResultSet(resultSet);
196: statementHandler.prepareResultSets("select test",
197: new MockResultSet[] { resultSet1, resultSet2,
198: resultSet3 });
199: MockStatement statement = (MockStatement) connection
200: .createStatement();
201: statement.executeQuery("select test");
202: MockResultSet testResultSet1 = (MockResultSet) statement
203: .getResultSet();
204: statement.getMoreResults();
205: MockResultSet testResultSet2 = (MockResultSet) statement
206: .getResultSet();
207: statement.getMoreResults();
208: MockResultSet testResultSet3 = (MockResultSet) statement
209: .getResultSet();
210: MockResultSet testResultSet = (MockResultSet) statement
211: .executeQuery("xyz");
212: assertSame(statement, testResultSet.getStatement());
213: assertSame(statement, testResultSet1.getStatement());
214: assertSame(statement, testResultSet2.getStatement());
215: assertSame(statement, testResultSet3.getStatement());
216: }
217:
218: public void testPrepareResultSetsNullValues() throws Exception {
219: statementHandler.prepareResultSets("select1",
220: new MockResultSet[] {});
221: MockStatement statement = (MockStatement) connection
222: .createStatement();
223: MockResultSet testResultSet = (MockResultSet) statement
224: .executeQuery("select1");
225: assertNull(testResultSet);
226: assertFalse(statement.getMoreResults());
227: assertEquals(-1, statement.getUpdateCount());
228: statementHandler.prepareResultSet("select2", null);
229: testResultSet = (MockResultSet) statement
230: .executeQuery("select2");
231: assertNull(testResultSet);
232: assertFalse(statement.getMoreResults());
233: assertFalse(statement.getMoreResults());
234: assertNull(statement.getResultSet());
235: assertEquals(-1, statement.getUpdateCount());
236: }
237:
238: public void testPrepareMultipleGlobalResultSets() throws Exception {
239: statementHandler.prepareGlobalResultSets(new MockResultSet[] {
240: resultSet3, resultSet2, resultSet1 });
241: statementHandler.prepareResultSet("select test from",
242: resultSet2);
243: MockStatement statement = (MockStatement) connection
244: .createStatement();
245: MockResultSet testResultSet = (MockResultSet) statement
246: .executeQuery("select test from");
247: assertTrue(isResultSet2(testResultSet));
248: assertTrue(isResultSet2((MockResultSet) statement
249: .getResultSet()));
250: assertEquals(-1, statement.getUpdateCount());
251: assertFalse(statement.getMoreResults());
252: assertNull(statement.getResultSet());
253: assertEquals(-1, statement.getUpdateCount());
254: testResultSet = (MockResultSet) statement.executeQuery("xyz");
255: assertTrue(isResultSet3(testResultSet));
256: assertTrue(isResultSet3((MockResultSet) statement
257: .getResultSet()));
258: assertEquals(-1, statement.getUpdateCount());
259: assertTrue(statement.getMoreResults());
260: assertTrue(isResultSet2((MockResultSet) statement
261: .getResultSet()));
262: assertEquals(-1, statement.getUpdateCount());
263: assertTrue(statement.getMoreResults());
264: assertTrue(isResultSet1((MockResultSet) statement
265: .getResultSet()));
266: assertEquals(-1, statement.getUpdateCount());
267: assertFalse(statement.getMoreResults());
268: assertNull(statement.getResultSet());
269: assertEquals(-1, statement.getUpdateCount());
270: }
271:
272: public void testPrepareMultipleGlobalResultSetsClose()
273: throws Exception {
274: statementHandler.prepareGlobalResultSets(new MockResultSet[] {
275: resultSet1, resultSet2, resultSet3 });
276: MockStatement statement = (MockStatement) connection
277: .createStatement();
278: statement.executeQuery("select test");
279: MockResultSet testResultSet1 = (MockResultSet) statement
280: .getResultSet();
281: statement.getMoreResults(Statement.CLOSE_CURRENT_RESULT);
282: MockResultSet testResultSet2 = (MockResultSet) statement
283: .getResultSet();
284: statement.getMoreResults(Statement.CLOSE_CURRENT_RESULT);
285: MockResultSet testResultSet3 = (MockResultSet) statement
286: .getResultSet();
287: statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
288: assertTrue(testResultSet1.isClosed());
289: assertTrue(testResultSet2.isClosed());
290: assertFalse(testResultSet3.isClosed());
291: }
292:
293: public void testPrepareGeneratedKeys() throws Exception {
294: statementHandler.prepareGeneratedKeys("insert into othertable",
295: resultSet2);
296: statementHandler.prepareGeneratedKeys("insert into table",
297: resultSet3);
298: MockStatement statement = (MockStatement) connection
299: .createStatement();
300: statement.executeUpdate("inser", new int[1]);
301: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
302: assertSame(statement, ((MockResultSet) statement
303: .getGeneratedKeys()).getStatement());
304: statement.execute("insert into", Statement.NO_GENERATED_KEYS);
305: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
306: statement.executeUpdate("do insert into table xyz",
307: new String[0]);
308: assertTrue(isResultSet3((MockResultSet) statement
309: .getGeneratedKeys()));
310: assertSame(statement, ((MockResultSet) statement
311: .getGeneratedKeys()).getStatement());
312: statementHandler.setUseRegularExpressions(true);
313: statement.executeUpdate("insert into table xyz", new String[0]);
314: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
315: statementHandler.prepareGlobalGeneratedKeys(resultSet1);
316: statement.execute("insert into table xyz",
317: Statement.RETURN_GENERATED_KEYS);
318: assertTrue(isResultSet1((MockResultSet) statement
319: .getGeneratedKeys()));
320: assertSame(statement, ((MockResultSet) statement
321: .getGeneratedKeys()).getStatement());
322: statement.execute("insert into table xyz");
323: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
324: statementHandler.setExactMatch(true);
325: statement.executeUpdate("insert into othertable",
326: Statement.RETURN_GENERATED_KEYS);
327: assertTrue(isResultSet2((MockResultSet) statement
328: .getGeneratedKeys()));
329: assertSame(statement, ((MockResultSet) statement
330: .getGeneratedKeys()).getStatement());
331: statement.executeUpdate("insert into othertable",
332: Statement.NO_GENERATED_KEYS);
333: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
334: statementHandler.clearGlobalGeneratedKeys();
335: statement.executeUpdate("abc", Statement.RETURN_GENERATED_KEYS);
336: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
337: statementHandler.clearGeneratedKeys();
338: statement.execute("insert into othertable",
339: Statement.RETURN_GENERATED_KEYS);
340: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
341: }
342:
343: public void testPrepareGeneratedKeysBatch() throws Exception {
344: statementHandler.prepareGeneratedKeys("insert into othertable",
345: resultSet2);
346: statementHandler.prepareGeneratedKeys("insert into table",
347: resultSet3);
348: MockStatement statement = (MockStatement) connection
349: .createStatement();
350: statement.executeUpdate("insert into othertable",
351: Statement.RETURN_GENERATED_KEYS);
352: assertTrue(isResultSet2((MockResultSet) statement
353: .getGeneratedKeys()));
354: statement.addBatch("insert into othertable");
355: statement.executeBatch();
356: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
357: }
358:
359: public void testPrepareUpdateCount() throws Exception {
360: statementHandler.prepareGlobalUpdateCount(2);
361: statementHandler.prepareUpdateCount("insert into", 3);
362: MockStatement statement = (MockStatement) connection
363: .createStatement();
364: int testUpdateCount = statement.executeUpdate("update xy");
365: assertEquals(2, testUpdateCount);
366: testUpdateCount = statement
367: .executeUpdate("insert into x(y) values(1)");
368: assertEquals(3, testUpdateCount);
369: statementHandler.setExactMatch(true);
370: testUpdateCount = statement
371: .executeUpdate("insert into x(y) values(1)");
372: assertEquals(2, testUpdateCount);
373: statementHandler.setExactMatch(false);
374: statementHandler.setCaseSensitive(true);
375: testUpdateCount = statement.executeUpdate("INSERT into xy");
376: assertEquals(2, testUpdateCount);
377: statementHandler.setCaseSensitive(false);
378: testUpdateCount = statement.executeUpdate("INSERT into xy");
379: assertEquals(3, testUpdateCount);
380: assertFalse(statement.execute("DELETE"));
381: assertEquals(2, statement.getUpdateCount());
382: assertNull(statement.getResultSet());
383: }
384:
385: public void testPrepareMultipleUpdateCounts() throws Exception {
386: statementHandler.prepareGlobalUpdateCount(2);
387: statementHandler.prepareUpdateCounts("insert into", new int[] {
388: 1, 2, 3 });
389: MockStatement statement = (MockStatement) connection
390: .createStatement();
391: int testUpdateCount = statement.executeUpdate("update xy");
392: assertEquals(2, testUpdateCount);
393: assertEquals(2, statement.getUpdateCount());
394: assertNull(statement.getResultSet());
395: assertFalse(statement.getMoreResults());
396: assertEquals(-1, statement.getUpdateCount());
397: assertNull(statement.getResultSet());
398: testUpdateCount = statement.executeUpdate("insert INTO");
399: assertEquals(1, testUpdateCount);
400: assertEquals(1, statement.getUpdateCount());
401: assertNull(statement.getResultSet());
402: assertFalse(statement.getMoreResults());
403: assertEquals(2, statement.getUpdateCount());
404: assertNull(statement.getResultSet());
405: assertFalse(statement.getMoreResults());
406: assertEquals(3, statement.getUpdateCount());
407: assertNull(statement.getResultSet());
408: assertFalse(statement.getMoreResults());
409: assertEquals(-1, statement.getUpdateCount());
410: assertNull(statement.getResultSet());
411: statement.execute("insert into");
412: assertEquals(1, statement.getUpdateCount());
413: assertFalse(statement.getMoreResults());
414: assertEquals(2, statement.getUpdateCount());
415: assertFalse(statement.getMoreResults());
416: assertEquals(3, statement.getUpdateCount());
417: assertFalse(statement.getMoreResults());
418: assertEquals(-1, statement.getUpdateCount());
419: }
420:
421: public void testPrepareUpdateCountBatch() throws Exception {
422: statementHandler.prepareGlobalUpdateCount(2);
423: statementHandler.prepareUpdateCount("insert into", 3);
424: MockStatement statement = (MockStatement) connection
425: .createStatement();
426: statement.addBatch("insert into x(y) values(1)");
427: statement.addBatch("DELETE xyz where");
428: statement.addBatch("update xy");
429: int[] updateCounts = statement.executeBatch();
430: assertTrue(updateCounts.length == 3);
431: assertEquals(3, updateCounts[0]);
432: assertEquals(2, updateCounts[1]);
433: assertEquals(2, updateCounts[2]);
434: statement.addBatch("select xy");
435: try {
436: statement.executeBatch();
437: fail();
438: } catch (BatchUpdateException exc) {
439: assertEquals(3, exc.getUpdateCounts().length);
440: assertEquals(3, exc.getUpdateCounts()[0]);
441: assertEquals(2, exc.getUpdateCounts()[1]);
442: assertEquals(2, exc.getUpdateCounts()[2]);
443: }
444: statement.clearBatch();
445: updateCounts = statement.executeBatch();
446: assertTrue(updateCounts.length == 0);
447: }
448:
449: public void testPrepareUpdateCountBatchFailureWithoutContinue()
450: throws Exception {
451: statementHandler.prepareGlobalUpdateCount(2);
452: statementHandler.prepareUpdateCount("insert into", 3);
453: MockStatement statement = (MockStatement) connection
454: .createStatement();
455: statement.addBatch("insert into x(y) values(1)");
456: statement.addBatch("DELETE xyz where");
457: statement.addBatch("update xy");
458: statementHandler.prepareThrowsSQLException("DELETE",
459: new SQLException("reason", "state", 25));
460: try {
461: statement.executeBatch();
462: fail();
463: } catch (BatchUpdateException exc) {
464: assertEquals(1, exc.getUpdateCounts().length);
465: assertEquals(3, exc.getUpdateCounts()[0]);
466: assertEquals(1, statementHandler.getExecutedStatements()
467: .size());
468: assertEquals("insert into x(y) values(1)", statementHandler
469: .getExecutedStatements().get(0));
470: assertEquals("reason", exc.getMessage());
471: assertEquals("state", exc.getSQLState());
472: assertEquals(25, exc.getErrorCode());
473: }
474: statementHandler.prepareThrowsSQLException("insert into",
475: new BatchUpdateException(new int[9]));
476: try {
477: statement.executeBatch();
478: fail();
479: } catch (BatchUpdateException exc) {
480: assertEquals(9, exc.getUpdateCounts().length);
481: }
482: }
483:
484: public void testPrepareUpdateCountBatchFailureWithContinue()
485: throws Exception {
486: statementHandler.prepareGlobalUpdateCount(2);
487: statementHandler.prepareUpdateCount("insert into", 3);
488: MockStatement statement = (MockStatement) connection
489: .createStatement();
490: statement.addBatch("insert into x(y) values(1)");
491: statement.addBatch("DELETE xyz where");
492: statement.addBatch("update xy");
493: statementHandler.prepareThrowsSQLException("DELETE");
494: statementHandler.setContinueProcessingOnBatchFailure(true);
495: try {
496: statement.executeBatch();
497: fail();
498: } catch (BatchUpdateException exc) {
499: assertEquals(3, exc.getUpdateCounts().length);
500: assertEquals(3, exc.getUpdateCounts()[0]);
501: assertEquals(-3, exc.getUpdateCounts()[1]);
502: assertEquals(2, exc.getUpdateCounts()[2]);
503: assertEquals(2, statementHandler.getExecutedStatements()
504: .size());
505: assertEquals("insert into x(y) values(1)", statementHandler
506: .getExecutedStatements().get(0));
507: assertEquals("update xy", statementHandler
508: .getExecutedStatements().get(1));
509: }
510: statementHandler.prepareThrowsSQLException("update xy",
511: new SQLException("reason", "state", 25));
512: try {
513: statement.executeBatch();
514: fail();
515: } catch (BatchUpdateException exc) {
516: assertEquals(3, exc.getUpdateCounts().length);
517: assertEquals(3, exc.getUpdateCounts()[0]);
518: assertEquals(-3, exc.getUpdateCounts()[1]);
519: assertEquals(-3, exc.getUpdateCounts()[2]);
520: assertEquals(3, statementHandler.getExecutedStatements()
521: .size());
522: assertEquals("reason", exc.getMessage());
523: assertEquals("state", exc.getSQLState());
524: assertEquals(25, exc.getErrorCode());
525: }
526: statement.addBatch("select");
527: try {
528: statement.executeBatch();
529: fail();
530: } catch (BatchUpdateException exc) {
531: assertEquals(4, exc.getUpdateCounts().length);
532: assertEquals(3, exc.getUpdateCounts()[0]);
533: assertEquals(-3, exc.getUpdateCounts()[1]);
534: assertEquals(-3, exc.getUpdateCounts()[2]);
535: assertEquals(-3, exc.getUpdateCounts()[3]);
536: assertEquals(4, statementHandler.getExecutedStatements()
537: .size());
538: assertEquals(
539: "SQL select in the list of batches returned a ResultSet.",
540: exc.getMessage());
541: }
542: }
543:
544: public void testPrepareMultipleGlobalUpdateCounts()
545: throws Exception {
546: statementHandler.prepareGlobalUpdateCounts(new int[] { 5, 4 });
547: statementHandler.prepareUpdateCount("insert into", 7);
548: MockStatement statement = (MockStatement) connection
549: .createStatement();
550: int testUpdateCount = statement.executeUpdate("insert into");
551: assertEquals(7, testUpdateCount);
552: assertEquals(7, statement.getUpdateCount());
553: assertNull(statement.getResultSet());
554: assertFalse(statement.getMoreResults());
555: assertEquals(-1, statement.getUpdateCount());
556: assertNull(statement.getResultSet());
557: testUpdateCount = statement.executeUpdate("XYZ");
558: assertEquals(5, testUpdateCount);
559: assertEquals(5, statement.getUpdateCount());
560: assertNull(statement.getResultSet());
561: assertFalse(statement.getMoreResults());
562: assertEquals(4, statement.getUpdateCount());
563: assertNull(statement.getResultSet());
564: assertFalse(statement.getMoreResults());
565: assertEquals(-1, statement.getUpdateCount());
566: assertNull(statement.getResultSet());
567: }
568:
569: public void testGetMoreResultsSingleResultSetAndUpdateCount()
570: throws Exception {
571: statementHandler.prepareGlobalResultSet(resultSet1);
572: statementHandler.prepareGlobalUpdateCount(2);
573: MockStatement statement = (MockStatement) connection
574: .createStatement();
575: assertFalse(statement.getMoreResults());
576: assertNull(statement.getResultSet());
577: assertEquals(-1, statement.getUpdateCount());
578: statement.execute("select");
579: MockResultSet currentResult = (MockResultSet) statement
580: .getResultSet();
581: assertEquals(resultSet1.getId(), currentResult.getId());
582: assertEquals(resultSet1.getId(), currentResult.getId());
583: assertEquals(-1, statement.getUpdateCount());
584: assertFalse(statement.getMoreResults());
585: assertTrue(currentResult.isClosed());
586: assertNull(statement.getResultSet());
587: assertFalse(statement.getMoreResults());
588: assertEquals(-1, statement.getUpdateCount());
589: statementHandler.prepareResultSet("select", resultSet2);
590: statement.executeQuery("select");
591: assertNotNull(statement.getResultSet());
592: statement.executeUpdate("insert");
593: assertNull(statement.getResultSet());
594: assertEquals(2, statement.getUpdateCount());
595: assertEquals(2, statement.getUpdateCount());
596: assertFalse(statement.getMoreResults());
597: assertEquals(-1, statement.getUpdateCount());
598: }
599:
600: public void testPrepareThrowsSQLException() throws Exception {
601: SQLException exception = new SQLWarning();
602: statementHandler.prepareThrowsSQLException("insert into test",
603: exception);
604: statementHandler.prepareThrowsSQLException("select from");
605: MockStatement statement = (MockStatement) connection
606: .createStatement();
607: statementHandler.prepareGlobalGeneratedKeys(resultSet1);
608: statement.executeUpdate("update xy",
609: Statement.RETURN_GENERATED_KEYS);
610: assertTrue(isResultSet1((MockResultSet) statement
611: .getGeneratedKeys()));
612: statementHandler.prepareGlobalGeneratedKeys(resultSet2);
613: try {
614: statement.executeUpdate("insert into test values",
615: Statement.RETURN_GENERATED_KEYS);
616: fail();
617: } catch (SQLException exc) {
618: assertTrue(isResultSet1((MockResultSet) statement
619: .getGeneratedKeys()));
620: assertSame(exception, exc);
621: }
622: statementHandler.setExactMatch(true);
623: statement.executeUpdate("insert into test values");
624: statementHandler.setExactMatch(false);
625: statementHandler.setCaseSensitive(true);
626: statement.executeUpdate("iNsert into test values");
627: try {
628: statement.executeQuery("select from");
629: fail();
630: } catch (SQLException exc) {
631: assertNotSame(exception, exc);
632: assertTrue(exc.getMessage().indexOf("select from") != -1);
633: }
634: }
635:
636: public void testResultSetType() throws Exception {
637: MockStatement statement1 = (MockStatement) connection
638: .createStatement();
639: statement1.setFetchDirection(ResultSet.FETCH_REVERSE);
640: statement1.setFetchSize(3);
641: MockPreparedStatement statement2 = (MockPreparedStatement) connection
642: .prepareStatement("select2",
643: ResultSet.TYPE_SCROLL_INSENSITIVE,
644: ResultSet.CONCUR_UPDATABLE);
645: MockCallableStatement statement3 = (MockCallableStatement) connection
646: .prepareCall("select3",
647: ResultSet.TYPE_SCROLL_SENSITIVE,
648: ResultSet.CONCUR_UPDATABLE,
649: ResultSet.CLOSE_CURSORS_AT_COMMIT);
650: MockResultSet resultSet = new MockResultSet("id1");
651: resultSet.setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
652: resultSet
653: .setResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
654: connection.getStatementResultSetHandler().prepareResultSet(
655: "select1", resultSet);
656: connection.getPreparedStatementResultSetHandler()
657: .prepareResultSet("select2", resultSet);
658: connection.getCallableStatementResultSetHandler()
659: .prepareResultSet("select3", resultSet);
660: MockResultSet returnedResultSet1 = (MockResultSet) statement1
661: .executeQuery("select1");
662: MockResultSet returnedResultSet2 = (MockResultSet) statement2
663: .executeQuery("select2");
664: MockResultSet returnedResultSet3 = (MockResultSet) statement3
665: .executeQuery("select3");
666: assertEquals(ResultSet.TYPE_FORWARD_ONLY, returnedResultSet1
667: .getType());
668: assertEquals(ResultSet.CONCUR_READ_ONLY, returnedResultSet1
669: .getConcurrency());
670: assertEquals(ResultSet.FETCH_REVERSE, returnedResultSet1
671: .getFetchDirection());
672: assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT,
673: returnedResultSet1.getHoldability());
674: assertEquals(3, returnedResultSet1.getFetchSize());
675: assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE,
676: returnedResultSet2.getType());
677: assertEquals(ResultSet.CONCUR_UPDATABLE, returnedResultSet2
678: .getConcurrency());
679: assertEquals(ResultSet.FETCH_FORWARD, returnedResultSet2
680: .getFetchDirection());
681: assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT,
682: returnedResultSet2.getHoldability());
683: assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE,
684: returnedResultSet3.getType());
685: assertEquals(ResultSet.CONCUR_UPDATABLE, returnedResultSet3
686: .getConcurrency());
687: assertEquals(ResultSet.FETCH_FORWARD, returnedResultSet3
688: .getFetchDirection());
689: assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT,
690: returnedResultSet3.getHoldability());
691: }
692: }
|