KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
12 import java.sql.DriverManager JavaDoc;
13 import java.sql.SQLException JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 /**
17  * Test that registering a {@link ConfigurationListenerIF} with the {@link ProxoolFacade}
18  * works.
19  *
20  * @version $Revision: 1.14 $, $Date: 2006/01/18 14:40:06 $
21  * @author Christian Nedregaard (christian_nedregaard@email.com)
22  * @author $Author: billhorsman $ (current maintainer)
23  * @since Proxool 0.7
24  */

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

32     public StateListenerTest(String JavaDoc s) {
33         super(s);
34     }
35
36     /**
37      * Test whether we can add a state listener and that it receives
38      * notification of change of state
39      */

40     public void testAddStateListener() throws Exception JavaDoc {
41
42         String JavaDoc testName = "addStateListener";
43         String JavaDoc alias = testName;
44
45         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
46                 TestConstants.HYPERSONIC_DRIVER,
47                 TestConstants.HYPERSONIC_TEST_URL);
48         Properties JavaDoc info = new Properties JavaDoc();
49         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
50         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
51         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "1");
52         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "0");
53         info.setProperty(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, "1000");
54         info.setProperty(ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY, "6000");
55         ProxoolFacade.registerConnectionPool(url, info);
56
57         assertEquals("maximumConnectionCount", 1, ProxoolFacade.getConnectionPoolDefinition(alias).getMaximumConnectionCount());
58
59         StateResultMonitor srm = new StateResultMonitor();
60         ProxoolFacade.addStateListener(alias, srm);
61
62         assertEquals("maximumConnectionCount", 1, ProxoolFacade.getConnectionPoolDefinition(alias).getMaximumConnectionCount());
63
64         Connection JavaDoc c1 = DriverManager.getConnection(url);
65
66         // Test BUSY
67
srm.setExpectedUpState(StateListenerIF.STATE_BUSY);
68         assertEquals("Timeout waiting for BUSY", ResultMonitor.SUCCESS, srm.getResult());
69
70         assertEquals("maximumConnectionCount", 1, ProxoolFacade.getConnectionPoolDefinition(alias).getMaximumConnectionCount());
71
72         try {
73             Connection JavaDoc c2 = DriverManager.getConnection(url);
74             fail("Didn't expect second connection since maximumConnectionCount is 1");
75         } catch (SQLException JavaDoc e) {
76             // We expect a refusal here
77
// Log message only so we don't get a worrying stack trace
78
LOG.debug("Ignoring expected refusal: " + e.getMessage());
79         }
80
81         // Test Overloaded
82
srm.setExpectedUpState(StateListenerIF.STATE_OVERLOADED);
83         assertEquals("Timeout waiting for OVERLOADED", ResultMonitor.SUCCESS, srm.getResult());
84
85         // Test Busy again
86
srm.setExpectedUpState(StateListenerIF.STATE_BUSY);
87         assertEquals("Timeout waiting for BUSY", ResultMonitor.SUCCESS, srm.getResult());
88
89         // Test Quiet again
90
c1.close();
91         srm.setExpectedUpState(StateListenerIF.STATE_QUIET);
92         assertEquals("Timeout waiting for QUIET", ResultMonitor.SUCCESS, srm.getResult());
93
94         // Bogus definition -> should be down
95
try {
96             ProxoolFacade.updateConnectionPool("proxool." + alias + ":blah:foo", null);
97         } catch (ProxoolException e) {
98             LOG.debug("Ignoring expected exception when trying to register a pool with a bogus driver", e);
99         }
100         srm.setExpectedUpState(StateListenerIF.STATE_DOWN);
101         assertEquals("Timeout waiting for DOWN", ResultMonitor.SUCCESS, srm.getResult());
102
103     }
104
105     class TestStateListener implements StateListenerIF {
106
107         private boolean somethingHappened;
108
109         private int upState;
110
111         public void upStateChanged(int newUpState) {
112             LOG.debug("upState: " + upState + " -> " + newUpState);
113             upState = newUpState;
114             somethingHappened = true;
115         }
116
117         boolean isSomethingHappened() {
118             return somethingHappened;
119         }
120
121         int getUpState() {
122             return upState;
123         }
124
125         void reset() {
126             upState = 0;
127             somethingHappened = false;
128         }
129
130         void waitForSomethingToHappen(int stateToWaitFor) {
131
132             long start = System.currentTimeMillis();
133             while (!somethingHappened) {
134                 if (upState == stateToWaitFor) {
135                     if (!somethingHappened) {
136                         LOG.error("Waiting for state = " + stateToWaitFor + " but it's already at that state");
137                         break;
138                     }
139                 }
140                 try {
141                     Thread.sleep(500);
142                 } catch (InterruptedException JavaDoc e) {
143                     LOG.error("Awoken", e);
144                 }
145                 if (System.currentTimeMillis() - start > 30000) {
146                     fail("Timeout waiting for something to happen");
147                 }
148             }
149
150         }
151
152         int zgetNextState(int stateToWaitFor) {
153             waitForSomethingToHappen(stateToWaitFor);
154             somethingHappened = false;
155             return upState;
156         }
157     }
158 }
159
160 /*
161  Revision history:
162  $Log: StateListenerTest.java,v $
163  Revision 1.14 2006/01/18 14:40:06 billhorsman
164  Unbundled Jakarta's Commons Logging.
165
166  Revision 1.13 2005/05/04 16:03:23 billhorsman
167  Now catches ProxoolException when pool is updated with bogus driver.
168
169  Revision 1.12 2004/06/02 21:05:19 billhorsman
170  Don't log worrying stack traces for expected exceptions.
171
172  Revision 1.11 2003/03/04 10:58:44 billhorsman
173  checkstyle
174
175  Revision 1.10 2003/03/04 10:24:40 billhorsman
176  removed try blocks around each test
177
178  Revision 1.9 2003/03/03 17:09:06 billhorsman
179  all tests now extend AbstractProxoolTest
180
181  Revision 1.8 2003/03/03 11:12:05 billhorsman
182  fixed licence
183
184  Revision 1.7 2003/03/02 00:37:23 billhorsman
185  more robust
186
187  Revision 1.6 2003/03/01 15:27:24 billhorsman
188  checkstyle
189
190  Revision 1.5 2003/03/01 15:24:09 billhorsman
191  tweaked properties
192
193  Revision 1.4 2003/02/28 17:41:13 billhorsman
194  more robust wait for state change
195
196  Revision 1.3 2003/02/27 18:01:48 billhorsman
197  completely rethought the test structure. it's now
198  more obvious. no new tests yet though.
199
200  Revision 1.2 2003/02/26 16:05:50 billhorsman
201  widespread changes caused by refactoring the way we
202  update and redefine pool definitions.
203
204  Revision 1.1 2003/02/19 23:07:57 billhorsman
205  new test
206
207  Revision 1.2 2003/02/19 15:14:22 billhorsman
208  fixed copyright (copy and paste error,
209  not copyright change)
210
211  Revision 1.1 2003/02/19 13:47:31 chr32
212  Added configuration listener test.
213
214  Revision 1.2 2003/02/18 16:58:12 chr32
215  Checkstyle.
216
217  Revision 1.1 2003/02/18 16:51:20 chr32
218  Added tests for ConnectionListeners.
219
220 */

221
Popular Tags