KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > TestAbandonedBasicDataSource


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.dbcp;
18
19 import java.io.IOException JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 /**
27  * TestSuite for BasicDataSource with abandoned connection trace enabled
28  *
29  * @author Dirk Verbeeck
30  * @version $Revision: 1.9 $ $Date: 2004/05/01 12:56:26 $
31  */

32 public class TestAbandonedBasicDataSource extends TestBasicDataSource {
33     public TestAbandonedBasicDataSource(String JavaDoc testName) {
34         super(testName);
35     }
36
37     public static Test suite() {
38         return new TestSuite(TestAbandonedBasicDataSource.class);
39     }
40
41     public void setUp() throws Exception JavaDoc {
42         super.setUp();
43
44         // abandoned enabled but should not affect the basic tests
45
// (very high timeout)
46
ds.setLogAbandoned(true);
47         ds.setRemoveAbandoned(true);
48         ds.setRemoveAbandonedTimeout(10000);
49     }
50
51     public void tearDown() throws Exception JavaDoc {
52         super.tearDown();
53         // nothing to do here
54
}
55
56     // ---------- Abandoned Test -----------
57

58     private void getConnection1() throws Exception JavaDoc {
59         System.err.println("BEGIN getConnection1()");
60         Connection JavaDoc conn = ds.getConnection();
61         System.err.println("conn: " + conn);
62         System.err.println("END getConnection1()");
63     }
64
65     private void getConnection2() throws Exception JavaDoc {
66         System.err.println("BEGIN getConnection2()");
67         Connection JavaDoc conn = ds.getConnection();
68         System.err.println("conn: " + conn);
69         System.err.println("END getConnection2()");
70     }
71
72     private void getConnection3() throws Exception JavaDoc {
73         System.err.println("BEGIN getConnection3()");
74         Connection JavaDoc conn = ds.getConnection();
75         System.err.println("conn: " + conn);
76         System.err.println("END getConnection3()");
77     }
78
79     public void testAbandoned() throws Exception JavaDoc {
80         // force abandoned
81
ds.setRemoveAbandonedTimeout(0);
82         ds.setMaxActive(1);
83
84         System.err.println("----------------------------------------");
85         getConnection1();
86         getConnection2();
87         getConnection3();
88         System.err.println("----------------------------------------");
89     }
90     
91     public void testAbandonedClose() throws Exception JavaDoc {
92         // force abandoned
93
ds.setRemoveAbandonedTimeout(0);
94         ds.setMaxActive(1);
95
96         Connection JavaDoc conn1 = getConnection();
97         assertNotNull(conn1);
98         assertEquals(1, ds.getNumActive());
99         
100         Connection JavaDoc conn2 = getConnection();
101         assertNotNull(conn2);
102         assertEquals(1, ds.getNumActive());
103         
104         try { conn2.close(); } catch (SQLException JavaDoc ex) { }
105         assertEquals(0, ds.getNumActive());
106         
107         try { conn1.close(); } catch (SQLException JavaDoc ex) { }
108         assertEquals(0, ds.getNumActive());
109     }
110
111     public void testAbandonedCloseWithExceptions() throws Exception JavaDoc {
112         // force abandoned
113
ds.setRemoveAbandonedTimeout(0);
114         ds.setMaxActive(1);
115         ds.setAccessToUnderlyingConnectionAllowed(true);
116
117         Connection JavaDoc conn1 = getConnection();
118         assertNotNull(conn1);
119         assertEquals(1, ds.getNumActive());
120         
121         Connection JavaDoc conn2 = getConnection();
122         assertNotNull(conn2);
123         assertEquals(1, ds.getNumActive());
124         
125         // set an IO failure causing the isClosed mathod to fail
126
TesterConnection tconn1 = (TesterConnection) ((DelegatingConnection)conn1).getInnermostDelegate();
127         tconn1.setFailure(new IOException JavaDoc("network error"));
128         TesterConnection tconn2 = (TesterConnection) ((DelegatingConnection)conn2).getInnermostDelegate();
129         tconn2.setFailure(new IOException JavaDoc("network error"));
130         
131         try { conn2.close(); } catch (SQLException JavaDoc ex) { }
132         assertEquals(0, ds.getNumActive());
133         
134         try { conn1.close(); } catch (SQLException JavaDoc ex) { }
135         assertEquals(0, ds.getNumActive());
136     }
137 }
138
Popular Tags