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