KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > admin > StatisticsListenerTest


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.admin;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.logicalcobwebs.proxool.AbstractProxoolTest;
11 import org.logicalcobwebs.proxool.ProxoolConstants;
12 import org.logicalcobwebs.proxool.ProxoolFacade;
13 import org.logicalcobwebs.proxool.TestConstants;
14 import org.logicalcobwebs.proxool.TestHelper;
15
16 import java.sql.DriverManager JavaDoc;
17 import java.text.DateFormat JavaDoc;
18 import java.text.SimpleDateFormat JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 /**
23  * Test {@link StatisticsListenerIF}
24  *
25  * @version $Revision: 1.13 $, $Date: 2006/01/18 14:40:05 $
26  * @author Bill Horsman (bill@logicalcobwebs.co.uk)
27  * @author $Author: billhorsman $ (current maintainer)
28  * @since Proxool 0.7
29  */

30 public class StatisticsListenerTest extends AbstractProxoolTest {
31
32     private static final Log LOG = LogFactory.getLog(StatisticsListenerTest.class);
33
34     /**
35      * HH:mm:ss
36      */

37     private static final DateFormat JavaDoc TIME_FORMAT = new SimpleDateFormat JavaDoc("mm:ss");
38
39     /**
40      * @see junit.framework.TestCase#TestCase
41      */

42     public StatisticsListenerTest(String JavaDoc s) {
43         super(s);
44     }
45
46     /**
47      * Can we listen to statistics
48      */

49     public void testListener() throws Exception JavaDoc {
50
51         String JavaDoc testName = "listener";
52         String JavaDoc alias = testName;
53         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
54                 TestConstants.HYPERSONIC_DRIVER,
55                 TestConstants.HYPERSONIC_TEST_URL);
56         Properties JavaDoc info = new Properties JavaDoc();
57         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
58         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
59         info.setProperty(ProxoolConstants.STATISTICS_PROPERTY, "5s");
60
61         // We don't test whether anything is logged, but this line should make something appear
62
info.setProperty(ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY, ProxoolConstants.STATISTICS_LOG_LEVEL_DEBUG);
63
64         // Register pool
65
ProxoolFacade.registerConnectionPool(url, info);
66
67         // Add listener
68
TestListener testListener = new TestListener();
69         ProxoolFacade.addStatisticsListener(alias, testListener);
70         Date JavaDoc lap0 = new Date JavaDoc();
71
72         // Wait for next statistics1 so we can guarantee that next set won't
73
// be produced whilst we are building connection
74
testListener.getNextStatistics();
75         Date JavaDoc lap1 = new Date JavaDoc();
76
77         DriverManager.getConnection(url).close();
78         Date JavaDoc lap2 = new Date JavaDoc();
79         StatisticsIF statistics1 = testListener.getNextStatistics();
80         Date JavaDoc lap3 = new Date JavaDoc();
81         StatisticsIF statistics2 = testListener.getNextStatistics();
82         Date JavaDoc lap4 = new Date JavaDoc();
83
84         StringBuffer JavaDoc detail = new StringBuffer JavaDoc();
85         detail.append("lap0:");
86         detail.append(TIME_FORMAT.format(lap0));
87         detail.append(", lap1:");
88         detail.append(TIME_FORMAT.format(lap1));
89         detail.append(", lap2:");
90         detail.append(TIME_FORMAT.format(lap2));
91         detail.append(", lap3:");
92         detail.append(TIME_FORMAT.format(lap3));
93         detail.append("(");
94         detail.append(statistics1.getServedCount());
95         detail.append("), lap4:");
96         detail.append(TIME_FORMAT.format(lap4));
97         detail.append("(");
98         detail.append(statistics2.getServedCount());
99         detail.append(")");
100         assertEquals("servedCount - " + detail, 1L, statistics1.getServedCount());
101
102     }
103
104     class TestListener implements StatisticsListenerIF {
105
106         private StatisticsIF statistics;
107
108         private boolean somethingHappened;
109
110         public void statistics(String JavaDoc alias, StatisticsIF statistics) {
111             this.statistics = statistics;
112             somethingHappened = true;
113         }
114
115         void reset() {
116             statistics = null;
117             somethingHappened = false;
118         }
119
120         public StatisticsIF getStatistics() {
121             return statistics;
122         }
123
124         void waitForSomethingToHappen() {
125
126             long start = System.currentTimeMillis();
127             while (!somethingHappened) {
128                 try {
129                     Thread.sleep(500);
130                 } catch (InterruptedException JavaDoc e) {
131                     LOG.error("Awoken", e);
132                 }
133                 if (System.currentTimeMillis() - start > 30000) {
134                     fail("Timeout waiting for something to happen");
135                 }
136             }
137
138         }
139
140         StatisticsIF getNextStatistics() {
141             somethingHappened = false;
142             waitForSomethingToHappen();
143             return statistics;
144         }
145
146     }
147 }
148
149 /*
150  Revision history:
151  $Log: StatisticsListenerTest.java,v $
152  Revision 1.13 2006/01/18 14:40:05 billhorsman
153  Unbundled Jakarta's Commons Logging.
154
155  Revision 1.12 2003/09/05 16:27:27 billhorsman
156  Better debug output.
157
158  Revision 1.11 2003/03/04 10:58:44 billhorsman
159  checkstyle
160
161  Revision 1.10 2003/03/04 10:24:40 billhorsman
162  removed try blocks around each test
163
164  Revision 1.9 2003/03/03 17:09:08 billhorsman
165  all tests now extend AbstractProxoolTest
166
167  Revision 1.8 2003/03/03 11:12:05 billhorsman
168  fixed licence
169
170  Revision 1.7 2003/03/01 15:49:33 billhorsman
171  fix
172
173  Revision 1.6 2003/03/01 15:38:38 billhorsman
174  better assert msg
175
176  Revision 1.5 2003/03/01 15:27:25 billhorsman
177  checkstyle
178
179  Revision 1.4 2003/02/27 18:01:48 billhorsman
180  completely rethought the test structure. it's now
181  more obvious. no new tests yet though.
182
183  Revision 1.3 2003/02/27 09:45:33 billhorsman
184  sleep a little
185
186  Revision 1.2 2003/02/26 16:05:51 billhorsman
187  widespread changes caused by refactoring the way we
188  update and redefine pool definitions.
189
190  Revision 1.1 2003/02/20 00:33:15 billhorsman
191  renamed monitor package -> admin
192
193  Revision 1.5 2003/02/19 23:36:50 billhorsman
194  renamed monitor package to admin
195
196  Revision 1.4 2003/02/19 15:14:30 billhorsman
197  fixed copyright (copy and paste error,
198  not copyright change)
199
200  Revision 1.3 2003/02/07 17:28:23 billhorsman
201  checkstyle and doc
202
203  Revision 1.2 2003/02/07 15:11:33 billhorsman
204  checkstyle
205
206  Revision 1.1 2003/02/07 15:10:37 billhorsman
207  new admin tests
208
209  */

210
Popular Tags