📄 testgenericobjectpool.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.NoSuchElementException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.TestObjectPool;
/**
* @author Rodney Waldhoff
* @author Dirk Verbeeck
* @version $Revision: 1.21 $ $Date: 2004/02/28 12:21:41 $
*/
public class TestGenericObjectPool extends TestObjectPool {
public TestGenericObjectPool(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestGenericObjectPool.class);
}
protected ObjectPool makeEmptyPool(int mincap) {
GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
pool.setMaxActive(mincap);
pool.setMaxIdle(mincap);
return pool;
}
protected Object getNthObject(int n) {
return String.valueOf(n);
}
public void setUp() throws Exception {
super.setUp();
pool = new GenericObjectPool(new SimpleFactory());
}
public void tearDown() throws Exception {
super.tearDown();
pool.close();
pool = null;
}
/**
* Activation failure on existing object doesn't fail the borrow
*/
public void testActivationException() throws Exception {
SimpleFactory factory = new SimpleFactory(true, false);
factory.setThrowExceptionOnActivate(true);
factory.setValidationEnabled(false);
GenericObjectPool pool = new GenericObjectPool(factory);
Object obj1 = pool.borrowObject();
pool.returnObject(obj1);
// obj1 was returned to the pool but failed to activate the second borrow
// a new object obj2 needs te be created
Object obj2 = pool.borrowObject();
assertTrue(obj1 != obj2);
}
/**
* Activation failure on new object fails the borrow
*/
public void testActivationExceptionOnNewObject() throws Exception {
SimpleFactory factory = new SimpleFactory(true, false);
factory.setThrowExceptionOnActivate(true);
factory.setValidationEnabled(false);
GenericObjectPool pool = new GenericObjectPool(factory);
Object obj1 = pool.borrowObject();
try {
Object obj2 = pool.borrowObject();
System.out.println("obj1: " + obj1);
System.out.println("obj2: " + obj2);
fail("a second object should have been created and failed to activate");
}
catch (Exception e) {}
}
public void testWhenExhaustedGrow() throws Exception {
GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
pool.setMaxActive(1);
pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
Object obj1 = pool.borrowObject();
assertNotNull(obj1);
Object obj2 = pool.borrowObject();
assertNotNull(obj2);
pool.returnObject(obj2);
pool.returnObject(obj1);
pool.close();
}
public void testWhenExhaustedFail() throws Exception {
GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
pool.setMaxActive(1);
pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
Object obj1 = pool.borrowObject();
assertNotNull(obj1);
try {
pool.borrowObject();
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
// expected
}
pool.returnObject(obj1);
pool.close();
}
public void testWhenExhaustedBlock() throws Exception {
GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
pool.setMaxActive(1);
pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
pool.setMaxWait(10L);
Object obj1 = pool.borrowObject();
assertNotNull(obj1);
try {
pool.borrowObject();
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
// expected
}
pool.returnObject(obj1);
pool.close();
}
public void testEvictWhileEmpty() throws Exception {
GenericObjectPool pool = new GenericObjectPool(new SimpleFactory(true,false));
pool.evict();
pool.evict();
pool.close();
}
public void testExceptionOnPassivateDuringReturn() throws Exception {
SimpleFactory factory = new SimpleFactory();
GenericObjectPool pool = new GenericObjectPool(factory);
Object obj = pool.borrowObject();
factory.setThrowExceptionOnPassivate(true);
pool.returnObject(obj);
assertEquals(0,pool.getNumIdle());
pool.close();
}
public void testWithInitiallyInvalid() throws Exception {
GenericObjectPool pool = new GenericObjectPool(new SimpleFactory(false));
pool.setTestOnBorrow(true);
try {
pool.borrowObject();
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
// expected
}
}
public void testWithSometimesInvalid() throws Exception {
GenericObjectPool pool = new GenericObjectPool(new SimpleFactory(true,false));
pool.setMaxIdle(10);
pool.setTestOnBorrow(true);
pool.setTestOnReturn(true);
pool.returnObject(pool.borrowObject());
assertEquals(0,pool.getNumIdle());
}
public void testSetFactoryWithActiveObjects() throws Exception {
GenericObjectPool pool = new GenericObjectPool();
pool.setMaxIdle(10);
pool.setFactory(new SimpleFactory());
Object obj = pool.borrowObject();
assertNotNull(obj);
try {
pool.setFactory(null);
fail("Expected IllegalStateException");
} catch(IllegalStateException e) {
// expected
}
try {
pool.setFactory(new SimpleFactory());
fail("Expected IllegalStateException");
} catch(IllegalStateException e) {
// expected
}
}
public void testSetFactoryWithNoActiveObjects() throws Exception {
GenericObjectPool pool = new GenericObjectPool();
pool.setMaxIdle(10);
pool.setFactory(new SimpleFactory());
Object obj = pool.borrowObject();
pool.returnObject(obj);
assertEquals(1,pool.getNumIdle());
pool.setFactory(new SimpleFactory());
assertEquals(0,pool.getNumIdle());
}
public void testZeroMaxActive() throws Exception {
pool.setMaxActive(0);
pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
Object obj = pool.borrowObject();
assertEquals(getNthObject(0),obj);
pool.returnObject(obj);
}
public void testNegativeMaxActive() throws Exception {
pool.setMaxActive(-1);
pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
Object obj = pool.borrowObject();
assertEquals(getNthObject(0),obj);
pool.returnObject(obj);
}
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(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
pool.borrowObject();
pool.borrowObject();
pool.borrowObject();
try {
pool.borrowObject();
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
// expected
}
}
public void testInvalidWhenExhaustedAction() throws Exception {
try {
pool.setWhenExhaustedAction(Byte.MAX_VALUE);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
try {
ObjectPool pool = new GenericObjectPool(
new SimpleFactory(),
GenericObjectPool.DEFAULT_MAX_ACTIVE,
Byte.MAX_VALUE,
GenericObjectPool.DEFAULT_MAX_WAIT,
GenericObjectPool.DEFAULT_MAX_IDLE,
false,
false,
GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
false
);
assertNotNull(pool);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -