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   * @since 3.4.0
37   */
38  class SweeperPoolTest {
39      /** The pool under test */
40      TestObjectPool pool;
41  
42      /** A bunch of object to pool */
43      Object o1;
44  
45      Object o2;
46  
47      Object o3;
48  
49      Object o4;
50  
51      Object o5;
52  
53      Object o6;
54  
55      /**
56       * Test the pool limits it's size, and disposes unneeded objects correctly
57       */
58      @Test
59      void maxSize() {
60          int sweepInterval = 0;
61          int initialCapacity = 5;
62          int maxSize = 2;
63          int minSize = 1;
64          int triggerSize = 2;
65  
66          pool = new TestObjectPool(maxSize, minSize, initialCapacity, sweepInterval, triggerSize);
67  
68          Object tmp = pool.get();
69          assertNull(tmp, "Expected object from pool to be null");
70          pool.put(o1);
71          assertEquals(1, pool.getSize(), "Expected pool to contain 1 object");
72          tmp = pool.get();
73          assertSame(tmp, o1, "Expected returned pool object to be the same as the one put in");
74          pool.put(o1);
75          pool.put(o2);
76          assertEquals(2, pool.getSize(), "Expected pool to contain 2 objects");
77          pool.put(o3);
78          assertEquals(2, pool.getSize(), "Expected pool to contain only a maximum of 2 objects.");
79          assertEquals(1, pool.testGetDisposedObjects().size(), "Expected 1 disposed pool object");
80          tmp = pool.testGetDisposedObjects().iterator().next();
81  
82          tmp = pool.get();
83          assertEquals(1, pool.getSize(), "Expected pool size to be 1 after removing one object");
84          Object tmp2 = pool.get();
85          assertEquals(0, pool.getSize(), "Expected pool size to be 0 after removing 2 objects");
86          assertNotSame(tmp, tmp2, "Expected returned objects to be different");
87      }
88  
89      @Test
90      void sweepAndTrim1() {
91          // test trigger
92          int sweepInterval = 1;
93          int initialCapacity = 5;
94          int maxSize = 5;
95          int minSize = 1;
96          int triggerSize = 2;
97  
98          pool = new TestObjectPool(maxSize, minSize, initialCapacity, sweepInterval, triggerSize);
99          pool.put(o1);
100         pool.put(o2);
101         pool.put(o3);
102         pool.put(o4);
103         // give the sweeper some time to run
104         synchronized (this) {
105             try {
106                 wait(2 * 1000);
107             } catch (InterruptedException e) {
108                 fail("Unexpected exception thrown. e=" + Tracer.traceToString(e));
109             }
110         }
111         assertEquals(1, pool.getSize(), "Expected pool to only contain 1 object");
112         assertEquals(3, pool.testGetDisposedObjects().size(), "Expected 3 disposed objects");
113     }
114 
115     @BeforeEach
116     void setUp() {
117         o1 = new Object();
118         o2 = new Object();
119         o3 = new Object();
120         o4 = new Object();
121         o5 = new Object();
122         o6 = new Object();
123     }
124 
125     @AfterEach
126     void tearDown() {
127         pool.dispose();
128         assertTrue(pool.isDisposed());
129         pool = null;
130     }
131 
132     static class TestObjectPool extends SweeperPool {
133         private Vector<Object> disposedObjects = new Vector<>();
134 
135         public TestObjectPool(int maxSize, int minSize, int intialCapacity, int sweepInterval, int triggerSize) {
136             super(maxSize, minSize, intialCapacity, sweepInterval, triggerSize);
137         }
138 
139         public void reset() {
140             disposedObjects.clear();
141         }
142 
143         public void objectDisposed(Object obj) {
144             disposedObjects.add(obj);
145         }
146 
147         public Vector<Object> testGetDisposedObjects() {
148             return disposedObjects;
149         }
150     }
151 }