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 org.apache.commons.pool.KeyedObjectPoolFactory;
21  import org.apache.commons.pool.KeyedPoolableObjectFactory;
22  import org.apache.commons.pool.TestKeyedObjectPoolFactory;
23  
24  /**
25   * Tests for {@link GenericKeyedObjectPoolFactory}.
26   *
27   * @author Sandy McArthur
28   * @version $Revision: 901944 $ $Date: 2010-01-21 17:27:04 -0700 (Thu, 21 Jan 2010) $
29   */
30  public class TestGenericKeyedObjectPoolFactory extends TestKeyedObjectPoolFactory {
31      public TestGenericKeyedObjectPoolFactory(final String name) {
32          super(name);
33      }
34  
35      protected KeyedObjectPoolFactory makeFactory(final KeyedPoolableObjectFactory objectFactory) {
36          return new GenericKeyedObjectPoolFactory(objectFactory);
37      }
38  
39      public void testConstructors() throws Exception {
40          GenericKeyedObjectPoolFactory factory = new GenericKeyedObjectPoolFactory(createObjectFactory());
41          factory.createPool().close();
42          GenericKeyedObjectPool pool;
43  
44  
45          final GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
46          config.maxActive = 1;
47          config.maxIdle = 2;
48          config.maxWait = 3;
49          config.minIdle = 4;
50          config.minEvictableIdleTimeMillis = 5;
51          config.numTestsPerEvictionRun = 6;
52          config.testOnBorrow = true;
53          config.testOnReturn = false;
54          config.testWhileIdle = true;
55          config.timeBetweenEvictionRunsMillis = 8;
56          config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
57          config.lifo = false;
58          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), config);
59          pool = (GenericKeyedObjectPool)factory.createPool();
60          assertEquals(1, pool.getMaxActive());
61          assertEquals(2, pool.getMaxIdle());
62          assertEquals(3, pool.getMaxWait());
63          assertEquals(4, pool.getMinIdle());
64          assertEquals(5, pool.getMinEvictableIdleTimeMillis());
65          assertEquals(6, pool.getNumTestsPerEvictionRun());
66          assertEquals(true, pool.getTestOnBorrow());
67          assertEquals(false, pool.getTestOnReturn());
68          assertEquals(true, pool.getTestWhileIdle());
69          assertEquals(false, pool.getLifo());
70          assertEquals(8, pool.getTimeBetweenEvictionRunsMillis());
71          assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
72          pool.close();
73  
74  
75          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1);
76          pool = (GenericKeyedObjectPool)factory.createPool();
77          assertEquals(1, pool.getMaxActive());
78          pool.close();
79  
80  
81          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, 125);
82          pool = (GenericKeyedObjectPool)factory.createPool();
83          assertEquals(1, pool.getMaxActive());
84          assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
85          assertEquals(125, pool.getMaxWait());
86          pool.close();
87  
88  
89          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, true, false);
90          pool = (GenericKeyedObjectPool)factory.createPool();
91          assertEquals(1, pool.getMaxActive());
92          assertEquals(2, pool.getMaxWait());
93          assertEquals(true, pool.getTestOnBorrow());
94          assertEquals(false, pool.getTestOnReturn());
95          assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
96          pool.close();
97  
98  
99          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 }