001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.commons.dbcp.managed;
019
020 import java.sql.SQLException;
021 import org.apache.commons.dbcp.BasicDataSource;
022 import org.apache.commons.dbcp.TestBasicDataSource;
023 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
024 import junit.framework.Test;
025 import junit.framework.TestSuite;
026
027 /**
028 * TestSuite for BasicManagedDataSource
029 */
030 public class TestBasicManagedDataSource extends TestBasicDataSource {
031 public TestBasicManagedDataSource(String testName) {
032 super(testName);
033 }
034
035 public static Test suite() {
036 return new TestSuite(TestBasicManagedDataSource.class);
037 }
038
039 protected BasicDataSource createDataSource() throws Exception {
040 BasicManagedDataSource basicManagedDataSource = new BasicManagedDataSource();
041 basicManagedDataSource.setTransactionManager(new TransactionManagerImpl());
042 return basicManagedDataSource;
043 }
044
045 public void testHashCode() throws Exception {
046 // TODO reenable... hashcode doesn't work when accessToUnderlyingConnectionAllowed is false
047 }
048
049 /**
050 * JIRA: DBCP-294
051 * Verify that PoolableConnections created by BasicManagedDataSource unregister themselves
052 * when reallyClosed.
053 */
054 public void testReallyClose() throws Exception {
055 BasicManagedDataSource basicManagedDataSource = new BasicManagedDataSource();
056 basicManagedDataSource.setTransactionManager(new TransactionManagerImpl());
057 basicManagedDataSource.setDriverClassName("org.apache.commons.dbcp.TesterDriver");
058 basicManagedDataSource.setUrl("jdbc:apache:commons:testdriver");
059 basicManagedDataSource.setUsername("username");
060 basicManagedDataSource.setPassword("password");
061 basicManagedDataSource.setMaxIdle(1);
062 // Create two connections
063 ManagedConnection conn = (ManagedConnection) basicManagedDataSource.getConnection();
064 assertNotNull(basicManagedDataSource.getTransactionRegistry().getXAResource(conn));
065 ManagedConnection conn2 = (ManagedConnection) basicManagedDataSource.getConnection();
066 conn2.close(); // Return one connection to the pool
067 conn.close(); // No room at the inn - this will trigger reallyClose(), which should unregister
068 try {
069 basicManagedDataSource.getTransactionRegistry().getXAResource(conn);
070 fail("Expecting SQLException - XAResources orphaned");
071 } catch (SQLException ex) {
072 // expected
073 }
074 conn2.close();
075 basicManagedDataSource.close();
076 }
077 }