1   /*
2    * Copyright 1999-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.pool;
17  
18  import junit.framework.TestCase;
19  
20  /***
21   * Abstract {@link TestCase} for {@link ObjectPool} implementations.
22   * @author Rodney Waldhoff
23   * @version $Revision: 383290 $ $Date: 2006-03-05 02:00:15 -0500 (Sun, 05 Mar 2006) $
24   */
25  public abstract class TestObjectPool extends TestCase {
26      public TestObjectPool(String testName) {
27          super(testName);
28      }
29  
30      /*** 
31       * Create an {@link ObjectPool} instance
32       * that can contain at least <i>mincapacity</i>
33       * idle and active objects, or
34       * throw {@link IllegalArgumentException}
35       * if such a pool cannot be created.
36       */
37      protected abstract ObjectPool makeEmptyPool(int mincapacity);
38  
39      /***
40       * Return what we expect to be the n<sup>th</sup>
41       * object (zero indexed) created by the _pool.
42       */
43      protected abstract Object getNthObject(int n);
44      
45      /***
46       * Is the implementations LIFO?
47       * @return
48       */
49      protected abstract boolean isLifo();
50      
51      /***
52       * Is the implementationn FIFO?
53       * @return
54       */
55      protected abstract boolean isFifo();
56  
57      public void setUp() throws Exception {
58      }
59      
60      public void tearDown() throws Exception {
61          _pool = null;
62      }
63      
64      public void testBaseBorrow() throws Exception {
65          try {
66              _pool = makeEmptyPool(3);
67          } catch(IllegalArgumentException e) {
68              return; // skip this test if unsupported
69          }
70          assertEquals(getNthObject(0),_pool.borrowObject());
71          assertEquals(getNthObject(1),_pool.borrowObject());
72          assertEquals(getNthObject(2),_pool.borrowObject());
73      }
74  
75      public void testBaseAddObject() throws Exception {
76          try {
77              _pool = makeEmptyPool(3);
78          } catch(IllegalArgumentException e) {
79              return; // skip this test if unsupported
80          }
81          try {
82              assertEquals(0,_pool.getNumIdle());
83              assertEquals(0,_pool.getNumActive());
84              _pool.addObject();
85              assertEquals(1,_pool.getNumIdle());
86              assertEquals(0,_pool.getNumActive());
87              Object obj = _pool.borrowObject();
88              assertEquals(getNthObject(0),obj);
89              assertEquals(0,_pool.getNumIdle());
90              assertEquals(1,_pool.getNumActive());
91              _pool.returnObject(obj);
92              assertEquals(1,_pool.getNumIdle());
93              assertEquals(0,_pool.getNumActive());
94          } catch(UnsupportedOperationException e) {
95              return; // skip this test if one of those calls is unsupported
96          }
97      }
98      
99      public void testBaseBorrowReturn() throws Exception {
100         try {
101             _pool = makeEmptyPool(3);
102         } catch(IllegalArgumentException e) {
103             return; // skip this test if unsupported
104         }
105         Object obj0 = _pool.borrowObject();
106         assertEquals(getNthObject(0),obj0);
107         Object obj1 = _pool.borrowObject();
108         assertEquals(getNthObject(1),obj1);
109         Object obj2 = _pool.borrowObject();
110         assertEquals(getNthObject(2),obj2);
111         _pool.returnObject(obj2);
112         obj2 = _pool.borrowObject();
113         assertEquals(getNthObject(2),obj2);
114         _pool.returnObject(obj1);
115         obj1 = _pool.borrowObject();
116         assertEquals(getNthObject(1),obj1);
117         _pool.returnObject(obj0);
118         _pool.returnObject(obj2);
119         obj2 = _pool.borrowObject();
120         if (isLifo()) {
121             assertEquals(getNthObject(2),obj2);
122         }
123         if (isFifo()) {
124             assertEquals(getNthObject(0),obj2);
125         }
126             
127         obj0 = _pool.borrowObject();
128         if (isLifo()) {
129             assertEquals(getNthObject(0),obj0);
130         }
131         if (isFifo()) {
132             assertEquals(getNthObject(2),obj0);
133         }
134     }
135 
136     public void testBaseNumActiveNumIdle() throws Exception {
137         try {
138             _pool = makeEmptyPool(3);
139         } catch(IllegalArgumentException e) {
140             return; // skip this test if unsupported
141         }
142         assertEquals(0,_pool.getNumActive());
143         assertEquals(0,_pool.getNumIdle());
144         Object obj0 = _pool.borrowObject();
145         assertEquals(1,_pool.getNumActive());
146         assertEquals(0,_pool.getNumIdle());
147         Object obj1 = _pool.borrowObject();
148         assertEquals(2,_pool.getNumActive());
149         assertEquals(0,_pool.getNumIdle());
150         _pool.returnObject(obj1);
151         assertEquals(1,_pool.getNumActive());
152         assertEquals(1,_pool.getNumIdle());
153         _pool.returnObject(obj0);
154         assertEquals(0,_pool.getNumActive());
155         assertEquals(2,_pool.getNumIdle());
156     }
157 
158     public void testBaseClear() throws Exception {
159         try {
160             _pool = makeEmptyPool(3);
161         } catch(IllegalArgumentException e) {
162             return; // skip this test if unsupported
163         }
164         assertEquals(0,_pool.getNumActive());
165         assertEquals(0,_pool.getNumIdle());
166         Object obj0 = _pool.borrowObject();
167         Object obj1 = _pool.borrowObject();
168         assertEquals(2,_pool.getNumActive());
169         assertEquals(0,_pool.getNumIdle());
170         _pool.returnObject(obj1);
171         _pool.returnObject(obj0);
172         assertEquals(0,_pool.getNumActive());
173         assertEquals(2,_pool.getNumIdle());
174         _pool.clear();
175         assertEquals(0,_pool.getNumActive());
176         assertEquals(0,_pool.getNumIdle());
177         Object obj2 = _pool.borrowObject();
178         assertEquals(getNthObject(2),obj2);
179     }
180 
181     public void testBaseInvalidateObject() throws Exception {
182         try {
183             _pool = makeEmptyPool(3);
184         } catch(IllegalArgumentException e) {
185             return; // skip this test if unsupported
186         }
187         assertEquals(0,_pool.getNumActive());
188         assertEquals(0,_pool.getNumIdle());
189         Object obj0 = _pool.borrowObject();
190         Object obj1 = _pool.borrowObject();
191         assertEquals(2,_pool.getNumActive());
192         assertEquals(0,_pool.getNumIdle());
193         _pool.invalidateObject(obj0);
194         assertEquals(1,_pool.getNumActive());
195         assertEquals(0,_pool.getNumIdle());
196         _pool.invalidateObject(obj1);
197         assertEquals(0,_pool.getNumActive());
198         assertEquals(0,_pool.getNumIdle());
199     }
200     
201     public void testBaseClosePool() throws Exception {
202         try {
203             _pool = makeEmptyPool(3);
204         } catch(IllegalArgumentException e) {
205             return; // skip this test if unsupported
206         }
207         Object obj = _pool.borrowObject();
208         _pool.returnObject(obj);
209         
210         _pool.close();
211         try {
212             _pool.borrowObject();
213             fail("Expected IllegalStateException");
214         } catch(IllegalStateException e) {
215             // expected
216         }
217     }
218 
219     public void testBaseCantCloseTwice() throws Exception {
220         try {
221             _pool = makeEmptyPool(3);
222         } catch(IllegalArgumentException e) {
223             return; // skip this test if unsupported
224         }
225         Object obj = _pool.borrowObject();
226         _pool.returnObject(obj);
227         
228         _pool.close();
229         try {
230             _pool.close();
231             fail("Expected IllegalStateException");
232         } catch(IllegalStateException e) {
233             // expected
234         }
235     }
236 
237     private ObjectPool _pool = null;
238 }