KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > largedata > LobLimits


1 /*
2
3 Derby - Class org.apache.derbyTesting.functionTests.tests.largedata.LobLimits
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.largedata;
23
24 import java.sql.*;
25 import java.io.*;
26
27 import org.apache.derby.tools.ij;
28 import org.apache.derby.tools.JDBCDisplayUtil;
29
30 /**
31 * This test is part of the "largedata" suite because this test tests data for
32 * lobs to the limits ( ie blob and clob can be 2G-1 maximum) and so this test
33 * may take considerable disk space as well as time to run. Hence it is not part
34 * of the derbyall suite but should ideally be run at some intervals to test if
35 * no regression has occurred.
36 */

37
38 public class LobLimits {
39
40    static boolean trace = false;
41    static final int _2GB = 2 * 1024 * 1024* 1024 - 1;
42    static final int _100MB = 100 * 1024 * 1024;
43    static final int MORE_DATA_THAN_COL_WIDTH= (_100MB)+1;
44    static final int NUM_TRAILING_SPACES = 33*1024;
45    
46    static PreparedStatement insertBlob = null;
47    static PreparedStatement selectBlob = null;
48    static PreparedStatement insertClob = null;
49    static PreparedStatement selectClob = null;
50    static PreparedStatement deleteBlob = null;
51    static PreparedStatement deleteClob = null;
52    static PreparedStatement insertBlob2 = null;
53    static PreparedStatement selectBlob2 = null;
54    static PreparedStatement insertClob2 = null;
55    static PreparedStatement selectClob2 = null;
56    static PreparedStatement deleteBlob2 = null;
57    static PreparedStatement deleteClob2 = null;
58
59    static final String JavaDoc DATAFILE = "byteLobLimits.dat";
60
61    static final String JavaDoc CHARDATAFILE = "charLobLimits.txt";
62
63    /**
64     * setup prepared statements and schema for the tests
65     * @param conn
66     * @throws SQLException
67     */

68    private void setup(Connection conn) throws SQLException {
69        System.out.println("-----------------------------------");
70        System.out.println(" START setup");
71
72        conn.setAutoCommit(true);
73        // Create a test table.
74
Statement s = conn.createStatement();
75        try {
76            s.execute("DROP TABLE BLOBTBL");
77        } catch (Exception JavaDoc e) {
78        }
79        try {
80            s.execute("DROP TABLE CLOBTBL");
81        } catch (Exception JavaDoc e) {
82        }
83        try {
84            s.execute("DROP TABLE BLOBTBL2");
85        } catch (Exception JavaDoc e) {
86        }
87        try {
88            s.execute("DROP TABLE CLOBTBL2");
89        } catch (Exception JavaDoc e) {
90        }
91
92        s.execute("CREATE TABLE BLOBTBL (ID INT NOT NULL PRIMARY KEY, "
93                + "POS BIGINT, DLEN BIGINT, CONTENT BLOB(2G))");
94
95        insertBlob = conn
96                .prepareStatement("INSERT INTO BLOBTBL values (?,?,?,?)");
97
98        s.execute("CREATE TABLE CLOBTBL (ID INT NOT NULL PRIMARY KEY,"
99                + "POS BIGINT, DLEN BIGINT, CONTENT CLOB(2G))");
100
101        insertBlob = conn
102                .prepareStatement("INSERT INTO BLOBTBL values (?,?,?,?)");
103        selectBlob = conn
104                .prepareStatement("SELECT CONTENT,DLEN FROM BLOBTBL WHERE ID = ?");
105
106        insertClob = conn
107                .prepareStatement("INSERT INTO CLOBTBL values (?,?,?,?)");
108
109        selectClob = conn
110                .prepareStatement("SELECT CONTENT,DLEN FROM CLOBTBL WHERE ID = ?");
111
112        deleteBlob = conn.prepareStatement("DELETE FROM BLOBTBL");
113        deleteClob = conn.prepareStatement("DELETE from CLOBTBL");
114
115        s.execute("CREATE TABLE BLOBTBL2 (ID INT NOT NULL PRIMARY KEY, "
116                + "POS BIGINT, CONTENT BLOB("+_100MB+"),DLEN BIGINT)");
117
118        insertBlob2 = conn
119                .prepareStatement("INSERT INTO BLOBTBL2 values (?,?,?,?)");
120
121        // Please dont change the clob column width,since tests use this width to
122
// test for truncation of trailing spaces.
123
s.execute("CREATE TABLE CLOBTBL2 (ID INT NOT NULL PRIMARY KEY,"
124                + "POS BIGINT, CONTENT CLOB("+_100MB+"), DLEN BIGINT)");
125
126        insertBlob2 = conn
127                .prepareStatement("INSERT INTO BLOBTBL2 values (?,?,?,?)");
128        selectBlob2 = conn
129                .prepareStatement("SELECT CONTENT,DLEN FROM BLOBTBL2 WHERE ID = ?");
130
131        insertClob2 = conn
132                .prepareStatement("INSERT INTO CLOBTBL2 values (?,?,?,?)");
133
134        selectClob2 = conn
135                .prepareStatement("SELECT CONTENT,DLEN FROM CLOBTBL2 WHERE ID = ?");
136        System.out.println("-----------------------------------");
137        System.out.println(" END setup");
138
139        deleteBlob2 = conn.prepareStatement("DELETE FROM BLOBTBL2");
140        deleteClob2 = conn.prepareStatement("DELETE from CLOBTBL2");
141        System.out.println("-----------------------------------");
142        System.out.println(" END setup");
143
144    }
145
146    /**
147     * Create an instance of this class and do the test.
148     */

149    public static void main(String JavaDoc[] args) {
150        //trace = Boolean.getBoolean("trace");
151
new LobLimits().runTests(args);
152
153    }
154
155    /**
156     * Create a JDBC connection using the arguments passed in from the harness,
157     * and then run the LOB tests.
158     *
159     * @param args
160     * Arguments from the harness.
161     */

162    public void runTests(String JavaDoc[] args) {
163        Connection conn = null;
164        try {
165
166            // use the ij utility to read the property file and
167
// make the initial connection.
168
ij.getPropertyArg(args);
169            conn = ij.startJBMS();
170
171            // do the initial setup,drop and create tables
172
// prepare stmts
173
setup(conn);
174
175            conn.setAutoCommit(false);
176
177            clobTests(conn);
178            blobTests(conn);
179            //cleanup
180
cleanup(conn);
181        } catch (Exception JavaDoc e) {
182            System.out.println("FAIL -- Unexpected exception:");
183            e.printStackTrace(System.out);
184        }
185    }
186
187    /**
188     * Close all prepared statements and connection
189     * @param conn
190     * @throws Exception
191     */

192    private void cleanup(Connection conn) throws Exception JavaDoc {
193        insertBlob.close();
194        selectBlob.close();
195        selectClob.close();
196        insertClob.close();
197        deleteClob.close();
198        deleteBlob.close();
199        insertBlob2.close();
200        selectBlob2.close();
201        selectClob2.close();
202        insertClob2.close();
203        deleteBlob2.close();
204        deleteClob2.close();
205        conn.close();
206        new File(DATAFILE).delete();
207        new File(CHARDATAFILE).delete();
208    }
209
210    /**
211     * tests specific for blobs
212     * @param conn
213     * @throws Exception
214     */

215    private static void blobTests(Connection conn) throws Exception JavaDoc {
216
217        try {
218            // Test - 2Gb blob ( actually it is 2gb -1)
219
// Note with setBinaryStream interface the maximum size for the
220
// stream, can be max value for an int.
221
// Also note, that lobs in derby currently supports
222
// maximum size of 2gb -1
223

224            // first do insert blob of 2g, 2 rows
225
insertBlob_SetBinaryStream("BlobTest #1", conn, insertBlob, _2GB,
226                    0, 2, _2GB);
227            // do a select to see if the inserts in test above went ok
228
selectBlob("BlobTest #2", conn, selectBlob, _2GB, 0, 1);
229            selectBlob("BlobTest #3", conn, selectBlob, _2GB, 1, 1);
230            
231            // now do a select of one of the 2gb rows and update another 2g row
232
// using the setBlob api, updated blob is of length 2gb
233
// Fix for Bug entry -DERBY-599[setBlob should not materialize blob
234
// into memory]
235
selectUpdateBlob("BlobTest #4",conn,selectBlob,_2GB,0,1,1);
236            // select row from blobtbl and then do insert into the blobtbl
237
// using setBlob
238
selectInsertBlob("BlobTest #4.1",conn,selectBlob,insertBlob,_2GB,0,3,1);
239                               
240            // Test - generate random data, write to a file, use it to insert
241
// data into blob and then read back and compare if all is ok
242
// currently in fvt ( derbyall), tests check for substrings etc and
243
// for small amounts of data. This test will test for 100mb of blob data
244

245            FileOutputStream fos = new FileOutputStream(DATAFILE);
246            RandomByteStream r = new RandomByteStream(new java.util.Random JavaDoc(),
247                    _100MB);
248            // write in chunks of 32k buffer
249
byte[] buffer = new byte[32 * 1024];
250            int count = 0;
251            
252            while((count=r.read(buffer))>=0)
253                fos.write(buffer,0,count);
254
255            fos.flush();
256            fos.close();
257
258            insertBlob2("BlobTest #5.1 ", conn, insertBlob2, _100MB, 0, 1,
259                    _100MB, DATAFILE);
260            selectBlob2("BlobTest #5.2 ", conn, selectBlob2, _100MB, 0, 1,
261                    DATAFILE);
262            
263            
264            // update the 2gb row in blobtbl with the 100mb data and compare if the update
265
// went ok.
266
selectUpdateBlob2("BlobTest #6",conn,selectBlob2,selectBlob,_100MB,0,1,1,DATAFILE);
267                        
268            deleteTable(conn, deleteBlob2, 1);
269            
270        } catch (Exception JavaDoc e) {
271            System.out.println("FAIL -- Unexpected exception:");
272            e.printStackTrace(System.out);
273        }
274
275        conn.commit();
276        
277        deleteTable(conn, deleteBlob, 3);
278
279        // Negative Test, use setBlob api to insert a 4GB blob.
280
long _4GB = 4*1024*1024*(1024L);
281        BlobImpl _4GbBlob = new BlobImpl(new RandomByteStream(new java.util.Random JavaDoc(),_4GB),_4GB);
282
283        try
284        {
285            insertBlob_SetBlob("BlobTest #7 (setBlob with 4Gb blob",conn,insertBlob,_4GbBlob,
286                    _4GB,0,1,0);
287        }
288        catch(SQLException sqle)
289        {
290            System.out.println("DERBY DOES NOT SUPPORT INSERT OF 4GB BLOB ");
291            expectedException(sqle);
292        }
293        // ADD NEW TESTS HERE
294
}
295
296    /**
297     * tests using clobs
298     * @param conn
299     * @throws Exception
300     */

301    private static void clobTests(Connection conn) throws Exception JavaDoc {
302        try {
303            // Test - 2Gb blob
304
// Note with setCharacterStream interface the maximum size for the
305
// stream has to be max value for a int which is (2GB -1 )
306
// first do insert clob of 2g, 2 rows
307
insertClob_SetCharacterStream("ClobTest #1", conn, insertClob,
308                    _2GB, 0, 2, _2GB);
309            // do a select to see if the inserts in test above went ok
310
selectClob("ClobTest #2", conn, selectClob, _2GB, 0, 1);
311            selectClob("ClobTest #3", conn, selectClob, _2GB, 0, 1);
312            // do a select and then update a row of 2gb size: uses getClob
313
selectUpdateClob("ClobTest #4",conn,selectClob,_2GB,0,1,1);
314            
315
316            // Test - generate random data, write to a file, use it to insert
317
// data into clob and then read back and compare if all is ok
318
// currently in fvt ( derbyall), tests check for substrings etc and
319
// for small amounts of data. This test will test for 100mb of clob data
320
writeToFile(CHARDATAFILE,new RandomCharReader(new java.util.Random JavaDoc(),_100MB));
321            insertClob2("ClobTest #5.1 ", conn, insertClob2, _100MB, 0, 1,
322                    _100MB, CHARDATAFILE);
323            selectClob2("ClobTest #5.2 ", conn, selectClob2, _100MB, 0, 1,
324                    CHARDATAFILE);
325
326            // Disabled for now, this will materialize, will open
327
// jira for it.
328
//updateClob2("ClobTest #8.1",conn,selectClob,_100MB,0,0,10,1,CHARDATAFILE);
329

330            // update the 2gb row in clobtbl with the 100mb data and compare if the update
331
// went ok.
332
selectUpdateClob2("ClobTest #8.2",conn,selectClob2,selectClob,_100MB,0,1,1,CHARDATAFILE);
333
334            // test for trailing space truncation
335
// insert 100mb+33k of data which has 33k of trailing space,
336
// into a column of 100mb
337
// insert should be successful, select should retrieve 100mb of data
338

339            // Generate random data and write to a file, this file will be used
340
// in the verification process after inserts and updates.
341
writeToFile(CHARDATAFILE,new RandomCharReader(new java.util.Random JavaDoc(),
342                    (NUM_TRAILING_SPACES +_100MB),NUM_TRAILING_SPACES));
343            insertClob2("ClobTest #6.1 ", conn, insertClob2,_100MB, 3, 1,
344                    (NUM_TRAILING_SPACES +_100MB), CHARDATAFILE);
345            // select will retrieve data and verify the data inserted.
346
selectClob2("ClobTest #6.2 ", conn, selectClob2, _100MB, 3, 1,
347                    CHARDATAFILE);
348
349            negativeSpaceTruncationTest("ClobTest #7",conn);
350            
351            // Test - for stream contains a trailing non-space character
352
// insert should throw an error
353
writeToFile(CHARDATAFILE,new RandomCharReader(new java.util.Random JavaDoc(),MORE_DATA_THAN_COL_WIDTH));
354            try
355            {
356                insertClob2("ClobTest #9.1 ", conn, insertClob2,MORE_DATA_THAN_COL_WIDTH, 4, 1,
357                        MORE_DATA_THAN_COL_WIDTH, CHARDATAFILE);
358            }catch(SQLException sqle)
359            {
360                System.out.println("NEGATIVE TEST - Expected Exception: truncation of non-blanks not allowed");
361                expectedException(sqle);
362            }
363            // no row must be retrieved.
364
selectClob2("ClobTest #9.2 ", conn, selectClob2, _100MB, 4, 0,
365                    CHARDATAFILE);
366
367            try
368            {
369                insertClob2("ClobTest #10 ", conn, insertClob2,MORE_DATA_THAN_COL_WIDTH, 4, 1,
370                        MORE_DATA_THAN_COL_WIDTH +1 , CHARDATAFILE);
371            }catch(SQLException sqle)
372            {
373                System.out.println("NEGATIVE TEST - Expected Exception: truncation of non-blanks not allowed and"+
374                        " stream length is one greater than actual length of the stream ");
375                expectedException(sqle);
376            }
377
378            try
379            {
380                insertClob2("ClobTest #11 ", conn, insertClob2,MORE_DATA_THAN_COL_WIDTH, 4, 1,
381                        MORE_DATA_THAN_COL_WIDTH -1 , CHARDATAFILE);
382            }catch(SQLException sqle)
383            {
384                System.out.println("NEGATIVE TEST - Expected Exception: truncation of non-blanks not allowed and"+
385                        " stream length is one less than actual length of the stream ");
386                expectedException(sqle);
387            }
388            deleteTable(conn, deleteClob2, 2);
389        } catch (Exception JavaDoc e) {
390            System.out.println("FAIL -- Unexpected exception:");
391            e.printStackTrace(System.out);
392        }
393
394        try {
395            // give -ve streamlength
396
insertClob_SetCharacterStream("ClobTest #12.1", conn, insertClob,
397                    _100MB, 4, 1, -1);
398        } catch (SQLException sqle) {
399            System.out.println("NEGATIVE TEST - Expected Exception:");
400            expectedException(sqle);
401        }
402
403        selectClob("ClobTest #12.2", conn, selectClob,_100MB, 4, 0);
404
405        deleteTable(conn, deleteClob, 2);
406        
407        // Negative tests use the setClob API to insert a 4GB clob
408

409        long _4GB = 4*1024*1024*(1024L);
410
411        ClobImpl _4GBClob = new ClobImpl(new RandomCharReader(new java.util.Random JavaDoc(),_4GB),_4GB);
412
413        try
414        {
415            insertClob_SetClob("ClobTest #13 (setClob with 4Gb clob",conn,insertClob,_4GBClob,
416                    _4GB,0,1,0);
417        }
418        catch(SQLException sqle)
419        {
420            System.out.println("DERBY DOES NOT SUPPORT INSERT OF 4GB CLOB ");
421            expectedException(sqle);
422        }
423
424        // ADD NEW TESTS HERE
425
}
426
427    private static void negativeSpaceTruncationTest(String JavaDoc msg,Connection conn)
428        throws Exception JavaDoc
429    {
430        // Negative test, stream has trailing spaces but the stream length is one
431
// more than the actual length of the stream
432
try
433        {
434            insertClob2(msg, conn, insertClob2,_100MB, 4, 1,
435                (NUM_TRAILING_SPACES +_100MB - 1), CHARDATAFILE);
436        }catch(SQLException sqle)
437        {
438            System.out.println("EXPECTED EXCEPTION - stream has trailing spaces,but stream "+
439                    " length is 1 less than actual length of stream");
440            expectedException(sqle);
441        }
442
443        try
444        {
445            insertClob2(msg, conn, insertClob2,_100MB, 5, 1,
446                (NUM_TRAILING_SPACES +_100MB + 1), CHARDATAFILE);
447        }catch(SQLException sqle)
448        {
449            System.out.println("EXPECTED EXCEPTION - stream has trailing spaces,but stream "+
450                    " length is 1 greater than actual length of stream");
451            expectedException(sqle);
452        }
453    }
454    
455  
456    /**
457     * insert blob
458     * @param bloblen length of blob to insert
459     * @param start start id value for insert
460     * @param rows insert rows number of rows
461     * @param streamLength stream length passed to setBinaryStream(,,length)
462     */

463    private static void insertBlob_SetBinaryStream(String JavaDoc testId,
464            Connection conn, PreparedStatement ps, int bloblen, int start,
465            int rows, int streamLength) throws SQLException {
466        System.out.println("========================================");
467        System.out.println("START " + testId + "insertBlob of size = "
468                + bloblen);
469        long ST = 0;
470        if (trace)
471            ST = System.currentTimeMillis();
472
473        int count = 0;
474        java.util.Random JavaDoc random = new java.util.Random JavaDoc();
475        for (int i = start; i < start + rows; i++) {
476            ps.setInt(1, i);
477            ps.setInt(2, 0);
478            ps.setLong(3, bloblen);
479            ps.setBinaryStream(4, new RandomByteStream(random, bloblen),
480                    streamLength);
481            count += ps.executeUpdate();
482        }
483        conn.commit();
484        if (trace) {
485            System.out.println("Insert Blob (" + bloblen + ")" + " rows= "
486                    + count + " = " + (long) (System.currentTimeMillis() - ST));
487
488        }
489        verifyTest(count, rows, " Rows inserted with blob of size (" + bloblen
490                + ") =");
491        System.out.println("========================================");
492
493    }
494
495    /**
496     * insert blob, using a setBlob api.
497     * @param bloblen
498     * length of blob to insert
499     * @param blob
500     * blob to insert
501     * @param start
502     * start id value for insert
503     * @param rows
504     * insert rows number of rows
505     * @param expectedRows
506     * rows expected to be inserted
507     */

508     private static void insertBlob_SetBlob(String JavaDoc testId, Connection conn,
509             PreparedStatement ps, java.sql.Blob JavaDoc blob, long bloblen, int start,
510             int rows, int expectedRows) throws SQLException {
511         System.out.println("========================================");
512         System.out.println("START " + testId + "insertBlob of size = "
513                 + bloblen);
514         long ST = 0;
515         if (trace)
516             ST = System.currentTimeMillis();
517         int count = 0;
518
519         try {
520             
521             for (int i = start; i < start + rows; i++) {
522                 ps.setInt(1, i);
523                 ps.setInt(2, 0);
524                 ps.setLong(3, bloblen);
525                 ps.setBlob(4, blob);
526                 count += ps.executeUpdate();
527             }
528             conn.commit();
529             if (trace) {
530                 System.out.println("Insert Blob (" + bloblen + ")" + " rows= "
531                         + count + " = "
532                         + (long) (System.currentTimeMillis() - ST));
533
534             }
535         } catch (SQLException e) {
536             verifyTest(count, expectedRows,
537                     " Rows inserted with blob of size (" + bloblen + ") =");
538             System.out.println("========================================");
539             throw e;
540         }
541
542         verifyTest(count, expectedRows,
543                 " Rows inserted with blob of size (" + bloblen + ") =");
544         System.out.println("========================================");
545
546     }
547
548
549    /**
550     * select from blob table (BLOBTBL)
551     * @param bloblen select expects to retrieve a blob of this length
552     * @param id id of the row to retrieve
553     * @param expectedRows number of rows expected to match id
554     */

555    private static void selectBlob(String JavaDoc testId, Connection conn,
556            PreparedStatement ps, int bloblen, int id, int expectedRows)
557            throws SQLException {
558        System.out.println("========================================");
559        System.out.println("START " + testId + " - SELECT BLOB of size = "
560                + bloblen);
561
562        long ST = 0;
563        ResultSet rs = null;
564
565        if (trace)
566            ST = System.currentTimeMillis();
567
568        int count = 0;
569        ps.setInt(1, id);
570        rs = ps.executeQuery();
571
572        while (rs.next()) {
573            count++;
574            Blob value = rs.getBlob(1);
575            long l = value.length();
576            long dlen = rs.getLong(2);
577            if (dlen != l) {
578                System.out.println("FAIL - MISMATCH LENGTHS GOT " + l
579                        + " expected " + dlen + " for row in BLOBTBL with ID="
580                        + id);
581            }
582        }
583        conn.commit();
584
585        verifyTest(count, expectedRows,
586                "Matched rows selected with blob of size(" + bloblen + ") =");
587
588        if (trace) {
589            System.out.println("Select Blob (" + bloblen + ")" + " rows= "
590                    + expectedRows + " = "
591                    + (long) (System.currentTimeMillis() - ST));
592        }
593        System.out.println("========================================");
594    }
595
596    /**
597     * insert blob into BLOBTBL2
598     * @param bloblen length of blob to insert
599     * @param start id value for insert
600     * @param rows insert rows number of rows
601     * @param streamLength stream length passed to setBinaryStream(,,length)
602     * @param file filename to match retrieved data against
603     */

604
605    private static void insertBlob2(String JavaDoc testId, Connection conn,
606            PreparedStatement ps, int bloblen, int start, int rows,
607            int streamLength, String JavaDoc file) throws Exception JavaDoc {
608        System.out.println("========================================");
609        System.out.println("START " + testId + "insert Blob of size = "
610                + bloblen);
611        int count = 0;
612        java.util.Random JavaDoc random = new java.util.Random JavaDoc();
613        FileInputStream fis = null;
614
615        long ST = 0;
616        if (trace)
617            ST = System.currentTimeMillis();
618
619        for (int i = start; i < start + rows; i++) {
620            fis = new FileInputStream(file);
621            ps.setInt(1, i);
622            ps.setInt(2, 0);
623            ps.setLong(4, bloblen);
624            ps.setBinaryStream(3, fis, streamLength);
625            count += ps.executeUpdate();
626            fis.close();
627        }
628        conn.commit();
629        if (trace) {
630            System.out.println("Insert Blob (" + bloblen + ")" + " rows= "
631                    + count + " = " + (long) (System.currentTimeMillis() - ST));
632
633        }
634        verifyTest(count, rows, " Rows inserted with blob of size (" + bloblen
635                + ") =");
636        System.out.println("========================================");
637
638    }
639
640    /**
641     * select from blob table (BLOBTBL2)
642     * @param bloblen select expects to retrieve a blob of this length
643     * @param id id of the row to retrieve
644     * @param expectedRows number of rows expected to match id
645     * @param file name of the file,against which the retrieved data is
646     * compared
647     */

648    private static void selectBlob2(String JavaDoc testId, Connection conn,
649            PreparedStatement ps, int bloblen, int id, int expectedRows,
650            String JavaDoc file) throws Exception JavaDoc {
651        System.out.println("========================================");
652        System.out.println("START " + testId + " - SELECT BLOB of size = "
653                + bloblen);
654
655        long ST = 0;
656        ResultSet rs = null;
657
658        if (trace)
659            ST = System.currentTimeMillis();
660
661        int count = 0;
662        ps.setInt(1, id);
663        rs = ps.executeQuery();
664
665        while (rs.next()) {
666            count++;
667            Blob value = rs.getBlob(1);
668            long l = value.length();
669            long dlen = rs.getLong(2);
670            if (dlen != l) {
671                System.out.println("FAIL - MISMATCH LENGTHS GOT " + l
672                        + " expected " + dlen + " for row in BLOBTBL with ID="
673                        + id);
674            } else
675                compareBlobToFile(value.getBinaryStream(), file);
676        }
677        conn.commit();
678
679        verifyTest(count, expectedRows,
680                "Matched rows selected with blob of size(" + bloblen + ") =");
681
682        if (trace) {
683            System.out.println("Select Blob (" + bloblen + ")" + " rows= "
684                    + expectedRows + " = "
685                    + (long) (System.currentTimeMillis() - ST));
686        }
687        System.out.println("========================================");
688    }
689
690    /**
691     * Basically this test will do an update using setBlob api -
692     * select row from blobtbl and then update a row in blobtbl
693     * and verify updated data in blobtbl
694     * @param ps select statement from which blob is retrieved
695     * @param bloblen updating value is of length bloblen
696     * @param id id of the row retrieved, for the update
697     * @param updateId id of the row that is updated
698     * @param expectedRows to be updated
699     */

700    private static void selectUpdateBlob(String JavaDoc testId, Connection conn,
701            PreparedStatement ps, int bloblen, int id, int updateId,
702            int expectedRows) throws Exception JavaDoc {
703        System.out.println("========================================");
704        System.out.println("START " + testId + " - select and then update blob of size= "
705                + bloblen + " - Uses getBlob api");
706
707        ResultSet rs = null;
708
709        ps.setInt(1, id);
710        rs = ps.executeQuery();
711        rs.next();
712        Blob value = rs.getBlob(1);
713        long l = value.length();
714        long dlen = rs.getLong(2);
715        if (dlen != l) {
716            System.out
717                    .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected "
718                            + dlen + " for row in BLOBTBL with ID=" + id);
719        }
720
721        PreparedStatement psUpd = conn
722                .prepareStatement("update BLOBTBL set content=?,dlen =? where id = ?");
723        psUpd.setBlob(1,value);
724        psUpd.setLong(2, l);
725        psUpd.setInt(3, updateId);
726
727        System.out.println("Rows Updated = " + psUpd.executeUpdate());
728        conn.commit();
729
730        // now select and verify that update went through ok.
731
ps.setInt(1, updateId);
732        ResultSet rs2 = ps.executeQuery();
733        rs2.next();
734        Blob updatedValue = rs2.getBlob(1);
735
736        if(updatedValue.length() != l)
737            System.out.println("FAIL - Retrieving the updated blob length does not match "+
738                    "expected length = "+l +" found = "+ updatedValue.length());
739
740        // close resultsets
741
conn.commit();
742        rs.close();
743        rs2.close();
744        psUpd.close();
745        System.out.println("========================================");
746    }
747
748    /**
749     * Basically this test will do an insert using setBlob api -
750     * select row from blobtbl and then insert a row in blobtbl
751     * and verify updated data in blobtbl
752     * @param ps select statement from which blob is retrieved
753     * @param bloblen updating value is of length bloblen
754     * @param id id of the row retrieved, for the update
755     * @param insertId id of the row that is inserted
756     * @param expectedRows to be updated
757     */

758    private static void selectInsertBlob(String JavaDoc testId, Connection conn,
759            PreparedStatement ps,PreparedStatement ins, int bloblen, int id, int insertId,
760            int expectedRows) throws Exception JavaDoc {
761        System.out.println("========================================");
762        System.out.println("START " + testId + " - select and then insert blob of size= "
763                + bloblen + " - Uses getBlob api to do select and setBlob for insert");
764
765        ResultSet rs = null;
766
767        ps.setInt(1, id);
768        rs = ps.executeQuery();
769        rs.next();
770        Blob value = rs.getBlob(1);
771        long l = value.length();
772        long dlen = rs.getLong(2);
773        if (dlen != l) {
774            System.out
775                    .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected "
776                            + dlen + " for row in BLOBTBL with ID=" + id);
777        }
778        
779        ins.setInt(1,insertId);
780        ins.setInt(2,0);
781        ins.setLong(3,l);
782        ins.setBlob(4,value);
783        
784        System.out.println("Rows Updated = " + ins.executeUpdate());
785        conn.commit();
786
787        // now select and verify that update went through ok.
788
ps.setInt(1, insertId);
789        ResultSet rs2 = ps.executeQuery();
790        rs2.next();
791        Blob insertedValue = rs2.getBlob(1);
792
793        if(insertedValue.length() != l)
794            System.out.println("FAIL - Retrieving the updated blob length does not match "+
795                    "expected length = "+l +" found = "+ insertedValue.length());
796
797        // close resultsets
798
conn.commit();
799        rs.close();
800        rs2.close();
801        System.out.println("========================================");
802    }
803
804
805    /**
806     * Basically this test will do an update using setBinaryStream api and verifies the
807     * updated data. select row from blobtbl2 and then update a row in blobtbl
808     * and verify updated data in blobtbl
809     * @param bloblen updating value is of length bloblen
810     * @param id id of the row retrieved, for the update
811     * @param updateId id of the row that is updated
812     * @param expectedRows to be updated
813     * @param file name of the file,against which the updated data is
814     * compared
815     */

816    private static void selectUpdateBlob2(String JavaDoc testId, Connection conn,
817            PreparedStatement ps,PreparedStatement sel,int bloblen, int id, int updateId,
818            int expectedRows,String JavaDoc file) throws Exception JavaDoc {
819        System.out.println("========================================");
820        System.out.println("START " + testId + " - select and then update blob of size= "
821                + bloblen + " - Uses getBlob and setBlob api");
822
823        ResultSet rs = null;
824        
825        // retrieve row from blobtbl2
826
ps.setInt(1, id);
827        rs = ps.executeQuery();
828        rs.next();
829        Blob value = rs.getBlob(1);
830        long l = value.length();
831        long dlen = rs.getLong(2);
832        if (dlen != l) {
833            System.out
834                    .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected "
835                            + dlen + " for row in BLOBTBL2 with ID=" + id);
836        }
837
838        PreparedStatement psUpd = conn
839                .prepareStatement("update BLOBTBL set content=?,dlen =? where id = ?");
840        psUpd.setBlob(1,value);
841        psUpd.setLong(2, l);
842        psUpd.setInt(3, updateId);
843
844        System.out.println("Rows Updated = " + psUpd.executeUpdate());
845        conn.commit();
846
847        // now select and verify that update went through ok.
848
sel.setInt(1, updateId);
849        ResultSet rs2 = sel.executeQuery();
850        rs2.next();
851        Blob updatedValue = rs2.getBlob(1);
852        
853        if (updatedValue.length() != l)
854        {
855            System.out.println("FAIL - MISMATCH length of updated blob value : expected="+
856                    l+" found ="+updatedValue.length());
857        }
858        else
859            compareBlobToFile(updatedValue.getBinaryStream(),file);
860
861        // close resultsets
862
conn.commit();
863        rs.close();
864        rs2.close();
865        psUpd.close();
866        System.out.println("========================================");
867
868    }
869
870    private static void compareBlobToFile(InputStream lobstream, String JavaDoc filename)
871            throws Exception JavaDoc {
872        FileInputStream file = new FileInputStream(filename);
873        int l = 0;
874        int b = 0;
875        do {
876            l = lobstream.read();
877            b = file.read();
878            if (l != b)
879            {
880                System.out.println("FAIL -- MISMATCH in data stored versus"+
881                        "data retrieved");
882                break;
883            }
884        } while (l != -1 && b != -1);
885    }
886
887    private static void deleteTable(Connection conn, PreparedStatement ps,
888            int expectedRows) throws SQLException {
889        int count = ps.executeUpdate();
890        conn.commit();
891        verifyTest(count, expectedRows, "Rows deleted =");
892    }
893
894    
895    /**
896     * insert clob
897     * @param cloblen length of clob to insert
898     * @param start id value for insert
899     * @param rows insert rows number of rows
900     * @param streamLength stream length passed to setCharacterStream(...,length)
901     */

902    private static void insertClob_SetCharacterStream(String JavaDoc testId,
903            Connection conn, PreparedStatement ps, int cloblen, int start,
904            int rows, int streamLength) throws SQLException {
905        System.out.println("========================================");
906        System.out.println("START " + testId + " -insertClob of size = "
907                + cloblen);
908
909        long ST = 0;
910        java.util.Random JavaDoc random = new java.util.Random JavaDoc();
911        int count = 0;
912        if (trace)
913            ST = System.currentTimeMillis();
914
915        for (int i = start; i < start + rows; i++) {
916            ps.setInt(1, i);
917            ps.setInt(2, 0);
918            ps.setLong(3, cloblen);
919            ps.setCharacterStream(4, new RandomCharReader(random, cloblen),
920                    streamLength);
921            count += ps.executeUpdate();
922        }
923        conn.commit();
924        if (trace) {
925            System.out.println("Insert Clob (" + cloblen + ")" + " rows= "
926                    + count + " = " + (long) (System.currentTimeMillis() - ST));
927
928        }
929        verifyTest(count, rows, "Rows inserted with clob of size (" + cloblen
930                + ") = ");
931        System.out.println("========================================");
932
933    }
934
935   /**
936     * insert clob, using a setClob api.
937     * @param cloblen
938     * length of clob to insert
939     * @param clob
940     * clob to insert
941     * @param start
942     * start id value for insert
943     * @param rows
944     * insert rows number of rows
945     * @param expectedRows
946     * rows expected to be inserted
947    */

948     private static void insertClob_SetClob(String JavaDoc testId, Connection conn,
949             PreparedStatement ps, java.sql.Clob JavaDoc clob, long cloblen, int start,
950             int rows, int expectedRows) throws SQLException {
951         System.out.println("========================================");
952         System.out.println("START " + testId + "insertClob of size = "
953                 + cloblen);
954         long ST = 0;
955         if (trace)
956            ST = System.currentTimeMillis();
957         int count = 0;
958
959         try {
960             
961             for (int i = start; i < start + rows; i++) {
962                 ps.setInt(1, i);
963                 ps.setInt(2, 0);
964                 ps.setLong(3, cloblen);
965                 ps.setClob(4, clob);
966                 count += ps.executeUpdate();
967             }
968             conn.commit();
969             if (trace) {
970                 System.out.println("Insert Clob (" + cloblen + ")" + " rows= "
971                         + count + " = "
972                         + (long) (System.currentTimeMillis() - ST));
973
974             }
975         } catch (SQLException e) {
976             verifyTest(count, expectedRows,
977                     " Rows inserted with clob of size (" + cloblen + ") =");
978             System.out.println("========================================");
979             throw e;
980         }
981
982         verifyTest(count, expectedRows,
983                 " Rows inserted with clob of size (" + cloblen + ") =");
984         System.out.println("========================================");
985
986     }
987
988    /**
989     * select from clob table
990     * @param cloblen select expects to retrieve a clob of this length
991     * @param id id of the row to retrieve
992     * @param expectedRows number of rows expected to match id
993     */

994    private static void selectClob(String JavaDoc testId, Connection conn,
995            PreparedStatement ps, int cloblen, int id, int expectedRows)
996            throws SQLException {
997        System.out.println("========================================");
998        System.out.println("START " + testId + " - SELECT CLOB of size = "
999                + cloblen);
1000
1001       long ST = 0;
1002       int count = 0;
1003       ResultSet rs = null;
1004       if (trace)
1005           ST = System.currentTimeMillis();
1006
1007       ps.setInt(1, id);
1008       rs = ps.executeQuery();
1009       while (rs.next()) {
1010           count++;
1011           Clob value = rs.getClob(1);
1012           long l = value.length();
1013           long dlen = rs.getLong(2);
1014           if (dlen != l) {
1015               System.out.println("FAIL - MISMATCH LENGTHS GOT " + l
1016                       + " expected " + dlen + " for row in CLOBTBL with ID="
1017                       + id);
1018           }
1019
1020       }
1021       conn.commit();
1022       if (trace) {
1023           System.out.println("Select Clob (" + cloblen + ")" + " rows= "
1024                   + expectedRows + " = "
1025                   + (long) (System.currentTimeMillis() - ST));
1026
1027       }
1028
1029       verifyTest(count, expectedRows,
1030               "Matched rows selected with clob of size(" + cloblen + ") =");
1031       System.out.println("========================================");
1032
1033   }
1034
1035   /**
1036    * insert clob into CLOBTBL2
1037    * @param cloblen length of clob to insert
1038    * @param start id value for insert
1039    * @param rows insert rows number of rows
1040    * @param streamLength stream length passed to setCharacterStream(pos,reader,streamLength)
1041    * @param file name of the file that has data to be inserted
1042    */

1043   private static void insertClob2(String JavaDoc testId, Connection conn,
1044           PreparedStatement ps, int cloblen, int start, int rows,
1045           int streamLength, String JavaDoc file) throws Exception JavaDoc {
1046       System.out.println("========================================");
1047       System.out.println("START " + testId + "insert Clob of size = "
1048               + cloblen);
1049       int count = 0;
1050       FileReader reader = null;
1051       long ST = 0;
1052       if (trace)
1053           ST = System.currentTimeMillis();
1054
1055       for (int i = start; i < start + rows; i++) {
1056           reader = new FileReader(file);
1057           ps.setInt(1, i);
1058           ps.setInt(2, 0);
1059           ps.setLong(4, cloblen);
1060           ps.setCharacterStream(3, reader, streamLength);
1061           count += ps.executeUpdate();
1062           reader.close();
1063       }
1064       conn.commit();
1065       if (trace) {
1066           System.out.println("Insert Clob (" + cloblen + ")" + " rows= "
1067                   + count + " = " + (long) (System.currentTimeMillis() - ST));
1068
1069       }
1070       verifyTest(count, rows, " Rows inserted with clob of size (" + cloblen
1071               + ") =");
1072       System.out.println("========================================");
1073
1074   }
1075
1076   /**
1077    * select from clob table (CLOBTBL2)
1078    * @param cloblen select expects to retrieve a clob of this length
1079    * @param id id of the row to retrieve
1080    * @param expectedRows number of rows expected to match id
1081    * @param file filename to compare the retrieved data against
1082    */

1083   private static void selectClob2(String JavaDoc testId, Connection conn,
1084           PreparedStatement ps, int cloblen, int id, int expectedRows,
1085           String JavaDoc file) throws SQLException, Exception JavaDoc {
1086       System.out.println("========================================");
1087       System.out.println("START " + testId + " - SELECT CLOB of size = "
1088               + cloblen);
1089
1090       long ST = 0;
1091       ResultSet rs = null;
1092
1093       if (trace)
1094           ST = System.currentTimeMillis();
1095
1096       int count = 0;
1097       ps.setInt(1, id);
1098       rs = ps.executeQuery();
1099
1100       while (rs.next()) {
1101           count++;
1102           Clob value = rs.getClob(1);
1103           long l = value.length();
1104           long dlen = rs.getLong(2);
1105           if (cloblen != l) {
1106               System.out.println("FAIL - MISMATCH LENGTHS GOT " + l
1107                       + " expected " + dlen + " for row in CLOBTBL2 with ID="
1108                       + id);
1109           } else
1110               compareClobToFile(value.getCharacterStream(), file, cloblen);
1111       }
1112       conn.commit();
1113
1114       verifyTest(count, expectedRows,
1115               "Matched rows selected with clob of size(" + cloblen + ") =");
1116
1117       if (trace) {
1118           System.out.println("Select Clob (" + cloblen + ")" + " rows= "
1119                   + expectedRows + " = "
1120                   + (long) (System.currentTimeMillis() - ST));
1121       }
1122       System.out.println("========================================");
1123   }
1124
1125   /*
1126    * Basically this test will do an update using setClob api -
1127    * select row from clobtbl and then update a row in clobtbl
1128    * and verify updated data in clobtbl
1129    */

1130   private static void selectUpdateClob(String JavaDoc testId, Connection conn,
1131           PreparedStatement ps, int cloblen, int id, int updateId,
1132           int expectedRows) throws Exception JavaDoc {
1133       System.out.println("========================================");
1134       System.out.println("START " + testId + " - select and then update clob of size= "
1135               + cloblen + " - Uses setClob api");
1136
1137       ResultSet rs = null;
1138
1139       ps.setInt(1, id);
1140       rs = ps.executeQuery();
1141       rs.next();
1142       Clob value = rs.getClob(1);
1143       long l = value.length();
1144       long dlen = rs.getLong(2);
1145       if (dlen != l) {
1146           System.out
1147                   .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected "
1148                           + dlen + " for row in CLOBTBL with ID=" + id);
1149       }
1150
1151       PreparedStatement psUpd = conn
1152               .prepareStatement("update CLOBTBL set content=?,dlen =? where id = ?");
1153       psUpd.setCharacterStream(1, value.getCharacterStream(), (int) l);
1154       psUpd.setLong(2, l);
1155       psUpd.setInt(3, updateId);
1156
1157       System.out.println("Rows Updated = " + psUpd.executeUpdate());
1158       conn.commit();
1159
1160       // now select and verify that update went through ok.
1161
ps.setInt(1, updateId);
1162       ResultSet rs2 = ps.executeQuery();
1163       rs2.next();
1164       Clob updatedValue = rs2.getClob(1);
1165
1166       if(updatedValue.length() != l)
1167           System.out.println("FAIL - Retrieving the updated clob length does not match "+
1168                   "expected length = "+l +" found = "+ updatedValue.length());
1169
1170       // close resultsets
1171
conn.commit();
1172       rs.close();
1173       rs2.close();
1174       psUpd.close();
1175       System.out.println("========================================");
1176   }
1177
1178   
1179   /*
1180    * Basically this test will do an update using setBlob api and verifies the
1181    * updated data. select row from clobtbl2 and then update a row in clobtbl
1182    * and verify updated data in clobtbl against the data in the original file
1183    */

1184   private static void selectUpdateClob2(String JavaDoc testId, Connection conn,
1185           PreparedStatement ps, PreparedStatement sel,int cloblen, int id, int updateId,
1186           int expectedRows,String JavaDoc file) throws Exception JavaDoc {
1187       System.out.println("========================================");
1188       System.out.println("START " + testId + " - select and then update clob of size= "
1189               + cloblen + " - Uses setClob api");
1190
1191       ResultSet rs = null;
1192       
1193       // retrieve row from clobtbl2
1194
ps.setInt(1, id);
1195       rs = ps.executeQuery();
1196       rs.next();
1197       Clob value = rs.getClob(1);
1198       long l = value.length();
1199       long dlen = rs.getLong(2);
1200       if (dlen != l) {
1201           System.out
1202                   .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected "
1203                           + dlen + " for row in CLOBTBL2 with ID=" + id);
1204       }
1205
1206       PreparedStatement psUpd = conn
1207               .prepareStatement("update CLOBTBL set content=?,dlen =? where id = ?");
1208       psUpd.setClob(1,value);
1209       psUpd.setLong(2, l);
1210       psUpd.setInt(3, updateId);
1211
1212       System.out.println("Rows Updated = " + psUpd.executeUpdate());
1213       conn.commit();
1214
1215       // now select and verify that update went through ok.
1216
sel.setInt(1, updateId);
1217       ResultSet rs2 = sel.executeQuery();
1218       rs2.next();
1219       Clob updatedValue = rs2.getClob(1);
1220       
1221       if (updatedValue.length() != l)
1222       {
1223           System.out.println("FAIL - MISMATCH length of updated clob value , found="+
1224                   updatedValue.length() +",expected = "+l);
1225       }
1226       else
1227           compareClobToFile(updatedValue.getCharacterStream(),file,(int)l);
1228
1229       // close resultsets
1230
conn.commit();
1231       rs.close();
1232       rs2.close();
1233       psUpd.close();
1234       System.out.println("========================================");
1235
1236   }
1237
1238   /*
1239    * Basically this test will do an update using updateClob api and verifies the
1240    * updated data. select row from clobtbl2 and then update a row in clobtbl
1241    * and verify updated data in clobtbl against the data in the original file
1242    * @param updateRowId id of the row that needs to be updated
1243    */

1244   private static void updateClob2(String JavaDoc testId, Connection conn,PreparedStatement sel,
1245           int cloblen, int id, int updateRowId,int updateIdVal,
1246           int expectedRows,String JavaDoc file) throws Exception JavaDoc {
1247       System.out.println("========================================");
1248       System.out.println("START " + testId + " - select and then update clob of size= "
1249               + cloblen + " - Uses updateClob api");
1250
1251       
1252       PreparedStatement ps1 = conn.prepareStatement("SELECT * FROM CLOBTBL FOR UPDATE", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
1253       PreparedStatement ps = conn.prepareStatement("SELECT CONTENT,DLEN FROM CLOBTBL2 where ID =?");
1254        
1255       ResultSet rs = null;
1256       ps.setInt(1,id);
1257       // retrieve row from clobtbl2
1258
rs = ps.executeQuery();
1259       rs.next();
1260       Clob value = rs.getClob(1);
1261       long l = value.length();
1262       long dlen = rs.getLong(2);
1263       if (dlen != l) {
1264           System.out
1265                   .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected "
1266                           + dlen + " for row in CLOBTBL2 with ID=" + id);
1267       }
1268       
1269       ResultSet rs1 = ps1.executeQuery();
1270       while (rs1.next()) {
1271            if (rs1.getInt(1) == updateRowId)
1272            {
1273                rs1.updateClob(4, value);
1274                rs1.updateInt(1, updateIdVal);
1275                rs1.updateInt(2, 0);
1276                rs1.updateLong(3, dlen);
1277                rs1.updateRow();
1278                break;
1279            }
1280        }
1281       // close resultsets
1282
conn.commit();
1283       rs.close();
1284       rs1.close();
1285       ps1.close();
1286       ps.close();
1287       
1288       // verify
1289
// now select and verify that update went through ok.
1290
sel.setInt(1, updateIdVal);
1291       ResultSet rs2 = sel.executeQuery();
1292       rs2.next();
1293       Clob updatedValue = rs2.getClob(1);
1294       
1295       if (updatedValue.length() != l)
1296       {
1297           System.out.println("FAIL - MISMATCH length of updated clob value , found="+
1298                   updatedValue.length() +",expected = "+l);
1299       }
1300       else
1301           compareClobToFile(updatedValue.getCharacterStream(),file,(int)l);
1302
1303
1304       System.out.println("========================================");
1305
1306   }
1307
1308   
1309   private static void compareClobToFile(Reader lobstream, String JavaDoc filename,int length)
1310           throws Exception JavaDoc {
1311       FileReader file = new FileReader(filename);
1312       int c1 = 0;
1313       int c2 = 0;
1314       long count = 0;
1315       do {
1316           c1 = lobstream.read();
1317           c2 = file.read();
1318           if (c1 != c2)
1319           {
1320               System.out.println("FAIL -- MISMATCH in data stored versus data retrieved at " + count);
1321               break;
1322           }
1323           count++;
1324           length--;
1325       } while (c1 != -1 && c2 != -1 && length > 0);
1326   }
1327
1328   private static void expectedException(SQLException sqle) {
1329
1330       while (sqle != null) {
1331           String JavaDoc sqlState = sqle.getSQLState();
1332           if (sqlState == null) {
1333               sqlState = "<NULL>";
1334           }
1335           System.out.println("EXPECTED SQL Exception: (" + sqlState + ") "
1336                   + sqle.getMessage());
1337
1338           sqle = sqle.getNextException();
1339       }
1340   }
1341
1342   private static void verifyTest(int affectedRows, int expectedRows,
1343           String JavaDoc test) {
1344       if (affectedRows != expectedRows)
1345           System.out.println("FAIL --" + test + affectedRows
1346                   + " , but expected rows =" + expectedRows);
1347       else
1348           System.out.println(test + affectedRows);
1349   }
1350   
1351   private static void writeToFile(String JavaDoc file,Reader r)
1352       throws IOException
1353   {
1354       // does file exist, if so delete and write to a fresh file
1355
File f =new File(file);
1356       if (f.exists())
1357           f.delete();
1358       FileWriter writer = new FileWriter(file);
1359       // write in chunks of 32k buffer
1360
char[] buffer = new char[32 * 1024];
1361       int count = 0;
1362       
1363       while((count = r.read(buffer)) >=0)
1364           writer.write(buffer,0,count);
1365       writer.flush();
1366       writer.close();
1367   }
1368}
1369
1370/**
1371 * Class to generate random byte data
1372 */

1373class RandomByteStream extends java.io.InputStream JavaDoc {
1374   private long length;
1375
1376   private java.util.Random JavaDoc dpr;
1377
1378   RandomByteStream(java.util.Random JavaDoc dpr, long length) {
1379       this.length = length;
1380       this.dpr = dpr;
1381
1382   }
1383
1384   public int read() {
1385       if (length <= 0)
1386           return -1;
1387
1388       length--;
1389       return (byte) (dpr.nextInt() >>> 25);
1390   }
1391
1392   public int read(byte[] data, int off, int len) {
1393
1394       if (length <= 0)
1395           return -1;
1396
1397       if (len > length)
1398           len = (int)length;
1399
1400       for (int i = 0; i < len; i++) {
1401           // chop off bits and return a +ve byte value.
1402
data[off + i] = (byte) (dpr.nextInt() >>> 25);
1403       }
1404
1405       length -= len;
1406       return len;
1407   }
1408}
1409
1410/*
1411 * Class to generate random char data, generates 1,2,3bytes character.
1412 */

1413class RandomCharReader extends java.io.Reader JavaDoc {
1414   private long length;
1415   private long numTrailingSpaces;
1416
1417   private java.util.Random JavaDoc dpr;
1418
1419   RandomCharReader(java.util.Random JavaDoc dpr, long length) {
1420       this.length = length;
1421       this.dpr = dpr;
1422       this.numTrailingSpaces = 0;
1423   }
1424
1425   RandomCharReader(java.util.Random JavaDoc dpr, long length,long numTrailingSpaces) {
1426       this.length = length;
1427       this.dpr = dpr;
1428       this.numTrailingSpaces = numTrailingSpaces;
1429   }
1430
1431   private int randomInt(int min, int max) {
1432       return dpr.nextInt(max - min) + min;
1433   }
1434
1435   private char getChar() {
1436       // return space for trailing spaces.
1437
if (length <= numTrailingSpaces)
1438       {
1439          return ' ';
1440       }
1441          
1442       double drand = dpr.nextDouble();
1443       char c = 'a';
1444       if (drand < 0.25)
1445           c = (char) randomInt((int) 'A', (int) 'Z');
1446       else if (drand < 0.5)
1447           switch (randomInt(1, 10)) {
1448           case 1:
1449               c = '\u00c0';
1450               break;
1451           case 2:
1452               c = '\u00c1';
1453               break;
1454           case 3:
1455               c = '\u00c2';
1456               break;
1457           case 4:
1458               c = '\u00ca';
1459               break;
1460           case 5:
1461               c = '\u00cb';
1462               break;
1463           case 6:
1464               c = '\u00d4';
1465               break;
1466           case 7:
1467               c = '\u00d8';
1468               break;
1469           case 8:
1470               c = '\u00d1';
1471               break;
1472           case 9:
1473               c = '\u00cd';
1474               break;
1475           default:
1476               c = '\u00dc';
1477               break;
1478           }
1479       else if (drand < 0.75)
1480           c = (char) randomInt((int) 'a', (int) 'z');
1481       else if (drand < 1.0)
1482           switch (randomInt(1, 10)) {
1483           case 1:
1484               c = '\u00e2';
1485               break;
1486           case 2:
1487               c = '\u00e4';
1488               break;
1489           case 3:
1490               c = '\u00e7';
1491               break;
1492           case 4:
1493               c = '\u00e8';
1494               break;
1495           case 5:
1496               c = '\u00ec';
1497               break;
1498           case 6:
1499               c = '\u00ef';
1500               break;
1501           case 7:
1502               c = '\u00f6';
1503               break;
1504           case 8:
1505               c = '\u00f9';
1506               break;
1507           case 9:
1508               c = '\u00fc';
1509               break;
1510           default:
1511               c = '\u00e5';
1512               break;
1513           }
1514
1515       return c;
1516
1517   }
1518
1519   public int read() {
1520       if (length <= 0)
1521           return -1;
1522
1523       length--;
1524       return getChar();
1525   }
1526
1527   public int read(char[] data, int off, int len) {
1528
1529       if (length <= 0)
1530           return -1;
1531
1532       if (len > length)
1533           len = (int)length;
1534
1535       for (int i = 0; i < len; i++) {
1536           data[off + i] = getChar();
1537           length -= 1;
1538       }
1539
1540       return len;
1541   }
1542
1543   public void close() {
1544
1545   }
1546}
1547
1548/**
1549 * Class used to simulate a 4GB Clob implementation to
1550 * check whether derby implements such large Clobs correctly.
1551 * Derby throws an error if the clob size exceeds 2GB
1552 **/

1553
1554class ClobImpl implements java.sql.Clob JavaDoc {
1555  long length;
1556  Reader myReader;
1557 
1558  public ClobImpl(Reader myReader,long length) {
1559      this.length = length;
1560      this.myReader = myReader;
1561  }
1562
1563  public long length() throws SQLException {
1564      return length;
1565  }
1566
1567  public String JavaDoc getSubString(long pos, int length) throws SQLException {
1568      throw new SQLException("Not implemented");
1569  }
1570
1571  public java.io.Reader JavaDoc getCharacterStream() throws SQLException {
1572      return myReader;
1573  }
1574
1575  public java.io.InputStream JavaDoc getAsciiStream() throws SQLException {
1576      throw new SQLException("Not implemented");
1577  }
1578
1579  public long position(String JavaDoc searchstr, long start) throws SQLException {
1580      throw new SQLException("Not implemented");
1581  }
1582
1583  public long position(Clob searchstr, long start) throws SQLException {
1584      throw new SQLException("Not implemented");
1585  }
1586
1587  public int setString(long pos, String JavaDoc str) throws SQLException {
1588      throw new SQLException("Not implemented");
1589  }
1590
1591  public int setString(long pos, String JavaDoc str, int offset, int len) throws SQLException {
1592      throw new SQLException("Not implemented");
1593  }
1594
1595  public java.io.OutputStream JavaDoc setAsciiStream(long pos) throws SQLException {
1596      throw new SQLException("Not implemented");
1597  }
1598
1599  public java.io.Writer JavaDoc setCharacterStream(long pos) throws SQLException {
1600      throw new SQLException("Not implemented");
1601  }
1602
1603  public void truncate(long len) throws SQLException {
1604      throw new SQLException("Not implemented");
1605  }
1606
1607  public void free() throws SQLException {
1608      throw new SQLException("Not implemented");
1609  }
1610
1611  public Reader getCharacterStream(long pos, long length) throws SQLException {
1612      throw new SQLException("Not implemented");
1613  }
1614
1615}
1616
1617/***
1618 * Class to simulate a 4Gb blob impl in order to test if Derby
1619 * handles such large blobs correctly. The main methods here are
1620 * only the length() and the getBinaryStream(). Rest are just
1621 * placeholders/dummy methods in order to implement the java.sql.Blob
1622 * interface
1623 * ----
1624 * Derby throws an error if the blob length exceeds the max range of
1625 * int.
1626 */

1627class BlobImpl implements java.sql.Blob JavaDoc
1628{
1629    long length;
1630    InputStream myStream;
1631    
1632    public BlobImpl(InputStream is, long length)
1633    {
1634        this.myStream = is;
1635        this.length = length;
1636    }
1637    public InputStream getBinaryStream()
1638    throws SQLException
1639    {
1640        return myStream;
1641    }
1642    
1643    public byte[] getBytes()
1644    throws SQLException
1645    {
1646        throw new SQLException("Not implemented");
1647    }
1648    public long length()
1649    throws SQLException
1650    {
1651        return length;
1652    }
1653    
1654    public long position(Blob pattern,long start)
1655    throws SQLException
1656    {
1657        throw new SQLException("Not implemented");
1658    }
1659    
1660    public long position(byte[] pattern,long start)
1661    throws SQLException
1662    {
1663        throw new SQLException("Not implemented");
1664    }
1665    public OutputStream setBinaryStream(long pos)
1666    throws SQLException
1667    
1668    {
1669        throw new SQLException("Not implemented");
1670    }
1671    
1672    public int setBytes(long pos,byte[] bytes)
1673    throws SQLException
1674    {
1675        throw new SQLException("Not implemented");
1676    }
1677    public int setBytes(long pos,byte[] bytes,int offset,int len)
1678    throws SQLException
1679    {
1680        throw new SQLException("Not implemented");
1681    }
1682    
1683    public void truncate(long len)
1684    throws SQLException
1685    {
1686        throw new SQLException("Not implemented");
1687    }
1688    
1689    public byte[] getBytes(long pos, int length)
1690    throws SQLException
1691    {
1692        throw new SQLException("Not implemented");
1693    }
1694    
1695}
1696
Popular Tags