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