|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package testsuite.simple; |
|
|
|
import java.io.BufferedInputStream; |
|
import java.io.BufferedOutputStream; |
|
import java.io.ByteArrayOutputStream; |
|
import java.io.File; |
|
import java.io.FileInputStream; |
|
import java.io.FileOutputStream; |
|
import java.io.InputStream; |
|
import java.sql.Connection; |
|
import java.sql.SQLException; |
|
|
|
import testsuite.BaseTestCase; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class BlobTest extends BaseTestCase { |
|
|
|
private static File testBlobFile; |
|
|
|
static { |
|
Runtime.getRuntime().addShutdownHook(new Thread() { |
|
public void run() { |
|
for (int i = 0; i < 5; i++) { |
|
try { |
|
if (testBlobFile.delete()) { |
|
break; |
|
} |
|
} catch (Throwable t) { |
|
} |
|
} |
|
} |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public BlobTest(String name) { |
|
super(name); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
junit.textui.TestRunner.run(BlobTest.class); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setUp() throws Exception { |
|
super.setUp(); |
|
|
|
if (versionMeetsMinimum(4, 0)) { |
|
int requiredSize = 32 * 1024 * 1024; |
|
|
|
if (testBlobFile == null || testBlobFile.length() != requiredSize) { |
|
createBlobFile(requiredSize); |
|
} |
|
|
|
} else { |
|
int requiredSize = 8 * 1024 * 1024; |
|
|
|
if (testBlobFile == null || testBlobFile.length() != requiredSize) { |
|
createBlobFile(requiredSize); |
|
} |
|
} |
|
|
|
createTestTable(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void tearDown() throws Exception { |
|
try { |
|
this.stmt.executeUpdate("DROP TABLE IF EXISTS BLOBTEST"); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} finally { |
|
super.tearDown(); |
|
} |
|
} |
|
|
|
public void testByteStreamInsert() throws Exception { |
|
testByteStreamInsert(this.conn); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
private void testByteStreamInsert(Connection c) throws Exception { |
|
BufferedInputStream bIn = new BufferedInputStream(new FileInputStream( |
|
testBlobFile)); |
|
this.pstmt = c |
|
.prepareStatement("INSERT INTO BLOBTEST(blobdata) VALUES (?)"); |
|
this.pstmt.setBinaryStream(1, bIn, (int) testBlobFile.length()); |
|
this.pstmt.execute(); |
|
|
|
this.pstmt.clearParameters(); |
|
doRetrieval(); |
|
} |
|
|
|
private boolean checkBlob(byte[] retrBytes) throws Exception { |
|
boolean passed = false; |
|
BufferedInputStream bIn = new BufferedInputStream(new FileInputStream( |
|
testBlobFile)); |
|
|
|
try { |
|
int fileLength = (int) testBlobFile.length(); |
|
if (retrBytes.length == fileLength) { |
|
for (int i = 0; i < fileLength; i++) { |
|
byte fromFile = (byte) (bIn.read() & 0xff); |
|
|
|
if (retrBytes[i] != fromFile) { |
|
passed = false; |
|
System.out.println("Byte pattern differed at position " |
|
+ i + " , " + retrBytes[i] + " != " + fromFile); |
|
|
|
for (int j = 0; (j < (i + 10)) ; j++) { |
|
System.out.print(Integer |
|
.toHexString(retrBytes[j] & 0xff) |
|
+ " "); |
|
} |
|
|
|
break; |
|
} |
|
|
|
passed = true; |
|
} |
|
} else { |
|
passed = false; |
|
System.out.println("retrBytes.length(" + retrBytes.length |
|
+ ") != testBlob.length(" + fileLength + ")"); |
|
} |
|
|
|
return passed; |
|
} finally { |
|
if (bIn != null) { |
|
bIn.close(); |
|
} |
|
} |
|
} |
|
|
|
private void createTestTable() throws Exception { |
|
|
|
|
|
|
|
try { |
|
this.stmt.executeUpdate("DROP TABLE BLOBTEST"); |
|
} catch (SQLException SQLE) { |
|
; |
|
} |
|
|
|
this.stmt |
|
.executeUpdate("CREATE TABLE BLOBTEST (pos int PRIMARY KEY auto_increment, " |
|
+ "blobdata LONGBLOB)"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void doRetrieval() throws Exception { |
|
boolean passed = false; |
|
this.rs = this.stmt |
|
.executeQuery("SELECT blobdata from BLOBTEST LIMIT 1"); |
|
this.rs.next(); |
|
|
|
byte[] retrBytes = this.rs.getBytes(1); |
|
passed = checkBlob(retrBytes); |
|
assertTrue( |
|
"Inserted BLOB data did not match retrieved BLOB data for getBytes().", |
|
passed); |
|
retrBytes = this.rs.getBlob(1).getBytes(1L, |
|
(int) this.rs.getBlob(1).length()); |
|
passed = checkBlob(retrBytes); |
|
assertTrue( |
|
"Inserted BLOB data did not match retrieved BLOB data for getBlob().", |
|
passed); |
|
|
|
InputStream inStr = this.rs.getBinaryStream(1); |
|
ByteArrayOutputStream bOut = new ByteArrayOutputStream(); |
|
int b; |
|
|
|
while ((b = inStr.read()) != -1) { |
|
bOut.write((byte) b); |
|
} |
|
|
|
retrBytes = bOut.toByteArray(); |
|
passed = checkBlob(retrBytes); |
|
assertTrue( |
|
"Inserted BLOB data did not match retrieved BLOB data for getBinaryStream().", |
|
passed); |
|
inStr = this.rs.getAsciiStream(1); |
|
bOut = new ByteArrayOutputStream(); |
|
|
|
while ((b = inStr.read()) != -1) { |
|
bOut.write((byte) b); |
|
} |
|
|
|
retrBytes = bOut.toByteArray(); |
|
passed = checkBlob(retrBytes); |
|
assertTrue( |
|
"Inserted BLOB data did not match retrieved BLOB data for getAsciiStream().", |
|
passed); |
|
inStr = this.rs.getUnicodeStream(1); |
|
bOut = new ByteArrayOutputStream(); |
|
|
|
while ((b = inStr.read()) != -1) { |
|
bOut.write((byte) b); |
|
} |
|
|
|
retrBytes = bOut.toByteArray(); |
|
passed = checkBlob(retrBytes); |
|
assertTrue( |
|
"Inserted BLOB data did not match retrieved BLOB data for getUnicodeStream().", |
|
passed); |
|
} |
|
|
|
private final static String TEST_BLOB_FILE_PREFIX = "cmj-testblob"; |
|
|
|
private void createBlobFile(int size) throws Exception { |
|
if (testBlobFile != null && testBlobFile.length() != size) { |
|
testBlobFile.delete(); |
|
} |
|
|
|
testBlobFile = File.createTempFile(TEST_BLOB_FILE_PREFIX, ".dat"); |
|
testBlobFile.deleteOnExit(); |
|
|
|
cleanupTempFiles(testBlobFile, TEST_BLOB_FILE_PREFIX); |
|
|
|
BufferedOutputStream bOut = new BufferedOutputStream( |
|
new FileOutputStream(testBlobFile)); |
|
|
|
int dataRange = Byte.MAX_VALUE - Byte.MIN_VALUE; |
|
|
|
for (int i = 0; i < size; i++) { |
|
bOut.write((byte) ((Math.random() * dataRange) + Byte.MIN_VALUE)); |
|
} |
|
|
|
bOut.flush(); |
|
bOut.close(); |
|
} |
|
} |
|
|