KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > simple > BlobTest


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23
24  */

25 package testsuite.simple;
26
27 import testsuite.BaseTestCase;
28
29 import java.io.BufferedInputStream JavaDoc;
30 import java.io.BufferedOutputStream JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.InputStream JavaDoc;
36
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.util.Properties JavaDoc;
40
41 /**
42  * Tests BLOB functionality in the driver.
43  *
44  * @author Mark Matthews
45  * @version $Id: BlobTest.java,v 1.1.2.2 2005/05/19 15:52:24 mmatthews Exp $
46  */

47 public class BlobTest extends BaseTestCase {
48     // ~ Static fields/initializers
49
// ---------------------------------------------
50

51     private static File JavaDoc testBlobFile;
52
53     // ~ Constructors
54
// -----------------------------------------------------------
55

56     /**
57      * Creates a new BlobTest object.
58      *
59      * @param name
60      * the test to run
61      */

62     public BlobTest(String JavaDoc name) {
63         super(name);
64     }
65
66     // ~ Methods
67
// ----------------------------------------------------------------
68

69     /**
70      * Runs all test cases in this test suite
71      *
72      * @param args
73      */

74     public static void main(String JavaDoc[] args) {
75         junit.textui.TestRunner.run(BlobTest.class);
76     }
77
78     /**
79      * Setup the test case
80      *
81      * @throws Exception
82      * if an error occurs
83      */

84     public void setUp() throws Exception JavaDoc {
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     /**
106      * Destroy resources created by test case
107      *
108      * @throws Exception
109      * if an error occurs
110      */

111     public void tearDown() throws Exception JavaDoc {
112         try {
113             this.stmt.executeUpdate("DROP TABLE IF EXISTS BLOBTEST");
114         } finally {
115             super.tearDown();
116         }
117     }
118
119     /**
120      * Tests inserting blob data as a stream
121      *
122      * @throws Exception
123      * if an error occurs
124      */

125     public void testByteStreamInsert() throws Exception JavaDoc {
126         BufferedInputStream JavaDoc bIn = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(
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     /**
138      * Tests multi-packet functionality with NIO layer.
139      *
140      * @throws Exception
141      * if an unexpected error occurs.
142      */

143     public void testBytesInsertNewIo() throws Exception JavaDoc {
144         Properties JavaDoc props = new Properties JavaDoc();
145         props.setProperty("useNewIO", "true");
146
147         this.conn = getConnectionWithProps(props);
148
149         testByteStreamInsert();
150     }
151
152     private boolean checkBlob(byte[] retrBytes) throws Exception JavaDoc {
153         boolean passed = false;
154         BufferedInputStream JavaDoc bIn = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(
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 < i) */; 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 JavaDoc {
194         //
195
// Catch the error, the table might exist
196
//
197
try {
198             this.stmt.executeUpdate("DROP TABLE BLOBTEST");
199         } catch (SQLException JavaDoc SQLE) {
200             ;
201         }
202
203         this.stmt
204                 .executeUpdate("CREATE TABLE BLOBTEST (pos int PRIMARY KEY auto_increment, "
205                         + "blobdata LONGBLOB)");
206     }
207
208     /**
209      * Mark this as deprecated to avoid warnings from compiler...
210      *
211      * @deprecated
212      *
213      * @throws Exception
214      * if an error occurs retrieving the value
215      */

216     private void doRetrieval() throws Exception JavaDoc {
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 JavaDoc inStr = this.rs.getBinaryStream(1);
235         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
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 JavaDoc();
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 JavaDoc();
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 JavaDoc {
274         if (testBlobFile != null && testBlobFile.length() != size) {
275             testBlobFile.delete();
276         }
277
278         testBlobFile = File.createTempFile("testblob", ".dat");
279         testBlobFile.deleteOnExit();
280
281         BufferedOutputStream JavaDoc bOut = new BufferedOutputStream JavaDoc(
282                 new FileOutputStream JavaDoc(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