1 17 package org.apache.avalon.excalibur.component.test; 18 19 import org.apache.avalon.framework.component.Component; 20 import org.apache.avalon.framework.component.ComponentManager; 21 import org.apache.avalon.excalibur.component.PoolableComponentHandler; 22 import org.apache.avalon.excalibur.testcase.BufferedLogger; 23 import org.apache.avalon.excalibur.testcase.ExcaliburTestCase; 24 25 32 public class PoolableComponentHandlerTestCase 33 extends ExcaliburTestCase 34 { 35 private Exception m_exception; 36 37 40 public PoolableComponentHandlerTestCase( String name ) 41 { 42 super( name ); 43 } 44 45 48 49 52 public void setUp() throws Exception 53 { 54 super.setUp(); 55 } 56 57 public void tearDown() throws Exception 58 { 59 super.tearDown(); 60 } 61 62 65 68 public void testDefaults() throws Exception 69 { 70 String name = "testDefaults"; 71 getLogger().info( "Test: " + name ); 72 73 int size = PoolableComponentHandler.DEFAULT_MAX_POOL_SIZE + 2; 74 75 BufferedLogger logger = new BufferedLogger(); 76 PoolableTestObject.setStaticLoggger( logger ); 77 PoolableTestObject.resetInstanceCounter(); 78 79 PoolableTestObjectInterface[] poolables = new PoolableTestObjectInterface[ size ]; 80 81 for( int i = 0; i < size; i++ ) 83 { 84 poolables[ i ] = 85 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 86 } 87 88 for( int i = 0; i < size; i++ ) 90 { 91 release( (Component)poolables[ i ] ); 92 poolables[ i ] = null; 93 } 94 95 for( int i = 0; i < size; i++ ) 97 { 98 poolables[ i ] = 99 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 100 } 101 102 for( int i = 0; i < size; i++ ) 104 { 105 release( (Component)poolables[ i ] ); 106 poolables[ i ] = null; 107 } 108 109 String resultLog = logger.toString(); 113 String expectedLog = 114 "DEBUG - PoolableTestObject #1 initialized.\n" + 115 "DEBUG - PoolableTestObject #2 initialized.\n" + 116 "DEBUG - PoolableTestObject #3 initialized.\n" + 117 "DEBUG - PoolableTestObject #4 initialized.\n" + 118 "DEBUG - PoolableTestObject #5 initialized.\n" + 119 "DEBUG - PoolableTestObject #6 initialized.\n" + 120 "DEBUG - PoolableTestObject #7 initialized.\n" + 121 "DEBUG - PoolableTestObject #8 initialized.\n" + 122 "DEBUG - PoolableTestObject #9 initialized.\n" + 123 "DEBUG - PoolableTestObject #10 initialized.\n" + 124 "DEBUG - PoolableTestObject #1 recycled.\n" + 125 "DEBUG - PoolableTestObject #1 disposed.\n" + "DEBUG - PoolableTestObject #2 recycled.\n" + 127 "DEBUG - PoolableTestObject #2 disposed.\n" + "DEBUG - PoolableTestObject #3 recycled.\n" + 129 "DEBUG - PoolableTestObject #4 recycled.\n" + 130 "DEBUG - PoolableTestObject #5 recycled.\n" + 131 "DEBUG - PoolableTestObject #6 recycled.\n" + 132 "DEBUG - PoolableTestObject #7 recycled.\n" + 133 "DEBUG - PoolableTestObject #8 recycled.\n" + 134 "DEBUG - PoolableTestObject #9 recycled.\n" + 135 "DEBUG - PoolableTestObject #10 recycled.\n" + 136 "DEBUG - PoolableTestObject #11 initialized.\n" + 137 "DEBUG - PoolableTestObject #12 initialized.\n" + 138 "DEBUG - PoolableTestObject #10 recycled.\n" + "DEBUG - PoolableTestObject #10 disposed.\n" + "DEBUG - PoolableTestObject #9 recycled.\n" + 141 "DEBUG - PoolableTestObject #9 disposed.\n" + "DEBUG - PoolableTestObject #8 recycled.\n" + 143 "DEBUG - PoolableTestObject #7 recycled.\n" + 144 "DEBUG - PoolableTestObject #6 recycled.\n" + 145 "DEBUG - PoolableTestObject #5 recycled.\n" + 146 "DEBUG - PoolableTestObject #4 recycled.\n" + 147 "DEBUG - PoolableTestObject #3 recycled.\n" + 148 "DEBUG - PoolableTestObject #11 recycled.\n" + 149 "DEBUG - PoolableTestObject #12 recycled.\n"; 150 151 try 152 { 153 assertEquals( "Log did not contain the expected output.", resultLog, expectedLog ); 154 } 155 catch(junit.framework.ComparisonFailure cf) 156 { 157 System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog); 159 throw cf; 160 } 161 } 162 163 166 public void testMax4() throws Exception 167 { 168 String name = "testMax4"; 169 getLogger().info( "Test: " + name ); 170 171 int size = 4 + 1; 172 173 BufferedLogger logger = new BufferedLogger(); 174 PoolableTestObject.setStaticLoggger( logger ); 175 PoolableTestObject.resetInstanceCounter(); 176 177 PoolableTestObjectInterface[] poolables = new PoolableTestObjectInterface[ size ]; 178 179 for( int i = 0; i < size; i++ ) 181 { 182 poolables[ i ] = 183 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 184 } 185 186 for( int i = 0; i < size; i++ ) 188 { 189 release( (Component)poolables[ i ] ); 190 poolables[ i ] = null; 191 } 192 193 for( int i = 0; i < size; i++ ) 195 { 196 poolables[ i ] = 197 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 198 } 199 200 for( int i = 0; i < size; i++ ) 202 { 203 release( (Component)poolables[ i ] ); 204 poolables[ i ] = null; 205 } 206 207 String resultLog = logger.toString(); 210 String expectedLog = 211 "DEBUG - PoolableTestObject #1 initialized.\n" + 212 "DEBUG - PoolableTestObject #2 initialized.\n" + 213 "DEBUG - PoolableTestObject #3 initialized.\n" + 214 "DEBUG - PoolableTestObject #4 initialized.\n" + 215 "DEBUG - PoolableTestObject #5 initialized.\n" + 216 "DEBUG - PoolableTestObject #1 recycled.\n" + 217 "DEBUG - PoolableTestObject #1 disposed.\n" + "DEBUG - PoolableTestObject #2 recycled.\n" + 219 "DEBUG - PoolableTestObject #3 recycled.\n" + 220 "DEBUG - PoolableTestObject #4 recycled.\n" + 221 "DEBUG - PoolableTestObject #5 recycled.\n" + 222 "DEBUG - PoolableTestObject #6 initialized.\n" + 223 "DEBUG - PoolableTestObject #5 recycled.\n" + "DEBUG - PoolableTestObject #5 disposed.\n" + "DEBUG - PoolableTestObject #4 recycled.\n" + 226 "DEBUG - PoolableTestObject #3 recycled.\n" + 227 "DEBUG - PoolableTestObject #2 recycled.\n" + 228 "DEBUG - PoolableTestObject #6 recycled.\n"; 229 230 try 231 { 232 assertEquals( "Log did not contain the expected output.", resultLog, expectedLog ); 233 } 234 catch(junit.framework.ComparisonFailure cf) 235 { 236 System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog); 238 throw cf; 239 } 240 } 241 242 245 public void testMax4StrictNoBlocking() throws Exception 246 { 247 String name = "testMax4StrictNoBlocking"; 248 getLogger().info( "Test: " + name ); 249 250 int size = 4; 251 252 BufferedLogger logger = new BufferedLogger(); 253 PoolableTestObject.setStaticLoggger( logger ); 254 PoolableTestObject.resetInstanceCounter(); 255 256 PoolableTestObjectInterface[] poolables = new PoolableTestObjectInterface[ size ]; 257 258 for( int i = 0; i < size; i++ ) 260 { 261 poolables[ i ] = 262 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 263 } 264 265 try 267 { 268 lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 269 fail( "Attempt to get more Pollables than are in the pool should have failed." ); 270 } 271 catch( Exception e ) 272 { 273 } 275 276 for( int i = 0; i < size; i++ ) 278 { 279 release( (Component)poolables[ i ] ); 280 poolables[ i ] = null; 281 } 282 283 String resultLog = logger.toString(); 286 String expectedLog = 287 "DEBUG - PoolableTestObject #1 initialized.\n" + 288 "DEBUG - PoolableTestObject #2 initialized.\n" + 289 "DEBUG - PoolableTestObject #3 initialized.\n" + 290 "DEBUG - PoolableTestObject #4 initialized.\n" + 291 "DEBUG - PoolableTestObject #1 recycled.\n" + 292 "DEBUG - PoolableTestObject #2 recycled.\n" + 293 "DEBUG - PoolableTestObject #3 recycled.\n" + 294 "DEBUG - PoolableTestObject #4 recycled.\n"; 295 296 try 297 { 298 assertEquals( "Log did not contain the expected output.", resultLog, expectedLog ); 299 } 300 catch(junit.framework.ComparisonFailure cf) 301 { 302 System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog); 304 throw cf; 305 } 306 } 307 308 private static class TestMax4StrictBlockingThread extends Thread 309 { 310 private final ComponentManager manager; 311 private final BufferedLogger logger; 312 313 public TestMax4StrictBlockingThread( ComponentManager manager, BufferedLogger logger ) 314 { 315 this.manager = manager; 316 this.logger = logger; 317 } 318 319 public void run() 320 { 321 final String name = "testMax4StrictBlocking"; 322 try 323 { 324 logger.debug( "Lookup in second thread." ); 325 PoolableTestObjectInterface poolable = (PoolableTestObjectInterface) manager.lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 326 327 try 329 { 330 Thread.sleep( 500 ); 331 } 332 catch( InterruptedException e ) 333 { 334 } 335 336 logger.debug( "Release in second thread." ); 337 manager.release( (Component)poolable ); 338 } 339 catch( Exception e ) 340 { 341 e.printStackTrace (); 342 } 343 } 344 } 345 346 349 public void testMax4StrictBlocking() throws Exception 350 { 351 final String name = "testMax4StrictBlocking"; 352 getLogger().info( "Test: " + name ); 353 354 int size = 3; 355 356 m_exception = null; 358 359 final BufferedLogger logger = new BufferedLogger(); 360 PoolableTestObject.setStaticLoggger( logger ); 361 PoolableTestObject.resetInstanceCounter(); 362 363 PoolableTestObjectInterface[] poolables = new PoolableTestObjectInterface[ size ]; 364 365 for( int i = 0; i < size; i++ ) 367 { 368 poolables[ i ] = 369 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 370 } 371 372 TestMax4StrictBlockingThread secondThread = new TestMax4StrictBlockingThread( manager, logger ); 374 secondThread.start(); 375 376 try 378 { 379 Thread.sleep( 250 ); 380 } 381 catch( InterruptedException e ) 382 { 383 } 384 385 logger.debug( "Lookup in main thread." ); 387 PoolableTestObjectInterface poolable = 388 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 389 390 logger.debug( "Release in main thread." ); 391 release( (Component)poolable ); 392 393 secondThread.join(); 394 395 for( int i = 0; i < size; i++ ) 397 { 398 release( (Component)poolables[ i ] ); 399 poolables[ i ] = null; 400 } 401 402 assertTrue( "Unexpected exception in second thread.", m_exception == null ); 404 405 String resultLog = logger.toString(); 408 String expectedLog = 409 "DEBUG - PoolableTestObject #1 initialized.\n" + 410 "DEBUG - PoolableTestObject #2 initialized.\n" + 411 "DEBUG - PoolableTestObject #3 initialized.\n" + 412 "DEBUG - Lookup in second thread.\n" + 413 "DEBUG - PoolableTestObject #4 initialized.\n" + 414 "DEBUG - Lookup in main thread.\n" + 415 "DEBUG - Release in second thread.\n" + 416 "DEBUG - PoolableTestObject #4 recycled.\n" + 417 "DEBUG - Release in main thread.\n" + 418 "DEBUG - PoolableTestObject #4 recycled.\n" + 419 "DEBUG - PoolableTestObject #1 recycled.\n" + 420 "DEBUG - PoolableTestObject #2 recycled.\n" + 421 "DEBUG - PoolableTestObject #3 recycled.\n"; 422 423 try 424 { 425 assertEquals( "Log did not contain the expected output.", resultLog, expectedLog ); 426 } 427 catch(junit.framework.ComparisonFailure cf) 428 { 429 System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog); 431 throw cf; 432 } 433 434 } 435 436 439 public void testMax4StrictBlockingTimeout() throws Exception 440 { 441 String name = "testMax4StrictBlockingTimeout"; 442 getLogger().info( "Test: " + name ); 443 444 int size = 4; 445 446 BufferedLogger logger = new BufferedLogger(); 447 PoolableTestObject.setStaticLoggger( logger ); 448 PoolableTestObject.resetInstanceCounter(); 449 450 PoolableTestObjectInterface[] poolables = new PoolableTestObjectInterface[ size ]; 451 452 for( int i = 0; i < size; i++ ) 454 { 455 poolables[ i ] = 456 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 457 } 458 459 long start = System.currentTimeMillis(); 461 try 462 { 463 lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 464 fail( "Attempt to get more Pollables than are in the pool should have failed." ); 465 } 466 catch( Exception e ) 467 { 468 } 470 long dur = System.currentTimeMillis() - start; 471 assertTrue( "Block timeout was not within 50 milliseconds of the configured 500 milliseconds,", 472 dur >= 450 && dur <= 550 ); 473 474 for( int i = 0; i < size; i++ ) 476 { 477 release( (Component)poolables[ i ] ); 478 poolables[ i ] = null; 479 } 480 481 String resultLog = logger.toString(); 484 String expectedLog = 485 "DEBUG - PoolableTestObject #1 initialized.\n" + 486 "DEBUG - PoolableTestObject #2 initialized.\n" + 487 "DEBUG - PoolableTestObject #3 initialized.\n" + 488 "DEBUG - PoolableTestObject #4 initialized.\n" + 489 "DEBUG - PoolableTestObject #1 recycled.\n" + 490 "DEBUG - PoolableTestObject #2 recycled.\n" + 491 "DEBUG - PoolableTestObject #3 recycled.\n" + 492 "DEBUG - PoolableTestObject #4 recycled.\n"; 493 494 try 495 { 496 assertEquals( "Log did not contain the expected output.", resultLog, expectedLog ); 497 } 498 catch(junit.framework.ComparisonFailure cf) 499 { 500 System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog); 502 throw cf; 503 } 504 } 505 506 509 public void testTrimming() throws Exception 510 { 511 String name = "testTrimming"; 512 getLogger().info( "Test: " + name ); 513 514 BufferedLogger logger = new BufferedLogger(); 515 PoolableTestObject.setStaticLoggger( logger ); 516 PoolableTestObject.resetInstanceCounter(); 517 518 PoolableTestObjectInterface[] poolables = new PoolableTestObjectInterface[ 4 ]; 519 520 for( int i = 0; i < 4; i++ ) 522 { 523 poolables[ i ] = 524 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 525 } 526 for( int i = 0; i < 4; i++ ) 527 { 528 release( (Component)poolables[ i ] ); 529 poolables[ i ] = null; 530 } 531 for( int i = 0; i < 4; i++ ) 532 { 533 poolables[ i ] = 534 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 535 } 536 for( int i = 0; i < 4; i++ ) 537 { 538 release( (Component)poolables[ i ] ); 539 poolables[ i ] = null; 540 } 541 542 try 544 { 545 Thread.sleep( 550 ); 546 } 547 catch( InterruptedException e ) 548 { 549 } 550 551 for( int i = 0; i < 2; i++ ) 553 { 554 poolables[ i ] = 555 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 556 } 557 for( int i = 0; i < 2; i++ ) 558 { 559 release( (Component)poolables[ i ] ); 560 poolables[ i ] = null; 561 } 562 563 try 565 { 566 Thread.sleep( 550 ); 567 } 568 catch( InterruptedException e ) 569 { 570 } 571 572 for( int i = 0; i < 4; i++ ) 576 { 577 poolables[ i ] = 578 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name ); 579 } 580 for( int i = 0; i < 4; i++ ) 581 { 582 release( (Component)poolables[ i ] ); 583 poolables[ i ] = null; 584 } 585 586 587 String resultLog = logger.toString(); 590 String expectedLog = 591 "DEBUG - PoolableTestObject #1 initialized.\n" + "DEBUG - PoolableTestObject #2 initialized.\n" + 593 "DEBUG - PoolableTestObject #3 initialized.\n" + 594 "DEBUG - PoolableTestObject #4 initialized.\n" + 595 "DEBUG - PoolableTestObject #1 recycled.\n" + "DEBUG - PoolableTestObject #2 recycled.\n" + 597 "DEBUG - PoolableTestObject #3 recycled.\n" + 598 "DEBUG - PoolableTestObject #4 recycled.\n" + 599 "DEBUG - PoolableTestObject #4 recycled.\n" + "DEBUG - PoolableTestObject #3 recycled.\n" + 601 "DEBUG - PoolableTestObject #2 recycled.\n" + 602 "DEBUG - PoolableTestObject #1 recycled.\n" + 603 "DEBUG - PoolableTestObject #1 recycled.\n" + "DEBUG - PoolableTestObject #2 recycled.\n" + 605 "DEBUG - PoolableTestObject #4 disposed.\n" + "DEBUG - PoolableTestObject #3 disposed.\n" + 607 "DEBUG - PoolableTestObject #5 initialized.\n" + "DEBUG - PoolableTestObject #6 initialized.\n" + 609 "DEBUG - PoolableTestObject #2 recycled.\n" + "DEBUG - PoolableTestObject #1 recycled.\n" + 611 "DEBUG - PoolableTestObject #5 recycled.\n" + 612 "DEBUG - PoolableTestObject #6 recycled.\n"; 613 614 try 615 { 616 assertEquals( "Log did not contain the expected output.", resultLog, expectedLog ); 617 } 618 catch(junit.framework.ComparisonFailure cf) 619 { 620 System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog); 622 throw cf; 623 } 624 } 625 } 626 627 | Popular Tags |