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.sql.Connection;
021: import java.util.Properties;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.pool.ObjectPool;
027: import org.apache.commons.pool.impl.GenericObjectPool;
028:
029: /**
030: * TestSuite for PoolingDataSource
031: *
032: * @version $Revision: 392677 $ $Date: 2006-04-08 21:42:24 -0700 (Sat, 08 Apr 2006) $
033: */
034: public class TestPoolingDataSource extends TestConnectionPool {
035: public TestPoolingDataSource(String testName) {
036: super (testName);
037: }
038:
039: public static Test suite() {
040: return new TestSuite(TestPoolingDataSource.class);
041: }
042:
043: protected Connection getConnection() throws Exception {
044: return ds.getConnection();
045: }
046:
047: protected PoolingDataSource ds = null;
048: private GenericObjectPool pool = null;
049:
050: public void setUp() throws Exception {
051: super .setUp();
052: pool = new GenericObjectPool();
053: pool.setMaxActive(getMaxActive());
054: pool.setMaxWait(getMaxWait());
055: Properties props = new Properties();
056: props.setProperty("user", "username");
057: props.setProperty("password", "password");
058: PoolableConnectionFactory factory = new PoolableConnectionFactory(
059: new DriverConnectionFactory(new TesterDriver(),
060: "jdbc:apache:commons:testdriver", props), pool,
061: null, "SELECT DUMMY FROM DUAL", true, true);
062: pool.setFactory(factory);
063: ds = new PoolingDataSource(pool);
064: ds.setAccessToUnderlyingConnectionAllowed(true);
065: }
066:
067: public void tearDown() throws Exception {
068: pool.close();
069: super .tearDown();
070: }
071:
072: public void testPoolGuardConnectionWrapperEqualsSameDelegate()
073: throws Exception {
074: // Get a maximal set of connections from the pool
075: Connection[] c = new Connection[getMaxActive()];
076: for (int i = 0; i < c.length; i++) {
077: c[i] = newConnection();
078: }
079: // Close the delegate of one wrapper in the pool
080: ((DelegatingConnection) c[0]).getDelegate().close();
081:
082: // Grab a new connection - should get c[0]'s closed connection
083: // so should be delegate-equivalent, so equal
084: Connection con = newConnection();
085: assertTrue(c[0].equals(con));
086: assertTrue(con.equals(c[0]));
087: for (int i = 0; i < c.length; i++) {
088: c[i].close();
089: }
090: }
091:
092: private void checkPoolGuardConnectionWrapperEqualsReflexive()
093: throws Exception {
094: Connection con = ds.getConnection();
095: Connection con2 = con;
096: assertTrue(con2.equals(con));
097: assertTrue(con.equals(con2));
098: con.close();
099: }
100:
101: /*
102: * JIRA: DBCP-198
103: */
104: public void testPoolGuardConnectionWrapperEqualsReflexive()
105: throws Exception {
106: // Statndard setup - using DelegatingConnections
107: // returned from PoolableConnectionFactory
108: checkPoolGuardConnectionWrapperEqualsReflexive();
109: // Force PoolGuardConnectionWrappers to wrap non-Delegating connections
110: pool.close();
111: pool = new GenericObjectPool();
112: pool.setMaxActive(getMaxActive());
113: pool.setMaxWait(getMaxWait());
114: Properties props = new Properties();
115: props.setProperty("user", "username");
116: props.setProperty("password", "password");
117: NonDelegatingPoolableConnectionFactory factory = new NonDelegatingPoolableConnectionFactory(
118: new DriverConnectionFactory(new TesterDriver(),
119: "jdbc:apache:commons:testdriver", props), pool);
120: pool.setFactory(factory);
121: ds = new PoolingDataSource(pool);
122: checkPoolGuardConnectionWrapperEqualsReflexive();
123: }
124:
125: public void testPoolGuardConnectionWrapperEqualsFail()
126: throws Exception {
127: Connection con1 = ds.getConnection();
128: Connection con2 = ds.getConnection();
129: assertFalse(con1.equals(con2));
130: con1.close();
131: con2.close();
132: }
133:
134: public void testPoolGuardConnectionWrapperEqualsNull()
135: throws Exception {
136: Connection con1 = ds.getConnection();
137: Connection con2 = null;
138: assertFalse(con1.equals(con2));
139: con1.close();
140: }
141:
142: public void testPoolGuardConnectionWrapperEqualsType()
143: throws Exception {
144: Connection con1 = ds.getConnection();
145: Integer con2 = new Integer(0);
146: assertFalse(con1.equals(con2));
147: con1.close();
148: }
149:
150: public void testestPoolGuardConnectionWrapperEqualInnermost()
151: throws Exception {
152: ds.setAccessToUnderlyingConnectionAllowed(true);
153: DelegatingConnection con = (DelegatingConnection) ds
154: .getConnection();
155: Connection inner = con.getInnermostDelegate();
156: ds.setAccessToUnderlyingConnectionAllowed(false);
157: DelegatingConnection con2 = new DelegatingConnection(inner);
158: assertTrue(con2.equals(con));
159: assertTrue(con.innermostDelegateEquals(con2
160: .getInnermostDelegate()));
161: assertTrue(con2.innermostDelegateEquals(inner));
162: assertTrue(con.equals(con2));
163: }
164:
165: /** Factory to return non-delegating connections for DBCP-198 test */
166: private class NonDelegatingPoolableConnectionFactory extends
167: PoolableConnectionFactory {
168: public NonDelegatingPoolableConnectionFactory(
169: ConnectionFactory connFactory, ObjectPool pool) {
170: super (connFactory, pool, null, null, true, true);
171: }
172:
173: synchronized public Object makeObject() throws Exception {
174: return _connFactory.createConnection();
175: }
176: }
177: }
|