View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.TestCase;
20  
21  import java.util.Vector;
22  
23  /**
24   * Created on 21/06/2003
25   *
26   * @author <a href="mailto:bert@tuaworks.co.nz">Bert van Brakel</a>
27   * @version $Revision$
28   */
29  public class SweeperPoolTest
30      extends TestCase
31  {
32      /** The pool under test */
33      TestObjectPool pool;
34  
35      /** A bunch of object to pool */
36      Object o1;
37  
38      Object o2;
39  
40      Object o3;
41  
42      Object o4;
43  
44      Object o5;
45  
46      Object o6;
47  
48      /**
49       * Constructor
50       */
51      public SweeperPoolTest()
52      {
53          super();
54      }
55  
56      /**
57       * Constructor
58       *
59       * @param arg0
60       */
61      public SweeperPoolTest( String arg0 )
62      {
63          super( arg0 );
64      }
65  
66      /**
67       * Test the pool limits it's size, and disposes unneeded objects correctly
68       */
69      public void testMaxSize()
70      {
71          int sweepInterval = 0;
72          int initialCapacity = 5;
73          int maxSize = 2;
74          int minSize = 1;
75          int triggerSize = 2;
76  
77          pool = new TestObjectPool( maxSize, minSize, initialCapacity, sweepInterval, triggerSize );
78  
79          Object tmp = pool.get();
80          assertNull( "Expected object from pool to be null", tmp );
81          pool.put( o1 );
82          assertEquals( "Expected pool to contain 1 object", 1, pool.getSize() );
83          tmp = pool.get();
84          assertSame( "Expected returned pool object to be the same as the one put in", tmp, o1 );
85          pool.put( o1 );
86          pool.put( o2 );
87          assertEquals( "Expected pool to contain 2 objects", 2, pool.getSize() );
88          pool.put( o3 );
89          assertEquals( "Expected pool to contain only a maximum of 2 objects.", 2, pool.getSize() );
90          assertEquals( "Expected 1 disposed pool object", 1, pool.testGetDisposedObjects().size() );
91          tmp = pool.testGetDisposedObjects().iterator().next();
92  
93          tmp = pool.get();
94          assertEquals( "Expected pool size to be 1 after removing one object", 1, pool.getSize() );
95          Object tmp2 = pool.get();
96          assertEquals( "Expected pool size to be 0 after removing 2 objects", 0, pool.getSize() );
97          assertNotSame( "Expected returned objects to be different", tmp, tmp2 );
98  
99      }
100 
101     public void testSweepAndTrim1()
102     {
103         // test trigger
104         int sweepInterval = 1;
105         int initialCapacity = 5;
106         int maxSize = 5;
107         int minSize = 1;
108         int triggerSize = 2;
109 
110         pool = new TestObjectPool( maxSize, minSize, initialCapacity, sweepInterval, triggerSize );
111         pool.put( o1 );
112         pool.put( o2 );
113         pool.put( o3 );
114         pool.put( o4 );
115         // give the sweeper some time to run
116         synchronized ( this )
117         {
118             try
119             {
120                 wait( 2 * 1000 );
121             }
122             catch ( InterruptedException e )
123             {
124                 fail( "Unexpected exception thrown. e=" + Tracer.traceToString( e ) );
125             }
126         }
127         assertEquals( "Expected pool to only contain 1 object", 1, pool.getSize() );
128         assertEquals( "Expected 3 disposed objects", 3, pool.testGetDisposedObjects().size() );
129 
130     }
131 
132     /**
133      * @see junit.framework.TestCase#setUp()
134      */
135     protected void setUp()
136         throws Exception
137     {
138 
139         o1 = new Object();
140         o2 = new Object();
141         o3 = new Object();
142         o4 = new Object();
143         o5 = new Object();
144         o6 = new Object();
145         super.setUp();
146     }
147 
148     /**
149      * @see junit.framework.TestCase#tearDown()
150      */
151     protected void tearDown()
152         throws Exception
153     {
154         pool.dispose();
155         assertTrue( pool.isDisposed() );
156         pool = null;
157         super.tearDown();
158 
159     }
160 
161     class TestObjectPool
162         extends SweeperPool
163     {
164         private Vector<Object> disposedObjects = new Vector<Object>();
165 
166         public TestObjectPool( int maxSize, int minSize, int intialCapacity, int sweepInterval, int triggerSize )
167         {
168             super( maxSize, minSize, intialCapacity, sweepInterval, triggerSize );
169         }
170 
171         public void reset()
172         {
173             disposedObjects.clear();
174         }
175 
176         /**
177          * @see nz.co.bonzo.beans.castor.pool.ObjectPool#objectDisposed(java.lang.Object)
178          */
179         public void objectDisposed( Object obj )
180         {
181             disposedObjects.add( obj );
182         }
183 
184         public Vector<Object> testGetDisposedObjects()
185         {
186             return disposedObjects;
187         }
188 
189     }
190 
191 }