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.Connection;
012: import java.sql.DriverManager;
013: import java.sql.SQLException;
014: import java.util.Properties;
015:
016: /**
017: * Test that registering a {@link ConfigurationListenerIF} with the {@link ProxoolFacade}
018: * works.
019: *
020: * @version $Revision: 1.14 $, $Date: 2006/01/18 14:40:06 $
021: * @author Christian Nedregaard (christian_nedregaard@email.com)
022: * @author $Author: billhorsman $ (current maintainer)
023: * @since Proxool 0.7
024: */
025: public class StateListenerTest extends AbstractProxoolTest {
026:
027: private static final Log LOG = LogFactory
028: .getLog(StateListenerTest.class);
029:
030: /**
031: * @see junit.framework.TestCase#TestCase
032: */
033: public StateListenerTest(String s) {
034: super (s);
035: }
036:
037: /**
038: * Test whether we can add a state listener and that it receives
039: * notification of change of state
040: */
041: public void testAddStateListener() throws Exception {
042:
043: String testName = "addStateListener";
044: String alias = testName;
045:
046: String url = TestHelper.buildProxoolUrl(alias,
047: TestConstants.HYPERSONIC_DRIVER,
048: TestConstants.HYPERSONIC_TEST_URL);
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.MAXIMUM_CONNECTION_COUNT_PROPERTY,
057: "1");
058: info
059: .setProperty(
060: ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
061: "0");
062: info.setProperty(
063: ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY,
064: "1000");
065: info
066: .setProperty(
067: ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY,
068: "6000");
069: ProxoolFacade.registerConnectionPool(url, info);
070:
071: assertEquals("maximumConnectionCount", 1, ProxoolFacade
072: .getConnectionPoolDefinition(alias)
073: .getMaximumConnectionCount());
074:
075: StateResultMonitor srm = new StateResultMonitor();
076: ProxoolFacade.addStateListener(alias, srm);
077:
078: assertEquals("maximumConnectionCount", 1, ProxoolFacade
079: .getConnectionPoolDefinition(alias)
080: .getMaximumConnectionCount());
081:
082: Connection c1 = DriverManager.getConnection(url);
083:
084: // Test BUSY
085: srm.setExpectedUpState(StateListenerIF.STATE_BUSY);
086: assertEquals("Timeout waiting for BUSY", ResultMonitor.SUCCESS,
087: srm.getResult());
088:
089: assertEquals("maximumConnectionCount", 1, ProxoolFacade
090: .getConnectionPoolDefinition(alias)
091: .getMaximumConnectionCount());
092:
093: try {
094: Connection c2 = DriverManager.getConnection(url);
095: fail("Didn't expect second connection since maximumConnectionCount is 1");
096: } catch (SQLException e) {
097: // We expect a refusal here
098: // Log message only so we don't get a worrying stack trace
099: LOG.debug("Ignoring expected refusal: " + e.getMessage());
100: }
101:
102: // Test Overloaded
103: srm.setExpectedUpState(StateListenerIF.STATE_OVERLOADED);
104: assertEquals("Timeout waiting for OVERLOADED",
105: ResultMonitor.SUCCESS, srm.getResult());
106:
107: // Test Busy again
108: srm.setExpectedUpState(StateListenerIF.STATE_BUSY);
109: assertEquals("Timeout waiting for BUSY", ResultMonitor.SUCCESS,
110: srm.getResult());
111:
112: // Test Quiet again
113: c1.close();
114: srm.setExpectedUpState(StateListenerIF.STATE_QUIET);
115: assertEquals("Timeout waiting for QUIET",
116: ResultMonitor.SUCCESS, srm.getResult());
117:
118: // Bogus definition -> should be down
119: try {
120: ProxoolFacade.updateConnectionPool("proxool." + alias
121: + ":blah:foo", null);
122: } catch (ProxoolException e) {
123: LOG
124: .debug(
125: "Ignoring expected exception when trying to register a pool with a bogus driver",
126: e);
127: }
128: srm.setExpectedUpState(StateListenerIF.STATE_DOWN);
129: assertEquals("Timeout waiting for DOWN", ResultMonitor.SUCCESS,
130: srm.getResult());
131:
132: }
133:
134: class TestStateListener implements StateListenerIF {
135:
136: private boolean somethingHappened;
137:
138: private int upState;
139:
140: public void upStateChanged(int newUpState) {
141: LOG.debug("upState: " + upState + " -> " + newUpState);
142: upState = newUpState;
143: somethingHappened = true;
144: }
145:
146: boolean isSomethingHappened() {
147: return somethingHappened;
148: }
149:
150: int getUpState() {
151: return upState;
152: }
153:
154: void reset() {
155: upState = 0;
156: somethingHappened = false;
157: }
158:
159: void waitForSomethingToHappen(int stateToWaitFor) {
160:
161: long start = System.currentTimeMillis();
162: while (!somethingHappened) {
163: if (upState == stateToWaitFor) {
164: if (!somethingHappened) {
165: LOG.error("Waiting for state = "
166: + stateToWaitFor
167: + " but it's already at that state");
168: break;
169: }
170: }
171: try {
172: Thread.sleep(500);
173: } catch (InterruptedException e) {
174: LOG.error("Awoken", e);
175: }
176: if (System.currentTimeMillis() - start > 30000) {
177: fail("Timeout waiting for something to happen");
178: }
179: }
180:
181: }
182:
183: int zgetNextState(int stateToWaitFor) {
184: waitForSomethingToHappen(stateToWaitFor);
185: somethingHappened = false;
186: return upState;
187: }
188: }
189: }
190:
191: /*
192: Revision history:
193: $Log: StateListenerTest.java,v $
194: Revision 1.14 2006/01/18 14:40:06 billhorsman
195: Unbundled Jakarta's Commons Logging.
196:
197: Revision 1.13 2005/05/04 16:03:23 billhorsman
198: Now catches ProxoolException when pool is updated with bogus driver.
199:
200: Revision 1.12 2004/06/02 21:05:19 billhorsman
201: Don't log worrying stack traces for expected exceptions.
202:
203: Revision 1.11 2003/03/04 10:58:44 billhorsman
204: checkstyle
205:
206: Revision 1.10 2003/03/04 10:24:40 billhorsman
207: removed try blocks around each test
208:
209: Revision 1.9 2003/03/03 17:09:06 billhorsman
210: all tests now extend AbstractProxoolTest
211:
212: Revision 1.8 2003/03/03 11:12:05 billhorsman
213: fixed licence
214:
215: Revision 1.7 2003/03/02 00:37:23 billhorsman
216: more robust
217:
218: Revision 1.6 2003/03/01 15:27:24 billhorsman
219: checkstyle
220:
221: Revision 1.5 2003/03/01 15:24:09 billhorsman
222: tweaked properties
223:
224: Revision 1.4 2003/02/28 17:41:13 billhorsman
225: more robust wait for state change
226:
227: Revision 1.3 2003/02/27 18:01:48 billhorsman
228: completely rethought the test structure. it's now
229: more obvious. no new tests yet though.
230:
231: Revision 1.2 2003/02/26 16:05:50 billhorsman
232: widespread changes caused by refactoring the way we
233: update and redefine pool definitions.
234:
235: Revision 1.1 2003/02/19 23:07:57 billhorsman
236: new test
237:
238: Revision 1.2 2003/02/19 15:14:22 billhorsman
239: fixed copyright (copy and paste error,
240: not copyright change)
241:
242: Revision 1.1 2003/02/19 13:47:31 chr32
243: Added configuration listener test.
244:
245: Revision 1.2 2003/02/18 16:58:12 chr32
246: Checkstyle.
247:
248: Revision 1.1 2003/02/18 16:51:20 chr32
249: Added tests for ConnectionListeners.
250:
251: */
|