KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > regression > BlobRegressionTest


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.regression;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.BufferedOutputStream JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.StringReader JavaDoc;
34 import java.sql.Blob JavaDoc;
35 import java.sql.Connection JavaDoc;
36 import java.sql.DriverManager JavaDoc;
37 import java.sql.PreparedStatement JavaDoc;
38 import java.sql.ResultSet JavaDoc;
39 import java.sql.Statement JavaDoc;
40 import java.util.Properties JavaDoc;
41
42 import testsuite.BaseTestCase;
43
44 /**
45  * Tests fixes for BLOB handling.
46  *
47  * @author Mark Matthews
48  * @version $Id: BlobRegressionTest.java,v 1.1.2.19 2005/03/09 18:16:16
49  * mmatthews Exp $
50  */

51 public class BlobRegressionTest extends BaseTestCase {
52     /**
53      * Creates a new BlobRegressionTest.
54      *
55      * @param name
56      * name of the test to run
57      */

58     public BlobRegressionTest(String JavaDoc name) {
59         super(name);
60     }
61
62     /**
63      * Runs all test cases in this test suite
64      *
65      * @param args
66      */

67     public static void main(String JavaDoc[] args) {
68         junit.textui.TestRunner.run(BlobRegressionTest.class);
69     }
70
71     /**
72      *
73      *
74      * @throws Exception
75      * ...
76      */

77     public void testBug2670() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc blob = this.rs.getBlob(1);
99
100             //
101
// Test mid-point insertion
102
//
103
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             //
114
// Test end-point insertion
115
//
116
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     /**
126      *
127      *
128      * @throws Exception
129      * ...
130      */

131     public void testUpdateLongBlobGT16M() throws Exception JavaDoc {
132         if (versionMeetsMinimum(4, 0)) {
133             try {
134                 byte[] blobData = new byte[18 * 1024 * 1024]; // 18M blob
135

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 JavaDoc 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     /**
155      *
156      * @throws Exception
157      */

158     public void testUpdatableBlobsWithCharsets() throws Exception JavaDoc {
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 JavaDoc pStmt = this.conn
172                     .prepareStatement("INSERT INTO testUpdatableBlobsWithCharsets (pk, field1) VALUES (1, ?)");
173             pStmt.setBinaryStream(1, new ByteArrayInputStream JavaDoc(smallBlob),
174                     smallBlob.length);
175             pStmt.executeUpdate();
176
177             Statement JavaDoc 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 JavaDoc(smallBlob),
191                     smallBlob.length);
192             this.rs.updateRow();
193
194             ResultSet JavaDoc 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 JavaDoc {
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 JavaDoc sql = "insert into testBug5490 values(?,?)";
221
222             int blobFileSize = 871;
223             File JavaDoc blobFile = newTempBinaryFile("Bug5490", blobFileSize);
224
225             PreparedStatement JavaDoc pStmt = this.conn.prepareStatement(sql,
226                     ResultSet.TYPE_SCROLL_INSENSITIVE,
227                     ResultSet.CONCUR_READ_ONLY);
228             pStmt.setInt(1, 2);
229             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(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     /**
249      * Tests BUG#8096 where emulated locators corrupt binary data when using
250      * server-side prepared statements.
251      *
252      * @throws Exception
253      * if the test fails.
254      */

255     public void testBug8096() throws Exception JavaDoc {
256         int dataSize = 256;
257
258         Properties JavaDoc props = new Properties JavaDoc();
259         props.setProperty("emulateLocators", "true");
260         Connection JavaDoc locatorConn = getConnectionWithProps(props);
261
262         String JavaDoc createTable = "CREATE TABLE testBug8096 (ID VARCHAR(10) "
263                 + "PRIMARY KEY, DATA LONGBLOB)";
264         String JavaDoc select = "SELECT ID, 'DATA' AS BLOB_DATA FROM testBug8096 "
265                 + "WHERE ID = ?";
266         String JavaDoc insert = "INSERT INTO testBug8096 (ID, DATA) VALUES (?, '')";
267
268         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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                 // Will print out all of the values that don't match.
316
// All negative values will instead be replaced with 63.
317
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     /**
328      * Tests fix for BUG#9040 - PreparedStatement.addBatch() doesn't work with
329      * server-side prepared statements and streaming BINARY data.
330      *
331      * @throws Exception
332      * if the test fails.
333      */

334     public void testBug9040() throws Exception JavaDoc {
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 JavaDoc(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 JavaDoc(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 JavaDoc {
370         String JavaDoc tableName = "testBug10850";
371         
372         createTable(tableName, "(field1 TEXT)");
373         
374         PreparedStatement JavaDoc pStmt = null;
375         
376         try {
377             pStmt = this.conn.prepareStatement("INSERT INTO " +
378         
379                 tableName + " VALUES (?)");
380             pStmt.setCharacterStream(1, new StringReader JavaDoc(""), 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 JavaDoc(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 JavaDoc newTempBinaryFile(String JavaDoc name, long size) throws IOException JavaDoc {
400         File JavaDoc tempFile = File.createTempFile(name, "tmp");
401         tempFile.deleteOnExit();
402         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(tempFile);
403         BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(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