View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.pool2;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  import static org.junit.jupiter.api.Assertions.fail;
23  
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   */
28  public class TestBaseObjectPool extends AbstractTestObjectPool {
29  
30      private static final class TestObjectPool extends BaseObjectPool<Object> {
31  
32          @Override
33          public Object borrowObject() {
34              return null;
35          }
36  
37          @Override
38          public void invalidateObject(final Object obj) {
39          }
40  
41          @Override
42          public void returnObject(final Object obj) {
43          }
44      }
45  
46      private ObjectPool<String> pool;
47  
48      /**
49       * @param n Ignored by this implemented. Used by sub-classes.
50       * @return the Nth object (zero indexed)
51       */
52      protected Object getNthObject(final int n) {
53          if (this.getClass() != TestBaseObjectPool.class) {
54              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
55          }
56          throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
57      }
58  
59      protected boolean isFifo() {
60          if (this.getClass() != TestBaseObjectPool.class) {
61              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
62          }
63          return false;
64      }
65  
66      protected boolean isLifo() {
67          if (this.getClass() != TestBaseObjectPool.class) {
68              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
69          }
70          return false;
71      }
72  
73      /**
74       * @param minCapacity Ignored by this implemented. Used by sub-classes.
75       * @return A newly created empty pool
76       */
77      protected <E extends Exception> ObjectPool<String> makeEmptyPool(final int minCapacity) {
78          if (this.getClass() != TestBaseObjectPool.class) {
79              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
80          }
81          throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
82      }
83  
84      @Override
85      protected <E extends Exception> ObjectPool<Object> makeEmptyPool(final PooledObjectFactory<Object> factory) {
86          if (this.getClass() != TestBaseObjectPool.class) {
87              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
88          }
89          throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
90      }
91  
92      @Test
93      public void testBaseAddObject() throws Exception {
94          try {
95              pool = makeEmptyPool(3);
96          } catch (final UnsupportedOperationException e) {
97              return; // skip this test if unsupported
98          }
99          try {
100             assertEquals(0, pool.getNumIdle());
101             assertEquals(0, pool.getNumActive());
102             pool.addObject();
103             assertEquals(1, pool.getNumIdle());
104             assertEquals(0, pool.getNumActive());
105             final String obj = pool.borrowObject();
106             assertEquals(getNthObject(0), obj);
107             assertEquals(0, pool.getNumIdle());
108             assertEquals(1, pool.getNumActive());
109             pool.returnObject(obj);
110             assertEquals(1, pool.getNumIdle());
111             assertEquals(0, pool.getNumActive());
112         } catch (final UnsupportedOperationException e) {
113             return; // skip this test if one of those calls is unsupported
114         } finally {
115             pool.close();
116         }
117     }
118 
119     @Test
120     public void testBaseBorrow() throws Exception {
121         try {
122             pool = makeEmptyPool(3);
123         } catch (final UnsupportedOperationException e) {
124             return; // skip this test if unsupported
125         }
126         assertEquals(getNthObject(0), pool.borrowObject());
127         assertEquals(getNthObject(1), pool.borrowObject());
128         assertEquals(getNthObject(2), pool.borrowObject());
129         pool.close();
130     }
131 
132     @Test
133     public void testBaseBorrowReturn() throws Exception {
134         try {
135             pool = makeEmptyPool(3);
136         } catch (final UnsupportedOperationException e) {
137             return; // skip this test if unsupported
138         }
139         String obj0 = pool.borrowObject();
140         assertEquals(getNthObject(0), obj0);
141         String obj1 = pool.borrowObject();
142         assertEquals(getNthObject(1), obj1);
143         String obj2 = pool.borrowObject();
144         assertEquals(getNthObject(2), obj2);
145         pool.returnObject(obj2);
146         obj2 = pool.borrowObject();
147         assertEquals(getNthObject(2), obj2);
148         pool.returnObject(obj1);
149         obj1 = pool.borrowObject();
150         assertEquals(getNthObject(1), obj1);
151         pool.returnObject(obj0);
152         pool.returnObject(obj2);
153         obj2 = pool.borrowObject();
154         if (isLifo()) {
155             assertEquals(getNthObject(2), obj2);
156         }
157         if (isFifo()) {
158             assertEquals(getNthObject(0), obj2);
159         }
160 
161         obj0 = pool.borrowObject();
162         if (isLifo()) {
163             assertEquals(getNthObject(0), obj0);
164         }
165         if (isFifo()) {
166             assertEquals(getNthObject(2), obj0);
167         }
168         pool.close();
169     }
170 
171     @Test
172     public void testBaseClear() throws Exception {
173         try {
174             pool = makeEmptyPool(3);
175         } catch (final UnsupportedOperationException e) {
176             return; // skip this test if unsupported
177         }
178         assertEquals(0, pool.getNumActive());
179         assertEquals(0, pool.getNumIdle());
180         final String obj0 = pool.borrowObject();
181         final String obj1 = pool.borrowObject();
182         assertEquals(2, pool.getNumActive());
183         assertEquals(0, pool.getNumIdle());
184         pool.returnObject(obj1);
185         pool.returnObject(obj0);
186         assertEquals(0, pool.getNumActive());
187         assertEquals(2, pool.getNumIdle());
188         pool.clear();
189         assertEquals(0, pool.getNumActive());
190         assertEquals(0, pool.getNumIdle());
191         final Object obj2 = pool.borrowObject();
192         assertEquals(getNthObject(2), obj2);
193         pool.close();
194     }
195 
196     @Test
197     public void testBaseClosePool() throws Exception{
198         try {
199             pool = makeEmptyPool(3);
200         } catch (final UnsupportedOperationException e) {
201             return; // skip this test if unsupported
202         }
203         final String obj = pool.borrowObject();
204         pool.returnObject(obj);
205 
206         pool.close();
207         assertThrows(IllegalStateException.class, pool::borrowObject);
208     }
209 
210     @Test
211     public void testBaseInvalidateObject() throws Exception {
212         try {
213             pool = makeEmptyPool(3);
214         } catch (final UnsupportedOperationException e) {
215             return; // skip this test if unsupported
216         }
217         assertEquals(0, pool.getNumActive());
218         assertEquals(0, pool.getNumIdle());
219         final String obj0 = pool.borrowObject();
220         final String obj1 = pool.borrowObject();
221         assertEquals(2, pool.getNumActive());
222         assertEquals(0, pool.getNumIdle());
223         pool.invalidateObject(obj0);
224         assertEquals(1, pool.getNumActive());
225         assertEquals(0, pool.getNumIdle());
226         pool.invalidateObject(obj1);
227         assertEquals(0, pool.getNumActive());
228         assertEquals(0, pool.getNumIdle());
229         pool.close();
230     }
231 
232     @Test
233     public void testBaseNumActiveNumIdle() throws Exception {
234         try {
235             pool = makeEmptyPool(3);
236         } catch (final UnsupportedOperationException e) {
237             return; // skip this test if unsupported
238         }
239         assertEquals(0, pool.getNumActive());
240         assertEquals(0, pool.getNumIdle());
241         final String obj0 = pool.borrowObject();
242         assertEquals(1, pool.getNumActive());
243         assertEquals(0, pool.getNumIdle());
244         final String obj1 = pool.borrowObject();
245         assertEquals(2, pool.getNumActive());
246         assertEquals(0, pool.getNumIdle());
247         pool.returnObject(obj1);
248         assertEquals(1, pool.getNumActive());
249         assertEquals(1, pool.getNumIdle());
250         pool.returnObject(obj0);
251         assertEquals(0, pool.getNumActive());
252         assertEquals(2, pool.getNumIdle());
253         pool.close();
254     }
255 
256     @Test
257     public void testClose() {
258         @SuppressWarnings("resource")
259         final ObjectPool<Object> pool = new TestObjectPool();
260 
261         pool.close();
262         pool.close(); // should not error as of Pool 2.0.
263     }
264 
265     @Test
266     public void testUnsupportedOperations() {
267         if (!getClass().equals(TestBaseObjectPool.class)) {
268             return; // skip redundant tests
269         }
270         try (final ObjectPool<Object> pool = new TestObjectPool()) {
271 
272             assertTrue(pool.getNumIdle() < 0, "Negative expected.");
273             assertTrue(pool.getNumActive() < 0, "Negative expected.");
274 
275             assertThrows(UnsupportedOperationException.class, pool::clear);
276             assertThrows(UnsupportedOperationException.class, pool::addObject);
277         }
278     }
279 }