KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > closed


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.closed
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.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.ResultSetMetaData JavaDoc;
29 import java.sql.DatabaseMetaData JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.SQLWarning JavaDoc;
32
33 import org.apache.derby.tools.ij;
34 import org.apache.derby.tools.JDBCDisplayUtil;
35 import org.apache.derbyTesting.functionTests.util.TestUtil;
36
37 /**
38     Test execution of closed JDBC objects. Executing or accessing a closed
39     object should report that it is closed.
40     <p>
41     Note that alot of this behavior is not very specifically specified in
42     the JDBC guide, so this test is local to our own handler. Running JBMS
43     under other handlers (such as weblogic) may produce different results due
44     to how they cache data and reuse client-side objects.
45
46     @author ames
47  */

48 public class closed implements Runnable JavaDoc {
49
50     private static boolean jsr169_test = false;
51
52     public static void main(String JavaDoc[] args) {
53         System.out.println("Test closed starting");
54         boolean passed = true;
55
56         try {
57             Connection JavaDoc conn;
58
59             // use the ij utility to read the property file and
60
// make the initial connection.
61
ij.getPropertyArg(args);
62             conn = ij.startJBMS();
63
64             passed = testDerby62(conn) && passed;
65
66             // want all tests to run regardless of intermediate errors
67
passed = testStatement(conn) && passed;
68
69             passed = testPreparedStatement(conn) && passed;
70
71             passed = testResultSet(conn) && passed;
72
73             // this test needs to be last, because the connection will
74
// be closed by it.
75
passed = testConnection(conn) && passed;
76
77             if (!conn.isClosed()) {
78                 passed = false;
79                 System.out.println("FAIL -- connection not closed by test");
80                 conn.close();
81             }
82
83             // shutdown the database
84
System.out.println("Test database shutdown ...");
85             passed = shutdownTest("wombat", "shutdown=true");
86
87             // shutdown the system
88
System.out.println("Test system shutdown ...");
89             passed = shutdownTest("", "shutdown=true");
90             
91
92
93         } catch (Throwable JavaDoc e) {
94             passed = false;
95             System.out.println("FAIL -- unexpected exception:");
96             JDBCDisplayUtil.ShowException(System.out, e);
97         }
98
99         if (passed)
100             System.out.println("PASS");
101         System.out.println("Test closed finished");
102     }
103
104     static boolean shutdownTest(String JavaDoc databaseName, String JavaDoc shutdownString) throws SQLException JavaDoc {
105     // static boolean shutdownTest(String databaseName, String shutdownString) throws SQLException, IllegalAccessException, ClassNotFoundException, InstantiationException {
106

107         boolean passed = true;
108
109         Connection JavaDoc c1 = TestUtil.getConnection("wombat",null);
110         Connection JavaDoc c2 = TestUtil.getConnection("wombat",null);
111         Connection JavaDoc c3a = TestUtil.getConnection("wombat",null);
112         Connection JavaDoc c3b = TestUtil.getConnection("wombat",null);
113         
114         try {
115             c3a.createStatement().execute("DROP TABLE CLOSED.LOCKME");
116         } catch (SQLException JavaDoc sqle) {
117         }
118         try {
119             c3a.createStatement().execute("DROP PROCEDURE SLEEP");
120         } catch (SQLException JavaDoc sqle) {
121         }
122
123         c3a.createStatement().execute("CREATE TABLE CLOSED.LOCKME(i int)");
124         
125         c3a.createStatement().execute("create procedure sleep(t INTEGER) dynamic result sets 0 language java external name 'java.lang.Thread.sleep' parameter style java");
126         c3a.setAutoCommit(false);
127         c3a.createStatement().execute("LOCK TABLE CLOSED.LOCKME IN SHARE MODE");
128         
129         closed r2 = new closed(c2, "CALL sleep(10000)");
130         closed r3 = new closed(c3b, "LOCK TABLE CLOSED.LOCKME IN EXCLUSIVE MODE");
131
132         Thread JavaDoc t2 = new Thread JavaDoc(r2);
133         t2.start();
134         Thread JavaDoc t3 = new Thread JavaDoc(r3);
135         t3.start();
136
137         try {
138             Thread.currentThread().sleep(2000);
139         } catch (InterruptedException JavaDoc ie) {
140             System.out.println(ie);
141         }
142
143         SQLException JavaDoc s = null;
144         try {
145             TestUtil.getConnection(databaseName, shutdownString);
146         } catch (SQLException JavaDoc sqle) {
147             s = sqle;
148         }
149
150         try {
151             t2.join();
152         } catch (InterruptedException JavaDoc ie) {
153             System.out.println(ie);
154         }
155         try {
156             t3.join();
157         } catch (InterruptedException JavaDoc ie) {
158             System.out.println(ie);
159         }
160
161         System.out.println(r2.result);
162         System.out.println(r3.result);
163
164         if (s != null)
165             JDBCDisplayUtil.ShowException(System.out, s);
166
167         if (!c1.isClosed()) {
168             passed = false;
169             System.out.println("FAIL -- connection not shutdown " + databaseName + ";" + shutdownString);
170             c1.close();
171         }
172         if (!c2.isClosed()) {
173             passed = false;
174             System.out.println("FAIL -- active connection not shutdown " + databaseName + ";" + shutdownString);
175             c2.close();
176         }
177
178         System.out.println("Shutdown test completed.");
179         return passed;
180     }
181
182     // for the shutdown test
183
private Connection JavaDoc cc;
184     private String JavaDoc sql;
185     String JavaDoc result;
186     private closed(Connection JavaDoc cc, String JavaDoc sql) {
187         this.cc = cc;
188         this.sql = sql;
189     }
190     public void run() {
191
192         try {
193             cc.createStatement().execute(sql);
194             result = "Sleep thread completed " + sql;
195         } catch (SQLException JavaDoc sqle) {
196
197             // this is to avoid different cannons for different JVMs since
198
// an java.lang.InterruptedException is thrown.
199
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
200             sb.append(sql);
201             sb.append(" - ");
202             sb.append(sqle.getSQLState());
203             while (sqle != null)
204             {
205                 if (sqle != null) {
206                     sb.append(", ");
207                     sb.append(sqle.getSQLState());
208                     sb.append(" -- ");
209                     if (sqle.getMessage().indexOf("InterruptedException") != -1)
210                         sb.append("InterruptedException");
211                     else
212                     {
213                         sb.append(sqle.getMessage());
214                         sqle.printStackTrace(System.out);
215                     }
216                 } else {
217                     sb.append(sqle.getMessage());
218                 }
219                 sqle = sqle.getNextException();
220             }
221             result = sb.toString();
222         }
223     }
224
225     static boolean testStatement(Connection JavaDoc conn) throws SQLException JavaDoc {
226         Statement JavaDoc s;
227         boolean passed = true;
228
229         s = conn.createStatement();
230         s.execute("create table t (i int)");
231         s.execute("create table s (i int)");
232
233         try {
234             s.execute("create table u (i int)");
235         } catch (SQLException JavaDoc se) {
236             // out impl lets you execute from closed, as stmt object is reusable
237
// after it is closed.
238
passed = false; // won't pass unless caught
239
// could verify exception #...
240
JDBCDisplayUtil.ShowSQLException(System.out,se);
241         }
242         if (!passed)
243             System.out.println("FAIL -- no error on execute of closed statement");
244         return passed;
245     }
246
247     static boolean testPreparedStatement(Connection JavaDoc conn) throws SQLException JavaDoc {
248         PreparedStatement JavaDoc ps;
249         boolean passed = true;
250
251         ps = conn.prepareStatement("insert into t values (1)");
252         ps.execute();
253         ps.execute();
254         ps.close();
255
256         try {
257             passed = false; // won't pass unless caught
258
ps.execute();
259         } catch (SQLException JavaDoc se) {
260             passed = true;
261             // could verify exception #...
262
JDBCDisplayUtil.ShowSQLException(System.out,se);
263         }
264         if (!passed)
265             System.out.println("FAIL -- no error on execute of closed prepared statement");
266
267         return passed;
268     }
269
270
271     static boolean testResultSet(Connection JavaDoc conn) throws SQLException JavaDoc {
272         PreparedStatement JavaDoc ps;
273         Statement JavaDoc s;
274         ResultSet JavaDoc rs;
275         boolean passed = true;
276
277         // first, get a few values into a table:
278
ps = conn.prepareStatement("insert into s values (1)");
279         ps.execute();
280         ps.execute();
281         ps.execute();
282         ps.execute();
283         ps.execute();
284         ps.close();
285
286         s = conn.createStatement();
287         rs = s.executeQuery("select * from s");
288
289         rs.next();
290         rs.next();
291         rs.close();
292
293         try {
294             passed = false; // won't pass unless caught
295
rs.next();
296         } catch (SQLException JavaDoc se) {
297             passed = true;
298             // could verify exception #...
299
JDBCDisplayUtil.ShowSQLException(System.out,se);
300         }
301         if (!passed)
302             System.out.println("FAIL -- no error on next of closed result set");
303
304         // now see that rs after statement closed is closed also
305
rs = s.executeQuery("select * from s");
306
307         rs.next();
308         rs.next();
309         s.close();
310
311         try {
312             passed = false; // won't pass unless caught
313
rs.next();
314         } catch (SQLException JavaDoc se) {
315             passed = true;
316             // could verify exception #...
317
JDBCDisplayUtil.ShowSQLException(System.out,se);
318         }
319         if (!passed)
320             System.out.println("FAIL -- no error on next of result set with closed statement");
321
322         return passed;
323     }
324
325     static boolean testConnection(Connection JavaDoc conn) throws SQLException JavaDoc {
326         DatabaseMetaData JavaDoc dmd;
327         ResultSet JavaDoc rs;
328         Statement JavaDoc s;
329         PreparedStatement JavaDoc ps;
330         boolean passed = true;
331
332         dmd = conn.getMetaData();
333         s = conn.createStatement();
334         ps = conn.prepareStatement("create table w (i int)");
335
336         rs = dmd.getTables("%","%","%",null); // should work
337

338         conn.close();
339
340         // should not be able to execute an existing statement
341
try {
342             passed = false; // won't pass unless caught
343
s.execute("create table x (i int)");
344         } catch (SQLException JavaDoc se) {
345             passed = true;
346             // could verify exception #...
347
JDBCDisplayUtil.ShowSQLException(System.out,se);
348         }
349         if (!passed)
350             System.out.println("FAIL -- no error on statement execute after connection close");
351
352         // should not be able to execute an existing prepared statement
353
try {
354             passed = false; // won't pass unless caught
355
ps.execute();
356         } catch (SQLException JavaDoc se) {
357             passed = true;
358             // could verify exception #...
359
JDBCDisplayUtil.ShowSQLException(System.out,se);
360         }
361         if (!passed)
362             System.out.println("FAIL -- no error on prepared statement execute after connection close");
363
364         // should not be able to create a statement...
365
try {
366             passed = false; // won't pass unless caught
367
s = conn.createStatement();
368         } catch (SQLException JavaDoc se) {
369             passed = true;
370             // could verify exception #...
371
JDBCDisplayUtil.ShowSQLException(System.out,se);
372         }
373         if (!passed)
374             System.out.println("FAIL -- no error on statement creation after connection close");
375
376         // should not be able to prepare a statement...
377
try {
378             passed = false; // won't pass unless caught
379
ps = conn.prepareStatement("create table z (i int)");
380         } catch (SQLException JavaDoc se) {
381             passed = true;
382             // could verify exception #...
383
JDBCDisplayUtil.ShowSQLException(System.out,se);
384         }
385         if (!passed)
386             System.out.println("FAIL -- no error on statement preparation after connection close");
387
388         // should not be able to see metadata info...
389
try {
390             passed = false; // won't pass unless caught
391
rs.next();
392         } catch (SQLException JavaDoc se) {
393             passed = true;
394             // could verify exception #...
395
JDBCDisplayUtil.ShowSQLException(System.out,se);
396         }
397         if (!passed)
398             System.out.println("FAIL -- no error on metadata reading after connection close");
399
400         // should not be able to get any more metadata info...
401
try {
402             passed = false; // won't pass unless caught
403
rs = dmd.getColumns("%","%","%","%");
404         } catch (SQLException JavaDoc se) {
405             passed = true;
406             // could verify exception #...
407
JDBCDisplayUtil.ShowSQLException(System.out,se);
408         }
409         if (!passed)
410             System.out.println("FAIL -- no error on metadata collecting after connection close");
411
412         // should not be able to get metadata object...
413
try {
414             passed = false; // won't pass unless caught
415
dmd = conn.getMetaData();
416         } catch (SQLException JavaDoc se) {
417             passed = true;
418             // could verify exception #...
419
JDBCDisplayUtil.ShowSQLException(System.out,se);
420         }
421         if (!passed)
422             System.out.println("FAIL -- no error on getting metadata after connection close");
423
424         return passed;
425     }
426
427     static boolean testDerby62(Connection JavaDoc conn) throws SQLException JavaDoc {
428
429         System.out.println("Test case for Derby-62 - serialization error with SQLException");
430         try {
431             conn.createStatement().execute("DROP TABLE APP.DERBY62_DAIN_SUNDSTROM");
432             return false;
433         } catch (SQLException JavaDoc sqle) {
434             boolean passed = true;
435             try {
436                 // ensure we can serialize this exception.
437
java.io.ObjectOutputStream JavaDoc oos = new java.io.ObjectOutputStream JavaDoc(new java.io.ByteArrayOutputStream JavaDoc(1024));
438                 oos.writeObject(sqle);
439                 oos.close();
440             } catch (java.io.IOException JavaDoc ioe)
441             {
442                 System.out.println("IOException " + ioe.getMessage());
443                 passed = false;
444
445             }
446             System.out.println(sqle.getMessage());
447             return passed;
448         }
449     }
450
451 }
452
Popular Tags