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.pool;
18  
19  
20  /**
21   * @author Rodney Waldhoff
22   * @author Sandy McArthur
23   * @version $Revision: 901944 $ $Date: 2010-01-21 17:27:04 -0700 (Thu, 21 Jan 2010) $
24   */
25  public class TestBaseKeyedObjectPool extends TestKeyedObjectPool {
26      private KeyedObjectPool _pool = null;
27  
28      public TestBaseKeyedObjectPool(final String testName) {
29          super(testName);
30      }
31  
32      protected KeyedObjectPool makeEmptyPool(KeyedPoolableObjectFactory factory) {
33          if (this.getClass() != TestBaseKeyedObjectPool.class) {
34              fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
35          }
36          throw new UnsupportedOperationException("BaseKeyedObjectPool isn't a complete implementation.");
37      }
38  
39      /**
40       * Create an {@link KeyedObjectPool} instance
41       * that can contain at least <i>mincapacity</i>
42       * idle and active objects, or
43       * throw {@link IllegalArgumentException}
44       * if such a pool cannot be created.
45       */
46      protected KeyedObjectPool makeEmptyPool(int mincapacity) {
47          if (this.getClass() != TestBaseKeyedObjectPool.class) {
48              fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
49          }
50          throw new UnsupportedOperationException("BaseKeyedObjectPool isn't a complete implementation.");
51      }
52  
53      /**
54       * Return what we expect to be the n<sup>th</sup>
55       * object (zero indexed) created by the pool
56       * for the given key.
57       */
58      protected Object getNthObject(Object key, int n) {
59          if (this.getClass() != TestBaseKeyedObjectPool.class) {
60              fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
61          }
62          throw new UnsupportedOperationException("BaseKeyedObjectPool isn't a complete implementation.");
63      }
64  
65      protected Object makeKey(int n) {
66          if (this.getClass() != TestBaseKeyedObjectPool.class) {
67              fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
68          }
69          throw new UnsupportedOperationException("BaseKeyedObjectPool isn't a complete implementation.");
70      }
71  
72      public void setUp() throws Exception {
73          super.setUp();
74      }
75  
76      public void tearDown() throws Exception {
77          _pool = null;
78          super.tearDown();
79      }
80  
81      public void testUnsupportedOperations() throws Exception {
82          if (!getClass().equals(TestBaseKeyedObjectPool.class)) {
83              return; // skip redundant tests
84          }
85          KeyedObjectPool pool = new BaseKeyedObjectPool() { 
86              public Object borrowObject(Object key) {
87                  return null;
88              }
89              public void returnObject(Object key, Object obj) {
90              }
91              public void invalidateObject(Object key, Object obj) {
92              }            
93          };
94          
95          try {
96              pool.addObject("key");
97              fail("Expected UnsupportedOperationException");
98          } catch(UnsupportedOperationException e) {
99              // expected
100         }
101 
102         assertTrue("Negative expected.", pool.getNumIdle() < 0);
103         assertTrue("Negative expected.", pool.getNumIdle("key") < 0);
104         assertTrue("Negative expected.", pool.getNumActive() < 0);
105         assertTrue("Negative expected.", pool.getNumActive("key") < 0);
106 
107         try {
108             pool.clear();
109             fail("Expected UnsupportedOperationException");
110         } catch(UnsupportedOperationException e) {
111             // expected
112         }
113 
114         try {
115             pool.clear("key");
116             fail("Expected UnsupportedOperationException");
117         } catch(UnsupportedOperationException e) {
118             // expected
119         }
120 
121         try {
122             pool.setFactory(null);
123             fail("Expected UnsupportedOperationException");
124         } catch(UnsupportedOperationException e) {
125             // expected
126         }
127 
128         pool.close(); // a no-op, probably should be remove
129 
130     }
131 
132     protected boolean isLifo() {
133         if (getClass() != TestBaseKeyedObjectPool.class) {
134             fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
135         }
136         return false;
137     }
138 
139     protected boolean isFifo() {
140         if (getClass() != TestBaseKeyedObjectPool.class) {
141             fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
142         }
143         return false;
144     }
145 
146     public void testBaseBorrowReturn() throws Exception {
147         try {
148             _pool = makeEmptyPool(3);
149         } catch(UnsupportedOperationException uoe) {
150             return; // skip this test if unsupported
151         }
152         Object keya = makeKey(0);
153         Object obj0 = _pool.borrowObject(keya);
154         assertEquals(getNthObject(keya,0),obj0);
155         Object obj1 = _pool.borrowObject(keya);
156         assertEquals(getNthObject(keya,1),obj1);
157         Object obj2 = _pool.borrowObject(keya);
158         assertEquals(getNthObject(keya,2),obj2);
159         _pool.returnObject(keya,obj2);
160         obj2 = _pool.borrowObject(keya);
161         assertEquals(getNthObject(keya,2),obj2);
162         _pool.returnObject(keya,obj1);
163         obj1 = _pool.borrowObject(keya);
164         assertEquals(getNthObject(keya,1),obj1);
165         _pool.returnObject(keya,obj0);
166         _pool.returnObject(keya,obj2);
167         obj2 = _pool.borrowObject(keya);
168         if (isLifo()) {
169             assertEquals(getNthObject(keya,2),obj2);
170         }
171         if (isFifo()) {
172             assertEquals(getNthObject(keya,0),obj2);
173         }
174         obj0 = _pool.borrowObject(keya);
175         if (isLifo()) {
176             assertEquals(getNthObject(keya,0),obj0);
177         }
178         if (isFifo()) {
179             assertEquals(getNthObject(keya,2),obj0);
180         }
181     }
182 
183     public void testBaseBorrow() throws Exception {
184         try {
185             _pool = makeEmptyPool(3);
186         } catch(UnsupportedOperationException uoe) {
187             return; // skip this test if unsupported
188         }
189         Object keya = makeKey(0);
190         Object keyb = makeKey(1);
191         assertEquals("1",getNthObject(keya,0),_pool.borrowObject(keya));
192         assertEquals("2",getNthObject(keyb,0),_pool.borrowObject(keyb));
193         assertEquals("3",getNthObject(keyb,1),_pool.borrowObject(keyb));
194         assertEquals("4",getNthObject(keya,1),_pool.borrowObject(keya));
195         assertEquals("5",getNthObject(keyb,2),_pool.borrowObject(keyb));
196         assertEquals("6",getNthObject(keya,2),_pool.borrowObject(keya));
197     }
198 
199     public void testBaseNumActiveNumIdle() throws Exception {
200         try {
201             _pool = makeEmptyPool(3);
202         } catch(UnsupportedOperationException uoe) {
203             return; // skip this test if unsupported
204         }
205         Object keya = makeKey(0);
206         assertEquals(0,_pool.getNumActive(keya));
207         assertEquals(0,_pool.getNumIdle(keya));
208         Object obj0 = _pool.borrowObject(keya);
209         assertEquals(1,_pool.getNumActive(keya));
210         assertEquals(0,_pool.getNumIdle(keya));
211         Object obj1 = _pool.borrowObject(keya);
212         assertEquals(2,_pool.getNumActive(keya));
213         assertEquals(0,_pool.getNumIdle(keya));
214         _pool.returnObject(keya,obj1);
215         assertEquals(1,_pool.getNumActive(keya));
216         assertEquals(1,_pool.getNumIdle(keya));
217         _pool.returnObject(keya,obj0);
218         assertEquals(0,_pool.getNumActive(keya));
219         assertEquals(2,_pool.getNumIdle(keya));
220 
221         assertEquals(0,_pool.getNumActive("xyzzy12345"));
222         assertEquals(0,_pool.getNumIdle("xyzzy12345"));
223     }
224 
225     public void testBaseNumActiveNumIdle2() throws Exception {
226         try {
227             _pool = makeEmptyPool(6);
228         } catch(UnsupportedOperationException uoe) {
229             return; // skip this test if unsupported
230         }
231         Object keya = makeKey(0);
232         Object keyb = makeKey(1);
233         assertEquals(0,_pool.getNumActive());
234         assertEquals(0,_pool.getNumIdle());
235         assertEquals(0,_pool.getNumActive(keya));
236         assertEquals(0,_pool.getNumIdle(keya));
237         assertEquals(0,_pool.getNumActive(keyb));
238         assertEquals(0,_pool.getNumIdle(keyb));
239 
240         Object objA0 = _pool.borrowObject(keya);
241         Object objB0 = _pool.borrowObject(keyb);
242 
243         assertEquals(2,_pool.getNumActive());
244         assertEquals(0,_pool.getNumIdle());
245         assertEquals(1,_pool.getNumActive(keya));
246         assertEquals(0,_pool.getNumIdle(keya));
247         assertEquals(1,_pool.getNumActive(keyb));
248         assertEquals(0,_pool.getNumIdle(keyb));
249 
250         Object objA1 = _pool.borrowObject(keya);
251         Object objB1 = _pool.borrowObject(keyb);
252 
253         assertEquals(4,_pool.getNumActive());
254         assertEquals(0,_pool.getNumIdle());
255         assertEquals(2,_pool.getNumActive(keya));
256         assertEquals(0,_pool.getNumIdle(keya));
257         assertEquals(2,_pool.getNumActive(keyb));
258         assertEquals(0,_pool.getNumIdle(keyb));
259 
260         _pool.returnObject(keya,objA0);
261         _pool.returnObject(keyb,objB0);
262 
263         assertEquals(2,_pool.getNumActive());
264         assertEquals(2,_pool.getNumIdle());
265         assertEquals(1,_pool.getNumActive(keya));
266         assertEquals(1,_pool.getNumIdle(keya));
267         assertEquals(1,_pool.getNumActive(keyb));
268         assertEquals(1,_pool.getNumIdle(keyb));
269 
270         _pool.returnObject(keya,objA1);
271         _pool.returnObject(keyb,objB1);
272 
273         assertEquals(0,_pool.getNumActive());
274         assertEquals(4,_pool.getNumIdle());
275         assertEquals(0,_pool.getNumActive(keya));
276         assertEquals(2,_pool.getNumIdle(keya));
277         assertEquals(0,_pool.getNumActive(keyb));
278         assertEquals(2,_pool.getNumIdle(keyb));
279     }
280 
281     public void testBaseClear() throws Exception {
282         try {
283             _pool = makeEmptyPool(3);
284         } catch(UnsupportedOperationException uoe) {
285             return; // skip this test if unsupported
286         }
287         Object keya = makeKey(0);
288         assertEquals(0,_pool.getNumActive(keya));
289         assertEquals(0,_pool.getNumIdle(keya));
290         Object obj0 = _pool.borrowObject(keya);
291         Object obj1 = _pool.borrowObject(keya);
292         assertEquals(2,_pool.getNumActive(keya));
293         assertEquals(0,_pool.getNumIdle(keya));
294         _pool.returnObject(keya,obj1);
295         _pool.returnObject(keya,obj0);
296         assertEquals(0,_pool.getNumActive(keya));
297         assertEquals(2,_pool.getNumIdle(keya));
298         _pool.clear(keya);
299         assertEquals(0,_pool.getNumActive(keya));
300         assertEquals(0,_pool.getNumIdle(keya));
301         Object obj2 = _pool.borrowObject(keya);
302         assertEquals(getNthObject(keya,2),obj2);
303     }
304 
305     public void testBaseInvalidateObject() throws Exception {
306         try {
307             _pool = makeEmptyPool(3);
308         } catch(UnsupportedOperationException uoe) {
309             return; // skip this test if unsupported
310         }
311         Object keya = makeKey(0);
312         assertEquals(0,_pool.getNumActive(keya));
313         assertEquals(0,_pool.getNumIdle(keya));
314         Object obj0 = _pool.borrowObject(keya);
315         Object obj1 = _pool.borrowObject(keya);
316         assertEquals(2,_pool.getNumActive(keya));
317         assertEquals(0,_pool.getNumIdle(keya));
318         _pool.invalidateObject(keya,obj0);
319         assertEquals(1,_pool.getNumActive(keya));
320         assertEquals(0,_pool.getNumIdle(keya));
321         _pool.invalidateObject(keya,obj1);
322         assertEquals(0,_pool.getNumActive(keya));
323         assertEquals(0,_pool.getNumIdle(keya));
324     }
325 
326     public void testBaseAddObject() throws Exception {
327         try {
328             _pool = makeEmptyPool(3);
329         } catch(UnsupportedOperationException uoe) {
330             return; // skip this test if unsupported
331         }
332         Object key = makeKey(0);
333         try {
334             assertEquals(0,_pool.getNumIdle());
335             assertEquals(0,_pool.getNumActive());
336             assertEquals(0,_pool.getNumIdle(key));
337             assertEquals(0,_pool.getNumActive(key));
338             _pool.addObject(key);
339             assertEquals(1,_pool.getNumIdle());
340             assertEquals(0,_pool.getNumActive());
341             assertEquals(1,_pool.getNumIdle(key));
342             assertEquals(0,_pool.getNumActive(key));
343             Object obj = _pool.borrowObject(key);
344             assertEquals(getNthObject(key,0),obj);
345             assertEquals(0,_pool.getNumIdle());
346             assertEquals(1,_pool.getNumActive());
347             assertEquals(0,_pool.getNumIdle(key));
348             assertEquals(1,_pool.getNumActive(key));
349             _pool.returnObject(key,obj);
350             assertEquals(1,_pool.getNumIdle());
351             assertEquals(0,_pool.getNumActive());
352             assertEquals(1,_pool.getNumIdle(key));
353             assertEquals(0,_pool.getNumActive(key));
354         } catch(UnsupportedOperationException e) {
355             return; // skip this test if one of those calls is unsupported
356         }
357     }
358 }