KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbc4 > CallableStatementTest


1 /*
2  
3    Derby - Class CallableStatementTest
4  
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11  
12       http://www.apache.org/licenses/LICENSE-2.0
13  
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19  
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.jdbc4;
23
24 import junit.framework.*;
25
26 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
27
28 import java.io.IOException JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.sql.*;
31 import java.lang.reflect.Method JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 /**
35  * Tests of the <code>java.sql.CallableStatement</code> JDBC40 API.
36  */

37 public class CallableStatementTest
38     extends BaseJDBCTestCase {
39
40
41     /** Default callable statement used by the tests. */
42     private CallableStatement cStmt = null;
43     
44     /**
45      * Create a test with the given name.
46      *
47      * @param name name of the test.
48      */

49     public CallableStatementTest(String JavaDoc name) {
50         super(name);
51     }
52
53     /**
54      * Create a default callable statement and connection.
55      *
56      * @throws SQLException if creation of connection or callable statement
57      * fail.
58      */

59     protected void setUp()
60         throws SQLException {
61         cStmt = prepareCall("? = CALL FLOOR(?)");
62         cStmt.registerOutParameter(1, Types.DOUBLE);
63     }
64
65     /**
66      * Close default callable statement and connection.
67      *
68      * @throws SQLException if closing of the connection or the callable
69      * statement fail.
70      */

71     protected void tearDown()
72         throws Exception JavaDoc {
73         if (cStmt != null && !cStmt.isClosed()) {
74             cStmt.close();
75         }
76
77         super.tearDown();
78     }
79    
80     public void testNamedParametersAreNotSupported()
81         throws SQLException {
82         DatabaseMetaData met = getConnection().getMetaData();
83         assertFalse("Named parameters are not supported, but the metadata " +
84                     "says they are", met.supportsNamedParameters());
85         met = null;
86     }
87     
88     public void testGetDoubleIntOnInParameter()
89         throws SQLException {
90         cStmt.setDouble(2, 3.3);
91         cStmt.execute();
92         try {
93             cStmt.getDouble(2);
94             fail("Calling getDouble on an IN parameter should throw " +
95                  "an exception");
96         } catch (SQLException sqle) {
97             // SQLState differ between DerbyNetClient and embedded.
98
String JavaDoc sqlState = usingDerbyNetClient() ? "XJ091" : "XCL26";
99             assertSQLState("Unexpected SQLState", sqlState, sqle);
100         }
101     }
102     
103     public void testGetNClobIntNotImplemented()
104         throws SQLException {
105         try {
106             cStmt.getNClob(1);
107             fail("CallableStatement.getNClob(int) should not be implemented");
108         } catch (SQLFeatureNotSupportedException sfnse) {
109             // We are fine, do nothing.
110
}
111     }
112     
113     public void testGetNClobStringNotImplemented()
114         throws SQLException {
115         try {
116             cStmt.getNClob("some-parameter-name");
117             fail("CallableStatement.getNClob(String) " +
118                  "should not be implemented");
119         } catch (SQLFeatureNotSupportedException sfnse) {
120             // We are fine, do nothing.
121
}
122     }
123
124     public void testGetNStringIntNotImplemented()
125         throws SQLException {
126         try {
127             cStmt.getNString(1);
128             fail("CallableStatement.getNString(int) " +
129                  "should not be implemented");
130         } catch (SQLFeatureNotSupportedException sfnse) {
131             // We are fine, do nothing.
132
}
133     }
134
135     public void testGetNStringStringNotImplemented()
136         throws SQLException {
137         try {
138             cStmt.getNString("some-parameter-name");
139             fail("CallableStatement.getNString(String) " +
140                  "should not be implemented");
141         } catch (SQLFeatureNotSupportedException sfnse) {
142             // We are fine, do nothing.
143
}
144     }
145
146     
147     public void testGetCharacterStreamIntOnInvalidTypeDOUBLE()
148         throws SQLException {
149         cStmt.setDouble(2, 3.3);
150         cStmt.execute();
151         try {
152             cStmt.getCharacterStream(1);
153             fail("An exception signalling invalid data type conversion " +
154                  "should have been thrown");
155         } catch (SQLDataException sqlde) {
156             assertSQLState("Exception with invalid SQL state thrown on " +
157                     "invalid data type conversion", "22005", sqlde);
158         }
159     }
160
161     /**
162      * Test which SQLState is thrown when getCharacterStream is called
163      * on an IN parameter of an unsupported type.
164      */

165     public void testGetCharacterStreamIntOnInParameterOfInvalidType()
166         throws SQLException {
167         cStmt.setDouble(2, 3.3);
168         cStmt.execute();
169         try {
170             cStmt.getCharacterStream(2);
171             fail("Calling getCharacterStream on an IN parameter should " +
172                  "throw an exception");
173         } catch (SQLException sqle) {
174             // SQLState differ between DerbyNetClient and embedded.
175
String JavaDoc sqlState = usingDerbyNetClient() ? "XJ091" : "XCL26";
176             assertSQLState("Exception with invalid SQL state thrown for " +
177                            "getCharacterStream on IN parameter",
178                            sqlState, sqle);
179         }
180     }
181     
182     /**
183      * Test which SQLState is thrown when getCharacterStream is called
184      * on an IN parameter of a supported type.
185      */

186     public void testGetCharacterStreamIntOnInParameterOfValidType()
187         throws SQLException {
188         cStmt = CallableStatementTestSetup.getBinaryDirectProcedure(getConnection());
189         cStmt.setString(1, "A string");
190         cStmt.execute();
191         try {
192             cStmt.getCharacterStream(1);
193             fail("Calling getCharacterStream on an IN parameter should " +
194                  "throw an exception");
195         } catch (SQLException sqle) {
196             // SQLState differ between DerbyNetClient and embedded.
197
String JavaDoc sqlState = usingDerbyNetClient() ? "XJ091" : "XCL26";
198             assertSQLState("Exception with invalid SQL state thrown for " +
199                            "getCharacterStream on IN parameter",
200                            sqlState, sqle);
201         }
202     }
203     
204     /**
205      * Test basic use of getCharacterStream on character data.
206      * Create a CallableStatement that takes an integer as input and returns
207      * the number as a string. The string is read as a stream, and the integer
208      * is recreated from it and compared to the integer passed in.
209      */

210     public void testGetCharacterStreamIntVARCHAR()
211         throws IOException JavaDoc, SQLException {
212         cStmt = CallableStatementTestSetup.getIntToStringFunction(getConnection());
213         cStmt.setInt(2, 4509);
214         assertFalse("No resultsets should be returned", cStmt.execute());
215         assertEquals("Incorrect updatecount", -1, cStmt.getUpdateCount());
216         // Get a character stream
217
Reader JavaDoc cStream = cStmt.getCharacterStream(1);
218         assertFalse("Stream should not be null", cStmt.wasNull());
219         assertNotNull("Stream is null even though wasNull() returned false",
220                 cStream);
221         char[] chars = new char[4];
222         assertEquals("Wrong number of characters read",
223                 4, cStream.read(chars));
224         // Make sure we have reached end of stream.
225
assertEquals("Expected end of stream, but there were more data",
226                 -1, cStream.read());
227         cStream.close();
228         String JavaDoc result = new String JavaDoc(chars);
229         assertEquals("Incorrect result obtained through java.io.Reader",
230                 "4509", result);
231     }
232     
233     /**
234      * Test basic use of getCharacterStream on binary data.
235      * Create a CallableStatement that takes a string as input and returns
236      * a byte representation, which is then read through a stream. The string
237      * is recreated and compared to the one passed in. Note that strings must
238      * be represented in UTF-16BE for this to work.
239      */

240     public void testGetCharacterStreamIntVARBINARYDirect()
241         throws IOException JavaDoc, SQLException {
242         String JavaDoc data = "This is the test string.";
243         cStmt = CallableStatementTestSetup.getBinaryDirectProcedure(getConnection());
244         cStmt.setString(1, data);
245         assertFalse("No resultsets should be returned", cStmt.execute());
246         // Note that getUpdateCount behaves differently on client and embedded.
247
assertEquals("Incorrect updatecount",
248                      usingEmbedded() ? 0 : -1,
249                      cStmt.getUpdateCount());
250         Reader JavaDoc cStream = cStmt.getCharacterStream(2);
251         assertFalse("Stream should not be null", cStmt.wasNull());
252         assertNotNull("Stream is null even though wasNull() returned false",
253                 cStream);
254         // Assume we don't know how many bytes the string will be represented
255
// by, just create enough space and read until stream is exhausted.
256
// To be able to read the string back, getBytes must be called with
257
// UTF-16BE charset, because Derby uses UTF-16BE encoding as default.
258
// JDBC does not specify which charset to use for binary data, and
259
// UTF-16BE was apparently selected to match JCC.
260
char[] tmpChars = new char[data.length() * 4];
261         int curChar = cStream.read();
262         int index = 0;
263         while (curChar != -1) {
264             tmpChars[index] = (char)curChar;
265             index++;
266             curChar = cStream.read();
267         }
268         cStream.close();
269         char[] chars = new char[index];
270         System.arraycopy(tmpChars, 0, chars, 0, index);
271         String JavaDoc result = new String JavaDoc(chars);
272         assertEquals("Incorrect result obtained through java.io.Reader",
273                 data, result);
274     }
275
276     /**
277      * Fetch a string stored as bytes from the database through a reader,
278      * then recreate the string.
279      */

280     public void testGetCharacterStreamIntVARBINARYFromDb()
281         throws IOException JavaDoc, SQLException {
282         cStmt = CallableStatementTestSetup.getBinaryFromDbFunction(getConnection());
283         cStmt.setInt(2, CallableStatementTestSetup.STRING_BYTES_ID);
284         assertFalse("No resultsets should be returned", cStmt.execute());
285         assertEquals("Incorrect updatecount", -1, cStmt.getUpdateCount());
286         Reader JavaDoc cStream = cStmt.getCharacterStream(1);
287         assertFalse("Stream should not be null", cStmt.wasNull());
288         assertNotNull("Stream is null even though wasNull() returned false",
289                 cStream);
290         char[] tmpChars = new char[32672];
291         int curChar = cStream.read();
292         int index = 0;
293         while (curChar != -1) {
294             tmpChars[index] = (char)curChar;
295             index++;
296             curChar = cStream.read();
297         }
298         char[] chars = new char[index];
299         System.arraycopy(tmpChars, 0, chars, 0, index);
300         tmpChars = null;
301         cStream.close();
302         String JavaDoc result = new String JavaDoc(chars);
303         assertEquals("Strings not equal",
304                      CallableStatementTestSetup.STRING_BYTES,
305                      result);
306     }
307
308     /**
309      * Read a SQL NULL value from a VARBINARY column through a reader.
310      */

311     public void testGetCharacterStreamIntOnVARBINARYWithNull()
312         throws SQLException {
313         cStmt = CallableStatementTestSetup.getBinaryFromDbFunction(getConnection());
314         cStmt.setInt(2, CallableStatementTestSetup.SQL_NULL_ID);
315         assertFalse("No resultsets should be returned", cStmt.execute());
316         assertEquals("Incorrect updatecount", -1, cStmt.getUpdateCount());
317         Reader JavaDoc cStream = cStmt.getCharacterStream(1);
318         assertTrue("Stream should be null", cStmt.wasNull());
319         assertNull("Stream is not null even though wasNull() returned true",
320                 cStream);
321     }
322     
323     /**
324      * Read a SQL NULL value from a VARCHAR column through a reader.
325      */

326     public void testGetCharacterStreamIntOnVARCHARWithNull()
327         throws SQLException {
328         cStmt = CallableStatementTestSetup.getVarcharFromDbFunction(getConnection());
329         cStmt.setInt(2, CallableStatementTestSetup.SQL_NULL_ID);
330         assertFalse("No resultsets should be returned", cStmt.execute());
331         assertEquals("Incorrect updatecount", -1, cStmt.getUpdateCount());
332         Reader JavaDoc cStream = cStmt.getCharacterStream(1);
333         assertTrue("Stream should be null", cStmt.wasNull());
334         assertNull("Stream is not null even though wasNull() returned true",
335                 cStream);
336     }
337     
338     public void testGetCharacterStreamStringNotImplemented()
339         throws SQLException {
340         try {
341             cStmt.getCharacterStream("some-parameter-name");
342             fail("CallableStatement.getCharacterStream(String) " +
343                  "should not be implemented");
344         } catch (SQLFeatureNotSupportedException sfnse) {
345             // We are fine, do nothing.
346
}
347     }
348
349     public void testGetNCharacterStreamIntNotImplemented()
350         throws SQLException {
351         try {
352             cStmt.getNCharacterStream(1);
353             fail("CallableStatement.getNCharacterStream(int) " +
354                  "should not be implemented");
355         } catch (SQLFeatureNotSupportedException sfnse) {
356             // We are fine, do nothing.
357
}
358     }
359     
360     public void testGetNCharacterStreamStringNotImplemented()
361         throws SQLException {
362         try {
363             cStmt.getNCharacterStream("some-parameter-name");
364             fail("CallableStatement.getNCharacterStream(String) " +
365                  "should not be implemented");
366         } catch (SQLFeatureNotSupportedException sfnse) {
367             // We are fine, do nothing.
368
}
369     }
370
371     public void testSetBlobNotImplemented()
372         throws SQLException {
373         try {
374             cStmt.setBlob("some-parameter-name", (Blob)null);
375             fail("CallableStatement.setBlob(String, Blob) " +
376                  "should not be implemented");
377         } catch (SQLFeatureNotSupportedException sfnse) {
378             // We are fine, do nothing.
379
}
380     }
381     
382     public void testSetClobNotImplemented()
383         throws SQLException {
384         try {
385             cStmt.setClob("some-parameter-name", (Clob)null);
386             fail("CallableStatement.setClob(String, Clob) " +
387                  "should not be implemented");
388         } catch (SQLFeatureNotSupportedException sfnse) {
389             // We are fine, do nothing.
390
}
391     }
392
393     public void testSetNCharacterStreamNotImplemented()
394         throws SQLException {
395         try {
396             cStmt.setNCharacterStream("some-parameter-name", null, 0l);
397             fail("CallableStatement.setNCharacterStream(String,Reader,long) " +
398                  "should not be implemented");
399         } catch (SQLFeatureNotSupportedException sfnse) {
400             // We are fine, do nothing.
401
}
402     }
403
404     public void testSetNClobNClobNotImplemented()
405         throws SQLException {
406         try {
407             cStmt.setNClob("some-parameter-name", (NClob)null);
408             fail("CallableStatement.setNClob(String, NClob) " +
409                  "should not be implemented");
410         } catch (SQLFeatureNotSupportedException sfnse) {
411             // We are fine, do nothing.
412
}
413     }
414
415     public void testSetNClobReaderNotImplemented()
416         throws SQLException {
417         try {
418             cStmt.setNClob("some-parameter-name", null, 0l);
419             fail("CallableStatement.setNClob(String, Reader, long) " +
420                  "should not be implemented");
421         } catch (SQLFeatureNotSupportedException sfnse) {
422             // We are fine, do nothing.
423
}
424     }
425
426     public void testSetNStringNotImplemented()
427         throws SQLException {
428         try {
429             cStmt.setNString("some-parameter-name", "some-value");
430             fail("CallableStatement.setNString(String, String) " +
431                  "should not be implemented");
432         } catch (SQLFeatureNotSupportedException sfnse) {
433             // We are fine, do nothing.
434
}
435     }
436    
437     public void testGetSQLXMLIntNotImplemented()
438         throws SQLException {
439         try {
440             cStmt.getSQLXML(1);
441             fail("CallableStatement.getSQLXML(int) " +
442                  "should not be implemented");
443         } catch (SQLFeatureNotSupportedException sfnse) {
444             // We are fine, do nothing.
445
}
446     }
447     
448     public void testGetSQLXMLStringNotImplemented()
449         throws SQLException {
450         try {
451             cStmt.getSQLXML("some-parameter-name");
452             fail("CallableStatement.getSQLXML(String) " +
453                  "should not be implemented");
454         } catch (SQLFeatureNotSupportedException sfnse) {
455             // We are fine, do nothing.
456
}
457     }
458
459     public void testSetSQLXMLNotImplemented()
460         throws SQLException {
461         try {
462             cStmt.setSQLXML("some-parameter-name", null);
463             fail("CallableStatement.setSQLXML(String, SQLXML) " +
464                  "should not be implemented");
465         } catch (SQLFeatureNotSupportedException sfnse) {
466             // We are fine, do nothing.
467
}
468     }
469
470     /** Helper method for testIsWrapperFor*Statement test cases. */
471     private void testIsWrapperForXXXStatement(Class JavaDoc klass) throws SQLException {
472         assertTrue("The CallableStatement is not a wrapper for "
473                        + klass.getName(),
474                    cStmt.isWrapperFor(klass));
475     }
476
477     public void testIsWrapperForStatement() throws SQLException {
478         testIsWrapperForXXXStatement(Statement.class);
479     }
480
481     public void testIsWrapperForPreparedStatement() throws SQLException {
482         testIsWrapperForXXXStatement(PreparedStatement.class);
483     }
484
485     public void testIsWrapperForCallableStatement() throws SQLException {
486         testIsWrapperForXXXStatement(CallableStatement.class);
487     }
488
489     public void testIsNotWrapperForResultSet() throws SQLException {
490         assertFalse(cStmt.isWrapperFor(ResultSet.class));
491     }
492
493     public void testUnwrapStatement() throws SQLException {
494         Statement stmt = cStmt.unwrap(Statement.class);
495         assertSame("Unwrap returned wrong object.", cStmt, stmt);
496     }
497
498     public void testUnwrapPreparedStatement() throws SQLException {
499         PreparedStatement ps = cStmt.unwrap(PreparedStatement.class);
500         assertSame("Unwrap returned wrong object.", cStmt, ps);
501     }
502
503     public void testUnwrapCallableStatement() throws SQLException {
504         Statement cs = cStmt.unwrap(CallableStatement.class);
505         assertSame("Unwrap returned wrong object.", cStmt, cs);
506     }
507
508     public void testUnwrapResultSet() {
509         try {
510             ResultSet rs = cStmt.unwrap(ResultSet.class);
511             fail("Unwrap didn't fail.");
512         } catch (SQLException e) {
513             assertSQLState("XJ128", e);
514         }
515     }
516
517     /**
518      *
519      * Tests the setCharacterStream method that accepts length as a long
520      * parameter in the Callable Statement interface
521      *
522      * @throws SQLException Upon any error that occurs while calling this
523      * method
524      *
525      */

526
527     public void testSetCharacterStream() throws SQLException {
528         try {
529             cStmt.setCharacterStream("Some String",null,0L);
530             fail("CallableStatement.setCharacterStream() " +
531                  "should not be implemented");
532         }
533         catch(SQLFeatureNotSupportedException sqlfne) {
534             //Do nothing as this is the expected behaviour
535

536         }
537     }
538
539     /**
540      *
541      * Tests the setAsciiStream method that accepts length as a long
542      * parameter in the Callable Statement interface
543      *
544      * @throws SQLException Upon any error that occurs while calling this
545      * method
546      *
547      */

548
549     public void testSetAsciiStream() throws SQLException {
550         try {
551             cStmt.setAsciiStream("Some String",null,0L);
552             fail("CallableStatement.setAsciiStream() " +
553                  "should not be implemented");
554         }
555         catch(SQLFeatureNotSupportedException sqlfne) {
556             //Do nothing as this is the expected behaviour
557

558         }
559     }
560
561     /**
562      *
563      * Tests the setBinaryStream method that accepts length as a long
564      * parameter in the Callable Statement interface
565      *
566      * @throws SQLException Upon any error that occurs while calling this
567      * method
568      *
569      */

570
571     public void testSetBinaryStream() throws SQLException {
572         try {
573             cStmt.setBinaryStream("Some String",null,0L);
574             fail("CallableStatement.setBinaryStream() " +
575                  "should not be implemented");
576         }
577         catch(SQLFeatureNotSupportedException sqlfne) {
578             //Do nothing as this is the expected behaviour
579

580         }
581     }
582
583     /**
584      * Return suite with all tests of the class.
585      */

586     public static Test suite() {
587         TestSuite mainSuite = new TestSuite();
588         TestSuite suite = new TestSuite(CallableStatementTest.class,
589                                         "CallableStatementTest suite");
590         mainSuite.addTest(new CallableStatementTestSetup(suite));
591         mainSuite.addTest(SetObjectUnsupportedTest.suite(true));
592         return mainSuite;
593     }
594     
595 } // End class CallableStatementTest
596
Popular Tags