KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.blobclob4BLOB
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.ByteArrayInputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.FileWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.io.OutputStreamWriter JavaDoc;
36 import java.io.Reader JavaDoc;
37 import java.io.StringBufferInputStream JavaDoc;
38 import java.io.StringReader JavaDoc;
39 import java.io.Writer JavaDoc;
40 import java.sql.Blob JavaDoc;
41 import java.sql.Clob JavaDoc;
42 import java.sql.Connection JavaDoc;
43 import java.sql.PreparedStatement JavaDoc;
44 import java.sql.ResultSet JavaDoc;
45 import java.sql.ResultSetMetaData JavaDoc;
46 import java.sql.SQLException JavaDoc;
47 import java.io.UnsupportedEncodingException JavaDoc;
48 import java.sql.Statement JavaDoc;
49 import java.sql.Types JavaDoc;
50 import java.util.zip.CRC32 JavaDoc;
51
52 import org.apache.derby.tools.JDBCDisplayUtil;
53 import org.apache.derby.tools.ij;
54 import org.apache.derbyTesting.functionTests.util.Formatters;
55 import org.apache.derbyTesting.functionTests.util.TestUtil;
56 import java.util.Arrays JavaDoc;
57
58 /**
59  * Test of JDBC blob and clob
60  *
61  * @author paulat
62  */

63
64 public class blobclob4BLOB {
65
66     static String JavaDoc[] fileName;
67     static long[] fileCRC32;
68     static String JavaDoc[] basefileName; // for printing messages so that no path info is in .out
69
static String JavaDoc filePath;
70     static String JavaDoc unicodeFilePath;
71     static String JavaDoc sep;
72     static long[] fileLength;
73     static int numFiles;
74     static int numRows;
75     static int numStrings;
76     static String JavaDoc[] unicodeStrings;
77     static int numRowsUnicode;
78     static String JavaDoc unicodeFileName;
79
80     static boolean isDerbyNet = false;
81     static boolean debug = true;
82     private static final String JavaDoc START = "\nSTART: ";
83
84     static
85     {
86         numFiles = 5;
87         filePath = "extin";
88         fileName = new String JavaDoc[numFiles];
89         basefileName = new String JavaDoc[numFiles];
90         fileLength = new long[numFiles];
91         fileCRC32 = new long[numFiles];
92
93         fileName[0] = "short.utf"; // set up a short (fit in one page) blob/clob
94
fileName[1] = "littleclob.utf"; // set up a long (longer than a page) blob/clob
95
fileName[2] = "empty.utf"; // set up a blob/clob with nothing in it
96
fileName[3] = "searchclob.utf"; // set up a blob/clob to search with
97
fileName[4] = "aclob.utf"; // set up a really long (over 300K) blob/clob
98

99         for (int i = 0 ; i < numFiles; i++)
100         {
101             basefileName[i] = fileName[i];
102         }
103
104         numRows = 10;
105
106         numStrings = 3;
107         unicodeStrings = new String JavaDoc[numStrings];
108         unicodeStrings[0] = "\u0061\u0062\u0063"; // abc
109
unicodeStrings[1] = "\u0370\u0371\u0372";
110         unicodeStrings[2] = "\u05d0\u05d1\u05d2";
111         numRowsUnicode = 6;
112
113         unicodeFilePath = "extinout";
114         unicodeFileName = "unicodeFile.txt";
115     }
116
117
118     public static void main(String JavaDoc[] args)
119     {
120         System.out.println("Test blobclob starting");
121
122         isDerbyNet = TestUtil.isNetFramework();
123
124         try
125         {
126             // first check to see if the path to extin/out dir is ok.
127
sep = System.getProperty("file.separator");
128             boolean exists = (new File JavaDoc(filePath, fileName[0])).exists();
129             if (!exists)
130             {
131                 String JavaDoc userDir = System.getProperty("user.dir");
132                 filePath = userDir + sep + ".." + sep + filePath;
133                 unicodeFilePath = userDir + sep + ".." + sep + unicodeFilePath;
134             }
135             for (int i=0; i < numFiles; i++)
136             {
137                 fileName[i] = filePath + sep + fileName[i];
138                 
139                 FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fileName[i]);
140                 fileCRC32[i] = getStreamCheckSum(fis);
141             }
142             unicodeFileName = unicodeFilePath + sep + unicodeFileName;
143
144             // use the ij utility to read the property file and
145
// make the initial connection.
146
ij.getPropertyArg(args);
147             Connection JavaDoc conn = ij.startJBMS();
148             // turn off autocommit, otherwise blobs/clobs cannot hang around
149
// until end of transaction
150
conn.setAutoCommit(false);
151
152             prepareCLOBMAIN(conn);
153             prepareSearchClobTable(conn);
154             prepareUnicodeTable(conn);
155             prepareUnicodeFile(conn);
156             // prepareBinaryTable(conn);
157

158             setCharacterStreamTest(conn);
159
160             // unicodeTest();
161
// clobTestGroupfetch(conn);
162

163             clobTest0(conn);
164             clobTest11(conn);
165             clobTest12(conn);
166             clobTest13Trigger(conn);
167             clobTest2(conn);
168             clobTest22(conn);
169             clobTest3(conn);
170             clobTest32(conn);
171             clobTest4(conn);
172             clobTest42(conn);
173             clobTest51(conn);
174             clobTest52(conn);
175             clobTest53(conn);
176             clobTest54(conn);
177             clobTest6(conn);
178             clobTest7(conn);
179
180             clobTest8(conn);
181
182             clobTest91(conn);
183             clobTest92(conn);
184             clobTest93(conn);
185             clobTest94(conn);
186             clobTest95(conn);
187   
188            // restart the connection
189
conn = ij.startJBMS();
190             
191             // do not run these tests with DerbyNet
192
if (!TestUtil.isJCCFramework()) {
193                 clobTest10(conn);
194                 clobTest14(conn);
195             }
196   
197             conn.setAutoCommit(false);
198             clobTest96(conn);
199
200             prepareBlobTable(conn);
201             prepareSearchBlobTable(conn);
202
203             blobTest0(conn);
204             blobTest2(conn);
205             blobTest3(conn);
206             blobTest4(conn);
207             blobTest51(conn);
208             blobTest52(conn);
209             blobTest53(conn);
210             blobTest54(conn);
211             blobTest6(conn);
212             blobTest7(conn);
213             blobTest8Trigger(conn);
214             blobTest91(conn);
215             blobTest92(conn);
216             blobTest93(conn);
217             blobTest94(conn);
218             blobTest95(conn);
219      
220             // restart the connection
221
conn = ij.startJBMS();
222             conn.setAutoCommit(false);
223             blobTest96(conn);
224             
225             // do not run these tests with DerbyNet
226
if (!TestUtil.isJCCFramework()) {
227                 blobTest10(conn);
228                 blobTest11(conn);
229             }
230
231             clobTestSelfDestructive(conn);
232             clobTestSelfDestructive2(conn);
233
234             conn.commit();
235             clobNegativeTest_Derby265(conn);
236             blobNegativeTest_Derby265(conn);
237
238             // restart the connection for cleaning up
239
conn = ij.startJBMS();
240             String JavaDoc[] testObjects = {"table testclob_main", "table searchclob",
241                                   "table testunicode", "table testunicode2",
242                                   "table testclob10", "table testinteger",
243                                   "table testclobcolumn", "table testclob2",
244                                   "table testclob7", "table testlongrowclob",
245                                   "table testblob", "table searchblob",
246                                   "table testvarbinary", "table testinteger2",
247                                   "table testblobcolumn", "table testblob2",
248                                   "table testblobx", "table testlongrowblob",
249                                   "table maps", "table maps_blob"};
250             Statement JavaDoc stmt = conn.createStatement();
251             TestUtil.cleanUpTest(stmt, testObjects);
252             conn.close();
253             System.out.println("FINISHED TEST blobclob :-)");
254         }
255         catch (SQLException JavaDoc e)
256         {
257             TestUtil.dumpSQLExceptions(e);
258             if (debug) e.printStackTrace(System.out);
259         }
260         catch (Throwable JavaDoc e)
261         {
262             System.out.println("xFAIL -- unexpected exception:" + e.toString());
263 // e.fillInStackTrace();
264
if (debug) e.printStackTrace(System.out);
265         }
266         System.out.println("Test blobclob finished\n");
267     }
268
269
270     private static void insertRow(PreparedStatement JavaDoc ps, String JavaDoc s)
271         throws SQLException JavaDoc
272     {
273         ps.clearParameters();
274         ps.setString(1, s);
275         ps.setInt(2, s.length());
276         ps.executeUpdate();
277     }
278
279     private static void insertRow(PreparedStatement JavaDoc ps, String JavaDoc s, int i)
280         throws SQLException JavaDoc
281     {
282         ps.setString(1, s);
283         ps.setInt(2, s.length());
284         ps.setInt(3, i);
285         ps.executeUpdate();
286     }
287
288     private static void insertRow(PreparedStatement JavaDoc ps, byte[] b)
289         throws SQLException JavaDoc, IOException JavaDoc
290     {
291         ps.setBytes(1, b);
292         ps.setInt(2, b.length);
293         ps.setLong(3, getStreamCheckSum(new ByteArrayInputStream JavaDoc(b)));
294         ps.executeUpdate();
295     }
296
297     /*
298         Set up a table with all kinds of CLOB values,
299         some short (less than 1 page), some long (more than 1 page)
300         some very large (many pages).
301         Table has 2 cols: the first is the value, the second is the length of
302         the value.
303         (Also sets the fileLength array.)
304     */

305     private static void prepareCLOBMAIN(Connection JavaDoc conn)
306     {
307         System.out.println(START +"prepareCLOBMAIN");
308         ResultSet JavaDoc rs;
309         Statement JavaDoc stmt;
310
311         try {
312             stmt = conn.createStatement();
313             stmt.execute(
314         // creating table small then add large column - that way forcing table to have default small page size, but have large rows.
315
"create table testCLOB_MAIN (b integer)");
316         stmt.execute("alter table testCLOB_MAIN add column a CLOB(1M)");
317             PreparedStatement JavaDoc ps = conn.prepareStatement(
318                 "insert into testCLOB_MAIN (a, b) values(?,?)");
319
320             // insert small strings
321
insertRow(ps,"");
322             insertRow(ps,"you can lead a horse to water but you can't form it into beverage");
323             insertRow(ps,"a stitch in time says ouch");
324             insertRow(ps,"here is a string with a return \n character");
325
326
327             // insert larger strings using setAsciiStream
328
for (int i = 0; i < numFiles; i++)
329             {
330                 // prepare an InputStream from the file
331
File JavaDoc file = new File JavaDoc(fileName[i]);
332                 fileLength[i] = file.length();
333                 /*
334                 System.out.println("inserting filename[" +i +
335                                    "]" + basefileName[i] +
336                                    " length: " + fileLength[i]);
337                 */

338                 InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
339
340                 System.out.println("===> inserting " + basefileName[i] + " length = "
341                                        + fileLength[i]);
342
343                 // insert a streaming column
344
ps.setAsciiStream(1, fileIn, (int)fileLength[i]);
345                 ps.setInt(2, (int)fileLength[i]);
346                 ps.executeUpdate();
347                 ps.clearParameters();
348                 fileIn.close();
349             }
350
351             // insert a null
352
ps.setNull(1, Types.CLOB);
353             ps.setInt(2, 0);
354             ps.executeUpdate();
355
356             conn.commit();
357
358             // set numRows
359
rs = stmt.executeQuery("select count(*) from testCLOB_MAIN");
360             int realNumRows = -1;
361             if (rs.next())
362                 realNumRows = rs.getInt(1);
363             if (realNumRows <= 0)
364                 System.out.println("FAIL. No rows in table testCLOB_MAIN");
365             if (realNumRows != numRows)
366                 System.out.println("FAIL. numRows is incorrect");
367             rs.close();
368
369         }
370         catch (SQLException JavaDoc e) {
371             TestUtil.dumpSQLExceptions(e);
372             if (debug) e.printStackTrace(System.out);
373         }
374         catch (Throwable JavaDoc e) {
375             System.out.println("FAIL -- unexpected exception:" + e.toString());
376             if (debug) e.printStackTrace(System.out);
377         }
378         //System.out.println("prepareCLOBMAIN finished");
379
}
380
381
382
383     /*
384         Set up a table with clobs to search for
385         most short (less than 1 page), some long (more than 1 page)
386         some very large (many pages) ??
387     */

388     private static void prepareSearchClobTable(Connection JavaDoc conn)
389     {
390         System.out.println(START + "prepareSearchClobTable");
391         ResultSet JavaDoc rs;
392         Statement JavaDoc stmt;
393
394         try
395         {
396             stmt = conn.createStatement();
397             // creating table small then add large column - that way forcing table to have default small page size, but have large rows.
398
stmt.execute("create table searchClob (b integer)");
399             stmt.execute("alter table searchClob add column a CLOB(300k)");
400             PreparedStatement JavaDoc ps = conn.prepareStatement(
401                 "insert into searchClob (a, b) values(?,?)");
402             insertRow(ps,"horse");
403             insertRow(ps,"ouch");
404             insertRow(ps,"\n");
405             insertRow(ps,"");
406             insertRow(ps,"Beginning");
407             insertRow(ps,"position-69");
408             insertRow(ps,"I-am-hiding-here-at-position-5910");
409             insertRow(ps,"Position-9907");
410
411             // insert larger strings using setAsciiStream
412
for (int i = 0; i < numFiles; i++)
413             {
414                 // prepare an InputStream from the file
415
File JavaDoc file = new File JavaDoc(fileName[i]);
416                 fileLength[i] = file.length();
417                 InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
418
419                 /*
420                 System.out.println("inserting filename[" +i +
421                                    "]" + basefileName[i] +
422                                    " length: " + fileLength[i]);
423                 */

424                 System.out.println("===> inserting " + basefileName[i] + " length = "
425                                        + fileLength[i]);
426
427                 // insert a streaming column
428

429                 ps.setAsciiStream(1, fileIn, (int)fileLength[i]);
430                 ps.setInt(2, (int)fileLength[i]);
431                 ps.executeUpdate();
432                 ps.clearParameters();
433                 fileIn.close();
434             }
435
436             // insert a null
437
ps.setNull(1, Types.CLOB);
438             ps.setInt(2, 0);
439             ps.executeUpdate();
440
441             conn.commit();
442         }
443         catch (SQLException JavaDoc e) {
444             TestUtil.dumpSQLExceptions(e);
445             if (debug) e.printStackTrace(System.out);
446         }
447         catch (Throwable JavaDoc e) {
448             System.out.println("FAIL -- unexpected exception:" + e.toString());
449             if (debug) e.printStackTrace(System.out);
450         }
451         System.out.println("prepareSearchClobTable finished");
452     }
453
454
455     /*
456         Set up a table with unicode strings in it
457         some short (less than 1 page), some long (more than 1 page)
458         Table has 3 cols: the first is the value, the second is the length of
459         the value, the third is the array index (or else -1 for the ones from files).
460         (Also sets the fileLength array.)
461         Try slurping the thing into a String.
462     */

463     private static void prepareUnicodeTable(Connection JavaDoc conn)
464     {
465         ResultSet JavaDoc rs;
466         Statement JavaDoc stmt;
467          System.out.println(START + "prepareUnicodeTable");
468         try {
469             stmt = conn.createStatement();
470             // creating table small then add large column - that way forcing table to have default small page size, but have large rows.
471
stmt.execute("create table testUnicode (b integer, c integer)");
472             stmt.execute("alter table testUnicode add column a CLOB(100k)");
473             PreparedStatement JavaDoc ps = conn.prepareStatement(
474             // "insert into testUnicode values(?,?,?)");
475
"insert into testUnicode (a, b, c) values(?,?,?)");
476
477             // insert small strings
478

479             for (int i = 0; i < numStrings; i++)
480             {
481                 insertRow(ps,unicodeStrings[i],i);
482             }
483
484             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(5000);
485             for (int i = 0; i < 5000; i++)
486                 sb.append('q');
487             String JavaDoc largeString = new String JavaDoc(sb);
488
489             // insert larger strings
490
for (int i = 0; i < numStrings; i++)
491             {
492                 insertRow(ps,unicodeStrings[i] + largeString + unicodeStrings[i] + "pppppppppp",i);
493             }
494             conn.commit();
495
496             // set numRows
497
rs = stmt.executeQuery("select count(*) from testUnicode");
498             int realNumRows = -1;
499             if (rs.next())
500                 realNumRows = rs.getInt(1);
501             if (realNumRows <= 0)
502                 System.out.println("FAIL. No rows in table testUnicode");
503             if (realNumRows != numRowsUnicode)
504                 System.out.println("FAIL. numRowsUnicode is incorrect");
505
506         }
507         catch (SQLException JavaDoc e) {
508             TestUtil.dumpSQLExceptions(e);
509         }
510         catch (Throwable JavaDoc e) {
511             System.out.println("FAIL -- unexpected exception:" + e.toString());
512             if (debug) e.printStackTrace(System.out);
513         }
514
515     }
516
517
518     /*
519       Tests PreparedStatement.setCharacterStream
520     */

521     private static void setCharacterStreamTest(Connection JavaDoc conn)
522     {
523         ResultSet JavaDoc rs;
524         Statement JavaDoc stmt;
525          System.out.println(START + "setCharacterStreamTest");
526         try
527         {
528             stmt = conn.createStatement();
529             // forcing table with default table space.
530
stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.storage.pageSize','4096')");
531             stmt.execute("create table testUnicode2 (a CLOB(100k))");
532             stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.storage.pageSize','0')");
533             PreparedStatement JavaDoc ps = conn.prepareStatement(
534                 "insert into testUnicode2 values(?)");
535
536             // insert large string using setCharacterStream
537
// prepare an InputStream from the file
538
File JavaDoc file = new File JavaDoc(unicodeFileName);
539             InputStream JavaDoc fileIS = new FileInputStream JavaDoc(file);
540             Reader JavaDoc filer = new InputStreamReader JavaDoc(fileIS,"UTF8");
541             // insert a streaming column
542
ps.setCharacterStream(1, filer, 5009);
543             ps.executeUpdate();
544             filer.close();
545             conn.commit();
546
547             rs = stmt.executeQuery("select a from testUnicode2");
548             while (rs.next())
549             {
550                 Clob JavaDoc clob = rs.getClob(1);
551                 System.out.println("Length of clob is " + clob.length());
552                 Reader JavaDoc r = clob.getCharacterStream();
553                 char[] buf = new char[3];
554                 for (int i = 0; i < numStrings; i++)
555                 {
556                     r.read(buf);
557                     if (unicodeStrings[i].equals(new String JavaDoc(buf)))
558                         System.out.println("unicode string " + i + " matched");
559                     else
560                         System.out.println("unicode string " + i + " not matched");
561                 }
562                 for (int i = 0; i < 5000; i++)
563                 {
564                     int c = r.read();
565                     if (c == -1)
566                     {
567                         System.out.println("EOF reached at i = " + i);
568                         break;
569                     }
570                     if ((char)c != 'p')
571                     {
572                         System.out.println("A p was missed, got a " + (char)c);
573                     }
574                 }
575                 if (r.read() != -1)
576                     System.out.println("EOF was missed");
577                 else
578                     System.out.println("EOF matched");
579             }
580             conn.commit();
581
582             System.out.println("setCharacterStreamTest finished");
583         }
584         catch (SQLException JavaDoc e)
585         {
586             TestUtil.dumpSQLExceptions(e);
587         }
588         catch (Throwable JavaDoc e)
589         {
590             System.out.println("FAIL -- unexpected exception:" + e.toString());
591             if (debug) e.printStackTrace(System.out);
592         }
593     }
594
595
596     /*
597       Make a file with unicode stuff in it.
598      */

599     private static void prepareUnicodeFile(Connection JavaDoc conn)
600     {
601          System.out.println(START + "prepareUnicodeFile");
602         try
603         {
604             File JavaDoc file = new File JavaDoc(unicodeFileName);
605             OutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
606             Writer JavaDoc filew = new OutputStreamWriter JavaDoc(fos,"UTF8");
607             // FileWriter filew = new FileWriter(file);
608
filew.write(unicodeStrings[0]);
609             filew.write(unicodeStrings[1]);
610             filew.write(unicodeStrings[2]);
611             for (int i = 0; i < 5000; i++)
612                 filew.write('p');
613             filew.close();
614
615             InputStream JavaDoc fis = new FileInputStream JavaDoc(file);
616             Reader JavaDoc filer = new InputStreamReader JavaDoc(fis,"UTF8");
617             // FileReader filer = new FileReader(file);
618
char bufs[][] = new char[numStrings][3];
619             for (int i = 0; i < numStrings; i++)
620             {
621                 filer.read(bufs[i]);
622                 String JavaDoc s = new String JavaDoc(bufs[i]);
623                 if (s.equals(unicodeStrings[i]))
624                     System.out.println("unicode string " + i + " correct");
625                 else
626                     System.out.println("FAILED: unicode string " + i + " incorrect");
627             }
628             for (int i = 0; i < 5000; i++)
629                 if (filer.read() != 'p')
630                     System.out.println("Not a p : i = " + i);
631             if (filer.read() != -1)
632                 System.out.println("Not EOF");
633             filer.close();
634             System.out.println("Finished prepareUnicodeFile");
635         }
636         catch (Throwable JavaDoc e)
637         {
638             System.out.println("FAIL -- unexpected exception:" + e.toString());
639             if (debug) e.printStackTrace(System.out);
640         }
641     }
642
643
644
645     /*
646         basic test of getAsciiStream
647         also tests length
648         need to run prepareCLOBMAIN first
649     */

650     private static void clobTest0(Connection JavaDoc conn)
651     {
652         ResultSet JavaDoc rs;
653         Statement JavaDoc stmt;
654          System.out.println(START + "clobTest0");
655         try
656         {
657             stmt = conn.createStatement();
658             rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
659             byte[] buff = new byte[128];
660             // fetch row back, get the column as a clob.
661
Clob JavaDoc clob;
662             int clobLength, i = 0;
663             while (rs.next()) {
664                 i++;
665                 // get the first column in select as a clob
666
clob = rs.getClob(1);
667                 if (clob == null)
668                     continue;
669                 InputStream JavaDoc fin = clob.getAsciiStream();
670                 int columnSize = 0;
671                 for (;;) {
672                     int size = fin.read(buff);
673                     if (size == -1)
674                         break;
675                     columnSize += size;
676                 }
677                 clobLength = rs.getInt(2);
678
679                 if (columnSize != clobLength)
680                     System.out.println("test failed, columnSize should be " + clobLength
681                        + ", but it is " + columnSize + ", i = " + i);
682                 if (columnSize != clob.length())
683                 {
684                     System.out.println("test failed, clob.length() should be " + columnSize
685                        + ", but it is " + clob.length() + ", i = " + i);
686                 }
687             }
688             rs.close();
689             conn.commit();
690             System.out.println("clobTest0 finished");
691         }
692         catch (SQLException JavaDoc e) {
693             TestUtil.dumpSQLExceptions(e);
694         }
695         catch (Throwable JavaDoc e) {
696             System.out.println("FAIL -- unexpected exception:" + e.toString());
697             if (debug) e.printStackTrace(System.out);
698         }
699     }
700
701
702     /*
703         basic test of getCharacterStream
704         also tests length
705         need to run prepareCLOBMAIN first
706     */

707     private static void clobTest11(Connection JavaDoc conn) {
708
709         ResultSetMetaData JavaDoc met;
710         ResultSet JavaDoc rs;
711         Statement JavaDoc stmt;
712          System.out.println(START + "clobTest1");
713         try {
714             stmt = conn.createStatement();
715             rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
716             met = rs.getMetaData();
717             char[] buff = new char[128];
718             // fetch row back, get the column as a clob.
719
int i = 0, clobLength = 0;
720             while (rs.next()) {
721                 i++;
722                 // get the first column as a clob
723
Clob JavaDoc clob = rs.getClob(1);
724                 if (clob == null)
725                     continue;
726                 Reader JavaDoc reader = clob.getCharacterStream();
727                 int columnSize = 0;
728                 for (;;) {
729                     int size = reader.read(buff);
730                     if (size == -1)
731                         break;
732                     // System.out.println("the next buffer is :" + buff);
733
columnSize += size;
734                 }
735                 clobLength = rs.getInt(2);
736                 if (columnSize != clobLength)
737                     System.out.println("test failed, columnSize should be " + clobLength
738                        + ", but it is " + columnSize + ", i = " + i);
739                 if (columnSize != clob.length())
740                     System.out.println("test failed, clob.length() should be " + columnSize
741                        + ", but it is " + clob.length() + ", i = " + i);
742                 
743             }
744             rs.close();
745             conn.commit();
746             System.out.println("clobTest11 finished");
747         }
748         catch (SQLException JavaDoc e) {
749             TestUtil.dumpSQLExceptions(e);
750         }
751         catch (Throwable JavaDoc e) {
752             System.out.println("FAIL -- unexpected exception:" + e.toString());
753             if (debug) e.printStackTrace(System.out);
754         }
755     }
756
757
758     /*
759         test of getCharacterStream on a table containing unicode characters
760         need to run prepareUnicodeTable first
761      */

762     private static void clobTest12(Connection JavaDoc conn)
763     {
764         ResultSet JavaDoc rs;
765         Statement JavaDoc stmt;
766         System.out.println(START + "clobTest12");
767         try
768         {
769             stmt = conn.createStatement();
770             rs = stmt.executeQuery("select a,b,c from testUnicode");
771             int i = 0, colLength = 0, arrayIndex = 0;
772             while (rs.next())
773             {
774                 i++;
775                 colLength = rs.getInt(2);
776                 arrayIndex = rs.getInt(3);
777                 Clob JavaDoc clob = rs.getClob(1);
778                 if (clob == null)
779                 {
780                     System.out.println("row " + i + " is null, skipped");
781                     continue;
782                 }
783                 Reader JavaDoc reader = clob.getCharacterStream();
784
785                 int columnSize = 0, c;
786                 String JavaDoc compareString = "";
787                 for (;;)
788                 {
789                     c = reader.read();
790                     if (c == -1)
791                         break;
792                     if (columnSize < 3)
793                         compareString += (char)c;
794                     columnSize ++;
795                 }
796                 if (compareString.equals(unicodeStrings[arrayIndex]))
797                     System.out.println("Succeeded to match, row " + i);
798                 else
799                 {
800                     System.out.println("Failed to match, row " + i +
801                     ". compareString = " + compareString + ". arrayIndex = " +
802                     arrayIndex + ". unicodeStrings[arrayIndex] = " +
803                     unicodeStrings[arrayIndex]);
804
805                 }
806                 if (columnSize != colLength)
807                     System.out.println("test failed, columnSize should be " + colLength
808                        + ", but it is " + columnSize + ", i = " + i);
809                 else
810                     System.out.println("PASSED, row " + i + ", length was " + columnSize);
811             }
812             conn.commit();
813             System.out.println("clobTest12 finished");
814         }
815         catch (SQLException JavaDoc e)
816         {
817             TestUtil.dumpSQLExceptions(e);
818         }
819         catch (Throwable JavaDoc e)
820         {
821             System.out.println("FAIL -- unexpected exception:" + e.toString());
822             if (debug) e.printStackTrace(System.out);
823         }
824     }
825
826     /**
827      * Test triggers on CLOB columns.
828     */

829     private static void clobTest13Trigger(Connection JavaDoc conn)
830     {
831         System.out.println(START + "clobTest13Trigger");
832         try {
833             Statement JavaDoc stmt = conn.createStatement();
834             stmt.executeUpdate("CREATE TABLE clobTest13TriggerA (a CLOB(400k), b int)");
835             stmt.executeUpdate("CREATE TABLE clobTest13TriggerB (a CLOB(400k), b int)");
836             stmt.executeUpdate(
837                     "create trigger T13A after update on testCLOB_MAIN " +
838                     "referencing new as n old as o " +
839                     "for each row mode db2sql "+
840                     "insert into clobTest13TriggerA(a, b) values (n.a, n.b)");
841             stmt.executeUpdate(
842                     "create trigger T13B after INSERT on clobTest13TriggerA " +
843                     "referencing new_table as n " +
844                     "for each statement mode db2sql "+
845                     "insert into clobTest13TriggerB(a, b) select n.a, n.b from n");
846             
847             conn.commit();
848             ResultSet JavaDoc rs = stmt.executeQuery(
849                   "select a,length(a),b from testCLOB_MAIN order by b");
850             
851             showClobContents("testCLOB_MAIN", rs);
852             
853             rs.close();
854             conn.commit();
855             int rowCount = stmt.executeUpdate("UPDATE testCLOB_MAIN set b = b + 0");
856             System.out.println("main update row count :" + rowCount);
857             conn.commit();
858             rs = stmt.executeQuery(
859                "select a,length(a),b from clobTest13TriggerA order by b");
860             showClobContents("row trigger", rs);
861             rs.close();
862             conn.commit();
863             
864             rs = stmt.executeQuery(
865             "select a,length(a),b from clobTest13TriggerB order by b");
866             showClobContents("statement trigger", rs);
867             rs.close();
868            conn.commit();
869          
870             stmt.executeUpdate("DROP TRIGGER T13A");
871             stmt.executeUpdate("DROP TABLE clobTest13TriggerB");
872             stmt.executeUpdate("DROP TABLE clobTest13TriggerA");
873             
874             stmt.close();
875             conn.commit();
876             System.out.println("clobTest13Trigger finished");
877         }
878         catch (SQLException JavaDoc e) {
879             TestUtil.dumpSQLExceptions(e);
880             do {
881             e.printStackTrace(System.out);
882             e = e.getNextException();
883             } while (e != null);
884             
885         }
886         catch (Throwable JavaDoc e) {
887             System.out.println("FAIL -- unexpected exception:" + e.toString());
888             if (debug) e.printStackTrace(System.out);
889         }
890         
891     }
892
893
894
895
896     /*
897     test getSubString
898     need to run prepareCLOBMAIN first
899     */

900     private static void clobTest2(Connection JavaDoc conn)
901     {
902         ResultSet JavaDoc rs;
903         Statement JavaDoc stmt;
904         System.out.println(START + "clobTest2");
905         try
906         {
907             stmt = conn.createStatement();
908             rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
909             int i = 0, clobLength = 0;
910             Clob JavaDoc clob;
911             while (rs.next())
912             {
913                 i++;
914                 clob = rs.getClob(1);
915                 if (clob == null)
916                     continue;
917                 clobLength = rs.getInt(2);
918                 blobclob4BLOB.printInterval(clob, 9905, 50, 0, i, clobLength);
919                 blobclob4BLOB.printInterval(clob, 5910, 150, 1, i, clobLength);
920                 blobclob4BLOB.printInterval(clob, 5910, 50, 2, i, clobLength);
921                 blobclob4BLOB.printInterval(clob, 204, 50, 3, i, clobLength);
922                 blobclob4BLOB.printInterval(clob, 68, 50, 4, i, clobLength);
923                 blobclob4BLOB.printInterval(clob, 1, 50, 5, i, clobLength);
924                 blobclob4BLOB.printInterval(clob, 1, 1, 6, i, clobLength);
925                 blobclob4BLOB.printInterval(
926                         clob, 1, 0, 7, i, clobLength); // length 0 at start
927
blobclob4BLOB.printInterval(
928                         clob, clobLength + 1, 0, 8, i, clobLength); // and end
929
/*
930                 System.out.println(i + "(0) " + clob.getSubString(9905,50));
931                 System.out.println(i + "(1) " + clob.getSubString(5910,150));
932                 System.out.println(i + "(2) " + clob.getSubString(5910,50));
933                 System.out.println(i + "(3) " + clob.getSubString(204,50));
934                 System.out.println(i + "(4) " + clob.getSubString(68,50));
935                 System.out.println(i + "(5) " + clob.getSubString(1,50));
936                 System.out.println(i + "(6) " + clob.getSubString(1,1));
937                 */

938                 if (clobLength > 100)
939                 {
940                     String JavaDoc res = clob.getSubString(clobLength-99,200);
941                     System.out.println(i + "(9) ");
942                     if (res.length() != 100)
943                         System.out.println("FAIL : length of substring is " +
944                             res.length() + " should be 100");
945                     else
946                         System.out.println(res);
947                 }
948             }
949             rs.close();
950             System.out.println("clobTest2 finished");
951         }
952         catch (SQLException JavaDoc e) {
953             TestUtil.dumpSQLExceptions(e);
954         }
955         catch (Throwable JavaDoc e) {
956             System.out.println("FAIL -- unexpected exception:" + e.toString());
957             if (debug) e.printStackTrace(System.out);
958         }
959     }
960
961
962     /*
963     test getSubString with unicode
964     need to run prepareUnicodeTable first
965     */

966     private static void clobTest22(Connection JavaDoc conn)
967     {
968         ResultSet JavaDoc rs;
969         Statement JavaDoc stmt;
970         System.out.println(START + "clobTest22");
971         try
972         {
973             stmt = conn.createStatement();
974             rs = stmt.executeQuery("select a,b,c from testUnicode");
975             int i = 0, clobLength = 0, arrayIndex = 0;
976             Clob JavaDoc clob;
977             while (rs.next())
978             {
979                 i++;
980                 System.out.print("Row " + i + " : ");
981                 clob = rs.getClob(1);
982                 if (clob == null)
983                     continue;
984                 clobLength = rs.getInt(2);
985                 arrayIndex = rs.getInt(3);
986                 if (clob.getSubString(1,3).equals(unicodeStrings[arrayIndex]))
987                     System.out.println("Succeeded");
988                 else
989                     System.out.println("Failed");
990                 if (clobLength > 5000)
991                 {
992                     if (clob.getSubString(5004,3).equals(unicodeStrings[arrayIndex]))
993                         System.out.println("Second time Succeeded");
994                     else
995                         System.out.println("Second time Failed");
996                 }
997             }
998             System.out.println("clobTest22 finished");
999         }
1000        catch (SQLException JavaDoc e)
1001        {
1002            TestUtil.dumpSQLExceptions(e);
1003        }
1004        catch (Throwable JavaDoc e)
1005        {
1006            System.out.println("FAIL -- unexpected exception:" + e.toString());
1007            if (debug) e.printStackTrace(System.out);
1008        }
1009    }
1010
1011
1012
1013    /*
1014    test position with a String argument
1015    need to run prepareCLOBMAIN first
1016    */

1017    private static void clobTest3(Connection JavaDoc conn)
1018    {
1019        ResultSet JavaDoc rs;
1020        Statement JavaDoc stmt;
1021         System.out.println(START + "clobTest3");
1022        try
1023        {
1024            stmt = conn.createStatement();
1025            rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
1026            int i = 0, clobLength = 0;
1027            Clob JavaDoc clob;
1028            while (rs.next())
1029            {
1030                i++;
1031                clob = rs.getClob(1);
1032                if (clob == null)
1033                    continue;
1034               clobLength = rs.getInt(2);
1035                if (clobLength > 20000)
1036                    continue;
1037                blobclob4BLOB.printPosition(i,"horse",1,clob, clobLength);
1038                blobclob4BLOB.printPosition(i,"ouch",1,clob, clobLength);
1039                blobclob4BLOB.printPosition(i,"\n",1,clob, clobLength);
1040                blobclob4BLOB.printPosition(i,"",1,clob, clobLength);
1041                blobclob4BLOB.printPosition(i,"Beginning",1,clob, clobLength);
1042                blobclob4BLOB.printPosition(i,"Beginning",2,clob, clobLength);
1043                blobclob4BLOB.printPosition(i,"position-69",1,clob, clobLength);
1044                blobclob4BLOB.printPosition(i,"This-is-position-204",1,clob, clobLength);
1045                blobclob4BLOB.printPosition(i,"I-am-hiding-here-at-position-5910",1,clob, clobLength);
1046                blobclob4BLOB.printPosition(i,"I-am-hiding-here-at-position-5910",5910,clob, clobLength);
1047                blobclob4BLOB.printPosition(i,"I-am-hiding-here-at-position-5910",5911,clob, clobLength);
1048                blobclob4BLOB.printPosition(i,"Position-9907",1,clob, clobLength);
1049            }
1050            rs.close();
1051            System.out.println("clobTest3 finished");
1052        }
1053        catch (SQLException JavaDoc e) {
1054            TestUtil.dumpSQLExceptions(e);
1055        }
1056        catch (Throwable JavaDoc e) {
1057            System.out.println("FAIL -- unexpected exception:" + e.toString());
1058            if (debug) e.printStackTrace(System.out);
1059        }
1060    }
1061
1062
1063    /*
1064    test position with a unicode String argument
1065    need to run prepareUnicodeTable first
1066    */

1067    private static void clobTest32(Connection JavaDoc conn)
1068    {
1069        ResultSet JavaDoc rs;
1070        Statement JavaDoc stmt;
1071        System.out.println(START + "clobTest32");
1072        try
1073        {
1074            stmt = conn.createStatement();
1075            rs = stmt.executeQuery("select a,b,c from testUnicode");
1076            int i = 0, clobLength = 0, arrayIndex = 0;
1077            long pos = 0;
1078            Clob JavaDoc clob;
1079            while (rs.next())
1080            {
1081                i++;
1082                clob = rs.getClob(1);
1083                if (clob == null)
1084                    continue;
1085                clobLength = rs.getInt(2);
1086                arrayIndex = rs.getInt(3);
1087
1088                pos = clob.position(unicodeStrings[arrayIndex],1);
1089                if (pos == 1)
1090                    System.out.println("Succeeded: Found unicode string " + arrayIndex +
1091                    " at position " + pos + ",row " + i);
1092                else
1093                    System.out.println("Failed: Found unicode string " + arrayIndex +
1094                    " at position " + pos + ",row " + i);
1095
1096                pos = clob.position(unicodeStrings[arrayIndex],4000);
1097                if (pos == 5004 || (pos == -1 && clobLength < 4000))
1098                    System.out.println("Succeeded: Found unicode string " + arrayIndex +
1099                    " at position " + pos + ",row " + i);
1100                else
1101                    System.out.println("Failed: Found unicode string " + arrayIndex +
1102                    " at position " + pos + ",row " + i);
1103            }
1104            System.out.println("clobTest32 finished");
1105        }
1106        catch (SQLException JavaDoc e)
1107        {
1108            TestUtil.dumpSQLExceptions(e);
1109        }
1110        catch (Throwable JavaDoc e)
1111        {
1112            System.out.println("FAIL -- unexpected exception:" + e.toString());
1113            if (debug) e.printStackTrace(System.out);
1114        }
1115    }
1116
1117
1118    /*
1119    test position with a Clob argument
1120    need to run prepareCLOBMAIN and prepareSearchClobTable first
1121    */

1122    private static void clobTest4(Connection JavaDoc conn)
1123    {
1124        ResultSet JavaDoc rs, rs2;
1125        Statement JavaDoc stmt, stmt2;
1126        System.out.println(START + "clobTest4");
1127        try
1128        {
1129            stmt = conn.createStatement();
1130            rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
1131            int i = 0, clobLength = 0;
1132            Clob JavaDoc clob;
1133            while (rs.next())
1134            {
1135                i++;
1136                clob = rs.getClob(1);
1137                if (clob == null)
1138                    continue;
1139                clobLength = rs.getInt(2);
1140                if (clobLength > 20000)
1141                    {
1142                        System.out.println("testCLOB_MAIN row " + i + " skipped (too large)");
1143                        continue;
1144                    }
1145                // inner loop over table of clobs to search for
1146
// clobs
1147
stmt2 = conn.createStatement();
1148                rs2 = stmt2.executeQuery("select a,b from searchClob");
1149                int j = 0, clobLength2 = 0;
1150                Clob JavaDoc searchClob;
1151                String JavaDoc searchStr;
1152                while (rs2.next())
1153                {
1154                    j++;
1155                    searchClob = rs2.getClob(1);
1156                    if (searchClob == null)
1157                        continue;
1158                    clobLength2 = rs2.getInt(2);
1159                    if (clobLength2 > 20000)
1160                    {
1161                        System.out.println("searchClob row " + j + " skipped (too large)");
1162                        continue;
1163                    }
1164                    if (clobLength2 < 150)
1165                        searchStr = rs2.getString(1);
1166                    else
1167                        searchStr = null;
1168
1169                    printPositionClob(i,searchStr,1,clob,j,searchClob);
1170                }
1171            }
1172            rs.close();
1173            System.out.println("clobTest4 finished");
1174        }
1175        catch (SQLException JavaDoc e) {
1176            TestUtil.dumpSQLExceptions(e);
1177            if (debug) e.printStackTrace(System.out);
1178        }
1179        catch (Throwable JavaDoc e) {
1180            System.out.println("FAIL -- unexpected exception:" + e.toString());
1181            if (debug) e.printStackTrace(System.out);
1182        }
1183    }
1184
1185
1186    /*
1187    test position with a Clob argument containing unicode characters
1188    need to run prepareCLOBMAIN and prepareSearchClobTable first
1189    */

1190    private static void clobTest42(Connection JavaDoc conn)
1191    {
1192        ResultSet JavaDoc rs;
1193        Statement JavaDoc stmt;
1194         System.out.println(START + "clobTest42");
1195        try
1196        {
1197            stmt = conn.createStatement();
1198            rs = stmt.executeQuery("select a,b,c from testUnicode");
1199            Clob JavaDoc[] clobArray = new Clob JavaDoc[numRowsUnicode];
1200            int i = 0;
1201            long pos = 0;
1202            while (rs.next())
1203            {
1204                clobArray[i++] = rs.getClob(1);
1205            }
1206
1207            for (int j = 0; j < 3; j++)
1208            {
1209                pos = clobArray[j+3].position(clobArray[j],1);
1210                if (pos == 1)
1211                    System.out.println("Succeeded: Found clob at position " + pos + ",row " + j);
1212                else
1213                    System.out.println("Failed: Found clob at position " + pos + ",row " + j);
1214                // pos = clobArray[i*2].position(clobArray[i*3],1);
1215
}
1216            System.out.println("clobTest42 finished");
1217        }
1218        catch (SQLException JavaDoc e)
1219        {
1220            TestUtil.dumpSQLExceptions(e);
1221        }
1222        catch (Throwable JavaDoc e)
1223        {
1224            System.out.println("FAIL -- unexpected exception:" + e.toString());
1225            if (debug) e.printStackTrace(System.out);
1226        }
1227    }
1228
1229
1230
1231    private static void printPositionClob(
1232        int rowNum,
1233        String JavaDoc searchStr,
1234        long position,
1235        Clob JavaDoc clob,
1236        int searchRowNum,
1237        Clob JavaDoc searchClob)
1238    {
1239        try
1240        {
1241            long result = clob.position(searchClob,position);
1242            if ("".equals(searchStr) && (result == 1)) {
1243                System.out.println("position(clob) FOUND @ 1 with empty search clob in clob of length " + clob.length());
1244                return;
1245            }
1246            if (result != -1)
1247            {
1248                System.out.print("Found ");
1249                if (searchStr != null)
1250                    System.out.print(searchStr);
1251                else
1252                    System.out.print("clob (row " + searchRowNum + ") ");
1253                System.out.println(" in row " + rowNum + " at position " + result);
1254            }
1255            else {
1256                System.out.println("position(clob) NOT FOUND " + rowNum + " searchStr " +
1257                    (searchStr != null ? searchStr : ">150chars"));
1258            }
1259        }
1260        catch (SQLException JavaDoc e)
1261        {
1262            TestUtil.dumpSQLExceptions(e);
1263        }
1264    }
1265
1266
1267    /* datatype tests */
1268
1269    // make sure clobs work for small CLOB fields
1270
// also test length method
1271
private static void clobTest51(Connection JavaDoc conn) {
1272
1273        ResultSetMetaData JavaDoc met;
1274        ResultSet JavaDoc rs;
1275        Statement JavaDoc stmt;
1276        System.out.println(START + "clobTest51");
1277        try {
1278            stmt = conn.createStatement();
1279            stmt.execute("create table testCLOB10 (a CLOB(10))");
1280
1281            PreparedStatement JavaDoc ps = conn.prepareStatement(
1282                "insert into testCLOB10 values(?)");
1283            String JavaDoc val = "";
1284            for (int i = 0; i < 10; i++)
1285            {
1286                // insert a string
1287
ps.setString(1, val);
1288                ps.executeUpdate();
1289                val += "x";
1290            }
1291
1292            rs = stmt.executeQuery("select a from testCLOB10");
1293            met = rs.getMetaData();
1294            byte[] buff = new byte[128];
1295            int j = 0;
1296            // fetch all rows back, get the columns as clobs.
1297
while (rs.next())
1298            {
1299                // get the first column as a clob
1300
Clob JavaDoc clob = rs.getClob(1);
1301                if (clob == null)
1302                    continue;
1303                InputStream JavaDoc fin = clob.getAsciiStream();
1304                int columnSize = 0;
1305                for (;;)
1306                {
1307                    int size = fin.read(buff);
1308                    if (size == -1)
1309                        break;
1310                    columnSize += size;
1311                }
1312                if (columnSize != j)
1313                    System.out.println("FAIL - Expected clob size : " + j +
1314                        " Got clob size : " + columnSize);
1315                if (clob.length() != j)
1316                    System.out.println("FAIL - Expected clob length : " + j +
1317                        " Got clob length : " + clob.length());
1318                j++;
1319            }
1320            System.out.println("clobTest51 finished");
1321        }
1322        catch (SQLException JavaDoc e) {
1323            if (isDerbyNet)
1324                System.out.println("EXPECTED SQL Exception: " + e.getMessage());
1325            else
1326                TestUtil.dumpSQLExceptions(e);
1327        }
1328        catch (Throwable JavaDoc e) {
1329            System.out.println("FAIL -- unexpected exception:" + e.toString());
1330            if (debug) e.printStackTrace(System.out);
1331        }
1332    }
1333
1334
1335   // make sure cannot get a clob from an int column
1336
private static void clobTest52(Connection JavaDoc conn) {
1337
1338        ResultSetMetaData JavaDoc met;
1339        ResultSet JavaDoc rs;
1340        Statement JavaDoc stmt;
1341
1342        try {
1343            System.out.println(START + "clobTest52");
1344            stmt = conn.createStatement();
1345            System.out.println("create table testInteger (a integer)");
1346            stmt.execute("create table testInteger (a integer)");
1347
1348            int i = 1;
1349            System.out.println("insert into testInteger values('158')");
1350            PreparedStatement JavaDoc ps = conn.prepareStatement("insert into testInteger values(158)");
1351            ps.executeUpdate();
1352
1353            System.out.println("select a from testInteger");
1354            rs = stmt.executeQuery("select a from testInteger");
1355            met = rs.getMetaData();
1356            while (rs.next()) {
1357                // get the first column as a clob
1358
System.out.println("getClob(1)");
1359                Clob JavaDoc clob = rs.getClob(1);
1360                if (clob == null)
1361                    System.out.println("clob is null");
1362                else
1363                    System.out.println("clob is not null");
1364            }
1365            System.out.println("clobTest52 finished");
1366        }
1367        catch (SQLException JavaDoc e) {
1368            System.out.println("52: SQLException");
1369            if (isDerbyNet)
1370                System.out.println("EXPECTED SQL Exception: " + e.getMessage());
1371            else
1372                TestUtil.dumpSQLExceptions(e);
1373        }
1374        catch (Throwable JavaDoc e) {
1375            System.out.println("52: Throwable");
1376            System.out.println("FAIL -- unexpected exception:" + e.toString());
1377            if (debug) e.printStackTrace(System.out);
1378        }
1379    }
1380
1381
1382   // test creating a clob column, currently this doesn't work since we don't
1383
// have a clob datatype (get a syntax error on the create table statement)
1384
private static void clobTest53(Connection JavaDoc conn) {
1385
1386        ResultSetMetaData JavaDoc met;
1387        ResultSet JavaDoc rs;
1388        Statement JavaDoc stmt;
1389        System.out.println(START + "clobTest53");
1390        try {
1391            stmt = conn.createStatement();
1392            stmt.execute("create table testClobColumn (a clob(1K))");
1393
1394            System.out.println("clobTest53 finished");
1395        }
1396        catch (SQLException JavaDoc e) {
1397            TestUtil.dumpSQLExceptions(e);
1398        }
1399        catch (Throwable JavaDoc e) {
1400            System.out.println("FAIL -- unexpected exception:" + e.toString());
1401            if (debug) e.printStackTrace(System.out);
1402        }
1403    }
1404
1405    
1406    /*
1407        make sure setClob doesn't work on an int column
1408        need to run prepareCLOBMAIN first
1409    */

1410    private static void clobTest54(Connection JavaDoc conn)
1411    {
1412        ResultSet JavaDoc rs;
1413        Statement JavaDoc stmt1, stmt2;
1414        System.out.println(START + "clobTest54");
1415        try
1416        {
1417            stmt1 = conn.createStatement();
1418            stmt1.execute("create table testClob2 (a integer, b integer)");
1419            PreparedStatement JavaDoc ps = conn.prepareStatement(
1420                "insert into testClob2 values(?,?)");
1421            stmt2 = conn.createStatement();
1422            rs = stmt2.executeQuery("select a,b from testCLOB_MAIN");
1423            Clob JavaDoc clob;
1424            int clobLength;
1425            while (rs.next())
1426            {
1427                // get the first ncolumn as a clob
1428
clob = rs.getClob(1);
1429                if (clob == null)
1430                    continue;
1431                clobLength = rs.getInt(2);
1432                ps.setClob(1,clob);
1433                ps.setInt(2,clobLength);
1434                ps.executeUpdate();
1435            }
1436            rs.close();
1437            conn.commit();
1438
1439            System.out.println("clobTest54 finished");
1440        }
1441        catch (SQLException JavaDoc e) {
1442            // Can't do a setClob on an int column. This is expected
1443
TestUtil.dumpSQLExceptions(e,true);
1444        }
1445        catch (Throwable JavaDoc e) {
1446            System.out.println("FAIL -- unexpected exception:" + e.toString());
1447            if (debug) e.printStackTrace(System.out);
1448        }
1449        System.out.println("end clobTest54");
1450    }
1451
1452
1453    /*
1454    test raising of exceptions
1455    need to run prepareCLOBMAIN first
1456    */

1457    private static void clobTest6(Connection JavaDoc conn)
1458    {
1459        ResultSet JavaDoc rs;
1460        Statement JavaDoc stmt;
1461        System.out.println(START + "clobTest6");
1462        try
1463        {
1464            stmt = conn.createStatement();
1465            rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
1466            int i = 0, clobLength = 0;
1467            Clob JavaDoc clob;
1468            rs.next();
1469            clob = rs.getClob(1);
1470            clobLength = rs.getInt(2);
1471            rs.close();
1472            if (clob == null)
1473                return;
1474
1475            
1476            // 0 or negative position value
1477
try
1478                {
1479                    clob.getSubString(0,5);
1480                    System.out.println("FAIL = clob.getSubString(0,5)");
1481                }
1482                catch (SQLException JavaDoc e)
1483                {
1484                boolean isExpected = isOutOfBoundException(e);
1485                                                       
1486                TestUtil.dumpSQLExceptions(e, isExpected);
1487                }
1488            
1489            // negative length value
1490
try
1491            {
1492                clob.getSubString(1,-76);
1493                System.out.println("FAIL = getSubString(1,-76)");
1494            }
1495            catch (SQLException JavaDoc e)
1496            {
1497                TestUtil.dumpSQLExceptions(e, isOutOfBoundException(e));
1498            }
1499            // boundary negative 1 length
1500
try {
1501                clob.getSubString(1,-1);
1502                System.out.println("FAIL = getSubString(1,-1)");
1503            } catch (SQLException JavaDoc e) {
1504                 TestUtil.dumpSQLExceptions(e, isOutOfBoundException(e));
1505             }
1506            // before start with length zero
1507
try
1508            {
1509                clob.getSubString(0,0);
1510                System.out.println("FAIL = getSubString(0,0)");
1511           }
1512            catch (SQLException JavaDoc e)
1513            {
1514                TestUtil.dumpSQLExceptions(e, isOutOfBoundException(e));
1515            }
1516            // 2 past end with length 0
1517
try {
1518                clob.getSubString(clobLength + 2,0);
1519                System.out.println("FAIL = getSubString(clobLength + 2,0)");
1520            } catch (SQLException JavaDoc e) {
1521                TestUtil.dumpSQLExceptions(e, isOutOfBoundException(e));
1522            }
1523            // 0 or negative position value
1524
try
1525            {
1526                clob.position("xx",-4000);
1527                System.out.println("FAIL = position('xx',-4000)");
1528            }
1529            catch (SQLException JavaDoc e)
1530            {
1531                TestUtil.dumpSQLExceptions(e, isOutOfBoundException(e));
1532            }
1533            // null pattern
1534
try
1535            {
1536                clob.position((String JavaDoc) null,5);
1537                System.out.println("FAIL = position((String) null,5)");
1538            }
1539            catch (SQLException JavaDoc e)
1540            {
1541                TestUtil.dumpSQLExceptions(e, "XJ072".equals(e.getSQLState()));
1542            }
1543            // 0 or negative position value
1544
try
1545            {
1546                clob.position(clob,-42);
1547                System.out.println("FAIL = position(clob,-42)");
1548            }
1549            catch (SQLException JavaDoc e)
1550            {
1551                TestUtil.dumpSQLExceptions(e, isOutOfBoundException(e));
1552            }
1553            // null pattern
1554
try
1555            {
1556                clob.position((Clob JavaDoc) null,5);
1557                System.out.println("FAIL = pposition((Clob) null,5)");
1558            }
1559            catch (SQLException JavaDoc e)
1560            {
1561                TestUtil.dumpSQLExceptions(e, "XJ072".equals(e.getSQLState()));
1562            }
1563            System.out.println("clobTest6 finished");
1564        }
1565        catch (SQLException JavaDoc e) {
1566            TestUtil.dumpSQLExceptions(e);
1567        }
1568        catch (Throwable JavaDoc e) {
1569            System.out.println("FAIL -- unexpected exception:" + e.toString());
1570            if (debug) e.printStackTrace(System.out);
1571        }
1572    }
1573
1574
1575    /*
1576        test setClob
1577        need to run prepareCLOBMAIN first
1578    */

1579    private static void clobTest7(Connection JavaDoc conn)
1580    {
1581        ResultSet JavaDoc rs, rs2;
1582        Statement JavaDoc stmt1, stmt2;
1583        System.out.println(START + "clobTest7");
1584        try
1585        {
1586            stmt1 = conn.createStatement();
1587            stmt1.execute("create table testClob7 (a CLOB(300k), b integer)");
1588            PreparedStatement JavaDoc ps = conn.prepareStatement(
1589                "insert into testClob7 values(?,?)");
1590            stmt2 = conn.createStatement();
1591            rs = stmt2.executeQuery("select a,b from testCLOB_MAIN");
1592            Clob JavaDoc clob;
1593            int clobLength;
1594            int rownum = 0;
1595            while (rs.next())
1596            {
1597                // get the first column as a clob
1598
clob = rs.getClob(1);
1599                if (clob == null)
1600                    continue;
1601                clobLength = rs.getInt(2);
1602                ps.setClob(1,clob);
1603                ps.setInt(2,clobLength);
1604                ps.executeUpdate();
1605            }
1606            rs.close();
1607            conn.commit();
1608
1609            rs2 = stmt2.executeQuery("select a,b from testClob7");
1610            Clob JavaDoc clob2;
1611            int clobLength2, j = 0;
1612            while (rs2.next())
1613            {
1614                j++;
1615                // get the first column as a clob
1616
clob2 = rs2.getClob(1);
1617                if (clob2 == null)
1618                    continue;
1619                clobLength2 = rs2.getInt(2);
1620                if (clob2.length() != clobLength2)
1621                    System.out.println("FAILED at row " + j);
1622            }
1623            rs2.close();
1624
1625            conn.commit();
1626            System.out.println("clobTest7 finished");
1627        }
1628        catch (SQLException JavaDoc e) {
1629            TestUtil.dumpSQLExceptions(e);
1630            if (debug) e.printStackTrace(System.out);
1631        }
1632        catch (Throwable JavaDoc e) {
1633            System.out.println("FAIL -- unexpected exception:" + e.toString());
1634            if (debug) e.printStackTrace(System.out);
1635        }
1636    }
1637
1638    /**
1639        Agressive test of position.
1640    */

1641    private static void clobTest8(Connection JavaDoc conn)
1642    {
1643        System.out.println(START + "clobTest8");
1644        try {
1645            Statement JavaDoc s = conn.createStatement();
1646
1647            s.execute("CREATE TABLE C8.T8POS(id INT NOT NULL PRIMARY KEY, DD CLOB(1m), pos INT, L INT)");
1648            s.execute("CREATE TABLE C8.T8PATT(PATT CLOB(300k))");
1649
1650            // characters used to fill the String
1651
char[] fill = new char[4];
1652            fill[0] = 'd'; // 1 byte UTF8 character (ASCII)
1653
fill[1] = '\u03a9'; // 2 byte UTF8 character (Greek)
1654
fill[2] = '\u0e14'; // 3 byte UTF8 character (Thai)
1655
fill[3] = 'j'; // 1 byte UTF8 character (ASCII)
1656

1657            char[] base = new char[256 * 1024];
1658
1659            for (int i = 0; i < base.length; i += 4) {
1660
1661                base[i] = fill[0];
1662                base[i+1] = fill[1];
1663                base[i+2] = fill[2];
1664                base[i+3] = fill[3];
1665
1666            }
1667
1668            char[] patternBase = new char[2 * 1024];
1669            for (int i = 0; i < patternBase.length; i += 8) {
1670
1671                patternBase[i] = 'p';
1672                patternBase[i+1] = 'a';
1673                patternBase[i+2] = 't';
1674                patternBase[i+3] = '\u03aa';
1675                patternBase[i+4] = (char) i; // changed value to keep pattern varyinh
1676
patternBase[i+5] = 'b';
1677                patternBase[i+6] = 'm';
1678                patternBase[i+7] = '\u0e15';
1679
1680            }
1681
1682            PreparedStatement JavaDoc ps = conn.prepareStatement("INSERT INTO C8.T8POS VALUES (?, ?, ?, ?)");
1683            PreparedStatement JavaDoc psp = conn.prepareStatement("INSERT INTO C8.T8PATT VALUES (?)");
1684
1685            T8insert(ps, 1, base, 256, patternBase, 8, 100, true);
1686            T8insert(ps, 2, base, 3988, patternBase, 8, 2045, true);
1687            T8insert(ps, 3, base, 16321, patternBase, 8, 4566, true);
1688            T8insert(ps, 4, base, 45662, patternBase, 8, 34555, true);
1689            T8insert(ps, 5, base, 134752, patternBase, 8, 67889, true);
1690            T8insert(ps, 6, base, 303, patternBase, 8, 80, false);
1691            T8insert(ps, 7, base, 4566, patternBase, 8, 2086, false);
1692            T8insert(ps, 8, base, 17882, patternBase, 8, 4426, false);
1693            T8insert(ps, 9, base, 41567, patternBase, 8, 31455, false);
1694            String JavaDoc pstr = T8insert(ps, 10, base, 114732, patternBase, 8, 87809, false);
1695
1696            conn.commit();
1697
1698            psp.setString(1, pstr);
1699            psp.executeUpdate();
1700
1701            System.out.println("small string pattern");
1702            checkClob8(s, pstr);
1703            conn.commit();
1704
1705            System.out.println("small java.sql.Clob pattern");
1706            ResultSet JavaDoc rsc = s.executeQuery("SELECT PATT FROM C8.T8PATT");
1707            rsc.next();
1708            checkClob8(s, rsc.getClob(1));
1709
1710            rsc.close();
1711
1712
1713            conn.commit();
1714
1715            s.execute("DELETE FROM C8.T8POS");
1716            s.execute("DELETE FROM C8.T8PATT");
1717
1718
1719            T8insert(ps, 1, base, 256, patternBase, 134, 100, true);
1720            T8insert(ps, 2, base, 3988, patternBase, 134, 2045, true);
1721            T8insert(ps, 3, base, 16321, patternBase, 134, 4566, true);
1722            T8insert(ps, 4, base, 45662, patternBase, 134, 34555, true);
1723            T8insert(ps, 5, base, 134752, patternBase, 134, 67889, true);
1724            T8insert(ps, 6, base, 303, patternBase, 134, 80, false);
1725            T8insert(ps, 7, base, 4566, patternBase, 134, 2086, false);
1726            T8insert(ps, 8, base, 17882, patternBase, 134, 4426, false);
1727            T8insert(ps, 9, base, 41567, patternBase, 134, 31455, false);
1728            pstr = T8insert(ps, 10, base, 114732, patternBase, 134, 87809, false);
1729
1730            conn.commit();
1731            psp.setString(1, pstr);
1732            psp.executeUpdate();
1733            conn.commit();
1734
1735
1736            System.out.println("medium string pattern");
1737            checkClob8(s, pstr);
1738            conn.commit();
1739
1740            System.out.println("medium java.sql.Clob pattern");
1741            rsc = s.executeQuery("SELECT PATT FROM C8.T8PATT");
1742            rsc.next();
1743            checkClob8(s, rsc.getClob(1));
1744
1745            s.execute("DELETE FROM C8.T8POS");
1746            s.execute("DELETE FROM C8.T8PATT");
1747
1748            T8insert(ps, 1, base, 256, patternBase, 679, 100, true);
1749            T8insert(ps, 2, base, 3988, patternBase, 679, 2045, true);
1750            T8insert(ps, 3, base, 16321, patternBase, 679, 4566, true);
1751            T8insert(ps, 4, base, 45662, patternBase, 679, 34555, true);
1752            T8insert(ps, 5, base, 134752, patternBase, 679, 67889, true);
1753            T8insert(ps, 6, base, 303, patternBase, 679, 80, false);
1754            T8insert(ps, 7, base, 4566, patternBase, 679, 2086, false);
1755            T8insert(ps, 8, base, 17882, patternBase, 679, 4426, false);
1756            T8insert(ps, 9, base, 41567, patternBase, 679, 31455, false);
1757            pstr = T8insert(ps, 10, base, 114732, patternBase, 679, 87809, false);
1758
1759            conn.commit();
1760            psp.setString(1, pstr);
1761            psp.executeUpdate();
1762            conn.commit();
1763
1764
1765            System.out.println("long string pattern");
1766            checkClob8(s, pstr);
1767            conn.commit();
1768
1769            System.out.println("long java.sql.Clob pattern");
1770            rsc = s.executeQuery("SELECT PATT FROM C8.T8PATT");
1771            rsc.next();
1772            checkClob8(s, rsc.getClob(1));
1773
1774            s.execute("DELETE FROM C8.T8POS");
1775            s.execute("DELETE FROM C8.T8PATT");
1776            ps.close();
1777            psp.close();
1778            //
1779

1780            s.execute("DROP TABLE C8.T8POS");
1781            s.execute("DROP TABLE C8.T8PATT");
1782
1783            s.close();
1784
1785            conn.commit();
1786
1787            System.out.println("complete clobTest8");
1788
1789
1790        } catch (SQLException JavaDoc e) {
1791            TestUtil.dumpSQLExceptions(e);
1792            e.printStackTrace(System.out);
1793        }
1794        catch (Throwable JavaDoc e) {
1795            System.out.println("FAIL -- unexpected exception:" + e.toString());
1796            e.printStackTrace(System.out);
1797        }
1798    }
1799
1800    private static void checkClob8(Statement JavaDoc s, String JavaDoc pstr) throws SQLException JavaDoc {
1801        
1802        ResultSet JavaDoc rs = s.executeQuery("SELECT ID, DD, POS, L FROM C8.T8POS ORDER BY 1");
1803
1804        while (rs.next()) {
1805
1806            int id = rs.getInt(1);
1807
1808            System.out.print("@" + id + " ");
1809
1810            java.sql.Clob JavaDoc cl = rs.getClob(2);
1811
1812            int pos = rs.getInt(3);
1813            int len = rs.getInt(4);
1814
1815            long clobPosition = cl.position(pstr, 1);
1816            if (clobPosition == (long) pos) {
1817                System.out.print(" position MATCH("+pos+")");
1818            } else {
1819                System.out.print(" position FAIL("+clobPosition+"!=" + pos +")");
1820            }
1821
1822            System.out.println("");
1823        }
1824        rs.close();
1825    }
1826    private static void checkClob8(Statement JavaDoc s, Clob JavaDoc pstr) throws SQLException JavaDoc {
1827        ResultSet JavaDoc rs = s.executeQuery("SELECT ID, DD, POS, L FROM C8.T8POS ORDER BY 1");
1828
1829        while (rs.next()) {
1830
1831            int id = rs.getInt(1);
1832
1833            System.out.print("@" + id + " ");
1834
1835            java.sql.Clob JavaDoc cl = rs.getClob(2);
1836
1837            int pos = rs.getInt(3);
1838            int len = rs.getInt(4);
1839
1840            long clobPosition = cl.position(pstr, 1);
1841            if (clobPosition == (long) pos) {
1842                System.out.print(" position MATCH("+pos+")");
1843            } else {
1844                System.out.print(" position FAIL("+clobPosition+"!=" + pos +")");
1845            }
1846
1847            System.out.println("");
1848        }
1849        rs.close();
1850    }
1851
1852    private static String JavaDoc T8insert(PreparedStatement JavaDoc ps, int id, char[] base, int bl, char[] pattern, int pl, int pos, boolean addPattern)
1853        throws SQLException JavaDoc {
1854
1855        StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1856        sb.append(base, 0, bl);
1857
1858        // Assume the pattern looks like Abcdefgh
1859
// put together a block of misleading matches such as
1860
// AAbAbcAbcdAbcde
1861

1862        int last = addPatternPrefix(sb, pattern, pl, 5, 10);
1863
1864        if (last >= (pos / 2))
1865            pos = (last + 10) * 2;
1866
1867        // now a set of misleading matches up to half the pattern width
1868
last = addPatternPrefix(sb, pattern, pl, pl/2, pos/2);
1869
1870        if (last >= pos)
1871            pos = last + 13;
1872
1873        // now a complete set of misleading matches
1874
pos = addPatternPrefix(sb, pattern, pl, pl - 1, pos);
1875
1876        if (addPattern) {
1877            // and then the pattern
1878
sb.insert(pos, pattern, 0, pl);
1879        } else {
1880            pos = -1;
1881        }
1882
1883
1884        String JavaDoc dd = sb.toString();
1885        String JavaDoc pstr = new String JavaDoc(pattern, 0, pl);
1886
1887        if (pos != dd.indexOf(pstr)) {
1888            System.out.println("FAIL - test confused pattern not at expected location");
1889
1890            System.out.println("POS = " + pos + " index " + dd.indexOf(pstr));
1891            System.out.println("LENG " + dd.length());
1892            // System.out.println(sb.toString());
1893
}
1894
1895
1896        // JDBC uses 1 offset for first character
1897
if (pos != -1)
1898            pos = pos + 1;
1899
1900        ps.setInt(1, id);
1901        ps.setString(2, dd);
1902        ps.setInt(3, pos);
1903        ps.setInt(4, dd.length());
1904        ps.executeUpdate();
1905
1906        return pstr;
1907
1908    }
1909
1910    private static int addPatternPrefix(StringBuffer JavaDoc sb, char[] pattern, int pl, int fakeCount, int pos) {
1911
1912        for (int i = 0; i < fakeCount && i < (pl - 1); i++) {
1913
1914            sb.insert(pos, pattern, 0, i + 1);
1915            pos += i + 1;
1916        }
1917
1918        return pos;
1919    }
1920
1921    /* advanced tests */
1922
1923    // make sure clob is still around after we go to the next row,
1924
// after we close the result set, and after we close the statement
1925
private static void clobTest91(Connection JavaDoc conn)
1926    {
1927        ResultSet JavaDoc rs;
1928        Statement JavaDoc stmt;
1929        System.out.println(START + "clobTest91");
1930        try {
1931            stmt = conn.createStatement();
1932            rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
1933            byte[] buff = new byte[128];
1934            Clob JavaDoc[] clobArray = new Clob JavaDoc[numRows];
1935            int[] clobLengthArray = new int[numRows];
1936            int j = 0;
1937            while (rs.next())
1938            {
1939                clobArray[j] = rs.getClob(1);
1940                clobLengthArray[j++] = rs.getInt(2);
1941            }
1942            rs.close();
1943            stmt.close();
1944
1945            for (int i = 0; i < numRows; i++)
1946            {
1947                if (clobArray[i] == null)
1948                    {
1949                        System.out.println("row " + i + " is null, skipped");
1950                        continue;
1951                    }
1952                InputStream JavaDoc fin = clobArray[i].getAsciiStream();
1953                int columnSize = 0;
1954                for (;;) {
1955                    int size = fin.read(buff);
1956                    if (size == -1)
1957                        break;
1958                    columnSize += size;
1959                }
1960                if (columnSize != clobLengthArray[i])
1961                    System.out.println("test failed, columnSize should be " +
1962                        clobLengthArray[i] + ", but it is " + columnSize + ", i = " + i);
1963                if (columnSize != clobArray[i].length())
1964                    System.out.println("test failed, clobArray[i].length() should be " + columnSize
1965                       + ", but it is " + clobArray[i].length() + ", i = " + i);
1966                System.out.println("done row " + i + ", length was " + clobLengthArray[i]);
1967            }
1968            System.out.println("clobTest91 finished");
1969        }
1970        catch (SQLException JavaDoc e) {
1971            TestUtil.dumpSQLExceptions(e);
1972        }
1973        catch (Throwable JavaDoc e) {
1974            System.out.println("FAIL -- unexpected exception:" + e.toString());
1975            if (debug) e.printStackTrace(System.out);
1976        }
1977    }
1978
1979
1980    /*
1981        test locking
1982        need to run prepareCLOBMAIN fverirst
1983    */

1984    private static void clobTest92(Connection JavaDoc conn)
1985    {
1986        ResultSet JavaDoc rs;
1987        Statement JavaDoc stmt,stmt2;
1988        System.out.println(START + "clobTest92");
1989        try
1990        {
1991            stmt = conn.createStatement();
1992            rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
1993            // fetch row back, get the column as a clob.
1994
Clob JavaDoc clob = null, shortClob = null;
1995            int clobLength;
1996            while (rs.next())
1997            {
1998                clobLength = rs.getInt(2);
1999                if (clobLength == 10000)
2000                    clob = rs.getClob(1);
2001                if (clobLength == 26)
2002                    shortClob = rs.getClob(1);
2003            }
2004            rs.close();
2005
2006            Connection JavaDoc conn2 = ij.startJBMS();
2007            // turn off autocommit, otherwise blobs/clobs cannot hang around
2008
// until end of transaction
2009
conn2.setAutoCommit(false);
2010
2011            // update should go through since we don't get any locks on clobs
2012
// that are not long columns
2013
stmt2 = conn2.createStatement();
2014            stmt2.executeUpdate("update testCLOB_MAIN set a = 'foo' where b = 26");
2015            if (shortClob.length() != 26)
2016                System.out.println("FAILED: clob length changed to " + shortClob.length());
2017            // should timeout waiting for the lock to do this
2018
stmt2 = conn2.createStatement();
2019            stmt2.executeUpdate("update testCLOB_MAIN set b = b + 1 where b = 10000");
2020
2021            conn.commit();
2022            conn2.rollback();
2023            System.out.println("clobTest92 finished");
2024        }
2025        catch (SQLException JavaDoc e) {
2026            TestUtil.dumpSQLExceptions(e);
2027        }
2028        catch (Throwable JavaDoc e) {
2029            System.out.println("FAIL -- unexpected exception:" + e.toString());
2030            if (debug) e.printStackTrace(System.out);
2031        }
2032    }
2033
2034
2035    /*
2036        test locking with a long row + long column
2037    */

2038    private static void clobTest93(Connection JavaDoc conn)
2039    {
2040        ResultSet JavaDoc rs;
2041        Statement JavaDoc stmt, stmt2;
2042        System.out.println(START + "clobTest93");
2043        try
2044        {
2045            stmt = conn.createStatement();
2046            // creating table to fit within default 4k table size, then add large columns
2047
stmt.execute("create table testLongRowClob (a varchar(2000))");
2048            stmt.execute("alter table testLongRowClob add column b varchar(3000)");
2049            stmt.execute("alter table testLongRowClob add column c varchar(2000)");
2050            stmt.execute("alter table testLongRowClob add column d varchar(3000)");
2051            stmt.execute("alter table testLongRowClob add column e CLOB(400k)");
2052            PreparedStatement JavaDoc ps = conn.prepareStatement(
2053                "insert into testLongRowClob values(?,?,?,?,?)");
2054            ps.setString(1,Formatters.padString("blaaa",2000));
2055            ps.setString(2,Formatters.padString("tralaaaa",3000));
2056            ps.setString(3,Formatters.padString("foodar",2000));
2057            ps.setString(4,Formatters.padString("moped",3000));
2058            File JavaDoc file = new File JavaDoc(fileName[1]);
2059            if (file.length() < 10000)
2060                System.out.println("ERROR: wrong file tested");
2061            InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
2062            ps.setAsciiStream(5, fileIn, (int)file.length());
2063            ps.executeUpdate();
2064            fileIn.close();
2065            conn.commit();
2066
2067            stmt = conn.createStatement();
2068            rs = stmt.executeQuery("select e from testLongRowClob");
2069            // fetch row back, get the column as a clob.
2070
Clob JavaDoc clob = null;
2071            while (rs.next())
2072                clob = rs.getClob(1);
2073            rs.close();
2074
2075            Connection JavaDoc conn2 = ij.startJBMS();
2076            // turn off autocommit, otherwise blobs/clobs cannot hang around
2077
// until end of transaction
2078
conn2.setAutoCommit(false);
2079            // the following should timeout
2080
stmt2 = conn2.createStatement();
2081            stmt2.executeUpdate("update testLongRowClob set e = 'smurfball' where a = 'blaaa'");
2082
2083            conn.commit();
2084            conn2.commit();
2085            System.out.println("clobTest92 finished");
2086        }
2087        catch (SQLException JavaDoc e) {
2088            TestUtil.dumpSQLExceptions(e);
2089        }
2090        catch (Throwable JavaDoc e) {
2091            System.out.println("FAIL -- unexpected exception:" + e.toString());
2092            if (debug) e.printStackTrace(System.out);
2093        }
2094    }
2095
2096    
2097    /*
2098        test accessing clob after commit
2099        need to run prepareCLOBMAIN first
2100    */

2101    private static void clobTest94(Connection JavaDoc conn)
2102    {
2103        ResultSet JavaDoc rs;
2104        Statement JavaDoc stmt;
2105        System.out.println(START + "clobTest94");
2106        try
2107        {
2108            stmt = conn.createStatement();
2109            rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
2110            // fetch row back, get the column as a clob.
2111
Clob JavaDoc clob = null, shortClob = null;
2112            int clobLength;
2113            int i = 0;
2114            while (rs.next())
2115            {
2116                //System.out.println("ACCESSING ROW:" + i++);
2117
clobLength = rs.getInt(2);
2118                if (clobLength == 10000)
2119                    clob = rs.getClob(1);
2120                if (clobLength == 26)
2121                    shortClob = rs.getClob(1);
2122            }
2123            rs.close();
2124            conn.commit();
2125
2126            // no problem accessing this after commit since it is in memory
2127
System.out.println("shortClob length after commit is " + shortClob.length());
2128            // these should all give blob/clob data unavailable exceptions
2129
try
2130            {
2131                clob.length();
2132            }
2133            catch (SQLException JavaDoc e)
2134            {
2135                TestUtil.dumpSQLExceptions(e);
2136            }
2137            try
2138            {
2139                clob.getSubString(2,3);
2140            }
2141            catch (SQLException JavaDoc e)
2142            {
2143                TestUtil.dumpSQLExceptions(e);
2144            }
2145            try
2146            {
2147                clob.getAsciiStream();
2148            }
2149            catch (SQLException JavaDoc e)
2150            {
2151                TestUtil.dumpSQLExceptions(e);
2152            }
2153            try
2154            {
2155                clob.position("foo",2);
2156            }
2157            catch (SQLException JavaDoc e)
2158            {
2159                TestUtil.dumpSQLExceptions(e);
2160            }
2161            try
2162            {
2163                clob.position(clob,2);
2164            }
2165            catch (SQLException JavaDoc e)
2166            {
2167                TestUtil.dumpSQLExceptions(e);
2168            }
2169
2170            System.out.println("clobTest94 finished");
2171        }
2172        catch (SQLException JavaDoc e) {
2173            TestUtil.dumpSQLExceptions(e);
2174        }
2175        catch (Throwable JavaDoc e) {
2176            System.out.println("FAIL -- unexpected exception:" + e.toString());
2177            if (debug) e.printStackTrace(System.out);
2178        }
2179    }
2180
2181
2182    /*
2183        test accessing clob after closing the connection
2184        need to run prepareCLOBMAIN first
2185    */

2186    private static void clobTest95(Connection JavaDoc conn)
2187    {
2188        ResultSet JavaDoc rs;
2189        Statement JavaDoc stmt;
2190        System.out.println(START + "clobTest95");
2191        try
2192        {
2193            stmt = conn.createStatement();
2194            rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
2195            // fetch row back, get the column as a clob.
2196
Clob JavaDoc clob = null, shortClob = null;
2197            int clobLength;
2198            while (rs.next())
2199            {
2200                clobLength = rs.getInt(2);
2201                if (clobLength == 10000)
2202                    clob = rs.getClob(1);
2203                if (clobLength == 26)
2204                    shortClob = rs.getClob(1);
2205            }
2206            rs.close();
2207            conn.commit();
2208            conn.close();
2209
2210            try {
2211            // no problem accessing this after commit since it is in memory
2212
System.out.println("shortClob length after closing connection is " + shortClob.length());
2213            }
2214            catch (SQLException JavaDoc e)
2215            {
2216                if (isDerbyNet)
2217                    System.out.println("EXPECTED SQL Exception: " + e.getMessage());
2218                else
2219                    TestUtil.dumpSQLExceptions(e);
2220                
2221            }
2222            // these should all give blob/clob data unavailable exceptions
2223
try
2224            {
2225                clob.length();
2226            }
2227            catch (SQLException JavaDoc e)
2228            {
2229                if (isDerbyNet)
2230                    System.out.println("EXPECTED SQL Exception: " + e.getMessage());
2231                else
2232                    TestUtil.dumpSQLExceptions(e);
2233            }
2234            try
2235            {
2236                clob.getSubString(2,3);
2237            }
2238            catch (SQLException JavaDoc e)
2239            {
2240                if (isDerbyNet)
2241                    System.out.println("EXPECTED SQL Exception: " + e.getMessage());
2242                else
2243                    TestUtil.dumpSQLExceptions(e);
2244            }
2245            try
2246            {
2247                clob.getAsciiStream();
2248            }
2249            catch (SQLException JavaDoc e)
2250            {
2251                if (isDerbyNet)
2252                    System.out.println("EXPECTED SQL Exception: " + e.getMessage());
2253                else
2254                    TestUtil.dumpSQLExceptions(e);
2255            }
2256            try
2257            {
2258                clob.position("foo",2);
2259            }
2260            catch (SQLException JavaDoc e)
2261            {
2262                if (isDerbyNet)
2263                    System.out.println("EXPECTED SQL Exception: " + e.getMessage());
2264                else
2265                    TestUtil.dumpSQLExceptions(e);
2266            }
2267            try
2268            {
2269                clob.position(clob,2);
2270            }
2271            catch (SQLException JavaDoc e)
2272            {
2273                
2274                if (isDerbyNet)
2275                    System.out.println("EXPECTED SQL Exception: " + e.getMessage());
2276                else
2277                    TestUtil.dumpSQLExceptions(e);
2278            }
2279
2280            System.out.println("clobTest95 finished");
2281        }
2282        catch (SQLException JavaDoc e) {
2283            TestUtil.dumpSQLExceptions(e);
2284        }
2285        catch (Throwable JavaDoc e) {
2286            System.out.println("FAIL -- unexpected exception:" + e.toString());
2287            if (debug) e.printStackTrace(System.out);
2288        }
2289    }
2290
2291
2292    /*
2293        test clob finalizer closes the container
2294        (should only release table and row locks that are read_committed)
2295        need to run prepareCLOBMAIN first
2296        NOTE: this test does not produce output since it needs to call the
2297        garbage collector whose behaviour is unreliable. It is in the test run to
2298        exercise the code (most of the time).
2299    */

2300    private static void clobTest96(Connection JavaDoc conn)
2301    {
2302        ResultSet JavaDoc rs;
2303        Statement JavaDoc stmt;
2304        System.out.println(START + "clobTest96");
2305        try {
2306            stmt = conn.createStatement();
2307            rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
2308            byte[] buff = new byte[128];
2309            Clob JavaDoc[] clobArray = new Clob JavaDoc[numRows];
2310            int[] clobLengthArray = new int[numRows];
2311            int j = 0;
2312            while (rs.next())
2313            {
2314                clobArray[j] = rs.getClob(1);
2315                clobLengthArray[j++] = rs.getInt(2);
2316            }
2317            rs.close();
2318            stmt.close();
2319
2320            for (int i = 0; i < numRows; i++)
2321            {
2322                clobArray[i] = null;
2323            }
2324
2325            System.gc();
2326            System.gc();
2327
2328            // System.out.println("after gc");
2329
// printLockTable(conn);
2330

2331            System.out.println("clobTest96 finished");
2332        }
2333        catch (SQLException JavaDoc e) {
2334            TestUtil.dumpSQLExceptions(e);
2335        }
2336        catch (Throwable JavaDoc e) {
2337            System.out.println("FAIL -- unexpected exception:" + e.toString());
2338            if (debug) e.printStackTrace(System.out);
2339        }
2340    }
2341
2342    /*
2343        test clob finalizer closes the container
2344        (should only release table and row locks that are read_committed)
2345        need to run prepareCLOBMAIN first
2346        NOTE: this test does not produce output since it needs to call the
2347        garbage collector whose behaviour is unreliable. It is in the test run to
2348        exercise the code (most of the time).
2349    */

2350    /*
2351     The bug here is that if we do 2 getBlobs on the same column, we reopen the
2352     container twice, but only remember the 2nd container. Then closing the
2353     container on one of the blobs causes the 2nd one not to work.
2354     (Also, closing both blobs leaves one container open.)
2355    */

2356
2357    private static void bug2(Connection JavaDoc conn)
2358    {
2359        ResultSet JavaDoc rs;
2360        Statement JavaDoc stmt;
2361        System.out.println(START + "bug2");
2362        try {
2363            stmt = conn.createStatement();
2364            rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
2365            byte[] buff = new byte[128];
2366            Clob JavaDoc[] clobArray = new Clob JavaDoc[numRows*2];
2367            int[] clobLengthArray = new int[numRows*2];
2368            int j = 0;
2369            while (rs.next())
2370            {
2371                clobArray[j] = rs.getClob(1);
2372                clobLengthArray[j++] = rs.getInt(2);
2373                clobArray[j] = rs.getClob(1);
2374                clobLengthArray[j++] = rs.getInt(2);
2375            }
2376            rs.close();
2377            stmt.close();
2378
2379            // null out clobs at all the even positions
2380
for (int i = 0; i < numRows*2; i=i+2)
2381            {
2382                clobArray[i] = null;
2383            }
2384
2385            System.gc();
2386            System.gc();
2387
2388            System.out.println("after gc");
2389            // printLockTable(conn);
2390

2391            // access clobs at all the odd positions
2392
for (int i = 1; i < numRows*2+1; i=i+2)
2393            {
2394                if (clobArray[i].length() != clobLengthArray[i])
2395                    System.out.println("Error at array position " + i);
2396            }
2397
2398            System.out.println("clobTest97 finished");
2399        }
2400        catch (SQLException JavaDoc e) {
2401            TestUtil.dumpSQLExceptions(e);
2402        }
2403        catch (Throwable JavaDoc e) {
2404            System.out.println("FAIL -- unexpected exception:" + e.toString());
2405            if (debug) e.printStackTrace(System.out);
2406        }
2407    }
2408
2409    /*
2410        test locking
2411        need to run prepareCLOBMAIN first
2412    */

2413    private static void clobTestGroupfetch(Connection JavaDoc conn)
2414    {
2415        ResultSet JavaDoc rs;
2416        Statement JavaDoc stmt,stmt2;
2417        System.out.println(START + "clobTestGroupFetch");
2418        try
2419        {
2420            stmt = conn.createStatement();
2421            rs = stmt.executeQuery("select a,b from testCLOB_MAIN");
2422            // fetch row back, get the column as a clob.
2423
int clobLength;
2424            while (rs.next())
2425            {
2426                clobLength = rs.getInt(2);
2427                String JavaDoc s = rs.getString(1);
2428                printLockTable(conn);
2429            }
2430            rs.close();
2431
2432            System.out.println("clobTestGroupFetch finished");
2433        }
2434        catch (SQLException JavaDoc e) {
2435            TestUtil.dumpSQLExceptions(e);
2436        }
2437        catch (Throwable JavaDoc e) {
2438            System.out.println("FAIL -- unexpected exception:" + e.toString());
2439            if (debug) e.printStackTrace(System.out);
2440        }
2441    }
2442
2443
2444    /*
2445    This bug is happening because the clob.length() sets the stream to some
2446    position (probably to the end). Then when you do a getString() on the same
2447    column, you are using the same SQLChar object, and you try to call readExternal
2448    on the stream, but since it isn't at the beginning it doesn't work.
2449    */

2450    private static void bug(Connection JavaDoc conn)
2451    {
2452        ResultSet JavaDoc rs;
2453        Statement JavaDoc stmt, stmt2;
2454        System.out.println(START + "bug" );
2455        try
2456        {
2457            stmt = conn.createStatement();
2458            // creating table to fit within default 4k table size, then add large columns
2459
stmt.execute("create table testLongRowClob (a varchar(2000))");
2460            stmt.execute("alter table testLongRowClob add column b varchar(3000)");
2461            stmt.execute("alter table testLongRowClob add column c varchar(2000)");
2462            stmt.execute("alter table testLongRowClob add column d varchar(3000)");
2463            stmt.execute("alter table testLongRowClob add column e CLOB(400k)");
2464            PreparedStatement JavaDoc ps = conn.prepareStatement(
2465                "insert into testLongRowClob values(?,?,?,?,?)");
2466            ps.setString(1,Formatters.padString("blaaa",2000));
2467            ps.setString(2,Formatters.padString("tralaaaa",3000));
2468            ps.setString(3,Formatters.padString("foodar",2000));
2469            ps.setString(4,Formatters.padString("moped",3000));
2470            File JavaDoc file = new File JavaDoc(fileName[1]);
2471            if (file.length() < 10000)
2472                System.out.println("ERROR: wrong file tested");
2473            InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
2474            ps.setAsciiStream(5, fileIn, (int)file.length());
2475            ps.executeUpdate();
2476            fileIn.close();
2477            conn.commit();
2478
2479            Connection JavaDoc conn2 = ij.startJBMS();
2480            // turn off autocommit, otherwise blobs/clobs cannot hang around
2481
// until end of transaction
2482
conn2.setAutoCommit(false);
2483
2484            // printLockTable(conn2);
2485
stmt = conn.createStatement();
2486            rs = stmt.executeQuery("select e from testLongRowClob");
2487            printLockTable(conn2);
2488
2489            // fetch row back, get the column as a clob.
2490
Clob JavaDoc clob = null;
2491            int clobLength, i = 0;
2492            while (rs.next())
2493            {
2494                i++;
2495                clob = rs.getClob(1);
2496                System.out.println("got it");
2497                // bug doesn't happen if the below is commented out
2498
System.out.println("clob length is " + clob.length());
2499                System.out.println("the thing as a string is : \n" + rs.getString(1));
2500                printLockTable(conn2);
2501            }
2502            rs.close();
2503            System.out.println("After closing result set");
2504            printLockTable(conn2);
2505
2506            stmt2 = conn2.createStatement();
2507            stmt2.executeUpdate("update testLongRowClob set e = 'smurfball' where a = 'blaaa'");
2508            printLockTable(conn2);
2509
2510            System.out.println("clob length is " + clob.length());
2511
2512            conn.commit();
2513            conn2.commit();
2514            System.out.println("clobTest92 finished");
2515        }
2516        catch (SQLException JavaDoc e) {
2517            TestUtil.dumpSQLExceptions(e);
2518        }
2519        catch (Throwable JavaDoc e) {
2520            System.out.println("FAIL -- unexpected exception:" + e.toString());
2521            if (debug) e.printStackTrace(System.out);
2522        }
2523    }
2524
2525    // test getAsciiStream, print out the result
2526
// this is just temporary, for comparison with getAsciiStream
2527
private static void clobTest9999(Connection JavaDoc conn) {
2528
2529        ResultSetMetaData JavaDoc met;
2530        ResultSet JavaDoc rs;
2531        Statement JavaDoc stmt;
2532        System.out.println(START + "clobTest9999");
2533        try {
2534            stmt = conn.createStatement();
2535            stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.storage.pageSize','4096')");
2536            stmt.execute("create table clobTest9999 (a CLOB(300k))");
2537            stmt.execute("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.storage.pageSize','0')");
2538
2539            int i = 4;
2540            // prepare an InputStream from the file
2541
File JavaDoc file = new File JavaDoc(fileName[i]);
2542            fileLength[i] = file.length();
2543            InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
2544
2545            System.out.println("===> testing " + fileName[i] + " length = "
2546                                   + fileLength[i]);
2547
2548            // insert a streaming column
2549
PreparedStatement JavaDoc ps = conn.prepareStatement("insert into clobTest9999 values(?)");
2550            ps.setAsciiStream(1, fileIn, (int)fileLength[i]);
2551            ps.executeUpdate();
2552            fileIn.close();
2553
2554            rs = stmt.executeQuery("select a from clobTest9999");
2555            met = rs.getMetaData();
2556            // fetch row back, get the column as a clob.
2557
while (rs.next()) {
2558                // get the first column as a clob
2559
Clob JavaDoc clob = rs.getClob(1);
2560                InputStream JavaDoc fin = clob.getAsciiStream();
2561                int columnSize = 0;
2562                for (;;) {
2563                    int j = fin.read();
2564                    if (j == -1)
2565                        break;
2566                    System.out.print((char) j);
2567                }
2568            }
2569            System.out.println("Finished clobTest9999");
2570
2571        }
2572        catch (SQLException JavaDoc e) {
2573            TestUtil.dumpSQLExceptions(e);
2574        }
2575        catch (Throwable JavaDoc e) {
2576            System.out.println("FAIL -- unexpected exception:" + e.toString());
2577            if (debug) e.printStackTrace(System.out);
2578        }
2579    }
2580
2581
2582    /**
2583     * Test fix for derby-1382.
2584     *
2585     * Test that the getClob() returns the correct value for the clob before and
2586     * after updating the clob when using result sets of type
2587     * TYPE_SCROLL_INSENSITIVE.
2588     *
2589     * The method updateString(int, String) is used to set the value on the
2590     * clob because the method updateBlob(int, Blob) has not yet been
2591     * implemented for DerbyNetClient.
2592     *
2593     * @param conn Connection
2594     * @throws SQLException
2595     */

2596    private static void clobTest10(Connection JavaDoc conn) throws SQLException JavaDoc {
2597        Statement JavaDoc s = conn.createStatement();
2598        s.execute("CREATE TABLE derby1382 (c1 int, c2 clob)");
2599
2600        String JavaDoc clobData = "initial clob ";
2601        PreparedStatement JavaDoc ps =
2602                conn.prepareStatement("insert into derby1382 values (?, ?)");
2603        for (int i=0; i<10; i++) {
2604            ps.setInt(1, i);
2605            ps.setString(2, clobData + i);
2606            ps.execute();
2607        }
2608        ps.close();
2609
2610        Statement JavaDoc scrollStmt = conn.createStatement(
2611                ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
2612        ResultSet JavaDoc rs = scrollStmt.executeQuery("SELECT * FROM derby1382");
2613
2614        String JavaDoc value;
2615        Clob JavaDoc c;
2616
2617        rs.first();
2618        checkContentsBeforeAndAfterUpdatingClob(rs);
2619        rs.next();
2620        checkContentsBeforeAndAfterUpdatingClob(rs);
2621        rs.relative(3);
2622        checkContentsBeforeAndAfterUpdatingClob(rs);
2623        rs.absolute(7);
2624        checkContentsBeforeAndAfterUpdatingClob(rs);
2625        rs.previous();
2626        checkContentsBeforeAndAfterUpdatingClob(rs);
2627        rs.last();
2628        checkContentsBeforeAndAfterUpdatingClob(rs);
2629        rs.previous();
2630        checkContentsBeforeAndAfterUpdatingClob(rs);
2631
2632        rs.close();
2633        scrollStmt.close();
2634
2635        s.execute("DROP TABLE derby1382");
2636        s.close();
2637    }
2638
2639    private static void checkContentsBeforeAndAfterUpdatingClob(ResultSet JavaDoc rs)
2640            throws SQLException JavaDoc
2641    {
2642        Clob JavaDoc c;
2643        String JavaDoc value, expectedValue;
2644        String JavaDoc clobData = "initial clob ";
2645        String JavaDoc updatedClobData = "updated clob ";
2646
2647        c = rs.getClob(2);
2648        // check contents
2649
value = c.getSubString(1, (int)c.length());
2650        expectedValue = clobData + rs.getInt(1);
2651        if (value.compareToIgnoreCase(expectedValue) != 0) {
2652            System.out.println("clobTest10 - Error: wrong clob value");
2653        }
2654        // update contents
2655
value = updatedClobData + rs.getInt(1);
2656        rs.updateString(2, value);
2657        rs.updateRow();
2658        // check update values
2659
rs.next(); // leave the row
2660
rs.previous(); // go back to updated row
2661
c = rs.getClob(2);
2662        // check contents
2663
value = c.getSubString(1, (int)c.length());
2664        expectedValue = updatedClobData + rs.getInt(1);
2665        if (value.compareToIgnoreCase(expectedValue) != 0) {
2666            System.out.println("clobTest10 - Error: wrong clob value");
2667        }
2668    }
2669
2670    /**
2671     * Test fix for derby-1421.
2672     *
2673     * Test that the getClob() returns the correct value for the blob before and
2674     * after updating the Clob using the method updateCharacterStream().
2675     *
2676     * @param conn Connection
2677     * @throws SQLException
2678     */

2679    private static void clobTest14(Connection JavaDoc conn) throws SQLException JavaDoc {
2680        Statement JavaDoc s = conn.createStatement();
2681        s.execute("CREATE TABLE derby1421 (c1 int, c2 clob)");
2682
2683        String JavaDoc clobData = "initial clob ";
2684        PreparedStatement JavaDoc ps =
2685                conn.prepareStatement("insert into derby1421 values (?, ?)");
2686        for (int i=0; i<10; i++) {
2687            ps.setInt(1, i);
2688            ps.setString(2, clobData + i);
2689            ps.execute();
2690        }
2691        ps.close();
2692
2693        Statement JavaDoc scrollStmt = conn.createStatement(
2694                ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
2695        ResultSet JavaDoc rs = scrollStmt.executeQuery("SELECT * FROM derby1421");
2696
2697        rs.first();
2698        updateClobWithUpdateCharacterStream(rs);
2699        rs.next();
2700        updateClobWithUpdateCharacterStream(rs);
2701        rs.relative(3);
2702        updateClobWithUpdateCharacterStream(rs);
2703        rs.absolute(7);
2704        updateClobWithUpdateCharacterStream(rs);
2705        rs.previous();
2706        updateClobWithUpdateCharacterStream(rs);
2707        rs.last();
2708        updateClobWithUpdateCharacterStream(rs);
2709        rs.previous();
2710        updateClobWithUpdateCharacterStream(rs);
2711
2712        rs.close();
2713        scrollStmt.close();
2714
2715        s.execute("DROP TABLE derby1421");
2716        s.close();
2717    }
2718
2719    private static void updateClobWithUpdateCharacterStream(ResultSet JavaDoc rs)
2720            throws SQLException JavaDoc
2721    {
2722        Clob JavaDoc c;
2723        String JavaDoc value, expectedValue;
2724        String JavaDoc clobData = "initial clob ";
2725        String JavaDoc updatedClobData = "updated clob ";
2726
2727        c = rs.getClob(2);
2728        // check contents
2729
value = c.getSubString(1, (int)c.length());
2730        expectedValue = clobData + rs.getInt(1);
2731        if (value.compareToIgnoreCase(expectedValue) != 0) {
2732            System.out.println("clobTest14 - Error: wrong clob value");
2733        }
2734
2735        // update contents
2736
value = (updatedClobData + rs.getInt(1));
2737        Reader JavaDoc updateValue = new StringReader JavaDoc(value);
2738        rs.updateCharacterStream(2, updateValue, value.length());
2739        rs.updateRow();
2740        // check update values
2741
rs.next(); // leave the row
2742
rs.previous(); // go back to updated row
2743
c = rs.getClob(2);
2744        // check contents
2745
value = c.getSubString(1, (int)c.length());
2746        expectedValue = updatedClobData + rs.getInt(1);
2747        if (value.compareToIgnoreCase(expectedValue) != 0) {
2748            System.out.println("clobTest14 - Error: wrong clob value");
2749        }
2750    }
2751
2752    // test behaviour of system with self destructive user
2753
// update a long column underneath a clob
2754
private static void clobTestSelfDestructive(Connection JavaDoc conn)
2755    {
2756        ResultSet JavaDoc rs;
2757        Statement JavaDoc stmt;
2758        System.out.println(START + "clobTestSelfDestructive");
2759        try
2760        {
2761            stmt = conn.createStatement();
2762            rs = stmt.executeQuery("select a,b from testCLOB_MAIN where b = 10000");
2763            byte[] buff = new byte[128];
2764            // fetch row back, get the column as a clob.
2765
Clob JavaDoc clob = null;
2766            InputStream JavaDoc fin;
2767            int clobLength = 0, i = 0;
2768            if (rs.next())
2769            {
2770                i++;
2771                clobLength = rs.getInt(2);
2772                // get the first column as a clob
2773
clob = rs.getClob(1);
2774            }
2775            System.out.println("length of clob chosen is " + clobLength);
2776            fin = clob.getAsciiStream();
2777            int columnSize = 0;
2778
2779            PreparedStatement JavaDoc ps = conn.prepareStatement(
2780                "update testCLOB_MAIN set a = ? where b = 10000");
2781            StringBuffer JavaDoc foo = new StringBuffer JavaDoc();
2782            for (int k = 0; k < 1000; k++)
2783                foo.append('j');
2784            ps.setString(1,foo.toString());
2785            ps.executeUpdate();
2786
2787            System.out.println("After update");
2788
2789            rs = stmt.executeQuery("select a from testCLOB_MAIN where b = 10000");
2790            while (rs.next())
2791            {
2792                int j = 1;
2793                String JavaDoc val = rs.getString(1);
2794                System.out.println("Row " + j + " value.substring(0,50) is " + val.substring(0,50));
2795                j++;
2796            }
2797
2798            while (columnSize < 11000)
2799            {
2800                int size = fin.read(buff);
2801                if (size == -1)
2802                    break;
2803                columnSize += size;
2804                // printing the return from each read is very implementation dependent
2805
}
2806            System.out.println(columnSize + " total bytes read");
2807
2808            if (columnSize != clobLength)
2809                System.out.println("test failed, columnSize should be " + clobLength
2810                    + ", but it is " + columnSize + ", i = " + i);
2811            if (columnSize != clob.length())
2812                System.out.println("test failed, clob.length() should be " + columnSize
2813                    + ", but it is " + clob.length() + ", i = " + i);
2814            conn.rollback();
2815            System.out.println("clobTestSelfDestructive finished");
2816        }
2817        catch (SQLException JavaDoc e)
2818        {
2819            TestUtil.dumpSQLExceptions(e);
2820        }
2821        catch (Throwable JavaDoc e)
2822        {
2823            System.out.println("FAIL -- unexpected exception:" + e.toString());
2824            if (debug) e.printStackTrace(System.out);
2825        }
2826    }
2827
2828
2829    // test behaviour of system with self destructive user
2830
// drop table and see what happens to the clob
2831
// expect an IOException when moving to a new page of the long column
2832
private static void clobTestSelfDestructive2(Connection JavaDoc conn)
2833    {
2834        ResultSet JavaDoc rs;
2835        Statement JavaDoc stmt;
2836        System.out.println(START + "clobTestSelfDestructive2");
2837        try
2838        {
2839            stmt = conn.createStatement();
2840            rs = stmt.executeQuery("select a,b from testCLOB_MAIN where b = 10000");
2841            byte[] buff = new byte[128];
2842            // fetch row back, get the column as a clob.
2843
Clob JavaDoc clob = null;
2844            InputStream JavaDoc fin;
2845            int clobLength = 0, i = 0;
2846            if (rs.next())
2847            {
2848                i++;
2849                clobLength = rs.getInt(2);
2850                // get the first column as a clob
2851
clob = rs.getClob(1);
2852            }
2853            System.out.println("length of clob chosen is " + clobLength);
2854            fin = clob.getAsciiStream();
2855            int columnSize = 0;
2856
2857            stmt.executeUpdate("drop table testCLOB_MAIN");
2858            System.out.println("After drop");
2859
2860            System.out.println("Expect to get an IOException, container has been closed");
2861            while (columnSize < 11000)
2862            {
2863                int size = fin.read(buff);
2864                if (size == -1)
2865                    break;
2866                columnSize += size;
2867                // printing the return from each read is very implementation dependent
2868
}
2869            System.out.println(columnSize + " total bytes read");
2870
2871            conn.rollback();
2872            System.out.println("clobTestSelfDestructive2 finished");
2873        }
2874        catch (SQLException JavaDoc e)
2875        {
2876            TestUtil.dumpSQLExceptions(e);
2877        }
2878        catch (java.io.IOException JavaDoc ioe)
2879        {
2880            System.out.println("EXPECTED IO Exception:" + ioe.getMessage());
2881        }
2882        catch (Throwable JavaDoc e)
2883        {
2884            System.out.println("FAIL -- unexpected exception:" + e.toString());
2885            if (debug) e.printStackTrace(System.out);
2886        }
2887    }
2888
2889
2890
2891    private static void printLockTable(Connection JavaDoc conn)
2892    {
2893        ResultSet JavaDoc rs;
2894        Statement JavaDoc stmt,stmt2;
2895        try
2896        {
2897            System.out.println("\nLock table\n----------------------------------------");
2898            stmt = conn.createStatement();
2899            rs = stmt.executeQuery(
2900                "select xid,type,lockcount,mode,tablename,lockname,state from SYSCS_DIAG.LOCK_TABLE t where t.tableType <> 'S'");
2901            while (rs.next())
2902            {
2903                String JavaDoc xid = rs.getString("xid");
2904                String JavaDoc type = rs.getString("type");
2905                String JavaDoc lockcount = rs.getString("lockcount");
2906                String JavaDoc mode = rs.getString("mode");
2907                String JavaDoc tablename = rs.getString("tablename");
2908                String JavaDoc lockname = rs.getString("lockname");
2909                String JavaDoc state = rs.getString("state");
2910                System.out.println("Lock{xid = " + xid + ", type = " + type +
2911                    ", lockcount = " + lockcount + ", mode = " + mode +
2912                    ", tablename = " + tablename + ", lockname = " + lockname +
2913                    ", state = " + state + " } ");
2914            }
2915            System.out.println("----------------------------------------\n");
2916        }
2917        catch (SQLException JavaDoc e) {
2918            TestUtil.dumpSQLExceptions(e);
2919        }
2920        catch (Throwable JavaDoc e) {
2921            System.out.println("FAIL -- unexpected exception:" + e.toString());
2922            if (debug) e.printStackTrace(System.out);
2923        }
2924    }
2925
2926
2927
2928
2929
2930
2931    private static void unicodeTest()
2932    {
2933        System.out.println(START + "unicodeTest");
2934        try
2935        {
2936            // String to Unicode bytes
2937
byte[] unicodeArray = {0xfffffffe, 0xffffffff, 0x01, 0x68, 0x00, 0x65,
2938                            0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f};
2939            String JavaDoc str = new String JavaDoc (unicodeArray, "Unicode");
2940            System.out.println("First string is : " + str);
2941            // byte[] array = str.getBytes("UnicodeBigUnmarked");
2942
// Unicode bytes to String
2943
// double byte not supported
2944
// str = new String (array, "DoubleByte");
2945
// System.out.println("Second string is : " + str);
2946

2947            byte[] uni2 = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x74, 0x68, 0x65, 0x72, 0x65};
2948            String JavaDoc suni2 = new String JavaDoc (uni2,"Unicode");
2949            System.out.println("uni2 is :" + uni2);
2950
2951            String JavaDoc uni3 = "\u0020\u0021\u0023";
2952            System.out.println("uni3 is :" + uni3);
2953            String JavaDoc uni4 = "\u0061\u0062\u0063";
2954            System.out.println("uni4 is :" + uni4);
2955            System.out.println("uni4 equals abc ? " + uni4.equals("abc"));
2956            String JavaDoc uni5 = "\u0370\u0371\u0372";
2957            System.out.println("uni5 is :" + uni5);
2958            System.out.println("uni5 equals ??? ? " + uni5.equals("???"));
2959            System.out.println("uni5 equals uni5 ? " + uni5.equals(uni5));
2960            String JavaDoc uni6 = "\u05d0\u05d1\u05d2";
2961            System.out.println("uni6 is :" + uni6);
2962            System.out.println("uni5 equals uni6 ? " + uni5.equals(uni6));
2963            System.out.println("uni6 equals uni6 ? " + uni6.equals(uni6));
2964
2965            FileWriter JavaDoc fw;
2966
2967        }
2968        catch (Throwable JavaDoc e)
2969        {
2970            System.out.println("FAIL -- unexpected exception:" + e.toString());
2971            if (debug) e.printStackTrace(System.out);
2972        }
2973    }
2974
2975
2976    /*
2977        Set up a table with all kinds of blob values,
2978        some short (less than 1 page), some long (more than 1 page)
2979        some very large (many pages).
2980        Table has 2 cols: the first is the value, the second is the length of
2981        the value.
2982        (Also sets the fileLength array.)
2983    */

2984    private static void prepareBlobTable(Connection JavaDoc conn)
2985    {
2986        ResultSet JavaDoc rs;
2987        Statement JavaDoc stmt;
2988        System.out.println(START + "prepareBlobTable");
2989        try {
2990            stmt = conn.createStatement();
2991            // creating table to fit within default 4k table size, then add large column
2992
stmt.execute("create table testBlob (b integer)");
2993            stmt.execute("alter table testBlob add column a blob(300k)");
2994            stmt.execute("alter table testBlob add column crc32 BIGINT");
2995            
2996            PreparedStatement JavaDoc ps = conn.prepareStatement(
2997                "insert into testBlob (a, b, crc32) values(?,?,?)");
2998
2999            // insert small strings
3000
insertRow(ps,"".getBytes("US-ASCII"));
3001            insertRow(ps,"you can lead a horse to water but you can't form it into beverage".getBytes("US-ASCII"));
3002            insertRow(ps,"a stitch in time says ouch".getBytes("US-ASCII"));
3003            insertRow(ps,"here is a string with a return \n character".getBytes("US-ASCII"));
3004
3005            // insert larger strings using setBinaryStream
3006
for (int i = 0; i < numFiles; i++)
3007            {
3008                // prepare an InputStream from the file
3009
File JavaDoc file = new File JavaDoc(fileName[i]);
3010                fileLength[i] = file.length();
3011                InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
3012
3013                System.out.println("===> inserting " + basefileName[i] + " length = "
3014                                       + fileLength[i]);
3015
3016                // insert a streaming column
3017
ps.setBinaryStream(1, fileIn, (int)fileLength[i]);
3018                ps.setInt(2, (int)fileLength[i]);
3019                ps.setLong(3, fileCRC32[i]);
3020                ps.executeUpdate();
3021                fileIn.close();
3022            }
3023
3024            // insert a null
3025
ps.setNull(1, Types.BLOB);
3026            ps.setInt(2, 0);
3027            ps.setNull(3, Types.BIGINT);
3028            ps.executeUpdate();
3029
3030            ps.close();
3031            conn.commit();
3032
3033            // set numRows
3034
rs = stmt.executeQuery("select count(*) from testBlob");
3035            int realNumRows = -1;
3036            if (rs.next())
3037                realNumRows = rs.getInt(1);
3038            if (realNumRows <= 0)
3039                System.out.println("FAIL. No rows in table testCLOB_MAIN");
3040            if (realNumRows != numRows)
3041                System.out.println("FAIL. numRows is incorrect");
3042            
3043            stmt.close();
3044            conn.commit();
3045
3046        }
3047        catch (SQLException JavaDoc e) {
3048            TestUtil.dumpSQLExceptions(e);
3049        }
3050        catch (Throwable JavaDoc e) {
3051            System.out.println("FAIL -- unexpected exception:" + e.toString());
3052            if (debug) e.printStackTrace(System.out);
3053        }
3054    }
3055
3056
3057    /*
3058        Set up a table with binary values,
3059        Table has 2 cols: the first is the value, the second is the length of
3060        the value.
3061    */

3062    private static void prepareBinaryTable(Connection JavaDoc conn)
3063    {
3064        ResultSet JavaDoc rs;
3065        Statement JavaDoc stmt;
3066        System.out.println(START + "prepareBinaryTable");
3067        try
3068        {
3069            stmt = conn.createStatement();
3070            stmt.execute("create table testBinary (a blob(80), b integer, crc32 bigint)");
3071            PreparedStatement JavaDoc ps = conn.prepareStatement(
3072                "insert into testBinary values(?,?,?)");
3073
3074            // insert small strings
3075
insertRow(ps,"".getBytes("US-ASCII"));
3076            insertRow(ps,"you can lead a horse to water but you can't form it into beverage".getBytes("US-ASCII"));
3077            insertRow(ps,"a stitch in time says ouch".getBytes("US-ASCII"));
3078            insertRow(ps,"here is a string with a return \n character".getBytes("US-ASCII"));
3079
3080            // insert a null
3081
// ps.setNull(1, Types.BINARY);
3082
// ps.setInt(2, 0);
3083
// ps.executeUpdate();
3084

3085            conn.commit();
3086        }
3087        catch (SQLException JavaDoc e)
3088        {
3089            TestUtil.dumpSQLExceptions(e);
3090        }
3091        catch (Throwable JavaDoc e)
3092        {
3093            System.out.println("FAIL -- unexpected exception:" + e.toString());
3094            if (debug) e.printStackTrace(System.out);
3095        }
3096    }
3097
3098
3099
3100    /*
3101        Set up a table with blobs to search for
3102        most short (less than 1 page), some long (more than 1 page)
3103        some very large (many pages) ??
3104    */

3105    private static void prepareSearchBlobTable(Connection JavaDoc conn)
3106    {
3107        ResultSet JavaDoc rs;
3108        Statement JavaDoc stmt;
3109        System.out.println(START + "prepareSearchBlobTable");
3110        try {
3111            stmt = conn.createStatement();
3112            // creating table to fit within default 4k table size, then add large column
3113
stmt.execute("create table searchBlob (b integer)");
3114            stmt.execute("alter table searchBlob add column a blob(300k)");
3115            stmt.execute("alter table searchBlob add column crc32 BIGINT");
3116            PreparedStatement JavaDoc ps = conn.prepareStatement(
3117                "insert into searchBlob (a, b, crc32) values(?,?,?)");
3118            insertRow(ps,"horse".getBytes("US-ASCII"));
3119            insertRow(ps,"ouch".getBytes("US-ASCII"));
3120            insertRow(ps,"\n".getBytes("US-ASCII"));
3121            insertRow(ps,"".getBytes("US-ASCII"));
3122            insertRow(ps,"Beginning".getBytes("US-ASCII"));
3123            insertRow(ps,"position-69".getBytes("US-ASCII"));
3124            insertRow(ps,"I-am-hiding-here-at-position-5910".getBytes("US-ASCII"));
3125            insertRow(ps,"Position-9907".getBytes("US-ASCII"));
3126
3127            // insert larger blobs using setBinaryStream
3128
for (int i = 0; i < numFiles; i++)
3129            {
3130                // prepare an InputStream from the file
3131
File JavaDoc file = new File JavaDoc(fileName[i]);
3132                fileLength[i] = file.length();
3133                InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
3134
3135                System.out.println("===> inserting " + basefileName[i] + " length = "
3136                                       + fileLength[i]);
3137
3138                // insert a streaming column
3139
ps.setBinaryStream(1, fileIn, (int)fileLength[i]);
3140                ps.setInt(2, (int)fileLength[i]);
3141                ps.setLong(3, fileCRC32[i]);
3142                ps.executeUpdate();
3143                fileIn.close();
3144            }
3145
3146            // insert a null
3147
ps.setNull(1, Types.BLOB);
3148            ps.setInt(2, 0);
3149            ps.setNull(3, Types.BIGINT);
3150            ps.executeUpdate();
3151
3152            conn.commit();
3153        }
3154        catch (SQLException JavaDoc e) {
3155            TestUtil.dumpSQLExceptions(e);
3156        }
3157        catch (Throwable JavaDoc e) {
3158            System.out.println("FAIL -- unexpected exception:" + e.toString());
3159            if (debug) e.printStackTrace(System.out);
3160        }
3161    }
3162
3163
3164    /*
3165        basic test of getBinaryStream
3166        also tests length
3167        need to run prepareBlobTable first
3168    */

3169    private static void blobTest0(Connection JavaDoc conn)
3170    {
3171        ResultSet JavaDoc rs;
3172        Statement JavaDoc stmt;
3173        System.out.println(START + "blobTest0");
3174        try
3175        {
3176            stmt = conn.createStatement();
3177            rs = stmt.executeQuery("select a,b,crc32 from testBlob");
3178            testBlobContents(rs);
3179            stmt.close();
3180            conn.commit();
3181            System.out.println("blobTest0 finished");
3182        }
3183        catch (SQLException JavaDoc e) {
3184            TestUtil.dumpSQLExceptions(e);
3185        }
3186        catch (Throwable JavaDoc e) {
3187            System.out.println("FAIL -- unexpected exception:" + e.toString());
3188            if (debug) e.printStackTrace(System.out);
3189        }
3190    }
3191
3192    /**
3193     * Test the contents of the testBlob table or ResultSet
3194     * with identical shape.
3195     * @param rs
3196     * @throws SQLException
3197     * @throws IOException
3198     */

3199    private static void testBlobContents(ResultSet JavaDoc rs)
3200    throws SQLException JavaDoc, IOException JavaDoc
3201    {
3202        int nullCount = 0;
3203        int rowCount = 0;
3204        byte[] buff = new byte[128];
3205        // fetch row back, get the long varbinary column as a blob.
3206
Blob JavaDoc blob;
3207        int blobLength = 0, i = 0;
3208        while (rs.next()) {
3209            i++;
3210            // get the first column as a clob
3211
blob = rs.getBlob(1);
3212            long crc32 = rs.getLong(3);
3213            boolean crc2Null = rs.wasNull();
3214            if (blob == null) {
3215                if (!crc2Null)
3216                    System.out.println("FAIL: NULL BLOB but non-NULL checksum");
3217                nullCount++;
3218                continue;
3219            }
3220            
3221            rowCount++;
3222            
3223            long blobcrc32 = getStreamCheckSum(blob.getBinaryStream());
3224            
3225            if (blobcrc32 != crc32) {
3226                System.out.println("FAIL: mismatched checksums for blob with length " +
3227                        blob.length());
3228            }
3229            
3230            
3231            InputStream JavaDoc fin = blob.getBinaryStream();
3232            int columnSize = 0;
3233            for (;;) {
3234                int size = fin.read(buff);
3235                if (size == -1)
3236                    break;
3237                columnSize += size;
3238            }
3239            blobLength = rs.getInt(2);
3240            if (columnSize != blobLength)
3241                System.out.println("test failed, columnSize should be " + blobLength
3242                   + ", but it is " + columnSize + ", i = " + i);
3243            if (columnSize != blob.length())
3244                System.out.println("test failed, blob.length() should be " + columnSize
3245                   + ", but it is " + blob.length() + ", i = " + i);
3246        }
3247        System.out.println("Row Count " + rowCount + " Null Row " + nullCount);
3248    }
3249
3250    /*
3251    test getBytes
3252    need to run prepareBlobTable first
3253    */

3254    private static void blobTest2(Connection JavaDoc conn)
3255    {
3256        ResultSet JavaDoc rs;
3257        Statement JavaDoc stmt;
3258        System.out.println(START + "blobTest2");
3259        try
3260        {
3261            stmt = conn.createStatement();
3262            rs = stmt.executeQuery("select a,b from testBlob");
3263            int i = 0, blobLength = 0;
3264            Blob JavaDoc blob;
3265            while (rs.next())
3266            {
3267                i++;
3268                blob = rs.getBlob(1);
3269                if (blob == null)
3270                    continue;
3271                blobLength = rs.getInt(2);
3272                blobclob4BLOB.printInterval(blob, 9905, 50, 0, i, blobLength);
3273                blobclob4BLOB.printInterval(blob, 5910, 150, 1, i, blobLength);
3274                blobclob4BLOB.printInterval(blob, 5910, 50, 2, i, blobLength);
3275                blobclob4BLOB.printInterval(blob, 204, 50, 3, i, blobLength);
3276                blobclob4BLOB.printInterval(blob, 68, 50, 4, i, blobLength);
3277                blobclob4BLOB.printInterval(blob, 1, 50, 5, i, blobLength);
3278                blobclob4BLOB.printInterval(blob, 1, 1, 6, i, blobLength);
3279                blobclob4BLOB.printInterval(
3280                        blob, 1, 0, 7, i, blobLength); // length 0 at start
3281
blobclob4BLOB.printInterval(
3282                        blob, blobLength + 1, 0, 8, i, blobLength); // and end
3283
/*
3284                System.out.println(i + "(0) " + new String(blob.getBytes(9905,50), "US-ASCII"));
3285                System.out.println(i + "(1) " + new String(blob.getBytes(5910,150), "US-ASCII"));
3286                System.out.println(i + "(2) " + new String(blob.getBytes(5910,50), "US-ASCII"));
3287                System.out.println(i + "(3) " + new String(blob.getBytes(204,50), "US-ASCII"));
3288                System.out.println(i + "(4) " + new String(blob.getBytes(68,50), "US-ASCII"));
3289                System.out.println(i + "(5) " + new String(blob.getBytes(1,50), "US-ASCII"));
3290                System.out.println(i + "(6) " + new String(blob.getBytes(1,1), "US-ASCII"));
3291                */

3292                if (blobLength > 100)
3293                {
3294                    byte[] res = blob.getBytes(blobLength-99,200);
3295                    System.out.println(i + "(9) ");
3296                    if (res.length != 100)
3297                        System.out.println("FAIL : length of bytes is " +
3298                            res.length + " should be 100");
3299                    else
3300                        System.out.println(new String JavaDoc(res, "US-ASCII")); // ensure fixed string
3301
}
3302            }
3303            stmt.close();
3304            conn.commit();
3305            System.out.println("blobTest2 finished");
3306        }
3307        catch (SQLException JavaDoc e) {
3308            TestUtil.dumpSQLExceptions(e);
3309        }
3310        catch (Throwable JavaDoc e) {
3311            System.out.println("FAIL -- unexpected exception:" + e.toString());
3312            if (debug) e.printStackTrace(System.out);
3313        }
3314    }
3315
3316
3317    /*
3318    test position with a byte[] argument
3319    need to run prepareBlobTable first
3320    */

3321    private static void blobTest3(Connection JavaDoc conn)
3322    {
3323        ResultSet JavaDoc rs;
3324        Statement JavaDoc stmt;
3325        System.out.println(START + "blobTest3");
3326        try
3327        {
3328            stmt = conn.createStatement();
3329            rs = stmt.executeQuery("select a,b from testBlob");
3330            int i = 0, blobLength = 0;
3331            Blob JavaDoc blob;
3332            while (rs.next())
3333            {
3334                i++;
3335                blob = rs.getBlob(1);
3336                if (blob == null)
3337                    continue;
3338               blobLength = rs.getInt(2);
3339                if (blobLength > 20000)
3340                    continue;
3341                blobLength = rs.getInt(2);
3342                blobclob4BLOB.printPosition(i,"horse",1,blob, blobLength);
3343                blobclob4BLOB.printPosition(i,"ouch",1,blob, blobLength);
3344                blobclob4BLOB.printPosition(i,"\n",1,blob, blobLength);
3345                blobclob4BLOB.printPosition(i,"",1,blob, blobLength);
3346                blobclob4BLOB.printPosition(i,"Beginning",1,blob, blobLength);
3347                blobclob4BLOB.printPosition(i,"Beginning",2,blob, blobLength);
3348                blobclob4BLOB.printPosition(i,"position-69",1,blob, blobLength);
3349                blobclob4BLOB.printPosition(i,"This-is-position-204",1,blob, blobLength);
3350                blobclob4BLOB.printPosition(i,"I-am-hiding-here-at-position-5910",1,blob, blobLength);
3351                blobclob4BLOB.printPosition(i,"I-am-hiding-here-at-position-5910",5910,blob, blobLength);
3352                blobclob4BLOB.printPosition(i,"I-am-hiding-here-at-position-5910",5911,blob, blobLength);
3353                blobclob4BLOB.printPosition(i,"Position-9907",1,blob, blobLength);
3354            }
3355            stmt.close();
3356            conn.commit();
3357            System.out.println("blobTest3 finished");
3358        }
3359        catch (SQLException JavaDoc e) {
3360            TestUtil.dumpSQLExceptions(e);
3361        }
3362        catch (Throwable JavaDoc e) {
3363            System.out.println("FAIL -- unexpected exception:" + e.toString());
3364            if (debug) e.printStackTrace(System.out);
3365        }
3366    }
3367
3368    /*
3369    test position with a Blob argument
3370    need to run prepareBlobTable and prepareSearchBlobTable first
3371    */

3372    private static void blobTest4(Connection JavaDoc conn)
3373    {
3374        ResultSet JavaDoc rs, rs2;
3375        Statement JavaDoc stmt, stmt2;
3376        System.out.println(START + "blobTest4");
3377        try
3378        {
3379            stmt = conn.createStatement();
3380            rs = stmt.executeQuery("select a,b from testBlob");
3381            int i = 0, blobLength = 0;
3382            Blob JavaDoc blob;
3383            while (rs.next())
3384            {
3385                i++;
3386                blob = rs.getBlob(1);
3387                if (blob == null)
3388                    continue;
3389                blobLength = rs.getInt(2);
3390                if (blobLength > 20000)
3391                    {
3392                        System.out.println("testBlob row " + i + " skipped (too large)");
3393                        continue;
3394                    }
3395                // inner loop over table of blobs to search for
3396
// blobs
3397
stmt2 = conn.createStatement();
3398                rs2 = stmt2.executeQuery("select a,b from searchBlob");
3399                int j = 0, blobLength2 = 0;
3400                Blob JavaDoc searchBlob;
3401                String JavaDoc searchStr;
3402                while (rs2.next())
3403                {
3404                    j++;
3405                    searchBlob = rs2.getBlob(1);
3406                    if (searchBlob == null)
3407                        continue;
3408                    blobLength2 = rs2.getInt(2);
3409                    if (blobLength2 > 20000)
3410                    {
3411                        System.out.println("searchBlob row " + j + " skipped (too large)");
3412                        continue;
3413                    }
3414                    if (blobLength2 < 150)
3415                        // get string for printing from bytes in fixed format
3416
searchStr = new String JavaDoc(rs2.getBytes(1),"US-ASCII");
3417                    else
3418                        searchStr = null;
3419
3420                    printPositionBlob(i,searchStr,1,blob,j,searchBlob);
3421                }
3422                stmt2.close();
3423            }
3424            stmt.close();
3425            conn.commit();
3426            System.out.println("blobTest4 finished");
3427        }
3428        catch (SQLException JavaDoc e) {
3429            TestUtil.dumpSQLExceptions(e);
3430        }
3431        catch (Throwable JavaDoc e) {
3432            System.out.println("FAIL -- unexpected exception:" + e.toString());
3433            if (debug) e.printStackTrace(System.out);
3434        }
3435    }
3436    
3437    /**
3438     * Test triggers on BLOB columns.
3439    */

3440    private static void blobTest8Trigger(Connection JavaDoc conn)
3441    {
3442        System.out.println(START + "blobTest8Trigger");
3443        try {
3444            Statement JavaDoc stmt = conn.createStatement();
3445            stmt.executeUpdate("CREATE TABLE blobTest8TriggerA (a BLOB(400k), b int, crc32 BIGINT)");
3446            stmt.executeUpdate("CREATE TABLE blobTest8TriggerB (a BLOB(400k), b int, crc32 BIGINT)");
3447            stmt.executeUpdate(
3448                    "create trigger T8A after update on testBlob " +
3449                    "referencing new as n old as o " +
3450                    "for each row mode db2sql "+
3451                    "insert into blobTest8TriggerA(a, b, crc32) values (n.a, n.b, n.crc32)");
3452            stmt.executeUpdate(
3453                    "create trigger T8B after INSERT on blobTest8TriggerA " +
3454                    "referencing new_table as n " +
3455                    "for each statement mode db2sql "+
3456                    "insert into blobTest8TriggerB(a, b, crc32) select n.a, n.b, n.crc32 from n");
3457            
3458            conn.commit();
3459            ResultSet JavaDoc rs = stmt.executeQuery(
3460                    "select a,b,crc32 from blobTest8TriggerA");
3461            testBlobContents(rs);
3462            rs.close();
3463            conn.commit();
3464            stmt.executeUpdate("UPDATE testBlob set b = b + 0");
3465            conn.commit();
3466            rs = stmt.executeQuery(
3467                "select a,b,crc32 from blobTest8TriggerA");
3468            testBlobContents(rs);
3469            rs.close();
3470            conn.commit();
3471            
3472            rs = stmt.executeQuery(
3473                "select a,b,crc32 from blobTest8TriggerB");
3474            testBlobContents(rs);
3475            rs.close();
3476            conn.commit();
3477            stmt.executeUpdate("DROP TRIGGER T8A");
3478            stmt.executeUpdate("DROP TABLE blobTest8TriggerB");
3479            stmt.executeUpdate("DROP TABLE blobTest8TriggerA");
3480            
3481            stmt.close();
3482            conn.commit();
3483            System.out.println("blobTest8Trigger finished");
3484        }
3485        catch (SQLException JavaDoc e) {
3486            TestUtil.dumpSQLExceptions(e);
3487            do {
3488            e.printStackTrace(System.out);
3489            e = e.getNextException();
3490            } while (e != null);
3491            
3492        }
3493        catch (Throwable JavaDoc e) {
3494            System.out.println("FAIL -- unexpected exception:" + e.toString());
3495            if (debug) e.printStackTrace(System.out);
3496        }
3497        
3498    }
3499
3500
3501    private static void printPositionBlob(
3502        int rowNum,
3503        String JavaDoc searchStr,
3504        long position,
3505        Blob JavaDoc blob,
3506        int searchRowNum,
3507        Blob JavaDoc searchBlob)
3508    {
3509        
3510        try
3511        {
3512            long result = blob.position(searchBlob,position);
3513            if ((searchStr != null) && searchStr.equals("") && (result == 1))
3514                return;
3515            if (result != -1)
3516            {
3517                System.out.print("Found ");
3518                if (searchStr != null)
3519                    System.out.print(searchStr);
3520                else
3521                    System.out.print("blob (row " + searchRowNum + ") ");
3522                System.out.println(" in row " + rowNum + " at position " + result);
3523            }
3524        }
3525        catch (SQLException JavaDoc e)
3526        {
3527            TestUtil.dumpSQLExceptions(e);
3528        }
3529    }
3530
3531    /* datatype tests */
3532
3533    // make sure blobs work for regular varbinary fields
3534
// also test length method
3535
private static void blobTest51(Connection JavaDoc conn) {
3536
3537        ResultSetMetaData JavaDoc met;
3538        ResultSet JavaDoc rs;
3539        Statement JavaDoc stmt;
3540        System.out.println(START + "blobTest51");
3541        try {
3542            stmt = conn.createStatement();
3543            stmt.execute("create table testVarbinary (a blob(13))");
3544
3545            PreparedStatement JavaDoc ps = conn.prepareStatement(
3546                "insert into testVarbinary values(?)");
3547            String JavaDoc val = "";
3548            
3549            for (int i = 0; i < 10; i++)
3550            {
3551                // insert a string
3552
ps.setBytes(1, val.getBytes("US-ASCII"));
3553                ps.executeUpdate();
3554                val = val.trim() + "x";
3555            }
3556
3557            rs = stmt.executeQuery("select a from testVarbinary");
3558            met = rs.getMetaData();
3559            byte[] buff = new byte[128];
3560            int j = 0;
3561            // fetch all rows back, get the columns as clobs.
3562
while (rs.next())
3563            {
3564                // get the first column as a clob
3565
Blob JavaDoc blob = rs.getBlob(1);
3566                if (blob == null)
3567                    continue;
3568                InputStream JavaDoc fin = blob.getBinaryStream();
3569                int columnSize = 0;
3570                for (;;)
3571                {
3572                    int size = fin.read(buff);
3573                    if (size == -1)
3574                        break;
3575                    columnSize += size;
3576                }
3577                if (columnSize != j)
3578                    System.out.println("FAIL - Expected blob size : " + j +
3579                        " Got blob size : " + columnSize);
3580                if (blob.length() != j)
3581                    System.out.println("FAIL - Expected blob length : " + j +
3582                        " Got blob length : " + blob.length());
3583                j++;
3584            }
3585            ps.close();
3586            stmt.close();
3587            conn.commit();
3588            System.out.println("blobTest51 finished");
3589        }
3590        catch (SQLException JavaDoc e) {
3591            TestUtil.dumpSQLExceptions(e);
3592        }
3593        catch (Throwable JavaDoc e) {
3594            System.out.println("FAIL -- unexpected exception:" + e.toString());
3595            if (debug) e.printStackTrace(System.out);
3596        }
3597    }
3598
3599
3600   // make sure cannot get a blob from an int column
3601
private static void blobTest52(Connection JavaDoc conn) {
3602
3603        Statement JavaDoc stmt = null;
3604        System.out.println(START + "blobTest52");
3605        try {
3606            stmt = conn.createStatement();
3607            stmt.execute("create table testInteger2 (a integer)");
3608
3609            PreparedStatement JavaDoc ps = conn.prepareStatement("insert into testInteger2 values(158)");
3610            ps.executeUpdate();
3611            ps.close();
3612
3613            ResultSet JavaDoc rs = stmt.executeQuery("select a from testInteger2");
3614            while (rs.next()) {
3615                // get the first column as a clob
3616
try {
3617                    Blob JavaDoc blob = rs.getBlob(1);
3618                    System.out.println("FAIL fetched java.sql.Blob from INT column");
3619                } catch (SQLException JavaDoc e) {
3620                    TestUtil.dumpSQLExceptions(e,
3621                            "22005".equals(e.getSQLState()) ||
3622                            "XCL12".equals(e.getSQLState()));
3623                    break;
3624                }
3625            }
3626            stmt.close();
3627            conn.commit();
3628           
3629        }
3630        catch (SQLException JavaDoc e) {
3631            TestUtil.dumpSQLExceptions(e);
3632        }
3633        catch (Throwable JavaDoc e) {
3634            System.out.println("FAIL -- unexpected exception:" + e.toString());
3635            if (debug) e.printStackTrace(System.out);
3636        }
3637        System.out.println("blobTest52 finished");
3638    }
3639
3640
3641   // test creating a blob column, currently this doesn't work since we don't
3642
// have a blob datatype (get a syntax error on the create table statement)
3643
private static void blobTest53(Connection JavaDoc conn) {
3644
3645        ResultSetMetaData JavaDoc met;
3646        ResultSet JavaDoc rs;
3647        Statement JavaDoc stmt;
3648        System.out.println(START + "blobTest53");
3649        try {
3650            stmt = conn.createStatement();
3651            stmt.execute("create table testBlobColumn (a blob(1K))");
3652            stmt.close();
3653            System.out.println("blobTest53 finished");
3654        }
3655        catch (SQLException JavaDoc e) {
3656            TestUtil.dumpSQLExceptions(e);
3657        }
3658        catch (Throwable JavaDoc e) {
3659            System.out.println("FAIL -- unexpected exception:" + e.toString());
3660            if (debug) e.printStackTrace(System.out);
3661        }
3662    }
3663
3664    /*
3665        make sure setBlob doesn't work for an int column
3666        need to run prepareBlobTable first
3667    */

3668    private static void blobTest54(Connection JavaDoc conn)
3669    {
3670        ResultSet JavaDoc rs;
3671        Statement JavaDoc stmt;
3672        System.out.println(START + "blobTest54");
3673        try
3674        {
3675            stmt = conn.createStatement();
3676            stmt.execute("create table testBlob2 (a integer, b integer)");
3677            PreparedStatement JavaDoc ps = conn.prepareStatement(
3678                "insert into testBlob2 values(?,?)");
3679            rs = stmt.executeQuery("select a,b from testBlob");
3680            Blob JavaDoc blob;
3681            int blobLength;
3682            while (rs.next())
3683            {
3684                // get the first column as a blob
3685
blob = rs.getBlob(1);
3686                if (blob == null)
3687                    continue;
3688                blobLength = rs.getInt(2);
3689                try {
3690                    ps.setBlob(1,blob);
3691                    ps.setInt(2,blobLength);
3692                    ps.executeUpdate();
3693                    System.out.println("FAIL setBlob worked on INT column");
3694                } catch (SQLException JavaDoc e) {
3695                    TestUtil.dumpSQLExceptions(e,
3696                            "22005".equals(e.getSQLState()) ||
3697                            "XCL12".equals(e.getSQLState()));
3698                     break;
3699                }
3700            }
3701            rs.close();
3702            conn.commit();
3703            stmt.close();
3704            
3705        }
3706        catch (SQLException JavaDoc e) {
3707            TestUtil.dumpSQLExceptions(e);
3708        }
3709        catch (Throwable JavaDoc e) {
3710            System.out.println("FAIL -- unexpected exception:" + e.toString());
3711            if (debug) e.printStackTrace(System.out);
3712        }
3713        System.out.println("blobTest54 finished");
3714    }
3715
3716
3717    /*
3718    test raising of exceptions
3719    need to run prepareBlobTable first
3720    */

3721    private static void blobTest6(Connection JavaDoc conn)
3722    {
3723        ResultSet JavaDoc rs;
3724        Statement JavaDoc stmt;
3725        System.out.println(START + "blobTest6");
3726        try
3727        {
3728            stmt = conn.createStatement();
3729            rs = stmt.executeQuery("select a,b from testBlob");
3730            int i = 0, blobLength = 0;
3731            Blob JavaDoc blob;
3732            while (rs.next())
3733            {
3734                if (i > 0)
3735                    break;
3736                i++;
3737                blob = rs.getBlob(1);
3738                if (blob == null)
3739                    continue;
3740                blobLength = rs.getInt(2);
3741                // test end cases
3742

3743                // 0 or negative position value
3744
try
3745                {
3746                    blob.getBytes(0,5);
3747                }
3748                catch (SQLException JavaDoc e)
3749                {
3750                    TestUtil.dumpSQLExceptions(e,isOutOfBoundException(e));
3751                }
3752                // negative length value
3753
try
3754                {
3755                    blob.getBytes(1,-76);
3756                }
3757                catch (SQLException JavaDoc e)
3758                {
3759                    TestUtil.dumpSQLExceptions(e,isOutOfBoundException(e));
3760                }
3761                // zero length value
3762
try
3763                {
3764                    blob.getBytes(1,-1);
3765                }
3766                catch (SQLException JavaDoc e)
3767                {
3768                    TestUtil.dumpSQLExceptions(e,isOutOfBoundException(e));
3769                }
3770                // before begin length 0
3771
try {
3772                    blob.getBytes(0,0);
3773                } catch (SQLException JavaDoc e) {
3774                    TestUtil.dumpSQLExceptions(e,isOutOfBoundException(e));
3775                }
3776                // after end length 0
3777
try {
3778                    blob.getBytes(blobLength + 2,0);
3779                } catch (SQLException JavaDoc e) {
3780                    TestUtil.dumpSQLExceptions(e,isOutOfBoundException(e));
3781                }
3782                // 0 or negative position value
3783
try
3784                {
3785                    blob.position(new byte[0],-4000);
3786                }
3787                catch (SQLException JavaDoc e)
3788                {
3789                    TestUtil.dumpSQLExceptions(e,isOutOfBoundException(e));
3790                }
3791                // null pattern
3792
try
3793                {
3794                    // bug 5247 in network server (NPE)
3795
blob.position((byte[]) null,5);
3796                }
3797                catch (SQLException JavaDoc e)
3798                {
3799                    TestUtil.dumpSQLExceptions(e,isNullSearchPattern(e));
3800                }
3801                // 0 or negative position value
3802
try
3803                {
3804                    blob.position(blob,-42);
3805                }
3806                catch (SQLException JavaDoc e)
3807                {
3808                    TestUtil.dumpSQLExceptions(e,isOutOfBoundException(e));
3809                }
3810                // null pattern
3811
try
3812                {
3813                    blob.position((Blob JavaDoc) null,5);
3814                }
3815                catch (SQLException JavaDoc e)
3816                {
3817                    TestUtil.dumpSQLExceptions(e,isNullSearchPattern(e));
3818                }
3819            }
3820            stmt.close();
3821            conn.commit();
3822            System.out.println("blobTest6 finished");
3823        }
3824        catch (SQLException JavaDoc e) {
3825            TestUtil.dumpSQLExceptions(e);
3826        }
3827        catch (Throwable JavaDoc e) {
3828            if (e instanceof NullPointerException JavaDoc)
3829            {
3830                if (isDerbyNet)
3831                    System.out.println("NullPointerException: KNOWN JCC issue Bug 5247");
3832            }
3833            else
3834            {
3835            System.out.println("FAIL -- unexpected exception:" + e.toString());
3836            if (debug) e.printStackTrace(System.out);
3837            }
3838        }
3839    }
3840
3841    /*
3842        test setBlob
3843        need to run prepareBlobTable first
3844    */

3845    private static void blobTest7(Connection JavaDoc conn)
3846    {
3847        ResultSet JavaDoc rs, rs2;
3848        Statement JavaDoc stmt1, stmt2;
3849        System.out.println(START + "blobTest7");
3850        try
3851        {
3852            stmt1 = conn.createStatement();
3853            stmt1.execute("create table testBlobX (a blob(300K), b integer)");
3854            PreparedStatement JavaDoc ps = conn.prepareStatement(
3855                "insert into testBlobX values(?,?)");
3856            stmt2 = conn.createStatement();
3857            rs = stmt2.executeQuery("select a,b from testBlob");
3858            Blob JavaDoc blob;
3859            int blobLength;
3860            while (rs.next())
3861            {
3862                // get the first column as a blob
3863
blob = rs.getBlob(1);
3864                if (blob == null)
3865                    continue;
3866                blobLength = rs.getInt(2);
3867                ps.setBlob(1,blob);
3868                ps.setInt(2,blobLength);
3869                ps.executeUpdate();
3870            }
3871            rs.close();
3872            conn.commit();
3873
3874            rs2 = stmt2.executeQuery("select a,b from testBlobX");
3875            Blob JavaDoc blob2;
3876            int blobLength2, j = 0;
3877            while (rs2.next())
3878            {
3879                j++;
3880                // get the first column as a clob
3881
blob2 = rs2.getBlob(1);
3882                if (blob2 == null)
3883                    continue;
3884                blobLength2 = rs2.getInt(2);
3885                if (blob2.length() != blobLength2)
3886                    System.out.println("FAILED at row " + j);
3887            }
3888            rs2.close();
3889
3890            stmt1.close();
3891            stmt2.close();
3892            conn.commit();
3893            System.out.println("blobTest7 finished");
3894        }
3895        catch (SQLException JavaDoc e) {
3896            TestUtil.dumpSQLExceptions(e);
3897        }
3898        catch (Throwable JavaDoc e) {
3899            System.out.println("blobTest7 FAIL -- unexpected exception:" + e.toString());
3900            e.fillInStackTrace();
3901            if (debug) e.printStackTrace(System.out);
3902        }
3903    }
3904
3905
3906    /* advanced tests */
3907
3908    // make sure blob is still around after we go to the next row,
3909
// after we close the result set, and after we close the statement
3910
private static void blobTest91(Connection JavaDoc conn)
3911    {
3912        ResultSet JavaDoc rs;
3913        Statement JavaDoc stmt;
3914        System.out.println(START + "blobTest91");
3915        try {
3916            stmt = conn.createStatement();
3917            rs = stmt.executeQuery("select a,b from testBlob");
3918            byte[] buff = new byte[128];
3919            Blob JavaDoc[] blobArray = new Blob JavaDoc[numRows];
3920            int[] blobLengthArray = new int[numRows];
3921            int j = 0;
3922            while (rs.next())
3923            {
3924                blobArray[j] = rs.getBlob(1);
3925                blobLengthArray[j++] = rs.getInt(2);
3926            }
3927            rs.close();
3928            stmt.close();
3929
3930            for (int i = 0; i < numRows; i++)
3931            {
3932                if (blobArray[i] == null)
3933                    {
3934                        System.out.println("row " + i + " is null, skipped");
3935                        continue;
3936                    }
3937                InputStream JavaDoc fin = blobArray[i].getBinaryStream();
3938                int columnSize = 0;
3939                for (;;) {
3940                    int size = fin.read(buff);
3941                    if (size == -1)
3942                        break;
3943                    columnSize += size;
3944                }
3945                if (columnSize != blobLengthArray[i])
3946                    System.out.println("test failed, columnSize should be " +
3947                        blobLengthArray[i] + ", but it is " + columnSize + ", i = " + i);
3948                if (columnSize != blobArray[i].length())
3949                    System.out.println("test failed, blobArray[i].length() should be " + columnSize
3950                       + ", but it is " + blobArray[i].length() + ", i = " + i);
3951                System.out.println("done row " + i + ", length was " + blobLengthArray[i]);
3952            }
3953            System.out.println("blobTest91 finished");
3954        }
3955        catch (SQLException JavaDoc e) {
3956            TestUtil.dumpSQLExceptions(e);
3957        }
3958        catch (Throwable JavaDoc e) {
3959            System.out.println("FAIL -- unexpected exception:" + e.toString());
3960            if (debug) e.printStackTrace(System.out);
3961        }
3962    }
3963
3964
3965    /*
3966        test locking
3967        need to run prepareBlobTable first
3968    */

3969    private static void blobTest92(Connection JavaDoc conn)
3970    {
3971        ResultSet JavaDoc rs;
3972        Statement JavaDoc stmt,stmt2;
3973        System.out.println(START + "blobTest92");
3974        try
3975        {
3976            stmt = conn.createStatement();
3977            rs = stmt.executeQuery("select a,b from testBlob");
3978            // fetch row back, get the column as a blob.
3979
Blob JavaDoc blob = null, shortBlob = null;
3980            int blobLength;
3981            while (rs.next())
3982            {
3983                blobLength = rs.getInt(2);
3984                if (blobLength == 10000)
3985                    blob = rs.getBlob(1);
3986                if (blobLength == 26)
3987                    shortBlob = rs.getBlob(1);
3988            }
3989            rs.close();
3990
3991            Connection JavaDoc conn2 = ij.startJBMS();
3992            // turn off autocommit, otherwise blobs/clobs cannot hang around
3993
// until end of transaction
3994
conn2.setAutoCommit(false);
3995            if (!TestUtil.isNetFramework())
3996            {
3997            // Note: Locks held until the end of transaction only for embedded.
3998
// Network Server cannot differentiate a getBlob from a getBytes so
3999
// does not hold locks for blob calls (DERBY-255)
4000
// The LOB is materialized on the client so we do not need to hold locks.
4001
// One ugly thing about this test is that these rows are used by other tests.
4002
// If this tests fails and the rows get updated, other tests can get
4003
// NullPointer exceptions.
4004

4005            // Update should go through since we don't get any locks on blobs
4006
// that are not long columns
4007
stmt2 = conn2.createStatement();
4008            stmt2.executeUpdate("update testBlob set a = null where b = 26");
4009            if (shortBlob.length() != 26)
4010                System.out.println("FAILED: blob length changed to " + shortBlob.length());
4011            // should timeout waiting for the lock to do this
4012

4013                stmt2 = conn2.createStatement();
4014                stmt2.executeUpdate("update testBlob set b = b + 1 where b = 10000");
4015                throw new Exception JavaDoc("FAIL: Should have gotten lock timeout");
4016            }
4017            else
4018            {
4019                System.out.println("Locks not held by Network Server for Blobs since they are materialized on client");
4020            }
4021            conn.commit();
4022            conn2.commit();
4023            System.out.println("blobTest92 finished");
4024        }
4025        catch (SQLException JavaDoc e) {
4026            TestUtil.dumpSQLExceptions(e);
4027        }
4028        catch (Throwable JavaDoc e) {
4029            System.out.println("FAIL -- unexpected exception:" + e.toString());
4030            if (debug) e.printStackTrace(System.out);
4031        }
4032    }
4033
4034
4035    /*
4036        test locking with a long row + long column
4037    */

4038    private static void blobTest93(Connection JavaDoc conn)
4039    {
4040        ResultSet JavaDoc rs;
4041        Statement JavaDoc stmt, stmt2;
4042        System.out.println(START + "blobTest93");
4043        try
4044        {
4045            stmt = conn.createStatement();
4046            // creating table to fit within default 4k table size, then add large columns
4047
stmt.execute("create table testLongRowBlob (a varchar(2000))");
4048            stmt.execute("alter table testLongRowBlob add column b varchar(3000)");
4049            stmt.execute("alter table testLongRowBlob add column c varchar(2000)");
4050            stmt.execute("alter table testLongRowBlob add column d varchar(3000)");
4051            stmt.execute("alter table testLongRowBlob add column e blob(300k)");
4052            PreparedStatement JavaDoc ps = conn.prepareStatement(
4053                "insert into testLongRowBlob values(?,?,?,?,?)");
4054            ps.setString(1,Formatters.padString("blaaa",2000));
4055            ps.setString(2,Formatters.padString("tralaaaa",3000));
4056            ps.setString(3,Formatters.padString("foodar",2000));
4057            ps.setString(4,Formatters.padString("moped",3000));
4058            File JavaDoc file = new File JavaDoc(fileName[1]);
4059            if (file.length() < 10000)
4060                System.out.println("ERROR: wrong file tested");
4061            InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
4062            
4063            ps.setBinaryStream(5, fileIn, (int)file.length());
4064            ps.executeUpdate();
4065            fileIn.close();
4066            conn.commit();
4067
4068            stmt = conn.createStatement();
4069            rs = stmt.executeQuery("select e from testLongRowBlob");
4070            Blob JavaDoc blob = null;
4071            while (rs.next()) {
4072                blob = rs.getBlob(1);
4073                checkBlobAgainstFile(1, blob);
4074           }
4075            rs.close();
4076
4077            Connection JavaDoc conn2 = ij.startJBMS();
4078            // turn off autocommit, otherwise blobs/clobs cannot hang around
4079
// until end of transaction
4080
conn2.setAutoCommit(false);
4081            // the following should timeout
4082
stmt2 = conn2.createStatement();
4083            stmt2.executeUpdate("update testLongRowBlob set e = null where a = 'blaaa'");
4084
4085            conn.commit();
4086            conn2.commit();
4087            System.out.println("blobTest93 finished");
4088        }
4089        catch (SQLException JavaDoc e) {
4090            TestUtil.dumpSQLExceptions(e);
4091        }
4092        catch (Throwable JavaDoc e) {
4093            System.out.println("FAIL -- unexpected exception:" + e.toString());
4094            if (debug) e.printStackTrace(System.out);
4095        }
4096    }
4097
4098
4099    /*
4100        test accessing blob after commit
4101        need to run prepareCLOBMAIN first
4102    */

4103    private static void blobTest94(Connection JavaDoc conn)
4104    {
4105        ResultSet JavaDoc rs;
4106        Statement JavaDoc stmt;
4107        System.out.println(START + "blobTest94");
4108        try
4109        {
4110            stmt = conn.createStatement();
4111            rs = stmt.executeQuery("select a,b from testBlob");
4112            // fetch row back, get the column as a blob.
4113
Blob JavaDoc blob = null, shortBlob = null;
4114            int blobLength;
4115            while (rs.next())
4116            {
4117                blobLength = rs.getInt(2);
4118                if (blobLength == 10000)
4119                    blob = rs.getBlob(1);
4120                if (blobLength == 26)
4121                    shortBlob = rs.getBlob(1);
4122            }
4123            rs.close();
4124            conn.commit();
4125
4126            // no problem accessing this after commit since it is in memory
4127
if (shortBlob != null)
4128            System.out.println("shortBlob length after commit is " + shortBlob.length());
4129            // these should all give blob/clob data unavailable exceptions
4130

4131            try
4132            {
4133                blob.length();
4134            }
4135            catch (SQLException JavaDoc e)
4136            {
4137                TestUtil.dumpSQLExceptions(e);
4138            }
4139            try
4140            {
4141                blob.getBytes(2,3);
4142            }
4143            catch (SQLException JavaDoc e)
4144            {
4145                TestUtil.dumpSQLExceptions(e);
4146            }
4147            try
4148            {
4149                blob.getBinaryStream();
4150            }
4151            catch (SQLException JavaDoc e)
4152            {
4153                TestUtil.dumpSQLExceptions(e);
4154            }
4155            try
4156            {
4157                blob.position("foo".getBytes("US-ASCII"),2);
4158            }
4159            catch (SQLException JavaDoc e)
4160            {
4161                TestUtil.dumpSQLExceptions(e);
4162            }
4163            try
4164            {
4165                blob.position(blob,2);
4166            }
4167            catch (SQLException JavaDoc e)
4168            {
4169                TestUtil.dumpSQLExceptions(e);
4170            }
4171
4172            System.out.println("blobTest94 finished");
4173        }
4174        catch (SQLException JavaDoc e) {
4175            TestUtil.dumpSQLExceptions(e);
4176        }
4177        catch (Throwable JavaDoc e) {
4178            System.out.println("FAIL -- unexpected exception:" + e.toString());
4179            if (debug) e.printStackTrace(System.out);
4180        }
4181
4182    }
4183
4184    /*
4185        test accessing blob after closing the connection
4186        need to run prepareCLOBMAIN first
4187    */

4188    private static void blobTest95(Connection JavaDoc conn)
4189    {
4190        ResultSet JavaDoc rs;
4191        Statement JavaDoc stmt;
4192        System.out.println(START + "blobTest95");
4193        try
4194        {
4195            stmt = conn.createStatement();
4196            rs = stmt.executeQuery("select a,b from testBlob");
4197            // fetch row back, get the column as a blob.
4198
Blob JavaDoc blob = null, shortBlob = null;
4199            int blobLength;
4200            while (rs.next())
4201            {
4202                blobLength = rs.getInt(2);
4203                if (blobLength == 10000)
4204                    blob = rs.getBlob(1);
4205                if (blobLength == 26)
4206                    shortBlob = rs.getBlob(1);
4207            }
4208            rs.close();
4209            conn.rollback();
4210            conn.close();
4211
4212            try {
4213                // no problem accessing this after commit since it is in memory
4214
System.out.println("shortBlob length after closing the connection is " + shortBlob.length());
4215            }
4216            catch (SQLException JavaDoc e)
4217            {
4218                expectedExceptionForNSOnly (e);
4219            }
4220
4221                // these should all give blob/clob data unavailable exceptions
4222
try
4223            {
4224                blob.length();
4225            }
4226            catch (SQLException JavaDoc e)
4227            {
4228                expectedExceptionForNSOnly (e);
4229            }
4230            try
4231            {
4232                blob.getBytes(2,3);
4233            }
4234            catch (SQLException JavaDoc e)
4235            {
4236                expectedExceptionForNSOnly (e);
4237            }
4238            try
4239            {
4240                blob.getBinaryStream();
4241            }
4242            catch (SQLException JavaDoc e)
4243            {
4244                expectedExceptionForNSOnly (e);
4245            }
4246            try
4247            {
4248                blob.position("foo".getBytes("US-ASCII"),2);
4249            }
4250            catch (SQLException JavaDoc e)
4251            {
4252                expectedExceptionForNSOnly (e);
4253            }
4254            try
4255            {
4256                blob.position(blob,2);
4257            }
4258            catch (SQLException JavaDoc e)
4259            {
4260                expectedExceptionForNSOnly (e);
4261            }
4262
4263            // restart the connection
4264
conn = ij.startJBMS();
4265            conn.setAutoCommit(false);
4266
4267            System.out.println("blobTest95 finished");
4268        }
4269        catch (SQLException JavaDoc e) {
4270            TestUtil.dumpSQLExceptions(e);
4271        }
4272        catch (Throwable JavaDoc e) {
4273            System.out.println("FAIL -- unexpected exception:" + e.toString());
4274            if (debug) e.printStackTrace(System.out);
4275        }
4276    }
4277
4278
4279    /*
4280        test blob finalizer closes the container
4281        (should only release table and row locks that are read_committed)
4282        need to run prepareCLOBMAIN first
4283        NOTE: this test does not produce output since it needs to call the
4284        garbage collector whose behaviour is unreliable. It is in the test run to
4285        exercise the code (most of the time).
4286    */

4287    private static void blobTest96(Connection JavaDoc conn)
4288    {
4289        ResultSet JavaDoc rs;
4290        Statement JavaDoc stmt;
4291        System.out.println(START + "blobTest96");
4292        try {
4293            stmt = conn.createStatement();
4294            rs = stmt.executeQuery("select a,b from testBlob");
4295            byte[] buff = new byte[128];
4296            Blob JavaDoc[] blobArray = new Blob JavaDoc[numRows];
4297            int[] blobLengthArray = new int[numRows];
4298            int j = 0;
4299            while (rs.next())
4300            {
4301                blobArray[j] = rs.getBlob(1);
4302                blobLengthArray[j++] = rs.getInt(2);
4303            }
4304            rs.close();
4305            stmt.close();
4306
4307            // printLockTable(conn);
4308

4309            for (int i = 0; i < numRows; i++)
4310            {
4311                blobArray[i] = null;
4312            }
4313
4314            // printLockTable(conn);
4315

4316            System.gc();
4317            System.gc();
4318
4319            // System.out.println("after gc");
4320
// printLockTable(conn);
4321

4322            System.out.println("blobTest96 finished");
4323        }
4324        catch (SQLException JavaDoc e) {
4325            TestUtil.dumpSQLExceptions(e);
4326        }
4327        catch (Throwable JavaDoc e) {
4328            System.out.println("FAIL -- unexpected exception:" + e.toString());
4329            if (debug) e.printStackTrace(System.out);
4330        }
4331    }
4332
4333    
4334    
4335    /**
4336     * Test fix for derby-1382.
4337     *
4338     * Test that the getBlob() returns the correct value for the blob before and
4339     * after updating the blob when using result sets of type
4340     * TYPE_SCROLL_INSENSITIVE.
4341     *
4342     * The method updateBytes(int, byte[]) is used to set the value on the
4343     * clob because the method updateClob(int, Clob) has not yet been
4344     * implemented for DerbyNetClient.
4345
4346     *
4347     * @param conn Connection
4348     * @throws SQLException
4349     */

4350    private static void blobTest10(Connection JavaDoc conn) throws SQLException JavaDoc {
4351        Statement JavaDoc s = conn.createStatement();
4352        s.execute("CREATE TABLE derby1382 (c1 int, c2 blob)");
4353
4354        String JavaDoc blobData = "initial blob ";
4355        PreparedStatement JavaDoc ps =
4356                conn.prepareStatement("insert into derby1382 values (?, ?)");
4357        for (int i=0; i<10; i++) {
4358            ps.setInt(1, i);
4359            ps.setBytes(2, (blobData + i).getBytes());
4360            ps.execute();
4361        }
4362        ps.close();
4363
4364        Statement JavaDoc scrollStmt = conn.createStatement(
4365                ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
4366        ResultSet JavaDoc rs = scrollStmt.executeQuery("SELECT * FROM derby1382");
4367
4368        rs.first();
4369        checkContentsBeforeAndAfterUpdatingBlob(rs);
4370        rs.next();
4371        checkContentsBeforeAndAfterUpdatingBlob(rs);
4372        rs.relative(3);
4373        checkContentsBeforeAndAfterUpdatingBlob(rs);
4374        rs.absolute(7);
4375        checkContentsBeforeAndAfterUpdatingBlob(rs);
4376        rs.previous();
4377        checkContentsBeforeAndAfterUpdatingBlob(rs);
4378        rs.last();
4379        checkContentsBeforeAndAfterUpdatingBlob(rs);
4380        rs.previous();
4381        checkContentsBeforeAndAfterUpdatingBlob(rs);
4382
4383        rs.close();
4384        scrollStmt.close();
4385
4386        s.execute("DROP TABLE derby1382");
4387        s.close();
4388    }
4389
4390    private static void checkContentsBeforeAndAfterUpdatingBlob(ResultSet JavaDoc rs)
4391            throws SQLException JavaDoc
4392    {
4393        Blob JavaDoc b;
4394        byte[] value, expectedValue;
4395        String JavaDoc blobData = "initial blob ";
4396        String JavaDoc updatedBlobData = "updated blob ";
4397        
4398        b = rs.getBlob(2);
4399        // check contents
4400
value = b.getBytes(1, blobData.length() + 1);
4401        expectedValue = (blobData + rs.getInt(1)).getBytes();
4402        if (!Arrays.equals(value, expectedValue)) {
4403            System.out.println("blobTest10 - Error: wrong blob value");
4404        }
4405
4406        // update contents
4407
value = (updatedBlobData + rs.getInt(1)).getBytes();
4408        rs.updateBytes(2, value);
4409        rs.updateRow();
4410        // check update values
4411
rs.next(); // leave the row
4412
rs.previous(); // go back to updated row
4413
b = rs.getBlob(2);
4414        // check contents
4415
value = b.getBytes(1, updatedBlobData.length() + 1);
4416        expectedValue = (updatedBlobData + rs.getInt(1)).getBytes();
4417        if (!Arrays.equals(value, expectedValue))
4418        {
4419            System.out.println("blobTest10 - Error: wrong blob value");
4420        }
4421    }
4422
4423    /**
4424     * Test fix for derby-1421.
4425     *
4426     * Test that the getBlob() returns the correct value for the blob before and
4427     * after updating the blob using the method updateBinaryStream().
4428     *
4429     * @param conn Connection
4430     * @throws SQLException
4431     */

4432    private static void blobTest11(Connection JavaDoc conn) throws SQLException JavaDoc {
4433        Statement JavaDoc s = conn.createStatement();
4434        s.execute("CREATE TABLE derby1421 (c1 int, c2 blob)");
4435
4436        String JavaDoc blobData = "initial blob ";
4437        PreparedStatement JavaDoc ps =
4438                conn.prepareStatement("insert into derby1421 values (?, ?)");
4439        for (int i=0; i<10; i++) {
4440            ps.setInt(1, i);
4441            ps.setBytes(2, (blobData + i).getBytes());
4442            ps.execute();
4443        }
4444        ps.close();
4445
4446        Statement JavaDoc scrollStmt = conn.createStatement(
4447                ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
4448        ResultSet JavaDoc rs = scrollStmt.executeQuery("SELECT * FROM derby1421");
4449
4450        rs.first();
4451        updateBlobWithUpdateBinaryStream(rs);
4452        rs.next();
4453        updateBlobWithUpdateBinaryStream(rs);
4454        rs.relative(3);
4455        updateBlobWithUpdateBinaryStream(rs);
4456        rs.absolute(7);
4457        updateBlobWithUpdateBinaryStream(rs);
4458        rs.previous();
4459        updateBlobWithUpdateBinaryStream(rs);
4460        rs.last();
4461        updateBlobWithUpdateBinaryStream(rs);
4462        rs.previous();
4463        updateBlobWithUpdateBinaryStream(rs);
4464
4465        rs.close();
4466        scrollStmt.close();
4467
4468        s.execute("DROP TABLE derby1421");
4469        s.close();
4470    }
4471
4472    private static void updateBlobWithUpdateBinaryStream(ResultSet JavaDoc rs)
4473            throws SQLException JavaDoc
4474    {
4475        Blob JavaDoc b;
4476        byte[] value, expectedValue;
4477        String JavaDoc blobData = "initial blob ";
4478        String JavaDoc updatedBlobData = "updated blob ";
4479        
4480        b = rs.getBlob(2);
4481        // check contents
4482
value = b.getBytes(1, blobData.length() + 1);
4483        expectedValue = (blobData + rs.getInt(1)).getBytes();
4484        if (!Arrays.equals(value, expectedValue)) {
4485            System.out.println("blobTest11 - Error: wrong blob value");
4486        }
4487
4488        // update contents
4489
value = (updatedBlobData + rs.getInt(1)).getBytes();
4490        InputStream JavaDoc updateValue = new ByteArrayInputStream JavaDoc(value);
4491        rs.updateBinaryStream(2, updateValue, value.length);
4492        rs.updateRow();
4493        // check update values
4494
rs.next(); // leave the row
4495
rs.previous(); // go back to updated row
4496
b = rs.getBlob(2);
4497        // check contents
4498
value = b.getBytes(1, updatedBlobData.length() + 1);
4499        expectedValue = (updatedBlobData + rs.getInt(1)).getBytes();
4500        if (!Arrays.equals(value, expectedValue))
4501        {
4502            System.out.println("blobTest11 - Error: wrong blob value");
4503        }
4504    }
4505
4506    /**
4507     * Test fix for derby-265.
4508     * Test that if getBlob is called after the transaction
4509     * in which it was created is committed, a proper user error
4510     * is thrown instead of an NPE.
4511     * Basically per the spec, getBlob is valid only for the duration of
4512     * the transaction in it was created in
4513     * @param conn
4514     * @throws SQLException
4515     * @throws FileNotFoundException
4516     * @throws IOException
4517     */

4518    private static void blobNegativeTest_Derby265(Connection JavaDoc conn)
4519            throws SQLException JavaDoc, FileNotFoundException JavaDoc,IOException JavaDoc {
4520        System.out.println(START + "blobTestNegativeTest_Derby265");
4521        // basically setup the tables for clob and blob
4522
Statement JavaDoc s = conn.createStatement();
4523        s.execute("create table \"MAPS_BLOB\"(MAP_ID int, MAP_NAME varchar(20),REGION varchar(20),AREA varchar(20), PHOTO_FORMAT varchar(20),PICTURE blob(2G))");
4524        conn.setAutoCommit(false);
4525        PreparedStatement JavaDoc ps = conn.prepareStatement("insert into \"MAPS_BLOB\" values(?,?,?,?,?,?)");
4526        
4527        for (int i = 0; i < 3; i++) {
4528            FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fileName[4]);
4529            ps.setInt(1, i);
4530            ps.setString(2, "x" + i);
4531            ps.setString(3, "abc");
4532            ps.setString(4, "abc");
4533            ps.setString(5, "abc");
4534            ps.setBinaryStream(6, new java.io.BufferedInputStream JavaDoc(fis), 300000);
4535            ps.executeUpdate();
4536            fis.close();
4537        }
4538        conn.commit();
4539
4540        conn.setAutoCommit(true);
4541        System.out.println("-----------------------------");
4542
4543        s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
4544                ResultSet.CONCUR_READ_ONLY);
4545        s.execute("SELECT \"MAP_ID\", \"MAP_NAME\", \"REGION\", \"AREA\", \"PHOTO_FORMAT\", \"PICTURE\" FROM \"MAPS_BLOB\"");
4546        ResultSet JavaDoc rs1 = s.getResultSet();
4547        Statement JavaDoc s2 = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
4548                ResultSet.CONCUR_READ_ONLY);
4549        s2.executeQuery("SELECT \"MAP_ID\", \"MAP_NAME\", \"REGION\", \"AREA\", \"PHOTO_FORMAT\", \"PICTURE\" FROM \"MAPS_BLOB\"");
4550        ResultSet JavaDoc rs2 = s2.getResultSet();
4551        rs2.next();
4552
4553        Blob JavaDoc b2 = rs2.getBlob(6);
4554        rs1.next();
4555        Blob JavaDoc b1 = rs1.getBlob(6);
4556        try {
4557            rs1.close();
4558            rs2.next();
4559            rs2.getBlob(6);
4560        } catch (SQLException JavaDoc sqle) {
4561            String JavaDoc sqlstate = sqle.getSQLState();
4562            boolean expected = (sqlstate != null &&
4563                        (sqlstate.equals("XJ073") || sqlstate.equals("XCL30")));
4564            if ( !expected ) { TestUtil.dumpSQLExceptions(sqle,expected); }
4565        }
4566        finally {
4567            rs2.close();
4568            s2.close();
4569            s.close();
4570            ps.close();
4571        }
4572
4573    }
4574
4575    /**
4576     * Test fix for derby-265.
4577     * Test that if getClob is called after the transaction
4578     * in which it was created is committed, a proper user error
4579     * is thrown instead of an NPE.
4580     * Basically per the spec, getClob is valid only for the duration of
4581     * the transaction in it was created in
4582     * @param conn
4583     * @throws SQLException
4584     * @throws FileNotFoundException
4585     * @throws IOException
4586     */

4587    private static void clobNegativeTest_Derby265(Connection JavaDoc conn)
4588            throws SQLException JavaDoc, FileNotFoundException JavaDoc,IOException JavaDoc {
4589
4590        System.out.println(START + "clobNegativeTest_Derby265");
4591        // basically setup the tables for clob
4592
Statement JavaDoc s = conn.createStatement();
4593        s.execute("create table \"MAPS\"(MAP_ID int, MAP_NAME varchar(20),REGION varchar(20),AREA varchar(20), PHOTO_FORMAT varchar(20),PICTURE clob(2G))");
4594        conn.setAutoCommit(false);
4595        PreparedStatement JavaDoc ps = conn.prepareStatement("insert into \"MAPS\" values(?,?,?,?,?,?)");
4596        for (int i = 0; i < 3; i++) {
4597            File JavaDoc file = new File JavaDoc(fileName[4]);
4598            InputStream JavaDoc fileIS = new FileInputStream JavaDoc(file);
4599            Reader JavaDoc fr = new InputStreamReader JavaDoc(fileIS, "US-ASCII");
4600            ps.setInt(1, i);
4601            ps.setString(2, "x" + i);
4602            ps.setString(3, "abc");
4603            ps.setString(4, "abc");
4604            ps.setString(5, "abc");
4605            ps.setCharacterStream(6, fr, 300000);
4606            ps.executeUpdate();
4607            fr.close();
4608        }
4609        conn.commit();
4610
4611        conn.setAutoCommit(true);
4612        System.out.println("-----------------------------");
4613        s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
4614                ResultSet.CONCUR_READ_ONLY);
4615        s.execute("SELECT \"MAP_ID\", \"MAP_NAME\", \"REGION\", \"AREA\", \"PHOTO_FORMAT\", \"PICTURE\" FROM \"MAPS\"");
4616        ResultSet JavaDoc rs1 = s.getResultSet();
4617        Statement JavaDoc s2 = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
4618                ResultSet.CONCUR_READ_ONLY);
4619        s2.executeQuery("SELECT \"MAP_ID\", \"MAP_NAME\", \"REGION\", \"AREA\", \"PHOTO_FORMAT\", \"PICTURE\" FROM \"MAPS\"");
4620        ResultSet JavaDoc rs2 = s2.getResultSet();
4621        rs2.next();
4622
4623        Clob JavaDoc b2 = rs2.getClob(6); // should be fine
4624
rs1.next();
4625        Clob JavaDoc b1 = rs1.getClob(6);
4626        try {
4627            rs1.close(); // this commits the transaction
4628
rs2.next();
4629            rs2.getClob(6); // no longer valid
4630
} catch (SQLException JavaDoc sqle) {
4631            String JavaDoc sqlstate = sqle.getSQLState();
4632            boolean expected = (sqlstate != null &&
4633                        (sqlstate.equals("XJ073") || sqlstate.equals("XCL30")));
4634            if ( !expected ) { TestUtil.dumpSQLExceptions(sqle,expected); }
4635        }
4636        finally {
4637            rs2.close();
4638            s2.close();
4639            s.close();
4640            ps.close();
4641        }
4642
4643    }
4644    static void printInterval(Clob JavaDoc clob, long pos, int length,
4645        int testNum, int iteration, int clobLength)
4646    {
4647        if (pos > clobLength + 1)
4648            System.out.println("CLOB getSubString " + pos + " > " + clobLength);
4649        try
4650        {
4651            /*
4652            System.out.println("printInterval(" + clob + "," + pos +"," +
4653                           length +"," + testNum + "," + iteration + "," +
4654                           clobLength + ")");
4655            */

4656            String JavaDoc ss = clob.getSubString(pos,length);
4657
4658            System.out.println(iteration + "(" + testNum + ") (len " + length + ") " + ss);
4659            if (ss.length() > length)
4660                System.out.println("FAIL getSubString("+pos+","+length+") returned a string of length " + ss.length());
4661
4662            long l1 = clob.length();
4663            if (l1 != clobLength) {
4664                System.out.println("CHECK - test has mismatched lengths " + l1 + " != " + clobLength);
4665            }
4666            if (pos > clobLength + 1)
4667                System.out.println("CLOB FAIL - NO ERROR ON getSubString POS TOO LARGE " + pos + " > " + clobLength);
4668
4669
4670        }
4671        catch (SQLException JavaDoc e)
4672        {
4673            String JavaDoc state = e.getSQLState();
4674            boolean expected = false;
4675
4676
4677            if (pos < 1 || pos > clobLength + 1)
4678            {
4679                if (isOutOfBoundException(e))
4680                    expected = true;
4681            }
4682            else
4683            {
4684                System.out.println("FAIL -- unexpected exception:" + e.toString());
4685            }
4686            TestUtil.dumpSQLExceptions(e,expected);
4687        }
4688        catch (StringIndexOutOfBoundsException JavaDoc obe)
4689        {
4690            // Known bug. JCC 5914.
4691
if ((pos > clobLength) && isDerbyNet)
4692            {
4693                System.out.println("EXPECTED Out of bounds exception");
4694            }
4695            else
4696            {
4697                System.out.println("FAIL -- unexpected exception:" + obe.toString());
4698                if (debug) obe.printStackTrace(System.out);
4699            }
4700        }
4701        catch (Exception JavaDoc e)
4702        {
4703            System.out.println("FAIL -- unexpected exception:" + e.toString());
4704            if (debug) e.printStackTrace(System.out);
4705        }
4706    }
4707
4708    static void printInterval(Blob JavaDoc blob, long pos, int length,
4709        int testNum, int iteration, long blobLength)
4710    {
4711        if (pos > blobLength + 1)
4712            System.out.println("testing Blob.getBytes() with pos " + pos + " > " + blobLength);
4713        try
4714        {
4715            // generate a new string out of the blob for comparison,
4716
// ensure it's using fixed format.
4717
System.out.println(iteration + "(" + testNum + ") " +
4718                new String JavaDoc(blob.getBytes(pos,length), "US-ASCII"));
4719
4720            long l1 = blob.length();
4721            if (l1 != blobLength) {
4722                System.out.println("CHECK - test has mismatched lengths " + l1 + " != " + blobLength);
4723            }
4724            if (pos > blobLength + 1)
4725                System.out.println("FAIL testing Blob.getBytes() with pos " + pos + " > " + blobLength);
4726        }
4727        catch (SQLException JavaDoc e)
4728        {
4729            String JavaDoc state = e.getSQLState();
4730            boolean expected = false;
4731
4732            if (pos < 1 || pos > blobLength + 1)
4733                expected = isOutOfBoundException(e);
4734
4735            TestUtil.dumpSQLExceptions(e, expected);
4736        }
4737        catch (Exception JavaDoc e)
4738        {
4739            System.out.println("FAIL: Caught exception " +
4740                                e.toString());
4741        }
4742    }
4743    static void printPosition(
4744        int rowNum,
4745        String JavaDoc searchStr,
4746        long position,
4747        Clob JavaDoc clob,
4748        long clobLength)
4749    {
4750
4751        try
4752        {
4753
4754            long result = clob.position(searchStr,position);
4755
4756            System.out.println("Found " + searchStr + " in row " + rowNum +
4757                    " starting from position " + position + " at position " +
4758                    (result == -1 ? " NOTFOUND " : Long.toString(result)));
4759
4760            long l1 = clob.length();
4761            if (l1 != clobLength) {
4762                System.out.println("CHECK - test has mismatched lengths " + l1 + " != " + clobLength);
4763            }
4764
4765
4766        }
4767        catch (SQLException JavaDoc e)
4768        {
4769            String JavaDoc state = e.getSQLState();
4770            boolean expected = false;
4771
4772            if (position < 1 || position > clobLength)
4773                expected = isOutOfBoundException(e);
4774
4775            if (searchStr == null)
4776                if ("XJ072".equals(state))
4777                    expected = true;
4778
4779            if ("".equals(searchStr))
4780                if ("XJ078".equals(state))
4781                    expected = true;
4782    
4783                
4784            TestUtil.dumpSQLExceptions(e, expected);
4785            e.printStackTrace(System.out);
4786        }
4787    }
4788
4789    static void printPosition(
4790        int rowNum,
4791        String JavaDoc searchStr,
4792        long position,
4793        Blob JavaDoc blob, int blobLength) throws UnsupportedEncodingException JavaDoc
4794    {
4795        try
4796        {
4797            long result = blob.position(searchStr.getBytes("US-ASCII"),position);
4798            if ((searchStr == "") && (result == 1))
4799                return;
4800            if (result != -1)
4801                System.out.println("Found " + searchStr + " in row " + rowNum +
4802                    " starting from position " + position + " at position " +
4803                    result);
4804
4805            long l1 = blob.length();
4806            if (l1 != blobLength) {
4807                System.out.println("CHECK - test has mismatched lengths " + l1 + " != " + blobLength);
4808            }
4809        }
4810        catch (SQLException JavaDoc e)
4811        {
4812            String JavaDoc state = e.getSQLState();
4813            boolean expected = false;
4814
4815            if (position < 1 || position > blobLength)
4816                expected = isOutOfBoundException(e);
4817
4818            if (searchStr == null)
4819                if ("XJ072".equals(state))
4820                    expected = true;
4821
4822            if ("".equals(searchStr))
4823                if ("XJ078".equals(state))
4824                    expected = true;
4825    
4826                
4827            TestUtil.dumpSQLExceptions(e, expected);
4828        }
4829    }
4830    /**
4831     * In network server we expect an exception.
4832     * In embedded we don't
4833     */

4834     
4835    static private void expectedExceptionForNSOnly (SQLException JavaDoc se)
4836    {
4837        TestUtil.dumpSQLExceptions(se, isDerbyNet);
4838    }
4839
4840    static private boolean isOutOfBoundException(SQLException JavaDoc se)
4841    {
4842        String JavaDoc sqlState = se.getSQLState();
4843        String JavaDoc msg = se.getMessage();
4844        if ("XJ070".equals(sqlState) ||
4845            "XJ071".equals(sqlState) ||
4846            "XJ076".equals(sqlState) ||
4847            (sqlState == null &&
4848             ((msg.indexOf("Index Out Of Bound") != -1) ||
4849              (msg.indexOf("Invalid position") != -1))))
4850            return true;
4851            
4852        return false;
4853    }
4854
4855    static private boolean isNullSearchPattern(SQLException JavaDoc se)
4856    {
4857        String JavaDoc sqlState = se.getSQLState();
4858        if ("XJ072".equals(sqlState) ||
4859            (sqlState == null &&
4860             se.getMessage().indexOf("Search pattern cannot be null") != -1))
4861            return true;
4862            
4863        return false;
4864    }
4865    
4866    /**
4867     * Run some simple checks comparing the Blob
4868     * to the file's length and checksum.
4869     * @throws SQLException
4870     * @throws IOException
4871     *
4872     */

4873    private static void checkBlobAgainstFile(int fileid,
4874            Blob JavaDoc blob) throws SQLException JavaDoc, IOException JavaDoc {
4875
4876        if (blob.length() != fileLength[fileid])
4877            System.out.println("FAIL BLOB length mismatch: "
4878                    + "BLOB " + blob.length()
4879                    + " FILE " + fileLength[fileid]);
4880        
4881        long blobsum = getStreamCheckSum(blob.getBinaryStream());
4882        if (blobsum != fileCRC32[fileid])
4883            System.out.println("FAIL BLOB checksum mismatch: "
4884                    + "BLOB " + blobsum
4885                    + " FILE " + fileCRC32[fileid]);
4886    }
4887  
4888    /**
4889     * Get the CRC32 checksum of a stream, reading
4890     * its contents entirely and closing it.
4891     */

4892    private static long getStreamCheckSum(InputStream JavaDoc in)
4893       throws IOException JavaDoc
4894    {
4895        CRC32 JavaDoc sum = new CRC32 JavaDoc();
4896        
4897        byte[] buf = new byte[32*1024];
4898        
4899        for (;;) {
4900            int read = in.read(buf);
4901            if (read == -1)
4902                break;
4903            sum.update(buf, 0, read);
4904        }
4905        in.close();
4906        return sum.getValue();
4907    }
4908    
4909    private static void showClobContents(String JavaDoc tag, ResultSet JavaDoc rs)
4910        throws SQLException JavaDoc, IOException JavaDoc
4911    {
4912        while (rs.next())
4913        {
4914            InputStream JavaDoc is = rs.getAsciiStream(1);
4915            if (is == null) {
4916                System.out.println(tag + ": NULL");
4917                continue;
4918            }
4919            long clobcrc = getStreamCheckSum(is);
4920            int clobLength = rs.getInt(2);
4921            int b = rs.getInt(3);
4922            
4923            System.out.println(tag + ": length " +
4924                    clobLength + " crc32 " + clobcrc + " " + b);
4925        }
4926    }
4927}
4928
4929
4930
Popular Tags