1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 24 import java.io.InputStream ; 25 import java.io.Reader ; 26 import java.sql.Connection ; 27 import java.sql.Statement ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 31 import java.io.IOException ; 32 import java.sql.SQLException ; 33 34 import org.apache.derby.tools.ij; 35 36 37 public class Stream { 38 39 public static void main(String [] args){ 40 41 Connection conn = null; 42 43 try{ 44 ij.getPropertyArg(args); 45 conn = ij.startJBMS(); 46 47 createTestTables(conn); 48 executeTests(conn); 49 dropTestTables(conn); 50 51 }catch(Throwable t){ 52 t.printStackTrace(); 53 54 }finally{ 55 if(conn != null){ 56 try{ 57 conn.close(); 58 59 }catch(SQLException e){ 60 e.printStackTrace(); 61 } 62 63 } 64 } 65 66 } 67 68 69 private static void createTestTables(Connection conn) 70 throws SQLException ,IOException { 71 72 createTable(conn); 73 createTestData(conn); 74 75 } 76 77 78 private static void createTable(Connection conn) throws SQLException { 79 80 Statement st = null; 81 82 try{ 83 84 st = conn.createStatement(); 85 st.execute("create table SMALL_BLOB_TABLE( SMALL_BLOB blob( 512 ))"); 86 st.execute("create table LARGE_BLOB_TABLE( LARGE_BLOB blob( 512k ))"); 87 st.execute("create table SMALL_CLOB_TABLE( SMALL_CLOB clob( 512 ))"); 88 st.execute("create table LARGE_CLOB_TABLE( LARGE_CLOB clob( 512k ))"); 89 90 }finally{ 91 if(st != null) 92 st.close(); 93 } 94 95 } 96 97 98 private static void createTestData(Connection conn) 99 throws SQLException ,IOException { 100 101 createSmallBlobTestData( conn ); 102 createLargeBlobTestData( conn ); 103 createSmallClobTestData( conn ); 104 createLargeClobTestData( conn ); 105 106 } 107 108 109 private static void createSmallBlobTestData(Connection conn) 110 throws SQLException ,IOException { 111 112 PreparedStatement st = null; 113 TestDataStream stream = null; 114 115 try{ 116 st = conn.prepareStatement("insert into SMALL_BLOB_TABLE(SMALL_BLOB) values(?)"); 117 stream = new TestDataStream(512); 118 st.setBinaryStream(1, stream, 512); 119 st.executeUpdate(); 120 121 }finally{ 122 if(st != null){ 123 st.close(); 124 } 125 126 if(stream != null){ 127 stream.close(); 128 } 129 130 } 131 132 } 133 134 135 private static void createLargeBlobTestData(Connection conn) 136 throws SQLException ,IOException { 137 138 PreparedStatement st = null; 139 TestDataStream stream = null; 140 141 try{ 142 st = conn.prepareStatement("insert into LARGE_BLOB_TABLE(LARGE_BLOB) values(?)"); 143 stream = new TestDataStream( 512 * 1024); 144 st.setBinaryStream(1,stream, 512 * 1024); 145 146 st.executeUpdate(); 147 148 }finally{ 149 if(st != null){ 150 st.close(); 151 } 152 153 if(stream != null){ 154 stream.close(); 155 } 156 } 157 } 158 159 160 private static void createSmallClobTestData(Connection conn) 161 throws SQLException ,IOException { 162 163 PreparedStatement st = null; 164 TestDataReader reader = null; 165 166 try{ 167 st = conn.prepareStatement("insert into SMALL_CLOB_TABLE( SMALL_CLOB ) values(?)"); 168 169 reader = new TestDataReader( 512 ); 170 st.setCharacterStream(1, 171 reader, 172 512); 173 174 st.executeUpdate(); 175 176 177 }finally{ 178 if(st != null) 179 st.close(); 180 181 if(reader != null) 182 reader.close(); 183 184 } 185 186 } 187 188 189 private static void createLargeClobTestData(Connection conn) 190 throws SQLException , IOException { 191 192 PreparedStatement st = null; 193 TestDataReader reader = null; 194 195 try{ 196 st = conn.prepareStatement("insert into LARGE_CLOB_TABLE( LARGE_CLOB ) values(?)"); 197 198 reader = new TestDataReader( 512 * 1024 ); 199 st.setCharacterStream(1, 200 reader, 201 512 * 1024 ); 202 203 st.executeUpdate(); 204 205 206 } finally { 207 if(st != null) 208 st.close(); 209 210 if(reader != null) 211 reader.close(); 212 } 213 } 214 215 private static void executeTests(Connection conn) 216 throws SQLException , IOException { 217 218 executeTestOnSmallBlob( conn ); 219 executeTestOnLargeBlob( conn ); 220 executeTestOnSmallClob( conn ); 221 executeTestOnLargeClob( conn ); 222 223 } 224 225 226 private static void executeTestOnSmallBlob( Connection conn ) 227 throws SQLException , IOException { 228 229 BlobTester tester = new BlobTester( "SMALL_BLOB_TABLE", 230 "SMALL_BLOB" ); 231 tester.testGetStreamTwice( conn ); 232 233 } 234 235 236 private static void executeTestOnLargeBlob( Connection conn ) 237 throws SQLException , IOException { 238 239 BlobTester tester = new BlobTester( "LARGE_BLOB_TABLE", 240 "LARGE_BLOB" ); 241 tester.testGetStreamTwice( conn ); 242 243 } 244 245 246 private static void executeTestOnSmallClob( Connection conn ) 247 throws SQLException , IOException { 248 249 ClobTester tester = new ClobTester( "SMALL_CLOB_TABLE", 250 "SMALL_CLOB" ); 251 tester.testGetReaderTwice( conn ); 252 253 } 254 255 256 private static void executeTestOnLargeClob( Connection conn ) 257 throws SQLException , IOException { 258 259 ClobTester tester = new ClobTester( "LARGE_CLOB_TABLE", 260 "LARGE_CLOB" ); 261 tester.testGetReaderTwice( conn ); 262 263 } 264 265 266 private static void dropTestTables( Connection conn ) throws SQLException { 267 268 Statement st = null; 269 270 try{ 271 st = conn.createStatement(); 272 st.execute("drop table SMALL_BLOB_TABLE"); 273 st.execute("drop table LARGE_BLOB_TABLE"); 274 st.execute("drop table SMALL_CLOB_TABLE"); 275 st.execute("drop table LARGE_CLOB_TABLE"); 276 277 }finally{ 278 if(st != null) 279 st.close(); 280 } 281 282 } 283 284 285 static class TestDataStream extends InputStream { 286 287 private long streamedLength = 0; 288 private final long total; 289 290 291 public TestDataStream(long length){ 292 total = length; 293 } 294 295 296 public int read(){ 297 298 if(streamedLength >= total){ 299 return -1; 300 } 301 302 return (int) ((streamedLength ++) % 256L); 303 304 } 305 306 307 public void close(){ 308 streamedLength = total; 309 } 310 311 } 312 313 314 static class TestDataReader extends Reader { 315 316 private long wroteLength = 0; 317 private final long total; 318 319 320 public TestDataReader(long length){ 321 total = length; 322 } 323 324 325 public void close(){ 326 wroteLength = total; 327 } 328 329 330 public int read( char[] cbuf, 331 int off, 332 int len ){ 333 334 if(wroteLength >= total) 335 return -1; 336 337 int i; 338 for(i = off ; 339 i < off + len && 340 wroteLength < total ; 341 i++, wroteLength ++){ 342 343 cbuf[i] = (char) (wroteLength % 0x10000L); 344 345 } 346 347 return i - off; 348 } 349 350 } 351 352 353 static class BlobTester { 354 355 final String tableName; 356 final String colName; 357 358 359 BlobTester(String tableName, 360 String colName){ 361 362 this.tableName = tableName; 363 this.colName = colName; 364 365 } 366 367 368 public void testGetStreamTwice(Connection conn) 369 throws SQLException , IOException { 370 371 Statement st = null; 372 ResultSet rs = null; 373 InputStream is = null; 374 375 try{ 376 st = conn.createStatement(); 377 378 rs = st.executeQuery("select " + 379 colName + " "+ 380 "from " + 381 tableName); 382 rs.next(); 383 384 System.out.println("get stream from " + tableName + "." + colName + " ..."); 385 is = rs.getBinaryStream(1); 386 is.close(); 387 388 System.out.println("get stream from " + tableName + "." + colName + " again ..."); 389 is = rs.getBinaryStream(1); 390 391 System.out.println("Expected exception did not happen."); 392 393 }catch(SQLException e){ acknowledgeException( e ); } 394 finally{ 395 if( st != null ) 396 st.close(); 397 398 if( rs != null ) 399 rs.close(); 400 401 if( is != null ) 402 is.close(); 403 404 } 405 } 406 } 407 408 private static void acknowledgeException( Throwable e ) 409 { 410 String message = e.getMessage(); 411 412 if ( message.startsWith( "java.sql." ) ) 415 { 416 int boilerplateEndIndex = message.indexOf( ": " ); 417 message = message.substring( boilerplateEndIndex + 2, message.length() ); 418 } 419 420 System.out.println("Expected exception may happen."); 421 System.out.println( e.getMessage() ); 422 } 423 424 static class ClobTester { 425 426 final String tableName; 427 final String colName; 428 429 public ClobTester( String tableName , 430 String colName ){ 431 432 this.tableName = tableName; 433 this.colName = colName; 434 435 } 436 437 438 public void testGetReaderTwice( Connection conn ) 439 throws SQLException , IOException { 440 441 Statement st = null; 442 ResultSet rs = null; 443 Reader reader = null; 444 445 try{ 446 st = conn.createStatement(); 447 448 rs = st.executeQuery( "select " + 449 colName + " " + 450 "from " + 451 tableName ); 452 rs.next(); 453 454 System.out.println("get reader from " + tableName + "." + colName + " ..."); 455 reader = rs.getCharacterStream(1); 456 reader.close(); 457 458 System.out.println("get reader from " + tableName + "." + colName + "again ..."); 459 reader = rs.getCharacterStream(1); 460 461 System.out.println("Expected exception did not happen."); 462 463 }catch(SQLException e){ acknowledgeException( e ); } 464 finally{ 465 if(st != null) 466 st.close(); 467 468 if(rs != null) 469 rs.close(); 470 471 if(reader != null) 472 reader.close(); 473 474 } 475 } 476 } 477 } 478 | Popular Tags |