KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbc4 > TestConnectionMethods


1 /*
2  
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.TestConnectionMethods
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.jdbc4;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.net.InetAddress JavaDoc;
33 import java.sql.Blob JavaDoc;
34 import java.sql.Clob JavaDoc;
35 import java.sql.Connection JavaDoc;
36 import java.sql.DriverManager JavaDoc;
37 import java.sql.PreparedStatement JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Statement JavaDoc;
40 import org.apache.derby.iapi.error.StandardException;
41 import org.apache.derby.drda.NetworkServerControl;
42 import org.apache.derby.tools.ij;
43 import org.apache.derby.shared.common.reference.SQLState;
44 import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
45 import org.apache.derbyTesting.functionTests.util.TestUtil;
46
47 /**
48  * This class is used to test the implementations of the JDBC 4.0 methods
49  * in the Connection interface
50  */

51
52 public class TestConnectionMethods {
53     Connection JavaDoc conn = null;
54     
55     private FileOutputStream JavaDoc serverOutput;
56     
57     /**
58      * Constructor for an object that is used for running test of the
59      * new connection methods defined by JDBC 4.
60      */

61     public TestConnectionMethods(Connection JavaDoc connIn) {
62         conn = connIn;
63     }
64     
65     /**
66      * Test the createClob method implementation in the Connection interface
67      * in the Network Client
68      */

69     void t_createClob_Client() {
70         int c;
71         Clob JavaDoc clob;
72         try {
73             Statement JavaDoc s = conn.createStatement();
74             s.execute("create table clobtable2(n int,clobcol CLOB)");
75             PreparedStatement JavaDoc ps = conn.prepareStatement("insert into clobtable2" +
76                     " values(?,?)");
77             ps.setInt(1,1000);
78             clob = conn.createClob();
79             File JavaDoc file = new File JavaDoc("extin/short.txt");
80             FileInputStream JavaDoc is = new FileInputStream JavaDoc(file);
81             OutputStream JavaDoc os = clob.setAsciiStream(1);
82             c = is.read();
83             while(c>0) {
84                 os.write(c);
85                 c = is.read();
86             }
87             ps.setClob(2, clob);
88             ps.executeUpdate();
89         } catch(SQLException JavaDoc e) {
90             e.printStackTrace();
91         } catch(FileNotFoundException JavaDoc fnfe){
92             fnfe.printStackTrace();
93         } catch(IOException JavaDoc ioe) {
94             ioe.printStackTrace();
95         } catch(Exception JavaDoc e) {
96             e.printStackTrace();
97         }
98     }
99     /**
100      * Test the createBlob method implementation in the Connection interface for
101      * in the Network Client
102      */

103     void t_createBlob_Client() {
104         int c;
105         Blob JavaDoc blob;
106         try {
107             Statement JavaDoc s = conn.createStatement();
108             s.execute("create table blobtable2(n int,blobcol BLOB)");
109             PreparedStatement JavaDoc ps = conn.prepareStatement("insert into blobtable2" +
110                     " values(?,?)");
111             ps.setInt(1,1000);
112             blob = conn.createBlob();
113             File JavaDoc file = new File JavaDoc("extin/short.txt");
114             FileInputStream JavaDoc is = new FileInputStream JavaDoc(file);
115             OutputStream JavaDoc os = blob.setBinaryStream(1);
116             c = is.read();
117             while(c>0) {
118                 os.write(c);
119                 c = is.read();
120             }
121             ps.setBlob(2, blob);
122             ps.executeUpdate();
123         } catch(SQLException JavaDoc e) {
124             e.printStackTrace();
125         } catch(FileNotFoundException JavaDoc fnfe){
126             fnfe.printStackTrace();
127         } catch(IOException JavaDoc ioe) {
128             ioe.printStackTrace();
129         } catch(Exception JavaDoc e) {
130             e.printStackTrace();
131         }
132     }
133     
134     /**
135      * Test the Connection.isValid method
136      */

137     void t_isValid() {
138         /*
139          * Test illegal parameter values
140          */

141         try {
142             conn.isValid(-1); // Negative timeout
143
System.out.println("FAIL: isValid(-1): " +
144                                "Invalid argument execption not thrown");
145         } catch (SQLException JavaDoc e) {
146             if(!StandardException.getSQLStateFromIdentifier(
147                 SQLState.INVALID_API_PARAMETER).equals(e.getSQLState())) {
148                 System.out.println("FAIL: isValid(-1): Unexpected SQLException" +
149                                    e);
150             }
151         }
152
153         /*
154          * Test with no timeout
155          */

156         try {
157             if (!conn.isValid(0)) {
158                 System.out.println("FAIL: isValid(0): returned false");
159             }
160         } catch(Exception JavaDoc e) {
161             System.out.println("FAIL: isValid(0): Unexpected exception: " + e);
162         }
163
164         /*
165          * Test with a valid timeout
166          */

167         try {
168             if (!conn.isValid(1)) {
169                 System.out.println("FAIL: isValid(1): returned false");
170             }
171         } catch(Exception JavaDoc e) {
172             System.out.println("FAIL: isValid(1): Unexpected exception: " + e);
173         }
174
175         /*
176          * Test on a closed connection
177          */

178         try {
179             conn.close();
180         } catch (SQLException JavaDoc e) {
181             System.out.println("FAIL: close failed: Unexpected exception: " + e);
182         }
183
184         try {
185             if (conn.isValid(0)) {
186                 System.out.println("FAIL: isValid(0) on closed connection: " +
187                                    "returned true");
188             }
189         } catch(Exception JavaDoc e) {
190             System.out.println("FAIL: isValid(0) on closed connection: " +
191                                "Unexpected exception: " + e);
192         }
193
194         /* Open a new connection and test it */
195         try {
196             conn = ij.startJBMS();
197         } catch (Exception JavaDoc e) {
198             System.out.println("FAIL: failed to open new connection: " +
199                                "Unexpected exception: " + e);
200         }
201
202         try {
203             if (!conn.isValid(0)) {
204                 System.out.println("FAIL: isValid(0) on open connection: " +
205                                    "returned false");
206             }
207         } catch(Exception JavaDoc e) {
208             System.out.println("FAIL: isValid(0) on open connection: " +
209                                "Unexpected exception: " + e);
210         }
211
212         /*
213          * Test on stopped database
214          */

215         shutdownDatabase();
216
217         /* Test if that connection is not valid */
218         try {
219             if (conn.isValid(0)) {
220                 System.out.println("FAIL: isValid(0) on stopped database: " +
221                                    "returned true");
222             }
223         } catch(Exception JavaDoc e) {
224             System.out.println("FAIL: isValid(0) on a stopped database: " +
225                                "Unexpected exception: " + e);
226         }
227
228         /* Start the database by getting a new connection to it */
229         try {
230             conn = ij.startJBMS();
231         } catch (Exception JavaDoc e) {
232             System.out.println("FAIL: failed to re-start database: " +
233                                "Unexpected exception: " + e);
234         }
235
236         /* Check that a new connection to the newly started database is valid */
237         try {
238             if (!conn.isValid(0)) {
239                 System.out.println("FAIL: isValid(0) on new connection: " +
240                                    "returned false");
241             }
242         } catch(Exception JavaDoc e) {
243             System.out.println("FAIL: isValid(0) on new connection: " +
244                                "Unexpected exception: " + e);
245         }
246
247         /*
248          * Test on stopped Network Server client
249          */

250         if ( !usingEmbeddedClient() ) {
251             stopNetworkServer();
252
253             /* Test that the connection is not valid */
254             try {
255                 if (conn.isValid(0)) {
256                     System.out.println("FAIL: isValid(0) on stopped database: " +
257                                       "returned true");
258                 }
259             } catch(Exception JavaDoc e) {
260                 System.out.println("FAIL: isValid(0) on a stopped database: " +
261                                    "Unexpected exception: " + e);
262             }
263
264             /*
265              * Start the network server and get a new connection and check that
266              * the new connection is valid.
267              */

268             startNetworkServer();
269
270             try {
271                 // Get a new connection to the database
272
conn = ij.startJBMS();
273             } catch (Exception JavaDoc e) {
274                 System.out.println("FAIL: failed to re-start database: " +
275                                    "Unexpected exception: " + e);
276                 e.printStackTrace();
277             }
278
279             /* Check that a new connection to the newly started Derby is valid */
280             try {
281                 if (!conn.isValid(0)) {
282                     System.out.println("FAIL: isValid(0) on new connection: " +
283                                        "returned false");
284                 }
285             } catch(Exception JavaDoc e) {
286                 System.out.println("FAIL: isValid(0) on new connection: " +
287                                   "Unexpected exception: " + e);
288             }
289         }
290     }
291
292     public void startTestConnectionMethods_Client() {
293         t_createClob_Client();
294         t_createBlob_Client();
295         t_isValid();
296     }
297     
298     public void startTestConnectionMethods_Embedded() {
299         t_isValid();
300     }
301
302     /**
303      * Shut down the test database
304      */

305     private void shutdownDatabase() {
306         try {
307             // Get the name for the database from the test's property file
308
String JavaDoc databaseName = System.getProperty("ij.dataSource.databaseName");
309             if (databaseName != null) {
310                 TestUtil.getConnection(databaseName, "shutdown=true");
311             }
312             else {
313                 System.out.println("FAIL: shutdownDatabase: " +
314                            "property ij.dataSource.databaseName not defined");
315             }
316      } catch (Exception JavaDoc e) {
317             // Ignore any exeptions from shutdown
318
}
319     }
320
321
322     /**
323      * Stop the network server
324      */

325     private void stopNetworkServer() {
326         try {
327             NetworkServerControl networkServer = new NetworkServerControl();
328             networkServer.shutdown();
329             if (serverOutput != null) {
330                 serverOutput.close();
331             }
332         } catch(Exception JavaDoc e) {
333             System.out.println("INFO: Network server shutdown returned: " + e);
334         }
335     }
336
337
338     /**
339      * Start the network server
340      */

341     private void startNetworkServer() {
342         String JavaDoc hostName = null;
343         int serverPort;
344
345         // Determines which host and port to run the network server on
346
// This is based how it is done in the test testSecMec.java
347
String JavaDoc serverName = TestUtil.getHostName();
348         if (serverName.equals("localhost")) {
349             serverPort = 1527;
350         }
351         else {
352             serverPort = 20000;
353         }
354
355         try {
356             String JavaDoc fileName = System.getProperty( "derby.system.home", "")
357                     + "serverConsoleOutput.log";
358             serverOutput = new FileOutputStream JavaDoc(fileName);
359
360             NetworkServerControl networkServer =
361                      new NetworkServerControl(InetAddress.getByName(serverName),
362                                               serverPort);
363             networkServer.start(new PrintWriter JavaDoc(serverOutput));
364
365             // Wait for the network server to start
366
boolean started = false;
367             int retries = 10; // Max retries = max seconds to wait
368
while (!started && retries > 0) {
369                 try {
370                     // Sleep 1 second and then ping the network server
371
Thread.sleep(1000);
372                     networkServer.ping();
373
374                     // If ping does not throw an exception the server has started
375
started = true;
376                 } catch(Exception JavaDoc e) {
377                     System.out.println("INFO: ping returned: " + e);
378                     retries--;
379              }
380          }
381
382             // Check if we got a reply on ping
383
if (!started) {
384                 System.out.println("FAIL: Failed to start network server");
385             }
386         } catch (Exception JavaDoc e) {
387             System.out.println("FAIL: startNetworkServer got exception: " + e);
388         }
389     }
390
391
392     /**
393      * <p>
394      * Return true if we're running under the embedded client.
395      * </p>
396      */

397     private static boolean usingEmbeddedClient()
398     {
399         return "embedded".equals( System.getProperty( "framework" ) );
400     }
401
402     
403     public static void main(String JavaDoc args[]) {
404         try {
405             // use the ij utility to read the property file and
406
// make the initial connection.
407
ij.getPropertyArg(args);
408         
409             Connection JavaDoc conn_main = ij.startJBMS();
410
411             if ( usingEmbeddedClient() )
412             {
413                 TestConnectionMethods tcm = new TestConnectionMethods( conn_main );
414                 tcm.startTestConnectionMethods_Embedded();
415             }
416             else // DerbyNetClient
417
{
418                 TestConnectionMethods tcm1 = new TestConnectionMethods( conn_main );
419                 tcm1.startTestConnectionMethods_Client();
420             }
421
422             conn_main.close();
423
424         } catch(Exception JavaDoc e) {
425             System.out.println(""+e);
426             e.printStackTrace();
427         }
428     }
429 }
430
Popular Tags