1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.pool.impl;
19  
20  import org.apache.commons.pool.TestObjectPoolFactory;
21  import org.apache.commons.pool.ObjectPoolFactory;
22  import org.apache.commons.pool.PoolableObjectFactory;
23  import org.apache.commons.pool.MethodCallPoolableObjectFactory;
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import java.util.NoSuchElementException;
28  
29  /**
30   * Tests for {@link GenericObjectPoolFactory}.
31   *
32   * @author Sandy McArthur
33   * @version $Revision: 604116 $ $Date: 2007-12-14 02:03:04 -0500 (Fri, 14 Dec 2007) $
34   */
35  public class TestGenericObjectPoolFactory extends TestObjectPoolFactory {
36      public TestGenericObjectPoolFactory(final String name) {
37          super(name);
38      }
39  
40      public static Test suite() {
41          return new TestSuite(TestGenericObjectPoolFactory.class);
42      }
43  
44      protected ObjectPoolFactory makeFactory(final PoolableObjectFactory objectFactory) throws UnsupportedOperationException {
45          return new GenericObjectPoolFactory(objectFactory);
46      }
47  
48      public void testConstructors() throws Exception {
49          GenericObjectPoolFactory factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory());
50          GenericObjectPool pool;
51          factory.createPool().close();
52  
53          final GenericObjectPool.Config config = new GenericObjectPool.Config();
54          config.maxActive = 1;
55          config.maxIdle = 2;
56          config.maxWait = 3;
57          config.minIdle = 4;
58          config.minEvictableIdleTimeMillis = 5;
59          config.numTestsPerEvictionRun = 6;
60          config.softMinEvictableIdleTimeMillis = 7;
61          config.testOnBorrow = true;
62          config.testOnReturn = false;
63          config.testWhileIdle = true;
64          config.lifo = false;
65          config.timeBetweenEvictionRunsMillis = 8;
66          config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
67          factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), config);
68          pool = (GenericObjectPool)factory.createPool();
69          assertEquals(1, pool.getMaxActive());
70          assertEquals(2, pool.getMaxIdle());
71          assertEquals(3, pool.getMaxWait());
72          assertEquals(4, pool.getMinIdle());
73          assertEquals(5, pool.getMinEvictableIdleTimeMillis());
74          assertEquals(6, pool.getNumTestsPerEvictionRun());
75          assertEquals(7, pool.getSoftMinEvictableIdleTimeMillis());
76          assertEquals(true, pool.getTestOnBorrow());
77          assertEquals(false, pool.getTestOnReturn());
78          assertEquals(true, pool.getTestWhileIdle());
79          assertEquals(false, pool.getLifo());
80          assertEquals(8, pool.getTimeBetweenEvictionRunsMillis());
81          assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
82          pool.borrowObject();
83          pool.close();
84  
85  
86          factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1);
87          pool = (GenericObjectPool)factory.createPool();
88          assertEquals(1, pool.getMaxActive());
89          pool.borrowObject();
90          pool.close();
91  
92  
93          factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 125);
94          pool = (GenericObjectPool)factory.createPool();
95          assertEquals(1, pool.getMaxActive());
96          assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
97          assertEquals(125, pool.getMaxWait());
98          pool.borrowObject();
99          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 }