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    *      https://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       * This implementation either fails or throws UnsupportedOperationException.
75       *
76       * @param <E> The exception type.
77       * @param minCapacity Ignored by this implemented. Used by sub-classes.
78       * @return A newly created empty pool
79       */
80      protected <E extends Exception> ObjectPool<String> makeEmptyPool(final int minCapacity) {
81          if (this.getClass() != TestBaseObjectPool.class) {
82              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
83          }
84          throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
85      }
86  
87      @Override
88      protected <E extends Exception> ObjectPool<Object> makeEmptyPool(final PooledObjectFactory<Object> factory) {
89          if (this.getClass() != TestBaseObjectPool.class) {
90              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
91          }
92          throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
93      }
94  
95      @Test
96      void testBaseAddObject() throws Exception {
97          try {
98              pool = makeEmptyPool(3);
99          } catch (final UnsupportedOperationException e) {
100             return; // skip this test if unsupported
101         }
102         try {
103             assertEquals(0, pool.getNumIdle());
104             assertEquals(0, pool.getNumActive());
105             pool.addObject();
106             assertEquals(1, pool.getNumIdle());
107             assertEquals(0, pool.getNumActive());
108             final String obj = pool.borrowObject();
109             assertEquals(getNthObject(0), obj);
110             assertEquals(0, pool.getNumIdle());
111             assertEquals(1, pool.getNumActive());
112             pool.returnObject(obj);
113             assertEquals(1, pool.getNumIdle());
114             assertEquals(0, pool.getNumActive());
115         } catch (final UnsupportedOperationException e) {
116             return; // skip this test if one of those calls is unsupported
117         } finally {
118             pool.close();
119         }
120     }
121 
122     @Test
123     void testBaseBorrow() throws Exception {
124         try {
125             pool = makeEmptyPool(3);
126         } catch (final UnsupportedOperationException e) {
127             return; // skip this test if unsupported
128         }
129         assertEquals(getNthObject(0), pool.borrowObject());
130         assertEquals(getNthObject(1), pool.borrowObject());
131         assertEquals(getNthObject(2), pool.borrowObject());
132         pool.close();
133     }
134 
135     @Test
136     void testBaseBorrowReturn() throws Exception {
137         try {
138             pool = makeEmptyPool(3);
139         } catch (final UnsupportedOperationException e) {
140             return; // skip this test if unsupported
141         }
142         String obj0 = pool.borrowObject();
143         assertEquals(getNthObject(0), obj0);
144         String obj1 = pool.borrowObject();
145         assertEquals(getNthObject(1), obj1);
146         String obj2 = pool.borrowObject();
147         assertEquals(getNthObject(2), obj2);
148         pool.returnObject(obj2);
149         obj2 = pool.borrowObject();
150         assertEquals(getNthObject(2), obj2);
151         pool.returnObject(obj1);
152         obj1 = pool.borrowObject();
153         assertEquals(getNthObject(1), obj1);
154         pool.returnObject(obj0);
155         pool.returnObject(obj2);
156         obj2 = pool.borrowObject();
157         if (isLifo()) {
158             assertEquals(getNthObject(2), obj2);
159         }
160         if (isFifo()) {
161             assertEquals(getNthObject(0), obj2);
162         }
163 
164         obj0 = pool.borrowObject();
165         if (isLifo()) {
166             assertEquals(getNthObject(0), obj0);
167         }
168         if (isFifo()) {
169             assertEquals(getNthObject(2), obj0);
170         }
171         pool.close();
172     }
173 
174     @Test
175     void testBaseClear() throws Exception {
176         try {
177             pool = makeEmptyPool(3);
178         } catch (final UnsupportedOperationException e) {
179             return; // skip this test if unsupported
180         }
181         assertEquals(0, pool.getNumActive());
182         assertEquals(0, pool.getNumIdle());
183         final String obj0 = pool.borrowObject();
184         final String obj1 = pool.borrowObject();
185         assertEquals(2, pool.getNumActive());
186         assertEquals(0, pool.getNumIdle());
187         pool.returnObject(obj1);
188         pool.returnObject(obj0);
189         assertEquals(0, pool.getNumActive());
190         assertEquals(2, pool.getNumIdle());
191         pool.clear();
192         assertEquals(0, pool.getNumActive());
193         assertEquals(0, pool.getNumIdle());
194         final Object obj2 = pool.borrowObject();
195         assertEquals(getNthObject(2), obj2);
196         pool.close();
197     }
198 
199     @Test
200     void testBaseClosePool() throws Exception {
201         try {
202             pool = makeEmptyPool(3);
203         } catch (final UnsupportedOperationException e) {
204             return; // skip this test if unsupported
205         }
206         final String obj = pool.borrowObject();
207         pool.returnObject(obj);
208         pool.close();
209         assertThrows(IllegalStateException.class, pool::borrowObject);
210     }
211 
212     @Test
213     void testBaseInvalidateObject() throws Exception {
214         try {
215             pool = makeEmptyPool(3);
216         } catch (final UnsupportedOperationException e) {
217             return; // skip this test if unsupported
218         }
219         assertEquals(0, pool.getNumActive());
220         assertEquals(0, pool.getNumIdle());
221         final String obj0 = pool.borrowObject();
222         final String obj1 = pool.borrowObject();
223         assertEquals(2, pool.getNumActive());
224         assertEquals(0, pool.getNumIdle());
225         pool.invalidateObject(obj0);
226         assertEquals(1, pool.getNumActive());
227         pool.invalidateObject(obj1);
228         assertEquals(0, pool.getNumActive());
229         pool.close();
230     }
231 
232     @Test
233     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     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     void testUnsupportedOperations() {
267         if (!getClass().equals(TestBaseObjectPool.class)) {
268             return; // skip redundant tests
269         }
270         try (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 }