KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3 Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.Stream
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, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 */

21
22 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30
31 import java.io.IOException JavaDoc;
32 import java.sql.SQLException JavaDoc;
33
34 import org.apache.derby.tools.ij;
35
36
37 public class Stream {
38
39     public static void main(String JavaDoc[] args){
40     
41     Connection JavaDoc conn = null;
42
43     try{
44         ij.getPropertyArg(args);
45         conn = ij.startJBMS();
46         
47         createTestTables(conn);
48         executeTests(conn);
49         dropTestTables(conn);
50         
51     }catch(Throwable JavaDoc t){
52         t.printStackTrace();
53         
54     }finally{
55         if(conn != null){
56         try{
57             conn.close();
58             
59         }catch(SQLException JavaDoc e){
60             e.printStackTrace();
61         }
62         
63         }
64     }
65     
66     }
67
68     
69     private static void createTestTables(Connection JavaDoc conn)
70     throws SQLException JavaDoc,IOException JavaDoc {
71     
72     createTable(conn);
73     createTestData(conn);
74     
75     }
76     
77     
78     private static void createTable(Connection JavaDoc conn) throws SQLException JavaDoc {
79     
80     Statement JavaDoc st = null;
81     
82     try{
83         
84         st = conn.createStatement();
85         st.execute("create table SMALL_BLOB_TABLE( SMALL_BLOB blob( 512 ))");
86         st.execute("create table LARGE_BLOB_TABLE( LARGE_BLOB blob( 512k ))");
87         st.execute("create table SMALL_CLOB_TABLE( SMALL_CLOB clob( 512 ))");
88         st.execute("create table LARGE_CLOB_TABLE( LARGE_CLOB clob( 512k ))");
89
90     }finally{
91         if(st != null)
92         st.close();
93     }
94     
95     }
96
97     
98     private static void createTestData(Connection JavaDoc conn)
99     throws SQLException JavaDoc,IOException JavaDoc {
100
101     createSmallBlobTestData( conn );
102     createLargeBlobTestData( conn );
103     createSmallClobTestData( conn );
104     createLargeClobTestData( conn );
105     
106     }
107     
108     
109     private static void createSmallBlobTestData(Connection JavaDoc conn)
110     throws SQLException JavaDoc,IOException JavaDoc {
111     
112     PreparedStatement JavaDoc st = null;
113     TestDataStream stream = null;
114
115     try{
116         st = conn.prepareStatement("insert into SMALL_BLOB_TABLE(SMALL_BLOB) values(?)");
117         stream = new TestDataStream(512);
118         st.setBinaryStream(1, stream, 512);
119         st.executeUpdate();
120         
121     }finally{
122         if(st != null){
123         st.close();
124         }
125
126         if(stream != null){
127         stream.close();
128         }
129         
130     }
131
132     }
133     
134     
135     private static void createLargeBlobTestData(Connection JavaDoc conn)
136     throws SQLException JavaDoc,IOException JavaDoc {
137     
138     PreparedStatement JavaDoc st = null;
139     TestDataStream stream = null;
140     
141     try{
142         st = conn.prepareStatement("insert into LARGE_BLOB_TABLE(LARGE_BLOB) values(?)");
143         stream = new TestDataStream( 512 * 1024);
144         st.setBinaryStream(1,stream, 512 * 1024);
145
146         st.executeUpdate();
147         
148     }finally{
149         if(st != null){
150         st.close();
151         }
152
153         if(stream != null){
154         stream.close();
155         }
156     }
157     }
158     
159     
160     private static void createSmallClobTestData(Connection JavaDoc conn)
161     throws SQLException JavaDoc,IOException JavaDoc {
162     
163     PreparedStatement JavaDoc st = null;
164     TestDataReader reader = null;
165     
166     try{
167         st = conn.prepareStatement("insert into SMALL_CLOB_TABLE( SMALL_CLOB ) values(?)");
168
169         reader = new TestDataReader( 512 );
170         st.setCharacterStream(1,
171                   reader,
172                   512);
173         
174         st.executeUpdate();
175         
176         
177     }finally{
178         if(st != null)
179         st.close();
180         
181         if(reader != null)
182         reader.close();
183         
184     }
185     
186     }
187     
188
189     private static void createLargeClobTestData(Connection JavaDoc conn)
190     throws SQLException JavaDoc, IOException JavaDoc {
191     
192     PreparedStatement JavaDoc st = null;
193     TestDataReader reader = null;
194
195     try{
196         st = conn.prepareStatement("insert into LARGE_CLOB_TABLE( LARGE_CLOB ) values(?)");
197         
198         reader = new TestDataReader( 512 * 1024 );
199         st.setCharacterStream(1,
200                   reader,
201                   512 * 1024 );
202         
203         st.executeUpdate();
204         
205         
206     } finally {
207         if(st != null)
208         st.close();
209         
210         if(reader != null)
211         reader.close();
212     }
213     }
214
215     private static void executeTests(Connection JavaDoc conn)
216     throws SQLException JavaDoc, IOException JavaDoc {
217     
218     executeTestOnSmallBlob( conn );
219     executeTestOnLargeBlob( conn );
220     executeTestOnSmallClob( conn );
221     executeTestOnLargeClob( conn );
222     
223     }
224     
225     
226     private static void executeTestOnSmallBlob( Connection JavaDoc conn )
227     throws SQLException JavaDoc, IOException JavaDoc {
228     
229     BlobTester tester = new BlobTester( "SMALL_BLOB_TABLE",
230                         "SMALL_BLOB" );
231     tester.testGetStreamTwice( conn );
232     
233     }
234
235
236     private static void executeTestOnLargeBlob( Connection JavaDoc conn )
237     throws SQLException JavaDoc, IOException JavaDoc {
238     
239     BlobTester tester = new BlobTester( "LARGE_BLOB_TABLE",
240                         "LARGE_BLOB" );
241     tester.testGetStreamTwice( conn );
242     
243     }
244     
245     
246     private static void executeTestOnSmallClob( Connection JavaDoc conn )
247     throws SQLException JavaDoc, IOException JavaDoc {
248     
249     ClobTester tester = new ClobTester( "SMALL_CLOB_TABLE",
250                         "SMALL_CLOB" );
251     tester.testGetReaderTwice( conn );
252
253     }
254
255
256     private static void executeTestOnLargeClob( Connection JavaDoc conn )
257     throws SQLException JavaDoc, IOException JavaDoc {
258     
259     ClobTester tester = new ClobTester( "LARGE_CLOB_TABLE",
260                         "LARGE_CLOB" );
261     tester.testGetReaderTwice( conn );
262
263     }
264     
265
266     private static void dropTestTables( Connection JavaDoc conn ) throws SQLException JavaDoc {
267     
268     Statement JavaDoc st = null;
269     
270     try{
271         st = conn.createStatement();
272         st.execute("drop table SMALL_BLOB_TABLE");
273         st.execute("drop table LARGE_BLOB_TABLE");
274         st.execute("drop table SMALL_CLOB_TABLE");
275         st.execute("drop table LARGE_CLOB_TABLE");
276
277     }finally{
278         if(st != null)
279         st.close();
280     }
281     
282     }
283     
284     
285     static class TestDataStream extends InputStream JavaDoc {
286     
287     private long streamedLength = 0;
288     private final long total;
289     
290     
291     public TestDataStream(long length){
292         total = length;
293     }
294     
295     
296     public int read(){
297         
298         if(streamedLength >= total){
299         return -1;
300         }
301
302         return (int) ((streamedLength ++) % 256L);
303         
304     }
305     
306     
307     public void close(){
308         streamedLength = total;
309     }
310     
311     }
312     
313
314     static class TestDataReader extends Reader JavaDoc {
315     
316     private long wroteLength = 0;
317     private final long total;
318
319     
320     public TestDataReader(long length){
321         total = length;
322     }
323
324     
325     public void close(){
326         wroteLength = total;
327     }
328
329     
330     public int read( char[] cbuf,
331              int off,
332              int len ){
333         
334         if(wroteLength >= total)
335         return -1;
336         
337         int i;
338         for(i = off ;
339         i < off + len &&
340             wroteLength < total ;
341         i++, wroteLength ++){
342         
343         cbuf[i] = (char) (wroteLength % 0x10000L);
344         
345         }
346
347         return i - off;
348     }
349     
350     }
351     
352     
353     static class BlobTester {
354     
355     final String JavaDoc tableName;
356     final String JavaDoc colName;
357     
358     
359     BlobTester(String JavaDoc tableName,
360            String JavaDoc colName){
361         
362         this.tableName = tableName;
363         this.colName = colName;
364         
365     }
366     
367     
368     public void testGetStreamTwice(Connection JavaDoc conn)
369         throws SQLException JavaDoc, IOException JavaDoc {
370         
371         Statement JavaDoc st = null;
372         ResultSet JavaDoc rs = null;
373         InputStream JavaDoc is = null;
374
375         try{
376         st = conn.createStatement();
377         
378         rs = st.executeQuery("select " +
379                      colName + " "+
380                      "from " +
381                      tableName);
382         rs.next();
383         
384         System.out.println("get stream from " + tableName + "." + colName + " ...");
385         is = rs.getBinaryStream(1);
386         is.close();
387         
388         System.out.println("get stream from " + tableName + "." + colName + " again ...");
389         is = rs.getBinaryStream(1);
390         
391         System.out.println("Expected exception did not happen.");
392         
393         }catch(SQLException JavaDoc e){ acknowledgeException( e ); }
394         finally{
395         if( st != null )
396             st.close();
397         
398         if( rs != null )
399             rs.close();
400         
401         if( is != null )
402             is.close();
403         
404         }
405     }
406     }
407
408     private static void acknowledgeException( Throwable JavaDoc e )
409     {
410         String JavaDoc message = e.getMessage();
411
412         // Get rid of network and jdk-specific canons by stripping off
413
// introductory exception class name
414
if ( message.startsWith( "java.sql." ) )
415         {
416             int boilerplateEndIndex = message.indexOf( ": " );
417             message = message.substring( boilerplateEndIndex + 2, message.length() );
418         }
419         
420         System.out.println("Expected exception may happen.");
421         System.out.println( e.getMessage() );
422     }
423
424     static class ClobTester {
425     
426     final String JavaDoc tableName;
427     final String JavaDoc colName;
428
429     public ClobTester( String JavaDoc tableName ,
430                String JavaDoc colName ){
431         
432         this.tableName = tableName;
433         this.colName = colName;
434         
435     }
436     
437     
438     public void testGetReaderTwice( Connection JavaDoc conn )
439         throws SQLException JavaDoc, IOException JavaDoc {
440         
441         Statement JavaDoc st = null;
442         ResultSet JavaDoc rs = null;
443         Reader JavaDoc reader = null;
444
445         try{
446         st = conn.createStatement();
447         
448         rs = st.executeQuery( "select " +
449                       colName + " " +
450                       "from " +
451                       tableName );
452         rs.next();
453         
454         System.out.println("get reader from " + tableName + "." + colName + " ...");
455         reader = rs.getCharacterStream(1);
456         reader.close();
457         
458         System.out.println("get reader from " + tableName + "." + colName + "again ...");
459         reader = rs.getCharacterStream(1);
460         
461         System.out.println("Expected exception did not happen.");
462         
463         }catch(SQLException JavaDoc e){ acknowledgeException( e ); }
464         finally{
465         if(st != null)
466             st.close();
467         
468         if(rs != null)
469             rs.close();
470
471         if(reader != null)
472             reader.close();
473
474         }
475     }
476     }
477 }
478
Popular Tags