1 20 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 21 import org.apache.derbyTesting.functionTests.util.TestInputStream; 22 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.Statement ; 29 import java.sql.Blob ; 30 import java.sql.Connection ; 31 import java.sql.SQLException ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 35 39 final public class BLOBTest extends BaseJDBCTestCase 40 { 41 45 public BLOBTest(String name) 46 { 47 super(name); 48 } 49 50 51 57 public void testUpdateBlobFromScrollableResultSetUsingResultSetMethods() 58 throws SQLException , IOException 59 { 60 final Statement stmt = 61 createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 62 ResultSet.CONCUR_UPDATABLE); 63 final ResultSet rs = 64 stmt.executeQuery("SELECT * from " + 65 BLOBDataModelSetup.getBlobTableName()); 66 println("Last"); 67 rs.last(); 68 69 final int newVal = rs.getInt(1) + 11; 70 final int newSize = rs.getInt(2) / 2; 71 testUpdateBlobWithResultSetMethods(rs, newVal, newSize); 72 73 println("Verify updated blob using result set"); 74 verifyBlob(newVal, newSize, rs.getBlob(3)); 75 76 rs.close(); 77 stmt.close(); 78 } 79 80 86 public void testUpdateBlobFromForwardOnlyResultSetUsingResultSetMethods() 87 throws SQLException , IOException 88 { 89 final Statement stmt = 90 createStatement(ResultSet.TYPE_FORWARD_ONLY, 91 ResultSet.CONCUR_UPDATABLE); 92 final ResultSet rs = 93 stmt.executeQuery("SELECT * from " + 94 BLOBDataModelSetup.getBlobTableName()); 95 96 while (rs.next()) { 97 println("Next"); 98 final int val = rs.getInt(1); 99 if (val == BLOBDataModelSetup.bigVal) break; 100 } 101 102 final int newVal = rs.getInt(1) + 11; 103 final int newSize = rs.getInt(2) / 2; 104 testUpdateBlobWithResultSetMethods(rs, newVal, newSize); 105 106 rs.close(); 107 stmt.close(); 108 } 109 110 116 public void testUpdateBlobFromScrollableResultSetUsingPositionedUpdates() 117 throws SQLException , IOException 118 { 119 final Statement stmt = 120 createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 121 ResultSet.CONCUR_UPDATABLE); 122 final ResultSet rs = 123 stmt.executeQuery("SELECT * from " + 124 BLOBDataModelSetup.getBlobTableName()); 125 println("Last"); 126 rs.last(); 127 128 final int newVal = rs.getInt(1) + 11; 129 final int newSize = rs.getInt(2) / 2; 130 testUpdateBlobWithPositionedUpdate(rs, newVal, newSize); 131 132 rs.relative(0); 134 println("Verify updated blob using result set"); 135 verifyBlob(newVal, newSize, rs.getBlob(3)); 136 137 rs.close(); 138 stmt.close(); 139 } 140 141 147 public void testUpdateBlobFromForwardOnlyResultSetUsingPositionedUpdates() 148 throws SQLException , IOException 149 { 150 final Statement stmt = 151 createStatement(ResultSet.TYPE_FORWARD_ONLY, 152 ResultSet.CONCUR_UPDATABLE); 153 final ResultSet rs = 154 stmt.executeQuery("SELECT * from " + 155 BLOBDataModelSetup.getBlobTableName()); 156 while (rs.next()) { 157 println("Next"); 158 final int val = rs.getInt(1); 159 if (val == BLOBDataModelSetup.bigVal) break; 160 } 161 162 final int newVal = rs.getInt(1) + 11; 163 final int newSize = rs.getInt(2) / 2; 164 testUpdateBlobWithPositionedUpdate(rs, newVal, newSize); 165 166 rs.close(); 167 stmt.close(); 168 } 169 170 177 public void testUpdateBlobFromScrollableResultSetWithProjectUsingResultSetMethods() 178 throws SQLException , IOException 179 { 180 final Statement stmt = 181 createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 182 ResultSet.CONCUR_UPDATABLE); 183 final ResultSet rs = 184 stmt.executeQuery("SELECT data,val,length from " + 185 BLOBDataModelSetup.getBlobTableName()); 186 println("Last"); 187 rs.last(); 188 189 final int newVal = rs.getInt(2) + 11; 190 final int newSize = rs.getInt(3) / 2; 191 testUpdateBlobWithResultSetMethods(rs, newVal, newSize); 192 193 println("Verify updated blob using result set"); 194 verifyBlob(newVal, newSize, rs.getBlob(1)); 195 196 rs.close(); 197 stmt.close(); 198 } 199 200 207 public void testUpdateBlobFromForwardOnlyResultSetWithProjectUsingResultSetMethods() 208 throws SQLException , IOException 209 { 210 final Statement stmt = 211 createStatement(ResultSet.TYPE_FORWARD_ONLY, 212 ResultSet.CONCUR_UPDATABLE); 213 final ResultSet rs = 214 stmt.executeQuery("SELECT data,val,length from " + 215 BLOBDataModelSetup.getBlobTableName()); 216 217 while (rs.next()) { 218 println("Next"); 219 final int val = rs.getInt("VAL"); 220 if (val == BLOBDataModelSetup.bigVal) break; 221 } 222 223 final int newVal = rs.getInt("VAL") + 11; 224 final int newSize = BLOBDataModelSetup.bigSize / 2; 225 testUpdateBlobWithResultSetMethods(rs, newVal, newSize); 226 227 rs.close(); 228 stmt.close(); 229 } 230 231 238 public void testUpdateBlobFromScrollableResultSetWithProjectUsingPositionedUpdates() 239 throws SQLException , IOException 240 { 241 final Statement stmt = 242 createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 243 ResultSet.CONCUR_UPDATABLE); 244 final ResultSet rs = 245 stmt.executeQuery("SELECT data from " + 246 BLOBDataModelSetup.getBlobTableName() + 247 " WHERE val= " + BLOBDataModelSetup.bigVal); 248 println("Last"); 249 rs.last(); 250 251 final int newVal = BLOBDataModelSetup.bigVal * 2; 252 final int newSize = BLOBDataModelSetup.bigSize / 2; 253 testUpdateBlobWithPositionedUpdate(rs, newVal, newSize); 254 255 rs.relative(0); 257 println("Verify updated blob using result set"); 258 verifyBlob(newVal, newSize, rs.getBlob("DATA")); 259 260 rs.close(); 261 stmt.close(); 262 } 263 264 271 public void testUpdateBlobFromForwardOnlyResultSetWithProjectUsingPositionedUpdates() 272 throws SQLException , IOException 273 { 274 final Statement stmt = 275 createStatement(ResultSet.TYPE_FORWARD_ONLY, 276 ResultSet.CONCUR_UPDATABLE); 277 final ResultSet rs = 278 stmt.executeQuery("SELECT data from " + 279 BLOBDataModelSetup.getBlobTableName() + 280 " WHERE val = " + BLOBDataModelSetup.bigVal); 281 rs.next(); 282 283 final int newVal = BLOBDataModelSetup.bigVal * 2; 284 final int newSize = BLOBDataModelSetup.bigSize / 2; 285 testUpdateBlobWithPositionedUpdate(rs, newVal, newSize); 286 287 rs.close(); 288 stmt.close(); 289 } 290 291 292 300 private void testUpdateBlobWithResultSetMethods(final ResultSet rs, 301 final int newVal, 302 final int newSize) 303 throws SQLException , IOException 304 { 305 int val = rs.getInt("VAL"); 306 int size = rs.getInt("LENGTH"); 307 println("VerifyBlob"); 308 verifyBlob(val, size, rs.getBlob("DATA")); 309 310 println("UpdateBlob"); 311 final TestInputStream newStream = new TestInputStream(newSize, newVal); 312 313 rs.updateInt("VAL", newVal); 314 rs.updateInt("LENGTH", newSize); 315 rs.updateBinaryStream("DATA", newStream, newSize); 316 rs.updateRow(); 317 318 println("Verify updated blob with another query"); 319 verifyNewValueInTable(newVal, newSize); 320 } 321 322 330 private void testUpdateBlobWithPositionedUpdate(final ResultSet rs, 331 final int newVal, 332 final int newSize) 333 throws SQLException , IOException 334 { 335 final PreparedStatement preparedStatement = getConnection().prepareStatement 336 ("UPDATE " + BLOBDataModelSetup.getBlobTableName() + 337 " SET val=?, length = ?, data = ? WHERE CURRENT OF " + 338 rs.getCursorName()); 339 340 println("UpdateBlob"); 341 342 final TestInputStream newStream = new TestInputStream(newSize, newVal); 343 344 preparedStatement.setInt(1, newVal); 345 preparedStatement.setInt(2, newSize); 346 preparedStatement.setBinaryStream(3, newStream, newSize); 347 preparedStatement.executeUpdate(); 348 349 println("Verify updated blob with another query"); 350 verifyNewValueInTable(newVal, newSize); 351 } 352 353 354 362 private void verifyNewValueInTable(final int newVal, 363 final int newSize) 364 throws IOException , SQLException 365 { 366 println("Verify new value in table: " + newVal); 367 368 final Statement stmt = createStatement(ResultSet.TYPE_FORWARD_ONLY, 369 ResultSet.CONCUR_READ_ONLY); 370 371 final ResultSet rs = 372 stmt.executeQuery("SELECT * FROM " + 373 BLOBDataModelSetup.getBlobTableName() + 374 " WHERE val = " + newVal); 375 376 println("Query executed, calling next"); 377 378 boolean foundVal = false; 379 380 while (rs.next()) { 381 println("Next called, verifying row"); 382 383 assertEquals("Unexpected value in val column", 384 newVal, rs.getInt(1)); 385 386 verifyBlob(newVal, newSize, rs.getBlob(3)); 387 foundVal = true; 388 } 389 assertTrue("No column with value= " + newVal + " found ", foundVal); 390 391 rs.close(); 392 stmt.close(); 393 } 394 395 403 private void verifyBlob(final int expectedVal, 404 final int expectedSize, 405 final Blob blob) 406 throws IOException , SQLException 407 { 408 final InputStream stream = blob.getBinaryStream(); 409 int blobSize = 0; 410 for (int val = stream.read(); val!=-1; val = stream.read()) { 411 blobSize++; 412 413 if (expectedVal!=val) { 415 assertEquals("Unexpected value in stream at position " + 416 blobSize, 417 expectedVal, val); 418 } 419 } 420 stream.close(); 421 assertEquals("Unexpected size of stream ", expectedSize, blobSize); 422 } 423 424 428 public static Test suite() 429 { 430 TestSuite mainSuite = new TestSuite(BLOBTest.class); 431 return new BLOBDataModelSetup(mainSuite); 432 } 433 434 438 public final void setUp() 439 throws Exception 440 { 441 println("Setup of: " + getName()); 442 getConnection().setAutoCommit(false); 443 } 444 } 445 | Popular Tags |