1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.util.Properties ; 12 import java.sql.DriverManager ; 13 14 24 public class ConfigurationListenerTest extends AbstractProxoolTest { 25 26 private static final Log LOG = LogFactory.getLog(ConfigurationListenerTest.class); 27 28 31 public ConfigurationListenerTest(String s) { 32 super(s); 33 } 34 35 40 public void testAddAndRemove() throws Exception { 41 42 String testName = "addAndRemove"; 43 String alias = testName; 44 45 String url = TestHelper.buildProxoolUrl(alias, 47 TestConstants.HYPERSONIC_DRIVER, 48 TestConstants.HYPERSONIC_TEST_URL); 49 Properties info = new Properties (); 50 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 51 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 52 ProxoolFacade.registerConnectionPool(url, info); 53 54 MyConfigurationListener mcl1 = new MyConfigurationListener(); 56 ProxoolFacade.addConfigurationListener(alias, mcl1); 57 58 Properties newInfo = new Properties (); 60 newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString()); 61 ProxoolFacade.updateConnectionPool(url, newInfo); 62 assertEquals("definitionReceived", true, mcl1.isUpdateReceived()); 63 mcl1.reset(); 64 65 MyConfigurationListener mcl2 = new MyConfigurationListener(); 67 ProxoolFacade.addConfigurationListener(alias, mcl2); 68 69 newInfo = new Properties (); 71 newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.FALSE.toString()); 72 ProxoolFacade.updateConnectionPool(url, newInfo); 73 assertEquals("definitionReceived", true, mcl1.isUpdateReceived()); 74 assertEquals("definitionReceived", true, mcl2.isUpdateReceived()); 75 mcl1.reset(); 76 mcl2.reset(); 77 78 ProxoolFacade.removeConfigurationListener(alias, mcl1); 80 81 newInfo = new Properties (); 83 newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString()); 84 ProxoolFacade.updateConnectionPool(url, newInfo); 85 assertEquals("definitionReceived", false, mcl1.isUpdateReceived()); 86 assertEquals("definitionReceived", true, mcl2.isUpdateReceived()); 87 mcl1.reset(); 88 mcl2.reset(); 89 90 DriverManager.getConnection(url).close(); 93 assertEquals("definitionReceived", false, mcl2.isUpdateReceived()); 94 mcl2.reset(); 95 96 LOG.debug("Getting another connection to trigger any pending config changes"); 99 DriverManager.getConnection(url, info).close(); 100 mcl2.reset(); 101 LOG.debug("Getting another connection which shouldn't cause another config change"); 102 DriverManager.getConnection(url, info).close(); 103 assertEquals("definitionReceived", false, mcl2.isUpdateReceived()); 104 mcl2.reset(); 105 106 LOG.debug("Expecting a change now"); 108 info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.FALSE.toString()); 109 DriverManager.getConnection(url, info).close(); 110 assertEquals("definitionReceived", true, mcl2.isUpdateReceived()); 111 mcl2.reset(); 112 113 ProxoolFacade.removeConfigurationListener(alias, mcl2); 115 116 newInfo = new Properties (); 118 newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.FALSE.toString()); 119 ProxoolFacade.updateConnectionPool(url, newInfo); 120 assertEquals("definitionReceived", false, mcl1.isUpdateReceived()); 121 assertEquals("definitionReceived", false, mcl2.isUpdateReceived()); 122 mcl1.reset(); 123 mcl2.reset(); 124 125 } 126 127 132 public void testConfigurationListeners() throws Exception { 133 134 String testName = "configurationListener"; 135 String alias = testName; 136 137 final String delegateUrl1 = TestConstants.HYPERSONIC_TEST_URL; 139 final String delegateUrl2 = TestConstants.HYPERSONIC_TEST_URL2; 140 141 final String url1 = TestHelper.buildProxoolUrl(alias, 142 TestConstants.HYPERSONIC_DRIVER, 143 delegateUrl1); 144 final String url2 = TestHelper.buildProxoolUrl(alias, 145 TestConstants.HYPERSONIC_DRIVER, 146 delegateUrl2); 147 148 Properties info = new Properties (); 149 info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER); 150 info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); 151 ProxoolFacade.registerConnectionPool(url1, info); 152 153 int propertyCount = info.size(); 154 155 MyConfigurationListener mcl = new MyConfigurationListener(); 157 ProxoolFacade.addConfigurationListener(alias, mcl); 158 159 ProxoolFacade.updateConnectionPool(url2, null); 161 LOG.debug("changed: " + mcl.getChangedInfo()); 162 LOG.debug("complete: " + mcl.getCompleteInfo()); 163 assertEquals("changed size", 0, mcl.getChangedInfo().size()); 164 assertEquals("complete size", propertyCount, mcl.getCompleteInfo().size()); 165 assertEquals("url", delegateUrl2, mcl.getConnectionPoolDefinition().getUrl()); 166 mcl.reset(); 167 168 Properties newInfo = new Properties (); 170 newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.TRUE.toString()); 171 ProxoolFacade.updateConnectionPool(url2, newInfo); 172 LOG.debug("changed: " + mcl.getChangedInfo()); 173 LOG.debug("complete: " + mcl.getCompleteInfo()); 174 assertEquals("completeInfo size", propertyCount + 1, mcl.getCompleteInfo().size()); 175 assertEquals("changedInfo size", 1, mcl.getChangedInfo().size()); 176 assertEquals("url", true, mcl.getConnectionPoolDefinition().isVerbose()); 177 mcl.reset(); 178 179 newInfo = new Properties (); 181 newInfo.setProperty(ProxoolConstants.VERBOSE_PROPERTY, Boolean.FALSE.toString()); 182 ProxoolFacade.updateConnectionPool(url2, newInfo); 183 LOG.debug("changed: " + mcl.getChangedInfo()); 184 LOG.debug("complete: " + mcl.getCompleteInfo()); 185 assertEquals("completeInfo size", propertyCount + 1, mcl.getCompleteInfo().size()); 186 assertEquals("changedInfo size", 1, mcl.getChangedInfo().size()); 187 assertEquals("url", false, mcl.getConnectionPoolDefinition().isVerbose()); 188 mcl.reset(); 189 190 } 191 192 class MyConfigurationListener implements ConfigurationListenerIF { 193 194 private Properties completeInfo; 195 196 private Properties changedInfo; 197 198 private ConnectionPoolDefinitionIF connectionPoolDefinition; 199 200 private boolean updateReceived; 201 202 public void definitionUpdated(ConnectionPoolDefinitionIF connectionPoolDefinition, Properties completeInfo, Properties changedInfo) { 203 this.connectionPoolDefinition = connectionPoolDefinition; 204 this.completeInfo = completeInfo; 205 this.changedInfo = changedInfo; 206 updateReceived = true; 207 } 208 209 public Properties getCompleteInfo() { 210 return completeInfo; 211 } 212 213 public Properties getChangedInfo() { 214 return changedInfo; 215 } 216 217 public ConnectionPoolDefinitionIF getConnectionPoolDefinition() { 218 return connectionPoolDefinition; 219 } 220 221 public boolean isUpdateReceived() { 222 return updateReceived; 223 } 224 225 public void reset() { 226 completeInfo.clear(); 227 changedInfo.clear(); 228 updateReceived = false; 229 } 230 231 } 232 233 } 234 235 290 | Popular Tags |