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.ConnectionInfoIF;
012: import org.logicalcobwebs.proxool.ProxoolConstants;
013: import org.logicalcobwebs.proxool.ProxoolFacade;
014: import org.logicalcobwebs.proxool.TestConstants;
015: import org.logicalcobwebs.proxool.TestHelper;
016:
017: import java.sql.Connection;
018: import java.sql.DriverManager;
019: import java.sql.Statement;
020: import java.util.Properties;
021:
022: /**
023: * Test {@link SnapshotIF}
024: *
025: * @version $Revision: 1.14 $, $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 SnapshotTest extends AbstractProxoolTest {
031:
032: private static final Log LOG = LogFactory
033: .getLog(SnapshotTest.class);
034:
035: /**
036: * @see junit.framework.TestCase#TestCase
037: */
038: public SnapshotTest(String s) {
039: super (s);
040: }
041:
042: /**
043: * Test whether the statistics we get back are roughly right.
044: */
045: public void testSnapshot() throws Exception {
046:
047: String testName = "snapshot";
048: final String alias = testName;
049: String url = TestHelper.buildProxoolUrl(alias,
050: TestConstants.HYPERSONIC_DRIVER,
051: TestConstants.HYPERSONIC_TEST_URL);
052: Properties info = new Properties();
053: info.setProperty(ProxoolConstants.USER_PROPERTY,
054: TestConstants.HYPERSONIC_USER);
055: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
056: TestConstants.HYPERSONIC_PASSWORD);
057: info.setProperty(ProxoolConstants.STATISTICS_PROPERTY,
058: "10s,15s");
059: info
060: .setProperty(
061: ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
062: "1");
063: info.setProperty(ProxoolConstants.TRACE_PROPERTY, "true");
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: {
074: Connection connection = DriverManager.getConnection(url);
075: connection.createStatement().execute(
076: TestConstants.HYPERSONIC_TEST_SQL);
077: connection.close();
078:
079: SnapshotIF snapshot = ProxoolFacade
080: .getSnapshot(alias, true);
081:
082: assertEquals("servedCount", 1L, snapshot.getServedCount());
083: assertEquals("refusedCount", 0L, snapshot.getRefusedCount());
084: assertEquals("activeConnectionCount", 0, snapshot
085: .getActiveConnectionCount());
086:
087: ConnectionInfoIF[] connectionInfos = snapshot
088: .getConnectionInfos();
089: assertTrue("connectionInfos.length != 0",
090: connectionInfos.length != 0);
091: assertEquals("connectionInfos activeCount", 0, getCount(
092: connectionInfos, ConnectionInfoIF.STATUS_ACTIVE));
093: assertEquals("connectionInfos sql count", 1,
094: connectionInfos[0].getSqlCalls().length);
095: assertEquals("connectionInfos lastSql",
096: TestConstants.HYPERSONIC_TEST_SQL,
097: connectionInfos[0].getSqlCalls()[0].replace(';',
098: ' ').trim());
099: }
100:
101: {
102: Connection connection = DriverManager.getConnection(url);
103: connection.createStatement().execute(
104: TestConstants.HYPERSONIC_TEST_SQL);
105: connection.createStatement().execute(
106: TestConstants.HYPERSONIC_TEST_SQL_2);
107:
108: SnapshotIF snapshot = ProxoolFacade
109: .getSnapshot(alias, true);
110:
111: assertEquals("servedCount", 2L, snapshot.getServedCount());
112: assertEquals("refusedCount", 0L, snapshot.getRefusedCount());
113: assertEquals("activeConnectionCount", 1, snapshot
114: .getActiveConnectionCount());
115:
116: ConnectionInfoIF[] connectionInfos = snapshot
117: .getConnectionInfos();
118: assertTrue("connectionInfos.length != 0",
119: connectionInfos.length != 0);
120: assertEquals("connectionInfos activeCount", 1, getCount(
121: connectionInfos, ConnectionInfoIF.STATUS_ACTIVE));
122: assertEquals("connectionInfos sql count", 2,
123: connectionInfos[0].getSqlCalls().length);
124: assertEquals("connectionInfos lastSql",
125: TestConstants.HYPERSONIC_TEST_SQL,
126: connectionInfos[0].getSqlCalls()[0].replace(';',
127: ' ').trim());
128: assertEquals("connectionInfos lastSql",
129: TestConstants.HYPERSONIC_TEST_SQL_2,
130: connectionInfos[0].getSqlCalls()[1].replace(';',
131: ' ').trim());
132:
133: connection.close();
134: }
135:
136: }
137:
138: private int getCount(ConnectionInfoIF[] connectionInfos, int status) {
139: int count = 0;
140: for (int i = 0; i < connectionInfos.length; i++) {
141: if (connectionInfos[i].getStatus() == status) {
142: count++;
143: }
144: }
145: return count;
146: }
147:
148: }
149:
150: /*
151: Revision history:
152: $Log: SnapshotTest.java,v $
153: Revision 1.14 2006/01/18 14:40:05 billhorsman
154: Unbundled Jakarta's Commons Logging.
155:
156: Revision 1.13 2005/10/07 08:13:46 billhorsman
157: Test new sqlCalls property of snapshot
158:
159: Revision 1.12 2003/03/10 23:46:43 billhorsman
160: checkstyle
161:
162: Revision 1.11 2003/03/06 14:25:53 billhorsman
163: fix for threading
164:
165: Revision 1.10 2003/03/06 11:31:17 billhorsman
166: fix for unlikely prototyper situation
167:
168: Revision 1.8 2003/03/04 10:58:44 billhorsman
169: checkstyle
170:
171: Revision 1.7 2003/03/04 10:24:40 billhorsman
172: removed try blocks around each test
173:
174: Revision 1.6 2003/03/03 17:09:08 billhorsman
175: all tests now extend AbstractProxoolTest
176:
177: Revision 1.5 2003/03/03 11:12:05 billhorsman
178: fixed licence
179:
180: Revision 1.4 2003/03/01 15:27:25 billhorsman
181: checkstyle
182:
183: Revision 1.3 2003/02/27 18:01:48 billhorsman
184: completely rethought the test structure. it's now
185: more obvious. no new tests yet though.
186:
187: Revision 1.2 2003/02/26 16:05:51 billhorsman
188: widespread changes caused by refactoring the way we
189: update and redefine pool definitions.
190:
191: Revision 1.1 2003/02/20 00:33:15 billhorsman
192: renamed monitor package -> admin
193:
194: Revision 1.3 2003/02/19 23:36:50 billhorsman
195: renamed monitor package to admin
196:
197: Revision 1.2 2003/02/19 15:14:29 billhorsman
198: fixed copyright (copy and paste error,
199: not copyright change)
200:
201: Revision 1.1 2003/02/07 17:28:36 billhorsman
202: *** empty log message ***
203:
204: */
|