1 21 22 package org.opensubsystems.core.persist.db.connectionpool; 23 24 import java.sql.Connection ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import java.util.Properties ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 31 import org.opensubsystems.core.error.OSSDatabaseAccessException; 32 import org.opensubsystems.core.error.OSSException; 33 import org.opensubsystems.core.error.OSSInternalErrorException; 34 import org.opensubsystems.core.util.Config; 35 import org.opensubsystems.core.util.GlobalConstants; 36 import org.opensubsystems.core.util.Log; 37 38 47 public abstract class PooledDatabaseConnectionFactoryImplBaseTest extends 48 DatabaseConnectionFactoryBaseTest 49 { 50 52 55 protected static final int ALL_CONNECTION_COUNT = 5; 56 57 64 protected static final int POOL_WAIT_PERIOD = 5000; 65 66 68 71 private static Logger s_logger = Log.getInstance( 72 PooledDatabaseConnectionFactoryImplBaseTest.class); 73 74 76 81 public PooledDatabaseConnectionFactoryImplBaseTest( 82 String strTestName 83 ) 84 { 85 super(strTestName); 86 } 87 88 90 95 protected void setUp( 96 ) throws Exception 97 { 98 String strTestName; 99 100 strTestName = getName(); 102 103 Properties predefinedProperties = Config.getInstance().getProperties(); 105 106 String strMinPoolSize = PooledDatabaseConnectionFactorySetupReader.DATABASE_POOL_BASE_PATH + 108 "." + DATASOURCE_NAME_1 + "." + 109 PooledDatabaseConnectionFactorySetupReader.DBPOOL_MIN_SIZE; 110 predefinedProperties.put(strMinPoolSize, Integer.toString(ALL_CONNECTION_COUNT - 1)); 117 118 String strInitPoolSize = PooledDatabaseConnectionFactorySetupReader.DATABASE_POOL_BASE_PATH + 120 "." + DATASOURCE_NAME_1 + "." + 121 PooledDatabaseConnectionFactorySetupReader.DBPOOL_INITIAL_SIZE; 122 predefinedProperties.put(strInitPoolSize, (new Integer (ALL_CONNECTION_COUNT)).toString()); 124 125 String strMaxPoolSize = PooledDatabaseConnectionFactorySetupReader.DATABASE_POOL_BASE_PATH + 127 "." + DATASOURCE_NAME_1 + "." + 128 PooledDatabaseConnectionFactorySetupReader.DBPOOL_MAX_SIZE; 129 predefinedProperties.put(strMaxPoolSize, (new Integer (ALL_CONNECTION_COUNT)).toString()); 131 132 String strWaitPeriod = PooledDatabaseConnectionFactorySetupReader.DATABASE_POOL_BASE_PATH + 134 "." + DATASOURCE_NAME_1 + "." + 135 PooledDatabaseConnectionFactorySetupReader.DBPOOL_WAIT_PERIOD; 136 predefinedProperties.put(strWaitPeriod, (new Integer (POOL_WAIT_PERIOD)).toString()); 138 139 String strCanGrow = PooledDatabaseConnectionFactorySetupReader.DATABASE_POOL_BASE_PATH + 141 "." + DATASOURCE_NAME_1 + "." + 142 PooledDatabaseConnectionFactorySetupReader.DBPOOL_CAN_GROW; 143 144 145 if (strTestName.equals("testRequestOneMoreCannotGrow")) 147 { 148 predefinedProperties.put(strCanGrow, GlobalConstants.INTEGER_0.toString()); 149 } 150 if (strTestName.equals("testRequestOneMoreCanGrow")) 151 { 152 predefinedProperties.put(strCanGrow, GlobalConstants.INTEGER_1.toString()); 153 } 154 155 Config.setInstance(new Config(predefinedProperties)); 156 157 super.setUp(); 158 } 159 160 166 public void testRequestXReturnX( 167 ) throws Exception 168 { 169 Connection con = null; 170 int iIndex; 171 List lstConIdentifiers = new ArrayList (ALL_CONNECTION_COUNT); 172 List lstConnections = new ArrayList (ALL_CONNECTION_COUNT); 173 174 try 175 { 176 for (iIndex = 0; iIndex < ALL_CONNECTION_COUNT; iIndex++) 177 { 178 con = m_connectionFactory.requestConnection(true); 181 182 lstConnections.add(con); 186 187 assertFalse("The pool should NOT return existing connection again" + 189 " since it wasn't returned yet.", 190 containsConnection(lstConIdentifiers, con)); 191 192 lstConIdentifiers.add(addItem(con)); 195 } 196 } 197 finally 198 { 199 for (iIndex = 0; (iIndex < ALL_CONNECTION_COUNT) 200 && (!lstConnections.isEmpty()); iIndex++) 201 { 202 m_connectionFactory.returnConnection( 204 (Connection )lstConnections.remove(0)); 205 } 206 } 207 208 assertTrue("Not all connections were returned.", lstConnections.isEmpty()); 209 210 try 214 { 215 for (iIndex = 0; iIndex < ALL_CONNECTION_COUNT; iIndex++) 216 { 217 con = m_connectionFactory.requestConnection(true); 220 221 lstConnections.add(con); 225 226 assertTrue("The pool should return existing connection again" + 228 " since it should be pooled.", 229 containsConnection(lstConIdentifiers, con)); 230 } 231 } 232 finally 233 { 234 for (iIndex = 0; (iIndex < ALL_CONNECTION_COUNT) 235 && (!lstConnections.isEmpty()); iIndex++) 236 { 237 m_connectionFactory.returnConnection( 239 (Connection )lstConnections.remove(0)); 240 } 241 } 242 243 assertTrue("Not all connections were returned.", lstConnections.isEmpty()); 244 } 245 246 255 public void testRequestOneMoreCannotGrow( 256 ) throws Exception 257 { 258 requestOneMore(false); 259 } 260 261 270 public void testRequestOneMoreCanGrow( 271 ) throws Exception 272 { 273 requestOneMore(true); 274 } 275 276 288 protected abstract boolean containsConnection( 289 List lstConnections, 290 Connection newConnection 291 ) throws OSSException; 292 293 295 302 private void requestOneMore( 303 boolean bCanGrow 304 ) throws Exception 305 { 306 Connection con = null; 307 List lstConnections = new ArrayList (ALL_CONNECTION_COUNT); 308 309 try 310 { 311 int iIndex; 312 313 for (iIndex = 0; iIndex < ALL_CONNECTION_COUNT; iIndex++) 315 { 316 con = m_connectionFactory.requestConnection(true); 318 319 lstConnections.add(con); 321 } 322 323 Connection cOneMoreConnection = null; 326 long startTime = 0; 327 long endTime = 0; 328 329 try 330 { 331 try 334 { 335 startTime = System.currentTimeMillis(); 336 cOneMoreConnection = m_connectionFactory.requestConnection(true); 338 339 if (bCanGrow) 340 { 341 assertNotNull("Either the pool doesn't implement growing beyond maximal size" + 342 " or it failed to grow.", cOneMoreConnection); 343 } 344 else 345 { 346 fail("Pool is setup to do not grow so another connections" + 348 " shouldn't be returned."); 349 } 350 } 351 catch (OSSDatabaseAccessException daeExc) 352 { 353 if (!bCanGrow) 354 { 355 endTime = System.currentTimeMillis() - startTime; 360 361 assertTrue("Either the pool doesn't implement pool waiting period" + 362 " when exhausted or the pool waited less that expected." + 363 " Expected wait = " + POOL_WAIT_PERIOD + " and waited = " 364 + endTime, endTime >= POOL_WAIT_PERIOD); 365 } 366 else 367 { 368 s_logger.log(Level.WARNING, 369 "Either the pool doesn't implement growing beyond maximal" + 370 " size or failed to grow.", daeExc); 371 fail("Either the pool doesn't implement growing beyond maximal" + 372 " size or it failed to grow."); 373 } 374 } 375 } 376 finally 377 { 378 m_connectionFactory.returnConnection(cOneMoreConnection); 379 } 380 } 381 finally 382 { 383 int iIndex; 384 385 for (iIndex = 0; (iIndex < ALL_CONNECTION_COUNT) 386 && (!lstConnections.isEmpty()); iIndex++) 387 { 388 m_connectionFactory.returnConnection( 390 (Connection )lstConnections.remove(0)); 391 } 392 } 393 394 assertTrue("Not all connections were returned.", lstConnections.isEmpty()); 395 } 396 397 406 protected Object addItem( 407 Connection cActualConnection 408 ) throws OSSInternalErrorException 409 { 410 return cActualConnection; 411 } 412 } 413 | Popular Tags |