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 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 GenericKeyedObjectPoolFactory}.
31   *
32   * @author Sandy McArthur
33   * @version $Revision: 604543 $ $Date: 2007-12-15 19:28:53 -0700 (Sat, 15 Dec 2007) $
34   */
35  public class TestGenericKeyedObjectPoolFactory extends TestKeyedObjectPoolFactory {
36      public TestGenericKeyedObjectPoolFactory(final String name) {
37          super(name);
38      }
39  
40      public static Test suite() {
41          return new TestSuite(TestGenericKeyedObjectPoolFactory.class);
42      }
43  
44      protected KeyedObjectPoolFactory makeFactory(final KeyedPoolableObjectFactory objectFactory) {
45          return new GenericKeyedObjectPoolFactory(objectFactory);
46      }
47  
48      public void testConstructors() throws Exception {
49          GenericKeyedObjectPoolFactory factory = new GenericKeyedObjectPoolFactory(createObjectFactory());
50          factory.createPool().close();
51          GenericKeyedObjectPool pool;
52  
53  
54          final GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
55          config.maxActive = 1;
56          config.maxIdle = 2;
57          config.maxWait = 3;
58          config.minIdle = 4;
59          config.minEvictableIdleTimeMillis = 5;
60          config.numTestsPerEvictionRun = 6;
61          config.testOnBorrow = true;
62          config.testOnReturn = false;
63          config.testWhileIdle = true;
64          config.timeBetweenEvictionRunsMillis = 8;
65          config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
66          config.lifo = false;
67          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), config);
68          pool = (GenericKeyedObjectPool)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(true, pool.getTestOnBorrow());
76          assertEquals(false, pool.getTestOnReturn());
77          assertEquals(true, pool.getTestWhileIdle());
78          assertEquals(false, pool.getLifo());
79          assertEquals(8, pool.getTimeBetweenEvictionRunsMillis());
80          assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
81          pool.close();
82  
83  
84          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1);
85          pool = (GenericKeyedObjectPool)factory.createPool();
86          assertEquals(1, pool.getMaxActive());
87          pool.close();
88  
89  
90          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, 125);
91          pool = (GenericKeyedObjectPool)factory.createPool();
92          assertEquals(1, pool.getMaxActive());
93          assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
94          assertEquals(125, pool.getMaxWait());
95          pool.close();
96  
97  
98          factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, true, false);
99          pool = (GenericKeyedObjectPool)factory.createPool();
100         assertEquals(1, pool.getMaxActive());
101         assertEquals(2, pool.getMaxWait());
102         assertEquals(true, pool.getTestOnBorrow());
103         assertEquals(false, pool.getTestOnReturn());
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);
109         pool = (GenericKeyedObjectPool)factory.createPool();
110         assertEquals(1, pool.getMaxActive());
111         assertEquals(2, pool.getMaxWait());
112         assertEquals(3, pool.getMaxIdle());
113         assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
114         pool.close();
115 
116 
117         factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, 4);
118         pool = (GenericKeyedObjectPool)factory.createPool();
119         assertEquals(1, pool.getMaxActive());
120         assertEquals(2, pool.getMaxWait());
121         assertEquals(3, pool.getMaxIdle());
122         assertEquals(4, pool.getMaxTotal());
123         assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
124         pool.close();
125 
126 
127         factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, true, false);
128         pool = (GenericKeyedObjectPool)factory.createPool();
129         assertEquals(1, pool.getMaxActive());
130         assertEquals(2, pool.getMaxWait());
131         assertEquals(3, pool.getMaxIdle());
132         assertEquals(true, pool.getTestOnBorrow());
133         assertEquals(false, pool.getTestOnReturn());
134         assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
135         pool.close();
136 
137 
138         factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, true, false, 4, 5, 6, false);
139         pool = (GenericKeyedObjectPool)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(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
150         pool.close();
151 
152 
153         factory = new GenericKeyedObjectPoolFactory(createObjectFactory(), 1, GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 2, 3, 4, true, false, 5, 6, 7, true);
154         pool = (GenericKeyedObjectPool)factory.createPool();
155         assertEquals(1, pool.getMaxActive());
156         assertEquals(2, pool.getMaxWait());
157         assertEquals(3, pool.getMaxIdle());
158         assertEquals(4, pool.getMaxTotal());
159         assertEquals(5, pool.getTimeBetweenEvictionRunsMillis());
160         assertEquals(6, pool.getNumTestsPerEvictionRun());
161         assertEquals(7, pool.getMinEvictableIdleTimeMillis());
162         assertEquals(true, pool.getTestOnBorrow());
163         assertEquals(false, pool.getTestOnReturn());
164         assertEquals(true, pool.getTestWhileIdle());
165         assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
166         pool.close();
167     }
168 }