001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.pool;
018    
019    
020    /**
021     * @author Rodney Waldhoff
022     * @author Sandy McArthur
023     * @version $Revision: 901944 $ $Date: 2010-01-21 17:27:04 -0700 (Thu, 21 Jan 2010) $
024     */
025    public class TestBaseKeyedObjectPool extends TestKeyedObjectPool {
026        private KeyedObjectPool _pool = null;
027    
028        public TestBaseKeyedObjectPool(final String testName) {
029            super(testName);
030        }
031    
032        protected KeyedObjectPool makeEmptyPool(KeyedPoolableObjectFactory factory) {
033            if (this.getClass() != TestBaseKeyedObjectPool.class) {
034                fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
035            }
036            throw new UnsupportedOperationException("BaseKeyedObjectPool isn't a complete implementation.");
037        }
038    
039        /**
040         * Create an {@link KeyedObjectPool} instance
041         * that can contain at least <i>mincapacity</i>
042         * idle and active objects, or
043         * throw {@link IllegalArgumentException}
044         * if such a pool cannot be created.
045         */
046        protected KeyedObjectPool makeEmptyPool(int mincapacity) {
047            if (this.getClass() != TestBaseKeyedObjectPool.class) {
048                fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
049            }
050            throw new UnsupportedOperationException("BaseKeyedObjectPool isn't a complete implementation.");
051        }
052    
053        /**
054         * Return what we expect to be the n<sup>th</sup>
055         * object (zero indexed) created by the pool
056         * for the given key.
057         */
058        protected Object getNthObject(Object key, int n) {
059            if (this.getClass() != TestBaseKeyedObjectPool.class) {
060                fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
061            }
062            throw new UnsupportedOperationException("BaseKeyedObjectPool isn't a complete implementation.");
063        }
064    
065        protected Object makeKey(int n) {
066            if (this.getClass() != TestBaseKeyedObjectPool.class) {
067                fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
068            }
069            throw new UnsupportedOperationException("BaseKeyedObjectPool isn't a complete implementation.");
070        }
071    
072        public void setUp() throws Exception {
073            super.setUp();
074        }
075    
076        public void tearDown() throws Exception {
077            _pool = null;
078            super.tearDown();
079        }
080    
081        public void testUnsupportedOperations() throws Exception {
082            if (!getClass().equals(TestBaseKeyedObjectPool.class)) {
083                return; // skip redundant tests
084            }
085            KeyedObjectPool pool = new BaseKeyedObjectPool() { 
086                public Object borrowObject(Object key) {
087                    return null;
088                }
089                public void returnObject(Object key, Object obj) {
090                }
091                public void invalidateObject(Object key, Object obj) {
092                }            
093            };
094            
095            try {
096                pool.addObject("key");
097                fail("Expected UnsupportedOperationException");
098            } catch(UnsupportedOperationException e) {
099                // expected
100            }
101    
102            assertTrue("Negative expected.", pool.getNumIdle() < 0);
103            assertTrue("Negative expected.", pool.getNumIdle("key") < 0);
104            assertTrue("Negative expected.", pool.getNumActive() < 0);
105            assertTrue("Negative expected.", pool.getNumActive("key") < 0);
106    
107            try {
108                pool.clear();
109                fail("Expected UnsupportedOperationException");
110            } catch(UnsupportedOperationException e) {
111                // expected
112            }
113    
114            try {
115                pool.clear("key");
116                fail("Expected UnsupportedOperationException");
117            } catch(UnsupportedOperationException e) {
118                // expected
119            }
120    
121            try {
122                pool.setFactory(null);
123                fail("Expected UnsupportedOperationException");
124            } catch(UnsupportedOperationException e) {
125                // expected
126            }
127    
128            pool.close(); // a no-op, probably should be remove
129    
130        }
131    
132        protected boolean isLifo() {
133            if (getClass() != TestBaseKeyedObjectPool.class) {
134                fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
135            }
136            return false;
137        }
138    
139        protected boolean isFifo() {
140            if (getClass() != TestBaseKeyedObjectPool.class) {
141                fail("Subclasses of TestBaseKeyedObjectPool must reimplement this method.");
142            }
143            return false;
144        }
145    
146        public void testBaseBorrowReturn() throws Exception {
147            try {
148                _pool = makeEmptyPool(3);
149            } catch(UnsupportedOperationException uoe) {
150                return; // skip this test if unsupported
151            }
152            Object keya = makeKey(0);
153            Object obj0 = _pool.borrowObject(keya);
154            assertEquals(getNthObject(keya,0),obj0);
155            Object obj1 = _pool.borrowObject(keya);
156            assertEquals(getNthObject(keya,1),obj1);
157            Object obj2 = _pool.borrowObject(keya);
158            assertEquals(getNthObject(keya,2),obj2);
159            _pool.returnObject(keya,obj2);
160            obj2 = _pool.borrowObject(keya);
161            assertEquals(getNthObject(keya,2),obj2);
162            _pool.returnObject(keya,obj1);
163            obj1 = _pool.borrowObject(keya);
164            assertEquals(getNthObject(keya,1),obj1);
165            _pool.returnObject(keya,obj0);
166            _pool.returnObject(keya,obj2);
167            obj2 = _pool.borrowObject(keya);
168            if (isLifo()) {
169                assertEquals(getNthObject(keya,2),obj2);
170            }
171            if (isFifo()) {
172                assertEquals(getNthObject(keya,0),obj2);
173            }
174            obj0 = _pool.borrowObject(keya);
175            if (isLifo()) {
176                assertEquals(getNthObject(keya,0),obj0);
177            }
178            if (isFifo()) {
179                assertEquals(getNthObject(keya,2),obj0);
180            }
181        }
182    
183        public void testBaseBorrow() throws Exception {
184            try {
185                _pool = makeEmptyPool(3);
186            } catch(UnsupportedOperationException uoe) {
187                return; // skip this test if unsupported
188            }
189            Object keya = makeKey(0);
190            Object keyb = makeKey(1);
191            assertEquals("1",getNthObject(keya,0),_pool.borrowObject(keya));
192            assertEquals("2",getNthObject(keyb,0),_pool.borrowObject(keyb));
193            assertEquals("3",getNthObject(keyb,1),_pool.borrowObject(keyb));
194            assertEquals("4",getNthObject(keya,1),_pool.borrowObject(keya));
195            assertEquals("5",getNthObject(keyb,2),_pool.borrowObject(keyb));
196            assertEquals("6",getNthObject(keya,2),_pool.borrowObject(keya));
197        }
198    
199        public void testBaseNumActiveNumIdle() throws Exception {
200            try {
201                _pool = makeEmptyPool(3);
202            } catch(UnsupportedOperationException uoe) {
203                return; // skip this test if unsupported
204            }
205            Object keya = makeKey(0);
206            assertEquals(0,_pool.getNumActive(keya));
207            assertEquals(0,_pool.getNumIdle(keya));
208            Object obj0 = _pool.borrowObject(keya);
209            assertEquals(1,_pool.getNumActive(keya));
210            assertEquals(0,_pool.getNumIdle(keya));
211            Object obj1 = _pool.borrowObject(keya);
212            assertEquals(2,_pool.getNumActive(keya));
213            assertEquals(0,_pool.getNumIdle(keya));
214            _pool.returnObject(keya,obj1);
215            assertEquals(1,_pool.getNumActive(keya));
216            assertEquals(1,_pool.getNumIdle(keya));
217            _pool.returnObject(keya,obj0);
218            assertEquals(0,_pool.getNumActive(keya));
219            assertEquals(2,_pool.getNumIdle(keya));
220    
221            assertEquals(0,_pool.getNumActive("xyzzy12345"));
222            assertEquals(0,_pool.getNumIdle("xyzzy12345"));
223        }
224    
225        public void testBaseNumActiveNumIdle2() throws Exception {
226            try {
227                _pool = makeEmptyPool(6);
228            } catch(UnsupportedOperationException uoe) {
229                return; // skip this test if unsupported
230            }
231            Object keya = makeKey(0);
232            Object keyb = makeKey(1);
233            assertEquals(0,_pool.getNumActive());
234            assertEquals(0,_pool.getNumIdle());
235            assertEquals(0,_pool.getNumActive(keya));
236            assertEquals(0,_pool.getNumIdle(keya));
237            assertEquals(0,_pool.getNumActive(keyb));
238            assertEquals(0,_pool.getNumIdle(keyb));
239    
240            Object objA0 = _pool.borrowObject(keya);
241            Object objB0 = _pool.borrowObject(keyb);
242    
243            assertEquals(2,_pool.getNumActive());
244            assertEquals(0,_pool.getNumIdle());
245            assertEquals(1,_pool.getNumActive(keya));
246            assertEquals(0,_pool.getNumIdle(keya));
247            assertEquals(1,_pool.getNumActive(keyb));
248            assertEquals(0,_pool.getNumIdle(keyb));
249    
250            Object objA1 = _pool.borrowObject(keya);
251            Object objB1 = _pool.borrowObject(keyb);
252    
253            assertEquals(4,_pool.getNumActive());
254            assertEquals(0,_pool.getNumIdle());
255            assertEquals(2,_pool.getNumActive(keya));
256            assertEquals(0,_pool.getNumIdle(keya));
257            assertEquals(2,_pool.getNumActive(keyb));
258            assertEquals(0,_pool.getNumIdle(keyb));
259    
260            _pool.returnObject(keya,objA0);
261            _pool.returnObject(keyb,objB0);
262    
263            assertEquals(2,_pool.getNumActive());
264            assertEquals(2,_pool.getNumIdle());
265            assertEquals(1,_pool.getNumActive(keya));
266            assertEquals(1,_pool.getNumIdle(keya));
267            assertEquals(1,_pool.getNumActive(keyb));
268            assertEquals(1,_pool.getNumIdle(keyb));
269    
270            _pool.returnObject(keya,objA1);
271            _pool.returnObject(keyb,objB1);
272    
273            assertEquals(0,_pool.getNumActive());
274            assertEquals(4,_pool.getNumIdle());
275            assertEquals(0,_pool.getNumActive(keya));
276            assertEquals(2,_pool.getNumIdle(keya));
277            assertEquals(0,_pool.getNumActive(keyb));
278            assertEquals(2,_pool.getNumIdle(keyb));
279        }
280    
281        public void testBaseClear() throws Exception {
282            try {
283                _pool = makeEmptyPool(3);
284            } catch(UnsupportedOperationException uoe) {
285                return; // skip this test if unsupported
286            }
287            Object keya = makeKey(0);
288            assertEquals(0,_pool.getNumActive(keya));
289            assertEquals(0,_pool.getNumIdle(keya));
290            Object obj0 = _pool.borrowObject(keya);
291            Object obj1 = _pool.borrowObject(keya);
292            assertEquals(2,_pool.getNumActive(keya));
293            assertEquals(0,_pool.getNumIdle(keya));
294            _pool.returnObject(keya,obj1);
295            _pool.returnObject(keya,obj0);
296            assertEquals(0,_pool.getNumActive(keya));
297            assertEquals(2,_pool.getNumIdle(keya));
298            _pool.clear(keya);
299            assertEquals(0,_pool.getNumActive(keya));
300            assertEquals(0,_pool.getNumIdle(keya));
301            Object obj2 = _pool.borrowObject(keya);
302            assertEquals(getNthObject(keya,2),obj2);
303        }
304    
305        public void testBaseInvalidateObject() throws Exception {
306            try {
307                _pool = makeEmptyPool(3);
308            } catch(UnsupportedOperationException uoe) {
309                return; // skip this test if unsupported
310            }
311            Object keya = makeKey(0);
312            assertEquals(0,_pool.getNumActive(keya));
313            assertEquals(0,_pool.getNumIdle(keya));
314            Object obj0 = _pool.borrowObject(keya);
315            Object obj1 = _pool.borrowObject(keya);
316            assertEquals(2,_pool.getNumActive(keya));
317            assertEquals(0,_pool.getNumIdle(keya));
318            _pool.invalidateObject(keya,obj0);
319            assertEquals(1,_pool.getNumActive(keya));
320            assertEquals(0,_pool.getNumIdle(keya));
321            _pool.invalidateObject(keya,obj1);
322            assertEquals(0,_pool.getNumActive(keya));
323            assertEquals(0,_pool.getNumIdle(keya));
324        }
325    
326        public void testBaseAddObject() throws Exception {
327            try {
328                _pool = makeEmptyPool(3);
329            } catch(UnsupportedOperationException uoe) {
330                return; // skip this test if unsupported
331            }
332            Object key = makeKey(0);
333            try {
334                assertEquals(0,_pool.getNumIdle());
335                assertEquals(0,_pool.getNumActive());
336                assertEquals(0,_pool.getNumIdle(key));
337                assertEquals(0,_pool.getNumActive(key));
338                _pool.addObject(key);
339                assertEquals(1,_pool.getNumIdle());
340                assertEquals(0,_pool.getNumActive());
341                assertEquals(1,_pool.getNumIdle(key));
342                assertEquals(0,_pool.getNumActive(key));
343                Object obj = _pool.borrowObject(key);
344                assertEquals(getNthObject(key,0),obj);
345                assertEquals(0,_pool.getNumIdle());
346                assertEquals(1,_pool.getNumActive());
347                assertEquals(0,_pool.getNumIdle(key));
348                assertEquals(1,_pool.getNumActive(key));
349                _pool.returnObject(key,obj);
350                assertEquals(1,_pool.getNumIdle());
351                assertEquals(0,_pool.getNumActive());
352                assertEquals(1,_pool.getNumIdle(key));
353                assertEquals(0,_pool.getNumActive(key));
354            } catch(UnsupportedOperationException e) {
355                return; // skip this test if one of those calls is unsupported
356            }
357        }
358    }