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.util.Properties;
012: import java.sql.DriverManager;
013:
014: /**
015: * Test that registering a {@link org.logicalcobwebs.proxool.ConfigurationListenerIF}
016: * with the {@link org.logicalcobwebs.proxool.ProxoolFacade}
017: * works.
018: *
019: * @version $Revision: 1.13 $, $Date: 2006/01/18 14:40:06 $
020: * @author Christian Nedregaard (christian_nedregaard@email.com)
021: * @author $Author: billhorsman $ (current maintainer)
022: * @since Proxool 0.7
023: */
024: public class ConfigurationListenerTest extends AbstractProxoolTest {
025:
026: private static final Log LOG = LogFactory
027: .getLog(ConfigurationListenerTest.class);
028:
029: /**
030: * @see junit.framework.TestCase#TestCase
031: */
032: public ConfigurationListenerTest(String s) {
033: super (s);
034: }
035:
036: /**
037: * Add a listener
038: *
039: * @throws Exception if anything goes wrong
040: */
041: public void testAddAndRemove() throws Exception {
042:
043: String testName = "addAndRemove";
044: String alias = testName;
045:
046: // Register pool
047: String url = TestHelper.buildProxoolUrl(alias,
048: TestConstants.HYPERSONIC_DRIVER,
049: TestConstants.HYPERSONIC_TEST_URL);
050: Properties info = new Properties();
051: info.setProperty(ProxoolConstants.USER_PROPERTY,
052: TestConstants.HYPERSONIC_USER);
053: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
054: TestConstants.HYPERSONIC_PASSWORD);
055: ProxoolFacade.registerConnectionPool(url, info);
056:
057: // add a listener
058: MyConfigurationListener mcl1 = new MyConfigurationListener();
059: ProxoolFacade.addConfigurationListener(alias, mcl1);
060:
061: // Update the definition
062: Properties newInfo = new Properties();
063: newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY,
064: Boolean.TRUE.toString());
065: ProxoolFacade.updateConnectionPool(url, newInfo);
066: assertEquals("definitionReceived", true, mcl1
067: .isUpdateReceived());
068: mcl1.reset();
069:
070: // add another listener
071: MyConfigurationListener mcl2 = new MyConfigurationListener();
072: ProxoolFacade.addConfigurationListener(alias, mcl2);
073:
074: // Update the definition
075: newInfo = new Properties();
076: newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY,
077: Boolean.FALSE.toString());
078: ProxoolFacade.updateConnectionPool(url, newInfo);
079: assertEquals("definitionReceived", true, mcl1
080: .isUpdateReceived());
081: assertEquals("definitionReceived", true, mcl2
082: .isUpdateReceived());
083: mcl1.reset();
084: mcl2.reset();
085:
086: // Remove the first listener
087: ProxoolFacade.removeConfigurationListener(alias, mcl1);
088:
089: // Update the definition
090: newInfo = new Properties();
091: newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY,
092: Boolean.TRUE.toString());
093: ProxoolFacade.updateConnectionPool(url, newInfo);
094: assertEquals("definitionReceived", false, mcl1
095: .isUpdateReceived());
096: assertEquals("definitionReceived", true, mcl2
097: .isUpdateReceived());
098: mcl1.reset();
099: mcl2.reset();
100:
101: // Check that just asking for another Connection (without config
102: // change) doesn't trigger another event
103: DriverManager.getConnection(url).close();
104: assertEquals("definitionReceived", false, mcl2
105: .isUpdateReceived());
106: mcl2.reset();
107:
108: // Now try again, but this time pass in the properties (without
109: // change)
110: LOG
111: .debug("Getting another connection to trigger any pending config changes");
112: DriverManager.getConnection(url, info).close();
113: mcl2.reset();
114: LOG
115: .debug("Getting another connection which shouldn't cause another config change");
116: DriverManager.getConnection(url, info).close();
117: assertEquals("definitionReceived", false, mcl2
118: .isUpdateReceived());
119: mcl2.reset();
120:
121: // This time pass in the properties (WITH change)
122: LOG.debug("Expecting a change now");
123: info.setProperty(ProxoolConstants.VERBOSE_PROPERTY,
124: Boolean.FALSE.toString());
125: DriverManager.getConnection(url, info).close();
126: assertEquals("definitionReceived", true, mcl2
127: .isUpdateReceived());
128: mcl2.reset();
129:
130: // Remove the second listener
131: ProxoolFacade.removeConfigurationListener(alias, mcl2);
132:
133: // Update the definition
134: newInfo = new Properties();
135: newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY,
136: Boolean.FALSE.toString());
137: ProxoolFacade.updateConnectionPool(url, newInfo);
138: assertEquals("definitionReceived", false, mcl1
139: .isUpdateReceived());
140: assertEquals("definitionReceived", false, mcl2
141: .isUpdateReceived());
142: mcl1.reset();
143: mcl2.reset();
144:
145: }
146:
147: /**
148: * Do configuration listeners work
149: *
150: * @throws Exception if anything goes wrong
151: */
152: public void testConfigurationListeners() throws Exception {
153:
154: String testName = "configurationListener";
155: String alias = testName;
156:
157: // Register pool
158: final String delegateUrl1 = TestConstants.HYPERSONIC_TEST_URL;
159: final String delegateUrl2 = TestConstants.HYPERSONIC_TEST_URL2;
160:
161: final String url1 = TestHelper.buildProxoolUrl(alias,
162: TestConstants.HYPERSONIC_DRIVER, delegateUrl1);
163: final String url2 = TestHelper.buildProxoolUrl(alias,
164: TestConstants.HYPERSONIC_DRIVER, delegateUrl2);
165:
166: Properties info = new Properties();
167: info.setProperty(ProxoolConstants.USER_PROPERTY,
168: TestConstants.HYPERSONIC_USER);
169: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
170: TestConstants.HYPERSONIC_PASSWORD);
171: ProxoolFacade.registerConnectionPool(url1, info);
172:
173: int propertyCount = info.size();
174:
175: // listen to the configuration
176: MyConfigurationListener mcl = new MyConfigurationListener();
177: ProxoolFacade.addConfigurationListener(alias, mcl);
178:
179: // Update the URL
180: ProxoolFacade.updateConnectionPool(url2, null);
181: LOG.debug("changed: " + mcl.getChangedInfo());
182: LOG.debug("complete: " + mcl.getCompleteInfo());
183: assertEquals("changed size", 0, mcl.getChangedInfo().size());
184: assertEquals("complete size", propertyCount, mcl
185: .getCompleteInfo().size());
186: assertEquals("url", delegateUrl2, mcl
187: .getConnectionPoolDefinition().getUrl());
188: mcl.reset();
189:
190: // Add the verbose property
191: Properties newInfo = new Properties();
192: newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY,
193: Boolean.TRUE.toString());
194: ProxoolFacade.updateConnectionPool(url2, newInfo);
195: LOG.debug("changed: " + mcl.getChangedInfo());
196: LOG.debug("complete: " + mcl.getCompleteInfo());
197: assertEquals("completeInfo size", propertyCount + 1, mcl
198: .getCompleteInfo().size());
199: assertEquals("changedInfo size", 1, mcl.getChangedInfo().size());
200: assertEquals("url", true, mcl.getConnectionPoolDefinition()
201: .isVerbose());
202: mcl.reset();
203:
204: // modify the verbose property
205: newInfo = new Properties();
206: newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY,
207: Boolean.FALSE.toString());
208: ProxoolFacade.updateConnectionPool(url2, newInfo);
209: LOG.debug("changed: " + mcl.getChangedInfo());
210: LOG.debug("complete: " + mcl.getCompleteInfo());
211: assertEquals("completeInfo size", propertyCount + 1, mcl
212: .getCompleteInfo().size());
213: assertEquals("changedInfo size", 1, mcl.getChangedInfo().size());
214: assertEquals("url", false, mcl.getConnectionPoolDefinition()
215: .isVerbose());
216: mcl.reset();
217:
218: }
219:
220: class MyConfigurationListener implements ConfigurationListenerIF {
221:
222: private Properties completeInfo;
223:
224: private Properties changedInfo;
225:
226: private ConnectionPoolDefinitionIF connectionPoolDefinition;
227:
228: private boolean updateReceived;
229:
230: public void definitionUpdated(
231: ConnectionPoolDefinitionIF connectionPoolDefinition,
232: Properties completeInfo, Properties changedInfo) {
233: this .connectionPoolDefinition = connectionPoolDefinition;
234: this .completeInfo = completeInfo;
235: this .changedInfo = changedInfo;
236: updateReceived = true;
237: }
238:
239: public Properties getCompleteInfo() {
240: return completeInfo;
241: }
242:
243: public Properties getChangedInfo() {
244: return changedInfo;
245: }
246:
247: public ConnectionPoolDefinitionIF getConnectionPoolDefinition() {
248: return connectionPoolDefinition;
249: }
250:
251: public boolean isUpdateReceived() {
252: return updateReceived;
253: }
254:
255: public void reset() {
256: completeInfo.clear();
257: changedInfo.clear();
258: updateReceived = false;
259: }
260:
261: }
262:
263: }
264:
265: /*
266: Revision history:
267: $Log: ConfigurationListenerTest.java,v $
268: Revision 1.13 2006/01/18 14:40:06 billhorsman
269: Unbundled Jakarta's Commons Logging.
270:
271: Revision 1.12 2004/05/26 17:19:09 brenuart
272: Allow JUnit tests to be executed against another database.
273: By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
274: This behavior can be overriden by setting the 'testConfig' environment property to another location.
275:
276: Revision 1.11 2003/04/27 15:44:19 billhorsman
277: better tests
278:
279: Revision 1.10 2003/04/10 21:48:58 billhorsman
280: enhanced to trap bug where config change event gets fired
281: all the time
282:
283: Revision 1.9 2003/03/04 10:24:40 billhorsman
284: removed try blocks around each test
285:
286: Revision 1.8 2003/03/03 17:08:54 billhorsman
287: all tests now extend AbstractProxoolTest
288:
289: Revision 1.7 2003/03/03 11:12:03 billhorsman
290: fixed licence
291:
292: Revision 1.6 2003/02/27 18:01:47 billhorsman
293: completely rethought the test structure. it's now
294: more obvious. no new tests yet though.
295:
296: Revision 1.5 2003/02/26 16:05:49 billhorsman
297: widespread changes caused by refactoring the way we
298: update and redefine pool definitions.
299:
300: Revision 1.4 2003/02/24 18:04:07 chr32
301: Fixde some eroneous property names.
302:
303: Revision 1.3 2003/02/19 17:00:51 chr32
304: Fixed eroneous method names.
305:
306: Revision 1.2 2003/02/19 15:14:22 billhorsman
307: fixed copyright (copy and paste error,
308: not copyright change)
309:
310: Revision 1.1 2003/02/19 13:47:31 chr32
311: Added configuration listener test.
312:
313: Revision 1.2 2003/02/18 16:58:12 chr32
314: Checkstyle.
315:
316: Revision 1.1 2003/02/18 16:51:20 chr32
317: Added tests for ConnectionListeners.
318:
319: */
|