KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3 Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.SuicideOfStreaming
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.net.InetAddress JavaDoc;
27 import java.sql.DriverManager JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.Statement JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.CallableStatement JavaDoc;
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.Blob JavaDoc;
34
35 import java.io.IOException JavaDoc;
36 import java.sql.SQLException JavaDoc;
37
38 import org.apache.derby.drda.NetworkServerControl;
39 import org.apache.derby.tools.ij;
40 import org.apache.derby.client.am.SqlCode;
41 import org.apache.derbyTesting.functionTests.util.TestUtil;
42 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
43
44 /**
45  *
46  * This test needs to be build with sanity=true and executed with system property of derby.debug.suicideOfLayerBStreaming=true.
47  * In thosee situation, exception will be happen when streaming from server to client as negative test.
48  *
49  * See SuicideOfStreaming_app.properties also.
50  *
51  */

52 public class SuicideOfStreaming extends BaseJDBCTestCase {
53     
54     private static NetworkServerControl networkServer = null;
55     
56     public static void main(String JavaDoc[] args){
57     
58     try{
59         
60         setSystemProperty("derby.debug.suicideOfLayerBStreaming","true");
61         
62         startServer();
63         
64         createTestTable();
65         testInterruptedReadOfLob();
66         
67         shutdownServer();
68         
69         fail("Streaming was not encountered exception. Suicide of streaming seems to be failed.");
70
71     }catch(Throwable JavaDoc t){
72         examineThrowable(t);
73         
74     }
75     }
76     
77     
78     private static void createTestTable()
79     throws SQLException JavaDoc,
80            IllegalAccessException JavaDoc,
81            ClassNotFoundException JavaDoc,
82            InstantiationException JavaDoc
83     {
84
85     Connection JavaDoc conn = connectServer();
86     
87     Statement JavaDoc createTableSt = conn.createStatement();
88     createTableSt.execute("create table TEST_TABLE( TEST_COL blob( 65536 ))");
89     createTableSt.close();
90     
91     conn.commit();
92     conn.close();
93
94     }
95     
96     
97     private static void testInterruptedReadOfLob()
98     throws SQLException JavaDoc,
99            IOException JavaDoc,
100            IllegalAccessException JavaDoc,
101            ClassNotFoundException JavaDoc,
102            InstantiationException JavaDoc
103     {
104     
105     Connection JavaDoc conn =
106         connectServer();
107     
108     conn.setAutoCommit(false);
109     
110     PreparedStatement JavaDoc insertLobSt =
111         conn.prepareStatement("insert into TEST_TABLE( TEST_COL ) values(?)");
112     insertLobSt.setBinaryStream(1,
113                     createOriginalDataInputStream( 65536 ),
114                     65536 );
115     insertLobSt.executeUpdate();
116     insertLobSt.close();
117
118     conn.commit();
119     
120     PreparedStatement JavaDoc st = conn.prepareStatement("select TEST_COL from TEST_TABLE");
121     ResultSet JavaDoc rs = st.executeQuery();
122     
123     rs.next();
124     
125     InputStream JavaDoc is = rs.getBinaryStream(1);
126
127     int c;
128     while( ( c = is.read() ) > -1 ){
129         
130         System.out.print(c);
131         System.out.print(",");
132         
133         if( ( (c + 1) % 256 ) == 0 )
134         System.out.println();
135
136     }
137     
138     is.close();
139     
140     rs.close();
141     st.close();
142     
143     conn.commit();
144     conn.close();
145
146     }
147     
148     
149     private static ByteArrayInputStream JavaDoc createOriginalDataInputStream(int length){
150
151     byte[] originalValue = new byte[ length ];
152
153     for(int i = 0; i < originalValue.length; i ++){
154         originalValue[i] = (byte) (i % 256);
155     }
156     
157     return new ByteArrayInputStream JavaDoc(originalValue);
158
159     }
160     
161     
162     protected static boolean isServerStarted(NetworkServerControl server,
163                          int ntries){
164     for (int i = 1; i <= ntries; i ++){
165         try {
166         Thread.sleep(500);
167         server.ping();
168         return true;
169         } catch (Exception JavaDoc e) {
170         if (i == ntries)
171             return false;
172         }
173     }
174     return false;
175     }
176
177     
178     private static void startServer()
179     throws Exception JavaDoc{
180     
181     try{
182         TestUtil.loadDriver();
183         
184     }catch(Exception JavaDoc e){
185         e.printStackTrace();
186     }
187     
188     
189     networkServer =
190         new NetworkServerControl(InetAddress.getByName("localhost"),
191                      1527);
192     networkServer.start( null );
193         
194     if(! isServerStarted( networkServer,
195                   60 ) )
196         System.exit(-1);
197     
198     }
199     
200     
201     private static void shutdownServer()
202     throws Exception JavaDoc{
203     
204     networkServer.shutdown();
205     
206     }
207     
208     
209     private static Connection JavaDoc connectServer()
210     throws SQLException JavaDoc {
211     
212     return DriverManager.getConnection(TestUtil.getJdbcUrlPrefix("localhost",
213                                      1527) +
214                        "wombat;create=true",
215                        "testuser",
216                        "testpassword");
217     
218     }
219
220     
221     
222     private static void examineThrowable( Throwable JavaDoc t ) {
223     
224     if(t instanceof SQLException JavaDoc){
225         examineSQLException( ( SQLException JavaDoc ) t );
226         
227     }else{
228         t.printStackTrace();
229         fail( t.getMessage() );
230         
231     }
232
233     }
234     
235
236     private static void examineSQLException( SQLException JavaDoc sqlex ) {
237     
238     if( ( usingDerbyNetClient() &&
239           examineExpectedInDerbyNetClient(sqlex) ) ||
240         ( usingDerbyNet() &&
241           examineExpectedInDerbyNet(sqlex) ) ){
242         
243         return;
244
245     }
246         
247     fail(sqlex.getMessage() + "," +
248          "SqlState: " + sqlex.getSQLState() + "," +
249          "SqlCode: " + sqlex.getErrorCode() );
250     
251     }
252
253
254     private static boolean examineExpectedInDerbyNetClient( SQLException JavaDoc sqlex ) {
255     return
256         sqlex.getSQLState().equals("58009") &&
257         sqlex.getErrorCode() == SqlCode.disconnectError.getCode();
258     }
259
260
261     private static boolean examineExpectedInDerbyNet( SQLException JavaDoc sqlex ) {
262     return sqlex.getErrorCode() == SqlCode.disconnectError.getCode();
263     }
264     
265     
266     //JUnit test support.
267
//
268
public SuicideOfStreaming(){
269     super("testSuicideOfStreaming");
270     }
271     
272     
273     public void testSuicideOfStreaming(){
274     main(new String JavaDoc[0]);
275     }
276     
277 }
278
279
Popular Tags