1 package org.jboss.test.jca.test; 2 3 import java.net.URL ; 4 import java.sql.Connection ; 5 6 import javax.management.Attribute ; 7 import javax.management.ObjectName ; 8 import javax.naming.InitialContext ; 9 import javax.resource.spi.ManagedConnectionFactory ; 10 import javax.sql.DataSource ; 11 import javax.transaction.TransactionManager ; 12 13 import junit.framework.Test; 14 15 import org.jboss.logging.Logger; 16 import org.jboss.mx.util.ObjectNameFactory; 17 import org.jboss.resource.connectionmanager.BaseConnectionManager2; 18 import org.jboss.resource.connectionmanager.CachedConnectionManager; 19 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool; 20 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool; 21 import org.jboss.resource.connectionmanager.ManagedConnectionPool; 22 import org.jboss.resource.connectionmanager.NoTxConnectionManager; 23 import org.jboss.resource.connectionmanager.PreFillPoolSupport; 24 import org.jboss.resource.connectionmanager.TransactionSynchronizer; 25 import org.jboss.test.JBossTestCase; 26 import org.jboss.test.jca.adapter.TestConnectionRequestInfo; 27 import org.jboss.test.jca.adapter.TestManagedConnectionFactory; 28 import org.jboss.tm.TransactionManagerLocator; 29 30 36 public class PreFillPoolingUnitTestCase extends JBossTestCase 37 { 38 Logger log = Logger.getLogger(PreFillPoolingUnitTestCase.class); 39 40 private static final ObjectName PREFILL_POOL = ObjectNameFactory.create("jboss.jca:name=PreFillDS,service=ManagedConnectionPool"); 41 private static final ObjectName NO_PREFILL_POOL = ObjectNameFactory.create("jboss.jca:name=NoPreFillDS,service=ManagedConnectionPool"); 42 private static final ObjectName NO_ELEMENT_PREFILL_POOL = ObjectNameFactory.create("jboss.jca:name=NoElementPreFillDS,service=ManagedConnectionPool"); 43 private static final ObjectName CRI_PREFILL_POOL = ObjectNameFactory.create("jboss.jca:name=CriPreFillDS,service=ManagedConnectionPool"); 44 private static final ObjectName INVALD_PREFILL_POOL = ObjectNameFactory.create("jboss.jca:name=InvalidPreFillDS,service=ManagedConnectionPool"); 45 46 private static final String CONN_NAME = "ConnectionCount"; 47 private static final String MIN_NAME = "MinSize"; 48 private static final String FLUSH_METHOD_NAME = "flush"; 49 private static final String PREFILL = "PreFill"; 50 51 static CachedConnectionManager ccm = new CachedConnectionManager(); 53 static TransactionManager tm = TransactionManagerLocator.getInstance().locate(); 54 static TestConnectionRequestInfo cri1 = new TestConnectionRequestInfo("info1"); 55 static TestConnectionRequestInfo cri2 = new TestConnectionRequestInfo("info2"); 56 57 public PreFillPoolingUnitTestCase(String name) 58 { 59 super(name); 60 } 61 62 73 public void testDeployPreFillPool() throws Exception 74 { 75 Boolean prefill = (Boolean )getServer().getAttribute(PREFILL_POOL, PREFILL); 76 assertTrue(prefill.booleanValue()); 77 78 Integer count = (Integer )getServer().getAttribute(PREFILL_POOL, CONN_NAME); 79 Integer min = (Integer )getServer().getAttribute(PREFILL_POOL, MIN_NAME); 80 81 assertTrue("Minimun count and connection count should be the same.", count.intValue() == min.intValue()); 82 83 84 } 85 86 97 public void testPreFillFlush() throws Exception 98 { 99 100 Boolean prefill = (Boolean )getServer().getAttribute(PREFILL_POOL, PREFILL); 101 assertTrue(prefill.booleanValue()); 102 103 Integer count = (Integer )getServer().getAttribute(PREFILL_POOL, CONN_NAME); 104 Integer min = (Integer )getServer().getAttribute(PREFILL_POOL, MIN_NAME); 105 assertTrue("Prefill is set to true. Minimun count and connection count should be the same.", count.intValue() == min.intValue()); 106 107 Attribute flush = new Attribute (PREFILL, Boolean.FALSE); 108 getServer().setAttribute(PREFILL_POOL, flush); 109 getServer().invoke(PREFILL_POOL, FLUSH_METHOD_NAME, new Object [0], new String [0]); 110 111 count = (Integer )getServer().getAttribute(PREFILL_POOL, CONN_NAME); 112 min = (Integer )getServer().getAttribute(PREFILL_POOL, MIN_NAME); 113 assertTrue("Pool was flushed and prefill set to false. Minimum count and connection count should not be equal.", count.intValue() != min.intValue()); 114 115 flush = new Attribute (PREFILL, Boolean.TRUE); 116 getServer().setAttribute(PREFILL_POOL, flush); 117 getServer().invoke(PREFILL_POOL, FLUSH_METHOD_NAME, new Object [0], new String [0]); 118 119 Thread.sleep(2000); 121 122 count = (Integer )getServer().getAttribute(PREFILL_POOL, CONN_NAME); 123 min = (Integer )getServer().getAttribute(PREFILL_POOL, MIN_NAME); 124 assertTrue("Prefill is set to true. Minimun count and connection count should be the same.", count.intValue() == min.intValue()); 125 126 127 } 128 129 135 public void testDeployNoPreFillPool() throws Exception 136 { 137 Boolean prefill = (Boolean )getServer().getAttribute(NO_PREFILL_POOL, PREFILL); 138 assertFalse("Prefill is set to false.", prefill.booleanValue()); 139 140 Integer count = (Integer )getServer().getAttribute(NO_PREFILL_POOL, CONN_NAME); 141 Integer min = (Integer )getServer().getAttribute(NO_PREFILL_POOL, MIN_NAME); 142 assertTrue("Prefill is set to false. Min count and connection count should not be equal", count.intValue() != min.intValue()); 143 144 145 } 146 147 152 public void testDeployElementNoPreFillPool() throws Exception 153 { 154 Boolean prefill = (Boolean )getServer().getAttribute(NO_ELEMENT_PREFILL_POOL, PREFILL); 155 assertFalse("Prefill was explicitly set ot false. Prefill should be false.", prefill.booleanValue()); 156 157 Integer count = (Integer )getServer().getAttribute(NO_ELEMENT_PREFILL_POOL, CONN_NAME); 158 Integer min = (Integer )getServer().getAttribute(NO_ELEMENT_PREFILL_POOL, MIN_NAME); 159 160 assertTrue("Prefill is set to false. Min count and connection count should not be equal", count.intValue() != min.intValue()); 161 162 } 163 164 169 public void testInvalidPreFillPool() throws Exception 170 { 171 Boolean prefill = (Boolean )getServer().getAttribute(INVALD_PREFILL_POOL, PREFILL); 172 assertTrue(prefill.booleanValue()); 173 174 Integer count = (Integer )getServer().getAttribute(INVALD_PREFILL_POOL, CONN_NAME); 175 Integer min = (Integer )getServer().getAttribute(INVALD_PREFILL_POOL, MIN_NAME); 176 assertTrue("Non supporting prefill pool is used. Min count and connection count should not be equal", count.intValue() != min.intValue()); 177 178 } 179 180 185 public void testPoolByCriPreFill() throws Exception 186 { 187 Boolean prefill = (Boolean )getServer().getAttribute(CRI_PREFILL_POOL, PREFILL); 188 assertTrue(prefill.booleanValue()); 189 190 Integer count = (Integer )getServer().getAttribute(CRI_PREFILL_POOL, CONN_NAME); 191 Integer min = (Integer )getServer().getAttribute(CRI_PREFILL_POOL, MIN_NAME); 192 193 assertTrue(count.intValue() != min.intValue()); 194 195 InitialContext ctx = super.getInitialContext(); 196 DataSource ds = (DataSource )ctx.lookup("CriPreFillDS"); 197 Connection conn = ds.getConnection("sa", ""); 198 199 count = (Integer )getServer().getAttribute(CRI_PREFILL_POOL, CONN_NAME); 200 min = (Integer )getServer().getAttribute(CRI_PREFILL_POOL, MIN_NAME); 201 202 assertTrue(count.intValue() == min.intValue()); 203 204 conn.close(); 205 206 Attribute flush = new Attribute (PREFILL, Boolean.TRUE); 208 getServer().setAttribute(PREFILL_POOL, flush); 209 getServer().invoke(CRI_PREFILL_POOL, FLUSH_METHOD_NAME, new Object [0], new String [0]); 210 211 count = (Integer )getServer().getAttribute(CRI_PREFILL_POOL, CONN_NAME); 212 min = (Integer )getServer().getAttribute(CRI_PREFILL_POOL, MIN_NAME); 213 214 assertTrue(count.intValue() != min.intValue()); 215 216 conn = ds.getConnection("sa", ""); 217 218 count = (Integer )getServer().getAttribute(CRI_PREFILL_POOL, CONN_NAME); 219 min = (Integer )getServer().getAttribute(CRI_PREFILL_POOL, MIN_NAME); 220 221 assertTrue(count.intValue() == min.intValue()); 222 223 } 224 225 226 public void testNonPreFillSupportingPoolPreFill() throws Exception { 227 228 int minSize = 3; 229 int maxSize = 5; 230 231 ManagedConnectionPool mcp = getOnePoolPrefill(minSize, maxSize, true); 232 BaseConnectionManager2 cm = getNoTxCM(mcp); 233 PreFillPoolSupport support = (PreFillPoolSupport)mcp; 234 support.prefill(null, null, false); 235 236 Thread.sleep(5000); 238 239 int currentSize = mcp.getConnectionCount(); 240 assertTrue(currentSize == minSize); 241 242 243 } 244 245 public void testNoPreFillSupportingPool() throws Exception { 246 247 int minSize = 3; 248 int maxSize = 5; 249 250 ManagedConnectionPool mcp = getOnePoolPrefill(minSize, maxSize, false); 251 BaseConnectionManager2 cm = getNoTxCM(mcp); 252 PreFillPoolSupport support = (PreFillPoolSupport)mcp; 253 support.prefill(null, null, false); 254 255 Thread.sleep(5000); 257 258 int currentSize = mcp.getConnectionCount(); 259 assertTrue(currentSize == 0); 260 assertTrue(currentSize != minSize); 261 262 263 } 264 265 public void testPreFillSupportingPool() throws Exception { 266 267 int minSize = 3; 268 int maxSize = 5; 269 270 ManagedConnectionPool mcp = getOnePoolPrefill(minSize, maxSize, true); 271 BaseConnectionManager2 cm = getNoTxCM(mcp); 272 PreFillPoolSupport support = (PreFillPoolSupport)mcp; 273 support.prefill(null, null, false); 274 275 Thread.sleep(5000); 277 278 int currentSize = mcp.getConnectionCount(); 279 assertTrue(currentSize == minSize); 280 281 } 282 283 private ManagedConnectionPool getOnePoolPrefill(int minSize, int maxSize, boolean prefill){ 284 285 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 286 pp.minSize = minSize; 287 pp.maxSize = maxSize; 288 pp.blockingTimeout = 10000; 289 pp.idleTimeout = 0; 290 pp.prefill = prefill; 291 ManagedConnectionFactory mcf = new TestManagedConnectionFactory(); 292 ManagedConnectionPool poolingStrategy = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log); 293 return poolingStrategy; 294 295 } 296 297 private BaseConnectionManager2 getNoTxCM(ManagedConnectionPool poolingStrategy) 298 { 299 TransactionSynchronizer.setTransactionManager(tm); 300 BaseConnectionManager2 cm = new NoTxConnectionManager(ccm, poolingStrategy); 301 poolingStrategy.setConnectionListenerFactory(cm); 302 return cm; 303 } 304 305 public static Test suite() throws Exception 306 { 307 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 308 URL resURL = loader.getResource("jca/prefill/prefill-ds.xml"); 309 Test t1 = getDeploySetup(PreFillPoolingUnitTestCase.class, resURL.toString()); 310 311 resURL = loader.getResource("jca/prefill/no-prefill-ds.xml"); 312 313 Test t2 = getDeploySetup(t1, resURL.toString()); 314 resURL = loader.getResource("jca/prefill/no-element-prefill-ds.xml"); 315 316 Test t3 = getDeploySetup(t2, resURL.toString()); 317 318 resURL = loader.getResource("jca/prefill/cri-prefill-ds.xml"); 319 320 Test t4 = getDeploySetup(t3, resURL.toString()); 321 resURL = loader.getResource("jca/prefill/invalid-prefill-ds.xml"); 322 323 return getDeploySetup(t4, resURL.toString()); 324 325 326 } 327 328 329 } 330 | Popular Tags |