001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.pool.impl; 019 020 import java.util.NoSuchElementException; 021 022 import org.apache.commons.pool.MethodCallPoolableObjectFactory; 023 import org.apache.commons.pool.ObjectPoolFactory; 024 import org.apache.commons.pool.PoolableObjectFactory; 025 import org.apache.commons.pool.TestObjectPoolFactory; 026 027 /** 028 * Tests for {@link GenericObjectPoolFactory}. 029 * 030 * @author Sandy McArthur 031 * @version $Revision: 901944 $ $Date: 2010-01-21 17:27:04 -0700 (Thu, 21 Jan 2010) $ 032 */ 033 public class TestGenericObjectPoolFactory extends TestObjectPoolFactory { 034 public TestGenericObjectPoolFactory(final String name) { 035 super(name); 036 } 037 038 protected ObjectPoolFactory makeFactory(final PoolableObjectFactory objectFactory) throws UnsupportedOperationException { 039 return new GenericObjectPoolFactory(objectFactory); 040 } 041 042 public void testConstructors() throws Exception { 043 GenericObjectPoolFactory factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory()); 044 GenericObjectPool pool; 045 factory.createPool().close(); 046 047 final GenericObjectPool.Config config = new GenericObjectPool.Config(); 048 config.maxActive = 1; 049 config.maxIdle = 2; 050 config.maxWait = 3; 051 config.minIdle = 4; 052 config.minEvictableIdleTimeMillis = 5; 053 config.numTestsPerEvictionRun = 6; 054 config.softMinEvictableIdleTimeMillis = 7; 055 config.testOnBorrow = true; 056 config.testOnReturn = false; 057 config.testWhileIdle = true; 058 config.lifo = false; 059 config.timeBetweenEvictionRunsMillis = 8; 060 config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW; 061 factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), config); 062 pool = (GenericObjectPool)factory.createPool(); 063 assertEquals(1, pool.getMaxActive()); 064 assertEquals(2, pool.getMaxIdle()); 065 assertEquals(3, pool.getMaxWait()); 066 assertEquals(4, pool.getMinIdle()); 067 assertEquals(5, pool.getMinEvictableIdleTimeMillis()); 068 assertEquals(6, pool.getNumTestsPerEvictionRun()); 069 assertEquals(7, pool.getSoftMinEvictableIdleTimeMillis()); 070 assertEquals(true, pool.getTestOnBorrow()); 071 assertEquals(false, pool.getTestOnReturn()); 072 assertEquals(true, pool.getTestWhileIdle()); 073 assertEquals(false, pool.getLifo()); 074 assertEquals(8, pool.getTimeBetweenEvictionRunsMillis()); 075 assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction()); 076 pool.borrowObject(); 077 pool.close(); 078 079 080 factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1); 081 pool = (GenericObjectPool)factory.createPool(); 082 assertEquals(1, pool.getMaxActive()); 083 pool.borrowObject(); 084 pool.close(); 085 086 087 factory = new GenericObjectPoolFactory(new MethodCallPoolableObjectFactory(), 1, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 125); 088 pool = (GenericObjectPool)factory.createPool(); 089 assertEquals(1, pool.getMaxActive()); 090 assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction()); 091 assertEquals(125, pool.getMaxWait()); 092 pool.borrowObject(); 093 long startTime = System.currentTimeMillis(); 094 try { 095 pool.borrowObject(); 096 fail(); 097 } catch (NoSuchElementException nsee) { 098 // expected 099 } 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 }