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.IOException;
021: import java.sql.Connection;
022: import java.sql.SQLException;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: /**
028: * TestSuite for BasicDataSource with abandoned connection trace enabled
029: *
030: * @author Dirk Verbeeck
031: * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
032: */
033: public class TestAbandonedBasicDataSource extends TestBasicDataSource {
034: public TestAbandonedBasicDataSource(String testName) {
035: super (testName);
036: }
037:
038: public static Test suite() {
039: return new TestSuite(TestAbandonedBasicDataSource.class);
040: }
041:
042: public void setUp() throws Exception {
043: super .setUp();
044:
045: // abandoned enabled but should not affect the basic tests
046: // (very high timeout)
047: ds.setLogAbandoned(true);
048: ds.setRemoveAbandoned(true);
049: ds.setRemoveAbandonedTimeout(10000);
050: }
051:
052: public void tearDown() throws Exception {
053: super .tearDown();
054: // nothing to do here
055: }
056:
057: // ---------- Abandoned Test -----------
058:
059: private void getConnection1() throws Exception {
060: System.err.println("BEGIN getConnection1()");
061: Connection conn = ds.getConnection();
062: System.err.println("conn: " + conn);
063: System.err.println("END getConnection1()");
064: }
065:
066: private void getConnection2() throws Exception {
067: System.err.println("BEGIN getConnection2()");
068: Connection conn = ds.getConnection();
069: System.err.println("conn: " + conn);
070: System.err.println("END getConnection2()");
071: }
072:
073: private void getConnection3() throws Exception {
074: System.err.println("BEGIN getConnection3()");
075: Connection conn = ds.getConnection();
076: System.err.println("conn: " + conn);
077: System.err.println("END getConnection3()");
078: }
079:
080: public void testAbandoned() throws Exception {
081: // force abandoned
082: ds.setRemoveAbandonedTimeout(0);
083: ds.setMaxActive(1);
084:
085: System.err.println("----------------------------------------");
086: getConnection1();
087: getConnection2();
088: getConnection3();
089: System.err.println("----------------------------------------");
090: }
091:
092: public void testAbandonedClose() throws Exception {
093: // force abandoned
094: ds.setRemoveAbandonedTimeout(0);
095: ds.setMaxActive(1);
096:
097: Connection conn1 = getConnection();
098: assertNotNull(conn1);
099: assertEquals(1, ds.getNumActive());
100:
101: Connection conn2 = getConnection();
102: assertNotNull(conn2);
103: assertEquals(1, ds.getNumActive());
104:
105: try {
106: conn2.close();
107: } catch (SQLException ex) {
108: }
109: assertEquals(0, ds.getNumActive());
110:
111: try {
112: conn1.close();
113: } catch (SQLException ex) {
114: }
115: assertEquals(0, ds.getNumActive());
116: }
117:
118: public void testAbandonedCloseWithExceptions() throws Exception {
119: // force abandoned
120: ds.setRemoveAbandonedTimeout(0);
121: ds.setMaxActive(1);
122: ds.setAccessToUnderlyingConnectionAllowed(true);
123:
124: Connection conn1 = getConnection();
125: assertNotNull(conn1);
126: assertEquals(1, ds.getNumActive());
127:
128: Connection conn2 = getConnection();
129: assertNotNull(conn2);
130: assertEquals(1, ds.getNumActive());
131:
132: // set an IO failure causing the isClosed mathod to fail
133: TesterConnection tconn1 = (TesterConnection) ((DelegatingConnection) conn1)
134: .getInnermostDelegate();
135: tconn1.setFailure(new IOException("network error"));
136: TesterConnection tconn2 = (TesterConnection) ((DelegatingConnection) conn2)
137: .getInnermostDelegate();
138: tconn2.setFailure(new IOException("network error"));
139:
140: try {
141: conn2.close();
142: } catch (SQLException ex) {
143: }
144: assertEquals(0, ds.getNumActive());
145:
146: try {
147: conn1.close();
148: } catch (SQLException ex) {
149: }
150: assertEquals(0, ds.getNumActive());
151: }
152: }
|