KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > pool > TestKeyedObjectPool


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 package org.apache.commons.pool;
17
18 import junit.framework.TestCase;
19
20 /**
21  * Abstract {@link TestCase} for {@link ObjectPool} implementations.
22  * @author Rodney Waldhoff
23  * @version $Revision$ $Date: 2005-02-26 05:13:28 -0800 (Sat, 26 Feb 2005) $
24  */

25 public abstract class TestKeyedObjectPool extends TestCase {
26     public TestKeyedObjectPool(String JavaDoc testName) {
27         super(testName);
28     }
29
30     /**
31      * Create an {@link KeyedObjectPool} instance
32      * that can contain at least <i>mincapacity</i>
33      * idle and active objects, or
34      * throw {@link IllegalArgumentException}
35      * if such a pool cannot be created.
36      */

37     protected abstract KeyedObjectPool makeEmptyPool(int mincapacity);
38
39     /**
40      * Return what we expect to be the n<sup>th</sup>
41      * object (zero indexed) created by the pool
42      * for the given key.
43      */

44     protected abstract Object JavaDoc getNthObject(Object JavaDoc key, int n);
45
46     protected abstract Object JavaDoc makeKey(int n);
47
48     public void setUp() throws Exception JavaDoc {
49     }
50     
51     public void tearDown() throws Exception JavaDoc {
52         _pool = null;
53     }
54     
55     public void testBaseBorrow() throws Exception JavaDoc {
56         try {
57             _pool = makeEmptyPool(3);
58         } catch(IllegalArgumentException JavaDoc e) {
59             return; // skip this test if unsupported
60
}
61         Object JavaDoc keya = makeKey(0);
62         Object JavaDoc keyb = makeKey(1);
63         assertEquals("1",getNthObject(keya,0),_pool.borrowObject(keya));
64         assertEquals("2",getNthObject(keyb,0),_pool.borrowObject(keyb));
65         assertEquals("3",getNthObject(keyb,1),_pool.borrowObject(keyb));
66         assertEquals("4",getNthObject(keya,1),_pool.borrowObject(keya));
67         assertEquals("5",getNthObject(keyb,2),_pool.borrowObject(keyb));
68         assertEquals("6",getNthObject(keya,2),_pool.borrowObject(keya));
69     }
70
71     public void testBaseBorrowReturn() throws Exception JavaDoc {
72         try {
73             _pool = makeEmptyPool(3);
74         } catch(IllegalArgumentException JavaDoc e) {
75             return; // skip this test if unsupported
76
}
77         Object JavaDoc keya = makeKey(0);
78         Object JavaDoc obj0 = _pool.borrowObject(keya);
79         assertEquals(getNthObject(keya,0),obj0);
80         Object JavaDoc obj1 = _pool.borrowObject(keya);
81         assertEquals(getNthObject(keya,1),obj1);
82         Object JavaDoc obj2 = _pool.borrowObject(keya);
83         assertEquals(getNthObject(keya,2),obj2);
84         _pool.returnObject(keya,obj2);
85         obj2 = _pool.borrowObject(keya);
86         assertEquals(getNthObject(keya,2),obj2);
87         _pool.returnObject(keya,obj1);
88         obj1 = _pool.borrowObject(keya);
89         assertEquals(getNthObject(keya,1),obj1);
90         _pool.returnObject(keya,obj0);
91         _pool.returnObject(keya,obj2);
92         obj2 = _pool.borrowObject(keya);
93         assertEquals(getNthObject(keya,2),obj2);
94         obj0 = _pool.borrowObject(keya);
95         assertEquals(getNthObject(keya,0),obj0);
96     }
97
98     public void testBaseNumActiveNumIdle() throws Exception JavaDoc {
99         try {
100             _pool = makeEmptyPool(3);
101         } catch(IllegalArgumentException JavaDoc e) {
102             return; // skip this test if unsupported
103
}
104         Object JavaDoc keya = makeKey(0);
105         assertEquals(0,_pool.getNumActive(keya));
106         assertEquals(0,_pool.getNumIdle(keya));
107         Object JavaDoc obj0 = _pool.borrowObject(keya);
108         assertEquals(1,_pool.getNumActive(keya));
109         assertEquals(0,_pool.getNumIdle(keya));
110         Object JavaDoc obj1 = _pool.borrowObject(keya);
111         assertEquals(2,_pool.getNumActive(keya));
112         assertEquals(0,_pool.getNumIdle(keya));
113         _pool.returnObject(keya,obj1);
114         assertEquals(1,_pool.getNumActive(keya));
115         assertEquals(1,_pool.getNumIdle(keya));
116         _pool.returnObject(keya,obj0);
117         assertEquals(0,_pool.getNumActive(keya));
118         assertEquals(2,_pool.getNumIdle(keya));
119         
120         assertEquals(0,_pool.getNumActive("xyzzy12345"));
121         assertEquals(0,_pool.getNumIdle("xyzzy12345"));
122     }
123
124     public void testBaseNumActiveNumIdle2() throws Exception JavaDoc {
125         try {
126             _pool = makeEmptyPool(6);
127         } catch(IllegalArgumentException JavaDoc e) {
128             return; // skip this test if unsupported
129
}
130         Object JavaDoc keya = makeKey(0);
131         Object JavaDoc keyb = makeKey(1);
132         assertEquals(0,_pool.getNumActive());
133         assertEquals(0,_pool.getNumIdle());
134         assertEquals(0,_pool.getNumActive(keya));
135         assertEquals(0,_pool.getNumIdle(keya));
136         assertEquals(0,_pool.getNumActive(keyb));
137         assertEquals(0,_pool.getNumIdle(keyb));
138
139         Object JavaDoc objA0 = _pool.borrowObject(keya);
140         Object JavaDoc objB0 = _pool.borrowObject(keyb);
141
142         assertEquals(2,_pool.getNumActive());
143         assertEquals(0,_pool.getNumIdle());
144         assertEquals(1,_pool.getNumActive(keya));
145         assertEquals(0,_pool.getNumIdle(keya));
146         assertEquals(1,_pool.getNumActive(keyb));
147         assertEquals(0,_pool.getNumIdle(keyb));
148
149         Object JavaDoc objA1 = _pool.borrowObject(keya);
150         Object JavaDoc objB1 = _pool.borrowObject(keyb);
151
152         assertEquals(4,_pool.getNumActive());
153         assertEquals(0,_pool.getNumIdle());
154         assertEquals(2,_pool.getNumActive(keya));
155         assertEquals(0,_pool.getNumIdle(keya));
156         assertEquals(2,_pool.getNumActive(keyb));
157         assertEquals(0,_pool.getNumIdle(keyb));
158
159         _pool.returnObject(keya,objA0);
160         _pool.returnObject(keyb,objB0);
161
162         assertEquals(2,_pool.getNumActive());
163         assertEquals(2,_pool.getNumIdle());
164         assertEquals(1,_pool.getNumActive(keya));
165         assertEquals(1,_pool.getNumIdle(keya));
166         assertEquals(1,_pool.getNumActive(keyb));
167         assertEquals(1,_pool.getNumIdle(keyb));
168
169         _pool.returnObject(keya,objA1);
170         _pool.returnObject(keyb,objB1);
171
172         assertEquals(0,_pool.getNumActive());
173         assertEquals(4,_pool.getNumIdle());
174         assertEquals(0,_pool.getNumActive(keya));
175         assertEquals(2,_pool.getNumIdle(keya));
176         assertEquals(0,_pool.getNumActive(keyb));
177         assertEquals(2,_pool.getNumIdle(keyb));
178     }
179
180     public void testBaseClear() throws Exception JavaDoc {
181         try {
182             _pool = makeEmptyPool(3);
183         } catch(IllegalArgumentException JavaDoc e) {
184             return; // skip this test if unsupported
185
}
186         Object JavaDoc keya = makeKey(0);
187         assertEquals(0,_pool.getNumActive(keya));
188         assertEquals(0,_pool.getNumIdle(keya));
189         Object JavaDoc obj0 = _pool.borrowObject(keya);
190         Object JavaDoc obj1 = _pool.borrowObject(keya);
191         assertEquals(2,_pool.getNumActive(keya));
192         assertEquals(0,_pool.getNumIdle(keya));
193         _pool.returnObject(keya,obj1);
194         _pool.returnObject(keya,obj0);
195         assertEquals(0,_pool.getNumActive(keya));
196         assertEquals(2,_pool.getNumIdle(keya));
197         _pool.clear(keya);
198         assertEquals(0,_pool.getNumActive(keya));
199         assertEquals(0,_pool.getNumIdle(keya));
200         Object JavaDoc obj2 = _pool.borrowObject(keya);
201         assertEquals(getNthObject(keya,2),obj2);
202     }
203
204     public void testBaseInvalidateObject() throws Exception JavaDoc {
205         try {
206             _pool = makeEmptyPool(3);
207         } catch(IllegalArgumentException JavaDoc e) {
208             return; // skip this test if unsupported
209
}
210         Object JavaDoc keya = makeKey(0);
211         assertEquals(0,_pool.getNumActive(keya));
212         assertEquals(0,_pool.getNumIdle(keya));
213         Object JavaDoc obj0 = _pool.borrowObject(keya);
214         Object JavaDoc obj1 = _pool.borrowObject(keya);
215         assertEquals(2,_pool.getNumActive(keya));
216         assertEquals(0,_pool.getNumIdle(keya));
217         _pool.invalidateObject(keya,obj0);
218         assertEquals(1,_pool.getNumActive(keya));
219         assertEquals(0,_pool.getNumIdle(keya));
220         _pool.invalidateObject(keya,obj1);
221         assertEquals(0,_pool.getNumActive(keya));
222         assertEquals(0,_pool.getNumIdle(keya));
223     }
224
225     public void testBaseAddObject() throws Exception JavaDoc {
226         try {
227             _pool = makeEmptyPool(3);
228         } catch(IllegalArgumentException JavaDoc e) {
229             return; // skip this test if unsupported
230
}
231         Object JavaDoc key = makeKey(0);
232         try {
233             assertEquals(0,_pool.getNumIdle());
234             assertEquals(0,_pool.getNumActive());
235             assertEquals(0,_pool.getNumIdle(key));
236             assertEquals(0,_pool.getNumActive(key));
237             _pool.addObject(key);
238             assertEquals(1,_pool.getNumIdle());
239             assertEquals(0,_pool.getNumActive());
240             assertEquals(1,_pool.getNumIdle(key));
241             assertEquals(0,_pool.getNumActive(key));
242             Object JavaDoc obj = _pool.borrowObject(key);
243             assertEquals(getNthObject(key,0),obj);
244             assertEquals(0,_pool.getNumIdle());
245             assertEquals(1,_pool.getNumActive());
246             assertEquals(0,_pool.getNumIdle(key));
247             assertEquals(1,_pool.getNumActive(key));
248             _pool.returnObject(key,obj);
249             assertEquals(1,_pool.getNumIdle());
250             assertEquals(0,_pool.getNumActive());
251             assertEquals(1,_pool.getNumIdle(key));
252             assertEquals(0,_pool.getNumActive(key));
253         } catch(UnsupportedOperationException JavaDoc e) {
254             return; // skip this test if one of those calls is unsupported
255
}
256     }
257
258     private KeyedObjectPool _pool = null;
259 }
260
Popular Tags