rwaldhoff 2003/03/07 07:18:21
Modified: pool/src/java/org/apache/commons/pool/impl
GenericObjectPool.java SoftReferenceObjectPool.java
StackObjectPool.java
pool/src/java/org/apache/commons/pool BaseObjectPool.java
pool/src/test/org/apache/commons/pool/impl
TestStackObjectPool.java
pool/src/test/org/apache/commons/pool TestObjectPool.java
Log:
add tests
Revision Changes Path
1.14 +13 -4
jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericObjectPool.java
Index: GenericObjectPool.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericObjectPool.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- GenericObjectPool.java 5 Mar 2003 19:22:52 -0000 1.13
+++ GenericObjectPool.java 7 Mar 2003 15:18:20 -0000 1.14
@@ -706,6 +706,7 @@
//-- ObjectPool methods ------------------------------------------
public synchronized Object borrowObject() throws Exception {
+ assertOpen();
long starttime = System.currentTimeMillis();
for(;;) {
ObjectTimestampPair pair = null;
@@ -766,12 +767,14 @@
}
public synchronized void invalidateObject(Object obj) throws Exception {
+ assertOpen();
_numActive--;
_factory.destroyObject(obj);
notifyAll(); // _numActive has changed
}
public synchronized void clear() {
+ assertOpen();
Iterator it = _pool.iterator();
while(it.hasNext()) {
try {
@@ -786,14 +789,17 @@
}
public int getNumActive() {
+ assertOpen();
return _numActive;
}
public int getNumIdle() {
+ assertOpen();
return _pool.size();
}
public void returnObject(Object obj) throws Exception {
+ assertOpen();
boolean success = true;
if(_testOnReturn && !(_factory.validateObject(obj))) {
success = false;
@@ -835,9 +841,11 @@
_evictionCursor = null;
}
startEvictor(-1L);
+ super.close();
}
synchronized public void setFactory(PoolableObjectFactory factory) throws
IllegalStateException {
+ assertOpen();
if(0 < getNumActive()) {
throw new IllegalStateException("Objects are already active");
} else {
@@ -847,6 +855,7 @@
}
public synchronized void evict() throws Exception {
+ assertOpen();
if(!_pool.isEmpty()) {
if(null == _evictionCursor) {
_evictionCursor =
(CursorableLinkedList.Cursor)(_pool.cursor(_pool.size()));
1.8 +10 -4
jakarta-commons/pool/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java
Index: SoftReferenceObjectPool.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- SoftReferenceObjectPool.java 5 Mar 2003 19:17:08 -0000 1.7
+++ SoftReferenceObjectPool.java 7 Mar 2003 15:18:20 -0000 1.8
@@ -102,6 +102,7 @@
}
public synchronized Object borrowObject() throws Exception {
+ assertOpen();
Object obj = null;
while(null == obj) {
if(_pool.isEmpty()) {
@@ -123,6 +124,7 @@
}
public void returnObject(Object obj) throws Exception {
+ assertOpen();
boolean success = true;
if(!(_factory.validateObject(obj))) {
success = false;
@@ -154,6 +156,7 @@
}
public synchronized void invalidateObject(Object obj) throws Exception {
+ assertOpen();
_numActive--;
_factory.destroyObject(obj);
notifyAll(); // _numActive has changed
@@ -169,6 +172,7 @@
}
public synchronized void clear() {
+ assertOpen();
if(null != _factory) {
Iterator iter = _pool.iterator();
while(iter.hasNext()) {
@@ -189,9 +193,11 @@
clear();
_pool = null;
_factory = null;
+ super.close();
}
synchronized public void setFactory(PoolableObjectFactory factory) throws
IllegalStateException {
+ assertOpen();
if(0 < getNumActive()) {
throw new IllegalStateException("Objects are already active");
} else {
1.9 +14 -5
jakarta-commons/pool/src/java/org/apache/commons/pool/impl/StackObjectPool.java
Index: StackObjectPool.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/impl/StackObjectPool.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- StackObjectPool.java 5 Mar 2003 19:17:08 -0000 1.8
+++ StackObjectPool.java 7 Mar 2003 15:18:20 -0000 1.9
@@ -61,6 +61,7 @@
package org.apache.commons.pool.impl;
+import java.util.EmptyStackException;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Stack;
@@ -158,10 +159,11 @@
}
public synchronized Object borrowObject() throws Exception {
+ assertOpen();
Object obj = null;
try {
obj = _pool.pop();
- } catch(Exception e) {
+ } catch(EmptyStackException e) {
if(null == _factory) {
throw new NoSuchElementException();
} else {
@@ -176,6 +178,7 @@
}
public void returnObject(Object obj) throws Exception {
+ assertOpen();
boolean success = true;
if(null != _factory) {
if(!(_factory.validateObject(obj))) {
@@ -211,6 +214,7 @@
}
public synchronized void invalidateObject(Object obj) throws Exception {
+ assertOpen();
_numActive--;
if(null != _factory ) {
_factory.destroyObject(obj);
@@ -219,14 +223,17 @@
}
public int getNumIdle() {
+ assertOpen();
return _pool.size();
}
public int getNumActive() {
+ assertOpen();
return _numActive;
}
public synchronized void clear() {
+ assertOpen();
if(null != _factory) {
Enumeration enum = _pool.elements();
while(enum.hasMoreElements()) {
@@ -244,9 +251,11 @@
clear();
_pool = null;
_factory = null;
+ super.close();
}
synchronized public void setFactory(PoolableObjectFactory factory) throws
IllegalStateException {
+ assertOpen();
if(0 < getNumActive()) {
throw new IllegalStateException("Objects are already active");
} else {
1.8 +18 -7
jakarta-commons/pool/src/java/org/apache/commons/pool/BaseObjectPool.java
Index: BaseObjectPool.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/BaseObjectPool.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- BaseObjectPool.java 25 Feb 2003 23:38:21 -0000 1.7
+++ BaseObjectPool.java 7 Mar 2003 15:18:21 -0000 1.8
@@ -95,10 +95,9 @@
throw new UnsupportedOperationException();
}
- /**
- * Does nothing this base implementation.
- */
public void close() throws Exception {
+ assertOpen();
+ closed = true;
}
/**
@@ -107,4 +106,16 @@
public void setFactory(PoolableObjectFactory factory) throws
IllegalStateException, UnsupportedOperationException {
throw new UnsupportedOperationException();
}
+
+ protected boolean isClosed() {
+ return closed;
+ }
+
+ protected void assertOpen() throws IllegalStateException {
+ if(closed) {
+ throw new IllegalStateException("Pool not open");
+ }
+ }
+
+ private boolean closed = false;
}
1.7 +99 -15
jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestStackObjectPool.java
Index: TestStackObjectPool.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestStackObjectPool.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TestStackObjectPool.java 30 Nov 2002 09:14:01 -0000 1.6
+++ TestStackObjectPool.java 7 Mar 2003 15:18:21 -0000 1.7
@@ -62,6 +62,7 @@
package org.apache.commons.pool.impl;
import java.util.BitSet;
+import java.util.NoSuchElementException;
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -84,17 +85,7 @@
}
protected ObjectPool makeEmptyPool(int mincap) {
- return new StackObjectPool(
- new PoolableObjectFactory() {
- int counter = 0;
- public Object makeObject() { return String.valueOf(counter++); }
- public void destroyObject(Object obj) { }
- public boolean validateObject(Object obj) { return true; }
- public void activateObject(Object obj) { }
- public void passivateObject(Object obj) { }
- },
- mincap
- );
+ return new StackObjectPool(new SimpleFactory());
}
protected Object getNthObject(int n) {
@@ -138,4 +129,97 @@
pool.invalidateObject(pool.borrowObject());
pool.clear();
}
+
+ public void testBorrowFromEmptyPoolWithNullFactory() throws Exception {
+ ObjectPool pool = new StackObjectPool();
+ try {
+ pool.borrowObject();
+ fail("Expected NoSuchElementException");
+ } catch(NoSuchElementException e) {
+ // expected
+ }
+ }
+
+ public void testSetFactory() throws Exception {
+ ObjectPool pool = new StackObjectPool();
+ try {
+ pool.borrowObject();
+ fail("Expected NoSuchElementException");
+ } catch(NoSuchElementException e) {
+ // expected
+ }
+ pool.setFactory(new SimpleFactory());
+ Object obj = pool.borrowObject();
+ assertNotNull(obj);
+ pool.returnObject(obj);
+ }
+
+ public void testCantResetFactoryWithActiveObjects() throws Exception {
+ ObjectPool pool = new StackObjectPool();
+ pool.setFactory(new SimpleFactory());
+ Object obj = pool.borrowObject();
+ assertNotNull(obj);
+
+ try {
+ pool.setFactory(new SimpleFactory());
+ fail("Expected IllegalStateException");
+ } catch(IllegalStateException e) {
+ // expected
+ }
+ }
+
+ public void testCanResetFactoryWithoutActiveObjects() throws Exception {
+ ObjectPool pool = new StackObjectPool();
+ {
+ pool.setFactory(new SimpleFactory());
+ Object obj = pool.borrowObject();
+ assertNotNull(obj);
+ pool.returnObject(obj);
+ }
+ {
+ pool.setFactory(new SimpleFactory());
+ Object obj = pool.borrowObject();
+ assertNotNull(obj);
+ pool.returnObject(obj);
+ }
+ }
+
+ public void testBorrowReturnWithSometimesInvalidObjects() throws Exception {
+ ObjectPool pool = new StackObjectPool(20);
+ pool.setFactory(
+ new PoolableObjectFactory() {
+ int counter = 0;
+ public Object makeObject() { return new Integer(counter++); }
+ public void destroyObject(Object obj) { }
+ public boolean validateObject(Object obj) {
+ if(obj instanceof Integer) {
+ return ((((Integer)obj).intValue() % 2) == 0);
+ } else {
+ return false;
+ }
+ }
+ public void activateObject(Object obj) { }
+ public void passivateObject(Object obj) { }
+ }
+ );
+
+ Object[] obj = new Object[10];
+ for(int i=0;i<10;i++) {
+ obj[i] = pool.borrowObject();
+ }
+ for(int i=0;i<10;i++) {
+ pool.returnObject(obj[i]);
+ }
+ assertEquals(5,pool.getNumIdle());
+ }
+
+ static class SimpleFactory implements PoolableObjectFactory {
+ int counter = 0;
+ public Object makeObject() { return String.valueOf(counter++); }
+ public void destroyObject(Object obj) { }
+ public boolean validateObject(Object obj) { return true; }
+ public void activateObject(Object obj) { }
+ public void passivateObject(Object obj) { }
+ }
}
+
1.3 +40 -4
jakarta-commons/pool/src/test/org/apache/commons/pool/TestObjectPool.java
Index: TestObjectPool.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/pool/src/test/org/apache/commons/pool/TestObjectPool.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestObjectPool.java 5 Mar 2003 19:17:08 -0000 1.2
+++ TestObjectPool.java 7 Mar 2003 15:18:21 -0000 1.3
@@ -195,6 +195,42 @@
assertEquals(0,_pool.getNumActive());
assertEquals(0,_pool.getNumIdle());
}
+
+ public void testClosePool() throws Exception {
+ try {
+ _pool = makeEmptyPool(3);
+ } catch(IllegalArgumentException e) {
+ return; // skip this test if unsupported
+ }
+ Object obj = _pool.borrowObject();
+ _pool.returnObject(obj);
+
+ _pool.close();
+ try {
+ _pool.borrowObject();
+ fail("Expected IllegalStateException");
+ } catch(IllegalStateException e) {
+ // expected
+ }
+ }
+
+ public void testCantCloseTwice() throws Exception {
+ try {
+ _pool = makeEmptyPool(3);
+ } catch(IllegalArgumentException e) {
+ return; // skip this test if unsupported
+ }
+ Object obj = _pool.borrowObject();
+ _pool.returnObject(obj);
+
+ _pool.close();
+ try {
+ _pool.close();
+ fail("Expected IllegalStateException");
+ } catch(IllegalStateException e) {
+ // expected
+ }
+ }
private ObjectPool _pool = null;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]