001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.dbcp;
019:
020: import java.io.PrintStream;
021: import java.io.PrintWriter;
022: import java.sql.Connection;
023: import java.sql.DriverManager;
024: import java.sql.SQLException;
025:
026: import junit.framework.Test;
027: import junit.framework.TestSuite;
028:
029: import org.apache.commons.pool.ObjectPool;
030: import org.apache.commons.pool.impl.GenericKeyedObjectPool;
031: import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
032: import org.apache.commons.pool.impl.GenericObjectPool;
033:
034: /**
035: * Tests for a "manually configured", {@link GenericObjectPool}
036: * based {@link PoolingDriver}.
037: * @author Rodney Waldhoff
038: * @author Sean C. Sullivan
039: * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
040: */
041: public class TestManual extends TestConnectionPool {
042: public TestManual(String testName) {
043: super (testName);
044: }
045:
046: public static Test suite() {
047: return new TestSuite(TestManual.class);
048: }
049:
050: protected Connection getConnection() throws Exception {
051: return DriverManager
052: .getConnection("jdbc:apache:commons:dbcp:test");
053: }
054:
055: private PoolingDriver driver = null;
056:
057: public void setUp() throws Exception {
058: super .setUp();
059: GenericObjectPool pool = new GenericObjectPool(null,
060: getMaxActive(), GenericObjectPool.WHEN_EXHAUSTED_BLOCK,
061: getMaxWait(), 10, true, true, 10000L, 5, 5000L, true);
062: DriverConnectionFactory cf = new DriverConnectionFactory(
063: new TesterDriver(), "jdbc:apache:commons:testdriver",
064: null);
065: GenericKeyedObjectPoolFactory opf = new GenericKeyedObjectPoolFactory(
066: null, 10, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK,
067: 2000L, 10, true, true, 10000L, 5, 5000L, true);
068: PoolableConnectionFactory pcf = new PoolableConnectionFactory(
069: cf, pool, opf, "SELECT COUNT(*) FROM DUAL", false, true);
070: assertNotNull(pcf);
071: driver = new PoolingDriver();
072: driver.registerPool("test", pool);
073: PoolingDriver.setAccessToUnderlyingConnectionAllowed(true);
074: DriverManager.registerDriver(driver);
075: }
076:
077: public void tearDown() throws Exception {
078: driver.closePool("test");
079: DriverManager.deregisterDriver(driver);
080: }
081:
082: public void test1() {
083: GenericObjectPool connectionPool = new GenericObjectPool(null);
084: ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
085: "jdbc:some:connect:string", "username", "password");
086: PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
087: connectionFactory, connectionPool, null, null, false,
088: true);
089: PoolingDataSource dataSource = new PoolingDataSource(
090: connectionPool);
091: }
092:
093: public void test2() {
094: GenericObjectPool connectionPool = new GenericObjectPool(null);
095: ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
096: "jdbc:some:connect:string", "username", "password");
097: PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
098: connectionFactory, connectionPool, null, null, false,
099: true);
100: PoolingDriver driver = new PoolingDriver();
101: driver.registerPool("example", connectionPool);
102: }
103:
104: /** @see http://issues.apache.org/bugzilla/show_bug.cgi?id=28912 */
105: public void testReportedBug28912() throws Exception {
106: Connection conn1 = getConnection();
107: assertNotNull(conn1);
108: conn1.close();
109:
110: Connection conn2 = getConnection();
111: assertNotNull(conn2);
112:
113: try {
114: conn1.close();
115: fail("Expected SQLException");
116: } catch (SQLException e) {
117: }
118: }
119:
120: /** @see http://issues.apache.org/bugzilla/show_bug.cgi?id=12400 */
121: public void testReportedBug12400() throws Exception {
122: ObjectPool connectionPool = new GenericObjectPool(null, 70,
123: GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 60000, 10);
124: ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
125: "jdbc:apache:commons:testdriver", "username",
126: "password");
127: PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
128: connectionFactory, connectionPool, null, null, false,
129: true);
130: assertNotNull(poolableConnectionFactory);
131: PoolingDriver driver = new PoolingDriver();
132: driver.registerPool("neusoftim", connectionPool);
133: Connection[] conn = new Connection[25];
134: for (int i = 0; i < 25; i++) {
135: conn[i] = DriverManager
136: .getConnection("jdbc:apache:commons:dbcp:neusoftim");
137: for (int j = 0; j < i; j++) {
138: assertTrue(conn[j] != conn[i]);
139: assertTrue(!conn[j].equals(conn[i]));
140: }
141: }
142: for (int i = 0; i < 25; i++) {
143: conn[i].close();
144: }
145: }
146:
147: public void testClosePool() throws Exception {
148: Connection conn = DriverManager
149: .getConnection("jdbc:apache:commons:dbcp:test");
150: assertNotNull(conn);
151: conn.close();
152:
153: PoolingDriver driver = (PoolingDriver) DriverManager
154: .getDriver("jdbc:apache:commons:dbcp:");
155: driver.closePool("test");
156:
157: try {
158: conn = DriverManager
159: .getConnection("jdbc:apache:commons:dbcp:test");
160: fail("expected SQLException");
161: } catch (SQLException e) {
162: // OK
163: }
164: }
165:
166: public void testInvalidateConnection() throws Exception {
167: Connection conn = DriverManager
168: .getConnection("jdbc:apache:commons:dbcp:test");
169: assertNotNull(conn);
170:
171: ObjectPool pool = driver.getConnectionPool("test");
172: assertEquals(1, pool.getNumActive());
173: assertEquals(0, pool.getNumIdle());
174:
175: PoolingDriver driver = (PoolingDriver) DriverManager
176: .getDriver("jdbc:apache:commons:dbcp:");
177: driver.invalidateConnection(conn);
178:
179: assertEquals(0, pool.getNumActive());
180: assertEquals(0, pool.getNumIdle());
181: assertTrue(conn.isClosed());
182: }
183:
184: public void testLogWriter() throws Exception {
185: PrintStream ps = System.out;
186: PrintWriter pw = new PrintWriter(System.err);
187: SQLException ex;
188:
189: DriverManager.setLogWriter(pw);
190: ex = new SQLNestedException("A", new Exception("a"));
191: ex.printStackTrace();
192: ex.printStackTrace(ps);
193: ex.printStackTrace(pw);
194: ex = new SQLNestedException("B", null);
195: ex.printStackTrace();
196: ex.printStackTrace(ps);
197: ex.printStackTrace(pw);
198: ex = new SQLNestedException(null, new Exception("c"));
199: ex.printStackTrace();
200: ex.printStackTrace(ps);
201: ex.printStackTrace(pw);
202: ex = new SQLNestedException(null, null);
203: ex.printStackTrace();
204: ex.printStackTrace(ps);
205: ex.printStackTrace(pw);
206:
207: DriverManager.setLogWriter(null);
208: ex = new SQLNestedException("A", new Exception("a"));
209: ex.printStackTrace();
210: ex.printStackTrace(ps);
211: ex.printStackTrace(pw);
212: ex = new SQLNestedException("B", null);
213: ex.printStackTrace();
214: ex.printStackTrace(ps);
215: ex.printStackTrace(pw);
216: ex = new SQLNestedException(null, new Exception("c"));
217: ex.printStackTrace();
218: ex.printStackTrace(ps);
219: ex.printStackTrace(pw);
220: ex = new SQLNestedException(null, null);
221: ex.printStackTrace();
222: ex.printStackTrace(ps);
223: ex.printStackTrace(pw);
224: }
225: }
|