Source Code Cross Referenced for lobStreams.java in  » Database-DBMS » db-derby-10.2 » org » apache » derbyTesting » functionTests » tests » jdbcapi » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » db derby 10.2 » org.apache.derbyTesting.functionTests.tests.jdbcapi 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:           Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.lobStreams
004:
005:           Licensed to the Apache Software Foundation (ASF) under one or more
006:           contributor license agreements.  See the NOTICE file distributed with
007:           this work for additional information regarding copyright ownership.
008:           The ASF licenses this file to You under the Apache License, Version 2.0
009:           (the "License"); you may not use this file except in compliance with
010:           the License.  You may obtain a copy of the License at
011:
012:              http://www.apache.org/licenses/LICENSE-2.0
013:
014:           Unless required by applicable law or agreed to in writing, software
015:           distributed under the License is distributed on an "AS IS" BASIS,
016:           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017:           See the License for the specific language governing permissions and
018:           limitations under the License.
019:
020:         */
021:
022:        package org.apache.derbyTesting.functionTests.tests.jdbcapi;
023:
024:        import java.io.File;
025:        import java.io.FileInputStream;
026:        import java.io.FileNotFoundException;
027:        import java.io.FileReader;
028:        import java.io.InputStream;
029:        import java.io.OutputStream;
030:        import java.io.Reader;
031:        import java.io.Writer;
032:        import java.sql.Blob;
033:        import java.sql.Clob;
034:        import java.sql.Connection;
035:        import java.sql.PreparedStatement;
036:        import java.sql.ResultSet;
037:        import java.sql.SQLException;
038:        import java.sql.Statement;
039:
040:        import org.apache.derby.tools.ij;
041:        import org.apache.derbyTesting.functionTests.util.TestUtil;
042:
043:        public class lobStreams {
044:
045:            static String[] fileName = new String[2];
046:            static String sep;
047:            static long fileLength;
048:
049:            static boolean debug = true;
050:            private static final String START = "\nSTART: ";
051:
052:            private static final String unicodeTestString = "This is a test string containing a few "
053:                    + "non-ascii characters:\nÆØÅ and æøå are used in norwegian: 'Blåbærsyltetøy' means"
054:                    + "'blueberry jam', and tastes great on pancakes. =)";
055:
056:            static {
057:                //            fileName[0] = "extin" + sep + "aclob.utf";
058:                //           fileName[1] = "extin" + sep + "littleclob.utf";
059:                fileName[0] = "aclob.utf";
060:                fileName[1] = "littleclob.utf";
061:            }
062:
063:            public static void main(String[] args) {
064:                System.out
065:                        .println("Test lob stream with multiple writes starting");
066:
067:                // check to see if we have the correct extin path, if the files aren't right here, try one more time
068:                boolean exists = (new File("extin", fileName[0])).exists();
069:                String sep = System.getProperty("file.separator");
070:                if (!exists) {
071:                    // assume it's in a dir up, if that's wrong too, too bad...
072:                    String userdir = System.getProperty("user.dir");
073:                    fileName[0] = userdir + sep + ".." + sep + "extin" + sep
074:                            + fileName[0];
075:                    fileName[1] = userdir + sep + ".." + sep + "extin" + sep
076:                            + fileName[1];
077:                } else {
078:                    // assume it's in a dir up, if that's wrong too, too bad...
079:                    fileName[0] = "extin" + sep + fileName[0];
080:                    fileName[1] = "extin" + sep + fileName[1];
081:                }
082:
083:                try {
084:                    // use the ij utility to read the property file and
085:                    // make the initial connection.
086:                    ij.getPropertyArg(args);
087:                    Connection conn = ij.startJBMS();
088:                    // turn off autocommit, otherwise blobs/clobs cannot hang around
089:                    // until end of transaction
090:                    conn.setAutoCommit(false);
091:
092:                    prepareTable(conn);
093:                    testBlobWrite3Param(conn);
094:                    resetBlobClob(conn);
095:                    testBlobWrite1Param(conn);
096:                    resetBlobClob(conn);
097:                    testClobAsciiWrite3Param(conn);
098:                    resetBlobClob(conn);
099:                    testClobAsciiWrite1Param(conn);
100:                    resetBlobClob(conn);
101:                    testClobCharacterWrite3ParamChar(conn);
102:                    resetBlobClob(conn);
103:                    testClobCharacterWrite3ParamString(conn);
104:                    resetBlobClob(conn);
105:                    testClobCharacterWrite1ParamString(conn);
106:                    resetBlobClob(conn);
107:                    testClobCharacterWrite1Char(conn);
108:
109:                    // restart the connection
110:                    conn.commit();
111:                    cleanUp(conn);
112:                    conn.commit();
113:                    conn.close();
114:
115:                } catch (Throwable e) {
116:                    System.out.println("FAIL -- unexpected exception:"
117:                            + e.toString());
118:                    if (debug)
119:                        e.printStackTrace();
120:                }
121:                System.out
122:                        .println("Test lob stream with multiple writes finished\n");
123:            }
124:
125:            private static void prepareTable(Connection conn) {
126:                try {
127:                    Statement stmt1 = conn.createStatement();
128:                    stmt1
129:                            .execute("create table testBlobX1 (a integer, b blob(300K), c clob(300K))");
130:                    stmt1.close();
131:
132:                    byte[] b2 = new byte[1];
133:                    b2[0] = (byte) 64;
134:                    String c2 = "c";
135:                    PreparedStatement stmt2 = conn
136:                            .prepareStatement("INSERT INTO testBlobX1(a, b, c) VALUES (?, ?, ?)");
137:                    stmt2.setInt(1, 1);
138:                    stmt2.setBytes(2, b2);
139:                    stmt2.setString(3, c2);
140:                    stmt2.execute();
141:                    stmt2.close();
142:
143:                } catch (SQLException e) {
144:                    TestUtil.dumpSQLExceptions(e);
145:                } catch (Throwable e) {
146:                    if (debug)
147:                        e.printStackTrace();
148:                }
149:
150:            }
151:
152:            private static void resetBlobClob(Connection conn) {
153:                try {
154:                    byte[] b2 = new byte[1];
155:                    b2[0] = (byte) 64;
156:                    String c2 = "a";
157:                    PreparedStatement stmt = conn
158:                            .prepareStatement("UPDATE testBlobX1 SET b = ?, c = ? WHERE a = 1");
159:                    stmt.setBytes(1, b2);
160:                    stmt.setString(2, c2);
161:                    stmt.execute();
162:                    stmt.close();
163:
164:                } catch (SQLException e) {
165:                    TestUtil.dumpSQLExceptions(e);
166:                } catch (Throwable e) {
167:                    if (debug)
168:                        e.printStackTrace();
169:                }
170:
171:            }
172:
173:            /**
174:             * Tests the BlobOutputStream.write(byte  b[], int off, int len) method
175:             **/
176:            private static void testBlobWrite3Param(Connection conn) {
177:                try {
178:                    System.out.println(START + "testBlobWrite3Param");
179:
180:                    PreparedStatement stmt3 = conn
181:                            .prepareStatement("SELECT b FROM testBlobX1 WHERE a = 1");
182:
183:                    ResultSet rs3 = stmt3.executeQuery();
184:
185:                    rs3.next();
186:
187:                    Blob blob = rs3.getBlob(1);
188:
189:                    File file = new File(fileName[0]);
190:                    fileLength = file.length();
191:                    InputStream fileIn = new FileInputStream(file);
192:
193:                    if (blob != null) {
194:                        int count = 0;
195:                        byte[] buffer = new byte[1024];
196:                        OutputStream outstream = blob.setBinaryStream(1L);
197:                        while ((count = fileIn.read(buffer)) != -1) {
198:                            outstream.write(buffer, 0, count);
199:                        }
200:                        outstream.close();
201:                        fileIn.close();
202:
203:                        PreparedStatement stmt4 = conn
204:                                .prepareStatement("UPDATE testBlobX1 SET b = ? WHERE a = 1");
205:                        stmt4.setBlob(1, blob);
206:                        stmt4.executeUpdate();
207:                        stmt4.close();
208:
209:                    } else {
210:                        System.out.println("FAIL -- blob is NULL");
211:                    }
212:
213:                    rs3.close();
214:                    rs3 = stmt3.executeQuery();
215:
216:                    if (rs3.next()) {
217:                        long new_length = rs3.getBlob(1).length();
218:                        if (new_length != fileLength) {
219:                            System.out
220:                                    .println("FAIL -- wrong blob length; original: "
221:                                            + fileLength
222:                                            + " blob length: "
223:                                            + new_length);
224:                        } else {
225:                            // Check contents ...
226:                            InputStream fStream = new FileInputStream(file);
227:                            InputStream lStream = rs3.getBlob(1)
228:                                    .getBinaryStream();
229:
230:                            if (!compareLob2File(fStream, lStream))
231:                                System.out
232:                                        .println("FAIL - Blob and file contents do not match");
233:
234:                            fStream.close();
235:                            lStream.close();
236:
237:                        }
238:                    } else {
239:                        System.out.println("FAIL -- blob not found");
240:                    }
241:                    rs3.close();
242:                    stmt3.close();
243:
244:                    System.out.println("testBlobWrite3Param finished");
245:                } catch (SQLException e) {
246:                    TestUtil.dumpSQLExceptions(e);
247:                } catch (Throwable e) {
248:                    if (debug)
249:                        e.printStackTrace();
250:                }
251:            }
252:
253:            /**
254:             * Tests the BlobOutputStream.write(int b) method
255:             **/
256:            private static void testBlobWrite1Param(Connection conn) {
257:                try {
258:                    System.out.println(START + "testBlobWrite1Param");
259:
260:                    PreparedStatement stmt3 = conn
261:                            .prepareStatement("SELECT b FROM testBlobX1 WHERE a = 1");
262:
263:                    ResultSet rs3 = stmt3.executeQuery();
264:
265:                    rs3.next();
266:
267:                    Blob blob = rs3.getBlob(1);
268:
269:                    File file = new File(fileName[1]);
270:                    fileLength = file.length();
271:                    InputStream fileIn = new FileInputStream(file);
272:
273:                    if (blob != null) {
274:                        int buffer;
275:                        OutputStream outstream = blob.setBinaryStream(1L);
276:                        while ((buffer = fileIn.read()) != -1) {
277:                            outstream.write(buffer);
278:                        }
279:                        outstream.close();
280:                        fileIn.close();
281:
282:                        PreparedStatement stmt4 = conn
283:                                .prepareStatement("UPDATE testBlobX1 SET b = ? WHERE a = 1");
284:                        stmt4.setBlob(1, blob);
285:                        stmt4.executeUpdate();
286:                        stmt4.close();
287:
288:                    } else {
289:                        System.out.println("FAIL -- blob is NULL");
290:                    }
291:
292:                    rs3.close();
293:                    rs3 = stmt3.executeQuery();
294:
295:                    if (rs3.next()) {
296:                        long new_length = rs3.getBlob(1).length();
297:                        if (new_length != fileLength) {
298:                            System.out
299:                                    .println("FAIL -- wrong blob length; original: "
300:                                            + fileLength
301:                                            + " blob length: "
302:                                            + new_length);
303:                        } else {
304:                            // Check contents ...
305:                            InputStream fStream = new FileInputStream(file);
306:                            InputStream lStream = rs3.getBlob(1)
307:                                    .getBinaryStream();
308:
309:                            if (!compareLob2File(fStream, lStream))
310:                                System.out
311:                                        .println("FAIL - Blob and file contents do not match");
312:
313:                            fStream.close();
314:                            lStream.close();
315:
316:                        }
317:                    } else {
318:                        System.out.println("FAIL -- blob not found");
319:                    }
320:                    rs3.close();
321:                    stmt3.close();
322:
323:                    System.out.println("testBlobWrite1Param finished");
324:                } catch (SQLException e) {
325:                    TestUtil.dumpSQLExceptions(e);
326:                } catch (Throwable e) {
327:                    if (debug)
328:                        e.printStackTrace();
329:                }
330:            }
331:
332:            /**
333:             * Tests the ClobOutputStream.write(byte  b[], int off, int len) method
334:             **/
335:            private static void testClobAsciiWrite3Param(Connection conn) {
336:                try {
337:                    System.out.println(START + "testClobAsciiWrite3Param");
338:
339:                    PreparedStatement stmt3 = conn
340:                            .prepareStatement("SELECT c FROM testBlobX1 WHERE a = 1");
341:
342:                    ResultSet rs3 = stmt3.executeQuery();
343:
344:                    rs3.next();
345:
346:                    Clob clob = rs3.getClob(1);
347:
348:                    File file = new File(fileName[0]);
349:                    fileLength = file.length();
350:                    InputStream fileIn = new FileInputStream(file);
351:
352:                    if (clob != null) {
353:                        int count = 0;
354:                        byte[] buffer = new byte[1024];
355:                        OutputStream outstream = clob.setAsciiStream(1L);
356:                        while ((count = fileIn.read(buffer)) != -1) {
357:                            outstream.write(buffer, 0, count);
358:                        }
359:                        outstream.close();
360:                        fileIn.close();
361:
362:                        PreparedStatement stmt4 = conn
363:                                .prepareStatement("UPDATE testBlobX1 SET c = ? WHERE a = 1");
364:                        stmt4.setClob(1, clob);
365:                        stmt4.executeUpdate();
366:                        stmt4.close();
367:                    } else {
368:                        System.out.println("FAIL -- clob is NULL");
369:                    }
370:
371:                    rs3.close();
372:                    rs3 = stmt3.executeQuery();
373:
374:                    if (rs3.next()) {
375:                        long new_length = rs3.getClob(1).length();
376:                        if (new_length != fileLength) {
377:                            System.out
378:                                    .println("FAIL -- wrong clob length; original: "
379:                                            + fileLength
380:                                            + " clob length: "
381:                                            + new_length);
382:                        } else {
383:                            // Check contents ...
384:                            InputStream fStream = new FileInputStream(file);
385:                            InputStream lStream = rs3.getClob(1)
386:                                    .getAsciiStream();
387:
388:                            if (!compareLob2File(fStream, lStream))
389:                                System.out
390:                                        .println("FAIL - Clob and file contents do not match");
391:
392:                            fStream.close();
393:                            lStream.close();
394:
395:                        }
396:                    } else {
397:                        System.out.println("FAIL -- clob not found");
398:                    }
399:                    rs3.close();
400:                    stmt3.close();
401:
402:                    System.out.println("testClobAsciiWrite3Param finished");
403:                } catch (SQLException e) {
404:                    TestUtil.dumpSQLExceptions(e);
405:                } catch (Throwable e) {
406:                    if (debug)
407:                        e.printStackTrace();
408:                }
409:            }
410:
411:            /**
412:             * Tests the ClobOutputStream.write(int b) method
413:             **/
414:            private static void testClobAsciiWrite1Param(Connection conn) {
415:                try {
416:                    System.out.println(START + "testClobAsciiWrite1Param");
417:
418:                    PreparedStatement stmt3 = conn
419:                            .prepareStatement("SELECT c FROM testBlobX1 WHERE a = 1");
420:
421:                    ResultSet rs3 = stmt3.executeQuery();
422:
423:                    rs3.next();
424:
425:                    Clob clob = rs3.getClob(1);
426:
427:                    File file = new File(fileName[1]);
428:                    fileLength = file.length();
429:                    InputStream fileIn = new FileInputStream(file);
430:
431:                    if (clob != null) {
432:                        int buffer;
433:                        OutputStream outstream = clob.setAsciiStream(1L);
434:                        while ((buffer = fileIn.read()) != -1) {
435:                            outstream.write(buffer);
436:                        }
437:                        outstream.close();
438:                        fileIn.close();
439:
440:                        PreparedStatement stmt4 = conn
441:                                .prepareStatement("UPDATE testBlobX1 SET c = ? WHERE a = 1");
442:                        stmt4.setClob(1, clob);
443:                        stmt4.executeUpdate();
444:                        stmt4.close();
445:
446:                    } else {
447:                        System.out.println("FAIL -- clob is NULL");
448:                    }
449:
450:                    rs3.close();
451:                    rs3 = stmt3.executeQuery();
452:
453:                    if (rs3.next()) {
454:                        long new_length = rs3.getClob(1).length();
455:                        if (new_length != fileLength) {
456:                            System.out
457:                                    .println("FAIL -- wrong clob length; original: "
458:                                            + fileLength
459:                                            + " clob length: "
460:                                            + new_length);
461:                        } else {
462:                            // Check contents ...
463:                            InputStream fStream = new FileInputStream(file);
464:                            InputStream lStream = rs3.getClob(1)
465:                                    .getAsciiStream();
466:
467:                            if (!compareLob2File(fStream, lStream))
468:                                System.out
469:                                        .println("FAIL - Clob and file contents do not match");
470:
471:                            fStream.close();
472:                            lStream.close();
473:
474:                        }
475:                    } else {
476:                        System.out.println("FAIL -- clob not found");
477:                    }
478:                    rs3.close();
479:                    stmt3.close();
480:
481:                    System.out.println("testClobAsciiWrite1Param finished");
482:                } catch (SQLException e) {
483:                    TestUtil.dumpSQLExceptions(e);
484:                } catch (Throwable e) {
485:                    if (debug)
486:                        e.printStackTrace();
487:                }
488:            }
489:
490:            /**
491:             * Tests the ClobWriter.write(char cbuf[], int off, int len) method
492:             **/
493:            private static void testClobCharacterWrite3ParamChar(Connection conn) {
494:                try {
495:                    System.out.println(START
496:                            + "testClobCharacterWrite3ParamChar");
497:
498:                    PreparedStatement stmt3 = conn
499:                            .prepareStatement("SELECT c FROM testBlobX1 WHERE a = 1");
500:
501:                    ResultSet rs3 = stmt3.executeQuery();
502:
503:                    rs3.next();
504:
505:                    Clob clob = rs3.getClob(1);
506:                    char[] testdata = unicodeTestString.toCharArray();
507:
508:                    if (clob != null) {
509:                        Writer clobWriter = clob.setCharacterStream(1L);
510:                        clobWriter.write(testdata, 0, testdata.length);
511:                        clobWriter.close();
512:
513:                        PreparedStatement stmt4 = conn
514:                                .prepareStatement("UPDATE testBlobX1 SET c = ? WHERE a = 1");
515:                        stmt4.setClob(1, clob);
516:                        stmt4.executeUpdate();
517:                        stmt4.close();
518:                    } else {
519:                        System.out.println("FAIL -- clob is NULL");
520:                    }
521:
522:                    rs3.close();
523:                    rs3 = stmt3.executeQuery();
524:
525:                    if (rs3.next()) {
526:                        long new_length = rs3.getClob(1).length();
527:                        if (new_length != testdata.length) {
528:                            System.out
529:                                    .println("FAIL -- wrong clob length; original: "
530:                                            + testdata.length
531:                                            + " clob length: " + new_length);
532:                        } else {
533:                            // Check contents ...
534:                            Reader lStream = rs3.getClob(1)
535:                                    .getCharacterStream();
536:
537:                            if (!compareClobReader2CharArray(testdata, lStream))
538:                                System.out
539:                                        .println("FAIL - Clob and buffer contents do not match");
540:
541:                            lStream.close();
542:
543:                        }
544:                    } else {
545:                        System.out.println("FAIL -- clob not found");
546:                    }
547:                    rs3.close();
548:                    stmt3.close();
549:
550:                    System.out
551:                            .println("testClobCharacterWrite3ParamChar finished");
552:                } catch (SQLException e) {
553:                    TestUtil.dumpSQLExceptions(e);
554:                } catch (Throwable e) {
555:                    if (debug)
556:                        e.printStackTrace();
557:                }
558:            }
559:
560:            /**
561:             * Tests the ClobWriter.write(String str, int off, int len) method
562:             **/
563:            private static void testClobCharacterWrite3ParamString(
564:                    Connection conn) {
565:                try {
566:                    System.out.println(START
567:                            + "testClobCharacterWrite3ParamString");
568:
569:                    PreparedStatement stmt3 = conn
570:                            .prepareStatement("SELECT c FROM testBlobX1 WHERE a = 1");
571:
572:                    ResultSet rs3 = stmt3.executeQuery();
573:
574:                    rs3.next();
575:
576:                    Clob clob = rs3.getClob(1);
577:
578:                    if (clob != null) {
579:                        Writer clobWriter = clob.setCharacterStream(1L);
580:                        clobWriter.write(unicodeTestString, 0,
581:                                unicodeTestString.length());
582:                        clobWriter.close();
583:
584:                        PreparedStatement stmt4 = conn
585:                                .prepareStatement("UPDATE testBlobX1 SET c = ? WHERE a = 1");
586:                        stmt4.setClob(1, clob);
587:                        stmt4.executeUpdate();
588:                        stmt4.close();
589:                    } else {
590:                        System.out.println("FAIL -- clob is NULL");
591:                    }
592:
593:                    rs3.close();
594:                    rs3 = stmt3.executeQuery();
595:
596:                    if (rs3.next()) {
597:                        long new_length = rs3.getClob(1).length();
598:                        if (new_length != unicodeTestString.length()) {
599:                            System.out
600:                                    .println("FAIL -- wrong clob length; original: "
601:                                            + unicodeTestString.length()
602:                                            + " clob length: " + new_length);
603:                        } else {
604:                            // Check contents ...
605:                            Reader lStream = rs3.getClob(1)
606:                                    .getCharacterStream();
607:
608:                            if (!compareClobReader2CharArray(unicodeTestString
609:                                    .toCharArray(), lStream))
610:                                System.out
611:                                        .println("FAIL - Clob and buffer contents do not match");
612:
613:                            lStream.close();
614:
615:                        }
616:                    } else {
617:                        System.out.println("FAIL -- clob not found");
618:                    }
619:                    rs3.close();
620:                    stmt3.close();
621:
622:                    System.out
623:                            .println("testClobCharacterWrite3ParamString finished");
624:                } catch (SQLException e) {
625:                    TestUtil.dumpSQLExceptions(e);
626:                } catch (Throwable e) {
627:                    if (debug)
628:                        e.printStackTrace();
629:                }
630:            }
631:
632:            /**
633:             * Tests the ClobWriter.write(String str) method
634:             **/
635:            private static void testClobCharacterWrite1ParamString(
636:                    Connection conn) {
637:                try {
638:                    System.out.println(START
639:                            + "testClobCharacterWrite1ParamString");
640:
641:                    PreparedStatement stmt3 = conn
642:                            .prepareStatement("SELECT c FROM testBlobX1 WHERE a = 1");
643:
644:                    ResultSet rs3 = stmt3.executeQuery();
645:
646:                    rs3.next();
647:
648:                    Clob clob = rs3.getClob(1);
649:
650:                    if (clob != null) {
651:                        Writer clobWriter = clob.setCharacterStream(1L);
652:                        clobWriter.write(unicodeTestString);
653:                        clobWriter.close();
654:
655:                        PreparedStatement stmt4 = conn
656:                                .prepareStatement("UPDATE testBlobX1 SET c = ? WHERE a = 1");
657:                        stmt4.setClob(1, clob);
658:                        stmt4.executeUpdate();
659:                        stmt4.close();
660:                    } else {
661:                        System.out.println("FAIL -- clob is NULL");
662:                    }
663:
664:                    rs3.close();
665:                    rs3 = stmt3.executeQuery();
666:
667:                    if (rs3.next()) {
668:                        long new_length = rs3.getClob(1).length();
669:                        if (new_length != unicodeTestString.length()) {
670:                            System.out
671:                                    .println("FAIL -- wrong clob length; original: "
672:                                            + unicodeTestString.length()
673:                                            + " clob length: " + new_length);
674:                        } else {
675:                            // Check contents ...
676:                            Reader lStream = rs3.getClob(1)
677:                                    .getCharacterStream();
678:
679:                            if (!compareClobReader2CharArray(unicodeTestString
680:                                    .toCharArray(), lStream))
681:                                System.out
682:                                        .println("FAIL - Clob and buffer contents do not match");
683:
684:                            lStream.close();
685:
686:                        }
687:                    } else {
688:                        System.out.println("FAIL -- clob not found");
689:                    }
690:                    rs3.close();
691:                    stmt3.close();
692:
693:                    System.out
694:                            .println("testClobCharacterWrite1ParamString finished");
695:                } catch (SQLException e) {
696:                    TestUtil.dumpSQLExceptions(e);
697:                } catch (Throwable e) {
698:                    if (debug)
699:                        e.printStackTrace();
700:                }
701:            }
702:
703:            /**
704:             * Tests the ClobWriter.write(int c) method
705:             **/
706:            private static void testClobCharacterWrite1Char(Connection conn) {
707:                try {
708:                    System.out.println(START + "testClobCharacterWrite1Char");
709:
710:                    PreparedStatement stmt3 = conn
711:                            .prepareStatement("SELECT c FROM testBlobX1 WHERE a = 1");
712:
713:                    ResultSet rs3 = stmt3.executeQuery();
714:
715:                    rs3.next();
716:
717:                    Clob clob = rs3.getClob(1);
718:
719:                    char testchar = 'a';
720:
721:                    if (clob != null) {
722:                        Writer clobWriter = clob.setCharacterStream(1L);
723:                        clobWriter.write(testchar);
724:                        clobWriter.close();
725:
726:                        PreparedStatement stmt4 = conn
727:                                .prepareStatement("UPDATE testBlobX1 SET c = ? WHERE a = 1");
728:                        stmt4.setClob(1, clob);
729:                        stmt4.executeUpdate();
730:                        stmt4.close();
731:                    } else {
732:                        System.out.println("FAIL -- clob is NULL");
733:                    }
734:
735:                    rs3.close();
736:                    rs3 = stmt3.executeQuery();
737:
738:                    if (rs3.next()) {
739:                        long new_length = rs3.getClob(1).length();
740:                        Clob fish = rs3.getClob(1);
741:                        if (new_length != 1) {
742:                            System.out
743:                                    .println("FAIL -- wrong clob length; original: "
744:                                            + 1 + " clob length: " + new_length);
745:                        } else {
746:                            // Check contents ...
747:                            Reader lStream = rs3.getClob(1)
748:                                    .getCharacterStream();
749:                            char clobchar = (char) lStream.read();
750:
751:                            if (clobchar != testchar)
752:                                System.out
753:                                        .println("FAIL - fetched Clob and original contents do not match");
754:
755:                            lStream.close();
756:
757:                        }
758:                    } else {
759:                        System.out.println("FAIL -- clob not found");
760:                    }
761:                    rs3.close();
762:                    stmt3.close();
763:
764:                    System.out.println("testClobCharacterWrite1Char finished");
765:                } catch (SQLException e) {
766:                    TestUtil.dumpSQLExceptions(e);
767:                } catch (Throwable e) {
768:                    if (debug)
769:                        e.printStackTrace();
770:                }
771:            }
772:
773:            private static boolean compareLob2File(InputStream fStream,
774:                    InputStream lStream) {
775:                byte[] fByte = new byte[1024];
776:                byte[] lByte = new byte[1024];
777:                int lLength = 0, fLength = 0;
778:                String fString, lString;
779:
780:                try {
781:                    do {
782:                        fLength = fStream.read(fByte, 0, 1024);
783:                        lLength = lStream.read(lByte, 0, 1024);
784:                        if (!java.util.Arrays.equals(fByte, lByte))
785:                            return false;
786:                    } while (fLength > 0 && lLength > 0);
787:
788:                    fStream.close();
789:                    lStream.close();
790:                } catch (Throwable e) {
791:                    if (debug)
792:                        e.printStackTrace();
793:                }
794:                return true;
795:            }
796:
797:            private static boolean compareClobReader2CharArray(char[] cArray,
798:                    Reader charReader) {
799:                char[] clobChars = new char[cArray.length];
800:
801:                int readChars = 0;
802:                int totalCharsRead = 0;
803:
804:                try {
805:                    do {
806:                        readChars = charReader.read(clobChars, totalCharsRead,
807:                                cArray.length - totalCharsRead);
808:                        if (readChars != -1)
809:                            totalCharsRead += readChars;
810:                    } while (readChars != -1 && totalCharsRead < cArray.length);
811:                    charReader.close();
812:                    if (!java.util.Arrays.equals(cArray, clobChars))
813:                        return false;
814:
815:                } catch (Throwable e) {
816:                    if (debug)
817:                        e.printStackTrace();
818:                }
819:                return true;
820:            }
821:
822:            private static void cleanUp(Connection conn) throws SQLException {
823:                String[] testObjects = { "table testBlobX1" };
824:                Statement cleanupStmt = conn.createStatement();
825:                TestUtil.cleanUpTest(cleanupStmt, testObjects);
826:            }
827:
828:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.