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.DriverManager;
012: import java.sql.Connection;
013: import java.sql.SQLException;
014: import java.util.Properties;
015:
016: /**
017: * Test that we can update the definition of a pool
018: *
019: * @version $Revision: 1.9 $, $Date: 2006/01/18 14:40:06 $
020: * @author bill
021: * @author $Author: billhorsman $ (current maintainer)
022: * @since Proxool 0.8
023: */
024: public class UpdateDefinitionTest extends AbstractProxoolTest {
025:
026: private static final Log LOG = LogFactory
027: .getLog(UpdateDefinitionTest.class);
028:
029: public UpdateDefinitionTest(String alias) {
030: super (alias);
031: }
032:
033: /**
034: * Can we change the delegate URL of a pool
035: */
036: public void testChangeUrl() throws Exception {
037:
038: String testName = "changeUrl";
039: String alias = testName;
040:
041: String url1 = TestHelper.buildProxoolUrl(alias,
042: TestConstants.HYPERSONIC_DRIVER,
043: TestConstants.HYPERSONIC_TEST_URL);
044:
045: String url2 = TestHelper.buildProxoolUrl(alias,
046: TestConstants.HYPERSONIC_DRIVER,
047: TestConstants.HYPERSONIC_TEST_URL2);
048:
049: Properties info = new Properties();
050: info.setProperty(ProxoolConstants.USER_PROPERTY,
051: TestConstants.HYPERSONIC_USER);
052: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
053: TestConstants.HYPERSONIC_PASSWORD);
054: info
055: .setProperty(
056: ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
057: "2");
058:
059: // register pool
060: ProxoolFacade.registerConnectionPool(url1, info);
061:
062: // Get one connection
063: DriverManager.getConnection(url1).close();
064: assertEquals("connectionsServedCount", 1L, ProxoolFacade
065: .getSnapshot(alias, false).getServedCount());
066:
067: ProxoolFacade.updateConnectionPool(url2, null);
068:
069: // Get another connection
070: DriverManager.getConnection(url2).close();
071: assertEquals("connectionsServedCount", 2L, ProxoolFacade
072: .getSnapshot(alias, false).getServedCount());
073:
074: ProxoolFacade.updateConnectionPool(url1, null);
075: DriverManager.getConnection(url1).close();
076: assertEquals("connectionsServedCount", 3L, ProxoolFacade
077: .getSnapshot(alias, false).getServedCount());
078:
079: }
080:
081: /**
082: * Can we update a pool definition by passing a new Properties object?
083: */
084: public void testUpdate() throws Exception, ClassNotFoundException {
085:
086: String testName = "update";
087: String alias = testName;
088:
089: String url = TestHelper.buildProxoolUrl(alias,
090: TestConstants.HYPERSONIC_DRIVER,
091: TestConstants.HYPERSONIC_TEST_URL);
092: Properties info = new Properties();
093: info.setProperty(ProxoolConstants.USER_PROPERTY,
094: TestConstants.HYPERSONIC_USER);
095: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
096: TestConstants.HYPERSONIC_PASSWORD);
097: info
098: .setProperty(
099: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
100: "1");
101: ProxoolFacade.registerConnectionPool(url, info);
102:
103: // Open a connection. Just for the hell of it
104: DriverManager.getConnection(url).close();
105:
106: assertEquals("maximumConnectionCount", 1, ProxoolFacade
107: .getConnectionPoolDefinition(alias)
108: .getMaximumConnectionCount());
109:
110: // Update using facade
111: info = new Properties();
112: info
113: .setProperty(
114: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
115: "2");
116: ProxoolFacade.updateConnectionPool(url, info);
117: assertEquals("maximumConnectionCount", 2, ProxoolFacade
118: .getConnectionPoolDefinition(alias)
119: .getMaximumConnectionCount());
120:
121: // Now do it on the fly (this is a redefine really)
122: info = new Properties();
123: info.setProperty(ProxoolConstants.USER_PROPERTY,
124: TestConstants.HYPERSONIC_USER);
125: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
126: TestConstants.HYPERSONIC_PASSWORD);
127: info
128: .setProperty(
129: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
130: "3");
131: DriverManager.getConnection(url, info).close();
132: assertEquals("maximumConnectionCount", 3, ProxoolFacade
133: .getConnectionPoolDefinition(alias)
134: .getMaximumConnectionCount());
135:
136: }
137:
138: /**
139: * If we request a connection using exactly the same URL and properties check that it doesn't trigger an update
140: * which forces the pool to be restarted (all existing connections destroyed).
141: */
142: public void testDefinitionNotChanging() throws SQLException,
143: ProxoolException {
144:
145: String testName = "definitionNotChanging";
146: String alias = testName;
147:
148: Connection c1 = null;
149: try {
150: String url = TestHelper.buildProxoolUrl(alias,
151: TestConstants.HYPERSONIC_DRIVER,
152: TestConstants.HYPERSONIC_TEST_URL);
153: Properties info = new Properties();
154: info.setProperty(ProxoolConstants.USER_PROPERTY,
155: TestConstants.HYPERSONIC_USER);
156: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
157: TestConstants.HYPERSONIC_PASSWORD);
158: info.setProperty(
159: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
160: "1");
161: c1 = DriverManager.getConnection(url, info);
162: assertEquals("id=1", 1L, ProxoolFacade.getId(c1));
163: } finally {
164: c1.close();
165: }
166: // The second attempt (using the same definition) should give back the same connection ID
167: Connection c2 = null;
168: try {
169: String url = TestHelper.buildProxoolUrl(alias,
170: TestConstants.HYPERSONIC_DRIVER,
171: TestConstants.HYPERSONIC_TEST_URL);
172: Properties info = new Properties();
173: info.setProperty(ProxoolConstants.USER_PROPERTY,
174: TestConstants.HYPERSONIC_USER);
175: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
176: TestConstants.HYPERSONIC_PASSWORD);
177: info.setProperty(
178: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
179: "1");
180: c2 = DriverManager.getConnection(url, info);
181: assertEquals("id=1", 1L, ProxoolFacade.getId(c2));
182: } finally {
183: c2.close();
184: }
185: // Not the same object. It's wrapped.
186: assertNotSame("c1!=c2", c1, c2);
187: }
188:
189: /**
190: * Can we update a pool definition by calling updateConnectionPool?
191: */
192: public void testUpdateUsingAPI() throws Exception,
193: ClassNotFoundException {
194:
195: String testName = "updateUsingAPI";
196: String alias = testName;
197:
198: String url = ProxoolConstants.PROXOOL
199: + ProxoolConstants.ALIAS_DELIMITER + alias
200: + ProxoolConstants.URL_DELIMITER
201: + TestConstants.HYPERSONIC_DRIVER
202: + ProxoolConstants.URL_DELIMITER
203: + TestConstants.HYPERSONIC_TEST_URL2; //"jdbc:hsqldb:db/update";
204:
205: LOG.debug("Register pool");
206: Properties info = new Properties();
207: String checkAlias = ProxoolFacade.registerConnectionPool(url,
208: info);
209: assertEquals(alias, checkAlias);
210:
211: LOG.debug("setConfigurationListener");
212: ProxoolFacade.addConfigurationListener(alias,
213: new ConfigurationListenerIF() {
214: public void definitionUpdated(
215: ConnectionPoolDefinitionIF connectionPoolDefinition,
216: Properties completeInfo,
217: Properties changedInfo) {
218: }
219: });
220:
221: LOG.debug("setStateListener");
222: ProxoolFacade.addStateListener(alias, new StateListenerIF() {
223: public void upStateChanged(int upState) {
224: }
225: });
226:
227: LOG.debug("Update pool");
228: ProxoolFacade.updateConnectionPool(url, info);
229:
230: }
231:
232: }
233:
234: /*
235: Revision history:
236: $Log: UpdateDefinitionTest.java,v $
237: Revision 1.9 2006/01/18 14:40:06 billhorsman
238: Unbundled Jakarta's Commons Logging.
239:
240: Revision 1.8 2005/09/25 21:48:09 billhorsman
241: New test to check that asking for a connection using the same URL and properties doesn't redefine the pool.
242:
243: Revision 1.7 2004/05/26 17:19:10 brenuart
244: Allow JUnit tests to be executed against another database.
245: By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
246: This behavior can be overriden by setting the 'testConfig' environment property to another location.
247:
248: Revision 1.6 2003/03/04 12:50:44 billhorsman
249: fix
250:
251: Revision 1.5 2003/03/04 10:24:40 billhorsman
252: removed try blocks around each test
253:
254: Revision 1.4 2003/03/03 17:09:07 billhorsman
255: all tests now extend AbstractProxoolTest
256:
257: Revision 1.3 2003/03/03 11:12:05 billhorsman
258: fixed licence
259:
260: Revision 1.2 2003/03/01 15:27:24 billhorsman
261: checkstyle
262:
263: Revision 1.1 2003/02/27 18:01:48 billhorsman
264: completely rethought the test structure. it's now
265: more obvious. no new tests yet though.
266:
267: */
|