1 25 package testsuite.regression; 26 27 import java.io.ByteArrayInputStream ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.BufferedOutputStream ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.io.StringReader ; 34 import java.sql.Blob ; 35 import java.sql.Connection ; 36 import java.sql.DriverManager ; 37 import java.sql.PreparedStatement ; 38 import java.sql.ResultSet ; 39 import java.sql.Statement ; 40 import java.util.Properties ; 41 42 import testsuite.BaseTestCase; 43 44 51 public class BlobRegressionTest extends BaseTestCase { 52 58 public BlobRegressionTest(String name) { 59 super(name); 60 } 61 62 67 public static void main(String [] args) { 68 junit.textui.TestRunner.run(BlobRegressionTest.class); 69 } 70 71 77 public void testBug2670() throws Exception { 78 try { 79 byte[] blobData = new byte[32]; 80 81 for (int i = 0; i < blobData.length; i++) { 82 blobData[i] = 1; 83 } 84 85 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug2670"); 86 this.stmt 87 .executeUpdate("CREATE TABLE testBug2670(blobField LONGBLOB)"); 88 89 PreparedStatement pStmt = this.conn 90 .prepareStatement("INSERT INTO testBug2670 (blobField) VALUES (?)"); 91 pStmt.setBytes(1, blobData); 92 pStmt.executeUpdate(); 93 94 this.rs = this.stmt 95 .executeQuery("SELECT blobField FROM testBug2670"); 96 this.rs.next(); 97 98 Blob blob = this.rs.getBlob(1); 99 100 blob.setBytes(4, new byte[] { 2, 2, 2, 2 }); 104 105 byte[] newBlobData = blob.getBytes(1L, (int) blob.length()); 106 107 assertTrue("Blob changed length", blob.length() == blobData.length); 108 109 assertTrue("New data inserted wrongly", 110 ((newBlobData[3] == 2) && (newBlobData[4] == 2) 111 && (newBlobData[5] == 2) && (newBlobData[6] == 2))); 112 113 blob.setBytes(32, new byte[] { 2, 2, 2, 2 }); 117 118 assertTrue("Blob length should be 3 larger", 119 blob.length() == (blobData.length + 3)); 120 } finally { 121 this.stmt.executeUpdate("DROP TABLE IF EXISTS testUpdateLongBlob"); 122 } 123 } 124 125 131 public void testUpdateLongBlobGT16M() throws Exception { 132 if (versionMeetsMinimum(4, 0)) { 133 try { 134 byte[] blobData = new byte[18 * 1024 * 1024]; 136 this.stmt 137 .executeUpdate("DROP TABLE IF EXISTS testUpdateLongBlob"); 138 this.stmt 139 .executeUpdate("CREATE TABLE testUpdateLongBlob(blobField LONGBLOB)"); 140 this.stmt 141 .executeUpdate("INSERT INTO testUpdateLongBlob (blobField) VALUES (NULL)"); 142 143 PreparedStatement pStmt = this.conn 144 .prepareStatement("UPDATE testUpdateLongBlob SET blobField=?"); 145 pStmt.setBytes(1, blobData); 146 pStmt.executeUpdate(); 147 } finally { 148 this.stmt 149 .executeUpdate("DROP TABLE IF EXISTS testUpdateLongBlob"); 150 } 151 } 152 } 153 154 158 public void testUpdatableBlobsWithCharsets() throws Exception { 159 byte[] smallBlob = new byte[32]; 160 161 for (byte i = 0; i < smallBlob.length; i++) { 162 smallBlob[i] = i; 163 } 164 165 try { 166 this.stmt 167 .executeUpdate("DROP TABLE IF EXISTS testUpdatableBlobsWithCharsets"); 168 this.stmt 169 .executeUpdate("CREATE TABLE testUpdatableBlobsWithCharsets(pk INT NOT NULL PRIMARY KEY, field1 BLOB)"); 170 171 PreparedStatement pStmt = this.conn 172 .prepareStatement("INSERT INTO testUpdatableBlobsWithCharsets (pk, field1) VALUES (1, ?)"); 173 pStmt.setBinaryStream(1, new ByteArrayInputStream (smallBlob), 174 smallBlob.length); 175 pStmt.executeUpdate(); 176 177 Statement updStmt = this.conn 178 .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 179 ResultSet.CONCUR_UPDATABLE); 180 181 this.rs = updStmt 182 .executeQuery("SELECT pk, field1 FROM testUpdatableBlobsWithCharsets"); 183 System.out.println(this.rs); 184 this.rs.next(); 185 186 for (byte i = 0; i < smallBlob.length; i++) { 187 smallBlob[i] = (byte) (i + 32); 188 } 189 190 this.rs.updateBinaryStream(2, new ByteArrayInputStream (smallBlob), 191 smallBlob.length); 192 this.rs.updateRow(); 193 194 ResultSet newRs = this.stmt 195 .executeQuery("SELECT field1 FROM testUpdatableBlobsWithCharsets"); 196 197 newRs.next(); 198 199 byte[] updatedBlob = newRs.getBytes(1); 200 201 for (byte i = 0; i < smallBlob.length; i++) { 202 byte origValue = smallBlob[i]; 203 byte newValue = updatedBlob[i]; 204 205 assertTrue("Original byte at position " + i + ", " + origValue 206 + " != new value, " + newValue, origValue == newValue); 207 } 208 209 } finally { 210 this.stmt 211 .executeUpdate("DROP TABLE IF EXISTS testUpdatableBlobsWithCharsets"); 212 } 213 } 214 215 public void testBug5490() throws Exception { 216 try { 217 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5490"); 218 this.stmt.executeUpdate("CREATE TABLE testBug5490" 219 + "(pk INT NOT NULL PRIMARY KEY, blobField BLOB)"); 220 String sql = "insert into testBug5490 values(?,?)"; 221 222 int blobFileSize = 871; 223 File blobFile = newTempBinaryFile("Bug5490", blobFileSize); 224 225 PreparedStatement pStmt = this.conn.prepareStatement(sql, 226 ResultSet.TYPE_SCROLL_INSENSITIVE, 227 ResultSet.CONCUR_READ_ONLY); 228 pStmt.setInt(1, 2); 229 FileInputStream fis = new FileInputStream (blobFile); 230 pStmt.setBinaryStream(2, fis, blobFileSize); 231 pStmt.execute(); 232 fis.close(); 233 pStmt.close(); 234 235 this.rs = this.stmt 236 .executeQuery("SELECT blobField FROM testBug5490"); 237 238 this.rs.next(); 239 240 byte[] returned = this.rs.getBytes(1); 241 242 assertEquals(blobFileSize, returned.length); 243 } finally { 244 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5490"); 245 } 246 } 247 248 255 public void testBug8096() throws Exception { 256 int dataSize = 256; 257 258 Properties props = new Properties (); 259 props.setProperty("emulateLocators", "true"); 260 Connection locatorConn = getConnectionWithProps(props); 261 262 String createTable = "CREATE TABLE testBug8096 (ID VARCHAR(10) " 263 + "PRIMARY KEY, DATA LONGBLOB)"; 264 String select = "SELECT ID, 'DATA' AS BLOB_DATA FROM testBug8096 " 265 + "WHERE ID = ?"; 266 String insert = "INSERT INTO testBug8096 (ID, DATA) VALUES (?, '')"; 267 268 String id = "1"; 269 byte[] testData = new byte[dataSize]; 270 271 for (int i = 0; i < testData.length; i++) { 272 testData[i] = (byte) i; 273 } 274 275 try { 276 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug8096"); 277 278 this.stmt.executeUpdate(createTable); 279 280 PreparedStatement ps = locatorConn.prepareStatement(insert); 281 ps.setString(1, id); 282 ps.execute(); 283 284 ps = locatorConn.prepareStatement(select); 285 ps.setString(1, id); 286 287 this.rs = ps.executeQuery(); 288 289 if (this.rs.next()) { 290 Blob b = rs.getBlob("BLOB_DATA"); 291 b.setBytes(1, testData); 292 } 293 294 this.rs.close(); 295 ps.close(); 296 297 ps = locatorConn.prepareStatement(select); 298 ps.setString(1, id); 299 300 this.rs = ps.executeQuery(); 301 302 byte[] result = null; 303 if (this.rs.next()) { 304 Blob b = this.rs.getBlob("BLOB_DATA"); 305 306 result = b.getBytes(1, dataSize - 1); 307 } 308 309 this.rs.close(); 310 ps.close(); 311 312 assertNotNull(result); 313 314 for (int i = 0; i < result.length && i < testData.length; i++) { 315 if (result[i] != testData[i]) { 318 assertEquals("At position " + i, testData[i], result[i]); 319 } 320 } 321 322 } finally { 323 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug8096"); 324 } 325 } 326 327 334 public void testBug9040() throws Exception { 335 try { 336 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug9040"); 337 338 this.stmt.executeUpdate("create table if not exists testBug9040 " 339 + "(primary_key int not null primary key, " 340 + "data mediumblob)"); 341 342 this.pstmt = this.conn 343 .prepareStatement("replace into testBug9040 (primary_key, data) values(?,?)"); 344 345 int primaryKey = 1; 346 byte[] data = "First Row".getBytes(); 347 this.pstmt.setInt(1, primaryKey); 348 this.pstmt.setBinaryStream(2, new ByteArrayInputStream (data), 349 data.length); 350 this.pstmt.addBatch(); 351 352 primaryKey = 2; 353 data = "Second Row".getBytes(); 354 this.pstmt.setInt(1, primaryKey); 355 this.pstmt.setBinaryStream(2, new ByteArrayInputStream (data), 356 data.length); 357 this.pstmt.addBatch(); 358 359 this.pstmt.executeBatch(); 360 } finally { 361 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug9040"); 362 363 if (this.pstmt != null) { 364 this.pstmt.close(); 365 } 366 } 367 } 368 369 public void testBug10850() throws Exception { 370 String tableName = "testBug10850"; 371 372 createTable(tableName, "(field1 TEXT)"); 373 374 PreparedStatement pStmt = null; 375 376 try { 377 pStmt = this.conn.prepareStatement("INSERT INTO " + 378 379 tableName + " VALUES (?)"); 380 pStmt.setCharacterStream(1, new StringReader (""), 0); 381 pStmt.executeUpdate(); 382 383 assertEquals("0", getSingleIndexedValueWithQuery(1, "SELECT LENGTH(field1) FROM " + tableName).toString()); 384 this.stmt.executeUpdate("TRUNCATE TABLE " + tableName); 385 386 pStmt.clearParameters(); 387 pStmt.setBinaryStream(1, new ByteArrayInputStream (new byte[0]), 0); 388 pStmt.executeUpdate(); 389 390 assertEquals("0", getSingleIndexedValueWithQuery(1, "SELECT LENGTH(field1) FROM " + tableName).toString()); 391 this.stmt.executeUpdate("TRUNCATE TABLE " + tableName); 392 } finally { 393 if (pStmt != null) { 394 pStmt.close(); 395 } 396 } 397 } 398 399 private File newTempBinaryFile(String name, long size) throws IOException { 400 File tempFile = File.createTempFile(name, "tmp"); 401 tempFile.deleteOnExit(); 402 FileOutputStream fos = new FileOutputStream (tempFile); 403 BufferedOutputStream bos = new BufferedOutputStream (fos); 404 for (long i = 0; i < size; i++) { 405 bos.write((byte) i); 406 } 407 bos.close(); 408 assertTrue(tempFile.exists()); 409 assertEquals(size, tempFile.length()); 410 return tempFile; 411 } 412 413 } | Popular Tags |