KickJava   Java API By Example, From Geeks To Geeks.

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


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 TestObjectPool extends TestCase {
26     public TestObjectPool(String JavaDoc testName) {
27         super(testName);
28     }
29
30     /**
31      * Create an {@link ObjectPool} 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 ObjectPool 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      */

43     protected abstract Object JavaDoc getNthObject(int n);
44
45     public void setUp() throws Exception JavaDoc {
46     }
47     
48     public void tearDown() throws Exception JavaDoc {
49         _pool = null;
50     }
51     
52     public void testBaseBorrow() throws Exception JavaDoc {
53         try {
54             _pool = makeEmptyPool(3);
55         } catch(IllegalArgumentException JavaDoc e) {
56             return; // skip this test if unsupported
57
}
58         assertEquals(getNthObject(0),_pool.borrowObject());
59         assertEquals(getNthObject(1),_pool.borrowObject());
60         assertEquals(getNthObject(2),_pool.borrowObject());
61     }
62
63     public void testBaseAddObject() throws Exception JavaDoc {
64         try {
65             _pool = makeEmptyPool(3);
66         } catch(IllegalArgumentException JavaDoc e) {
67             return; // skip this test if unsupported
68
}
69         try {
70             assertEquals(0,_pool.getNumIdle());
71             assertEquals(0,_pool.getNumActive());
72             _pool.addObject();
73             assertEquals(1,_pool.getNumIdle());
74             assertEquals(0,_pool.getNumActive());
75             Object JavaDoc obj = _pool.borrowObject();
76             assertEquals(getNthObject(0),obj);
77             assertEquals(0,_pool.getNumIdle());
78             assertEquals(1,_pool.getNumActive());
79             _pool.returnObject(obj);
80             assertEquals(1,_pool.getNumIdle());
81             assertEquals(0,_pool.getNumActive());
82         } catch(UnsupportedOperationException JavaDoc e) {
83             return; // skip this test if one of those calls is unsupported
84
}
85     }
86     
87     public void testBaseBorrowReturn() throws Exception JavaDoc {
88         try {
89             _pool = makeEmptyPool(3);
90         } catch(IllegalArgumentException JavaDoc e) {
91             return; // skip this test if unsupported
92
}
93         Object JavaDoc obj0 = _pool.borrowObject();
94         assertEquals(getNthObject(0),obj0);
95         Object JavaDoc obj1 = _pool.borrowObject();
96         assertEquals(getNthObject(1),obj1);
97         Object JavaDoc obj2 = _pool.borrowObject();
98         assertEquals(getNthObject(2),obj2);
99         _pool.returnObject(obj2);
100         obj2 = _pool.borrowObject();
101         assertEquals(getNthObject(2),obj2);
102         _pool.returnObject(obj1);
103         obj1 = _pool.borrowObject();
104         assertEquals(getNthObject(1),obj1);
105         _pool.returnObject(obj0);
106         _pool.returnObject(obj2);
107         obj2 = _pool.borrowObject();
108         assertEquals(getNthObject(2),obj2);
109         obj0 = _pool.borrowObject();
110         assertEquals(getNthObject(0),obj0);
111     }
112
113     public void testBaseNumActiveNumIdle() throws Exception JavaDoc {
114         try {
115             _pool = makeEmptyPool(3);
116         } catch(IllegalArgumentException JavaDoc e) {
117             return; // skip this test if unsupported
118
}
119         assertEquals(0,_pool.getNumActive());
120         assertEquals(0,_pool.getNumIdle());
121         Object JavaDoc obj0 = _pool.borrowObject();
122         assertEquals(1,_pool.getNumActive());
123         assertEquals(0,_pool.getNumIdle());
124         Object JavaDoc obj1 = _pool.borrowObject();
125         assertEquals(2,_pool.getNumActive());
126         assertEquals(0,_pool.getNumIdle());
127         _pool.returnObject(obj1);
128         assertEquals(1,_pool.getNumActive());
129         assertEquals(1,_pool.getNumIdle());
130         _pool.returnObject(obj0);
131         assertEquals(0,_pool.getNumActive());
132         assertEquals(2,_pool.getNumIdle());
133     }
134
135     public void testBaseClear() throws Exception JavaDoc {
136         try {
137             _pool = makeEmptyPool(3);
138         } catch(IllegalArgumentException JavaDoc e) {
139             return; // skip this test if unsupported
140
}
141         assertEquals(0,_pool.getNumActive());
142         assertEquals(0,_pool.getNumIdle());
143         Object JavaDoc obj0 = _pool.borrowObject();
144         Object JavaDoc obj1 = _pool.borrowObject();
145         assertEquals(2,_pool.getNumActive());
146         assertEquals(0,_pool.getNumIdle());
147         _pool.returnObject(obj1);
148         _pool.returnObject(obj0);
149         assertEquals(0,_pool.getNumActive());
150         assertEquals(2,_pool.getNumIdle());
151         _pool.clear();
152         assertEquals(0,_pool.getNumActive());
153         assertEquals(0,_pool.getNumIdle());
154         Object JavaDoc obj2 = _pool.borrowObject();
155         assertEquals(getNthObject(2),obj2);
156     }
157
158     public void testBaseInvalidateObject() throws Exception JavaDoc {
159         try {
160             _pool = makeEmptyPool(3);
161         } catch(IllegalArgumentException JavaDoc e) {
162             return; // skip this test if unsupported
163
}
164         assertEquals(0,_pool.getNumActive());
165         assertEquals(0,_pool.getNumIdle());
166         Object JavaDoc obj0 = _pool.borrowObject();
167         Object JavaDoc obj1 = _pool.borrowObject();
168         assertEquals(2,_pool.getNumActive());
169         assertEquals(0,_pool.getNumIdle());
170         _pool.invalidateObject(obj0);
171         assertEquals(1,_pool.getNumActive());
172         assertEquals(0,_pool.getNumIdle());
173         _pool.invalidateObject(obj1);
174         assertEquals(0,_pool.getNumActive());
175         assertEquals(0,_pool.getNumIdle());
176     }
177     
178     public void testBaseClosePool() throws Exception JavaDoc {
179         try {
180             _pool = makeEmptyPool(3);
181         } catch(IllegalArgumentException JavaDoc e) {
182             return; // skip this test if unsupported
183
}
184         Object JavaDoc obj = _pool.borrowObject();
185         _pool.returnObject(obj);
186         
187         _pool.close();
188         try {
189             _pool.borrowObject();
190             fail("Expected IllegalStateException");
191         } catch(IllegalStateException JavaDoc e) {
192             // expected
193
}
194     }
195
196     public void testBaseCantCloseTwice() throws Exception JavaDoc {
197         try {
198             _pool = makeEmptyPool(3);
199         } catch(IllegalArgumentException JavaDoc e) {
200             return; // skip this test if unsupported
201
}
202         Object JavaDoc obj = _pool.borrowObject();
203         _pool.returnObject(obj);
204         
205         _pool.close();
206         try {
207             _pool.close();
208             fail("Expected IllegalStateException");
209         } catch(IllegalStateException JavaDoc e) {
210             // expected
211
}
212     }
213
214     private ObjectPool _pool = null;
215 }
216
Popular Tags