KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > simple > CallableStatementTest


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23
24  */

25 package testsuite.simple;
26
27 import com.mysql.jdbc.SQLError;
28
29 import testsuite.BaseTestCase;
30
31 import java.sql.CallableStatement JavaDoc;
32 import java.sql.Connection JavaDoc;
33 import java.sql.ResultSet JavaDoc;
34 import java.sql.ResultSetMetaData JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Types JavaDoc;
37
38 import java.util.Properties JavaDoc;
39
40 /**
41  * Tests callable statement functionality.
42  *
43  * @author Mark Matthews
44  * @version $Id: CallableStatementTest.java,v 1.1.2.1 2005/05/13 18:58:37
45  * mmatthews Exp $
46  */

47 public class CallableStatementTest extends BaseTestCase {
48     /**
49      * DOCUMENT ME!
50      *
51      * @param name
52      */

53     public CallableStatementTest(String JavaDoc name) {
54         super(name);
55
56         // TODO Auto-generated constructor stub
57
}
58
59     /**
60      * Tests functioning of inout parameters
61      *
62      * @throws Exception
63      * if the test fails
64      */

65
66     public void testInOutParams() throws Exception JavaDoc {
67         if (versionMeetsMinimum(5, 0)) {
68             CallableStatement JavaDoc storedProc = null;
69
70             try {
71                 this.stmt
72                         .executeUpdate("DROP PROCEDURE IF EXISTS testInOutParam");
73                 this.stmt
74                         .executeUpdate("create procedure testInOutParam(INOUT foo VARCHAR(15))\n"
75                                 + "begin\n"
76                                 + "select concat(foo, foo) INTO foo;\n"
77                                 + "end\n");
78
79                 storedProc = this.conn.prepareCall("{call testInOutParam(?)}");
80
81                 storedProc.setString(1, "abcd");
82                 storedProc.registerOutParameter(1, Types.VARCHAR);
83
84                 storedProc.execute();
85                 String JavaDoc retrievedString = storedProc.getString(1);
86                 assertTrue(retrievedString.equals("abcdabcd"));
87             } finally {
88                 this.stmt.executeUpdate("DROP PROCEDURE testInOutParam");
89             }
90         }
91     }
92
93     /**
94      * Tests functioning of output parameters.
95      *
96      * @throws Exception
97      * if the test fails.
98      */

99     public void testOutParams() throws Exception JavaDoc {
100         if (versionMeetsMinimum(5, 0)) {
101             CallableStatement JavaDoc storedProc = null;
102
103             try {
104                 this.stmt
105                         .executeUpdate("DROP PROCEDURE IF EXISTS testOutParam");
106                 this.stmt
107                         .executeUpdate("CREATE PROCEDURE testOutParam(x int, out y int)\n"
108                                 + "begin\n"
109                                 + "declare z int;\n"
110                                 + "set z = x+1, y = z;\n" + "end\n");
111
112                 storedProc = this.conn.prepareCall("{call testOutParam(?, ?)}");
113
114                 storedProc.setInt(1, 5);
115                 storedProc.registerOutParameter(2, Types.INTEGER);
116
117                 storedProc.execute();
118
119                 System.out.println(storedProc);
120
121                 int indexedOutParamToTest = storedProc.getInt(2);
122                 int namedOutParamToTest = storedProc.getInt("y");
123
124                 assertTrue("Named and indexed parameter are not the same",
125                         indexedOutParamToTest == namedOutParamToTest);
126                 assertTrue("Output value not returned correctly",
127                         indexedOutParamToTest == 6);
128
129                 // Start over, using named parameters, this time
130
storedProc.clearParameters();
131                 storedProc.setInt("x", 32);
132                 storedProc.registerOutParameter("y", Types.INTEGER);
133
134                 storedProc.execute();
135
136                 indexedOutParamToTest = storedProc.getInt(2);
137                 namedOutParamToTest = storedProc.getInt("y");
138
139                 assertTrue("Named and indexed parameter are not the same",
140                         indexedOutParamToTest == namedOutParamToTest);
141                 assertTrue("Output value not returned correctly",
142                         indexedOutParamToTest == 33);
143
144                 try {
145                     storedProc.registerOutParameter("x", Types.INTEGER);
146                     assertTrue(
147                             "Should not be able to register an out parameter on a non-out parameter",
148                             true);
149                 } catch (SQLException JavaDoc sqlEx) {
150                     if (!SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
151                             .getSQLState())) {
152                         throw sqlEx;
153                     }
154                 }
155
156                 try {
157                     storedProc.registerOutParameter(1, Types.INTEGER);
158                     assertTrue(
159                             "Should not be able to register an out parameter on a non-out parameter",
160                             true);
161                 } catch (SQLException JavaDoc sqlEx) {
162                     if (!SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
163                             .getSQLState())) {
164                         throw sqlEx;
165                     }
166                 }
167
168                 try {
169                     storedProc.getInt("x");
170                     assertTrue(
171                             "Should not be able to retreive an out parameter on a non-out parameter",
172                             true);
173                 } catch (SQLException JavaDoc sqlEx) {
174                     if (!SQLError.SQL_STATE_COLUMN_NOT_FOUND.equals(sqlEx
175                             .getSQLState())) {
176                         throw sqlEx;
177                     }
178                 }
179             } finally {
180                 this.stmt.executeUpdate("DROP PROCEDURE testOutParam");
181             }
182         }
183     }
184
185     /**
186      * Tests functioning of output parameters.
187      *
188      * @throws Exception
189      * if the test fails.
190      */

191     public void testResultSet() throws Exception JavaDoc {
192         if (versionMeetsMinimum(5, 0)) {
193             CallableStatement JavaDoc storedProc = null;
194
195             try {
196                 this.stmt
197                         .executeUpdate("DROP TABLE IF EXISTS testSpResultTbl1");
198                 this.stmt
199                         .executeUpdate("DROP TABLE IF EXISTS testSpResultTbl2");
200                 this.stmt
201                         .executeUpdate("CREATE TABLE testSpResultTbl1 (field1 INT)");
202                 this.stmt
203                         .executeUpdate("INSERT INTO testSpResultTbl1 VALUES (1), (2)");
204                 this.stmt
205                         .executeUpdate("CREATE TABLE testSpResultTbl2 (field2 varchar(255))");
206                 this.stmt
207                         .executeUpdate("INSERT INTO testSpResultTbl2 VALUES ('abc'), ('def')");
208
209                 this.stmt
210                         .executeUpdate("DROP PROCEDURE IF EXISTS testSpResult");
211                 this.stmt
212                         .executeUpdate("CREATE PROCEDURE testSpResult()\n"
213                                 + "BEGIN\n"
214                                 + "SELECT field2 FROM testSpResultTbl2 WHERE field2='abc';\n"
215                                 + "UPDATE testSpResultTbl1 SET field1=2;\n"
216                                 + "SELECT field2 FROM testSpResultTbl2 WHERE field2='def';\n"
217                                 + "end\n");
218
219                 storedProc = this.conn.prepareCall("{call testSpResult()}");
220
221                 storedProc.execute();
222
223                 this.rs = storedProc.getResultSet();
224
225                 ResultSetMetaData JavaDoc rsmd = this.rs.getMetaData();
226
227                 assertTrue(rsmd.getColumnCount() == 1);
228                 assertTrue("field2".equals(rsmd.getColumnName(1)));
229                 assertTrue(rsmd.getColumnType(1) == Types.VARCHAR);
230
231                 assertTrue(this.rs.next());
232
233                 assertTrue("abc".equals(this.rs.getString(1)));
234
235                 // TODO: This does not yet work in MySQL 5.0
236
// assertTrue(!storedProc.getMoreResults());
237
// assertTrue(storedProc.getUpdateCount() == 2);
238
assertTrue(storedProc.getMoreResults());
239
240                 ResultSet JavaDoc nextResultSet = storedProc.getResultSet();
241
242                 rsmd = nextResultSet.getMetaData();
243
244                 assertTrue(rsmd.getColumnCount() == 1);
245                 assertTrue("field2".equals(rsmd.getColumnName(1)));
246                 assertTrue(rsmd.getColumnType(1) == Types.VARCHAR);
247
248                 assertTrue(nextResultSet.next());
249
250                 assertTrue("def".equals(nextResultSet.getString(1)));
251
252                 nextResultSet.close();
253
254                 this.rs.close();
255
256                 storedProc.execute();
257
258             } finally {
259                 this.stmt
260                         .executeUpdate("DROP PROCEDURE IF EXISTS testSpResult");
261                 this.stmt
262                         .executeUpdate("DROP TABLE IF EXISTS testSpResultTbl1");
263                 this.stmt
264                         .executeUpdate("DROP TABLE IF EXISTS testSpResultTbl2");
265             }
266         }
267     }
268
269     /**
270      * Tests parsing of stored procedures
271      *
272      * @throws Exception
273      * if an error occurs.
274      */

275     public void testSPParse() throws Exception JavaDoc {
276
277         if (versionMeetsMinimum(5, 0)) {
278
279             CallableStatement JavaDoc storedProc = null;
280
281             try {
282
283                 this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testSpParse");
284                 this.stmt
285                         .executeUpdate("CREATE PROCEDURE testSpParse(IN FOO VARCHAR(15))\n"
286                                 + "BEGIN\n" + "SELECT 1;\n" + "end\n");
287
288                 storedProc = this.conn.prepareCall("{call testSpParse()}");
289
290             } finally {
291                 this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testSpParse");
292             }
293         }
294     }
295
296     /**
297      * Tests parsing/execution of stored procedures with no parameters...
298      *
299      * @throws Exception
300      * if an error occurs.
301      */

302     public void testSPNoParams() throws Exception JavaDoc {
303
304         if (versionMeetsMinimum(5, 0)) {
305
306             CallableStatement JavaDoc storedProc = null;
307
308             try {
309
310                 this.stmt
311                         .executeUpdate("DROP PROCEDURE IF EXISTS testSPNoParams");
312                 this.stmt.executeUpdate("CREATE PROCEDURE testSPNoParams()\n"
313                         + "BEGIN\n" + "SELECT 1;\n" + "end\n");
314
315                 storedProc = this.conn.prepareCall("{call testSPNoParams()}");
316                 storedProc.execute();
317
318             } finally {
319                 this.stmt
320                         .executeUpdate("DROP PROCEDURE IF EXISTS testSPNoParams");
321             }
322         }
323     }
324
325     /**
326      * Tests parsing of stored procedures
327      *
328      * @throws Exception
329      * if an error occurs.
330      */

331     public void testSPCache() throws Exception JavaDoc {
332
333         if (versionMeetsMinimum(5, 0)) {
334
335             CallableStatement JavaDoc storedProc = null;
336
337             try {
338
339                 this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testSpParse");
340                 this.stmt
341                         .executeUpdate("CREATE PROCEDURE testSpParse(IN FOO VARCHAR(15))\n"
342                                 + "BEGIN\n" + "SELECT 1;\n" + "end\n");
343
344                 int numIterations = 10000;
345
346                 long startTime = System.currentTimeMillis();
347
348                 for (int i = 0; i < numIterations; i++) {
349                     storedProc = this.conn.prepareCall("{call testSpParse(?)}");
350                     storedProc.close();
351                 }
352
353                 long elapsedTime = System.currentTimeMillis() - startTime;
354
355                 System.out.println("Standard parsing/execution: " + elapsedTime
356                         + " ms");
357
358                 storedProc = this.conn.prepareCall("{call testSpParse(?)}");
359                 storedProc.setString(1, "abc");
360                 this.rs = storedProc.executeQuery();
361
362                 assertTrue(this.rs.next());
363                 assertTrue(this.rs.getInt(1) == 1);
364
365                 Properties JavaDoc props = new Properties JavaDoc();
366                 props.setProperty("cacheCallableStmts", "true");
367
368                 Connection JavaDoc cachedSpConn = getConnectionWithProps(props);
369
370                 startTime = System.currentTimeMillis();
371
372                 for (int i = 0; i < numIterations; i++) {
373                     storedProc = cachedSpConn
374                             .prepareCall("{call testSpParse(?)}");
375                     storedProc.close();
376                 }
377
378                 elapsedTime = System.currentTimeMillis() - startTime;
379
380                 System.out
381                         .println("Cached parse stage: " + elapsedTime + " ms");
382
383                 storedProc = cachedSpConn.prepareCall("{call testSpParse(?)}");
384                 storedProc.setString(1, "abc");
385                 this.rs = storedProc.executeQuery();
386
387                 assertTrue(this.rs.next());
388                 assertTrue(this.rs.getInt(1) == 1);
389
390             } finally {
391                 this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testSpParse");
392             }
393         }
394     }
395
396     /**
397      * Runs all test cases in this test suite
398      *
399      * @param args
400      */

401     public static void main(String JavaDoc[] args) {
402         junit.textui.TestRunner.run(CallableStatementTest.class);
403     }
404 }
405
Popular Tags