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 java.util.Vector;
20  
21  import org.junit.jupiter.api.AfterEach;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  
25  import static org.junit.jupiter.api.Assertions.assertEquals;
26  import static org.junit.jupiter.api.Assertions.assertNotSame;
27  import static org.junit.jupiter.api.Assertions.assertNull;
28  import static org.junit.jupiter.api.Assertions.assertSame;
29  import static org.junit.jupiter.api.Assertions.assertTrue;
30  import static org.junit.jupiter.api.Assertions.fail;
31  
32  /**
33   * Created on 21/06/2003
34   *
35   * @author <a href="mailto:bert@tuaworks.co.nz">Bert van Brakel</a>
36   * @version $Id: $Id
37   * @since 3.4.0
38   */
39  public class SweeperPoolTest {
40      /** The pool under test */
41      TestObjectPool pool;
42  
43      /** A bunch of object to pool */
44      Object o1;
45  
46      Object o2;
47  
48      Object o3;
49  
50      Object o4;
51  
52      Object o5;
53  
54      Object o6;
55  
56      /**
57       * Test the pool limits it's size, and disposes unneeded objects correctly
58       */
59      @Test
60      public void testMaxSize() {
61          int sweepInterval = 0;
62          int initialCapacity = 5;
63          int maxSize = 2;
64          int minSize = 1;
65          int triggerSize = 2;
66  
67          pool = new TestObjectPool(maxSize, minSize, initialCapacity, sweepInterval, triggerSize);
68  
69          Object tmp = pool.get();
70          assertNull(tmp, "Expected object from pool to be null");
71          pool.put(o1);
72          assertEquals(1, pool.getSize(), "Expected pool to contain 1 object");
73          tmp = pool.get();
74          assertSame(tmp, o1, "Expected returned pool object to be the same as the one put in");
75          pool.put(o1);
76          pool.put(o2);
77          assertEquals(2, pool.getSize(), "Expected pool to contain 2 objects");
78          pool.put(o3);
79          assertEquals(2, pool.getSize(), "Expected pool to contain only a maximum of 2 objects.");
80          assertEquals(1, pool.testGetDisposedObjects().size(), "Expected 1 disposed pool object");
81          tmp = pool.testGetDisposedObjects().iterator().next();
82  
83          tmp = pool.get();
84          assertEquals(1, pool.getSize(), "Expected pool size to be 1 after removing one object");
85          Object tmp2 = pool.get();
86          assertEquals(0, pool.getSize(), "Expected pool size to be 0 after removing 2 objects");
87          assertNotSame(tmp, tmp2, "Expected returned objects to be different");
88      }
89  
90      /**
91       * <p>testSweepAndTrim1.</p>
92       */
93      @org.junit.jupiter.api.Test
94      public void testSweepAndTrim1() {
95          // test trigger
96          int sweepInterval = 1;
97          int initialCapacity = 5;
98          int maxSize = 5;
99          int minSize = 1;
100         int triggerSize = 2;
101 
102         pool = new TestObjectPool(maxSize, minSize, initialCapacity, sweepInterval, triggerSize);
103         pool.put(o1);
104         pool.put(o2);
105         pool.put(o3);
106         pool.put(o4);
107         // give the sweeper some time to run
108         synchronized (this) {
109             try {
110                 wait(2 * 1000);
111             } catch (InterruptedException e) {
112                 fail("Unexpected exception thrown. e=" + Tracer.traceToString(e));
113             }
114         }
115         assertEquals(1, pool.getSize(), "Expected pool to only contain 1 object");
116         assertEquals(3, pool.testGetDisposedObjects().size(), "Expected 3 disposed objects");
117     }
118 
119     /**
120      * <p>setUp.</p>
121      *
122      * @throws java.lang.Exception if any.
123      */
124     @BeforeEach
125     public void setUp() throws Exception {
126         o1 = new Object();
127         o2 = new Object();
128         o3 = new Object();
129         o4 = new Object();
130         o5 = new Object();
131         o6 = new Object();
132     }
133 
134     /**
135      * <p>tearDown.</p>
136      *
137      * @throws java.lang.Exception if any.
138      */
139     @AfterEach
140     public void tearDown() throws Exception {
141         pool.dispose();
142         assertTrue(pool.isDisposed());
143         pool = null;
144     }
145 
146     class TestObjectPool extends SweeperPool {
147         private Vector<Object> disposedObjects = new Vector<Object>();
148 
149         public TestObjectPool(int maxSize, int minSize, int intialCapacity, int sweepInterval, int triggerSize) {
150             super(maxSize, minSize, intialCapacity, sweepInterval, triggerSize);
151         }
152 
153         public void reset() {
154             disposedObjects.clear();
155         }
156 
157         /**
158          * @see nz.co.bonzo.beans.castor.pool.ObjectPool#objectDisposed(java.lang.Object)
159          */
160         public void objectDisposed(Object obj) {
161             disposedObjects.add(obj);
162         }
163 
164         public Vector<Object> testGetDisposedObjects() {
165             return disposedObjects;
166         }
167     }
168 }