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