001: package com.mockrunner.test.jdbc;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.InputStream;
005: import java.io.Reader;
006: import java.io.StringReader;
007: import java.sql.BatchUpdateException;
008: import java.sql.SQLException;
009: import java.sql.SQLWarning;
010: import java.sql.Statement;
011: import java.util.ArrayList;
012: import java.util.HashMap;
013: import java.util.List;
014: import java.util.Map;
015:
016: import com.mockrunner.base.BaseTestCase;
017: import com.mockrunner.jdbc.PreparedStatementResultSetHandler;
018: import com.mockrunner.mock.jdbc.MockBlob;
019: import com.mockrunner.mock.jdbc.MockClob;
020: import com.mockrunner.mock.jdbc.MockConnection;
021: import com.mockrunner.mock.jdbc.MockNClob;
022: import com.mockrunner.mock.jdbc.MockPreparedStatement;
023: import com.mockrunner.mock.jdbc.MockResultSet;
024: import com.mockrunner.mock.jdbc.MockSQLXML;
025:
026: public class MockPreparedStatementTest extends BaseTestCase {
027: private PreparedStatementResultSetHandler preparedStatementHandler;
028: private MockConnection connection;
029: private MockResultSet resultSet1;
030: private MockResultSet resultSet2;
031: private MockResultSet resultSet3;
032:
033: protected void setUp() throws Exception {
034: super .setUp();
035: resultSet1 = new MockResultSet("");
036: resultSet1.addRow(new String[] { "a", "b", "c" });
037: resultSet2 = new MockResultSet("");
038: resultSet2.addRow(new String[] { "column11", "column21" });
039: resultSet2.addRow(new String[] { "column12", "column22" });
040: resultSet3 = new MockResultSet("");
041: resultSet3.addRow(new String[] { "test1", "test2" });
042: resultSet3.addRow(new String[] { "test3", "test4" });
043: resultSet3.addRow(new String[] { "test5", "test6" });
044: connection = getJDBCMockObjectFactory().getMockConnection();
045: preparedStatementHandler = connection
046: .getPreparedStatementResultSetHandler();
047: }
048:
049: protected void tearDown() throws Exception {
050: super .tearDown();
051: preparedStatementHandler = null;
052: connection = null;
053: resultSet1 = null;
054: resultSet2 = null;
055: resultSet3 = null;
056: }
057:
058: private boolean isEmpty(MockResultSet resultSet) {
059: return resultSet.getRowCount() == 0;
060: }
061:
062: private boolean isResultSet1(MockResultSet resultSet) {
063: return resultSet.getRowCount() == 1;
064: }
065:
066: private boolean isResultSet2(MockResultSet resultSet) {
067: return resultSet.getRowCount() == 2;
068: }
069:
070: private boolean isResultSet3(MockResultSet resultSet) {
071: return resultSet.getRowCount() == 3;
072: }
073:
074: public void testPrepareResultSet() throws Exception {
075: preparedStatementHandler.prepareGlobalResultSet(resultSet1);
076: preparedStatementHandler.prepareResultSet("select xyz",
077: resultSet2);
078: List params = new ArrayList();
079: params.add(new Integer(2));
080: params.add("Test");
081: preparedStatementHandler.prepareResultSet("select test",
082: resultSet3, params);
083: MockPreparedStatement statement = (MockPreparedStatement) connection
084: .prepareStatement("select test from x where value = ? and y = ?");
085: MockResultSet testResultSet = (MockResultSet) statement
086: .executeQuery();
087: assertTrue(isResultSet1(testResultSet));
088: statement.setInt(1, 2);
089: statement.setString(2, "Test");
090: testResultSet = (MockResultSet) statement.executeQuery();
091: assertTrue(isResultSet3(testResultSet));
092: statement.setBoolean(3, true);
093: testResultSet = (MockResultSet) statement.executeQuery();
094: assertTrue(isResultSet3(testResultSet));
095: preparedStatementHandler.setExactMatchParameter(true);
096: testResultSet = (MockResultSet) statement.executeQuery();
097: assertTrue(isResultSet1(testResultSet));
098: statement.clearParameters();
099: statement.setInt(1, 2);
100: statement.setNString(2, "Test");
101: testResultSet = (MockResultSet) statement.executeQuery();
102: assertTrue(isResultSet3(testResultSet));
103: statement.setString(3, "Test");
104: testResultSet = (MockResultSet) statement.executeQuery();
105: assertTrue(isResultSet1(testResultSet));
106: preparedStatementHandler.prepareResultSet("select test",
107: resultSet3, new Object[] { "xyz", new Long(1) });
108: statement.clearParameters();
109: statement.setString(1, "ab");
110: statement.setLong(2, 1);
111: testResultSet = (MockResultSet) statement.executeQuery();
112: assertTrue(isResultSet1(testResultSet));
113: statement.setString(1, "xyz");
114: testResultSet = (MockResultSet) statement.executeQuery();
115: assertTrue(isResultSet3(testResultSet));
116: statement.setNString(3, "xyz");
117: testResultSet = (MockResultSet) statement.executeQuery();
118: assertTrue(isResultSet1(testResultSet));
119: preparedStatementHandler.setExactMatchParameter(false);
120: statement.clearParameters();
121: statement.setString(1, "xyz");
122: statement.setLong(2, 1);
123: statement.setNString(3, "xyz");
124: statement.setString(4, "zzz");
125: testResultSet = (MockResultSet) statement.executeQuery();
126: assertTrue(isResultSet3(testResultSet));
127: statement = (MockPreparedStatement) connection
128: .prepareStatement("select xyzxyz");
129: statement.setLong(1, 2);
130: testResultSet = (MockResultSet) statement.executeQuery();
131: assertTrue(isResultSet2(testResultSet));
132: preparedStatementHandler.setExactMatch(true);
133: testResultSet = (MockResultSet) statement.executeQuery();
134: assertTrue(isResultSet1(testResultSet));
135: preparedStatementHandler.prepareResultSet("select xyzxyz",
136: resultSet3, new Object[] {});
137: testResultSet = (MockResultSet) statement.executeQuery();
138: assertTrue(isResultSet3(testResultSet));
139: preparedStatementHandler.setExactMatchParameter(true);
140: testResultSet = (MockResultSet) statement.executeQuery();
141: assertTrue(isResultSet1(testResultSet));
142: preparedStatementHandler.setExactMatchParameter(false);
143: preparedStatementHandler.setExactMatch(false);
144: assertTrue(statement.execute());
145: assertTrue(isResultSet3((MockResultSet) statement
146: .getResultSet()));
147: Map paramMap = new HashMap();
148: paramMap.put(new Integer(1), "Test");
149: paramMap.put(new Integer(2), new MockClob("Test"));
150: preparedStatementHandler.prepareResultSet("select xyzxyz",
151: resultSet3, paramMap);
152: statement.clearParameters();
153: statement.setString(1, "Test");
154: statement.setString(2, "Test");
155: statement.setClob(3, new MockClob("Test"));
156: testResultSet = (MockResultSet) statement.executeQuery();
157: assertTrue(isResultSet3(testResultSet));
158: preparedStatementHandler.setExactMatchParameter(true);
159: testResultSet = (MockResultSet) statement.executeQuery();
160: assertTrue(isResultSet2(testResultSet));
161: preparedStatementHandler.setExactMatch(true);
162: testResultSet = (MockResultSet) statement.executeQuery();
163: assertTrue(isResultSet1(testResultSet));
164: }
165:
166: public void testPrepareMultipleResultSets() throws Exception {
167: preparedStatementHandler.prepareResultSet("select xyz",
168: resultSet2);
169: preparedStatementHandler.prepareResultSets("select xyz",
170: new MockResultSet[] { resultSet1, resultSet2,
171: resultSet3 }, new Object[] { "1",
172: new Integer(2) });
173: MockPreparedStatement statement = (MockPreparedStatement) connection
174: .prepareStatement("select xyz from x where value = ? and y = ?");
175: statement.setString(1, "1");
176: MockResultSet testResultSet = (MockResultSet) statement
177: .executeQuery();
178: assertTrue(isResultSet2(testResultSet));
179: assertTrue(isResultSet2((MockResultSet) statement
180: .getResultSet()));
181: assertEquals(-1, statement.getUpdateCount());
182: assertFalse(statement.getMoreResults());
183: assertNull(statement.getResultSet());
184: assertEquals(-1, statement.getUpdateCount());
185: statement.setInt(2, 2);
186: statement.setInt(3, 2);
187: testResultSet = (MockResultSet) statement.executeQuery();
188: assertTrue(isResultSet1(testResultSet));
189: assertTrue(isResultSet1((MockResultSet) statement
190: .getResultSet()));
191: assertNotSame(resultSet1, statement.getResultSet());
192: assertEquals(-1, statement.getUpdateCount());
193: assertTrue(statement.getMoreResults());
194: assertTrue(isResultSet2((MockResultSet) statement
195: .getResultSet()));
196: assertNotSame(resultSet2, statement.getResultSet());
197: assertEquals(-1, statement.getUpdateCount());
198: assertTrue(statement.getMoreResults());
199: assertTrue(isResultSet3((MockResultSet) statement
200: .getResultSet()));
201: assertNotSame(resultSet3, statement.getResultSet());
202: assertEquals(-1, statement.getUpdateCount());
203: assertFalse(statement.getMoreResults());
204: assertNull(statement.getResultSet());
205: assertEquals(-1, statement.getUpdateCount());
206: preparedStatementHandler.setExactMatchParameter(true);
207: testResultSet = (MockResultSet) statement.executeQuery();
208: assertTrue(isResultSet2(testResultSet));
209: assertTrue(isResultSet2((MockResultSet) statement
210: .getResultSet()));
211: assertEquals(-1, statement.getUpdateCount());
212: assertFalse(statement.getMoreResults());
213: }
214:
215: public void testPrepareMultipleResultSetsClose() throws Exception {
216: Map parameters = new HashMap();
217: parameters.put(new Integer(1), new Long(1));
218: parameters.put(new Integer(2), new Long(2));
219: preparedStatementHandler.prepareResultSets("select xyz",
220: new MockResultSet[] { resultSet3, resultSet2,
221: resultSet1 }, parameters);
222: MockPreparedStatement statement = (MockPreparedStatement) connection
223: .prepareStatement("select xyz from x where value = ? and y = ?");
224: statement.setLong(1, 1);
225: statement.setLong(2, 2);
226: statement.setString(3, "3");
227: statement.execute();
228: MockResultSet testResultSet1 = (MockResultSet) statement
229: .getResultSet();
230: statement.getMoreResults();
231: MockResultSet testResultSet2 = (MockResultSet) statement
232: .getResultSet();
233: statement.getMoreResults();
234: MockResultSet testResultSet3 = (MockResultSet) statement
235: .getResultSet();
236: statement.getMoreResults();
237: assertTrue(testResultSet1.isClosed());
238: assertTrue(testResultSet2.isClosed());
239: assertTrue(testResultSet3.isClosed());
240: statement.executeQuery();
241: testResultSet1 = (MockResultSet) statement.getResultSet();
242: statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
243: testResultSet2 = (MockResultSet) statement.getResultSet();
244: statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
245: testResultSet3 = (MockResultSet) statement.getResultSet();
246: statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
247: assertFalse(testResultSet1.isClosed());
248: assertFalse(testResultSet2.isClosed());
249: assertFalse(testResultSet3.isClosed());
250: statement.execute();
251: testResultSet1 = (MockResultSet) statement.getResultSet();
252: statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
253: testResultSet2 = (MockResultSet) statement.getResultSet();
254: statement.getMoreResults(Statement.CLOSE_ALL_RESULTS);
255: testResultSet3 = (MockResultSet) statement.getResultSet();
256: statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
257: assertFalse(testResultSet1.isClosed());
258: assertTrue(testResultSet2.isClosed());
259: assertFalse(testResultSet3.isClosed());
260: }
261:
262: public void testCurrentResultSetCloseOnExecute() throws Exception {
263: Map parameters = new HashMap();
264: parameters.put(new Integer(1), new Long(1));
265: parameters.put(new Integer(2), new Long(2));
266: preparedStatementHandler.prepareResultSet("select xyz",
267: resultSet1);
268: MockPreparedStatement statement = (MockPreparedStatement) connection
269: .prepareStatement("select xyz from x where value = ? and y = ?");
270: statement.setLong(1, 1);
271: statement.setLong(2, 2);
272: MockResultSet testResultSet1 = (MockResultSet) statement
273: .executeQuery();
274: statement.setString(3, "3");
275: statement.executeUpdate();
276: assertTrue(testResultSet1.isClosed());
277: }
278:
279: public void testPrepareResultSetsStatementSet() throws Exception {
280: preparedStatementHandler.prepareResultSet("select xyz",
281: resultSet1);
282: preparedStatementHandler.prepareResultSets("select xyz",
283: new MockResultSet[] { resultSet3, resultSet2 },
284: new Object[] { "1" });
285: MockPreparedStatement statement = (MockPreparedStatement) connection
286: .prepareStatement("select xyz from x where value = ? and y = ?");
287: MockResultSet testResultSet1 = (MockResultSet) statement
288: .executeQuery();
289: statement.setString(1, "1");
290: statement.execute();
291: MockResultSet testResultSet2 = (MockResultSet) statement
292: .getResultSet();
293: statement.getMoreResults();
294: MockResultSet testResultSet3 = (MockResultSet) statement
295: .getResultSet();
296: assertSame(statement, testResultSet1.getStatement());
297: assertSame(statement, testResultSet2.getStatement());
298: assertSame(statement, testResultSet3.getStatement());
299: }
300:
301: public void testPrepareResultSetsNullValues() throws Exception {
302: List parameters = new ArrayList();
303: parameters.add("25");
304: preparedStatementHandler.prepareResultSets("select1",
305: new MockResultSet[] {}, parameters);
306: MockPreparedStatement statement = (MockPreparedStatement) connection
307: .prepareStatement("select1");
308: statement.setString(1, "25");
309: MockResultSet testResultSet = (MockResultSet) statement
310: .executeQuery();
311: assertNull(testResultSet);
312: assertNull(statement.getResultSet());
313: assertEquals(-1, statement.getUpdateCount());
314: assertFalse(statement.getMoreResults());
315: assertNull(statement.getResultSet());
316: preparedStatementHandler.prepareResultSet("select2", null,
317: parameters);
318: statement = (MockPreparedStatement) connection
319: .prepareStatement("select2");
320: statement.setString(1, "25");
321: testResultSet = (MockResultSet) statement.executeQuery();
322: assertNull(testResultSet);
323: assertNull(statement.getResultSet());
324: assertEquals(-1, statement.getUpdateCount());
325: assertFalse(statement.getMoreResults());
326: assertNull(statement.getResultSet());
327: }
328:
329: public void testPrepareResultSetNullParameter() throws Exception {
330: List params = new ArrayList();
331: params.add(new Integer(2));
332: params.add(null);
333: preparedStatementHandler.prepareResultSet("select test",
334: resultSet1, params);
335: MockPreparedStatement statement = (MockPreparedStatement) connection
336: .prepareStatement("select test from x where value = ? and y = ?");
337: MockResultSet testResultSet = (MockResultSet) statement
338: .executeQuery();
339: assertNull(testResultSet);
340: statement.setInt(1, 2);
341: testResultSet = (MockResultSet) statement.executeQuery();
342: assertNull(testResultSet);
343: statement.setString(2, null);
344: testResultSet = (MockResultSet) statement.executeQuery();
345: assertTrue(isResultSet1(testResultSet));
346: preparedStatementHandler.setExactMatchParameter(true);
347: testResultSet = (MockResultSet) statement.executeQuery();
348: assertTrue(isResultSet1(testResultSet));
349: statement.setString(3, null);
350: testResultSet = (MockResultSet) statement.executeQuery();
351: assertNull(testResultSet);
352: }
353:
354: public void testPrepareUpdateCount() throws Exception {
355: preparedStatementHandler.prepareGlobalUpdateCount(5);
356: preparedStatementHandler.prepareUpdateCount("delete xyz", 1);
357: List params = new ArrayList();
358: params.add(new Integer(1));
359: preparedStatementHandler.prepareUpdateCount("INSERT INTO", 3,
360: params);
361: preparedStatementHandler.prepareUpdateCount("INSERT INTO", 4,
362: new Object[] { "1", "2" });
363: MockPreparedStatement statement = (MockPreparedStatement) connection
364: .prepareStatement("insert into x(y) values(?)");
365: int testUpdateCount = statement.executeUpdate();
366: assertEquals(5, testUpdateCount);
367: statement.setInt(1, 1);
368: statement.setInt(2, 2);
369: testUpdateCount = statement.executeUpdate();
370: assertEquals(3, testUpdateCount);
371: preparedStatementHandler.setExactMatchParameter(true);
372: testUpdateCount = statement.executeUpdate();
373: assertEquals(5, testUpdateCount);
374: statement.clearParameters();
375: statement.setString(1, "1");
376: statement.setString(2, "2");
377: testUpdateCount = statement.executeUpdate();
378: assertEquals(4, testUpdateCount);
379: preparedStatementHandler.setCaseSensitive(true);
380: testUpdateCount = statement.executeUpdate();
381: assertEquals(5, testUpdateCount);
382: statement = (MockPreparedStatement) connection
383: .prepareStatement("delete xyz where ? = ?");
384: testUpdateCount = statement.executeUpdate();
385: assertEquals(1, testUpdateCount);
386: preparedStatementHandler.setExactMatch(true);
387: testUpdateCount = statement.executeUpdate();
388: assertEquals(5, testUpdateCount);
389: preparedStatementHandler.setExactMatch(false);
390: assertFalse(statement.execute());
391: assertEquals(1, statement.getUpdateCount());
392: assertNull(statement.getResultSet());
393: preparedStatementHandler.prepareReturnsResultSet("delete xyz",
394: true);
395: assertTrue(statement.execute());
396: assertEquals(-1, statement.getUpdateCount());
397: assertNull(statement.getResultSet());
398: }
399:
400: public void testPrepareMultipleUpdateCounts() throws Exception {
401: List parameter = new ArrayList();
402: parameter.add("1");
403: parameter.add(new Integer(2));
404: preparedStatementHandler.prepareUpdateCount("insert into", 5);
405: preparedStatementHandler.prepareUpdateCounts("insert into",
406: new int[] { 1, 2, 3 }, parameter);
407: MockPreparedStatement statement = (MockPreparedStatement) connection
408: .prepareStatement("insert into x(y) values(?)");
409: statement.setString(1, "1");
410: statement.execute();
411: assertEquals(5, statement.getUpdateCount());
412: assertNull(statement.getResultSet());
413: assertFalse(statement.getMoreResults());
414: assertEquals(-1, statement.getUpdateCount());
415: assertNull(statement.getResultSet());
416: statement.setInt(2, 2);
417: int updateCount = statement.executeUpdate();
418: assertEquals(1, updateCount);
419: assertEquals(1, statement.getUpdateCount());
420: assertNull(statement.getResultSet());
421: assertFalse(statement.getMoreResults());
422: assertEquals(2, statement.getUpdateCount());
423: assertNull(statement.getResultSet());
424: assertFalse(statement.getMoreResults());
425: assertEquals(3, statement.getUpdateCount());
426: assertNull(statement.getResultSet());
427: assertFalse(statement.getMoreResults());
428: assertEquals(-1, statement.getUpdateCount());
429: assertNull(statement.getResultSet());
430: assertFalse(statement.getMoreResults());
431: }
432:
433: public void testPrepareUpdateCountNullParameter() throws Exception {
434: preparedStatementHandler.prepareUpdateCount("INSERT INTO", 4,
435: new Object[] { null, "2" });
436: MockPreparedStatement statement = (MockPreparedStatement) connection
437: .prepareStatement("insert into x(y) values(?)");
438: int testUpdateCount = statement.executeUpdate();
439: assertEquals(0, testUpdateCount);
440: statement.setNull(1, 1);
441: testUpdateCount = statement.executeUpdate();
442: assertEquals(0, testUpdateCount);
443: statement.setString(2, "2");
444: testUpdateCount = statement.executeUpdate();
445: assertEquals(4, testUpdateCount);
446: statement.setNull(3, 1);
447: testUpdateCount = statement.executeUpdate();
448: assertEquals(4, testUpdateCount);
449: preparedStatementHandler.setExactMatchParameter(true);
450: testUpdateCount = statement.executeUpdate();
451: assertEquals(0, testUpdateCount);
452: }
453:
454: public void testPrepareUpdateCountBatch() throws Exception {
455: preparedStatementHandler.prepareGlobalUpdateCount(2);
456: preparedStatementHandler.prepareUpdateCount("insert into", 3);
457: preparedStatementHandler.prepareUpdateCount("insert into", 4,
458: new Object[] { "1", "2" });
459: MockPreparedStatement statement = (MockPreparedStatement) connection
460: .prepareStatement("insert into x(y) values(?)");
461: statement.setString(1, "1");
462: statement.setString(2, "2");
463: statement.addBatch();
464: statement.clearParameters();
465: statement.addBatch();
466: statement.setString(1, "1");
467: statement.setInt(2, 3);
468: statement.addBatch();
469: int[] updateCounts = statement.executeBatch();
470: assertTrue(updateCounts.length == 3);
471: assertEquals(4, updateCounts[0]);
472: assertEquals(3, updateCounts[1]);
473: assertEquals(3, updateCounts[2]);
474: preparedStatementHandler.prepareReturnsResultSet("insert into",
475: true);
476: try {
477: statement.executeBatch();
478: fail();
479: } catch (BatchUpdateException exc) {
480: assertEquals(0, exc.getUpdateCounts().length);
481: }
482: statement = (MockPreparedStatement) connection
483: .prepareStatement("update xyz");
484: statement.setString(1, "1");
485: statement.setString(2, "2");
486: statement.addBatch();
487: updateCounts = statement.executeBatch();
488: assertTrue(updateCounts.length == 1);
489: assertEquals(2, updateCounts[0]);
490: Map paramMap = new HashMap();
491: paramMap.put(new Integer(1), "1");
492: paramMap.put(new Integer(2), "2");
493: preparedStatementHandler.prepareUpdateCount("update", 7,
494: paramMap);
495: updateCounts = statement.executeBatch();
496: assertTrue(updateCounts.length == 1);
497: assertEquals(7, updateCounts[0]);
498: preparedStatementHandler.prepareThrowsSQLException("update",
499: paramMap);
500: try {
501: statement.executeBatch();
502: fail();
503: } catch (BatchUpdateException exc) {
504: assertEquals(0, exc.getUpdateCounts().length);
505: }
506: }
507:
508: public void testPrepareUpdateCountBatchFailureWithoutContinue()
509: throws Exception {
510: preparedStatementHandler.prepareGlobalUpdateCount(2);
511: preparedStatementHandler.prepareUpdateCount("insert into", 3);
512: preparedStatementHandler.prepareUpdateCount("insert into", 4,
513: new Object[] { "1", "2" });
514: preparedStatementHandler.setExactMatchParameter(true);
515: MockPreparedStatement statement = (MockPreparedStatement) connection
516: .prepareStatement("insert into x(y) values(?)");
517: statement.setString(1, "1");
518: statement.setString(2, "2");
519: statement.addBatch();
520: statement.clearParameters();
521: statement.addBatch();
522: statement.setString(1, "5");
523: statement.setInt(2, 3);
524: statement.addBatch();
525: Map paramMap = new HashMap();
526: paramMap.put(new Integer(1), "5");
527: paramMap.put(new Integer(2), new Integer(3));
528: preparedStatementHandler.prepareThrowsSQLException("insert",
529: new SQLException("reason", "code", 25), paramMap);
530: try {
531: statement.executeBatch();
532: fail();
533: } catch (BatchUpdateException exc) {
534: assertEquals(2, preparedStatementHandler
535: .getExecutedStatements().size());
536: assertEquals("insert into x(y) values(?)",
537: preparedStatementHandler.getExecutedStatements()
538: .get(0));
539: assertEquals("insert into x(y) values(?)",
540: preparedStatementHandler.getExecutedStatements()
541: .get(1));
542: assertEquals(2, exc.getUpdateCounts().length);
543: assertEquals(4, exc.getUpdateCounts()[0]);
544: assertEquals(3, exc.getUpdateCounts()[1]);
545: }
546: preparedStatementHandler.prepareThrowsSQLException(
547: "insert into", new BatchUpdateException(new int[9]),
548: new HashMap());
549: try {
550: statement.executeBatch();
551: fail();
552: } catch (BatchUpdateException exc) {
553: assertEquals(9, exc.getUpdateCounts().length);
554: }
555: preparedStatementHandler.prepareReturnsResultSet("insert into",
556: true);
557: try {
558: statement.executeBatch();
559: fail();
560: } catch (BatchUpdateException exc) {
561: assertEquals(0, exc.getUpdateCounts().length);
562: }
563: }
564:
565: public void testPrepareUpdateCountBatchFailureWithContinue()
566: throws Exception {
567: preparedStatementHandler.prepareGlobalUpdateCount(2);
568: preparedStatementHandler.prepareUpdateCount("insert into", 3);
569: preparedStatementHandler.prepareUpdateCount("insert into", 4,
570: new Object[] { "1", "2" });
571: preparedStatementHandler.setExactMatchParameter(true);
572: preparedStatementHandler
573: .setContinueProcessingOnBatchFailure(true);
574: MockPreparedStatement statement = (MockPreparedStatement) connection
575: .prepareStatement("insert into x(y) values(?)");
576: statement.setString(1, "1");
577: statement.setString(2, "2");
578: statement.addBatch();
579: statement.clearParameters();
580: statement.addBatch();
581: statement.setString(1, "5");
582: statement.setInt(2, 3);
583: statement.addBatch();
584: preparedStatementHandler.prepareThrowsSQLException("insert",
585: new BatchUpdateException(new int[9]), new HashMap());
586: try {
587: statement.executeBatch();
588: fail();
589: } catch (BatchUpdateException exc) {
590: assertEquals(2, preparedStatementHandler
591: .getExecutedStatements().size());
592: assertEquals("insert into x(y) values(?)",
593: preparedStatementHandler.getExecutedStatements()
594: .get(0));
595: assertEquals("insert into x(y) values(?)",
596: preparedStatementHandler.getExecutedStatements()
597: .get(1));
598: assertEquals(3, exc.getUpdateCounts().length);
599: assertEquals(4, exc.getUpdateCounts()[0]);
600: assertEquals(-3, exc.getUpdateCounts()[1]);
601: assertEquals(3, exc.getUpdateCounts()[2]);
602: }
603: preparedStatementHandler.prepareReturnsResultSet("insert into",
604: true);
605: try {
606: statement.executeBatch();
607: fail();
608: } catch (BatchUpdateException exc) {
609: assertEquals(3, exc.getUpdateCounts().length);
610: assertEquals(-3, exc.getUpdateCounts()[0]);
611: assertEquals(-3, exc.getUpdateCounts()[1]);
612: assertEquals(-3, exc.getUpdateCounts()[2]);
613: }
614: }
615:
616: public void testPrepareThrowsSQLException() throws Exception {
617: SQLException exception = new SQLWarning();
618: preparedStatementHandler
619: .prepareThrowsSQLException("insert into");
620: preparedStatementHandler.prepareUpdateCount("insert into", 3,
621: new ArrayList());
622: List params = new ArrayList();
623: params.add("test");
624: preparedStatementHandler.prepareThrowsSQLException("UPDATE",
625: exception, params);
626: preparedStatementHandler.prepareThrowsSQLException("UPDATE",
627: new Object[] { "1", "2" });
628: MockPreparedStatement statement = (MockPreparedStatement) connection
629: .prepareStatement("insert into x(y) values(?)");
630: try {
631: statement.execute();
632: fail();
633: } catch (SQLException exc) {
634: assertNotSame(exception, exc);
635: assertTrue(exc.getMessage().indexOf("insert into") != -1);
636: }
637: preparedStatementHandler.setExactMatch(true);
638: statement.execute();
639: statement = (MockPreparedStatement) connection
640: .prepareStatement("update");
641: statement.execute();
642: statement.setString(1, "test");
643: try {
644: statement.execute();
645: fail();
646: } catch (SQLException exc) {
647: assertSame(exception, exc);
648: }
649: preparedStatementHandler.setCaseSensitive(true);
650: statement.execute();
651: preparedStatementHandler.setCaseSensitive(false);
652: statement.setString(1, "1");
653: statement.setString(2, "2");
654: statement.setString(3, "3");
655: try {
656: statement.execute();
657: fail();
658: } catch (SQLException exc) {
659: assertNotSame(exception, exc);
660: assertTrue(exc.getMessage().indexOf("UPDATE") != -1);
661: }
662: preparedStatementHandler.setExactMatchParameter(true);
663: statement.execute();
664: }
665:
666: public void testPrepareGeneratedKeys() throws Exception {
667: List params = new ArrayList();
668: params.add("1");
669: params.add(new Long(2));
670: preparedStatementHandler.prepareGeneratedKeys("delete xyz",
671: resultSet1);
672: preparedStatementHandler.prepareGeneratedKeys("insert into",
673: resultSet2);
674: preparedStatementHandler.prepareGeneratedKeys("insert into",
675: resultSet3, params);
676: MockPreparedStatement statement = (MockPreparedStatement) connection
677: .prepareStatement("delete xyz",
678: Statement.RETURN_GENERATED_KEYS);
679: statement.executeUpdate("delete xyz");
680: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
681: statement.executeUpdate();
682: assertTrue(isResultSet1((MockResultSet) statement
683: .getGeneratedKeys()));
684: statement.executeQuery();
685: assertTrue(isResultSet1((MockResultSet) statement
686: .getGeneratedKeys()));
687: statement.execute();
688: assertTrue(isResultSet1((MockResultSet) statement
689: .getGeneratedKeys()));
690: statement = (MockPreparedStatement) connection
691: .prepareStatement("insert into xyz",
692: Statement.RETURN_GENERATED_KEYS);
693: statement.execute();
694: assertTrue(isResultSet2((MockResultSet) statement
695: .getGeneratedKeys()));
696: statement.setString(1, "1");
697: statement.executeQuery();
698: assertTrue(isResultSet2((MockResultSet) statement
699: .getGeneratedKeys()));
700: statement.setLong(2, 2);
701: statement.execute();
702: assertTrue(isResultSet3((MockResultSet) statement
703: .getGeneratedKeys()));
704: statement.executeUpdate("delete xyz");
705: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
706: statement.setLong(2, 1);
707: statement.executeUpdate();
708: assertTrue(isResultSet2((MockResultSet) statement
709: .getGeneratedKeys()));
710: statement.executeQuery("select");
711: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
712: preparedStatementHandler.setExactMatch(true);
713: statement.executeUpdate();
714: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
715: preparedStatementHandler.setExactMatch(false);
716: preparedStatementHandler.setExactMatchParameter(true);
717: statement.setLong(2, 2);
718: statement.execute();
719: assertTrue(isResultSet3((MockResultSet) statement
720: .getGeneratedKeys()));
721: statement.setString(3, "3");
722: statement.executeQuery();
723: assertTrue(isResultSet2((MockResultSet) statement
724: .getGeneratedKeys()));
725: statement = (MockPreparedStatement) connection
726: .prepareStatement("insert into xyz",
727: Statement.NO_GENERATED_KEYS);
728: statement.execute();
729: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
730: statement.execute("insert into xyz",
731: Statement.RETURN_GENERATED_KEYS);
732: assertTrue(isResultSet2((MockResultSet) statement
733: .getGeneratedKeys()));
734: statement.executeQuery();
735: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
736: }
737:
738: public void testPrepareGeneratedKeysBatch() throws Exception {
739: List params = new ArrayList();
740: params.add("1");
741: params.add(new Long(2));
742: preparedStatementHandler.prepareGeneratedKeys("insert into",
743: resultSet2, new Object[] { "2" });
744: preparedStatementHandler.prepareGeneratedKeys("insert into",
745: resultSet3, params);
746: MockPreparedStatement statement = (MockPreparedStatement) connection
747: .prepareStatement("insert into",
748: Statement.RETURN_GENERATED_KEYS);
749: statement.setString(1, "2");
750: statement.addBatch();
751: statement.executeBatch();
752: assertTrue(isResultSet2((MockResultSet) statement
753: .getGeneratedKeys()));
754: statement.setString(1, "3");
755: statement.addBatch();
756: statement.executeBatch();
757: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
758: statement.setString(1, "1");
759: statement.setLong(2, 2);
760: statement.setString(3, "1");
761: statement.addBatch();
762: statement.executeBatch();
763: assertTrue(isResultSet3((MockResultSet) statement
764: .getGeneratedKeys()));
765: statement = (MockPreparedStatement) connection
766: .prepareStatement("insert into",
767: Statement.NO_GENERATED_KEYS);
768: statement.setString(1, "2");
769: statement.addBatch();
770: statement.executeBatch();
771: assertTrue(isEmpty((MockResultSet) statement.getGeneratedKeys()));
772: }
773:
774: public void testClearResultSetsAndUpdateCounts() throws Exception {
775: preparedStatementHandler.prepareGlobalUpdateCount(5);
776: preparedStatementHandler.prepareUpdateCount("delete xyz", 1);
777: preparedStatementHandler.prepareGlobalResultSet(resultSet1);
778: preparedStatementHandler.prepareResultSet("select xyz",
779: resultSet2);
780: preparedStatementHandler.prepareResultSet("select test",
781: resultSet3);
782: MockPreparedStatement statement = (MockPreparedStatement) connection
783: .prepareStatement("select test");
784: MockResultSet resultSet = (MockResultSet) statement
785: .executeQuery();
786: assertTrue(isResultSet3(resultSet));
787: preparedStatementHandler.clearResultSets();
788: resultSet = (MockResultSet) statement.executeQuery();
789: assertTrue(isResultSet1(resultSet));
790: statement = (MockPreparedStatement) connection
791: .prepareStatement("delete xyz");
792: int updateCount = statement.executeUpdate();
793: assertEquals(1, updateCount);
794: preparedStatementHandler.clearUpdateCounts();
795: updateCount = statement.executeUpdate();
796: assertEquals(5, updateCount);
797: }
798:
799: public void testGetMoreResultsSingleResultSetAndUpdateCount()
800: throws Exception {
801: preparedStatementHandler.prepareResultSet("select", resultSet1,
802: new ArrayList());
803: preparedStatementHandler.prepareUpdateCount("insert", 3,
804: new ArrayList());
805: MockPreparedStatement preparedStatement = (MockPreparedStatement) connection
806: .prepareStatement("select");
807: assertFalse(preparedStatement.getMoreResults());
808: preparedStatement.execute();
809: MockResultSet currentResult = (MockResultSet) preparedStatement
810: .getResultSet();
811: assertNotNull(currentResult);
812: assertFalse(preparedStatement.getMoreResults());
813: assertTrue(currentResult.isClosed());
814: assertNull(preparedStatement.getResultSet());
815: assertFalse(preparedStatement.getMoreResults());
816: preparedStatement = (MockPreparedStatement) connection
817: .prepareStatement("insert");
818: assertEquals(-1, preparedStatement.getUpdateCount());
819: preparedStatement.executeUpdate();
820: assertEquals(3, preparedStatement.getUpdateCount());
821: assertEquals(3, preparedStatement.getUpdateCount());
822: assertFalse(preparedStatement.getMoreResults());
823: assertEquals(-1, preparedStatement.getUpdateCount());
824: preparedStatementHandler.prepareResultSet("selectother",
825: resultSet1);
826: preparedStatement.execute();
827: preparedStatement.execute("selectother");
828: assertEquals(-1, preparedStatement.getUpdateCount());
829: assertNotNull(preparedStatement.getResultSet());
830: assertFalse(preparedStatement.getMoreResults());
831: assertEquals(-1, preparedStatement.getUpdateCount());
832: assertNull(preparedStatement.getResultSet());
833: }
834:
835: public void testGetGeneratedKeysFailure() throws Exception {
836: MockPreparedStatement preparedStatement = (MockPreparedStatement) connection
837: .prepareStatement("insert");
838: try {
839: preparedStatement.execute("insert", 50000);
840: fail();
841: } catch (SQLException exc) {
842: //should throw exception
843: }
844: try {
845: preparedStatement.executeUpdate("insert", 50000);
846: fail();
847: } catch (SQLException exc) {
848: //should throw exception
849: }
850: preparedStatement.executeUpdate("insert",
851: Statement.RETURN_GENERATED_KEYS);
852: MockResultSet keys = (MockResultSet) preparedStatement
853: .getGeneratedKeys();
854: assertSame(preparedStatement, keys.getStatement());
855: preparedStatementHandler.prepareGlobalGeneratedKeys(resultSet2);
856: preparedStatement.executeUpdate("insert", new int[0]);
857: keys = (MockResultSet) preparedStatement.getGeneratedKeys();
858: assertTrue(isResultSet2(keys));
859: }
860:
861: public void testClearParameters() throws Exception {
862: MockPreparedStatement preparedStatement = (MockPreparedStatement) connection
863: .prepareStatement("insert");
864: preparedStatement.setBoolean(0, true);
865: preparedStatement.setString(1, "abc");
866: preparedStatement.clearParameters();
867: assertEquals(0, preparedStatement.getIndexedParameterMap()
868: .size());
869: assertEquals(0, preparedStatement.getParameterMap().size());
870: }
871:
872: public void testSetStreamParameters() throws Exception {
873: MockPreparedStatement preparedStatement = (MockPreparedStatement) connection
874: .prepareStatement("insert");
875: ByteArrayInputStream updateStream = new ByteArrayInputStream(
876: new byte[] { 1, 2, 3, 4, 5 });
877: preparedStatement.setAsciiStream(1, updateStream, (long) 2);
878: InputStream inputStream = (InputStream) preparedStatement
879: .getParameterMap().get(new Integer(1));
880: assertEquals(1, inputStream.read());
881: assertEquals(2, inputStream.read());
882: assertEquals(-1, inputStream.read());
883: updateStream = new ByteArrayInputStream(new byte[] { 1, 2, 3,
884: 4, 5 });
885: preparedStatement.setAsciiStream(1, updateStream);
886: inputStream = (InputStream) preparedStatement.getParameterMap()
887: .get(new Integer(1));
888: assertEquals(1, inputStream.read());
889: assertEquals(2, inputStream.read());
890: assertEquals(3, inputStream.read());
891: assertEquals(4, inputStream.read());
892: assertEquals(5, inputStream.read());
893: assertEquals(-1, inputStream.read());
894: updateStream = new ByteArrayInputStream(new byte[] { 1, 2, 3,
895: 4, 5 });
896: preparedStatement.setBinaryStream(2, updateStream, (long) 3);
897: inputStream = (InputStream) preparedStatement.getParameterMap()
898: .get(new Integer(2));
899: assertEquals(1, inputStream.read());
900: assertEquals(2, inputStream.read());
901: assertEquals(3, inputStream.read());
902: assertEquals(-1, inputStream.read());
903: StringReader updateReader = new StringReader("test");
904: preparedStatement.setCharacterStream(1, updateReader);
905: Reader inputReader = (Reader) preparedStatement
906: .getParameterMap().get(new Integer(1));
907: assertEquals('t', (char) inputReader.read());
908: assertEquals('e', (char) inputReader.read());
909: assertEquals('s', (char) inputReader.read());
910: assertEquals('t', (char) inputReader.read());
911: assertEquals(-1, inputReader.read());
912: updateReader = new StringReader("test");
913: preparedStatement.setCharacterStream(1, updateReader, 1);
914: inputReader = (Reader) preparedStatement.getParameterMap().get(
915: new Integer(1));
916: assertEquals('t', (char) inputReader.read());
917: assertEquals(-1, inputReader.read());
918: updateReader = new StringReader("test");
919: preparedStatement.setNCharacterStream(1, updateReader, 2);
920: inputReader = (Reader) preparedStatement.getParameterMap().get(
921: new Integer(1));
922: assertEquals('t', (char) inputReader.read());
923: assertEquals('e', (char) inputReader.read());
924: assertEquals(-1, inputReader.read());
925: }
926:
927: public void testSetBlobAndClobParameters() throws Exception {
928: MockPreparedStatement preparedStatement = (MockPreparedStatement) connection
929: .prepareStatement("insert");
930: preparedStatement.setBlob(1, new MockBlob(
931: new byte[] { 1, 2, 3 }));
932: assertEquals(new MockBlob(new byte[] { 1, 2, 3 }),
933: preparedStatement.getParameterMap().get(new Integer(1)));
934: preparedStatement.setBlob(1, new ByteArrayInputStream(
935: new byte[] { 1, 2, 3 }));
936: assertEquals(new MockBlob(new byte[] { 1, 2, 3 }),
937: preparedStatement.getParameterMap().get(new Integer(1)));
938: preparedStatement.setBlob(1, new ByteArrayInputStream(
939: new byte[] { 1, 2, 3, 4, 5 }), 3);
940: assertEquals(new MockBlob(new byte[] { 1, 2, 3 }),
941: preparedStatement.getParameterMap().get(new Integer(1)));
942: preparedStatement.setClob(2, new MockClob("test"));
943: assertEquals(new MockClob("test"), preparedStatement
944: .getParameterMap().get(new Integer(2)));
945: preparedStatement.setClob(2, new StringReader("test"));
946: assertEquals(new MockClob("test"), preparedStatement
947: .getParameterMap().get(new Integer(2)));
948: preparedStatement.setClob(2, new StringReader("testxyz"), 4);
949: assertEquals(new MockClob("test"), preparedStatement
950: .getParameterMap().get(new Integer(2)));
951: preparedStatement.setNClob(3, new MockNClob("test"));
952: assertEquals(new MockNClob("test"), preparedStatement
953: .getParameterMap().get(new Integer(3)));
954: preparedStatement.setNClob(3, new StringReader("test"));
955: assertEquals(new MockNClob("test"), preparedStatement
956: .getParameterMap().get(new Integer(3)));
957: preparedStatement.setNClob(3, new StringReader("testxyz"), 4);
958: assertEquals(new MockNClob("test"), preparedStatement
959: .getParameterMap().get(new Integer(3)));
960: }
961:
962: public void testSetSQLXMLParameter() throws Exception {
963: MockPreparedStatement preparedStatement = (MockPreparedStatement) connection
964: .prepareStatement("insert");
965: preparedStatement.setSQLXML(1, new MockSQLXML(
966: "<test>abc</test>"));
967: assertEquals(new MockSQLXML("<test>abc</test>"),
968: preparedStatement.getParameterMap().get(new Integer(1)));
969: }
970: }
|