Source Code Cross Referenced for OutBufferedStream.java in  » Database-DBMS » db-derby-10.2 » org » apache » derbyTesting » functionTests » tests » derbynet » 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.derbynet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:        Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.OutBufferedStream
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.derbynet;
023:
024:        import java.io.ByteArrayInputStream;
025:        import java.io.InputStream;
026:        import java.io.OutputStream;
027:        import java.io.FileOutputStream;
028:        import java.net.InetAddress;
029:        import java.sql.DriverManager;
030:        import java.sql.Connection;
031:        import java.sql.Statement;
032:        import java.sql.PreparedStatement;
033:        import java.sql.CallableStatement;
034:        import java.sql.ResultSet;
035:        import java.sql.Blob;
036:
037:        import java.io.IOException;
038:        import java.io.FileNotFoundException;
039:        import java.sql.SQLException;
040:
041:        import org.apache.derby.drda.NetworkServerControl;
042:        import org.apache.derby.tools.ij;
043:        import org.apache.derbyTesting.functionTests.util.TestUtil;
044:
045:        /**
046:         *
047:         * This program tests streaming lob with derby.drda.streamOutBufferSize configuration.
048:         *
049:         * When derby.drda.streamOutBufferSize was configured, 
050:         * a buffer of configured size was placed at network server just before sending stream to client.
051:         *
052:         * The configuration was written in OutBufferedStream_app.properties.
053:         *
054:         */
055:        public class OutBufferedStream {
056:            private static NetworkServerControl networkServer = null;
057:
058:            // Need this to keep track of database has been created or not
059:            // to avoid case of DERBY-300
060:            private static boolean dbNotCreated = true;
061:
062:            public static void main(String[] args) {
063:
064:                try {
065:
066:                    ij.getPropertyArg(args);
067:
068:                    SwitchablePrintStream testExecutionOutput = new SwitchablePrintStream(
069:                            System.out);
070:                    SwitchablePrintStream testExecutionErrOutput = new SwitchablePrintStream(
071:                            System.out);
072:
073:                    OutputStream originalOut = System.out;
074:                    OutputStream originalErr = System.err;
075:
076:                    System.setOut(testExecutionOutput);
077:                    System.setErr(testExecutionErrOutput);
078:
079:                    startServer();
080:
081:                    System.out.println("start test.");
082:
083:                    createTestTable();
084:                    testReadOfLob();
085:
086:                    System.out.println("test done.");
087:
088:                    OutputStream shutdownLogFileStream = null;
089:                    OutputStream shutdownErrLogFileStream = null;
090:
091:                    try {
092:                        shutdownLogFileStream = getShutdownLogFileStream();
093:                        shutdownErrLogFileStream = getShutdownErrLogFileStream();
094:
095:                        testExecutionOutput.switchOutput(shutdownLogFileStream);
096:                        testExecutionErrOutput
097:                                .switchOutput(shutdownErrLogFileStream);
098:
099:                        shutdownServer();
100:
101:                        testExecutionOutput.switchOutput(originalOut);
102:                        testExecutionErrOutput.switchOutput(originalErr);
103:
104:                    } finally {
105:
106:                        if (shutdownLogFileStream != null) {
107:                            shutdownLogFileStream.flush();
108:                            shutdownLogFileStream.close();
109:                        }
110:
111:                        if (shutdownErrLogFileStream != null) {
112:                            shutdownErrLogFileStream.flush();
113:                            shutdownErrLogFileStream.close();
114:                        }
115:                    }
116:
117:                } catch (Throwable t) {
118:                    t.printStackTrace();
119:
120:                }
121:            }
122:
123:            private static void createTestTable() throws SQLException,
124:                    IllegalAccessException, ClassNotFoundException,
125:                    InstantiationException {
126:
127:                Connection conn = getConnection();
128:
129:                Statement createTableSt = conn.createStatement();
130:                createTableSt
131:                        .execute("create table TEST_TABLE( TEST_COL blob( 65536 ))");
132:                createTableSt.close();
133:
134:                conn.commit();
135:                conn.close();
136:
137:            }
138:
139:            private static void testReadOfLob() throws SQLException,
140:                    IOException, IllegalAccessException,
141:                    ClassNotFoundException, InstantiationException {
142:
143:                Connection conn = getConnection();
144:
145:                conn.setAutoCommit(false);
146:
147:                PreparedStatement insertLobSt = conn
148:                        .prepareStatement("insert into TEST_TABLE( TEST_COL ) values(?)");
149:                insertLobSt.setBinaryStream(1,
150:                        createOriginalDataInputStream(65536), 65536);
151:                insertLobSt.executeUpdate();
152:                insertLobSt.close();
153:
154:                conn.commit();
155:
156:                PreparedStatement st = conn
157:                        .prepareStatement("select TEST_COL from TEST_TABLE");
158:                ResultSet rs = st.executeQuery();
159:
160:                rs.next();
161:
162:                InputStream is = rs.getBinaryStream(1);
163:
164:                int c;
165:                while ((c = is.read()) > -1) {
166:
167:                    System.out.print(c);
168:                    System.out.print(",");
169:
170:                    if (((c + 1) % 256) == 0)
171:                        System.out.println();
172:
173:                }
174:
175:                is.close();
176:
177:                rs.close();
178:                st.close();
179:
180:                conn.commit();
181:                conn.close();
182:
183:                System.out.println();
184:
185:            }
186:
187:            private static ByteArrayInputStream createOriginalDataInputStream(
188:                    int length) {
189:
190:                byte[] originalValue = new byte[length];
191:
192:                for (int i = 0; i < originalValue.length; i++) {
193:                    originalValue[i] = (byte) (i % 256);
194:                }
195:
196:                return new ByteArrayInputStream(originalValue);
197:
198:            }
199:
200:            protected static boolean isServerStarted(
201:                    NetworkServerControl server, int ntries) {
202:                for (int i = 1; i <= ntries; i++) {
203:                    try {
204:                        Thread.sleep(500);
205:                        server.ping();
206:                        return true;
207:                    } catch (Exception e) {
208:                        if (i == ntries)
209:                            return false;
210:                    }
211:                }
212:                return false;
213:            }
214:
215:            private static void startServer() throws Exception {
216:
217:                try {
218:                    TestUtil.loadDriver();
219:
220:                } catch (Exception e) {
221:                    e.printStackTrace();
222:                }
223:
224:                networkServer = new NetworkServerControl(InetAddress
225:                        .getByName("localhost"), 1527);
226:                networkServer.start(null);
227:
228:                if (!isServerStarted(networkServer, 60))
229:                    System.exit(-1);
230:
231:            }
232:
233:            private static void shutdownServer() throws Exception {
234:
235:                networkServer.shutdown();
236:
237:            }
238:
239:            private static Connection getConnection() throws SQLException {
240:
241:                String dbName = "wombat";
242:                if (dbNotCreated) {
243:                    dbName = dbName + ";create=true";
244:                    dbNotCreated = false;
245:                }
246:                return DriverManager.getConnection(TestUtil.getJdbcUrlPrefix(
247:                        "localhost", 1527)
248:                        + dbName, "testuser", "testpassword");
249:
250:            }
251:
252:            private static OutputStream getShutdownLogFileStream()
253:                    throws FileNotFoundException {
254:
255:                return new FileOutputStream("outBufferedStream."
256:                        + System.getProperty("framework", "") + "."
257:                        + "shutdown.std.log");
258:            }
259:
260:            private static OutputStream getShutdownErrLogFileStream()
261:                    throws FileNotFoundException {
262:
263:                return new FileOutputStream("outBufferedStream."
264:                        + System.getProperty("framework", "") + "."
265:                        + "shutdown.err.log");
266:            }
267:
268:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.