Source Code Cross Referenced for Stream.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.Stream
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.InputStream;
025:        import java.io.Reader;
026:        import java.sql.Connection;
027:        import java.sql.Statement;
028:        import java.sql.PreparedStatement;
029:        import java.sql.ResultSet;
030:
031:        import java.io.IOException;
032:        import java.sql.SQLException;
033:
034:        import org.apache.derby.tools.ij;
035:
036:        public class Stream {
037:
038:            public static void main(String[] args) {
039:
040:                Connection conn = null;
041:
042:                try {
043:                    ij.getPropertyArg(args);
044:                    conn = ij.startJBMS();
045:
046:                    createTestTables(conn);
047:                    executeTests(conn);
048:                    dropTestTables(conn);
049:
050:                } catch (Throwable t) {
051:                    t.printStackTrace();
052:
053:                } finally {
054:                    if (conn != null) {
055:                        try {
056:                            conn.close();
057:
058:                        } catch (SQLException e) {
059:                            e.printStackTrace();
060:                        }
061:
062:                    }
063:                }
064:
065:            }
066:
067:            private static void createTestTables(Connection conn)
068:                    throws SQLException, IOException {
069:
070:                createTable(conn);
071:                createTestData(conn);
072:
073:            }
074:
075:            private static void createTable(Connection conn)
076:                    throws SQLException {
077:
078:                Statement st = null;
079:
080:                try {
081:
082:                    st = conn.createStatement();
083:                    st
084:                            .execute("create table SMALL_BLOB_TABLE( SMALL_BLOB blob( 512 ))");
085:                    st
086:                            .execute("create table LARGE_BLOB_TABLE( LARGE_BLOB blob( 512k ))");
087:                    st
088:                            .execute("create table SMALL_CLOB_TABLE( SMALL_CLOB clob( 512 ))");
089:                    st
090:                            .execute("create table LARGE_CLOB_TABLE( LARGE_CLOB clob( 512k ))");
091:
092:                } finally {
093:                    if (st != null)
094:                        st.close();
095:                }
096:
097:            }
098:
099:            private static void createTestData(Connection conn)
100:                    throws SQLException, IOException {
101:
102:                createSmallBlobTestData(conn);
103:                createLargeBlobTestData(conn);
104:                createSmallClobTestData(conn);
105:                createLargeClobTestData(conn);
106:
107:            }
108:
109:            private static void createSmallBlobTestData(Connection conn)
110:                    throws SQLException, IOException {
111:
112:                PreparedStatement st = null;
113:                TestDataStream stream = null;
114:
115:                try {
116:                    st = conn
117:                            .prepareStatement("insert into SMALL_BLOB_TABLE(SMALL_BLOB) values(?)");
118:                    stream = new TestDataStream(512);
119:                    st.setBinaryStream(1, stream, 512);
120:                    st.executeUpdate();
121:
122:                } finally {
123:                    if (st != null) {
124:                        st.close();
125:                    }
126:
127:                    if (stream != null) {
128:                        stream.close();
129:                    }
130:
131:                }
132:
133:            }
134:
135:            private static void createLargeBlobTestData(Connection conn)
136:                    throws SQLException, IOException {
137:
138:                PreparedStatement st = null;
139:                TestDataStream stream = null;
140:
141:                try {
142:                    st = conn
143:                            .prepareStatement("insert into LARGE_BLOB_TABLE(LARGE_BLOB) values(?)");
144:                    stream = new TestDataStream(512 * 1024);
145:                    st.setBinaryStream(1, stream, 512 * 1024);
146:
147:                    st.executeUpdate();
148:
149:                } finally {
150:                    if (st != null) {
151:                        st.close();
152:                    }
153:
154:                    if (stream != null) {
155:                        stream.close();
156:                    }
157:                }
158:            }
159:
160:            private static void createSmallClobTestData(Connection conn)
161:                    throws SQLException, IOException {
162:
163:                PreparedStatement st = null;
164:                TestDataReader reader = null;
165:
166:                try {
167:                    st = conn
168:                            .prepareStatement("insert into SMALL_CLOB_TABLE( SMALL_CLOB ) values(?)");
169:
170:                    reader = new TestDataReader(512);
171:                    st.setCharacterStream(1, reader, 512);
172:
173:                    st.executeUpdate();
174:
175:                } finally {
176:                    if (st != null)
177:                        st.close();
178:
179:                    if (reader != null)
180:                        reader.close();
181:
182:                }
183:
184:            }
185:
186:            private static void createLargeClobTestData(Connection conn)
187:                    throws SQLException, IOException {
188:
189:                PreparedStatement st = null;
190:                TestDataReader reader = null;
191:
192:                try {
193:                    st = conn
194:                            .prepareStatement("insert into LARGE_CLOB_TABLE( LARGE_CLOB ) values(?)");
195:
196:                    reader = new TestDataReader(512 * 1024);
197:                    st.setCharacterStream(1, reader, 512 * 1024);
198:
199:                    st.executeUpdate();
200:
201:                } finally {
202:                    if (st != null)
203:                        st.close();
204:
205:                    if (reader != null)
206:                        reader.close();
207:                }
208:            }
209:
210:            private static void executeTests(Connection conn)
211:                    throws SQLException, IOException {
212:
213:                executeTestOnSmallBlob(conn);
214:                executeTestOnLargeBlob(conn);
215:                executeTestOnSmallClob(conn);
216:                executeTestOnLargeClob(conn);
217:
218:            }
219:
220:            private static void executeTestOnSmallBlob(Connection conn)
221:                    throws SQLException, IOException {
222:
223:                BlobTester tester = new BlobTester("SMALL_BLOB_TABLE",
224:                        "SMALL_BLOB");
225:                tester.testGetStreamTwice(conn);
226:
227:            }
228:
229:            private static void executeTestOnLargeBlob(Connection conn)
230:                    throws SQLException, IOException {
231:
232:                BlobTester tester = new BlobTester("LARGE_BLOB_TABLE",
233:                        "LARGE_BLOB");
234:                tester.testGetStreamTwice(conn);
235:
236:            }
237:
238:            private static void executeTestOnSmallClob(Connection conn)
239:                    throws SQLException, IOException {
240:
241:                ClobTester tester = new ClobTester("SMALL_CLOB_TABLE",
242:                        "SMALL_CLOB");
243:                tester.testGetReaderTwice(conn);
244:
245:            }
246:
247:            private static void executeTestOnLargeClob(Connection conn)
248:                    throws SQLException, IOException {
249:
250:                ClobTester tester = new ClobTester("LARGE_CLOB_TABLE",
251:                        "LARGE_CLOB");
252:                tester.testGetReaderTwice(conn);
253:
254:            }
255:
256:            private static void dropTestTables(Connection conn)
257:                    throws SQLException {
258:
259:                Statement st = null;
260:
261:                try {
262:                    st = conn.createStatement();
263:                    st.execute("drop table SMALL_BLOB_TABLE");
264:                    st.execute("drop table LARGE_BLOB_TABLE");
265:                    st.execute("drop table SMALL_CLOB_TABLE");
266:                    st.execute("drop table LARGE_CLOB_TABLE");
267:
268:                } finally {
269:                    if (st != null)
270:                        st.close();
271:                }
272:
273:            }
274:
275:            static class TestDataStream extends InputStream {
276:
277:                private long streamedLength = 0;
278:                private final long total;
279:
280:                public TestDataStream(long length) {
281:                    total = length;
282:                }
283:
284:                public int read() {
285:
286:                    if (streamedLength >= total) {
287:                        return -1;
288:                    }
289:
290:                    return (int) ((streamedLength++) % 256L);
291:
292:                }
293:
294:                public void close() {
295:                    streamedLength = total;
296:                }
297:
298:            }
299:
300:            static class TestDataReader extends Reader {
301:
302:                private long wroteLength = 0;
303:                private final long total;
304:
305:                public TestDataReader(long length) {
306:                    total = length;
307:                }
308:
309:                public void close() {
310:                    wroteLength = total;
311:                }
312:
313:                public int read(char[] cbuf, int off, int len) {
314:
315:                    if (wroteLength >= total)
316:                        return -1;
317:
318:                    int i;
319:                    for (i = off; i < off + len && wroteLength < total; i++, wroteLength++) {
320:
321:                        cbuf[i] = (char) (wroteLength % 0x10000L);
322:
323:                    }
324:
325:                    return i - off;
326:                }
327:
328:            }
329:
330:            static class BlobTester {
331:
332:                final String tableName;
333:                final String colName;
334:
335:                BlobTester(String tableName, String colName) {
336:
337:                    this .tableName = tableName;
338:                    this .colName = colName;
339:
340:                }
341:
342:                public void testGetStreamTwice(Connection conn)
343:                        throws SQLException, IOException {
344:
345:                    Statement st = null;
346:                    ResultSet rs = null;
347:                    InputStream is = null;
348:
349:                    try {
350:                        st = conn.createStatement();
351:
352:                        rs = st.executeQuery("select " + colName + " "
353:                                + "from " + tableName);
354:                        rs.next();
355:
356:                        System.out.println("get stream from " + tableName + "."
357:                                + colName + " ...");
358:                        is = rs.getBinaryStream(1);
359:                        is.close();
360:
361:                        System.out.println("get stream from " + tableName + "."
362:                                + colName + " again ...");
363:                        is = rs.getBinaryStream(1);
364:
365:                        System.out
366:                                .println("Expected exception did not happen.");
367:
368:                    } catch (SQLException e) {
369:                        acknowledgeException(e);
370:                    } finally {
371:                        if (st != null)
372:                            st.close();
373:
374:                        if (rs != null)
375:                            rs.close();
376:
377:                        if (is != null)
378:                            is.close();
379:
380:                    }
381:                }
382:            }
383:
384:            private static void acknowledgeException(Throwable e) {
385:                String message = e.getMessage();
386:
387:                // Get rid of network and jdk-specific canons by stripping off
388:                // introductory exception class name
389:                if (message.startsWith("java.sql.")) {
390:                    int boilerplateEndIndex = message.indexOf(": ");
391:                    message = message.substring(boilerplateEndIndex + 2,
392:                            message.length());
393:                }
394:
395:                System.out.println("Expected exception may happen.");
396:                System.out.println(e.getMessage());
397:            }
398:
399:            static class ClobTester {
400:
401:                final String tableName;
402:                final String colName;
403:
404:                public ClobTester(String tableName, String colName) {
405:
406:                    this .tableName = tableName;
407:                    this .colName = colName;
408:
409:                }
410:
411:                public void testGetReaderTwice(Connection conn)
412:                        throws SQLException, IOException {
413:
414:                    Statement st = null;
415:                    ResultSet rs = null;
416:                    Reader reader = null;
417:
418:                    try {
419:                        st = conn.createStatement();
420:
421:                        rs = st.executeQuery("select " + colName + " "
422:                                + "from " + tableName);
423:                        rs.next();
424:
425:                        System.out.println("get reader from " + tableName + "."
426:                                + colName + " ...");
427:                        reader = rs.getCharacterStream(1);
428:                        reader.close();
429:
430:                        System.out.println("get reader from " + tableName + "."
431:                                + colName + "again ...");
432:                        reader = rs.getCharacterStream(1);
433:
434:                        System.out
435:                                .println("Expected exception did not happen.");
436:
437:                    } catch (SQLException e) {
438:                        acknowledgeException(e);
439:                    } finally {
440:                        if (st != null)
441:                            st.close();
442:
443:                        if (rs != null)
444:                            rs.close();
445:
446:                        if (reader != null)
447:                            reader.close();
448:
449:                    }
450:                }
451:            }
452:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.