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.TestObjectPoolFactory;
021    import org.apache.commons.pool.ObjectPoolFactory;
022    import org.apache.commons.pool.PoolableObjectFactory;
023    import org.apache.commons.pool.MethodCallPoolableObjectFactory;
024    import junit.framework.Test;
025    import junit.framework.TestSuite;
026    
027    import java.util.NoSuchElementException;
028    
029    /**
030     * Tests for {@link GenericObjectPoolFactory}.
031     *
032     * @author Sandy McArthur
033     * @version $Revision: 604116 $ $Date: 2007-12-14 02:03:04 -0500 (Fri, 14 Dec 2007) $
034     */
035    public class TestGenericObjectPoolFactory extends TestObjectPoolFactory {
036        public TestGenericObjectPoolFactory(final String name) {
037            super(name);
038        }
039    
040        public static Test suite() {
041            return new TestSuite(TestGenericObjectPoolFactory.class);
042        }
043    
044        protected ObjectPoolFactory makeFactory(final PoolableObjectFactory objectFactory) throws UnsupportedOperationException {
045            return new GenericObjectPoolFactory(objectFactory);
046        }
047    
048        public void testConstructors() throws Exception {
049            GenericObjectPoolFactory factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory());
050            GenericObjectPool pool;
051            factory.createPool().close();
052    
053            final GenericObjectPool.Config config = new GenericObjectPool.Config();
054            config.maxActive = 1;
055            config.maxIdle = 2;
056            config.maxWait = 3;
057            config.minIdle = 4;
058            config.minEvictableIdleTimeMillis = 5;
059            config.numTestsPerEvictionRun = 6;
060            config.softMinEvictableIdleTimeMillis = 7;
061            config.testOnBorrow = true;
062            config.testOnReturn = false;
063            config.testWhileIdle = true;
064            config.lifo = false;
065            config.timeBetweenEvictionRunsMillis = 8;
066            config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
067            factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), config);
068            pool = (GenericObjectPool)factory.createPool();
069            assertEquals(1, pool.getMaxActive());
070            assertEquals(2, pool.getMaxIdle());
071            assertEquals(3, pool.getMaxWait());
072            assertEquals(4, pool.getMinIdle());
073            assertEquals(5, pool.getMinEvictableIdleTimeMillis());
074            assertEquals(6, pool.getNumTestsPerEvictionRun());
075            assertEquals(7, pool.getSoftMinEvictableIdleTimeMillis());
076            assertEquals(true, pool.getTestOnBorrow());
077            assertEquals(false, pool.getTestOnReturn());
078            assertEquals(true, pool.getTestWhileIdle());
079            assertEquals(false, pool.getLifo());
080            assertEquals(8, pool.getTimeBetweenEvictionRunsMillis());
081            assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
082            pool.borrowObject();
083            pool.close();
084    
085    
086            factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1);
087            pool = (GenericObjectPool)factory.createPool();
088            assertEquals(1, pool.getMaxActive());
089            pool.borrowObject();
090            pool.close();
091    
092    
093            factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 125);
094            pool = (GenericObjectPool)factory.createPool();
095            assertEquals(1, pool.getMaxActive());
096            assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
097            assertEquals(125, pool.getMaxWait());
098            pool.borrowObject();
099            long startTime = System.currentTimeMillis();
100            try {
101                pool.borrowObject();
102                fail();
103            } catch (NoSuchElementException nsee) {
104                // expected
105            }
106            long delay = System.currentTimeMillis() - startTime;
107            assertTrue("delay: " + delay, delay > 100);
108            pool.close();
109    
110    
111            factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, true, false);
112            pool = (GenericObjectPool)factory.createPool();
113            assertEquals(1, pool.getMaxActive());
114            assertEquals(2, pool.getMaxWait());
115            assertEquals(true, pool.getTestOnBorrow());
116            assertEquals(false, pool.getTestOnReturn());
117            assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
118            pool.borrowObject();
119            pool.close();
120    
121    
122            factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, 3);
123            pool = (GenericObjectPool)factory.createPool();
124            assertEquals(1, pool.getMaxActive());
125            assertEquals(2, pool.getMaxWait());
126            assertEquals(3, pool.getMaxIdle());
127            assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
128            pool.borrowObject();
129            pool.close();
130    
131    
132            factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, true, false);
133            pool = (GenericObjectPool)factory.createPool();
134            assertEquals(1, pool.getMaxActive());
135            assertEquals(2, pool.getMaxWait());
136            assertEquals(3, pool.getMaxIdle());
137            assertEquals(true, pool.getTestOnBorrow());
138            assertEquals(false, pool.getTestOnReturn());
139            assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
140            pool.borrowObject();
141            pool.close();
142    
143    
144            factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, true, false, 4, 5, 6, false);
145            pool = (GenericObjectPool)factory.createPool();
146            assertEquals(1, pool.getMaxActive());
147            assertEquals(2, pool.getMaxWait());
148            assertEquals(3, pool.getMaxIdle());
149            assertEquals(4, pool.getTimeBetweenEvictionRunsMillis());
150            assertEquals(5, pool.getNumTestsPerEvictionRun());
151            assertEquals(6, pool.getMinEvictableIdleTimeMillis());
152            assertEquals(true, pool.getTestOnBorrow());
153            assertEquals(false, pool.getTestOnReturn());
154            assertEquals(false, pool.getTestWhileIdle());
155            assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
156            pool.borrowObject();
157            pool.close();
158    
159    
160            factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, 4, true, false, 5, 6, 7, true);
161            pool = (GenericObjectPool)factory.createPool();
162            assertEquals(1, pool.getMaxActive());
163            assertEquals(2, pool.getMaxWait());
164            assertEquals(3, pool.getMaxIdle());
165            assertEquals(4, pool.getMinIdle());
166            assertEquals(5, pool.getTimeBetweenEvictionRunsMillis());
167            assertEquals(6, pool.getNumTestsPerEvictionRun());
168            assertEquals(7, pool.getMinEvictableIdleTimeMillis());
169            assertEquals(true, pool.getTestOnBorrow());
170            assertEquals(false, pool.getTestOnReturn());
171            assertEquals(true, pool.getTestWhileIdle());
172            assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
173            pool.borrowObject();
174            pool.close();
175    
176    
177            factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, 4, true, false, 5, 6, 7, true, 8, false);
178            pool = (GenericObjectPool)factory.createPool();
179            assertEquals(1, pool.getMaxActive());
180            assertEquals(2, pool.getMaxWait());
181            assertEquals(3, pool.getMaxIdle());
182            assertEquals(4, pool.getMinIdle());
183            assertEquals(5, pool.getTimeBetweenEvictionRunsMillis());
184            assertEquals(6, pool.getNumTestsPerEvictionRun());
185            assertEquals(7, pool.getMinEvictableIdleTimeMillis());
186            assertEquals(8, pool.getSoftMinEvictableIdleTimeMillis());
187            assertEquals(true, pool.getTestOnBorrow());
188            assertEquals(false, pool.getTestOnReturn());
189            assertEquals(true, pool.getTestWhileIdle());
190            assertEquals(false, pool.getLifo());
191            assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
192            pool.borrowObject();
193            pool.close();
194        }
195    }