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:
017: package org.apache.commons.pool.impl;
018:
019: import java.util.BitSet;
020: import java.util.HashMap;
021: import java.util.NoSuchElementException;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.pool.KeyedObjectPool;
027: import org.apache.commons.pool.KeyedPoolableObjectFactory;
028: import org.apache.commons.pool.TestKeyedObjectPool;
029:
030: /**
031: * @author Rodney Waldhoff
032: * @version $Revision: 383290 $ $Date: 2006-03-05 02:00:15 -0500 (Sun, 05 Mar 2006) $
033: */
034: public class TestStackKeyedObjectPool extends TestKeyedObjectPool {
035: public TestStackKeyedObjectPool(String testName) {
036: super (testName);
037: }
038:
039: public static Test suite() {
040: return new TestSuite(TestStackKeyedObjectPool.class);
041: }
042:
043: protected KeyedObjectPool makeEmptyPool(int mincapacity) {
044: StackKeyedObjectPool pool = new StackKeyedObjectPool(
045: new SimpleFactory(), mincapacity);
046: return pool;
047: }
048:
049: protected Object getNthObject(Object key, int n) {
050: return String.valueOf(key) + String.valueOf(n);
051: }
052:
053: protected Object makeKey(int n) {
054: return String.valueOf(n);
055: }
056:
057: private StackKeyedObjectPool pool = null;
058:
059: public void setUp() throws Exception {
060: super .setUp();
061: pool = new StackKeyedObjectPool(
062: new KeyedPoolableObjectFactory() {
063: int counter = 0;
064:
065: public Object makeObject(Object key) {
066: return String.valueOf(key)
067: + String.valueOf(counter++);
068: }
069:
070: public void destroyObject(Object key, Object obj) {
071: }
072:
073: public boolean validateObject(Object key, Object obj) {
074: return true;
075: }
076:
077: public void activateObject(Object key, Object obj) {
078: }
079:
080: public void passivateObject(Object key, Object obj) {
081: }
082: });
083: }
084:
085: public void tearDown() throws Exception {
086: super .tearDown();
087: pool = null;
088: }
089:
090: public void testCloseBug() throws Exception {
091: {
092: Object obj0 = pool.borrowObject("");
093: Object obj1 = pool.borrowObject("");
094: assertEquals(2, pool.getNumActive(""));
095: assertEquals(0, pool.getNumIdle(""));
096: pool.returnObject("", obj1);
097: pool.returnObject("", obj0);
098: assertEquals(0, pool.getNumActive(""));
099: assertEquals(2, pool.getNumIdle(""));
100: }
101: {
102: Object obj0 = pool.borrowObject("2");
103: Object obj1 = pool.borrowObject("2");
104: assertEquals(2, pool.getNumActive("2"));
105: assertEquals(0, pool.getNumIdle("2"));
106: pool.returnObject("2", obj1);
107: pool.returnObject("2", obj0);
108: assertEquals(0, pool.getNumActive("2"));
109: assertEquals(2, pool.getNumIdle("2"));
110: }
111: pool.close();
112: }
113:
114: public void testIdleCap() throws Exception {
115: Object[] active = new Object[100];
116: for (int i = 0; i < 100; i++) {
117: active[i] = pool.borrowObject("");
118: }
119: assertEquals(100, pool.getNumActive(""));
120: assertEquals(0, pool.getNumIdle(""));
121: for (int i = 0; i < 100; i++) {
122: pool.returnObject("", active[i]);
123: assertEquals(99 - i, pool.getNumActive(""));
124: assertEquals((i < 8 ? i + 1 : 8), pool.getNumIdle(""));
125: }
126: }
127:
128: public void testPoolWithNullFactory() throws Exception {
129: KeyedObjectPool pool = new StackKeyedObjectPool(10);
130: for (int i = 0; i < 10; i++) {
131: pool.returnObject("X", new Integer(i));
132: }
133: for (int j = 0; j < 3; j++) {
134: Integer[] borrowed = new Integer[10];
135: BitSet found = new BitSet();
136: for (int i = 0; i < 10; i++) {
137: borrowed[i] = (Integer) (pool.borrowObject("X"));
138: assertNotNull(borrowed);
139: assertTrue(!found.get(borrowed[i].intValue()));
140: found.set(borrowed[i].intValue());
141: }
142: for (int i = 0; i < 10; i++) {
143: pool.returnObject("X", borrowed[i]);
144: }
145: }
146: pool.invalidateObject("X", pool.borrowObject("X"));
147: pool.invalidateObject("X", pool.borrowObject("X"));
148: pool.clear("X");
149: pool.clear();
150: }
151:
152: public void testVariousConstructors() throws Exception {
153: {
154: StackKeyedObjectPool pool = new StackKeyedObjectPool();
155: assertNotNull(pool);
156: }
157: {
158: StackKeyedObjectPool pool = new StackKeyedObjectPool(10);
159: assertNotNull(pool);
160: }
161: {
162: StackKeyedObjectPool pool = new StackKeyedObjectPool(10, 5);
163: assertNotNull(pool);
164: }
165: {
166: StackKeyedObjectPool pool = new StackKeyedObjectPool(null);
167: assertNotNull(pool);
168: }
169: {
170: StackKeyedObjectPool pool = new StackKeyedObjectPool(null,
171: 10);
172: assertNotNull(pool);
173: }
174: {
175: StackKeyedObjectPool pool = new StackKeyedObjectPool(null,
176: 10, 5);
177: assertNotNull(pool);
178: }
179: }
180:
181: public void testToString() throws Exception {
182: StackKeyedObjectPool pool = new StackKeyedObjectPool(
183: new SimpleFactory());
184: assertNotNull(pool.toString());
185: Object obj = pool.borrowObject("key");
186: assertNotNull(pool.toString());
187: pool.returnObject("key", obj);
188: assertNotNull(pool.toString());
189: }
190:
191: public void testBorrowFromEmptyPoolWithNullFactory()
192: throws Exception {
193: KeyedObjectPool pool = new StackKeyedObjectPool();
194: try {
195: pool.borrowObject("x");
196: fail("Expected NoSuchElementException");
197: } catch (NoSuchElementException e) {
198: // expected
199: }
200: }
201:
202: public void testSetFactory() throws Exception {
203: KeyedObjectPool pool = new StackKeyedObjectPool();
204: try {
205: pool.borrowObject("x");
206: fail("Expected NoSuchElementException");
207: } catch (NoSuchElementException e) {
208: // expected
209: }
210: pool.setFactory(new SimpleFactory());
211: Object obj = pool.borrowObject("x");
212: assertNotNull(obj);
213: pool.returnObject("x", obj);
214: }
215:
216: public void testCantResetFactoryWithActiveObjects()
217: throws Exception {
218: KeyedObjectPool pool = new StackKeyedObjectPool();
219: pool.setFactory(new SimpleFactory());
220: Object obj = pool.borrowObject("x");
221: assertNotNull(obj);
222:
223: try {
224: pool.setFactory(new SimpleFactory());
225: fail("Expected IllegalStateException");
226: } catch (IllegalStateException e) {
227: // expected
228: }
229: }
230:
231: public void testCanResetFactoryWithoutActiveObjects()
232: throws Exception {
233: KeyedObjectPool pool = new StackKeyedObjectPool();
234: {
235: pool.setFactory(new SimpleFactory());
236: Object obj = pool.borrowObject("x");
237: assertNotNull(obj);
238: pool.returnObject("x", obj);
239: }
240: {
241: pool.setFactory(new SimpleFactory());
242: Object obj = pool.borrowObject("x");
243: assertNotNull(obj);
244: pool.returnObject("x", obj);
245: }
246: }
247:
248: public void testBorrowReturnWithSometimesInvalidObjects()
249: throws Exception {
250: KeyedObjectPool pool = new StackKeyedObjectPool();
251: pool.setFactory(new KeyedPoolableObjectFactory() {
252: int counter = 0;
253:
254: public Object makeObject(Object key) {
255: return new Integer(counter++);
256: }
257:
258: public void destroyObject(Object key, Object obj) {
259: }
260:
261: public boolean validateObject(Object key, Object obj) {
262: if (obj instanceof Integer) {
263: return ((((Integer) obj).intValue() % 2) == 1);
264: } else {
265: return false;
266: }
267: }
268:
269: public void activateObject(Object key, Object obj) {
270: }
271:
272: public void passivateObject(Object key, Object obj) {
273: if (obj instanceof Integer) {
274: if ((((Integer) obj).intValue() % 3) == 0) {
275: throw new RuntimeException("Couldn't passivate");
276: }
277: } else {
278: throw new RuntimeException("Couldn't passivate");
279: }
280: }
281: });
282:
283: Object[] obj = new Object[10];
284: for (int i = 0; i < 10; i++) {
285: obj[i] = pool.borrowObject("key");
286: }
287: for (int i = 0; i < 10; i++) {
288: pool.returnObject("key", obj[i]);
289: }
290: assertEquals(3, pool.getNumIdle("key"));
291: }
292:
293: class SimpleFactory implements KeyedPoolableObjectFactory {
294: HashMap map = new HashMap();
295:
296: public Object makeObject(Object key) {
297: int counter = 0;
298: Integer Counter = (Integer) (map.get(key));
299: if (null != Counter) {
300: counter = Counter.intValue();
301: }
302: map.put(key, new Integer(counter + 1));
303: return String.valueOf(key) + String.valueOf(counter);
304: }
305:
306: public void destroyObject(Object key, Object obj) {
307: }
308:
309: public boolean validateObject(Object key, Object obj) {
310: return true;
311: }
312:
313: public void activateObject(Object key, Object obj) {
314: }
315:
316: public void passivateObject(Object key, Object obj) {
317: }
318: }
319:
320: protected boolean isLifo() {
321: return true;
322: }
323:
324: protected boolean isFifo() {
325: return false;
326: }
327: }
|