View Javadoc

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 java.util.NoSuchElementException;
21  
22  import org.apache.commons.pool.MethodCallPoolableObjectFactory;
23  import org.apache.commons.pool.ObjectPoolFactory;
24  import org.apache.commons.pool.PoolableObjectFactory;
25  import org.apache.commons.pool.TestObjectPoolFactory;
26  
27  /**
28   * Tests for {@link GenericObjectPoolFactory}.
29   *
30   * @author Sandy McArthur
31   * @version $Revision: 901944 $ $Date: 2010-01-21 17:27:04 -0700 (Thu, 21 Jan 2010) $
32   */
33  public class TestGenericObjectPoolFactory extends TestObjectPoolFactory {
34      public TestGenericObjectPoolFactory(final String name) {
35          super(name);
36      }
37  
38      protected ObjectPoolFactory makeFactory(final PoolableObjectFactory objectFactory) throws UnsupportedOperationException {
39          return new GenericObjectPoolFactory(objectFactory);
40      }
41  
42      public void testConstructors() throws Exception {
43          GenericObjectPoolFactory factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory());
44          GenericObjectPool pool;
45          factory.createPool().close();
46  
47          final GenericObjectPool.Config config = new GenericObjectPool.Config();
48          config.maxActive = 1;
49          config.maxIdle = 2;
50          config.maxWait = 3;
51          config.minIdle = 4;
52          config.minEvictableIdleTimeMillis = 5;
53          config.numTestsPerEvictionRun = 6;
54          config.softMinEvictableIdleTimeMillis = 7;
55          config.testOnBorrow = true;
56          config.testOnReturn = false;
57          config.testWhileIdle = true;
58          config.lifo = false;
59          config.timeBetweenEvictionRunsMillis = 8;
60          config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
61          factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), config);
62          pool = (GenericObjectPool)factory.createPool();
63          assertEquals(1, pool.getMaxActive());
64          assertEquals(2, pool.getMaxIdle());
65          assertEquals(3, pool.getMaxWait());
66          assertEquals(4, pool.getMinIdle());
67          assertEquals(5, pool.getMinEvictableIdleTimeMillis());
68          assertEquals(6, pool.getNumTestsPerEvictionRun());
69          assertEquals(7, pool.getSoftMinEvictableIdleTimeMillis());
70          assertEquals(true, pool.getTestOnBorrow());
71          assertEquals(false, pool.getTestOnReturn());
72          assertEquals(true, pool.getTestWhileIdle());
73          assertEquals(false, pool.getLifo());
74          assertEquals(8, pool.getTimeBetweenEvictionRunsMillis());
75          assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
76          pool.borrowObject();
77          pool.close();
78  
79  
80          factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1);
81          pool = (GenericObjectPool)factory.createPool();
82          assertEquals(1, pool.getMaxActive());
83          pool.borrowObject();
84          pool.close();
85  
86  
87          factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 125);
88          pool = (GenericObjectPool)factory.createPool();
89          assertEquals(1, pool.getMaxActive());
90          assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
91          assertEquals(125, pool.getMaxWait());
92          pool.borrowObject();
93          long startTime = System.currentTimeMillis();
94          try {
95              pool.borrowObject();
96              fail();
97          } catch (NoSuchElementException nsee) {
98              // expected
99          }
100         long delay = System.currentTimeMillis() - startTime;
101         assertTrue("delay: " + delay, delay > 100);
102         pool.close();
103 
104 
105         factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, true, false);
106         pool = (GenericObjectPool)factory.createPool();
107         assertEquals(1, pool.getMaxActive());
108         assertEquals(2, pool.getMaxWait());
109         assertEquals(true, pool.getTestOnBorrow());
110         assertEquals(false, pool.getTestOnReturn());
111         assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
112         pool.borrowObject();
113         pool.close();
114 
115 
116         factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, 3);
117         pool = (GenericObjectPool)factory.createPool();
118         assertEquals(1, pool.getMaxActive());
119         assertEquals(2, pool.getMaxWait());
120         assertEquals(3, pool.getMaxIdle());
121         assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
122         pool.borrowObject();
123         pool.close();
124 
125 
126         factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, true, false);
127         pool = (GenericObjectPool)factory.createPool();
128         assertEquals(1, pool.getMaxActive());
129         assertEquals(2, pool.getMaxWait());
130         assertEquals(3, pool.getMaxIdle());
131         assertEquals(true, pool.getTestOnBorrow());
132         assertEquals(false, pool.getTestOnReturn());
133         assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
134         pool.borrowObject();
135         pool.close();
136 
137 
138         factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, true, false, 4, 5, 6, false);
139         pool = (GenericObjectPool)factory.createPool();
140         assertEquals(1, pool.getMaxActive());
141         assertEquals(2, pool.getMaxWait());
142         assertEquals(3, pool.getMaxIdle());
143         assertEquals(4, pool.getTimeBetweenEvictionRunsMillis());
144         assertEquals(5, pool.getNumTestsPerEvictionRun());
145         assertEquals(6, pool.getMinEvictableIdleTimeMillis());
146         assertEquals(true, pool.getTestOnBorrow());
147         assertEquals(false, pool.getTestOnReturn());
148         assertEquals(false, pool.getTestWhileIdle());
149         assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
150         pool.borrowObject();
151         pool.close();
152 
153 
154         factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, 4, true, false, 5, 6, 7, true);
155         pool = (GenericObjectPool)factory.createPool();
156         assertEquals(1, pool.getMaxActive());
157         assertEquals(2, pool.getMaxWait());
158         assertEquals(3, pool.getMaxIdle());
159         assertEquals(4, pool.getMinIdle());
160         assertEquals(5, pool.getTimeBetweenEvictionRunsMillis());
161         assertEquals(6, pool.getNumTestsPerEvictionRun());
162         assertEquals(7, pool.getMinEvictableIdleTimeMillis());
163         assertEquals(true, pool.getTestOnBorrow());
164         assertEquals(false, pool.getTestOnReturn());
165         assertEquals(true, pool.getTestWhileIdle());
166         assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
167         pool.borrowObject();
168         pool.close();
169 
170 
171         factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, 4, true, false, 5, 6, 7, true, 8, false);
172         pool = (GenericObjectPool)factory.createPool();
173         assertEquals(1, pool.getMaxActive());
174         assertEquals(2, pool.getMaxWait());
175         assertEquals(3, pool.getMaxIdle());
176         assertEquals(4, pool.getMinIdle());
177         assertEquals(5, pool.getTimeBetweenEvictionRunsMillis());
178         assertEquals(6, pool.getNumTestsPerEvictionRun());
179         assertEquals(7, pool.getMinEvictableIdleTimeMillis());
180         assertEquals(8, pool.getSoftMinEvictableIdleTimeMillis());
181         assertEquals(true, pool.getTestOnBorrow());
182         assertEquals(false, pool.getTestOnReturn());
183         assertEquals(true, pool.getTestWhileIdle());
184         assertEquals(false, pool.getLifo());
185         assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
186         pool.borrowObject();
187         pool.close();
188     }
189 }