pool.returnObject("x",obj);
}
}
public void testBorrowReturnWithSometimesInvalidObjects() throws Exception {
KeyedObjectPool pool = new StackKeyedObjectPool();
pool.setFactory(
new KeyedPoolableObjectFactory() {
int counter = 0;
public Object makeObject(Object key) { return new Integer(counter++); }
public void destroyObject(Object key, Object obj) { }
public boolean validateObject(Object key, Object obj) {
if(obj instanceof Integer) {
return ((((Integer)obj).intValue() % 2) == 1);
} else {
return false;
}
}
public void activateObject(Object key, Object obj) { }
public void passivateObject(Object key, Object obj) {
if(obj instanceof Integer) {
if((((Integer)obj).intValue() % 3) == 0) {
throw new RuntimeException("Couldn't passivate");
}
} else {
throw new RuntimeException("Couldn't passivate");
}
}
}
);
Object[] obj = new Object[10];
for(int i=0;i<10;i++) {
obj[i] = pool.borrowObject("key");
}
for(int i=0;i<10;i++) {
pool.returnObject("key",obj[i]);
}
assertEquals(3,pool.getNumIdle("key"));
}