KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > BLOBTest


1 /**
2  *
3  * Derby - Class BLOBTest
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements. See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
21 import org.apache.derbyTesting.functionTests.util.TestInputStream;
22 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.Blob JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34
35 /**
36  * Tests reading and updating binary large objects (BLOBs).
37  * @author Andreas Korneliussen
38  */

39 final public class BLOBTest extends BaseJDBCTestCase
40 {
41     /**
42      * Constructor
43      * @param name name of test case (method).
44      */

45     public BLOBTest(String JavaDoc name)
46     {
47         super(name);
48     }
49
50     
51     /**
52      * Tests updating a Blob from a scollable resultset, using
53      * result set update methods.
54      * @exception SQLException causes test to fail with error
55      * @exception IOException causes test to fail with error
56      */

57     public void testUpdateBlobFromScrollableResultSetUsingResultSetMethods()
58         throws SQLException JavaDoc, IOException JavaDoc
59     {
60         final Statement JavaDoc stmt =
61             createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
62                                 ResultSet.CONCUR_UPDATABLE);
63         final ResultSet JavaDoc rs =
64             stmt.executeQuery("SELECT * from " +
65                               BLOBDataModelSetup.getBlobTableName());
66         println("Last");
67         rs.last();
68         
69         final int newVal = rs.getInt(1) + 11;
70         final int newSize = rs.getInt(2) / 2;
71         testUpdateBlobWithResultSetMethods(rs, newVal, newSize);
72         
73         println("Verify updated blob using result set");
74         verifyBlob(newVal, newSize, rs.getBlob(3));
75         
76         rs.close();
77         stmt.close();
78     }
79
80     /**
81      * Tests updating a Blob from a forward only resultset, using
82      * result set update methods.
83      * @exception SQLException causes test to fail with error
84      * @exception IOException causes test to fail with error
85      */

86     public void testUpdateBlobFromForwardOnlyResultSetUsingResultSetMethods()
87         throws SQLException JavaDoc, IOException JavaDoc
88     {
89         final Statement JavaDoc stmt =
90             createStatement(ResultSet.TYPE_FORWARD_ONLY,
91                                 ResultSet.CONCUR_UPDATABLE);
92         final ResultSet JavaDoc rs =
93             stmt.executeQuery("SELECT * from " +
94                               BLOBDataModelSetup.getBlobTableName());
95         
96         while (rs.next()) {
97             println("Next");
98             final int val = rs.getInt(1);
99             if (val == BLOBDataModelSetup.bigVal) break;
100         }
101         
102         final int newVal = rs.getInt(1) + 11;
103         final int newSize = rs.getInt(2) / 2;
104         testUpdateBlobWithResultSetMethods(rs, newVal, newSize);
105         
106         rs.close();
107         stmt.close();
108     }
109
110     /**
111      * Tests updating a Blob from a scollable resultset, using
112      * positioned updates.
113      * @exception SQLException causes test to fail with error
114      * @exception IOException causes test to fail with error
115      */

116     public void testUpdateBlobFromScrollableResultSetUsingPositionedUpdates()
117         throws SQLException JavaDoc, IOException JavaDoc
118     {
119         final Statement JavaDoc stmt =
120             createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
121                                 ResultSet.CONCUR_UPDATABLE);
122         final ResultSet JavaDoc rs =
123             stmt.executeQuery("SELECT * from " +
124                               BLOBDataModelSetup.getBlobTableName());
125         println("Last");
126         rs.last();
127         
128         final int newVal = rs.getInt(1) + 11;
129         final int newSize = rs.getInt(2) / 2;
130         testUpdateBlobWithPositionedUpdate(rs, newVal, newSize);
131
132         rs.relative(0); // Necessary after a positioned update
133

134         println("Verify updated blob using result set");
135         verifyBlob(newVal, newSize, rs.getBlob(3));
136         
137         rs.close();
138         stmt.close();
139     }
140
141     /**
142      * Tests updating a Blob from a forward only resultset, using
143      * methods.
144      * @exception SQLException causes test to fail with error
145      * @exception IOException causes test to fail with error
146      */

147     public void testUpdateBlobFromForwardOnlyResultSetUsingPositionedUpdates()
148         throws SQLException JavaDoc, IOException JavaDoc
149     {
150         final Statement JavaDoc stmt =
151             createStatement(ResultSet.TYPE_FORWARD_ONLY,
152                                 ResultSet.CONCUR_UPDATABLE);
153         final ResultSet JavaDoc rs =
154             stmt.executeQuery("SELECT * from " +
155                               BLOBDataModelSetup.getBlobTableName());
156         while (rs.next()) {
157             println("Next");
158             final int val = rs.getInt(1);
159             if (val == BLOBDataModelSetup.bigVal) break;
160         }
161         
162         final int newVal = rs.getInt(1) + 11;
163         final int newSize = rs.getInt(2) / 2;
164         testUpdateBlobWithPositionedUpdate(rs, newVal, newSize);
165         
166         rs.close();
167         stmt.close();
168     }
169
170     /**
171      * Tests updating a Blob from a scollable resultset produced by a
172      * select query with projection. Updates are made using
173      * result set update methods.
174      * @exception SQLException causes test to fail with error
175      * @exception IOException causes test to fail with error
176      */

177     public void testUpdateBlobFromScrollableResultSetWithProjectUsingResultSetMethods()
178         throws SQLException JavaDoc, IOException JavaDoc
179     {
180         final Statement JavaDoc stmt =
181             createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
182                                 ResultSet.CONCUR_UPDATABLE);
183         final ResultSet JavaDoc rs =
184             stmt.executeQuery("SELECT data,val,length from " +
185                               BLOBDataModelSetup.getBlobTableName());
186         println("Last");
187         rs.last();
188         
189         final int newVal = rs.getInt(2) + 11;
190         final int newSize = rs.getInt(3) / 2;
191         testUpdateBlobWithResultSetMethods(rs, newVal, newSize);
192         
193         println("Verify updated blob using result set");
194         verifyBlob(newVal, newSize, rs.getBlob(1));
195         
196         rs.close();
197         stmt.close();
198     }
199
200     /**
201      * Tests updating a Blob from a forward only resultset, produced by
202      * a select query with projection. Updates are made using
203      * result set update methods.
204      * @exception SQLException causes test to fail with error
205      * @exception IOException causes test to fail with error
206      */

207     public void testUpdateBlobFromForwardOnlyResultSetWithProjectUsingResultSetMethods()
208         throws SQLException JavaDoc, IOException JavaDoc
209     {
210         final Statement JavaDoc stmt =
211             createStatement(ResultSet.TYPE_FORWARD_ONLY,
212                                 ResultSet.CONCUR_UPDATABLE);
213         final ResultSet JavaDoc rs =
214             stmt.executeQuery("SELECT data,val,length from " +
215                               BLOBDataModelSetup.getBlobTableName());
216         
217         while (rs.next()) {
218             println("Next");
219             final int val = rs.getInt("VAL");
220             if (val == BLOBDataModelSetup.bigVal) break;
221         }
222         
223         final int newVal = rs.getInt("VAL") + 11;
224         final int newSize = BLOBDataModelSetup.bigSize / 2;
225         testUpdateBlobWithResultSetMethods(rs, newVal, newSize);
226         
227         rs.close();
228         stmt.close();
229     }
230
231     /**
232      * Tests updating a Blob from a scollable resultset, produced by
233      * a select query with projection. Updates are made using
234      * positioned updates
235      * @exception SQLException causes test to fail with error
236      * @exception IOException causes test to fail with error
237      */

238     public void testUpdateBlobFromScrollableResultSetWithProjectUsingPositionedUpdates()
239         throws SQLException JavaDoc, IOException JavaDoc
240     {
241         final Statement JavaDoc stmt =
242             createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
243                                 ResultSet.CONCUR_UPDATABLE);
244         final ResultSet JavaDoc rs =
245             stmt.executeQuery("SELECT data from " +
246                               BLOBDataModelSetup.getBlobTableName() +
247                               " WHERE val= " + BLOBDataModelSetup.bigVal);
248         println("Last");
249         rs.last();
250         
251         final int newVal = BLOBDataModelSetup.bigVal * 2;
252         final int newSize = BLOBDataModelSetup.bigSize / 2;
253         testUpdateBlobWithPositionedUpdate(rs, newVal, newSize);
254
255         rs.relative(0); // Necessary after a positioned update
256

257         println("Verify updated blob using result set");
258         verifyBlob(newVal, newSize, rs.getBlob("DATA"));
259         
260         rs.close();
261         stmt.close();
262     }
263
264     /**
265      * Tests updating a Blob from a forward only resultset, produced by
266      * a select query with projection. Updates are made using
267      * positioned updates.
268      * @exception SQLException causes test to fail with error
269      * @exception IOException causes test to fail with error
270      */

271     public void testUpdateBlobFromForwardOnlyResultSetWithProjectUsingPositionedUpdates()
272         throws SQLException JavaDoc, IOException JavaDoc
273     {
274         final Statement JavaDoc stmt =
275             createStatement(ResultSet.TYPE_FORWARD_ONLY,
276                                 ResultSet.CONCUR_UPDATABLE);
277         final ResultSet JavaDoc rs =
278             stmt.executeQuery("SELECT data from " +
279                               BLOBDataModelSetup.getBlobTableName() +
280                               " WHERE val = " + BLOBDataModelSetup.bigVal);
281         rs.next();
282         
283         final int newVal = BLOBDataModelSetup.bigVal * 2;
284         final int newSize = BLOBDataModelSetup.bigSize / 2;
285         testUpdateBlobWithPositionedUpdate(rs, newVal, newSize);
286         
287         rs.close();
288         stmt.close();
289     }
290     
291     
292     /**
293      * Tests updating the Blob using result set update methods.
294      * @param rs result set, currently positioned on row to be updated
295      * @param newVal new value in val column and blob data
296      * @param newSize new size of Blob
297      * @exception SQLException causes test to fail with error
298      * @exception IOException causes test to fail with error
299      */

300     private void testUpdateBlobWithResultSetMethods(final ResultSet JavaDoc rs,
301                                                     final int newVal,
302                                                     final int newSize)
303         throws SQLException JavaDoc, IOException JavaDoc
304     {
305         int val = rs.getInt("VAL");
306         int size = rs.getInt("LENGTH");
307         println("VerifyBlob");
308         verifyBlob(val, size, rs.getBlob("DATA"));
309         
310         println("UpdateBlob");
311         final TestInputStream newStream = new TestInputStream(newSize, newVal);
312         
313         rs.updateInt("VAL", newVal);
314         rs.updateInt("LENGTH", newSize);
315         rs.updateBinaryStream("DATA", newStream, newSize);
316         rs.updateRow();
317         
318         println("Verify updated blob with another query");
319         verifyNewValueInTable(newVal, newSize);
320     }
321
322     /**
323      * Tests updating the Blob using positioned updates
324      * @param rs result set, currently positioned on row to be updated
325      * @param newVal new value in val column and blob data
326      * @param newSize new size of Blob
327      * @exception SQLException causes test to fail with error
328      * @exception IOException causes test to fail with error
329      */

330     private void testUpdateBlobWithPositionedUpdate(final ResultSet JavaDoc rs,
331                                                     final int newVal,
332                                                     final int newSize)
333         throws SQLException JavaDoc, IOException JavaDoc
334     {
335         final PreparedStatement JavaDoc preparedStatement = getConnection().prepareStatement
336             ("UPDATE " + BLOBDataModelSetup.getBlobTableName() +
337              " SET val=?, length = ?, data = ? WHERE CURRENT OF " +
338              rs.getCursorName());
339         
340         println("UpdateBlob");
341         
342         final TestInputStream newStream = new TestInputStream(newSize, newVal);
343         
344         preparedStatement.setInt(1, newVal);
345         preparedStatement.setInt(2, newSize);
346         preparedStatement.setBinaryStream(3, newStream, newSize);
347         preparedStatement.executeUpdate();
348         
349         println("Verify updated blob with another query");
350         verifyNewValueInTable(newVal, newSize);
351     }
352     
353     
354     /**
355      * Verifies that the table has row with column val=newVal
356      * and that it its data and size columns are consistent.
357      * @param newVal value expected to be found in the val column of a row
358      * @param newSize expected size of size column and size of blob
359      * @exception SQLException causes test to fail with error
360      * @exception IOException causes test to fail with error
361      */

362     private void verifyNewValueInTable(final int newVal,
363                                        final int newSize)
364         throws IOException JavaDoc, SQLException JavaDoc
365     {
366         println("Verify new value in table: " + newVal);
367         
368         final Statement JavaDoc stmt = createStatement(ResultSet.TYPE_FORWARD_ONLY,
369                                                    ResultSet.CONCUR_READ_ONLY);
370         
371         final ResultSet JavaDoc rs =
372             stmt.executeQuery("SELECT * FROM " +
373                               BLOBDataModelSetup.getBlobTableName() +
374                               " WHERE val = " + newVal);
375         
376         println("Query executed, calling next");
377         
378         boolean foundVal = false;
379         
380         while (rs.next()) {
381             println("Next called, verifying row");
382             
383             assertEquals("Unexpected value in val column",
384                          newVal, rs.getInt(1));
385             
386             verifyBlob(newVal, newSize, rs.getBlob(3));
387             foundVal = true;
388         }
389         assertTrue("No column with value= " + newVal + " found ", foundVal);
390         
391         rs.close();
392         stmt.close();
393     }
394                           
395     /**
396      * Verifies that the blob is consistent
397      * @param expectedVal the InputStream for the Blob should return this value
398      * for every byte
399      * @param expecteSize the Blob should have this size
400      * @exception SQLException causes test to fail with error
401      * @exception IOException causes test to fail with error
402      */

403     private void verifyBlob(final int expectedVal,
404                             final int expectedSize,
405                             final Blob JavaDoc blob)
406         throws IOException JavaDoc, SQLException JavaDoc
407     {
408         final InputStream stream = blob.getBinaryStream();
409         int blobSize = 0;
410         for (int val = stream.read(); val!=-1; val = stream.read()) {
411             blobSize++;
412             
413             // avoid doing a string-concat for every byte in blob
414
if (expectedVal!=val) {
415                 assertEquals("Unexpected value in stream at position " +
416                              blobSize,
417                              expectedVal, val);
418             }
419         }
420         stream.close();
421         assertEquals("Unexpected size of stream ", expectedSize, blobSize);
422     }
423
424     /**
425      * The suite decorates the tests of this class with
426      * a setup which creates and populates the data model.
427      */

428     public static Test suite()
429     {
430         TestSuite mainSuite = new TestSuite(BLOBTest.class);
431         return new BLOBDataModelSetup(mainSuite);
432     }
433
434     /**
435      * The setup creates a Connection to the database.
436      * @exception Exception any exception will cause test to fail with error.
437      */

438     public final void setUp()
439         throws Exception JavaDoc
440     {
441         println("Setup of: " + getName());
442         getConnection().setAutoCommit(false);
443     }
444 }
445
Popular Tags