KickJava   Java API By Example, From Geeks To Geeks.

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


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
28  *
29  * @author Dirk Verbeeck
30  * @version $Revision: 1.21 $ $Date: 2004/05/20 13:08:32 $
31  */

32 public class TestBasicDataSource extends TestConnectionPool {
33     public TestBasicDataSource(String JavaDoc testName) {
34         super(testName);
35     }
36
37     public static Test suite() {
38         return new TestSuite(TestBasicDataSource.class);
39     }
40
41     protected Connection JavaDoc getConnection() throws Exception JavaDoc {
42         return ds.getConnection();
43     }
44
45     protected BasicDataSource ds = null;
46     private static String JavaDoc CATALOG = "test catalog";
47
48     public void setUp() throws Exception JavaDoc {
49         super.setUp();
50         ds = new BasicDataSource();
51         ds.setDriverClassName("org.apache.commons.dbcp.TesterDriver");
52         ds.setUrl("jdbc:apache:commons:testdriver");
53         ds.setMaxActive(getMaxActive());
54         ds.setMaxWait(getMaxWait());
55         ds.setDefaultAutoCommit(true);
56         ds.setDefaultReadOnly(false);
57         ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
58         ds.setDefaultCatalog(CATALOG);
59         ds.setUsername("username");
60         ds.setPassword("password");
61         ds.setValidationQuery("SELECT DUMMY FROM DUAL");
62     }
63
64     public void tearDown() throws Exception JavaDoc {
65         super.tearDown();
66         ds = null;
67     }
68     
69     public void testTransactionIsolationBehavior() throws Exception JavaDoc {
70         Connection JavaDoc conn = getConnection();
71         assertTrue(conn != null);
72         assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn.getTransactionIsolation());
73         conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
74         conn.close();
75         
76         Connection JavaDoc conn2 = getConnection();
77         assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn2.getTransactionIsolation());
78         
79         Connection JavaDoc conn3 = getConnection();
80         assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn3.getTransactionIsolation());
81
82         conn2.close();
83         
84         conn3.close();
85     }
86
87     public void testPooling() throws Exception JavaDoc {
88         // this also needs access to the undelying connection
89
ds.setAccessToUnderlyingConnectionAllowed(true);
90         super.testPooling();
91     }
92     
93     public void testNoAccessToUnderlyingConnectionAllowed() throws Exception JavaDoc {
94         // default: false
95
assertEquals(false, ds.isAccessToUnderlyingConnectionAllowed());
96         
97         Connection JavaDoc conn = getConnection();
98         Connection JavaDoc dconn = ((DelegatingConnection) conn).getDelegate();
99         assertNull(dconn);
100         
101         dconn = ((DelegatingConnection) conn).getInnermostDelegate();
102         assertNull(dconn);
103     }
104
105     public void testAccessToUnderlyingConnectionAllowed() throws Exception JavaDoc {
106         ds.setAccessToUnderlyingConnectionAllowed(true);
107         assertEquals(true, ds.isAccessToUnderlyingConnectionAllowed());
108         
109         Connection JavaDoc conn = getConnection();
110         Connection JavaDoc dconn = ((DelegatingConnection) conn).getDelegate();
111         assertNotNull(dconn);
112         
113         dconn = ((DelegatingConnection) conn).getInnermostDelegate();
114         assertNotNull(dconn);
115         
116         assertTrue(dconn instanceof TesterConnection);
117     }
118     
119     public void testEmptyValidationQuery() throws Exception JavaDoc {
120         assertNotNull(ds.getValidationQuery());
121         
122         ds.setValidationQuery("");
123         assertNull(ds.getValidationQuery());
124
125         ds.setValidationQuery(" ");
126         assertNull(ds.getValidationQuery());
127     }
128
129     public void testInvalidValidationQuery() {
130         try {
131             ds.setValidationQuery("invalid");
132             ds.getConnection();
133             fail("expected SQLException");
134         }
135         catch (SQLException JavaDoc e) {
136             if (e.toString().indexOf("invalid") < 0) {
137                 fail("expected detailed error message");
138             }
139         }
140     }
141
142     public void testSetValidationTestProperties() {
143         // defaults
144
assertEquals(true, ds.getTestOnBorrow());
145         assertEquals(false, ds.getTestOnReturn());
146         assertEquals(false, ds.getTestWhileIdle());
147
148         ds.setTestOnBorrow(true);
149         ds.setTestOnReturn(true);
150         ds.setTestWhileIdle(true);
151         assertEquals(true, ds.getTestOnBorrow());
152         assertEquals(true, ds.getTestOnReturn());
153         assertEquals(true, ds.getTestWhileIdle());
154
155         ds.setTestOnBorrow(false);
156         ds.setTestOnReturn(false);
157         ds.setTestWhileIdle(false);
158         assertEquals(false, ds.getTestOnBorrow());
159         assertEquals(false, ds.getTestOnReturn());
160         assertEquals(false, ds.getTestWhileIdle());
161     }
162
163     public void testNoValidationQuery() throws Exception JavaDoc {
164         ds.setTestOnBorrow(true);
165         ds.setTestOnReturn(true);
166         ds.setTestWhileIdle(true);
167         ds.setValidationQuery("");
168         
169         Connection JavaDoc conn = ds.getConnection();
170         conn.close();
171
172         assertEquals(false, ds.getTestOnBorrow());
173         assertEquals(false, ds.getTestOnReturn());
174         assertEquals(false, ds.getTestWhileIdle());
175     }
176     
177     public void testDefaultCatalog() throws Exception JavaDoc {
178         Connection JavaDoc[] c = new Connection JavaDoc[getMaxActive()];
179         for (int i = 0; i < c.length; i++) {
180             c[i] = getConnection();
181             assertTrue(c[i] != null);
182             assertEquals(CATALOG, c[i].getCatalog());
183         }
184
185         for (int i = 0; i < c.length; i++) {
186             c[i].setCatalog("error");
187             c[i].close();
188         }
189         
190         for (int i = 0; i < c.length; i++) {
191             c[i] = getConnection();
192             assertTrue(c[i] != null);
193             assertEquals(CATALOG, c[i].getCatalog());
194         }
195
196         for (int i = 0; i < c.length; i++) {
197             c[i].close();
198         }
199     }
200     
201     public void testSetAutoCommitTrueOnClose() throws Exception JavaDoc {
202         ds.setAccessToUnderlyingConnectionAllowed(true);
203         ds.setDefaultAutoCommit(false);
204         
205         Connection JavaDoc conn = getConnection();
206         assertNotNull(conn);
207         assertEquals(false, conn.getAutoCommit());
208
209         Connection JavaDoc dconn = ((DelegatingConnection) conn).getInnermostDelegate();
210         assertNotNull(dconn);
211         assertEquals(false, dconn.getAutoCommit());
212
213         conn.close();
214
215         assertEquals(true, dconn.getAutoCommit());
216     }
217
218     public void testInitialSize() throws Exception JavaDoc {
219         ds.setMaxActive(20);
220         ds.setMaxIdle(20);
221         ds.setInitialSize(10);
222
223         Connection JavaDoc conn = getConnection();
224         assertNotNull(conn);
225         conn.close();
226
227         assertEquals(0, ds.getNumActive());
228         assertEquals(10, ds.getNumIdle());
229     }
230
231     // Bugzilla Bug 28251: Returning dead database connections to BasicDataSource
232
// isClosed() failure blocks returning a connection to the pool
233
public void testIsClosedFailure() throws SQLException JavaDoc {
234         ds.setAccessToUnderlyingConnectionAllowed(true);
235         Connection JavaDoc conn = ds.getConnection();
236         assertNotNull(conn);
237         assertEquals(1, ds.getNumActive());
238         
239         // set an IO failure causing the isClosed mathod to fail
240
TesterConnection tconn = (TesterConnection) ((DelegatingConnection)conn).getInnermostDelegate();
241         tconn.setFailure(new IOException JavaDoc("network error"));
242         
243         try {
244             conn.close();
245             fail("Expected SQLException");
246         }
247         catch(SQLException JavaDoc ex) { }
248         
249         assertEquals(0, ds.getNumActive());
250     }
251     
252     /**
253      * Bugzilla Bug 29054:
254      * The BasicDataSource.setTestOnReturn(boolean) is not carried through to
255      * the GenericObjectPool variable _testOnReturn.
256      */

257     public void testPropertyTestOnReturn() throws Exception JavaDoc {
258         ds.setValidationQuery("select 1 from dual");
259         ds.setTestOnBorrow(false);
260         ds.setTestWhileIdle(false);
261         ds.setTestOnReturn(true);
262         
263         Connection JavaDoc conn = ds.getConnection();
264         assertNotNull(conn);
265         
266         assertEquals(false, ds.connectionPool.getTestOnBorrow());
267         assertEquals(false, ds.connectionPool.getTestWhileIdle());
268         assertEquals(true, ds.connectionPool.getTestOnReturn());
269     }
270     
271     /**
272      * Bugzilla Bug 29055: AutoCommit and ReadOnly
273      * The DaffodilDB driver throws an SQLException if
274      * trying to commit or rollback a readOnly connection.
275      */

276     public void testRollbackReadOnly() throws Exception JavaDoc {
277         ds.setDefaultReadOnly(true);
278         ds.setDefaultAutoCommit(false);
279         
280         Connection JavaDoc conn = ds.getConnection();
281         assertNotNull(conn);
282         conn.close();
283     }
284 }
285
Popular Tags