001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.pool;
017:
018: import junit.framework.TestCase;
019:
020: /**
021: * Abstract {@link TestCase} for {@link ObjectPool} implementations.
022: * @author Rodney Waldhoff
023: * @version $Revision: 383290 $ $Date: 2006-03-05 02:00:15 -0500 (Sun, 05 Mar 2006) $
024: */
025: public abstract class TestKeyedObjectPool extends TestCase {
026: public TestKeyedObjectPool(String testName) {
027: super (testName);
028: }
029:
030: /**
031: * Create an {@link KeyedObjectPool} instance
032: * that can contain at least <i>mincapacity</i>
033: * idle and active objects, or
034: * throw {@link IllegalArgumentException}
035: * if such a pool cannot be created.
036: */
037: protected abstract KeyedObjectPool makeEmptyPool(int mincapacity);
038:
039: /**
040: * Return what we expect to be the n<sup>th</sup>
041: * object (zero indexed) created by the pool
042: * for the given key.
043: */
044: protected abstract Object getNthObject(Object key, int n);
045:
046: protected abstract Object makeKey(int n);
047:
048: /**
049: * Is the implementations LIFO?
050: * @return
051: */
052: protected abstract boolean isLifo();
053:
054: /**
055: * Is the implementationn FIFO?
056: * @return
057: */
058: protected abstract boolean isFifo();
059:
060: public void setUp() throws Exception {
061: }
062:
063: public void tearDown() throws Exception {
064: _pool = null;
065: }
066:
067: public void testBaseBorrow() throws Exception {
068: try {
069: _pool = makeEmptyPool(3);
070: } catch (IllegalArgumentException e) {
071: return; // skip this test if unsupported
072: }
073: Object keya = makeKey(0);
074: Object keyb = makeKey(1);
075: assertEquals("1", getNthObject(keya, 0), _pool
076: .borrowObject(keya));
077: assertEquals("2", getNthObject(keyb, 0), _pool
078: .borrowObject(keyb));
079: assertEquals("3", getNthObject(keyb, 1), _pool
080: .borrowObject(keyb));
081: assertEquals("4", getNthObject(keya, 1), _pool
082: .borrowObject(keya));
083: assertEquals("5", getNthObject(keyb, 2), _pool
084: .borrowObject(keyb));
085: assertEquals("6", getNthObject(keya, 2), _pool
086: .borrowObject(keya));
087: }
088:
089: public void testBaseBorrowReturn() throws Exception {
090: try {
091: _pool = makeEmptyPool(3);
092: } catch (IllegalArgumentException e) {
093: return; // skip this test if unsupported
094: }
095: Object keya = makeKey(0);
096: Object obj0 = _pool.borrowObject(keya);
097: assertEquals(getNthObject(keya, 0), obj0);
098: Object obj1 = _pool.borrowObject(keya);
099: assertEquals(getNthObject(keya, 1), obj1);
100: Object obj2 = _pool.borrowObject(keya);
101: assertEquals(getNthObject(keya, 2), obj2);
102: _pool.returnObject(keya, obj2);
103: obj2 = _pool.borrowObject(keya);
104: assertEquals(getNthObject(keya, 2), obj2);
105: _pool.returnObject(keya, obj1);
106: obj1 = _pool.borrowObject(keya);
107: assertEquals(getNthObject(keya, 1), obj1);
108: _pool.returnObject(keya, obj0);
109: _pool.returnObject(keya, obj2);
110: obj2 = _pool.borrowObject(keya);
111: if (isLifo()) {
112: assertEquals(getNthObject(keya, 2), obj2);
113: }
114: if (isFifo()) {
115: assertEquals(getNthObject(keya, 0), obj2);
116: }
117: obj0 = _pool.borrowObject(keya);
118: if (isLifo()) {
119: assertEquals(getNthObject(keya, 0), obj0);
120: }
121: if (isFifo()) {
122: assertEquals(getNthObject(keya, 2), obj0);
123: }
124: }
125:
126: public void testBaseNumActiveNumIdle() throws Exception {
127: try {
128: _pool = makeEmptyPool(3);
129: } catch (IllegalArgumentException e) {
130: return; // skip this test if unsupported
131: }
132: Object keya = makeKey(0);
133: assertEquals(0, _pool.getNumActive(keya));
134: assertEquals(0, _pool.getNumIdle(keya));
135: Object obj0 = _pool.borrowObject(keya);
136: assertEquals(1, _pool.getNumActive(keya));
137: assertEquals(0, _pool.getNumIdle(keya));
138: Object obj1 = _pool.borrowObject(keya);
139: assertEquals(2, _pool.getNumActive(keya));
140: assertEquals(0, _pool.getNumIdle(keya));
141: _pool.returnObject(keya, obj1);
142: assertEquals(1, _pool.getNumActive(keya));
143: assertEquals(1, _pool.getNumIdle(keya));
144: _pool.returnObject(keya, obj0);
145: assertEquals(0, _pool.getNumActive(keya));
146: assertEquals(2, _pool.getNumIdle(keya));
147:
148: assertEquals(0, _pool.getNumActive("xyzzy12345"));
149: assertEquals(0, _pool.getNumIdle("xyzzy12345"));
150: }
151:
152: public void testBaseNumActiveNumIdle2() throws Exception {
153: try {
154: _pool = makeEmptyPool(6);
155: } catch (IllegalArgumentException e) {
156: return; // skip this test if unsupported
157: }
158: Object keya = makeKey(0);
159: Object keyb = makeKey(1);
160: assertEquals(0, _pool.getNumActive());
161: assertEquals(0, _pool.getNumIdle());
162: assertEquals(0, _pool.getNumActive(keya));
163: assertEquals(0, _pool.getNumIdle(keya));
164: assertEquals(0, _pool.getNumActive(keyb));
165: assertEquals(0, _pool.getNumIdle(keyb));
166:
167: Object objA0 = _pool.borrowObject(keya);
168: Object objB0 = _pool.borrowObject(keyb);
169:
170: assertEquals(2, _pool.getNumActive());
171: assertEquals(0, _pool.getNumIdle());
172: assertEquals(1, _pool.getNumActive(keya));
173: assertEquals(0, _pool.getNumIdle(keya));
174: assertEquals(1, _pool.getNumActive(keyb));
175: assertEquals(0, _pool.getNumIdle(keyb));
176:
177: Object objA1 = _pool.borrowObject(keya);
178: Object objB1 = _pool.borrowObject(keyb);
179:
180: assertEquals(4, _pool.getNumActive());
181: assertEquals(0, _pool.getNumIdle());
182: assertEquals(2, _pool.getNumActive(keya));
183: assertEquals(0, _pool.getNumIdle(keya));
184: assertEquals(2, _pool.getNumActive(keyb));
185: assertEquals(0, _pool.getNumIdle(keyb));
186:
187: _pool.returnObject(keya, objA0);
188: _pool.returnObject(keyb, objB0);
189:
190: assertEquals(2, _pool.getNumActive());
191: assertEquals(2, _pool.getNumIdle());
192: assertEquals(1, _pool.getNumActive(keya));
193: assertEquals(1, _pool.getNumIdle(keya));
194: assertEquals(1, _pool.getNumActive(keyb));
195: assertEquals(1, _pool.getNumIdle(keyb));
196:
197: _pool.returnObject(keya, objA1);
198: _pool.returnObject(keyb, objB1);
199:
200: assertEquals(0, _pool.getNumActive());
201: assertEquals(4, _pool.getNumIdle());
202: assertEquals(0, _pool.getNumActive(keya));
203: assertEquals(2, _pool.getNumIdle(keya));
204: assertEquals(0, _pool.getNumActive(keyb));
205: assertEquals(2, _pool.getNumIdle(keyb));
206: }
207:
208: public void testBaseClear() throws Exception {
209: try {
210: _pool = makeEmptyPool(3);
211: } catch (IllegalArgumentException e) {
212: return; // skip this test if unsupported
213: }
214: Object keya = makeKey(0);
215: assertEquals(0, _pool.getNumActive(keya));
216: assertEquals(0, _pool.getNumIdle(keya));
217: Object obj0 = _pool.borrowObject(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: _pool.returnObject(keya, obj0);
223: assertEquals(0, _pool.getNumActive(keya));
224: assertEquals(2, _pool.getNumIdle(keya));
225: _pool.clear(keya);
226: assertEquals(0, _pool.getNumActive(keya));
227: assertEquals(0, _pool.getNumIdle(keya));
228: Object obj2 = _pool.borrowObject(keya);
229: assertEquals(getNthObject(keya, 2), obj2);
230: }
231:
232: public void testBaseInvalidateObject() throws Exception {
233: try {
234: _pool = makeEmptyPool(3);
235: } catch (IllegalArgumentException e) {
236: return; // skip this test if unsupported
237: }
238: Object keya = makeKey(0);
239: assertEquals(0, _pool.getNumActive(keya));
240: assertEquals(0, _pool.getNumIdle(keya));
241: Object obj0 = _pool.borrowObject(keya);
242: Object obj1 = _pool.borrowObject(keya);
243: assertEquals(2, _pool.getNumActive(keya));
244: assertEquals(0, _pool.getNumIdle(keya));
245: _pool.invalidateObject(keya, obj0);
246: assertEquals(1, _pool.getNumActive(keya));
247: assertEquals(0, _pool.getNumIdle(keya));
248: _pool.invalidateObject(keya, obj1);
249: assertEquals(0, _pool.getNumActive(keya));
250: assertEquals(0, _pool.getNumIdle(keya));
251: }
252:
253: public void testBaseAddObject() throws Exception {
254: try {
255: _pool = makeEmptyPool(3);
256: } catch (IllegalArgumentException e) {
257: return; // skip this test if unsupported
258: }
259: Object key = makeKey(0);
260: try {
261: assertEquals(0, _pool.getNumIdle());
262: assertEquals(0, _pool.getNumActive());
263: assertEquals(0, _pool.getNumIdle(key));
264: assertEquals(0, _pool.getNumActive(key));
265: _pool.addObject(key);
266: assertEquals(1, _pool.getNumIdle());
267: assertEquals(0, _pool.getNumActive());
268: assertEquals(1, _pool.getNumIdle(key));
269: assertEquals(0, _pool.getNumActive(key));
270: Object obj = _pool.borrowObject(key);
271: assertEquals(getNthObject(key, 0), obj);
272: assertEquals(0, _pool.getNumIdle());
273: assertEquals(1, _pool.getNumActive());
274: assertEquals(0, _pool.getNumIdle(key));
275: assertEquals(1, _pool.getNumActive(key));
276: _pool.returnObject(key, obj);
277: assertEquals(1, _pool.getNumIdle());
278: assertEquals(0, _pool.getNumActive());
279: assertEquals(1, _pool.getNumIdle(key));
280: assertEquals(0, _pool.getNumActive(key));
281: } catch (UnsupportedOperationException e) {
282: return; // skip this test if one of those calls is unsupported
283: }
284: }
285:
286: private KeyedObjectPool _pool = null;
287: }
|