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 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       *
51       * @return the Nth object (zero indexed)
52       */
53      protected Object getNthObject(final int n) {
54          if (this.getClass() != TestBaseObjectPool.class) {
55              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
56          }
57          throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
58      }
59  
60      protected boolean isFifo() {
61          if (this.getClass() != TestBaseObjectPool.class) {
62              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
63          }
64          return false;
65      }
66  
67      protected boolean isLifo() {
68          if (this.getClass() != TestBaseObjectPool.class) {
69              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
70          }
71          return false;
72      }
73  
74      /**
75       * @param minCapacity Ignored by this implemented. Used by sub-classes.
76       *
77       * @return A newly created empty pool
78       */
79      protected <E extends Exception> ObjectPool<String> makeEmptyPool(final int minCapacity) {
80          if (this.getClass() != TestBaseObjectPool.class) {
81              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
82          }
83          throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
84      }
85  
86      @Override
87      protected <E extends Exception> ObjectPool<Object> makeEmptyPool(final PooledObjectFactory<Object> factory) {
88          if (this.getClass() != TestBaseObjectPool.class) {
89              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
90          }
91          throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
92      }
93  
94      @Test
95      public void testBaseAddObject() throws Exception {
96          try {
97              pool = makeEmptyPool(3);
98          } catch (final UnsupportedOperationException e) {
99              return; // skip this test if unsupported
100         }
101         try {
102             assertEquals(0, pool.getNumIdle());
103             assertEquals(0, pool.getNumActive());
104             pool.addObject();
105             assertEquals(1, pool.getNumIdle());
106             assertEquals(0, pool.getNumActive());
107             final String obj = pool.borrowObject();
108             assertEquals(getNthObject(0), obj);
109             assertEquals(0, pool.getNumIdle());
110             assertEquals(1, pool.getNumActive());
111             pool.returnObject(obj);
112             assertEquals(1, pool.getNumIdle());
113             assertEquals(0, pool.getNumActive());
114         } catch (final UnsupportedOperationException e) {
115             return; // skip this test if one of those calls is unsupported
116         } finally {
117             pool.close();
118         }
119     }
120 
121     @Test
122     public void testBaseBorrow() throws Exception {
123         try {
124             pool = makeEmptyPool(3);
125         } catch (final UnsupportedOperationException e) {
126             return; // skip this test if unsupported
127         }
128         assertEquals(getNthObject(0), pool.borrowObject());
129         assertEquals(getNthObject(1), pool.borrowObject());
130         assertEquals(getNthObject(2), pool.borrowObject());
131         pool.close();
132     }
133 
134     @Test
135     public void testBaseBorrowReturn() throws Exception {
136         try {
137             pool = makeEmptyPool(3);
138         } catch (final UnsupportedOperationException e) {
139             return; // skip this test if unsupported
140         }
141         String obj0 = pool.borrowObject();
142         assertEquals(getNthObject(0), obj0);
143         String obj1 = pool.borrowObject();
144         assertEquals(getNthObject(1), obj1);
145         String obj2 = pool.borrowObject();
146         assertEquals(getNthObject(2), obj2);
147         pool.returnObject(obj2);
148         obj2 = pool.borrowObject();
149         assertEquals(getNthObject(2), obj2);
150         pool.returnObject(obj1);
151         obj1 = pool.borrowObject();
152         assertEquals(getNthObject(1), obj1);
153         pool.returnObject(obj0);
154         pool.returnObject(obj2);
155         obj2 = pool.borrowObject();
156         if (isLifo()) {
157             assertEquals(getNthObject(2), obj2);
158         }
159         if (isFifo()) {
160             assertEquals(getNthObject(0), obj2);
161         }
162 
163         obj0 = pool.borrowObject();
164         if (isLifo()) {
165             assertEquals(getNthObject(0), obj0);
166         }
167         if (isFifo()) {
168             assertEquals(getNthObject(2), obj0);
169         }
170         pool.close();
171     }
172 
173     @Test
174     public void testBaseClear() throws Exception {
175         try {
176             pool = makeEmptyPool(3);
177         } catch (final UnsupportedOperationException e) {
178             return; // skip this test if unsupported
179         }
180         assertEquals(0, pool.getNumActive());
181         assertEquals(0, pool.getNumIdle());
182         final String obj0 = pool.borrowObject();
183         final String obj1 = pool.borrowObject();
184         assertEquals(2, pool.getNumActive());
185         assertEquals(0, pool.getNumIdle());
186         pool.returnObject(obj1);
187         pool.returnObject(obj0);
188         assertEquals(0, pool.getNumActive());
189         assertEquals(2, pool.getNumIdle());
190         pool.clear();
191         assertEquals(0, pool.getNumActive());
192         assertEquals(0, pool.getNumIdle());
193         final Object obj2 = pool.borrowObject();
194         assertEquals(getNthObject(2), obj2);
195         pool.close();
196     }
197 
198     @Test
199     public void testBaseClosePool() throws Exception{
200         try {
201             pool = makeEmptyPool(3);
202         } catch (final UnsupportedOperationException e) {
203             return; // skip this test if unsupported
204         }
205         final String obj = pool.borrowObject();
206         pool.returnObject(obj);
207 
208         pool.close();
209         assertThrows(IllegalStateException.class, pool::borrowObject);
210     }
211 
212     @Test
213     public 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         assertEquals(0, pool.getNumIdle());
228         pool.invalidateObject(obj1);
229         assertEquals(0, pool.getNumActive());
230         assertEquals(0, pool.getNumIdle());
231         pool.close();
232     }
233 
234     @Test
235     public void testBaseNumActiveNumIdle() throws Exception {
236         try {
237             pool = makeEmptyPool(3);
238         } catch (final UnsupportedOperationException e) {
239             return; // skip this test if unsupported
240         }
241         assertEquals(0, pool.getNumActive());
242         assertEquals(0, pool.getNumIdle());
243         final String obj0 = pool.borrowObject();
244         assertEquals(1, pool.getNumActive());
245         assertEquals(0, pool.getNumIdle());
246         final String obj1 = pool.borrowObject();
247         assertEquals(2, pool.getNumActive());
248         assertEquals(0, pool.getNumIdle());
249         pool.returnObject(obj1);
250         assertEquals(1, pool.getNumActive());
251         assertEquals(1, pool.getNumIdle());
252         pool.returnObject(obj0);
253         assertEquals(0, pool.getNumActive());
254         assertEquals(2, pool.getNumIdle());
255         pool.close();
256     }
257 
258     @Test
259     public void testClose() {
260         @SuppressWarnings("resource")
261         final ObjectPool<Object> pool = new TestObjectPool();
262 
263         pool.close();
264         pool.close(); // should not error as of Pool 2.0.
265     }
266 
267     @Test
268     public void testUnsupportedOperations() {
269         if (!getClass().equals(TestBaseObjectPool.class)) {
270             return; // skip redundant tests
271         }
272         try (final ObjectPool<Object> pool = new TestObjectPool()) {
273 
274             assertTrue(pool.getNumIdle() < 0, "Negative expected.");
275             assertTrue(pool.getNumActive() < 0, "Negative expected.");
276 
277             assertThrows(UnsupportedOperationException.class, pool::clear);
278             assertThrows(UnsupportedOperationException.class, pool::addObject);
279         }
280     }
281 }