KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.sql.Connection JavaDoc;
22 import java.sql.DriverManager JavaDoc;
23 import java.sql.SQLException JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27
28 import org.apache.commons.pool.ObjectPool;
29 import org.apache.commons.pool.impl.GenericKeyedObjectPool;
30 import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
31 import org.apache.commons.pool.impl.GenericObjectPool;
32
33 /**
34  * Tests for a "manually configured", {@link GenericObjectPool}
35  * based {@link PoolingDriver}.
36  * @author Rodney Waldhoff
37  * @author Sean C. Sullivan
38  * @version $Revision: 1.20 $ $Date: 2004/05/20 17:54:50 $
39  */

40 public class TestManual extends TestConnectionPool {
41     public TestManual(String JavaDoc testName) {
42         super(testName);
43     }
44
45     public static Test suite() {
46         return new TestSuite(TestManual.class);
47     }
48
49     protected Connection JavaDoc getConnection() throws Exception JavaDoc {
50         return DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
51     }
52
53     private PoolingDriver driver = null;
54     
55     public void setUp() throws Exception JavaDoc {
56         super.setUp();
57         GenericObjectPool pool = new GenericObjectPool(null, getMaxActive(), GenericObjectPool.WHEN_EXHAUSTED_BLOCK, getMaxWait(), 10, true, true, 10000L, 5, 5000L, true);
58         DriverConnectionFactory cf = new DriverConnectionFactory(new TesterDriver(),"jdbc:apache:commons:testdriver",null);
59         GenericKeyedObjectPoolFactory opf = new GenericKeyedObjectPoolFactory(null, 10, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, 2000L, 10, true, true, 10000L, 5, 5000L, true);
60         PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, pool, opf, "SELECT COUNT(*) FROM DUAL", false, true);
61         assertNotNull(pcf);
62         driver = new PoolingDriver();
63         driver.registerPool("test",pool);
64         PoolingDriver.setAccessToUnderlyingConnectionAllowed(true);
65         DriverManager.registerDriver(driver);
66     }
67
68     public void tearDown() throws Exception JavaDoc {
69         driver.closePool("test");
70         DriverManager.deregisterDriver(driver);
71     }
72
73     /** @see http://issues.apache.org/bugzilla/show_bug.cgi?id=28912 */
74     public void testReportedBug28912() throws Exception JavaDoc {
75         Connection JavaDoc conn1 = getConnection();
76         assertNotNull(conn1);
77         conn1.close();
78         
79         Connection JavaDoc conn2 = getConnection();
80         assertNotNull(conn2);
81         
82         try {
83             conn1.close();
84             fail("Expected SQLException");
85         }
86         catch (SQLException JavaDoc e) { }
87     }
88     
89     /** @see http://issues.apache.org/bugzilla/show_bug.cgi?id=12400 */
90     public void testReportedBug12400() throws Exception JavaDoc {
91         ObjectPool connectionPool = new GenericObjectPool(
92             null,
93             70,
94             GenericObjectPool.WHEN_EXHAUSTED_BLOCK,
95             60000,
96             10);
97         ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
98             "jdbc:apache:commons:testdriver",
99             "username",
100             "password");
101         PoolableConnectionFactory poolableConnectionFactory =
102             new PoolableConnectionFactory(
103                 connectionFactory,
104                 connectionPool,
105                 null,
106                 null,
107                 false,
108                 true);
109         assertNotNull(poolableConnectionFactory);
110         PoolingDriver driver = new PoolingDriver();
111         driver.registerPool("neusoftim",connectionPool);
112         Connection JavaDoc[] conn = new Connection JavaDoc[25];
113         for(int i=0;i<25;i++) {
114             conn[i] = DriverManager.getConnection("jdbc:apache:commons:dbcp:neusoftim");
115             for(int j=0;j<i;j++) {
116                 assertTrue(conn[j] != conn[i]);
117                 assertTrue(!conn[j].equals(conn[i]));
118             }
119         }
120         for(int i=0;i<25;i++) {
121             conn[i].close();
122         }
123     }
124     
125     public void testClosePool() throws Exception JavaDoc {
126         Connection JavaDoc conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
127         assertNotNull(conn);
128         conn.close();
129         
130         PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
131         driver.closePool("test");
132
133         try {
134             conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
135             fail("expected SQLException");
136         }
137         catch (SQLException JavaDoc e) {
138             // OK
139
}
140     }
141     
142     public void testLogWriter() throws Exception JavaDoc {
143         PrintStream JavaDoc ps = System.out;
144         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(System.err);
145         SQLException JavaDoc ex;
146         
147         DriverManager.setLogWriter(pw);
148         ex = new SQLNestedException("A", new Exception JavaDoc("a"));
149         ex.printStackTrace();
150         ex.printStackTrace(ps);
151         ex.printStackTrace(pw);
152         ex = new SQLNestedException("B", null);
153         ex.printStackTrace();
154         ex.printStackTrace(ps);
155         ex.printStackTrace(pw);
156         ex = new SQLNestedException(null, new Exception JavaDoc("c"));
157         ex.printStackTrace();
158         ex.printStackTrace(ps);
159         ex.printStackTrace(pw);
160         ex = new SQLNestedException(null, null);
161         ex.printStackTrace();
162         ex.printStackTrace(ps);
163         ex.printStackTrace(pw);
164
165         DriverManager.setLogWriter(null);
166         ex = new SQLNestedException("A", new Exception JavaDoc("a"));
167         ex.printStackTrace();
168         ex.printStackTrace(ps);
169         ex.printStackTrace(pw);
170         ex = new SQLNestedException("B", null);
171         ex.printStackTrace();
172         ex.printStackTrace(ps);
173         ex.printStackTrace(pw);
174         ex = new SQLNestedException(null, new Exception JavaDoc("c"));
175         ex.printStackTrace();
176         ex.printStackTrace(ps);
177         ex.printStackTrace(pw);
178         ex = new SQLNestedException(null, null);
179         ex.printStackTrace();
180         ex.printStackTrace(ps);
181         ex.printStackTrace(pw);
182     }
183 }
184
Popular Tags