1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.ResultSetMetaData ; 27 import java.sql.ResultSet ; 28 import java.sql.Statement ; 29 import java.sql.SQLException ; 30 import java.sql.Types ; 31 import java.sql.PreparedStatement ; 32 33 import org.apache.derby.tools.ij; 34 import org.apache.derby.tools.JDBCDisplayUtil; 35 import org.apache.derbyTesting.functionTests.util.TestUtil; 36 37 import java.io.InputStream ; 38 import java.io.IOException ; 39 import java.io.File ; 40 import java.io.FileInputStream ; 41 import java.io.BufferedInputStream ; 42 import java.util.zip.CRC32 ; 43 import java.io.Reader ; 44 import java.io.StringReader ; 45 46 51 52 53 public class resultsetStream { 54 55 public static void main(String [] args) { 56 Connection con; 57 ResultSetMetaData met; 58 ResultSet rs; 59 Statement stmt; 60 61 System.out.println("Test resultsetStream starting"); 62 63 try 64 { 65 ij.getPropertyArg(args); 68 con = ij.startJBMS(); 69 70 stmt = con.createStatement(); 71 72 stmt.execute("create table t2 (len int, data LONG VARCHAR FOR BIT DATA)"); 73 PreparedStatement ppw = con.prepareStatement( 74 "insert into t2 (len, data) values (?, ?)"); 75 String filePath = "extin"; 76 String sep = System.getProperty("file.separator"); 77 boolean exists = (new File ("extin", "littleclob.utf")).exists(); 78 if (!exists) 79 { 80 String userDir = System.getProperty("user.dir"); 81 filePath = userDir + sep + ".." + sep + filePath; 82 } 83 File file = new File (filePath + sep + "littleclob.utf"); 84 int fileSize = (int) file.length(); 85 BufferedInputStream fileData = new BufferedInputStream (new FileInputStream (file)); 86 ppw.setInt(1, fileSize); 87 ppw.setBinaryStream(2, fileData, fileSize); 88 ppw.executeUpdate(); 89 90 file = new File (filePath + sep + "short.utf"); 91 fileSize = (int) file.length(); 92 fileData = new BufferedInputStream (new FileInputStream (file)); 93 ppw.setInt(1, fileSize); 94 ppw.setBinaryStream(2, fileData, fileSize); 95 ppw.executeUpdate(); 96 ppw.setInt(1, -1); 98 ppw.setBinaryStream(2, (java.io.InputStream ) null, 0); 99 ppw.executeUpdate(); 100 101 File rssg = new java.io.File (filePath + sep + "resultsetStream.gif"); 103 int rssgLength = (int) rssg.length(); 104 ppw.setInt(1, (int) rssgLength); 105 ppw.setBinaryStream(2, new FileInputStream (rssg), rssgLength); 106 ppw.executeUpdate(); 107 108 rs = stmt.executeQuery("select data from t2 where len = " 110 + rssgLength); 111 met = rs.getMetaData(); 112 System.out.println("getColumnCount(): "+ met.getColumnCount()); 113 while (rs.next()) 114 { 115 117 try { 119 120 InputStream is = rs.getBinaryStream(1); 121 if (is == null) { 122 System.out.println("FAIL - getBinaryStream() return null"); 123 break; 124 } 125 126 byte[] b200 = new byte[200]; 128 129 int count = 0; 131 132 while (count < 200) { 133 int r = is.read(b200, count, 200-count); 134 if (r == -1) 135 break; 136 count += r; 137 } 138 139 if (count != 200){ 140 System.out.println("FAIL - failed to read 200 bytes from known file"); 141 break; 142 } 143 144 CRC32 cs = new CRC32 (); 145 146 cs.reset(); 147 cs.update(b200); 148 149 System.out.println("Checksum of first 200 bytes " + cs.getValue()); 150 151 count = 200; 152 for (; is.read() != -1; count++) { 153 } 154 System.out.println("Size of file = " + count); 155 is.close(); 156 } 157 catch (Throwable e) { 158 System.out.println( 159 "FAIL - exection while processing valid file"); 160 if (e instanceof SQLException ) 161 JDBCDisplayUtil.ShowSQLException(System.out, (SQLException )e); 162 } 163 } 164 rs.close(); 165 166 rs = stmt.executeQuery("select data, len from t2 where len = " 168 + rssgLength); 169 met = rs.getMetaData(); 170 System.out.println("getColumnCount(): "+ met.getColumnCount()); 171 while (rs.next()) 172 { 173 175 try { 177 178 InputStream is = rs.getBinaryStream(1); 179 if (is == null) { 180 System.out.println("FAIL - getBinaryStream() return null"); 181 break; 182 } 183 184 byte[] b200 = new byte[200]; 186 187 int count = 0; 189 190 while (count < 200) { 191 int r = is.read(b200, count, 200-count); 192 if (r == -1) 193 break; 194 count += r; 195 } 196 197 if (count != 200){ 198 System.out.println("FAIL - failed to read 200 bytes from known file"); 199 break; 200 } 201 202 CRC32 cs = new CRC32 (); 203 204 cs.reset(); 205 cs.update(b200); 206 207 System.out.println("Checksum of first 200 bytes " + cs.getValue()); 208 209 System.out.println("second columns is " + rs.getInt(2)); 210 211 System.out.println("FAILS DUE TO BUG 5710"); 212 try { 213 is.read(); 214 System.out.println("FAIL - stream was not closed after a get*() call. " + is.getClass()); 215 break; 216 } catch (IOException ioe) { 217 } 219 } 220 catch (Throwable e) { 221 System.out.println( 222 "FAIL - exection while processing valid file"); 223 if (e instanceof SQLException ) 224 JDBCDisplayUtil.ShowSQLException(System.out, (SQLException )e); 225 } 226 } 227 rs.close(); 228 229 rs = stmt.executeQuery("select data from t2 where len = -1"); 231 met = rs.getMetaData(); 232 System.out.println("getColumnCount(): "+ met.getColumnCount()); 233 while (rs.next()) 234 { 235 237 239 InputStream is = rs.getBinaryStream(1); 240 if (is != null) { 241 System.out.println("FAIL - getBinaryStream() did not return null for SQL null"); 242 break; 243 } 244 245 } 246 rs.close(); 247 248 rs = stmt.executeQuery("select len, data from t2 where len = " 249 + fileSize); 250 rs.next(); 251 fileSize = rs.getInt(1); 252 fileData = new BufferedInputStream (rs.getBinaryStream(2)); 253 int readCount = 0; 254 while(true) 255 { 256 int data = fileData.read(); 257 if (data == -1) break; 258 readCount++; 259 } 260 fileData.close(); 261 System.out.println("len=" + fileSize); 262 System.out.println("number of reads=" + readCount); 263 264 267 byte[] tooFew = new byte[234]; 268 269 ppw.setInt(1, 234); 270 ppw.setBinaryStream(2, new java.io.ByteArrayInputStream (tooFew), 234); ppw.executeUpdate(); 272 273 274 ppw.setInt(1, 235); 275 ppw.setBinaryStream(2, new java.io.ByteArrayInputStream (tooFew), 235); try { 277 ppw.executeUpdate(); 278 System.out.println("FAIL - execute with setBinaryStream() with too few bytes succeeded"); 279 } catch (SQLException sqle) { 280 org.apache.derbyTesting.functionTests.util.TestUtil.dumpSQLExceptions(sqle, true); 281 } 282 283 ppw.setInt(1, 233); 284 ppw.setBinaryStream(2, new java.io.ByteArrayInputStream (tooFew), 233); try { 286 ppw.executeUpdate(); 287 System.out.println("FAIL - execute with setBinaryStream() with too many bytes succeeded"); 288 } catch (SQLException sqle) { 289 org.apache.derbyTesting.functionTests.util.TestUtil.dumpSQLExceptions(sqle, true); 290 } 291 292 293 ppw.close(); 294 rs.close(); 295 296 TestOfGetAsciiStream.executeTestOfGetAsciiStream(con); 297 298 cleanUp(stmt); 299 stmt.close(); 300 con.close(); 301 302 } 303 catch (SQLException e) { 304 dumpSQLExceptions(e); 305 } 306 catch (Throwable e) { 307 System.out.println("FAIL -- unexpected exception:" + e.toString()); 308 e.printStackTrace(); 309 } 310 311 System.out.println("Test resultsetStream finished"); 312 } 313 314 static private void dumpSQLExceptions (SQLException se) { 315 System.out.println("FAIL -- unexpected exception: " + se.toString()); 316 while (se != null) { 317 System.out.print("SQLSTATE("+se.getSQLState()+"):"); 318 se = se.getNextException(); 319 } 320 } 321 322 private static void cleanUp(Statement stmt) throws SQLException { 323 String [] testObjects = { " table t3", "table t2" }; 324 TestUtil.cleanUpTest(stmt, testObjects); 325 } 326 327 static class TestOfGetAsciiStream { 328 329 final static String TEST_STRING_DATA = 330 "ABCDEFG" + 331 "\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5" + 332 "\u00ff\u0100" + 333 "\u3042\u3044\u3046\u3048\u304a"; 334 335 336 static private void executeTestOfGetAsciiStream(Connection conn) throws SQLException { 337 338 System.out.println("Test of getAsciiStream"); 339 createTestTable(conn); 340 executeTestRun(conn); 341 342 } 343 344 345 static private void createTestTable(Connection conn) throws SQLException { 346 347 PreparedStatement st = null; 348 349 try{ 350 st = conn.prepareStatement("create table t3(text_data clob)"); 351 st.executeUpdate(); 352 353 }finally{ 354 if(st != null) 355 st.close(); 356 357 } 358 359 } 360 361 362 static private void executeTestRun(Connection conn) throws SQLException { 363 364 insertTestData(conn); 365 printTestData(conn); 366 367 } 368 369 370 static private void insertTestData(Connection conn) throws SQLException { 371 372 PreparedStatement st = null; 373 374 try{ 375 376 st = conn.prepareStatement("insert into t3(text_data) values(?)"); 377 st.setCharacterStream(1, 378 new StringReader (TEST_STRING_DATA), 379 TEST_STRING_DATA.length()); 380 st.executeUpdate(); 381 382 }finally{ 383 if(st != null) 384 st.close(); 385 386 } 387 388 } 389 390 391 static private void printTestData(Connection conn) throws SQLException { 392 393 PreparedStatement st = null; 394 ResultSet rs = null; 395 396 try{ 397 st = conn.prepareStatement("select " + 398 "text_data as text_data_col1," + 399 "text_data as text_data_col2 " + 400 "from " + 401 "t3"); 402 rs = st.executeQuery(); 403 404 while(rs.next()){ 405 printTestDataInARowViaStream(rs); 406 printTestDataInARowViaReader(rs); 407 } 408 409 }catch(IOException e){ 410 System.out.println("FAIL -- unexpected IOException: " + e.toString()); 411 e.printStackTrace(); 412 413 }finally{ 414 if(rs != null){ 415 rs.close(); 416 } 417 418 if(st != null){ 419 st.close(); 420 } 421 422 } 423 } 424 425 426 static private void printTestDataInARowViaStream(ResultSet rs) throws SQLException , 427 IOException { 428 429 InputStream is = null; 430 431 try{ 432 is = rs.getAsciiStream(1); 433 434 for(int c = is.read(); 435 c > -1; 436 c = is.read()){ 437 438 System.out.print(getCharacterCodeString((char) c)); 439 } 440 441 System.out.println(); 442 443 }finally{ 444 if(is != null) 445 is.close(); 446 } 447 448 } 449 450 451 static private void printTestDataInARowViaReader(ResultSet rs) throws SQLException , 452 IOException { 453 454 Reader reader = null; 455 456 try{ 457 reader = rs.getCharacterStream(2); 458 459 for(int c = reader.read(); 460 c > -1; 461 c = reader.read()){ 462 463 System.out.print(getCharacterCodeString((char) c)); 464 465 } 466 467 System.out.println(); 468 469 }finally{ 470 if(reader != null){ 471 reader.close(); 472 } 473 } 474 475 } 476 477 478 private static String getCharacterCodeString(char c){ 479 480 String hexString = Integer.toHexString((int) c); 481 482 while(hexString.length() < 4){ 483 hexString = "0" + hexString; 484 } 485 486 return "U+" + hexString; 487 } 488 489 } 490 491 } 492 | Popular Tags |