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 java.sql.CallableStatement;
009: import java.sql.Connection;
010: import java.sql.DriverManager;
011: import java.sql.PreparedStatement;
012: import java.sql.Statement;
013: import java.util.Properties;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017:
018: /**
019: * Test whether ProxyStatement works
020: *
021: * @version $Revision: 1.13 $, $Date: 2006/03/03 09:58:26 $
022: * @author bill
023: * @author $Author: billhorsman $ (current maintainer)
024: * @since Proxool 0.8
025: */
026: public class ProxyStatementTest extends AbstractProxoolTest {
027:
028: private static final Log LOG = LogFactory
029: .getLog(ProxyStatementTest.class);
030:
031: public ProxyStatementTest(String alias) {
032: super (alias);
033: }
034:
035: /**
036: * That we can get the delegate driver's Statement from the one given by Proxool
037: */
038: public void testDelegateStatement() throws Exception {
039: String testName = "delegateStatement";
040:
041: // Register pool
042: String url = registerPool(testName);
043:
044: // get a connection from the pool and create a statement with it
045: Connection c = DriverManager.getConnection(url);
046: Statement s = c.createStatement();
047: Statement delegateStatement = ProxoolFacade
048: .getDelegateStatement(s);
049: Class delegateStatementClass = delegateStatement.getClass();
050: s.close();
051:
052: LOG.debug("Statement " + s.getClass() + " is delegating to "
053: + delegateStatementClass);
054:
055: // get a *real* connection directly from the native driver (bypassing the pool)
056: Connection realConnection = TestHelper.getDirectConnection();
057: Statement realStatement = realConnection.createStatement();
058: Class realStatementClass = realStatement.getClass();
059:
060: realStatement.close();
061: realConnection.close();
062:
063: // are they of the same type ?
064: assertEquals("Delegate statement isn't of the expected type.",
065: realStatementClass, delegateStatementClass);
066: }
067:
068: /**
069: * Test what interfaces are supported when getting a PreparedStatement
070: */
071: public void testPreparedStatement() throws Exception {
072:
073: String testName = "preparedStatement";
074:
075: // Register pool
076: String url = registerPool(testName);
077:
078: // get a connection from the pool and create a prepare statement with it
079: Connection c = DriverManager.getConnection(url);
080: PreparedStatement s = c
081: .prepareStatement(TestConstants.HYPERSONIC_TEST_SQL);
082: Statement delegateStatement = ProxoolFacade
083: .getDelegateStatement(s);
084: Class delegateStatementClass = delegateStatement.getClass();
085: s.close();
086: c.close();
087:
088: LOG.debug("Statement " + s.getClass() + " is delegating to "
089: + delegateStatementClass);
090:
091: // get a *real* connection directly from the native driver (bypassing the pool)
092: Connection realConnection = TestHelper.getDirectConnection();
093: Statement realStatement = realConnection
094: .prepareStatement(TestConstants.HYPERSONIC_TEST_SQL);
095: Class realStatementClass = realStatement.getClass();
096:
097: realStatement.close();
098: realConnection.close();
099:
100: // are they of the same type ?
101: assertEquals("Delegate statement isn't of the expected type.",
102: realStatementClass, delegateStatementClass);
103: }
104:
105: /**
106: * Test what interfaces are supported when getting a CallableStatement
107: */
108: public void testCallableStatement() throws Exception {
109:
110: String testName = "callableStatement";
111:
112: // Register pool
113: String url = registerPool(testName);
114:
115: // get a connection from the pool and create a callable statement with it
116: Connection c = DriverManager.getConnection(url);
117: CallableStatement s = c
118: .prepareCall(TestConstants.HYPERSONIC_TEST_SQL);
119: Statement delegateStatement = ProxoolFacade
120: .getDelegateStatement(s);
121: Class delegateStatementClass = delegateStatement.getClass();
122: s.close();
123:
124: LOG.debug("Statement " + s.getClass() + " is delegating to "
125: + delegateStatementClass);
126:
127: // get a *real* connection directly from the native driver (bypassing the pool)
128: Connection realConnection = TestHelper.getDirectConnection();
129: Statement realStatement = realConnection
130: .prepareCall(TestConstants.HYPERSONIC_TEST_SQL);
131: Class realStatementClass = realStatement.getClass();
132:
133: realStatement.close();
134: realConnection.close();
135:
136: // are they of the same type ?
137: assertEquals("Delegate statement isn't of the expected type.",
138: realStatementClass, delegateStatementClass);
139: }
140:
141: /**
142: * That we can get the delegate driver's Connection from the one given by Proxool
143: */
144: public void testDelegateConnection() throws Exception {
145:
146: String testName = "delegateConnection";
147:
148: // Register pool
149: String url = registerPool(testName);
150:
151: // get a connection from the pool
152: Connection c = DriverManager.getConnection(url);
153: Connection delegateConnection = ProxoolFacade
154: .getDelegateConnection(c);
155: Class delegateConnectionClass = delegateConnection.getClass();
156:
157: LOG.debug("Connection " + c + " is delegating to "
158: + delegateConnectionClass);
159: c.close();
160:
161: // get a *real* connection directly from the native driver (bypassing the pool)
162: Connection realConnection = TestHelper.getDirectConnection();
163: Class realConnectionClass = realConnection.getClass();
164: realConnection.close();
165:
166: assertEquals("Connection isn't of the expected.",
167: realConnectionClass, delegateConnectionClass);
168: }
169:
170: /**
171: *
172: * @param alias
173: * @throws Exception
174: */
175: private String registerPool(String alias) throws Exception {
176: String url = TestHelper.buildProxoolUrl(alias,
177: TestConstants.HYPERSONIC_DRIVER,
178: TestConstants.HYPERSONIC_TEST_URL);
179: Properties info = new Properties();
180: info.setProperty(ProxoolConstants.USER_PROPERTY,
181: TestConstants.HYPERSONIC_USER);
182: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
183: TestConstants.HYPERSONIC_PASSWORD);
184: ProxoolFacade.registerConnectionPool(url, info);
185:
186: return url;
187: }
188:
189: public void testCloseByStatement() throws Exception {
190:
191: String testName = "snapshot";
192: final String alias = testName;
193: String url = TestHelper.buildProxoolUrl(alias,
194: TestConstants.HYPERSONIC_DRIVER,
195: TestConstants.HYPERSONIC_TEST_URL);
196: Properties info = new Properties();
197: info.setProperty(ProxoolConstants.USER_PROPERTY,
198: TestConstants.HYPERSONIC_USER);
199: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
200: TestConstants.HYPERSONIC_PASSWORD);
201: ProxoolFacade.registerConnectionPool(url, info);
202:
203: // get a connection from the pool and create a statement with it
204: Connection c = DriverManager.getConnection(url);
205: Statement s = c.createStatement();
206: // Even if we ask the statement to close it it should still work.
207: s.getConnection().close();
208: assertEquals("servedCount", 0, ProxoolFacade.getSnapshot(alias)
209: .getActiveConnectionCount());
210:
211: }
212: }
213:
214: /*
215: Revision history:
216: $Log: ProxyStatementTest.java,v $
217: Revision 1.13 2006/03/03 09:58:26 billhorsman
218: Fix for statement.getConnection(). See bug 1149834.
219:
220: Revision 1.12 2006/01/18 14:40:06 billhorsman
221: Unbundled Jakarta's Commons Logging.
222:
223: Revision 1.11 2004/07/13 21:32:41 billhorsman
224: Close the first connection first before opening the real connection (directly) otherwise you get a "database already in use"error on Windows.
225:
226: Revision 1.10 2004/05/26 17:19:09 brenuart
227: Allow JUnit tests to be executed against another database.
228: By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
229: This behavior can be overriden by setting the 'testConfig' environment property to another location.
230:
231: Revision 1.9 2004/03/23 21:17:23 billhorsman
232: added preparedStatement and callableStatement tests
233:
234: Revision 1.8 2003/12/09 18:52:19 billhorsman
235: checkstyle
236:
237: Revision 1.7 2003/11/05 00:00:52 billhorsman
238: Remove redundant test (already in FatalSqlExceptionTest)
239:
240: Revision 1.6 2003/08/27 18:03:20 billhorsman
241: added new getDelegateConnection() method
242:
243: Revision 1.5 2003/03/04 10:24:40 billhorsman
244: removed try blocks around each test
245:
246: Revision 1.4 2003/03/03 17:09:05 billhorsman
247: all tests now extend AbstractProxoolTest
248:
249: Revision 1.3 2003/03/03 11:12:05 billhorsman
250: fixed licence
251:
252: Revision 1.2 2003/03/01 15:27:24 billhorsman
253: checkstyle
254:
255: Revision 1.1 2003/02/27 18:01:48 billhorsman
256: completely rethought the test structure. it's now
257: more obvious. no new tests yet though.
258:
259: */
|