1 package org.jboss.test.jca.test; 2 3 import java.net.URL ; 4 import java.sql.Connection ; 5 6 import javax.management.ObjectName ; 7 import javax.naming.InitialContext ; 8 import javax.sql.DataSource ; 9 10 import junit.framework.Test; 11 12 import org.jboss.logging.Logger; 13 import org.jboss.mx.util.ObjectNameFactory; 14 import org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory; 15 import org.jboss.resource.connectionmanager.ConnectionListener; 16 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool; 17 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool; 18 import org.jboss.resource.connectionmanager.ManagedConnectionPool; 19 import org.jboss.resource.connectionmanager.NoTxConnectionManager; 20 import org.jboss.resource.connectionmanager.PreFillPoolSupport; 21 import org.jboss.test.JBossTestCase; 22 import org.jboss.test.jca.jdbc.TestDriver; 23 import org.jboss.test.jca.support.PoolHelper; 24 25 public class BackgroundValidationUnitTestCase extends JBossTestCase 26 { 27 Logger log = Logger.getLogger(PreFillPoolingUnitTestCase.class); 28 29 private static ObjectName INVALID_BACKGROUND_POOL = ObjectNameFactory.create("jboss.jca:name=TestFailedBackgroundDS,service=ManagedConnectionPool"); 30 private static ObjectName VALID_BACKGROUND_POOL = ObjectNameFactory.create("jboss.jca:name=TestSuccessBackgroundDS,service=ManagedConnectionPool"); 31 private static ObjectName INVALID_MATCH_POOL = ObjectNameFactory.create("jboss.jca:name=TestNonMatchDS,service=ManagedConnectionPool"); 32 private static ObjectName VALID_MATCH_POOL = ObjectNameFactory.create("jboss.jca:name=TestValidationMatchDS,service=ManagedConnectionPool"); 33 34 private PoolHelper helper; 35 36 public BackgroundValidationUnitTestCase(String name) throws Exception 37 { 38 super(name); 39 helper = PoolHelper.getInstance(getServer(), INVALID_BACKGROUND_POOL); 40 41 } 42 43 58 public void testDeployedBackgroundValidationFailure() throws Exception 59 { 60 61 InitialContext ctx = super.getInitialContext(); 62 DataSource ds = (DataSource ) ctx.lookup("TestFailedBackgroundDS"); 63 ds.getConnection("sa", "").close(); 64 65 Integer minCount = helper.getMinSize(); 66 Long backMin = helper.getBackgroundValMinutes(); 67 68 PoolHelper.sleepForValidation(backMin.intValue() * 1000 * 60); 69 70 Integer destroyedCount = helper.getDestroyed(); 71 72 assertTrue("Background validation ran. Destroyed count should exceed zero.", destroyedCount.intValue() > 0); 73 74 Integer connCount = helper.getConnectionCount(); 75 76 assertTrue(connCount.intValue() == minCount.intValue()); 77 78 helper.setPoolAttributeAndFlush(PoolHelper.POOL_ATT_BACKGROUND_VAL, Boolean.FALSE); 79 80 81 ds.getConnection().close(); 83 84 PoolHelper.sleepForValidation(backMin.intValue() * 1000 * 60); 85 86 destroyedCount = helper.getDestroyed(); 87 connCount = helper.getConnectionCount(); 88 89 assertTrue("Background validation is disabled. Destroyed count should be zero", destroyedCount.intValue() == 0); 90 assertTrue("Background validation is disabled. Pool should be filled to min", connCount.intValue() == minCount.intValue()); 91 92 93 } 94 95 public void testDeployedBackgroundValidationSuccess() throws Exception 96 { 97 InitialContext ctx = super.getInitialContext(); 98 DataSource ds = (DataSource ) ctx.lookup("TestSuccessBackgroundDS"); 99 ds.getConnection("sa", "").close(); 100 101 Integer minCount = PoolHelper.getMinSize(getServer(), VALID_BACKGROUND_POOL); 102 Long backMin = PoolHelper.getBackgroundValMinutes(getServer(), VALID_BACKGROUND_POOL); 103 104 PoolHelper.sleepForValidation(backMin.intValue() * 1000 * 60); 105 106 Integer destroyedCount = PoolHelper.getDestroyed(getServer(), VALID_BACKGROUND_POOL); 107 108 109 assertTrue("Background validation ran on valid pool. Destroyed count should not exceed zero.", destroyedCount.intValue() == 0); 110 111 112 } 113 114 115 121 public void testDeployedNonValidateOnMatch() throws Exception 122 { 123 124 InitialContext ctx = super.getInitialContext(); 125 DataSource ds = (DataSource ) ctx.lookup("TestNonMatchDS"); 126 ds.getConnection("sa", "").close(); 127 128 Integer destroyed = PoolHelper.getDestroyed(getServer(), INVALID_MATCH_POOL); 129 130 assertTrue("Validation should not have occured at this point.", destroyed.intValue() == 0); 131 132 ds.getConnection("sa", "").close(); 135 136 destroyed = PoolHelper.getDestroyed(getServer(), INVALID_MATCH_POOL); 137 138 assertTrue( 139 "Validation on match is set to true for invalid connections. Destroyed count should be greater than zero.", 140 destroyed.intValue() > 0); 141 142 143 } 144 150 public void testDeployedValidateOnMatch() throws Exception 151 { 152 153 InitialContext ctx = super.getInitialContext(); 154 DataSource ds = (DataSource ) ctx.lookup("TestValidationMatchDS"); 155 ds.getConnection("sa", "").close(); 156 157 Integer destroyed = PoolHelper.getDestroyed(getServer(), VALID_MATCH_POOL); 158 159 assertTrue("Validation should not have occured at this point.", destroyed.intValue() == 0); 160 161 ds.getConnection("sa", "").close(); 162 163 destroyed = PoolHelper.getDestroyed(getServer(), VALID_MATCH_POOL); 164 165 assertTrue("Validation on match is set to true for connections. Destroyed count should be zero.", destroyed 166 .intValue() == 0); 167 168 169 170 171 } 172 public void testValidateOnMatchSuccess() throws Exception 173 { 174 LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory(); 175 176 mcf.setDriverClass("org.jboss.test.jca.jdbc.TestDriver"); 177 mcf.setConnectionURL("jdbc:jboss-test-adapter"); 178 mcf.setValidConnectionCheckerClassName("org.jboss.test.jca.support.MockSuccessValidationConnectionChecker"); 179 mcf.setValidateOnMatch(true); 180 181 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 182 pp.minSize = 1; 183 pp.maxSize = 2; 184 pp.blockingTimeout = 10000; 185 pp.idleTimeout = 0; 186 pp.prefill = true; 187 pp.backgroundValidation = false; 188 pp.backgroundInterval = 1 * 1000 * 30; 189 ManagedConnectionPool mcp = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log); 190 NoTxConnectionManager noTxn = new NoTxConnectionManager(null, mcp); 191 mcp.setConnectionListenerFactory(noTxn); 192 193 ((PreFillPoolSupport)mcp).prefill(); 194 195 Thread.sleep(5000); 197 198 ConnectionListener cl = noTxn.getManagedConnection(null, null); 199 noTxn.returnManagedConnection(cl, false); 200 201 assertTrue(mcp.getConnectionDestroyedCount() == 0); 202 203 204 } 205 public void testBasicValidateOnMatchFailure() throws Exception 206 { 207 LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory(); 208 209 mcf.setDriverClass("org.jboss.test.jca.jdbc.TestDriver"); 210 mcf.setConnectionURL("jdbc:jboss-test-adapter"); 211 mcf.setValidConnectionCheckerClassName("org.jboss.test.jca.support.MockFailedValidationConnectionChecker"); 212 mcf.setValidateOnMatch(true); 213 214 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 215 pp.minSize = 1; 216 pp.maxSize = 2; 217 pp.blockingTimeout = 10000; 218 pp.idleTimeout = 0; 219 pp.prefill = true; 220 pp.backgroundValidation = false; 221 pp.backgroundInterval = 1 * 1000 * 30; 222 ManagedConnectionPool mcp = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log); 223 NoTxConnectionManager noTxn = new NoTxConnectionManager(null, mcp); 224 mcp.setConnectionListenerFactory(noTxn); 225 226 ((PreFillPoolSupport)mcp).prefill(); 227 228 Thread.sleep(5000); 230 231 ConnectionListener cl = noTxn.getManagedConnection(null, null); 232 noTxn.returnManagedConnection(cl, false); 233 234 assertTrue(mcp.getConnectionDestroyedCount() > 0); 235 236 } 237 238 239 public void testBasicBackgroundValidationSuccess() throws Exception 240 { 241 242 LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory(); 243 244 mcf.setDriverClass("org.jboss.test.jca.jdbc.TestDriver"); 245 mcf.setConnectionURL("jdbc:jboss-test-adapter"); 246 mcf.setValidConnectionCheckerClassName("org.jboss.test.jca.support.MockSuccessValidationConnectionChecker"); 247 248 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 249 pp.minSize = 1; 250 pp.maxSize = 2; 251 pp.blockingTimeout = 10000; 252 pp.idleTimeout = 0; 253 pp.prefill = false; 254 pp.backgroundValidation = true; 255 pp.backgroundInterval = 1 * 1000 * 30; 256 ManagedConnectionPool mcp = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log); 257 NoTxConnectionManager noTxn = new NoTxConnectionManager(null, mcp); 258 mcp.setConnectionListenerFactory(noTxn); 259 260 ConnectionListener cl = noTxn.getManagedConnection(null, null); 261 noTxn.returnManagedConnection(cl, false); 262 263 Thread.sleep(pp.backgroundInterval); 264 assertTrue(mcp.getConnectionDestroyedCount() == 0); 265 266 } 267 public void testBasicBackgroundValidationDestroy() throws Exception 268 { 269 270 LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory(); 271 272 mcf.setDriverClass("org.jboss.test.jca.jdbc.TestDriver"); 273 mcf.setConnectionURL("jdbc:jboss-test-adapter"); 274 mcf.setValidConnectionCheckerClassName("org.jboss.test.jca.support.MockFailedValidationConnectionChecker"); 275 276 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 277 pp.minSize = 1; 278 pp.maxSize = 2; 279 pp.blockingTimeout = 10000; 280 pp.idleTimeout = 0; 281 pp.prefill = false; 282 pp.backgroundValidation = true; 283 pp.backgroundInterval = 1 * 1000 * 30; 284 ManagedConnectionPool mcp = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log); 285 NoTxConnectionManager noTxn = new NoTxConnectionManager(null, mcp); 286 mcp.setConnectionListenerFactory(noTxn); 287 288 ConnectionListener cl = noTxn.getManagedConnection(null, null); 289 noTxn.returnManagedConnection(cl, false); 290 291 Thread.sleep(pp.backgroundInterval); 292 assertTrue(mcp.getConnectionDestroyedCount() > 0); 293 294 } 295 296 public static Test suite() throws Exception { 297 Test test1 = getDeploySetup(BackgroundValidationUnitTestCase.class, "jca-support.sar"); 298 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 299 URL resURL = loader.getResource("jca/validation/test-background-failed-validation-ds.xml"); 300 Test test2 = getDeploySetup(test1, resURL.toString()); 301 resURL = loader.getResource("jca/validation/test-background-success-validation-ds.xml"); 302 Test test3 = getDeploySetup(test2, resURL.toString()); 303 resURL = loader.getResource("jca/validation/test-non-validation-match-ds.xml"); 304 Test test4 = getDeploySetup(test3, resURL.toString()); 305 resURL = loader.getResource("jca/validation/test-validation-match-ds.xml"); 306 return getDeploySetup(test4, resURL.toString()); 307 308 309 } 310 311 } 312 | Popular Tags |