001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.pool;
018
019 import junit.framework.Test;
020 import junit.framework.TestSuite;
021
022 /**
023 * @author Rodney Waldhoff
024 * @author Sandy McArthur
025 * @version $Revision: 606064 $ $Date: 2007-12-20 19:12:02 -0500 (Thu, 20 Dec 2007) $
026 */
027 public class TestBaseObjectPool extends TestObjectPool {
028 private ObjectPool _pool = null;
029
030 public TestBaseObjectPool(String testName) {
031 super(testName);
032 }
033
034 public static Test suite() {
035 return new TestSuite(TestBaseObjectPool.class);
036 }
037
038 protected ObjectPool makeEmptyPool(int mincapacity) {
039 if (this.getClass() != TestBaseObjectPool.class) {
040 fail("Subclasses of TestBaseObjectPool must reimplement this method.");
041 }
042 throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
043 }
044
045 protected ObjectPool makeEmptyPool(final PoolableObjectFactory factory) {
046 if (this.getClass() != TestBaseObjectPool.class) {
047 fail("Subclasses of TestBaseObjectPool must reimplement this method.");
048 }
049 throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
050 }
051
052 protected Object getNthObject(final int n) {
053 if (this.getClass() != TestBaseObjectPool.class) {
054 fail("Subclasses of TestBaseObjectPool must reimplement this method.");
055 }
056 throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation.");
057 }
058
059 protected boolean isLifo() {
060 if (this.getClass() != TestBaseObjectPool.class) {
061 fail("Subclasses of TestBaseObjectPool must reimplement this method.");
062 }
063 return false;
064 }
065
066 protected boolean isFifo() {
067 if (this.getClass() != TestBaseObjectPool.class) {
068 fail("Subclasses of TestBaseObjectPool must reimplement this method.");
069 }
070 return false;
071 }
072
073 // tests
074 public void testUnsupportedOperations() throws Exception {
075 if (!getClass().equals(TestBaseObjectPool.class)) {
076 return; // skip redundant tests
077 }
078 ObjectPool pool = new BaseObjectPool() {
079 public Object borrowObject() {
080 return null;
081 }
082 public void returnObject(Object obj) {
083 }
084 public void invalidateObject(Object obj) {
085 }
086 };
087
088 assertTrue("Negative expected.", pool.getNumIdle() < 0);
089 assertTrue("Negative expected.", pool.getNumActive() < 0);
090
091 try {
092 pool.clear();
093 fail("Expected UnsupportedOperationException");
094 } catch(UnsupportedOperationException e) {
095 // expected
096 }
097
098 try {
099 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 }