1 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 ; 18 import java.sql.DriverManager ; 19 import java.sql.Statement ; 20 import java.util.Properties ; 21 22 30 public class SnapshotTest extends AbstractProxoolTest { 31 32 private static final Log LOG = LogFactory.getLog(SnapshotTest.class); 33 34 37 public SnapshotTest(String s) { 38 super(s); 39 } 40 41 44 public void testSnapshot() throws Exception { 45 46 String testName = "snapshot"; 47 final String alias = testName; 48 String url = TestHelper.buildProxoolUrl(alias, 49 TestConstants.HYPERSONIC_DRIVER, 50 TestConstants.HYPERSONIC_TEST_URL); 51 Properties info = new Properties (); 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 info.setProperty(ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY, ProxoolConstants.STATISTICS_LOG_LEVEL_DEBUG); 60 61 ProxoolFacade.registerConnectionPool(url, info); 63 64 { 65 Connection 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 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 172 | Popular Tags |