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