KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > derbynet > OutBufferedStream


1 /*
2
3 Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.OutBufferedStream
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.derbynet;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.net.InetAddress JavaDoc;
29 import java.sql.DriverManager JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.Statement JavaDoc;
32 import java.sql.PreparedStatement JavaDoc;
33 import java.sql.CallableStatement JavaDoc;
34 import java.sql.ResultSet JavaDoc;
35 import java.sql.Blob JavaDoc;
36
37 import java.io.IOException JavaDoc;
38 import java.io.FileNotFoundException JavaDoc;
39 import java.sql.SQLException JavaDoc;
40
41 import org.apache.derby.drda.NetworkServerControl;
42 import org.apache.derby.tools.ij;
43 import org.apache.derbyTesting.functionTests.util.TestUtil;
44
45 /**
46  *
47  * This program tests streaming lob with derby.drda.streamOutBufferSize configuration.
48  *
49  * When derby.drda.streamOutBufferSize was configured,
50  * a buffer of configured size was placed at network server just before sending stream to client.
51  *
52  * The configuration was written in OutBufferedStream_app.properties.
53  *
54  */

55 public class OutBufferedStream {
56     private static NetworkServerControl networkServer = null;
57     
58     // Need this to keep track of database has been created or not
59
// to avoid case of DERBY-300
60
private static boolean dbNotCreated = true;
61     
62     public static void main(String JavaDoc[] args){
63     
64     try{
65         
66         ij.getPropertyArg(args);
67         
68         
69         SwitchablePrintStream testExecutionOutput = new SwitchablePrintStream(System.out);
70         SwitchablePrintStream testExecutionErrOutput = new SwitchablePrintStream(System.out);
71         
72         OutputStream JavaDoc originalOut = System.out;
73         OutputStream JavaDoc originalErr = System.err;
74
75         System.setOut(testExecutionOutput);
76         System.setErr(testExecutionErrOutput);
77         
78         startServer();
79         
80         System.out.println("start test.");
81
82         createTestTable();
83         testReadOfLob();
84         
85         System.out.println("test done.");
86         
87         OutputStream JavaDoc shutdownLogFileStream = null;
88         OutputStream JavaDoc shutdownErrLogFileStream = null;
89
90         try{
91         shutdownLogFileStream = getShutdownLogFileStream();
92         shutdownErrLogFileStream = getShutdownErrLogFileStream();
93         
94         testExecutionOutput.switchOutput(shutdownLogFileStream);
95         testExecutionErrOutput.switchOutput(shutdownErrLogFileStream);
96
97         shutdownServer();
98         
99         testExecutionOutput.switchOutput(originalOut);
100         testExecutionErrOutput.switchOutput(originalErr);
101         
102         }finally{
103         
104         if(shutdownLogFileStream != null){
105             shutdownLogFileStream.flush();
106             shutdownLogFileStream.close();
107         }
108
109         if(shutdownErrLogFileStream != null){
110             shutdownErrLogFileStream.flush();
111             shutdownErrLogFileStream.close();
112         }
113         }
114         
115     }catch(Throwable JavaDoc t){
116         t.printStackTrace();
117         
118     }
119     }
120     
121     
122     private static void createTestTable()
123     throws SQLException JavaDoc,
124            IllegalAccessException JavaDoc,
125            ClassNotFoundException JavaDoc,
126            InstantiationException JavaDoc
127     {
128
129     Connection JavaDoc conn = getConnection();
130     
131     Statement JavaDoc createTableSt = conn.createStatement();
132     createTableSt.execute("create table TEST_TABLE( TEST_COL blob( 65536 ))");
133     createTableSt.close();
134     
135     conn.commit();
136     conn.close();
137
138     }
139     
140     
141     private static void testReadOfLob()
142     throws SQLException JavaDoc,
143            IOException JavaDoc,
144            IllegalAccessException JavaDoc,
145            ClassNotFoundException JavaDoc,
146            InstantiationException JavaDoc
147     {
148     
149     Connection JavaDoc conn =
150         getConnection();
151     
152     conn.setAutoCommit(false);
153     
154     PreparedStatement JavaDoc insertLobSt =
155         conn.prepareStatement("insert into TEST_TABLE( TEST_COL ) values(?)");
156     insertLobSt.setBinaryStream(1,
157                     createOriginalDataInputStream( 65536 ),
158                     65536 );
159     insertLobSt.executeUpdate();
160     insertLobSt.close();
161
162     conn.commit();
163     
164     PreparedStatement JavaDoc st = conn.prepareStatement("select TEST_COL from TEST_TABLE");
165     ResultSet JavaDoc rs = st.executeQuery();
166     
167     rs.next();
168     
169     InputStream JavaDoc is = rs.getBinaryStream(1);
170
171     int c;
172     while( ( c = is.read() ) > -1 ){
173         
174         System.out.print(c);
175         System.out.print(",");
176         
177         if( ( (c + 1) % 256 ) == 0 )
178         System.out.println();
179
180     }
181     
182     is.close();
183     
184     rs.close();
185     st.close();
186     
187     conn.commit();
188     conn.close();
189
190     System.out.println();
191     
192     }
193     
194     
195     private static ByteArrayInputStream JavaDoc createOriginalDataInputStream(int length){
196
197     byte[] originalValue = new byte[ length ];
198
199     for(int i = 0; i < originalValue.length; i ++){
200         originalValue[i] = (byte) (i % 256);
201     }
202     
203     return new ByteArrayInputStream JavaDoc(originalValue);
204
205     }
206     
207     
208     protected static boolean isServerStarted(NetworkServerControl server,
209                          int ntries){
210     for (int i = 1; i <= ntries; i ++){
211         try {
212         Thread.sleep(500);
213         server.ping();
214         return true;
215         } catch (Exception JavaDoc e) {
216         if (i == ntries)
217             return false;
218         }
219     }
220     return false;
221     }
222
223     
224     private static void startServer()
225     throws Exception JavaDoc{
226     
227     try{
228         TestUtil.loadDriver();
229         
230     }catch(Exception JavaDoc e){
231         e.printStackTrace();
232     }
233     
234     
235     networkServer =
236         new NetworkServerControl(InetAddress.getByName("localhost"),
237                      1527);
238     networkServer.start( null );
239         
240     if(! isServerStarted( networkServer,
241                   60 ) )
242         System.exit(-1);
243     
244     }
245     
246     
247     private static void shutdownServer()
248     throws Exception JavaDoc{
249     
250     networkServer.shutdown();
251     
252     }
253     
254     
255     private static Connection JavaDoc getConnection()
256     throws SQLException JavaDoc {
257     
258     String JavaDoc dbName = "wombat";
259     if (dbNotCreated)
260     {
261         dbName = dbName + ";create=true";
262         dbNotCreated = false;
263     }
264     return DriverManager.getConnection(TestUtil.getJdbcUrlPrefix("localhost",
265                                      1527) +
266                        dbName,
267                        "testuser",
268                        "testpassword");
269     
270     }
271     
272     
273     private static OutputStream JavaDoc getShutdownLogFileStream()
274     throws FileNotFoundException JavaDoc {
275     
276     return new FileOutputStream JavaDoc("outBufferedStream." +
277                     System.getProperty("framework","") + "." +
278                     "shutdown.std.log");
279     }
280     
281     
282     private static OutputStream JavaDoc getShutdownErrLogFileStream()
283     throws FileNotFoundException JavaDoc{
284     
285     return new FileOutputStream JavaDoc("outBufferedStream." +
286                     System.getProperty("framework","") + "." +
287                     "shutdown.err.log");
288     }
289     
290     
291 }
292
Popular Tags