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.admin;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.logicalcobwebs.proxool.AbstractProxoolTest;
011: import org.logicalcobwebs.proxool.ProxoolConstants;
012: import org.logicalcobwebs.proxool.ProxoolFacade;
013: import org.logicalcobwebs.proxool.TestConstants;
014: import org.logicalcobwebs.proxool.TestHelper;
015:
016: import java.sql.DriverManager;
017: import java.text.DateFormat;
018: import java.text.SimpleDateFormat;
019: import java.util.Date;
020: import java.util.Properties;
021:
022: /**
023: * Test {@link StatisticsListenerIF}
024: *
025: * @version $Revision: 1.13 $, $Date: 2006/01/18 14:40:05 $
026: * @author Bill Horsman (bill@logicalcobwebs.co.uk)
027: * @author $Author: billhorsman $ (current maintainer)
028: * @since Proxool 0.7
029: */
030: public class StatisticsListenerTest extends AbstractProxoolTest {
031:
032: private static final Log LOG = LogFactory
033: .getLog(StatisticsListenerTest.class);
034:
035: /**
036: * HH:mm:ss
037: */
038: private static final DateFormat TIME_FORMAT = new SimpleDateFormat(
039: "mm:ss");
040:
041: /**
042: * @see junit.framework.TestCase#TestCase
043: */
044: public StatisticsListenerTest(String s) {
045: super (s);
046: }
047:
048: /**
049: * Can we listen to statistics
050: */
051: public void testListener() throws Exception {
052:
053: String testName = "listener";
054: String alias = testName;
055: String url = TestHelper.buildProxoolUrl(alias,
056: TestConstants.HYPERSONIC_DRIVER,
057: TestConstants.HYPERSONIC_TEST_URL);
058: Properties info = new Properties();
059: info.setProperty(ProxoolConstants.USER_PROPERTY,
060: TestConstants.HYPERSONIC_USER);
061: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
062: TestConstants.HYPERSONIC_PASSWORD);
063: info.setProperty(ProxoolConstants.STATISTICS_PROPERTY, "5s");
064:
065: // We don't test whether anything is logged, but this line should make something appear
066: info.setProperty(
067: ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY,
068: ProxoolConstants.STATISTICS_LOG_LEVEL_DEBUG);
069:
070: // Register pool
071: ProxoolFacade.registerConnectionPool(url, info);
072:
073: // Add listener
074: TestListener testListener = new TestListener();
075: ProxoolFacade.addStatisticsListener(alias, testListener);
076: Date lap0 = new Date();
077:
078: // Wait for next statistics1 so we can guarantee that next set won't
079: // be produced whilst we are building connection
080: testListener.getNextStatistics();
081: Date lap1 = new Date();
082:
083: DriverManager.getConnection(url).close();
084: Date lap2 = new Date();
085: StatisticsIF statistics1 = testListener.getNextStatistics();
086: Date lap3 = new Date();
087: StatisticsIF statistics2 = testListener.getNextStatistics();
088: Date lap4 = new Date();
089:
090: StringBuffer detail = new StringBuffer();
091: detail.append("lap0:");
092: detail.append(TIME_FORMAT.format(lap0));
093: detail.append(", lap1:");
094: detail.append(TIME_FORMAT.format(lap1));
095: detail.append(", lap2:");
096: detail.append(TIME_FORMAT.format(lap2));
097: detail.append(", lap3:");
098: detail.append(TIME_FORMAT.format(lap3));
099: detail.append("(");
100: detail.append(statistics1.getServedCount());
101: detail.append("), lap4:");
102: detail.append(TIME_FORMAT.format(lap4));
103: detail.append("(");
104: detail.append(statistics2.getServedCount());
105: detail.append(")");
106: assertEquals("servedCount - " + detail, 1L, statistics1
107: .getServedCount());
108:
109: }
110:
111: class TestListener implements StatisticsListenerIF {
112:
113: private StatisticsIF statistics;
114:
115: private boolean somethingHappened;
116:
117: public void statistics(String alias, StatisticsIF statistics) {
118: this .statistics = statistics;
119: somethingHappened = true;
120: }
121:
122: void reset() {
123: statistics = null;
124: somethingHappened = false;
125: }
126:
127: public StatisticsIF getStatistics() {
128: return statistics;
129: }
130:
131: void waitForSomethingToHappen() {
132:
133: long start = System.currentTimeMillis();
134: while (!somethingHappened) {
135: try {
136: Thread.sleep(500);
137: } catch (InterruptedException e) {
138: LOG.error("Awoken", e);
139: }
140: if (System.currentTimeMillis() - start > 30000) {
141: fail("Timeout waiting for something to happen");
142: }
143: }
144:
145: }
146:
147: StatisticsIF getNextStatistics() {
148: somethingHappened = false;
149: waitForSomethingToHappen();
150: return statistics;
151: }
152:
153: }
154: }
155:
156: /*
157: Revision history:
158: $Log: StatisticsListenerTest.java,v $
159: Revision 1.13 2006/01/18 14:40:05 billhorsman
160: Unbundled Jakarta's Commons Logging.
161:
162: Revision 1.12 2003/09/05 16:27:27 billhorsman
163: Better debug output.
164:
165: Revision 1.11 2003/03/04 10:58:44 billhorsman
166: checkstyle
167:
168: Revision 1.10 2003/03/04 10:24:40 billhorsman
169: removed try blocks around each test
170:
171: Revision 1.9 2003/03/03 17:09:08 billhorsman
172: all tests now extend AbstractProxoolTest
173:
174: Revision 1.8 2003/03/03 11:12:05 billhorsman
175: fixed licence
176:
177: Revision 1.7 2003/03/01 15:49:33 billhorsman
178: fix
179:
180: Revision 1.6 2003/03/01 15:38:38 billhorsman
181: better assert msg
182:
183: Revision 1.5 2003/03/01 15:27:25 billhorsman
184: checkstyle
185:
186: Revision 1.4 2003/02/27 18:01:48 billhorsman
187: completely rethought the test structure. it's now
188: more obvious. no new tests yet though.
189:
190: Revision 1.3 2003/02/27 09:45:33 billhorsman
191: sleep a little
192:
193: Revision 1.2 2003/02/26 16:05:51 billhorsman
194: widespread changes caused by refactoring the way we
195: update and redefine pool definitions.
196:
197: Revision 1.1 2003/02/20 00:33:15 billhorsman
198: renamed monitor package -> admin
199:
200: Revision 1.5 2003/02/19 23:36:50 billhorsman
201: renamed monitor package to admin
202:
203: Revision 1.4 2003/02/19 15:14:30 billhorsman
204: fixed copyright (copy and paste error,
205: not copyright change)
206:
207: Revision 1.3 2003/02/07 17:28:23 billhorsman
208: checkstyle and doc
209:
210: Revision 1.2 2003/02/07 15:11:33 billhorsman
211: checkstyle
212:
213: Revision 1.1 2003/02/07 15:10:37 billhorsman
214: new admin tests
215:
216: */
|