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 TestObjectPool extends TestCase {
026: public TestObjectPool(String testName) {
027: super (testName);
028: }
029:
030: /**
031: * Create an {@link ObjectPool} 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 ObjectPool 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: */
043: protected abstract Object getNthObject(int n);
044:
045: /**
046: * Is the implementations LIFO?
047: * @return
048: */
049: protected abstract boolean isLifo();
050:
051: /**
052: * Is the implementationn FIFO?
053: * @return
054: */
055: protected abstract boolean isFifo();
056:
057: public void setUp() throws Exception {
058: }
059:
060: public void tearDown() throws Exception {
061: _pool = null;
062: }
063:
064: public void testBaseBorrow() throws Exception {
065: try {
066: _pool = makeEmptyPool(3);
067: } catch (IllegalArgumentException e) {
068: return; // skip this test if unsupported
069: }
070: assertEquals(getNthObject(0), _pool.borrowObject());
071: assertEquals(getNthObject(1), _pool.borrowObject());
072: assertEquals(getNthObject(2), _pool.borrowObject());
073: }
074:
075: public void testBaseAddObject() throws Exception {
076: try {
077: _pool = makeEmptyPool(3);
078: } catch (IllegalArgumentException e) {
079: return; // skip this test if unsupported
080: }
081: try {
082: assertEquals(0, _pool.getNumIdle());
083: assertEquals(0, _pool.getNumActive());
084: _pool.addObject();
085: assertEquals(1, _pool.getNumIdle());
086: assertEquals(0, _pool.getNumActive());
087: Object obj = _pool.borrowObject();
088: assertEquals(getNthObject(0), obj);
089: assertEquals(0, _pool.getNumIdle());
090: assertEquals(1, _pool.getNumActive());
091: _pool.returnObject(obj);
092: assertEquals(1, _pool.getNumIdle());
093: assertEquals(0, _pool.getNumActive());
094: } catch (UnsupportedOperationException e) {
095: return; // skip this test if one of those calls is unsupported
096: }
097: }
098:
099: 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: }
|