001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package testsuite.simple;
026:
027: import testsuite.BaseTestCase;
028:
029: import java.io.BufferedInputStream;
030: import java.io.BufferedOutputStream;
031: import java.io.ByteArrayOutputStream;
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.FileOutputStream;
035: import java.io.InputStream;
036:
037: import java.sql.Connection;
038: import java.sql.SQLException;
039:
040: /**
041: * Tests BLOB functionality in the driver.
042: *
043: * @author Mark Matthews
044: * @version $Id: BlobTest.java 6437 2007-05-24 20:17:01Z mmatthews $
045: */
046: public class BlobTest extends BaseTestCase {
047:
048: private static File testBlobFile;
049:
050: static {
051: Runtime.getRuntime().addShutdownHook(new Thread() {
052: public void run() {
053: for (int i = 0; i < 5; i++) {
054: try {
055: if (testBlobFile.delete()) {
056: break;
057: }
058: } catch (Throwable t) {
059: }
060: }
061: }
062: });
063: }
064:
065: /**
066: * Creates a new BlobTest object.
067: *
068: * @param name
069: * the test to run
070: */
071: public BlobTest(String name) {
072: super (name);
073: }
074:
075: /**
076: * Runs all test cases in this test suite
077: *
078: * @param args
079: */
080: public static void main(String[] args) {
081: junit.textui.TestRunner.run(BlobTest.class);
082: }
083:
084: /**
085: * Setup the test case
086: *
087: * @throws Exception
088: * if an error occurs
089: */
090: public void setUp() throws Exception {
091: super .setUp();
092:
093: if (versionMeetsMinimum(4, 0)) {
094: int requiredSize = 32 * 1024 * 1024;
095:
096: if (testBlobFile == null
097: || testBlobFile.length() != requiredSize) {
098: createBlobFile(requiredSize);
099: }
100:
101: } else {
102: int requiredSize = 8 * 1024 * 1024;
103:
104: if (testBlobFile == null
105: || testBlobFile.length() != requiredSize) {
106: createBlobFile(requiredSize);
107: }
108: }
109:
110: createTestTable();
111: }
112:
113: /**
114: * Destroy resources created by test case
115: *
116: * @throws Exception
117: * if an error occurs
118: */
119: public void tearDown() throws Exception {
120: try {
121: this .stmt.executeUpdate("DROP TABLE IF EXISTS BLOBTEST");
122: } catch (Exception e) {
123: e.printStackTrace();
124: } finally {
125: super .tearDown();
126: }
127: }
128:
129: public void testByteStreamInsert() throws Exception {
130: testByteStreamInsert(this .conn);
131: }
132:
133: /**
134: * Tests inserting blob data as a stream
135: *
136: * @throws Exception
137: * if an error occurs
138: */
139: private void testByteStreamInsert(Connection c) throws Exception {
140: BufferedInputStream bIn = new BufferedInputStream(
141: new FileInputStream(testBlobFile));
142: this .pstmt = c
143: .prepareStatement("INSERT INTO BLOBTEST(blobdata) VALUES (?)");
144: this .pstmt.setBinaryStream(1, bIn, (int) testBlobFile.length());
145: this .pstmt.execute();
146:
147: this .pstmt.clearParameters();
148: doRetrieval();
149: }
150:
151: private boolean checkBlob(byte[] retrBytes) throws Exception {
152: boolean passed = false;
153: BufferedInputStream bIn = new BufferedInputStream(
154: new FileInputStream(testBlobFile));
155:
156: try {
157: int fileLength = (int) testBlobFile.length();
158: if (retrBytes.length == fileLength) {
159: for (int i = 0; i < fileLength; i++) {
160: byte fromFile = (byte) (bIn.read() & 0xff);
161:
162: if (retrBytes[i] != fromFile) {
163: passed = false;
164: System.out
165: .println("Byte pattern differed at position "
166: + i
167: + " , "
168: + retrBytes[i]
169: + " != " + fromFile);
170:
171: for (int j = 0; (j < (i + 10)) /* && (j < i) */; j++) {
172: System.out.print(Integer
173: .toHexString(retrBytes[j] & 0xff)
174: + " ");
175: }
176:
177: break;
178: }
179:
180: passed = true;
181: }
182: } else {
183: passed = false;
184: System.out.println("retrBytes.length("
185: + retrBytes.length + ") != testBlob.length("
186: + fileLength + ")");
187: }
188:
189: return passed;
190: } finally {
191: if (bIn != null) {
192: bIn.close();
193: }
194: }
195: }
196:
197: private void createTestTable() throws Exception {
198: //
199: // Catch the error, the table might exist
200: //
201: try {
202: this .stmt.executeUpdate("DROP TABLE BLOBTEST");
203: } catch (SQLException SQLE) {
204: ;
205: }
206:
207: this .stmt
208: .executeUpdate("CREATE TABLE BLOBTEST (pos int PRIMARY KEY auto_increment, "
209: + "blobdata LONGBLOB)");
210: }
211:
212: /**
213: * Mark this as deprecated to avoid warnings from compiler...
214: *
215: * @deprecated
216: *
217: * @throws Exception
218: * if an error occurs retrieving the value
219: */
220: private void doRetrieval() throws Exception {
221: boolean passed = false;
222: this .rs = this .stmt
223: .executeQuery("SELECT blobdata from BLOBTEST LIMIT 1");
224: this .rs.next();
225:
226: byte[] retrBytes = this .rs.getBytes(1);
227: passed = checkBlob(retrBytes);
228: assertTrue(
229: "Inserted BLOB data did not match retrieved BLOB data for getBytes().",
230: passed);
231: retrBytes = this .rs.getBlob(1).getBytes(1L,
232: (int) this .rs.getBlob(1).length());
233: passed = checkBlob(retrBytes);
234: assertTrue(
235: "Inserted BLOB data did not match retrieved BLOB data for getBlob().",
236: passed);
237:
238: InputStream inStr = this .rs.getBinaryStream(1);
239: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
240: int b;
241:
242: while ((b = inStr.read()) != -1) {
243: bOut.write((byte) b);
244: }
245:
246: retrBytes = bOut.toByteArray();
247: passed = checkBlob(retrBytes);
248: assertTrue(
249: "Inserted BLOB data did not match retrieved BLOB data for getBinaryStream().",
250: passed);
251: inStr = this .rs.getAsciiStream(1);
252: bOut = new ByteArrayOutputStream();
253:
254: while ((b = inStr.read()) != -1) {
255: bOut.write((byte) b);
256: }
257:
258: retrBytes = bOut.toByteArray();
259: passed = checkBlob(retrBytes);
260: assertTrue(
261: "Inserted BLOB data did not match retrieved BLOB data for getAsciiStream().",
262: passed);
263: inStr = this .rs.getUnicodeStream(1);
264: bOut = new ByteArrayOutputStream();
265:
266: while ((b = inStr.read()) != -1) {
267: bOut.write((byte) b);
268: }
269:
270: retrBytes = bOut.toByteArray();
271: passed = checkBlob(retrBytes);
272: assertTrue(
273: "Inserted BLOB data did not match retrieved BLOB data for getUnicodeStream().",
274: passed);
275: }
276:
277: private final static String TEST_BLOB_FILE_PREFIX = "cmj-testblob";
278:
279: private void createBlobFile(int size) throws Exception {
280: if (testBlobFile != null && testBlobFile.length() != size) {
281: testBlobFile.delete();
282: }
283:
284: testBlobFile = File.createTempFile(TEST_BLOB_FILE_PREFIX,
285: ".dat");
286: testBlobFile.deleteOnExit();
287:
288: cleanupTempFiles(testBlobFile, TEST_BLOB_FILE_PREFIX);
289:
290: BufferedOutputStream bOut = new BufferedOutputStream(
291: new FileOutputStream(testBlobFile));
292:
293: int dataRange = Byte.MAX_VALUE - Byte.MIN_VALUE;
294:
295: for (int i = 0; i < size; i++) {
296: bOut
297: .write((byte) ((Math.random() * dataRange) + Byte.MIN_VALUE));
298: }
299:
300: bOut.flush();
301: bOut.close();
302: }
303: }
|