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