1 25 package testsuite.simple; 26 27 import testsuite.BaseTestCase; 28 29 import java.io.BufferedInputStream ; 30 import java.io.BufferedOutputStream ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.io.FileOutputStream ; 35 import java.io.InputStream ; 36 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.util.Properties ; 40 41 47 public class BlobTest extends BaseTestCase { 48 51 private static File testBlobFile; 52 53 56 62 public BlobTest(String name) { 63 super(name); 64 } 65 66 69 74 public static void main(String [] args) { 75 junit.textui.TestRunner.run(BlobTest.class); 76 } 77 78 84 public void setUp() throws Exception { 85 super.setUp(); 86 87 if (versionMeetsMinimum(4, 0)) { 88 int requiredSize = 32 * 1024 * 1024; 89 90 if (testBlobFile == null || testBlobFile.length() != requiredSize) { 91 createBlobFile(requiredSize); 92 } 93 94 } else { 95 int requiredSize = 8 * 1024 * 1024; 96 97 if (testBlobFile == null || testBlobFile.length() != requiredSize) { 98 createBlobFile(requiredSize); 99 } 100 } 101 102 createTestTable(); 103 } 104 105 111 public void tearDown() throws Exception { 112 try { 113 this.stmt.executeUpdate("DROP TABLE IF EXISTS BLOBTEST"); 114 } finally { 115 super.tearDown(); 116 } 117 } 118 119 125 public void testByteStreamInsert() throws Exception { 126 BufferedInputStream bIn = new BufferedInputStream (new FileInputStream ( 127 testBlobFile)); 128 this.pstmt = this.conn 129 .prepareStatement("INSERT INTO BLOBTEST(blobdata) VALUES (?)"); 130 this.pstmt.setBinaryStream(1, bIn, (int) testBlobFile.length()); 131 this.pstmt.execute(); 132 133 this.pstmt.clearParameters(); 134 doRetrieval(); 135 } 136 137 143 public void testBytesInsertNewIo() throws Exception { 144 Properties props = new Properties (); 145 props.setProperty("useNewIO", "true"); 146 147 this.conn = getConnectionWithProps(props); 148 149 testByteStreamInsert(); 150 } 151 152 private boolean checkBlob(byte[] retrBytes) throws Exception { 153 boolean passed = false; 154 BufferedInputStream bIn = new BufferedInputStream (new FileInputStream ( 155 testBlobFile)); 156 157 try { 158 int fileLength = (int) testBlobFile.length(); 159 if (retrBytes.length == fileLength) { 160 for (int i = 0; i < fileLength; i++) { 161 byte fromFile = (byte) (bIn.read() & 0xff); 162 163 if (retrBytes[i] != fromFile) { 164 passed = false; 165 System.out.println("Byte pattern differed at position " 166 + i + " , " + retrBytes[i] + " != " + fromFile); 167 168 for (int j = 0; (j < (i + 10)) ; j++) { 169 System.out.print(Integer 170 .toHexString(retrBytes[j] & 0xff) 171 + " "); 172 } 173 174 break; 175 } 176 177 passed = true; 178 } 179 } else { 180 passed = false; 181 System.out.println("retrBytes.length(" + retrBytes.length 182 + ") != testBlob.length(" + fileLength + ")"); 183 } 184 185 return passed; 186 } finally { 187 if (bIn != null) { 188 bIn.close(); 189 } 190 } 191 } 192 193 private void createTestTable() throws Exception { 194 try { 198 this.stmt.executeUpdate("DROP TABLE BLOBTEST"); 199 } catch (SQLException SQLE) { 200 ; 201 } 202 203 this.stmt 204 .executeUpdate("CREATE TABLE BLOBTEST (pos int PRIMARY KEY auto_increment, " 205 + "blobdata LONGBLOB)"); 206 } 207 208 216 private void doRetrieval() throws Exception { 217 boolean passed = false; 218 this.rs = this.stmt 219 .executeQuery("SELECT blobdata from BLOBTEST LIMIT 1"); 220 this.rs.next(); 221 222 byte[] retrBytes = this.rs.getBytes(1); 223 passed = checkBlob(retrBytes); 224 assertTrue( 225 "Inserted BLOB data did not match retrieved BLOB data for getBytes().", 226 passed); 227 retrBytes = this.rs.getBlob(1).getBytes(1L, 228 (int) this.rs.getBlob(1).length()); 229 passed = checkBlob(retrBytes); 230 assertTrue( 231 "Inserted BLOB data did not match retrieved BLOB data for getBlob().", 232 passed); 233 234 InputStream inStr = this.rs.getBinaryStream(1); 235 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 236 int b; 237 238 while ((b = inStr.read()) != -1) { 239 bOut.write((byte) b); 240 } 241 242 retrBytes = bOut.toByteArray(); 243 passed = checkBlob(retrBytes); 244 assertTrue( 245 "Inserted BLOB data did not match retrieved BLOB data for getBinaryStream().", 246 passed); 247 inStr = this.rs.getAsciiStream(1); 248 bOut = new ByteArrayOutputStream (); 249 250 while ((b = inStr.read()) != -1) { 251 bOut.write((byte) b); 252 } 253 254 retrBytes = bOut.toByteArray(); 255 passed = checkBlob(retrBytes); 256 assertTrue( 257 "Inserted BLOB data did not match retrieved BLOB data for getAsciiStream().", 258 passed); 259 inStr = this.rs.getUnicodeStream(1); 260 bOut = new ByteArrayOutputStream (); 261 262 while ((b = inStr.read()) != -1) { 263 bOut.write((byte) b); 264 } 265 266 retrBytes = bOut.toByteArray(); 267 passed = checkBlob(retrBytes); 268 assertTrue( 269 "Inserted BLOB data did not match retrieved BLOB data for getUnicodeStream().", 270 passed); 271 } 272 273 private void createBlobFile(int size) throws Exception { 274 if (testBlobFile != null && testBlobFile.length() != size) { 275 testBlobFile.delete(); 276 } 277 278 testBlobFile = File.createTempFile("testblob", ".dat"); 279 testBlobFile.deleteOnExit(); 280 281 BufferedOutputStream bOut = new BufferedOutputStream ( 282 new FileOutputStream (testBlobFile)); 283 284 int dataRange = Byte.MAX_VALUE - Byte.MIN_VALUE; 285 286 for (int i = 0; i < size; i++) { 287 bOut.write((byte) ((Math.random() * dataRange) + Byte.MIN_VALUE)); 288 } 289 290 bOut.flush(); 291 bOut.close(); 292 } 293 } 294 | Popular Tags |