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 TestBaseObjectPool extends TestObjectPool {
28      private ObjectPool _pool = null;
29  
30      public TestBaseObjectPool(String testName) {
31          super(testName);
32      }
33  
34      public static Test suite() {
35          return new TestSuite(TestBaseObjectPool.class);
36      }
37      
38      protected ObjectPool makeEmptyPool(int mincapacity) {
39          if (this.getClass() != TestBaseObjectPool.class) {
40              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
41          }
42          throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
43      }
44  
45      protected ObjectPool makeEmptyPool(final PoolableObjectFactory factory) {
46          if (this.getClass() != TestBaseObjectPool.class) {
47              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
48          }
49          throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
50      }
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 isLifo() {
60          if (this.getClass() != TestBaseObjectPool.class) {
61              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
62          }
63          return false;
64      }
65  
66      protected boolean isFifo() {
67          if (this.getClass() != TestBaseObjectPool.class) {
68              fail("Subclasses of TestBaseObjectPool must reimplement this method.");
69          }
70          return false;
71      }
72  
73      // tests
74      public void testUnsupportedOperations() throws Exception {
75          if (!getClass().equals(TestBaseObjectPool.class)) {
76              return; // skip redundant tests
77          }
78          ObjectPool pool = new BaseObjectPool() { 
79              public Object borrowObject() {
80                  return null;
81              }
82              public void returnObject(Object obj) {
83              }
84              public void invalidateObject(Object obj) {
85              }            
86          };   
87  
88          assertTrue("Negative expected.", pool.getNumIdle() < 0);
89          assertTrue("Negative expected.", pool.getNumActive() < 0);
90  
91          try {
92              pool.clear();
93              fail("Expected UnsupportedOperationException");
94          } catch(UnsupportedOperationException e) {
95              // expected
96          }
97  
98          try {
99              pool.addObject();
100             fail("Expected UnsupportedOperationException");
101         } catch(UnsupportedOperationException e) {
102             // expected
103         }
104 
105         try {
106             pool.setFactory(null);
107             fail("Expected UnsupportedOperationException");
108         } catch(UnsupportedOperationException e) {
109             // expected
110         }
111     }
112 
113     public void testClose() throws Exception {
114         ObjectPool pool = new BaseObjectPool() {
115             public Object borrowObject() {
116                 return null;
117             }
118             public void returnObject(Object obj) {
119             }
120             public void invalidateObject(Object obj) {
121             }
122         };
123 
124         pool.close();
125         pool.close(); // should not error as of Pool 2.0.
126     }
127 
128     public void testBaseBorrow() throws Exception {
129         try {
130             _pool = makeEmptyPool(3);
131         } catch(UnsupportedOperationException e) {
132             return; // skip this test if unsupported
133         }
134         assertEquals(getNthObject(0),_pool.borrowObject());
135         assertEquals(getNthObject(1),_pool.borrowObject());
136         assertEquals(getNthObject(2),_pool.borrowObject());
137     }
138 
139     public void testBaseAddObject() throws Exception {
140         try {
141             _pool = makeEmptyPool(3);
142         } catch(UnsupportedOperationException e) {
143             return; // skip this test if unsupported
144         }
145         try {
146             assertEquals(0,_pool.getNumIdle());
147             assertEquals(0,_pool.getNumActive());
148             _pool.addObject();
149             assertEquals(1,_pool.getNumIdle());
150             assertEquals(0,_pool.getNumActive());
151             Object obj = _pool.borrowObject();
152             assertEquals(getNthObject(0),obj);
153             assertEquals(0,_pool.getNumIdle());
154             assertEquals(1,_pool.getNumActive());
155             _pool.returnObject(obj);
156             assertEquals(1,_pool.getNumIdle());
157             assertEquals(0,_pool.getNumActive());
158         } catch(UnsupportedOperationException e) {
159             return; // skip this test if one of those calls is unsupported
160         }
161     }
162 
163     public void testBaseBorrowReturn() throws Exception {
164         try {
165             _pool = makeEmptyPool(3);
166         } catch(UnsupportedOperationException e) {
167             return; // skip this test if unsupported
168         }
169         Object obj0 = _pool.borrowObject();
170         assertEquals(getNthObject(0),obj0);
171         Object obj1 = _pool.borrowObject();
172         assertEquals(getNthObject(1),obj1);
173         Object obj2 = _pool.borrowObject();
174         assertEquals(getNthObject(2),obj2);
175         _pool.returnObject(obj2);
176         obj2 = _pool.borrowObject();
177         assertEquals(getNthObject(2),obj2);
178         _pool.returnObject(obj1);
179         obj1 = _pool.borrowObject();
180         assertEquals(getNthObject(1),obj1);
181         _pool.returnObject(obj0);
182         _pool.returnObject(obj2);
183         obj2 = _pool.borrowObject();
184         if (isLifo()) {
185             assertEquals(getNthObject(2),obj2);
186         }
187         if (isFifo()) {
188             assertEquals(getNthObject(0),obj2);
189         }
190 
191         obj0 = _pool.borrowObject();
192         if (isLifo()) {
193             assertEquals(getNthObject(0),obj0);
194         }
195         if (isFifo()) {
196             assertEquals(getNthObject(2),obj0);
197         }
198     }
199 
200     public void testBaseNumActiveNumIdle() throws Exception {
201         try {
202             _pool = makeEmptyPool(3);
203         } catch(UnsupportedOperationException e) {
204             return; // skip this test if unsupported
205         }
206         assertEquals(0,_pool.getNumActive());
207         assertEquals(0,_pool.getNumIdle());
208         Object obj0 = _pool.borrowObject();
209         assertEquals(1,_pool.getNumActive());
210         assertEquals(0,_pool.getNumIdle());
211         Object obj1 = _pool.borrowObject();
212         assertEquals(2,_pool.getNumActive());
213         assertEquals(0,_pool.getNumIdle());
214         _pool.returnObject(obj1);
215         assertEquals(1,_pool.getNumActive());
216         assertEquals(1,_pool.getNumIdle());
217         _pool.returnObject(obj0);
218         assertEquals(0,_pool.getNumActive());
219         assertEquals(2,_pool.getNumIdle());
220     }
221 
222     public void testBaseClear() throws Exception {
223         try {
224             _pool = makeEmptyPool(3);
225         } catch(UnsupportedOperationException e) {
226             return; // skip this test if unsupported
227         }
228         assertEquals(0,_pool.getNumActive());
229         assertEquals(0,_pool.getNumIdle());
230         Object obj0 = _pool.borrowObject();
231         Object obj1 = _pool.borrowObject();
232         assertEquals(2,_pool.getNumActive());
233         assertEquals(0,_pool.getNumIdle());
234         _pool.returnObject(obj1);
235         _pool.returnObject(obj0);
236         assertEquals(0,_pool.getNumActive());
237         assertEquals(2,_pool.getNumIdle());
238         _pool.clear();
239         assertEquals(0,_pool.getNumActive());
240         assertEquals(0,_pool.getNumIdle());
241         Object obj2 = _pool.borrowObject();
242         assertEquals(getNthObject(2),obj2);
243     }
244 
245     public void testBaseInvalidateObject() throws Exception {
246         try {
247             _pool = makeEmptyPool(3);
248         } catch(UnsupportedOperationException e) {
249             return; // skip this test if unsupported
250         }
251         assertEquals(0,_pool.getNumActive());
252         assertEquals(0,_pool.getNumIdle());
253         Object obj0 = _pool.borrowObject();
254         Object obj1 = _pool.borrowObject();
255         assertEquals(2,_pool.getNumActive());
256         assertEquals(0,_pool.getNumIdle());
257         _pool.invalidateObject(obj0);
258         assertEquals(1,_pool.getNumActive());
259         assertEquals(0,_pool.getNumIdle());
260         _pool.invalidateObject(obj1);
261         assertEquals(0,_pool.getNumActive());
262         assertEquals(0,_pool.getNumIdle());
263     }
264 
265     public void testBaseClosePool() throws Exception {
266         try {
267             _pool = makeEmptyPool(3);
268         } catch(UnsupportedOperationException e) {
269             return; // skip this test if unsupported
270         }
271         Object obj = _pool.borrowObject();
272         _pool.returnObject(obj);
273 
274         _pool.close();
275         try {
276             _pool.borrowObject();
277             fail("Expected IllegalStateException");
278         } catch(IllegalStateException e) {
279             // expected
280         }
281     }
282 }