1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.pool.impl;
19
20 import java.util.BitSet;
21 import java.util.HashMap;
22 import java.util.NoSuchElementException;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.commons.pool.KeyedObjectPool;
28 import org.apache.commons.pool.KeyedPoolableObjectFactory;
29 import org.apache.commons.pool.TestKeyedObjectPool;
30 import org.apache.commons.pool.TestBaseKeyedObjectPool;
31
32 /***
33 * @author Rodney Waldhoff
34 * @version $Revision: 605736 $ $Date: 2007-12-19 16:25:47 -0700 (Wed, 19 Dec 2007) $
35 */
36 public class TestStackKeyedObjectPool extends TestBaseKeyedObjectPool {
37 public TestStackKeyedObjectPool(String testName) {
38 super(testName);
39 }
40
41 public static Test suite() {
42 return new TestSuite(TestStackKeyedObjectPool.class);
43 }
44
45 protected KeyedObjectPool makeEmptyPool(int mincapacity) {
46 StackKeyedObjectPool pool = new StackKeyedObjectPool(new SimpleFactory(),mincapacity);
47 return pool;
48 }
49
50 protected KeyedObjectPool makeEmptyPool(KeyedPoolableObjectFactory factory) {
51 return new StackKeyedObjectPool(factory);
52 }
53
54 protected Object getNthObject(Object key, int n) {
55 return String.valueOf(key) + String.valueOf(n);
56 }
57
58 protected Object makeKey(int n) {
59 return String.valueOf(n);
60 }
61
62 private StackKeyedObjectPool pool = null;
63
64 public void setUp() throws Exception {
65 super.setUp();
66 pool = new StackKeyedObjectPool(
67 new KeyedPoolableObjectFactory() {
68 int counter = 0;
69 public Object makeObject(Object key) { return String.valueOf(key) + String.valueOf(counter++); }
70 public void destroyObject(Object key, Object obj) { }
71 public boolean validateObject(Object key, Object obj) { return true; }
72 public void activateObject(Object key, Object obj) { }
73 public void passivateObject(Object key, Object obj) { }
74 }
75 );
76 }
77
78
79 public void tearDown() throws Exception {
80 super.tearDown();
81 pool = null;
82 }
83
84 public void testCloseBug() throws Exception {
85 {
86 Object obj0 = pool.borrowObject("");
87 Object obj1 = pool.borrowObject("");
88 assertEquals(2,pool.getNumActive(""));
89 assertEquals(0,pool.getNumIdle(""));
90 pool.returnObject("",obj1);
91 pool.returnObject("",obj0);
92 assertEquals(0,pool.getNumActive(""));
93 assertEquals(2,pool.getNumIdle(""));
94 }
95 {
96 Object obj0 = pool.borrowObject("2");
97 Object obj1 = pool.borrowObject("2");
98 assertEquals(2,pool.getNumActive("2"));
99 assertEquals(0,pool.getNumIdle("2"));
100 pool.returnObject("2",obj1);
101 pool.returnObject("2",obj0);
102 assertEquals(0,pool.getNumActive("2"));
103 assertEquals(2,pool.getNumIdle("2"));
104 }
105 pool.close();
106 }
107
108 public void testIdleCap() throws Exception {
109 Object[] active = new Object[100];
110 for(int i=0;i<100;i++) {
111 active[i] = pool.borrowObject("");
112 }
113 assertEquals(100,pool.getNumActive(""));
114 assertEquals(0,pool.getNumIdle(""));
115 for(int i=0;i<100;i++) {
116 pool.returnObject("",active[i]);
117 assertEquals(99 - i,pool.getNumActive(""));
118 assertEquals((i < 8 ? i+1 : 8),pool.getNumIdle(""));
119 }
120 }
121
122 public void testPoolWithNullFactory() throws Exception {
123 KeyedObjectPool pool = new StackKeyedObjectPool(10);
124 for(int i=0;i<10;i++) {
125 pool.returnObject("X",new Integer(i));
126 }
127 for(int j=0;j<3;j++) {
128 Integer[] borrowed = new Integer[10];
129 BitSet found = new BitSet();
130 for(int i=0;i<10;i++) {
131 borrowed[i] = (Integer)(pool.borrowObject("X"));
132 assertNotNull(borrowed);
133 assertTrue(!found.get(borrowed[i].intValue()));
134 found.set(borrowed[i].intValue());
135 }
136 for(int i=0;i<10;i++) {
137 pool.returnObject("X",borrowed[i]);
138 }
139 }
140 pool.invalidateObject("X",pool.borrowObject("X"));
141 pool.invalidateObject("X",pool.borrowObject("X"));
142 pool.clear("X");
143 pool.clear();
144 }
145
146 public void testVariousConstructors() throws Exception {
147 {
148 StackKeyedObjectPool pool = new StackKeyedObjectPool();
149 assertNotNull(pool);
150 }
151 {
152 StackKeyedObjectPool pool = new StackKeyedObjectPool(10);
153 assertNotNull(pool);
154 }
155 {
156 StackKeyedObjectPool pool = new StackKeyedObjectPool(10,5);
157 assertNotNull(pool);
158 }
159 {
160 StackKeyedObjectPool pool = new StackKeyedObjectPool(null);
161 assertNotNull(pool);
162 }
163 {
164 StackKeyedObjectPool pool = new StackKeyedObjectPool(null,10);
165 assertNotNull(pool);
166 }
167 {
168 StackKeyedObjectPool pool = new StackKeyedObjectPool(null,10,5);
169 assertNotNull(pool);
170 }
171 }
172
173 public void testToString() throws Exception {
174 StackKeyedObjectPool pool = new StackKeyedObjectPool(new SimpleFactory());
175 assertNotNull(pool.toString());
176 Object obj = pool.borrowObject("key");
177 assertNotNull(pool.toString());
178 pool.returnObject("key",obj);
179 assertNotNull(pool.toString());
180 }
181
182 public void testBorrowFromEmptyPoolWithNullFactory() throws Exception {
183 KeyedObjectPool pool = new StackKeyedObjectPool();
184 try {
185 pool.borrowObject("x");
186 fail("Expected NoSuchElementException");
187 } catch(NoSuchElementException e) {
188
189 }
190 }
191
192 public void testSetFactory() throws Exception {
193 KeyedObjectPool pool = new StackKeyedObjectPool();
194 try {
195 pool.borrowObject("x");
196 fail("Expected NoSuchElementException");
197 } catch(NoSuchElementException e) {
198
199 }
200 pool.setFactory(new SimpleFactory());
201 Object obj = pool.borrowObject("x");
202 assertNotNull(obj);
203 pool.returnObject("x",obj);
204 }
205
206 public void testCantResetFactoryWithActiveObjects() throws Exception {
207 KeyedObjectPool pool = new StackKeyedObjectPool();
208 pool.setFactory(new SimpleFactory());
209 Object obj = pool.borrowObject("x");
210 assertNotNull(obj);
211
212 try {
213 pool.setFactory(new SimpleFactory());
214 fail("Expected IllegalStateException");
215 } catch(IllegalStateException e) {
216
217 }
218 }
219
220 public void testCanResetFactoryWithoutActiveObjects() throws Exception {
221 KeyedObjectPool pool = new StackKeyedObjectPool();
222 {
223 pool.setFactory(new SimpleFactory());
224 Object obj = pool.borrowObject("x");
225 assertNotNull(obj);
226 pool.returnObject("x",obj);
227 }
228 {
229 pool.setFactory(new SimpleFactory());
230 Object obj = pool.borrowObject("x");
231 assertNotNull(obj);
232 pool.returnObject("x",obj);
233 }
234 }
235
236 public void testBorrowReturnWithSometimesInvalidObjects() throws Exception {
237 KeyedObjectPool pool = new StackKeyedObjectPool(
238 new KeyedPoolableObjectFactory() {
239 int counter = 0;
240 public Object makeObject(Object key) { return new Integer(counter++); }
241 public void destroyObject(Object key, Object obj) { }
242 public boolean validateObject(Object key, Object obj) {
243 if(obj instanceof Integer) {
244 return ((((Integer)obj).intValue() % 2) == 1);
245 } else {
246 return false;
247 }
248 }
249 public void activateObject(Object key, Object obj) { }
250 public void passivateObject(Object key, Object obj) {
251 if(obj instanceof Integer) {
252 if((((Integer)obj).intValue() % 3) == 0) {
253 throw new RuntimeException("Couldn't passivate");
254 }
255 } else {
256 throw new RuntimeException("Couldn't passivate");
257 }
258 }
259 }
260 );
261
262 Object[] obj = new Object[10];
263 for(int i=0;i<10;i++) {
264 Object object = null;
265 int k = 0;
266 while (object == null && k < 100) {
267 try {
268 k++;
269 object = pool.borrowObject("key");
270 obj[i] = object;
271 } catch (NoSuchElementException ex) {
272
273 }
274 }
275 assertEquals("Each time we borrow, get one more active.", i+1, pool.getNumActive());
276 }
277
278 for(int i=0;i<10;i++) {
279 pool.returnObject("key",obj[i]);
280 assertEquals("Each time we borrow, get one less active.", 9-i, pool.getNumActive());
281 }
282
283 assertEquals(7,pool.getNumIdle());
284 assertEquals(new Integer(19), (Integer) pool.borrowObject("key"));
285 assertEquals(new Integer(17), (Integer) pool.borrowObject("key"));
286 assertEquals(new Integer(13), (Integer) pool.borrowObject("key"));
287 assertEquals(new Integer(11), (Integer) pool.borrowObject("key"));
288 assertEquals(new Integer(7), (Integer) pool.borrowObject("key"));
289 assertEquals(new Integer(5), (Integer) pool.borrowObject("key"));
290 assertEquals(new Integer(1), (Integer) pool.borrowObject("key"));
291 }
292
293 class SimpleFactory implements KeyedPoolableObjectFactory {
294 HashMap map = new HashMap();
295 public Object makeObject(Object key) {
296 int counter = 0;
297 Integer Counter = (Integer)(map.get(key));
298 if(null != Counter) {
299 counter = Counter.intValue();
300 }
301 map.put(key,new Integer(counter + 1));
302 return String.valueOf(key) + String.valueOf(counter);
303 }
304 public void destroyObject(Object key, Object obj) { }
305 public boolean validateObject(Object key, Object obj) { return true; }
306 public void activateObject(Object key, Object obj) { }
307 public void passivateObject(Object key, Object obj) { }
308 }
309
310 protected boolean isLifo() {
311 return true;
312 }
313
314 protected boolean isFifo() {
315 return false;
316 }
317 }