KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.resultsetStream
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.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Types JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32
33 import org.apache.derby.tools.ij;
34 import org.apache.derby.tools.JDBCDisplayUtil;
35 import org.apache.derbyTesting.functionTests.util.TestUtil;
36
37 import java.io.InputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.File JavaDoc;
40 import java.io.FileInputStream JavaDoc;
41 import java.io.BufferedInputStream JavaDoc;
42 import java.util.zip.CRC32 JavaDoc;
43 import java.io.Reader JavaDoc;
44 import java.io.StringReader JavaDoc;
45
46 /**
47  * Test of JDBC result set Stream calls.
48  *
49  * @author djd
50  */

51
52
53 public class resultsetStream {
54     
55     public static void main(String JavaDoc[] args) {
56         Connection JavaDoc con;
57         ResultSetMetaData JavaDoc met;
58         ResultSet JavaDoc rs;
59         Statement JavaDoc stmt;
60
61         System.out.println("Test resultsetStream starting");
62
63         try
64         {
65             // use the ij utility to read the property file and
66
// make the initial connection.
67
ij.getPropertyArg(args);
68             con = ij.startJBMS();
69
70             stmt = con.createStatement();
71
72             stmt.execute("create table t2 (len int, data LONG VARCHAR FOR BIT DATA)");
73             PreparedStatement JavaDoc ppw = con.prepareStatement(
74                 "insert into t2 (len, data) values (?, ?)");
75             String JavaDoc filePath = "extin";
76             String JavaDoc sep = System.getProperty("file.separator");
77             boolean exists = (new File JavaDoc("extin", "littleclob.utf")).exists();
78             if (!exists)
79             {
80                 String JavaDoc userDir = System.getProperty("user.dir");
81                     filePath = userDir + sep + ".." + sep + filePath;
82             }
83             File JavaDoc file = new File JavaDoc(filePath + sep + "littleclob.utf");
84             int fileSize = (int) file.length();
85             BufferedInputStream JavaDoc fileData = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
86             ppw.setInt(1, fileSize);
87             ppw.setBinaryStream(2, fileData, fileSize);
88             ppw.executeUpdate();
89
90             file = new File JavaDoc(filePath + sep + "short.utf");
91             fileSize = (int) file.length();
92             fileData = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
93             ppw.setInt(1, fileSize);
94             ppw.setBinaryStream(2, fileData, fileSize);
95             ppw.executeUpdate();
96             // null binary value
97
ppw.setInt(1, -1);
98             ppw.setBinaryStream(2, (java.io.InputStream JavaDoc) null, 0);
99             ppw.executeUpdate();
100
101             // value copied over from original Java object test.
102
File JavaDoc rssg = new java.io.File JavaDoc(filePath + sep + "resultsetStream.gif");
103             int rssgLength = (int) rssg.length();
104             ppw.setInt(1, (int) rssgLength);
105             ppw.setBinaryStream(2, new FileInputStream JavaDoc(rssg), rssgLength);
106             ppw.executeUpdate();
107
108             // try binary stream processing on a known file.
109
rs = stmt.executeQuery("select data from t2 where len = "
110                                     + rssgLength);
111             met = rs.getMetaData();
112             System.out.println("getColumnCount(): "+ met.getColumnCount());
113             while (rs.next())
114             {
115                 // JDBC columns use 1-based counting
116

117                 // get the first column as a stream
118
try {
119
120                     InputStream JavaDoc is = rs.getBinaryStream(1);
121                     if (is == null) {
122                         System.out.println("FAIL - getBinaryStream() return null");
123                         break;
124                     }
125
126                     // read the first 200 bytes from the stream and checksum them
127
byte[] b200 = new byte[200];
128
129                     // no guaratees to read all 200 bytes in one read call.
130
int count = 0;
131
132                     while (count < 200) {
133                         int r = is.read(b200, count, 200-count);
134                         if (r == -1)
135                             break;
136                         count += r;
137                     }
138
139                     if (count != 200){
140                         System.out.println("FAIL - failed to read 200 bytes from known file");
141                         break;
142                     }
143
144                     CRC32 JavaDoc cs = new CRC32 JavaDoc();
145
146                     cs.reset();
147                     cs.update(b200);
148
149                     System.out.println("Checksum of first 200 bytes " + cs.getValue());
150
151                     count = 200;
152                     for (; is.read() != -1; count++) {
153                     }
154                     System.out.println("Size of file = " + count);
155                     is.close();
156                 }
157                 catch (Throwable JavaDoc e) {
158                     System.out.println(
159                         "FAIL - exection while processing valid file");
160                     if (e instanceof SQLException JavaDoc)
161                     JDBCDisplayUtil.ShowSQLException(System.out, (SQLException JavaDoc)e);
162                 }
163             }
164             rs.close();
165
166             // check the stream is closed once another get call is made.
167
rs = stmt.executeQuery("select data, len from t2 where len = "
168                                     + rssgLength);
169             met = rs.getMetaData();
170             System.out.println("getColumnCount(): "+ met.getColumnCount());
171             while (rs.next())
172             {
173                 // JDBC columns use 1-based counting
174

175                 // get the first column as a stream
176
try {
177
178                     InputStream JavaDoc is = rs.getBinaryStream(1);
179                     if (is == null) {
180                         System.out.println("FAIL - getBinaryStream() return null");
181                         break;
182                     }
183
184                     // read the first 200 bytes from the stream and checksum them
185
byte[] b200 = new byte[200];
186
187                     // no guaratees to read all 200 bytes in one read call.
188
int count = 0;
189
190                     while (count < 200) {
191                         int r = is.read(b200, count, 200-count);
192                         if (r == -1)
193                             break;
194                         count += r;
195                     }
196
197                     if (count != 200){
198                         System.out.println("FAIL - failed to read 200 bytes from known file");
199                         break;
200                     }
201
202                     CRC32 JavaDoc cs = new CRC32 JavaDoc();
203
204                     cs.reset();
205                     cs.update(b200);
206
207                     System.out.println("Checksum of first 200 bytes " + cs.getValue());
208
209                     System.out.println("second columns is " + rs.getInt(2));
210
211                     System.out.println("FAILS DUE TO BUG 5710");
212                     try {
213                         is.read();
214                         System.out.println("FAIL - stream was not closed after a get*() call. " + is.getClass());
215                         break;
216                     } catch (IOException JavaDoc ioe) {
217                         // yes, stream should be closed
218
}
219                 }
220                 catch (Throwable JavaDoc e) {
221                     System.out.println(
222                         "FAIL - exection while processing valid file");
223                     if (e instanceof SQLException JavaDoc)
224                     JDBCDisplayUtil.ShowSQLException(System.out, (SQLException JavaDoc)e);
225                 }
226             }
227             rs.close();
228
229             // check a SQL null object gets a null stream
230
rs = stmt.executeQuery("select data from t2 where len = -1");
231             met = rs.getMetaData();
232             System.out.println("getColumnCount(): "+ met.getColumnCount());
233             while (rs.next())
234             {
235                 // JDBC columns use 1-based counting
236

237                 // get the first column as a stream
238

239                 InputStream JavaDoc is = rs.getBinaryStream(1);
240                 if (is != null) {
241                     System.out.println("FAIL - getBinaryStream() did not return null for SQL null");
242                     break;
243                 }
244
245             }
246             rs.close();
247
248             rs = stmt.executeQuery("select len, data from t2 where len = "
249                                     + fileSize);
250             rs.next();
251             fileSize = rs.getInt(1);
252             fileData = new BufferedInputStream JavaDoc(rs.getBinaryStream(2));
253             int readCount = 0;
254             while(true)
255             {
256                 int data = fileData.read();
257                 if (data == -1) break;
258                 readCount++;
259             }
260             fileData.close();
261             System.out.println("len=" + fileSize);
262             System.out.println("number of reads=" + readCount);
263
264             // check binary input streams of invalid length.
265
// JDBC 3.0 tutorial says stream contents must match length.
266

267             byte[] tooFew = new byte[234];
268
269             ppw.setInt(1, 234);
270             ppw.setBinaryStream(2, new java.io.ByteArrayInputStream JavaDoc(tooFew), 234); // matching length
271
ppw.executeUpdate();
272
273
274             ppw.setInt(1, 235);
275             ppw.setBinaryStream(2, new java.io.ByteArrayInputStream JavaDoc(tooFew), 235); // too few bytes in stream
276
try {
277                 ppw.executeUpdate();
278                 System.out.println("FAIL - execute with setBinaryStream() with too few bytes succeeded");
279             } catch (SQLException JavaDoc sqle) {
280                 org.apache.derbyTesting.functionTests.util.TestUtil.dumpSQLExceptions(sqle, true);
281             }
282
283             ppw.setInt(1, 233);
284             ppw.setBinaryStream(2, new java.io.ByteArrayInputStream JavaDoc(tooFew), 233); // too many bytes
285
try {
286                 ppw.executeUpdate();
287                 System.out.println("FAIL - execute with setBinaryStream() with too many bytes succeeded");
288             } catch (SQLException JavaDoc sqle) {
289                 org.apache.derbyTesting.functionTests.util.TestUtil.dumpSQLExceptions(sqle, true);
290             }
291
292
293             ppw.close();
294             rs.close();
295             
296             TestOfGetAsciiStream.executeTestOfGetAsciiStream(con);
297
298             cleanUp(stmt);
299             stmt.close();
300             con.close();
301
302         }
303         catch (SQLException JavaDoc e) {
304             dumpSQLExceptions(e);
305         }
306         catch (Throwable JavaDoc e) {
307             System.out.println("FAIL -- unexpected exception:" + e.toString());
308             e.printStackTrace();
309         }
310
311         System.out.println("Test resultsetStream finished");
312     }
313
314     static private void dumpSQLExceptions (SQLException JavaDoc se) {
315         System.out.println("FAIL -- unexpected exception: " + se.toString());
316         while (se != null) {
317             System.out.print("SQLSTATE("+se.getSQLState()+"):");
318             se = se.getNextException();
319         }
320     }
321     
322     private static void cleanUp(Statement JavaDoc stmt) throws SQLException JavaDoc {
323         String JavaDoc[] testObjects = { " table t3", "table t2" };
324         TestUtil.cleanUpTest(stmt, testObjects);
325     }
326     
327     static class TestOfGetAsciiStream {
328         
329         final static String JavaDoc TEST_STRING_DATA =
330             "ABCDEFG" +
331             "\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5" +
332             "\u00ff\u0100" +
333             "\u3042\u3044\u3046\u3048\u304a";
334                 
335         
336         static private void executeTestOfGetAsciiStream(Connection JavaDoc conn) throws SQLException JavaDoc {
337             
338             System.out.println("Test of getAsciiStream");
339             createTestTable(conn);
340             executeTestRun(conn);
341
342         }
343         
344
345         static private void createTestTable(Connection JavaDoc conn) throws SQLException JavaDoc {
346
347             PreparedStatement JavaDoc st = null;
348             
349             try{
350                 st = conn.prepareStatement("create table t3(text_data clob)");
351                 st.executeUpdate();
352                 
353             }finally{
354                 if(st != null)
355                     st.close();
356                 
357             }
358             
359         }
360         
361
362         static private void executeTestRun(Connection JavaDoc conn) throws SQLException JavaDoc {
363             
364             insertTestData(conn);
365             printTestData(conn);
366             
367         }
368
369
370         static private void insertTestData(Connection JavaDoc conn) throws SQLException JavaDoc {
371             
372             PreparedStatement JavaDoc st = null;
373             
374             try{
375                 
376                 st = conn.prepareStatement("insert into t3(text_data) values(?)");
377                 st.setCharacterStream(1,
378                               new StringReader JavaDoc(TEST_STRING_DATA),
379                               TEST_STRING_DATA.length());
380                 st.executeUpdate();
381                 
382             }finally{
383                 if(st != null)
384                     st.close();
385                 
386             }
387
388         }
389
390
391         static private void printTestData(Connection JavaDoc conn) throws SQLException JavaDoc {
392             
393             PreparedStatement JavaDoc st = null;
394             ResultSet JavaDoc rs = null;
395             
396             try{
397                 st = conn.prepareStatement("select " +
398                                "text_data as text_data_col1," +
399                                "text_data as text_data_col2 " +
400                                "from " +
401                                "t3");
402                 rs = st.executeQuery();
403                 
404                 while(rs.next()){
405                     printTestDataInARowViaStream(rs);
406                     printTestDataInARowViaReader(rs);
407                 }
408                 
409             }catch(IOException JavaDoc e){
410                 System.out.println("FAIL -- unexpected IOException: " + e.toString());
411                 e.printStackTrace();
412                 
413             }finally{
414                 if(rs != null){
415                     rs.close();
416                 }
417
418                 if(st != null){
419                     st.close();
420                 }
421                 
422             }
423         }
424         
425         
426         static private void printTestDataInARowViaStream(ResultSet JavaDoc rs) throws SQLException JavaDoc,
427                                               IOException JavaDoc{
428             
429             InputStream JavaDoc is = null;
430
431             try{
432                 is = rs.getAsciiStream(1);
433             
434                 for(int c = is.read();
435                     c > -1;
436                     c = is.read()){
437                     
438                     System.out.print(getCharacterCodeString((char) c));
439                 }
440                 
441                 System.out.println();
442                 
443             }finally{
444                 if(is != null)
445                     is.close();
446             }
447                 
448         }
449
450
451         static private void printTestDataInARowViaReader(ResultSet JavaDoc rs) throws SQLException JavaDoc,
452                                               IOException JavaDoc{
453
454             Reader JavaDoc reader = null;
455             
456             try{
457                 reader = rs.getCharacterStream(2);
458
459                 for(int c = reader.read();
460                     c > -1;
461                     c = reader.read()){
462                     
463                     System.out.print(getCharacterCodeString((char) c));
464
465                 }
466             
467                 System.out.println();
468                 
469             }finally{
470                 if(reader != null){
471                     reader.close();
472                 }
473             }
474             
475         }
476         
477         
478         private static String JavaDoc getCharacterCodeString(char c){
479
480             String JavaDoc hexString = Integer.toHexString((int) c);
481             
482             while(hexString.length() < 4){
483                 hexString = "0" + hexString;
484             }
485             
486             return "U+" + hexString;
487         }
488         
489     }
490
491 }
492
Popular Tags