KickJava   Java API By Example, From Geeks To Geeks.

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


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.ConnectionInfoIF;
12 import org.logicalcobwebs.proxool.ProxoolConstants;
13 import org.logicalcobwebs.proxool.ProxoolFacade;
14 import org.logicalcobwebs.proxool.TestConstants;
15 import org.logicalcobwebs.proxool.TestHelper;
16
17 import java.sql.Connection JavaDoc;
18 import java.sql.DriverManager JavaDoc;
19 import java.sql.Statement JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 /**
23  * Test {@link SnapshotIF}
24  *
25  * @version $Revision: 1.14 $, $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 SnapshotTest extends AbstractProxoolTest {
31
32     private static final Log LOG = LogFactory.getLog(SnapshotTest.class);
33
34     /**
35      * @see junit.framework.TestCase#TestCase
36      */

37     public SnapshotTest(String JavaDoc s) {
38         super(s);
39     }
40
41     /**
42      * Test whether the statistics we get back are roughly right.
43      */

44     public void testSnapshot() throws Exception JavaDoc {
45
46         String JavaDoc testName = "snapshot";
47         final String JavaDoc alias = testName;
48         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
49                 TestConstants.HYPERSONIC_DRIVER,
50                 TestConstants.HYPERSONIC_TEST_URL);
51         Properties JavaDoc info = new Properties JavaDoc();
52         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
53         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
54         info.setProperty(ProxoolConstants.STATISTICS_PROPERTY, "10s,15s");
55         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "1");
56         info.setProperty(ProxoolConstants.TRACE_PROPERTY, "true");
57
58         // We don't test whether anything is logged, but this line should make something appear
59
info.setProperty(ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY, ProxoolConstants.STATISTICS_LOG_LEVEL_DEBUG);
60
61         // Register pool
62
ProxoolFacade.registerConnectionPool(url, info);
63
64         {
65             Connection JavaDoc connection = DriverManager.getConnection(url);
66             connection.createStatement().execute(TestConstants.HYPERSONIC_TEST_SQL);
67             connection.close();
68
69             SnapshotIF snapshot = ProxoolFacade.getSnapshot(alias, true);
70
71             assertEquals("servedCount", 1L, snapshot.getServedCount());
72             assertEquals("refusedCount", 0L, snapshot.getRefusedCount());
73             assertEquals("activeConnectionCount", 0, snapshot.getActiveConnectionCount());
74
75             ConnectionInfoIF[] connectionInfos = snapshot.getConnectionInfos();
76             assertTrue("connectionInfos.length != 0", connectionInfos.length != 0);
77             assertEquals("connectionInfos activeCount", 0, getCount(connectionInfos, ConnectionInfoIF.STATUS_ACTIVE));
78             assertEquals("connectionInfos sql count", 1, connectionInfos[0].getSqlCalls().length);
79             assertEquals("connectionInfos lastSql", TestConstants.HYPERSONIC_TEST_SQL, connectionInfos[0].getSqlCalls()[0].replace(';', ' ').trim());
80         }
81
82         {
83             Connection JavaDoc connection = DriverManager.getConnection(url);
84             connection.createStatement().execute(TestConstants.HYPERSONIC_TEST_SQL);
85             connection.createStatement().execute(TestConstants.HYPERSONIC_TEST_SQL_2);
86
87             SnapshotIF snapshot = ProxoolFacade.getSnapshot(alias, true);
88
89             assertEquals("servedCount", 2L, snapshot.getServedCount());
90             assertEquals("refusedCount", 0L, snapshot.getRefusedCount());
91             assertEquals("activeConnectionCount", 1, snapshot.getActiveConnectionCount());
92
93             ConnectionInfoIF[] connectionInfos = snapshot.getConnectionInfos();
94             assertTrue("connectionInfos.length != 0", connectionInfos.length != 0);
95             assertEquals("connectionInfos activeCount", 1, getCount(connectionInfos, ConnectionInfoIF.STATUS_ACTIVE));
96             assertEquals("connectionInfos sql count", 2, connectionInfos[0].getSqlCalls().length);
97             assertEquals("connectionInfos lastSql", TestConstants.HYPERSONIC_TEST_SQL, connectionInfos[0].getSqlCalls()[0].replace(';', ' ').trim());
98             assertEquals("connectionInfos lastSql", TestConstants.HYPERSONIC_TEST_SQL_2, connectionInfos[0].getSqlCalls()[1].replace(';', ' ').trim());
99
100             connection.close();
101         }
102
103     }
104
105     private int getCount(ConnectionInfoIF[] connectionInfos, int status) {
106         int count = 0;
107         for (int i = 0; i < connectionInfos.length; i++) {
108             if (connectionInfos[i].getStatus() == status) {
109                 count++;
110             }
111         }
112         return count;
113     }
114
115 }
116
117 /*
118  Revision history:
119  $Log: SnapshotTest.java,v $
120  Revision 1.14 2006/01/18 14:40:05 billhorsman
121  Unbundled Jakarta's Commons Logging.
122
123  Revision 1.13 2005/10/07 08:13:46 billhorsman
124  Test new sqlCalls property of snapshot
125
126  Revision 1.12 2003/03/10 23:46:43 billhorsman
127  checkstyle
128
129  Revision 1.11 2003/03/06 14:25:53 billhorsman
130  fix for threading
131
132  Revision 1.10 2003/03/06 11:31:17 billhorsman
133  fix for unlikely prototyper situation
134
135  Revision 1.8 2003/03/04 10:58:44 billhorsman
136  checkstyle
137
138  Revision 1.7 2003/03/04 10:24:40 billhorsman
139  removed try blocks around each test
140
141  Revision 1.6 2003/03/03 17:09:08 billhorsman
142  all tests now extend AbstractProxoolTest
143
144  Revision 1.5 2003/03/03 11:12:05 billhorsman
145  fixed licence
146
147  Revision 1.4 2003/03/01 15:27:25 billhorsman
148  checkstyle
149
150  Revision 1.3 2003/02/27 18:01:48 billhorsman
151  completely rethought the test structure. it's now
152  more obvious. no new tests yet though.
153
154  Revision 1.2 2003/02/26 16:05:51 billhorsman
155  widespread changes caused by refactoring the way we
156  update and redefine pool definitions.
157
158  Revision 1.1 2003/02/20 00:33:15 billhorsman
159  renamed monitor package -> admin
160
161  Revision 1.3 2003/02/19 23:36:50 billhorsman
162  renamed monitor package to admin
163
164  Revision 1.2 2003/02/19 15:14:29 billhorsman
165  fixed copyright (copy and paste error,
166  not copyright change)
167
168  Revision 1.1 2003/02/07 17:28:36 billhorsman
169  *** empty log message ***
170
171  */

172
Popular Tags