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    
018    package org.apache.commons.pool.impl;
019    
020    import org.apache.commons.pool.TestKeyedObjectPoolFactory;
021    import org.apache.commons.pool.KeyedObjectPoolFactory;
022    import org.apache.commons.pool.KeyedPoolableObjectFactory;
023    import junit.framework.Test;
024    import junit.framework.TestSuite;
025    
026    /**
027     * Tests for {@link GenericKeyedObjectPoolFactory}.
028     *
029     * @author Sandy McArthur
030     * @version $Revision: 774099 $ $Date: 2009-05-12 17:29:02 -0400 (Tue, 12 May 2009) $
031     */
032    public class TestGenericKeyedObjectPoolFactory extends TestKeyedObjectPoolFactory {
033        public TestGenericKeyedObjectPoolFactory(final String name) {
034            super(name);
035        }
036    
037        public static Test suite() {
038            return new TestSuite(TestGenericKeyedObjectPoolFactory.class);
039        }
040    
041        protected KeyedObjectPoolFactory makeFactory(final KeyedPoolableObjectFactory objectFactory) {
042            return new GenericKeyedObjectPoolFactory(objectFactory);
043        }
044    
045        public void testConstructors() throws Exception {
046            GenericKeyedObjectPoolFactory factory = new GenericKeyedObjectPoolFactory(createObjectFactory());
047            factory.createPool().close();
048            GenericKeyedObjectPool pool;
049    
050    
051            final GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
052            config.maxActive = 1;
053            config.maxIdle = 2;
054            config.maxWait = 3;
055            config.minIdle = 4;
056            config.minEvictableIdleTimeMillis = 5;
057            config.numTestsPerEvictionRun = 6;
058            config.testOnBorrow = true;
059            config.testOnReturn = false;
060            config.testWhileIdle = true;
061            config.timeBetweenEvictionRunsMillis = 8;
062            config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
063            config.lifo = false;
064            factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), config);
065            pool = (GenericKeyedObjectPool)factory.createPool();
066            assertEquals(1, pool.getMaxActive());
067            assertEquals(2, pool.getMaxIdle());
068            assertEquals(3, pool.getMaxWait());
069            assertEquals(4, pool.getMinIdle());
070            assertEquals(5, pool.getMinEvictableIdleTimeMillis());
071            assertEquals(6, pool.getNumTestsPerEvictionRun());
072            assertEquals(true, pool.getTestOnBorrow());
073            assertEquals(false, pool.getTestOnReturn());
074            assertEquals(true, pool.getTestWhileIdle());
075            assertEquals(false, pool.getLifo());
076            assertEquals(8, pool.getTimeBetweenEvictionRunsMillis());
077            assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
078            pool.close();
079    
080    
081            factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1);
082            pool = (GenericKeyedObjectPool)factory.createPool();
083            assertEquals(1, pool.getMaxActive());
084            pool.close();
085    
086    
087            factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, 125);
088            pool = (GenericKeyedObjectPool)factory.createPool();
089            assertEquals(1, pool.getMaxActive());
090            assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
091            assertEquals(125, pool.getMaxWait());
092            pool.close();
093    
094    
095            factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, true, false);
096            pool = (GenericKeyedObjectPool)factory.createPool();
097            assertEquals(1, pool.getMaxActive());
098            assertEquals(2, pool.getMaxWait());
099            assertEquals(true, pool.getTestOnBorrow());
100            assertEquals(false, pool.getTestOnReturn());
101            assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
102            pool.close();
103    
104    
105            factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3);
106            pool = (GenericKeyedObjectPool)factory.createPool();
107            assertEquals(1, pool.getMaxActive());
108            assertEquals(2, pool.getMaxWait());
109            assertEquals(3, pool.getMaxIdle());
110            assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
111            pool.close();
112    
113    
114            factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, 4);
115            pool = (GenericKeyedObjectPool)factory.createPool();
116            assertEquals(1, pool.getMaxActive());
117            assertEquals(2, pool.getMaxWait());
118            assertEquals(3, pool.getMaxIdle());
119            assertEquals(4, pool.getMaxTotal());
120            assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
121            pool.close();
122    
123    
124            factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, true, false);
125            pool = (GenericKeyedObjectPool)factory.createPool();
126            assertEquals(1, pool.getMaxActive());
127            assertEquals(2, pool.getMaxWait());
128            assertEquals(3, pool.getMaxIdle());
129            assertEquals(true, pool.getTestOnBorrow());
130            assertEquals(false, pool.getTestOnReturn());
131            assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
132            pool.close();
133    
134    
135            factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, true, false, 4, 5, 6, false);
136            pool = (GenericKeyedObjectPool)factory.createPool();
137            assertEquals(1, pool.getMaxActive());
138            assertEquals(2, pool.getMaxWait());
139            assertEquals(3, pool.getMaxIdle());
140            assertEquals(4, pool.getTimeBetweenEvictionRunsMillis());
141            assertEquals(5, pool.getNumTestsPerEvictionRun());
142            assertEquals(6, pool.getMinEvictableIdleTimeMillis());
143            assertEquals(true, pool.getTestOnBorrow());
144            assertEquals(false, pool.getTestOnReturn());
145            assertEquals(false, pool.getTestWhileIdle());
146            assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
147            pool.close();
148    
149    
150            factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, 4, true, false, 5, 6, 7, true);
151            pool = (GenericKeyedObjectPool)factory.createPool();
152            assertEquals(1, pool.getMaxActive());
153            assertEquals(2, pool.getMaxWait());
154            assertEquals(3, pool.getMaxIdle());
155            assertEquals(4, pool.getMaxTotal());
156            assertEquals(5, pool.getTimeBetweenEvictionRunsMillis());
157            assertEquals(6, pool.getNumTestsPerEvictionRun());
158            assertEquals(7, pool.getMinEvictableIdleTimeMillis());
159            assertEquals(true, pool.getTestOnBorrow());
160            assertEquals(false, pool.getTestOnReturn());
161            assertEquals(true, pool.getTestWhileIdle());
162            assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
163            pool.close();
164        }
165    }