KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > pool > impl > TestStackObjectPool


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.pool.impl;
18
19 import java.util.BitSet JavaDoc;
20 import java.util.NoSuchElementException JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.commons.pool.ObjectPool;
28 import org.apache.commons.pool.PoolableObjectFactory;
29 import org.apache.commons.pool.TestObjectPool;
30
31 /**
32  * @author Rodney Waldhoff
33  * @author Dirk Verbeeck
34  * @version $Revision$ $Date: 2005-05-16 04:08:09 -0700 (Mon, 16 May 2005) $
35  */

36 public class TestStackObjectPool extends TestObjectPool {
37     public TestStackObjectPool(String JavaDoc testName) {
38         super(testName);
39     }
40
41     public static Test suite() {
42         return new TestSuite(TestStackObjectPool.class);
43     }
44
45     protected ObjectPool makeEmptyPool(int mincap) {
46         return new StackObjectPool(new SimpleFactory());
47     }
48     
49     protected Object JavaDoc getNthObject(int n) {
50         return String.valueOf(n);
51     }
52
53     public void testIdleCap() throws Exception JavaDoc {
54         ObjectPool pool = makeEmptyPool(8);
55         Object JavaDoc[] active = new Object JavaDoc[100];
56         for(int i=0;i<100;i++) {
57             active[i] = pool.borrowObject();
58         }
59         assertEquals(100,pool.getNumActive());
60         assertEquals(0,pool.getNumIdle());
61         for(int i=0;i<100;i++) {
62             pool.returnObject(active[i]);
63             assertEquals(99 - i,pool.getNumActive());
64             assertEquals((i < 8 ? i+1 : 8),pool.getNumIdle());
65         }
66     }
67
68     public void testPoolWithNullFactory() throws Exception JavaDoc {
69         ObjectPool pool = new StackObjectPool(10);
70         for(int i=0;i<10;i++) {
71             pool.returnObject(new Integer JavaDoc(i));
72         }
73         for(int j=0;j<3;j++) {
74             Integer JavaDoc[] borrowed = new Integer JavaDoc[10];
75             BitSet JavaDoc found = new BitSet JavaDoc();
76             for(int i=0;i<10;i++) {
77                 borrowed[i] = (Integer JavaDoc)(pool.borrowObject());
78                 assertNotNull(borrowed);
79                 assertTrue(!found.get(borrowed[i].intValue()));
80                 found.set(borrowed[i].intValue());
81             }
82             for(int i=0;i<10;i++) {
83                 pool.returnObject(borrowed[i]);
84             }
85         }
86         pool.invalidateObject(pool.borrowObject());
87         pool.invalidateObject(pool.borrowObject());
88         pool.clear();
89     }
90     
91     public void testBorrowFromEmptyPoolWithNullFactory() throws Exception JavaDoc {
92         ObjectPool pool = new StackObjectPool();
93         try {
94             pool.borrowObject();
95             fail("Expected NoSuchElementException");
96         } catch(NoSuchElementException JavaDoc e) {
97             // expected
98
}
99     }
100     
101     public void testSetFactory() throws Exception JavaDoc {
102         ObjectPool pool = new StackObjectPool();
103         try {
104             pool.borrowObject();
105             fail("Expected NoSuchElementException");
106         } catch(NoSuchElementException JavaDoc e) {
107             // expected
108
}
109         pool.setFactory(new SimpleFactory());
110         Object JavaDoc obj = pool.borrowObject();
111         assertNotNull(obj);
112         pool.returnObject(obj);
113     }
114
115     public void testCantResetFactoryWithActiveObjects() throws Exception JavaDoc {
116         ObjectPool pool = new StackObjectPool();
117         pool.setFactory(new SimpleFactory());
118         Object JavaDoc obj = pool.borrowObject();
119         assertNotNull(obj);
120
121         try {
122             pool.setFactory(new SimpleFactory());
123             fail("Expected IllegalStateException");
124         } catch(IllegalStateException JavaDoc e) {
125             // expected
126
}
127     }
128     
129     public void testCanResetFactoryWithoutActiveObjects() throws Exception JavaDoc {
130         ObjectPool pool = new StackObjectPool();
131         {
132             pool.setFactory(new SimpleFactory());
133             Object JavaDoc obj = pool.borrowObject();
134             assertNotNull(obj);
135             pool.returnObject(obj);
136         }
137         {
138             pool.setFactory(new SimpleFactory());
139             Object JavaDoc obj = pool.borrowObject();
140             assertNotNull(obj);
141             pool.returnObject(obj);
142         }
143     }
144
145     public void testBorrowReturnWithSometimesInvalidObjects() throws Exception JavaDoc {
146         ObjectPool pool = new StackObjectPool(20);
147         pool.setFactory(
148             new PoolableObjectFactory() {
149                 int counter = 0;
150                 public Object JavaDoc makeObject() { return new Integer JavaDoc(counter++); }
151                 public void destroyObject(Object JavaDoc obj) { }
152                 public boolean validateObject(Object JavaDoc obj) {
153                     if(obj instanceof Integer JavaDoc) {
154                         return ((((Integer JavaDoc)obj).intValue() % 2) == 1);
155                     } else {
156                         return false;
157                     }
158                 }
159                 public void activateObject(Object JavaDoc obj) { }
160                 public void passivateObject(Object JavaDoc obj) {
161                     if(obj instanceof Integer JavaDoc) {
162                         if((((Integer JavaDoc)obj).intValue() % 3) == 0) {
163                             throw new RuntimeException JavaDoc("Couldn't passivate");
164                         }
165                     } else {
166                         throw new RuntimeException JavaDoc("Couldn't passivate");
167                     }
168                 }
169             }
170         );
171
172         Object JavaDoc[] obj = new Object JavaDoc[10];
173         for(int i=0;i<10;i++) {
174             obj[i] = pool.borrowObject();
175         }
176         for(int i=0;i<10;i++) {
177             pool.returnObject(obj[i]);
178         }
179         assertEquals(3,pool.getNumIdle());
180     }
181     
182     public void testVariousConstructors() throws Exception JavaDoc {
183         {
184             StackObjectPool pool = new StackObjectPool();
185             assertNotNull(pool);
186         }
187         {
188             StackObjectPool pool = new StackObjectPool(10);
189             assertNotNull(pool);
190         }
191         {
192             StackObjectPool pool = new StackObjectPool(10,5);
193             assertNotNull(pool);
194         }
195         {
196             StackObjectPool pool = new StackObjectPool(null);
197             assertNotNull(pool);
198         }
199         {
200             StackObjectPool pool = new StackObjectPool(null,10);
201             assertNotNull(pool);
202         }
203         {
204             StackObjectPool pool = new StackObjectPool(null,10,5);
205             assertNotNull(pool);
206         }
207     }
208
209     private final List JavaDoc destroyed = new ArrayList JavaDoc();
210     public void testReturnObjectDiscardOrder() throws Exception JavaDoc {
211         // setup
212
// We need a factory that tracks what was discarded.
213
PoolableObjectFactory pof = new PoolableObjectFactory() {
214             int i = 0;
215             public Object JavaDoc makeObject() throws Exception JavaDoc {
216                 return new Integer JavaDoc(i++);
217             }
218
219             public void destroyObject(Object JavaDoc obj) throws Exception JavaDoc {
220                 destroyed.add(obj);
221             }
222
223             public boolean validateObject(Object JavaDoc obj) {
224                 return obj instanceof Integer JavaDoc;
225             }
226
227             public void activateObject(Object JavaDoc obj) throws Exception JavaDoc {
228             }
229
230             public void passivateObject(Object JavaDoc obj) throws Exception JavaDoc {
231             }
232         };
233         ObjectPool pool = new StackObjectPool(pof, 3);
234
235         // borrow more objects than the pool can hold
236
Integer JavaDoc i0 = (Integer JavaDoc)pool.borrowObject();
237         Integer JavaDoc i1 = (Integer JavaDoc)pool.borrowObject();
238         Integer JavaDoc i2 = (Integer JavaDoc)pool.borrowObject();
239         Integer JavaDoc i3 = (Integer JavaDoc)pool.borrowObject();
240
241         // tests
242
// return as many as the pool will hold.
243
pool.returnObject(i0);
244         pool.returnObject(i1);
245         pool.returnObject(i2);
246
247         // the pool should now be full.
248
assertEquals("No returned objects should have been destroyed yet.",0, destroyed.size());
249
250         // cause the pool to discard a returned object.
251
pool.returnObject(i3);
252         assertEquals("One object should have been destroyed.", 1, destroyed.size());
253
254         // check to see what object was destroyed
255
Integer JavaDoc d = (Integer JavaDoc)destroyed.get(0);
256         assertEquals("Destoryed objects should have the stalest object.", i0, d);
257     }
258
259     static class SimpleFactory implements PoolableObjectFactory {
260         int counter = 0;
261         public Object JavaDoc makeObject() { return String.valueOf(counter++); }
262         public void destroyObject(Object JavaDoc obj) { }
263         public boolean validateObject(Object JavaDoc obj) { return true; }
264         public void activateObject(Object JavaDoc obj) { }
265         public void passivateObject(Object JavaDoc obj) { }
266     }
267 }
268
269
Popular Tags