KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > ConfigurationListenerTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.util.Properties JavaDoc;
12 import java.sql.DriverManager JavaDoc;
13
14 /**
15  * Test that registering a {@link org.logicalcobwebs.proxool.ConfigurationListenerIF}
16  * with the {@link org.logicalcobwebs.proxool.ProxoolFacade}
17  * works.
18  *
19  * @version $Revision: 1.13 $, $Date: 2006/01/18 14:40:06 $
20  * @author Christian Nedregaard (christian_nedregaard@email.com)
21  * @author $Author: billhorsman $ (current maintainer)
22  * @since Proxool 0.7
23  */

24 public class ConfigurationListenerTest extends AbstractProxoolTest {
25
26     private static final Log LOG = LogFactory.getLog(ConfigurationListenerTest.class);
27
28     /**
29      * @see junit.framework.TestCase#TestCase
30      */

31     public ConfigurationListenerTest(String JavaDoc s) {
32         super(s);
33     }
34
35     /**
36      * Add a listener
37      *
38      * @throws Exception if anything goes wrong
39      */

40     public void testAddAndRemove() throws Exception JavaDoc {
41
42         String JavaDoc testName = "addAndRemove";
43         String JavaDoc alias = testName;
44
45         // Register pool
46
String JavaDoc url = TestHelper.buildProxoolUrl(alias,
47                 TestConstants.HYPERSONIC_DRIVER,
48                 TestConstants.HYPERSONIC_TEST_URL);
49         Properties JavaDoc info = new Properties JavaDoc();
50         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
51         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
52         ProxoolFacade.registerConnectionPool(url, info);
53
54         // add a listener
55
MyConfigurationListener mcl1 = new MyConfigurationListener();
56         ProxoolFacade.addConfigurationListener(alias, mcl1);
57
58         // Update the definition
59
Properties JavaDoc newInfo = new Properties JavaDoc();
60         newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString());
61         ProxoolFacade.updateConnectionPool(url, newInfo);
62         assertEquals("definitionReceived", true, mcl1.isUpdateReceived());
63         mcl1.reset();
64
65         // add another listener
66
MyConfigurationListener mcl2 = new MyConfigurationListener();
67         ProxoolFacade.addConfigurationListener(alias, mcl2);
68
69         // Update the definition
70
newInfo = new Properties JavaDoc();
71         newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.FALSE.toString());
72         ProxoolFacade.updateConnectionPool(url, newInfo);
73         assertEquals("definitionReceived", true, mcl1.isUpdateReceived());
74         assertEquals("definitionReceived", true, mcl2.isUpdateReceived());
75         mcl1.reset();
76         mcl2.reset();
77
78         // Remove the first listener
79
ProxoolFacade.removeConfigurationListener(alias, mcl1);
80
81         // Update the definition
82
newInfo = new Properties JavaDoc();
83         newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString());
84         ProxoolFacade.updateConnectionPool(url, newInfo);
85         assertEquals("definitionReceived", false, mcl1.isUpdateReceived());
86         assertEquals("definitionReceived", true, mcl2.isUpdateReceived());
87         mcl1.reset();
88         mcl2.reset();
89
90         // Check that just asking for another Connection (without config
91
// change) doesn't trigger another event
92
DriverManager.getConnection(url).close();
93         assertEquals("definitionReceived", false, mcl2.isUpdateReceived());
94         mcl2.reset();
95
96         // Now try again, but this time pass in the properties (without
97
// change)
98
LOG.debug("Getting another connection to trigger any pending config changes");
99         DriverManager.getConnection(url, info).close();
100         mcl2.reset();
101         LOG.debug("Getting another connection which shouldn't cause another config change");
102         DriverManager.getConnection(url, info).close();
103         assertEquals("definitionReceived", false, mcl2.isUpdateReceived());
104         mcl2.reset();
105
106         // This time pass in the properties (WITH change)
107
LOG.debug("Expecting a change now");
108         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.FALSE.toString());
109         DriverManager.getConnection(url, info).close();
110         assertEquals("definitionReceived", true, mcl2.isUpdateReceived());
111         mcl2.reset();
112
113         // Remove the second listener
114
ProxoolFacade.removeConfigurationListener(alias, mcl2);
115
116         // Update the definition
117
newInfo = new Properties JavaDoc();
118         newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.FALSE.toString());
119         ProxoolFacade.updateConnectionPool(url, newInfo);
120         assertEquals("definitionReceived", false, mcl1.isUpdateReceived());
121         assertEquals("definitionReceived", false, mcl2.isUpdateReceived());
122         mcl1.reset();
123         mcl2.reset();
124
125     }
126
127     /**
128      * Do configuration listeners work
129      *
130      * @throws Exception if anything goes wrong
131      */

132     public void testConfigurationListeners() throws Exception JavaDoc {
133
134         String JavaDoc testName = "configurationListener";
135         String JavaDoc alias = testName;
136
137         // Register pool
138
final String JavaDoc delegateUrl1 = TestConstants.HYPERSONIC_TEST_URL;
139         final String JavaDoc delegateUrl2 = TestConstants.HYPERSONIC_TEST_URL2;
140
141         final String JavaDoc url1 = TestHelper.buildProxoolUrl(alias,
142                 TestConstants.HYPERSONIC_DRIVER,
143                 delegateUrl1);
144         final String JavaDoc url2 = TestHelper.buildProxoolUrl(alias,
145                 TestConstants.HYPERSONIC_DRIVER,
146                 delegateUrl2);
147
148         Properties JavaDoc info = new Properties JavaDoc();
149         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
150         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
151         ProxoolFacade.registerConnectionPool(url1, info);
152
153         int propertyCount = info.size();
154
155         // listen to the configuration
156
MyConfigurationListener mcl = new MyConfigurationListener();
157         ProxoolFacade.addConfigurationListener(alias, mcl);
158
159         // Update the URL
160
ProxoolFacade.updateConnectionPool(url2, null);
161         LOG.debug("changed: " + mcl.getChangedInfo());
162         LOG.debug("complete: " + mcl.getCompleteInfo());
163         assertEquals("changed size", 0, mcl.getChangedInfo().size());
164         assertEquals("complete size", propertyCount, mcl.getCompleteInfo().size());
165         assertEquals("url", delegateUrl2, mcl.getConnectionPoolDefinition().getUrl());
166         mcl.reset();
167
168         // Add the verbose property
169
Properties JavaDoc newInfo = new Properties JavaDoc();
170         newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString());
171         ProxoolFacade.updateConnectionPool(url2, newInfo);
172         LOG.debug("changed: " + mcl.getChangedInfo());
173         LOG.debug("complete: " + mcl.getCompleteInfo());
174         assertEquals("completeInfo size", propertyCount + 1, mcl.getCompleteInfo().size());
175         assertEquals("changedInfo size", 1, mcl.getChangedInfo().size());
176         assertEquals("url", true, mcl.getConnectionPoolDefinition().isVerbose());
177         mcl.reset();
178
179         // modify the verbose property
180
newInfo = new Properties JavaDoc();
181         newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.FALSE.toString());
182         ProxoolFacade.updateConnectionPool(url2, newInfo);
183         LOG.debug("changed: " + mcl.getChangedInfo());
184         LOG.debug("complete: " + mcl.getCompleteInfo());
185         assertEquals("completeInfo size", propertyCount + 1, mcl.getCompleteInfo().size());
186         assertEquals("changedInfo size", 1, mcl.getChangedInfo().size());
187         assertEquals("url", false, mcl.getConnectionPoolDefinition().isVerbose());
188         mcl.reset();
189
190     }
191
192     class MyConfigurationListener implements ConfigurationListenerIF {
193
194         private Properties JavaDoc completeInfo;
195
196         private Properties JavaDoc changedInfo;
197
198         private ConnectionPoolDefinitionIF connectionPoolDefinition;
199
200         private boolean updateReceived;
201
202         public void definitionUpdated(ConnectionPoolDefinitionIF connectionPoolDefinition, Properties JavaDoc completeInfo, Properties JavaDoc changedInfo) {
203             this.connectionPoolDefinition = connectionPoolDefinition;
204             this.completeInfo = completeInfo;
205             this.changedInfo = changedInfo;
206             updateReceived = true;
207         }
208
209         public Properties JavaDoc getCompleteInfo() {
210             return completeInfo;
211         }
212
213         public Properties JavaDoc getChangedInfo() {
214             return changedInfo;
215         }
216
217         public ConnectionPoolDefinitionIF getConnectionPoolDefinition() {
218             return connectionPoolDefinition;
219         }
220
221         public boolean isUpdateReceived() {
222             return updateReceived;
223         }
224
225         public void reset() {
226             completeInfo.clear();
227             changedInfo.clear();
228             updateReceived = false;
229         }
230
231     }
232
233 }
234
235 /*
236  Revision history:
237  $Log: ConfigurationListenerTest.java,v $
238  Revision 1.13 2006/01/18 14:40:06 billhorsman
239  Unbundled Jakarta's Commons Logging.
240
241  Revision 1.12 2004/05/26 17:19:09 brenuart
242  Allow JUnit tests to be executed against another database.
243  By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
244  This behavior can be overriden by setting the 'testConfig' environment property to another location.
245
246  Revision 1.11 2003/04/27 15:44:19 billhorsman
247  better tests
248
249  Revision 1.10 2003/04/10 21:48:58 billhorsman
250  enhanced to trap bug where config change event gets fired
251  all the time
252
253  Revision 1.9 2003/03/04 10:24:40 billhorsman
254  removed try blocks around each test
255
256  Revision 1.8 2003/03/03 17:08:54 billhorsman
257  all tests now extend AbstractProxoolTest
258
259  Revision 1.7 2003/03/03 11:12:03 billhorsman
260  fixed licence
261
262  Revision 1.6 2003/02/27 18:01:47 billhorsman
263  completely rethought the test structure. it's now
264  more obvious. no new tests yet though.
265
266  Revision 1.5 2003/02/26 16:05:49 billhorsman
267  widespread changes caused by refactoring the way we
268  update and redefine pool definitions.
269
270  Revision 1.4 2003/02/24 18:04:07 chr32
271  Fixde some eroneous property names.
272
273  Revision 1.3 2003/02/19 17:00:51 chr32
274  Fixed eroneous method names.
275
276  Revision 1.2 2003/02/19 15:14:22 billhorsman
277  fixed copyright (copy and paste error,
278  not copyright change)
279
280  Revision 1.1 2003/02/19 13:47:31 chr32
281  Added configuration listener test.
282
283  Revision 1.2 2003/02/18 16:58:12 chr32
284  Checkstyle.
285
286  Revision 1.1 2003/02/18 16:51:20 chr32
287  Added tests for ConnectionListeners.
288
289 */

290
Popular Tags