1 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 ; 29 import java.io.Reader ; 30 import java.sql.*; 31 import java.lang.reflect.Method ; 32 import java.util.Vector ; 33 34 37 public class CallableStatementTest 38 extends BaseJDBCTestCase { 39 40 41 42 private CallableStatement cStmt = null; 43 44 49 public CallableStatementTest(String name) { 50 super(name); 51 } 52 53 59 protected void setUp() 60 throws SQLException { 61 cStmt = prepareCall("? = CALL FLOOR(?)"); 62 cStmt.registerOutParameter(1, Types.DOUBLE); 63 } 64 65 71 protected void tearDown() 72 throws Exception { 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 String 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 } 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 } 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 } 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 } 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 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 String 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 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 String 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 210 public void testGetCharacterStreamIntVARCHAR() 211 throws IOException , 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 Reader 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 assertEquals("Expected end of stream, but there were more data", 226 -1, cStream.read()); 227 cStream.close(); 228 String result = new String (chars); 229 assertEquals("Incorrect result obtained through java.io.Reader", 230 "4509", result); 231 } 232 233 240 public void testGetCharacterStreamIntVARBINARYDirect() 241 throws IOException , SQLException { 242 String 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 assertEquals("Incorrect updatecount", 248 usingEmbedded() ? 0 : -1, 249 cStmt.getUpdateCount()); 250 Reader 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 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 result = new String (chars); 272 assertEquals("Incorrect result obtained through java.io.Reader", 273 data, result); 274 } 275 276 280 public void testGetCharacterStreamIntVARBINARYFromDb() 281 throws IOException , 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 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 result = new String (chars); 303 assertEquals("Strings not equal", 304 CallableStatementTestSetup.STRING_BYTES, 305 result); 306 } 307 308 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 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 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 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 468 } 469 470 471 private void testIsWrapperForXXXStatement(Class 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 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 536 } 537 } 538 539 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 558 } 559 } 560 561 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 580 } 581 } 582 583 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 } | Popular Tags |