KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > component > test > PoolableComponentHandlerTestCase


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 /**
26  * Test the PoolableComponentHandler.
27  *
28  * @deprecated ECM is no longer supported
29  *
30  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
31  */

32 public class PoolableComponentHandlerTestCase
33     extends ExcaliburTestCase
34 {
35     private Exception JavaDoc m_exception;
36
37     /*---------------------------------------------------------------
38      * Constructors
39      *-------------------------------------------------------------*/

40     public PoolableComponentHandlerTestCase( String JavaDoc name )
41     {
42         super( name );
43     }
44
45     /*---------------------------------------------------------------
46      * Methods
47      *-------------------------------------------------------------*/

48
49     /*---------------------------------------------------------------
50      * TestCase Methods
51      *-------------------------------------------------------------*/

52     public void setUp() throws Exception JavaDoc
53     {
54         super.setUp();
55     }
56
57     public void tearDown() throws Exception JavaDoc
58     {
59         super.tearDown();
60     }
61
62     /*---------------------------------------------------------------
63      * Test Cases
64      *-------------------------------------------------------------*/

65     /**
66      * Test the default values and make sure that objects are reused as expected.
67      */

68     public void testDefaults() throws Exception JavaDoc
69     {
70         String JavaDoc 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         // Lookup the components.
82
for( int i = 0; i < size; i++ )
83         {
84             poolables[ i ] =
85                 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
86         }
87
88         // Release the components.
89
for( int i = 0; i < size; i++ )
90         {
91             release( (Component)poolables[ i ] );
92             poolables[ i ] = null;
93         }
94
95         // Lookup the components.
96
for( int i = 0; i < size; i++ )
97         {
98             poolables[ i ] =
99                 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
100         }
101
102         // Release the components.
103
for( int i = 0; i < size; i++ )
104         {
105             release( (Component)poolables[ i ] );
106             poolables[ i ] = null;
107         }
108
109         // The disposal of the objects will not show up in the log until the component manager is
110
// actually disposed.
111
// When objects are returned the pool, they are stored in a last in first off list.
112
String JavaDoc resultLog = logger.toString();
113         String JavaDoc 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" + // Still 9 outstanding
126
"DEBUG - PoolableTestObject #2 recycled.\n" +
127             "DEBUG - PoolableTestObject #2 disposed.\n" + // Still 8 outstanding
128
"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" + // Gets are in LIFO order.
139
"DEBUG - PoolableTestObject #10 disposed.\n" + // Still 9 outstanding
140
"DEBUG - PoolableTestObject #9 recycled.\n" +
141             "DEBUG - PoolableTestObject #9 disposed.\n" + // Still 8 outstanding
142
"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             // For clarity.
158
System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog);
159             throw cf;
160         }
161     }
162
163     /**
164      * Test a non-default max value.
165      */

166     public void testMax4() throws Exception JavaDoc
167     {
168         String JavaDoc 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         // Lookup the components.
180
for( int i = 0; i < size; i++ )
181         {
182             poolables[ i ] =
183                 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
184         }
185
186         // Release the components.
187
for( int i = 0; i < size; i++ )
188         {
189             release( (Component)poolables[ i ] );
190             poolables[ i ] = null;
191         }
192
193         // Lookup the components.
194
for( int i = 0; i < size; i++ )
195         {
196             poolables[ i ] =
197                 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
198         }
199
200         // Release the components.
201
for( int i = 0; i < size; i++ )
202         {
203             release( (Component)poolables[ i ] );
204             poolables[ i ] = null;
205         }
206
207         // The disposal of the objects will not show up in the log until the component manager is
208
// actually disposed.
209
String JavaDoc resultLog = logger.toString();
210         String JavaDoc 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" + // Still 4 outstanding
218
"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" + // Gets are in LIFO order.
224
"DEBUG - PoolableTestObject #5 disposed.\n" + // Still 4 outstanding
225
"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             // For clarity.
237
System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog);
238             throw cf;
239         }
240     }
241
242     /**
243      * Test a non-default max value with a strict max and no blocking
244      */

245     public void testMax4StrictNoBlocking() throws Exception JavaDoc
246     {
247         String JavaDoc 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         // Lookup the components.
259
for( int i = 0; i < size; i++ )
260         {
261             poolables[ i ] =
262                 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
263         }
264
265         // Try to get one more. Should fail.
266
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 JavaDoc e )
272         {
273             // Passed
274
}
275
276         // Release the components.
277
for( int i = 0; i < size; i++ )
278         {
279             release( (Component)poolables[ i ] );
280             poolables[ i ] = null;
281         }
282
283         // The disposal of the objects will not show up in the log until the component manager is
284
// actually disposed.
285
String JavaDoc resultLog = logger.toString();
286         String JavaDoc 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             // For clarity.
303
System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog);
304             throw cf;
305         }
306     }
307
308     private static class TestMax4StrictBlockingThread extends Thread JavaDoc
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 JavaDoc name = "testMax4StrictBlocking";
322             try
323             {
324                 logger.debug( "Lookup in second thread." );
325                 PoolableTestObjectInterface poolable = (PoolableTestObjectInterface) manager.lookup( PoolableTestObjectInterface.ROLE + "/" + name );
326
327                 // Give the main thread a chance to block
328
try
329                 {
330                     Thread.sleep( 500 );
331                 }
332                 catch( InterruptedException JavaDoc e )
333                 {
334                 }
335
336                 logger.debug( "Release in second thread." );
337                 manager.release( (Component)poolable );
338             }
339             catch( Exception JavaDoc e )
340             {
341                 e.printStackTrace ();
342             }
343         }
344     }
345
346     /**
347      * Test a non-default max value with a strict max and blocking with no timeout
348      */

349     public void testMax4StrictBlocking() throws Exception JavaDoc
350     {
351         final String JavaDoc name = "testMax4StrictBlocking";
352         getLogger().info( "Test: " + name );
353
354         int size = 3;
355
356         // Initialize the exception field.
357
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         // Lookup the components.
366
for( int i = 0; i < size; i++ )
367         {
368             poolables[ i ] =
369                 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
370         }
371
372         // In another thread, get and release another poolable to cause this one to wait.
373
TestMax4StrictBlockingThread secondThread = new TestMax4StrictBlockingThread( manager, logger );
374         secondThread.start();
375
376         // Give the second thread a chance to get the 4th poolable
377
try
378         {
379             Thread.sleep( 250 );
380         }
381         catch( InterruptedException JavaDoc e )
382         {
383         }
384
385         // Try to get one more. Should block until the other thread has put it back.
386
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         // Release the components.
396
for( int i = 0; i < size; i++ )
397         {
398             release( (Component)poolables[ i ] );
399             poolables[ i ] = null;
400         }
401
402         // Make sure that the second thread did not throw an exception
403
assertTrue( "Unexpected exception in second thread.", m_exception == null );
404
405         // The disposal of the objects will not show up in the log until the component manager is
406
// actually disposed.
407
String JavaDoc resultLog = logger.toString();
408         String JavaDoc 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             // For clarity.
430
System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog);
431             throw cf;
432         }
433
434     }
435
436     /**
437      * Test a non-default max value with a strict max and blocking with a timeout
438      */

439     public void testMax4StrictBlockingTimeout() throws Exception JavaDoc
440     {
441         String JavaDoc 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         // Lookup the components.
453
for( int i = 0; i < size; i++ )
454         {
455             poolables[ i ] =
456                 (PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
457         }
458
459         // Try to get one more. Should fail after 500 milliseconds.
460
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 JavaDoc e )
467         {
468             // Passed
469
}
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         // Release the components.
475
for( int i = 0; i < size; i++ )
476         {
477             release( (Component)poolables[ i ] );
478             poolables[ i ] = null;
479         }
480
481         // The disposal of the objects will not show up in the log until the component manager is
482
// actually disposed.
483
String JavaDoc resultLog = logger.toString();
484         String JavaDoc 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             // For clarity.
501
System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog);
502             throw cf;
503         }
504     }
505
506     /**
507      * Test the trimming features.
508      */

509     public void testTrimming() throws Exception JavaDoc
510     {
511         String JavaDoc 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         // Lookup and release all 4 components a couple of times.
521
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         // Now wait for 550 ms to trigger a trim on the next lookup.
543
try
544         {
545             Thread.sleep( 550 );
546         }
547         catch( InterruptedException JavaDoc e )
548         {
549         }
550
551         // Lookup and release 2 components to mark them as being recently used.
552
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         // Now wait for 550 ms to trigger a trim on the next lookup.
564
try
565         {
566             Thread.sleep( 550 );
567         }
568         catch( InterruptedException JavaDoc e )
569         {
570         }
571
572         // This next get should cause 2 of the components to be trimmed but the 2 we just lookedup
573
// should stay around.
574
// Lookup and release all 4 components to see which ones are left.
575
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         // The disposal of the objects will not show up in the log until the component manager is
588
// actually disposed.
589
String JavaDoc resultLog = logger.toString();
590         String JavaDoc expectedLog =
591             "DEBUG - PoolableTestObject #1 initialized.\n" + // First 4 lookups
592
"DEBUG - PoolableTestObject #2 initialized.\n" +
593             "DEBUG - PoolableTestObject #3 initialized.\n" +
594             "DEBUG - PoolableTestObject #4 initialized.\n" +
595             "DEBUG - PoolableTestObject #1 recycled.\n" + // First 4 releases
596
"DEBUG - PoolableTestObject #2 recycled.\n" +
597             "DEBUG - PoolableTestObject #3 recycled.\n" +
598             "DEBUG - PoolableTestObject #4 recycled.\n" +
599             "DEBUG - PoolableTestObject #4 recycled.\n" + // Second 4 releases already existed.
600
"DEBUG - PoolableTestObject #3 recycled.\n" +
601             "DEBUG - PoolableTestObject #2 recycled.\n" +
602             "DEBUG - PoolableTestObject #1 recycled.\n" +
603             "DEBUG - PoolableTestObject #1 recycled.\n" + // 2 lookups after wait.
604
"DEBUG - PoolableTestObject #2 recycled.\n" +
605             "DEBUG - PoolableTestObject #4 disposed.\n" + // First lookup after second wait triggers disposal of 2 old Poolables.
606
"DEBUG - PoolableTestObject #3 disposed.\n" +
607             "DEBUG - PoolableTestObject #5 initialized.\n" + // 4 lookups requred 2 more instances.
608
"DEBUG - PoolableTestObject #6 initialized.\n" +
609             "DEBUG - PoolableTestObject #2 recycled.\n" + // Final 4 releases
610
"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             // For clarity.
621
System.out.println( "Expected:\n" + expectedLog + "Was:\n" + resultLog);
622             throw cf;
623         }
624     }
625 }
626
627
Popular Tags