📄 testgenerickeyedobjectpool.java
字号:
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.pool.impl;
import java.util.HashMap;
import java.util.NoSuchElementException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.pool.KeyedObjectPool;
import org.apache.commons.pool.KeyedPoolableObjectFactory;
import org.apache.commons.pool.TestKeyedObjectPool;
/**
* @author Rodney Waldhoff
* @version $Revision: 1.18 $ $Date: 2004/02/28 11:46:11 $
*/
public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
public TestGenericKeyedObjectPool(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestGenericKeyedObjectPool.class);
}
protected KeyedObjectPool makeEmptyPool(int mincapacity) {
GenericKeyedObjectPool pool = new GenericKeyedObjectPool(
new KeyedPoolableObjectFactory() {
HashMap map = new HashMap();
public Object makeObject(Object key) {
int counter = 0;
Integer Counter = (Integer)(map.get(key));
if(null != Counter) {
counter = Counter.intValue();
}
map.put(key,new Integer(counter + 1));
return String.valueOf(key) + String.valueOf(counter);
}
public void destroyObject(Object key, Object obj) { }
public boolean validateObject(Object key, Object obj) { return true; }
public void activateObject(Object key, Object obj) { }
public void passivateObject(Object key, Object obj) { }
}
);
pool.setMaxActive(mincapacity);
pool.setMaxIdle(mincapacity);
return pool;
}
protected Object getNthObject(Object key, int n) {
return String.valueOf(key) + String.valueOf(n);
}
protected Object makeKey(int n) {
return String.valueOf(n);
}
private GenericKeyedObjectPool pool = null;
public void setUp() throws Exception {
super.setUp();
pool = new GenericKeyedObjectPool(new SimpleFactory());
}
public void tearDown() throws Exception {
super.tearDown();
pool.close();
pool = null;
}
public void testWithInitiallyInvalid() throws Exception {
GenericKeyedObjectPool pool = new GenericKeyedObjectPool(new SimpleFactory(false));
pool.setTestOnBorrow(true);
try {
pool.borrowObject("xyzzy");
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
// expected
}
}
public void testZeroMaxActive() throws Exception {
pool.setMaxActive(0);
pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
Object obj = pool.borrowObject("");
assertEquals("0",obj);
pool.returnObject("",obj);
}
public void testNegativeMaxActive() throws Exception {
pool.setMaxActive(-1);
pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
Object obj = pool.borrowObject("");
assertEquals("0",obj);
pool.returnObject("",obj);
}
public void testNumActiveNumIdle2() throws Exception {
assertEquals(0,pool.getNumActive());
assertEquals(0,pool.getNumIdle());
assertEquals(0,pool.getNumActive("A"));
assertEquals(0,pool.getNumIdle("A"));
assertEquals(0,pool.getNumActive("B"));
assertEquals(0,pool.getNumIdle("B"));
Object objA0 = pool.borrowObject("A");
Object objB0 = pool.borrowObject("B");
assertEquals(2,pool.getNumActive());
assertEquals(0,pool.getNumIdle());
assertEquals(1,pool.getNumActive("A"));
assertEquals(0,pool.getNumIdle("A"));
assertEquals(1,pool.getNumActive("B"));
assertEquals(0,pool.getNumIdle("B"));
Object objA1 = pool.borrowObject("A");
Object objB1 = pool.borrowObject("B");
assertEquals(4,pool.getNumActive());
assertEquals(0,pool.getNumIdle());
assertEquals(2,pool.getNumActive("A"));
assertEquals(0,pool.getNumIdle("A"));
assertEquals(2,pool.getNumActive("B"));
assertEquals(0,pool.getNumIdle("B"));
pool.returnObject("A",objA0);
pool.returnObject("B",objB0);
assertEquals(2,pool.getNumActive());
assertEquals(2,pool.getNumIdle());
assertEquals(1,pool.getNumActive("A"));
assertEquals(1,pool.getNumIdle("A"));
assertEquals(1,pool.getNumActive("B"));
assertEquals(1,pool.getNumIdle("B"));
pool.returnObject("A",objA1);
pool.returnObject("B",objB1);
assertEquals(0,pool.getNumActive());
assertEquals(4,pool.getNumIdle());
assertEquals(0,pool.getNumActive("A"));
assertEquals(2,pool.getNumIdle("A"));
assertEquals(0,pool.getNumActive("B"));
assertEquals(2,pool.getNumIdle("B"));
}
public void testMaxIdle() throws Exception {
pool.setMaxActive(100);
pool.setMaxIdle(8);
Object[] active = new Object[100];
for(int i=0;i<100;i++) {
active[i] = pool.borrowObject("");
}
assertEquals(100,pool.getNumActive(""));
assertEquals(0,pool.getNumIdle(""));
for(int i=0;i<100;i++) {
pool.returnObject("",active[i]);
assertEquals(99 - i,pool.getNumActive(""));
assertEquals((i < 8 ? i+1 : 8),pool.getNumIdle(""));
}
}
public void testMaxActive() throws Exception {
pool.setMaxActive(3);
pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
pool.borrowObject("");
pool.borrowObject("");
pool.borrowObject("");
try {
pool.borrowObject("");
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
// expected
}
}
public void testMaxTotal() throws Exception {
pool.setMaxActive(2);
pool.setMaxTotal(3);
pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
Object o1 = pool.borrowObject("a");
assertNotNull(o1);
Object o2 = pool.borrowObject("a");
assertNotNull(o2);
Object o3 = pool.borrowObject("b");
assertNotNull(o3);
try {
pool.borrowObject("c");
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
// expected
}
assertEquals(0, pool.getNumIdle());
pool.returnObject("b", o3);
assertEquals(1, pool.getNumIdle());
assertEquals(1, pool.getNumIdle("b"));
Object o4 = pool.borrowObject("b");
assertNotNull(o4);
assertEquals(0, pool.getNumIdle());
assertEquals(0, pool.getNumIdle("b"));
}
public void testSettersAndGetters() throws Exception {
GenericKeyedObjectPool pool = new GenericKeyedObjectPool();
{
pool.setFactory(new SimpleFactory());
}
{
pool.setMaxActive(123);
assertEquals(123,pool.getMaxActive());
}
{
pool.setMaxIdle(12);
assertEquals(12,pool.getMaxIdle());
}
{
pool.setMaxWait(1234L);
assertEquals(1234L,pool.getMaxWait());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -