001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: import java.sql.Connection;
012: import java.sql.DriverManager;
013: import java.util.Properties;
014:
015: /**
016: * Test whether the {@link ConnectionResetter} works.
017: *
018: * @version $Revision: 1.16 $, $Date: 2006/01/18 14:40:06 $
019: * @author Bill Horsman (bill@logicalcobwebs.co.uk)
020: * @author $Author: billhorsman $ (current maintainer)
021: * @since Proxool 0.5
022: */
023: public class ConnectionResetterTest extends AbstractProxoolTest {
024:
025: private static final Log LOG = LogFactory
026: .getLog(ConnectionResetterTest.class);
027:
028: /**
029: * @see junit.framework.TestCase#TestCase
030: */
031: public ConnectionResetterTest(String s) {
032: super (s);
033: }
034:
035: /**
036: * Test whether autoCommit is correctly reset when a connection is
037: * returned to the pool.
038: */
039: public void testAutoCommit() throws Exception {
040:
041: String testName = "autoCommit";
042: String alias = testName;
043:
044: String url = TestHelper.buildProxoolUrl(alias,
045: TestConstants.HYPERSONIC_DRIVER,
046: TestConstants.HYPERSONIC_TEST_URL);
047: Properties info = new Properties();
048: info.setProperty(ProxoolConstants.USER_PROPERTY,
049: TestConstants.HYPERSONIC_USER);
050: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
051: TestConstants.HYPERSONIC_PASSWORD);
052: ProxoolFacade.registerConnectionPool(url, info);
053:
054: Connection c1 = DriverManager.getConnection(url);
055:
056: Connection c2 = DriverManager.getConnection(url);
057:
058: c1.setAutoCommit(false);
059: c1.close();
060:
061: c1 = DriverManager.getConnection(url);
062: assertTrue("c1.getAutoCommit", c1.getAutoCommit());
063:
064: c2.close();
065: c1.close();
066: assertEquals("connectionCount", 2, ProxoolFacade.getSnapshot(
067: alias).getConnectionCount());
068:
069: }
070:
071: /**
072: * Test connectionCount when we deliberately introduce an exception during connection reset.
073: */
074: public void testFailedReset() throws Exception {
075:
076: try {
077: // This is a bit of a hack to force an exception during reset
078: ConnectionResetter.setTriggerResetException(true);
079:
080: String testName = "failedReset";
081: String alias = testName;
082:
083: String url = TestHelper.buildProxoolUrl(alias,
084: TestConstants.HYPERSONIC_DRIVER,
085: TestConstants.HYPERSONIC_TEST_URL);
086: Properties info = new Properties();
087: info.setProperty(ProxoolConstants.USER_PROPERTY,
088: TestConstants.HYPERSONIC_USER);
089: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
090: TestConstants.HYPERSONIC_PASSWORD);
091: ProxoolFacade.registerConnectionPool(url, info);
092:
093: Connection c1 = DriverManager.getConnection(url);
094: c1.setAutoCommit(false);
095: c1.close();
096:
097: assertEquals("connectionCount", 0, ProxoolFacade
098: .getSnapshot(alias).getConnectionCount());
099: } finally {
100: // Back to normal
101: ConnectionResetter.setTriggerResetException(false);
102: }
103:
104: }
105:
106: /**
107: * Test whether autoCommit is correctly reset when a connection is
108: * returned to the pool.
109: */
110: public void testReadOnly() throws Exception {
111:
112: String testName = "readOnly";
113: String alias = testName;
114:
115: String url = TestHelper.buildProxoolUrl(alias,
116: TestConstants.HYPERSONIC_DRIVER,
117: TestConstants.HYPERSONIC_TEST_URL);
118: Properties info = new Properties();
119: info.setProperty(ProxoolConstants.USER_PROPERTY,
120: TestConstants.HYPERSONIC_USER);
121: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
122: TestConstants.HYPERSONIC_PASSWORD);
123: info
124: .setProperty(
125: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
126: "2");
127: ProxoolFacade.registerConnectionPool(url, info);
128:
129: Connection c1 = DriverManager.getConnection(url);
130: ;
131: Connection c2 = DriverManager.getConnection(url);
132: ;
133:
134: boolean originalReadOnly = c1.isReadOnly();
135: c1.setReadOnly(true);
136: c1.close();
137:
138: c1 = DriverManager.getConnection(url);
139: ;
140: assertTrue("readOnly", c1.isReadOnly() == originalReadOnly);
141:
142: c2.close();
143: c1.close();
144:
145: }
146:
147: }
148:
149: /*
150: Revision history:
151: $Log: ConnectionResetterTest.java,v $
152: Revision 1.16 2006/01/18 14:40:06 billhorsman
153: Unbundled Jakarta's Commons Logging.
154:
155: Revision 1.15 2005/10/07 08:12:58 billhorsman
156: Test deliberate exception durinng reset
157:
158: Revision 1.14 2003/03/04 10:58:43 billhorsman
159: checkstyle
160:
161: Revision 1.13 2003/03/04 10:24:40 billhorsman
162: removed try blocks around each test
163:
164: Revision 1.12 2003/03/03 17:08:56 billhorsman
165: all tests now extend AbstractProxoolTest
166:
167: Revision 1.11 2003/03/03 11:12:04 billhorsman
168: fixed licence
169:
170: Revision 1.10 2003/02/27 18:01:47 billhorsman
171: completely rethought the test structure. it's now
172: more obvious. no new tests yet though.
173:
174: Revision 1.9 2003/02/19 15:14:22 billhorsman
175: fixed copyright (copy and paste error,
176: not copyright change)
177:
178: Revision 1.8 2003/02/06 17:41:02 billhorsman
179: now uses imported logging
180:
181: Revision 1.7 2002/12/16 17:05:38 billhorsman
182: new test structure
183:
184: Revision 1.6 2002/12/03 10:53:08 billhorsman
185: checkstyle
186:
187: Revision 1.5 2002/11/13 20:53:30 billhorsman
188: new tests for autoCommit and readOnly
189:
190: Revision 1.4 2002/11/12 20:24:12 billhorsman
191: checkstyle
192:
193: Revision 1.3 2002/11/12 20:18:26 billhorsman
194: Made connection resetter a bit more friendly. Now, if it encounters any problems during
195: reset then that connection is thrown away. This is going to cause you problems if you
196: always close connections in an unstable state (e.g. with transactions open. But then
197: again, it's better to know about that as soon as possible, right?
198:
199: Revision 1.2 2002/11/09 16:01:21 billhorsman
200: fixed CommandFilterIF implementation
201:
202: Revision 1.1 2002/11/06 21:08:02 billhorsman
203: new ConnectionResetter test
204:
205: */
|