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.TestKeyedObjectPoolFactory;
21  import org.apache.commons.pool.KeyedObjectPoolFactory;
22  import org.apache.commons.pool.KeyedPoolableObjectFactory;
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  /**
27   * Tests for {@link GenericKeyedObjectPoolFactory}.
28   *
29   * @author Sandy McArthur
30   * @version $Revision: 774099 $ $Date: 2009-05-12 17:29:02 -0400 (Tue, 12 May 2009) $
31   */
32  public class TestGenericKeyedObjectPoolFactory extends TestKeyedObjectPoolFactory {
33      public TestGenericKeyedObjectPoolFactory(final String name) {
34          super(name);
35      }
36  
37      public static Test suite() {
38          return new TestSuite(TestGenericKeyedObjectPoolFactory.class);
39      }
40  
41      protected KeyedObjectPoolFactory makeFactory(final KeyedPoolableObjectFactory objectFactory) {
42          return new GenericKeyedObjectPoolFactory(objectFactory);
43      }
44  
45      public void testConstructors() throws Exception {
46          GenericKeyedObjectPoolFactory factory = new GenericKeyedObjectPoolFactory(createObjectFactory());
47          factory.createPool().close();
48          GenericKeyedObjectPool pool;
49  
50  
51          final GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
52          config.maxActive = 1;
53          config.maxIdle = 2;
54          config.maxWait = 3;
55          config.minIdle = 4;
56          config.minEvictableIdleTimeMillis = 5;
57          config.numTestsPerEvictionRun = 6;
58          config.testOnBorrow = true;
59          config.testOnReturn = false;
60          config.testWhileIdle = true;
61          config.timeBetweenEvictionRunsMillis = 8;
62          config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
63          config.lifo = false;
64          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), config);
65          pool = (GenericKeyedObjectPool)factory.createPool();
66          assertEquals(1, pool.getMaxActive());
67          assertEquals(2, pool.getMaxIdle());
68          assertEquals(3, pool.getMaxWait());
69          assertEquals(4, pool.getMinIdle());
70          assertEquals(5, pool.getMinEvictableIdleTimeMillis());
71          assertEquals(6, pool.getNumTestsPerEvictionRun());
72          assertEquals(true, pool.getTestOnBorrow());
73          assertEquals(false, pool.getTestOnReturn());
74          assertEquals(true, pool.getTestWhileIdle());
75          assertEquals(false, pool.getLifo());
76          assertEquals(8, pool.getTimeBetweenEvictionRunsMillis());
77          assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
78          pool.close();
79  
80  
81          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1);
82          pool = (GenericKeyedObjectPool)factory.createPool();
83          assertEquals(1, pool.getMaxActive());
84          pool.close();
85  
86  
87          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, 125);
88          pool = (GenericKeyedObjectPool)factory.createPool();
89          assertEquals(1, pool.getMaxActive());
90          assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
91          assertEquals(125, pool.getMaxWait());
92          pool.close();
93  
94  
95          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, true, false);
96          pool = (GenericKeyedObjectPool)factory.createPool();
97          assertEquals(1, pool.getMaxActive());
98          assertEquals(2, pool.getMaxWait());
99          assertEquals(true, pool.getTestOnBorrow());
100         assertEquals(false, pool.getTestOnReturn());
101         assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
102         pool.close();
103 
104 
105         factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3);
106         pool = (GenericKeyedObjectPool)factory.createPool();
107         assertEquals(1, pool.getMaxActive());
108         assertEquals(2, pool.getMaxWait());
109         assertEquals(3, pool.getMaxIdle());
110         assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
111         pool.close();
112 
113 
114         factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, 4);
115         pool = (GenericKeyedObjectPool)factory.createPool();
116         assertEquals(1, pool.getMaxActive());
117         assertEquals(2, pool.getMaxWait());
118         assertEquals(3, pool.getMaxIdle());
119         assertEquals(4, pool.getMaxTotal());
120         assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
121         pool.close();
122 
123 
124         factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, true, false);
125         pool = (GenericKeyedObjectPool)factory.createPool();
126         assertEquals(1, pool.getMaxActive());
127         assertEquals(2, pool.getMaxWait());
128         assertEquals(3, pool.getMaxIdle());
129         assertEquals(true, pool.getTestOnBorrow());
130         assertEquals(false, pool.getTestOnReturn());
131         assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
132         pool.close();
133 
134 
135         factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, true, false, 4, 5, 6, false);
136         pool = (GenericKeyedObjectPool)factory.createPool();
137         assertEquals(1, pool.getMaxActive());
138         assertEquals(2, pool.getMaxWait());
139         assertEquals(3, pool.getMaxIdle());
140         assertEquals(4, pool.getTimeBetweenEvictionRunsMillis());
141         assertEquals(5, pool.getNumTestsPerEvictionRun());
142         assertEquals(6, pool.getMinEvictableIdleTimeMillis());
143         assertEquals(true, pool.getTestOnBorrow());
144         assertEquals(false, pool.getTestOnReturn());
145         assertEquals(false, pool.getTestWhileIdle());
146         assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
147         pool.close();
148 
149 
150         factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, 4, true, false, 5, 6, 7, true);
151         pool = (GenericKeyedObjectPool)factory.createPool();
152         assertEquals(1, pool.getMaxActive());
153         assertEquals(2, pool.getMaxWait());
154         assertEquals(3, pool.getMaxIdle());
155         assertEquals(4, pool.getMaxTotal());
156         assertEquals(5, pool.getTimeBetweenEvictionRunsMillis());
157         assertEquals(6, pool.getNumTestsPerEvictionRun());
158         assertEquals(7, pool.getMinEvictableIdleTimeMillis());
159         assertEquals(true, pool.getTestOnBorrow());
160         assertEquals(false, pool.getTestOnReturn());
161         assertEquals(true, pool.getTestWhileIdle());
162         assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
163         pool.close();
164     }
165 }