You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2013/12/20 12:03:36 UTC
svn commit: r1552593 - in /commons/proper/pool/trunk/src: changes/
test/java/org/apache/commons/pool2/ test/java/org/apache/commons/pool2/impl/
Author: markt
Date: Fri Dec 20 11:03:36 2013
New Revision: 1552593
URL: http://svn.apache.org/r1552593
Log:
POOL-245
Further expansion of the coverage of the unit tests.
Patch provided by Bruno P. Kinoshita.
Modified:
commons/proper/pool/trunk/src/changes/changes.xml
commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestPooledSoftReference.java
Modified: commons/proper/pool/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/changes/changes.xml?rev=1552593&r1=1552592&r2=1552593&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/changes/changes.xml (original)
+++ commons/proper/pool/trunk/src/changes/changes.xml Fri Dec 20 11:03:36 2013
@@ -74,6 +74,9 @@ The <action> type attribute can be add,u
Make the toString() method of ErodingKeyedObjectPool consistent with the
other pools.
</action>
+ <action issue="POOL-245" dev="markt" type="add" due-to="Bruno P. Kinoshita">
+ Further expansion of the coverage of the unit tests.
+ </action>
</release>
<release version="2.0" date="2013-11-11" description=
"This is a major new release that provides significant performance improvements
Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestPoolUtils.java?rev=1552593&r1=1552592&r2=1552593&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestPoolUtils.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestPoolUtils.java Fri Dec 20 11:03:36 2013
@@ -17,6 +17,7 @@
package org.apache.commons.pool2;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@@ -26,6 +27,7 @@ import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@@ -247,6 +249,14 @@ public class TestPoolUtils {
// expected
}
+ try {
+ @SuppressWarnings("unchecked")
+ final KeyedObjectPool<Object,Object> pool = createProxy(KeyedObjectPool.class, (List<String>)null);
+ PoolUtils.checkMinIdle(pool, (Collection<?>) Collections.emptyList(), 1, 1);
+ } catch (IllegalArgumentException iae) {
+ fail("PoolUtils.checkMinIdle(KeyedObjectPool,Collection,int,long) must accept empty lists.");
+ }
+
// Because this isn't deterministic and you can get false failures, try more than once.
AssertionFailedError afe = null;
int triesLeft = 3;
@@ -468,13 +478,6 @@ public class TestPoolUtils {
// expected
}
- try {
- PoolUtils.erodingPool((ObjectPool<Object>)null, 0);
- fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a non-positive factor.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
-
final List<String> calledMethods = new ArrayList<String>();
final InvocationHandler handler = new MethodCallLogger(calledMethods) {
@Override
@@ -488,6 +491,14 @@ public class TestPoolUtils {
}
};
+ try {
+ @SuppressWarnings({"unchecked", "unused"})
+ Object o = PoolUtils.erodingPool(createProxy(ObjectPool.class, handler), -1f);
+ fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a non-positive factor.");
+ } catch(IllegalArgumentException iae) {
+ // expected
+ }
+
// If the logic behind PoolUtils.erodingPool changes then this will need to be tweaked.
float factor = 0.01f; // about ~9 seconds until first discard
@SuppressWarnings("unchecked")
@@ -497,6 +508,9 @@ public class TestPoolUtils {
final List<String> expectedMethods = new ArrayList<String>();
assertEquals(expectedMethods, calledMethods);
+ pool.addObject();
+ expectedMethods.add("addObject");
+
Object o = pool.borrowObject();
expectedMethods.add("borrowObject");
@@ -506,6 +520,12 @@ public class TestPoolUtils {
expectedMethods.add("returnObject");
assertEquals(expectedMethods, calledMethods);
+ // the invocation handler always returns 1
+ assertEquals(1, pool.getNumActive());
+ expectedMethods.add("getNumActive");
+ assertEquals(1, pool.getNumIdle());
+ expectedMethods.add("getNumIdle");
+
for (int i=0; i < 5; i ++) {
o = pool.borrowObject();
expectedMethods.add("borrowObject");
@@ -529,10 +549,31 @@ public class TestPoolUtils {
pool.returnObject(o);
expectedMethods.add("getNumIdle");
expectedMethods.add("invalidateObject");
+ pool.clear();
+ pool.close();
+ expectedMethods.add("clear");
+ expectedMethods.add("close");
assertEquals(expectedMethods, calledMethods);
}
@Test
+ public void testErodingObjectPoolDefaultFactor() {
+ @SuppressWarnings("unchecked")
+ ObjectPool<Object> internalPool = createProxy(ObjectPool.class, new InvocationHandler() {
+ @Override
+ public Object invoke(Object arg0, Method arg1, Object[] arg2)
+ throws Throwable {
+ return null;
+ }
+ });
+ ObjectPool<Object> pool = PoolUtils.erodingPool(internalPool);
+ String expectedToString = "ErodingObjectPool{factor=ErodingFactor{factor=1.0, idleHighWaterMark=1}, pool=" + internalPool + "}";
+ // The factor is not exposed, but will be printed in the toString() method
+ // In this case since we didn't pass one, the default 1.0f will be printed
+ assertEquals(expectedToString, pool.toString());
+ }
+
+ @Test
public void testErodingPoolKeyedObjectPool() throws Exception {
try {
PoolUtils.erodingPool((KeyedObjectPool<Object,Object>)null);
@@ -549,26 +590,12 @@ public class TestPoolUtils {
}
try {
- PoolUtils.erodingPool((KeyedObjectPool<Object,Object>)null, 0);
- fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a non-positive factor.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
-
- try {
PoolUtils.erodingPool((KeyedObjectPool<Object,Object>)null, 1f, true);
fail("PoolUtils.erodingPool(KeyedObjectPool, float, boolean) must not allow a null pool.");
} catch(IllegalArgumentException iae) {
// expected
}
- try {
- PoolUtils.erodingPool((KeyedObjectPool<Object,Object>)null, 0, false);
- fail("PoolUtils.erodingPool(ObjectPool, float, boolean) must not allow a non-positive factor.");
- } catch(IllegalArgumentException iae) {
- // expected
- }
-
final List<String> calledMethods = new ArrayList<String>();
final InvocationHandler handler = new MethodCallLogger(calledMethods) {
@Override
@@ -582,6 +609,22 @@ public class TestPoolUtils {
}
};
+ try {
+ @SuppressWarnings({"unchecked", "unused"})
+ Object o = PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), 0f);
+ fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a non-positive factor.");
+ } catch(IllegalArgumentException iae) {
+ // expected
+ }
+
+ try {
+ @SuppressWarnings({"unchecked", "unused"})
+ Object o = PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), 0f, false);
+ fail("PoolUtils.erodingPool(ObjectPool, float, boolean) must not allow a non-positive factor.");
+ } catch(IllegalArgumentException iae) {
+ // expected
+ }
+
// If the logic behind PoolUtils.erodingPool changes then this will need to be tweaked.
float factor = 0.01f; // about ~9 seconds until first discard
@SuppressWarnings("unchecked")
@@ -593,6 +636,9 @@ public class TestPoolUtils {
final Object key = "key";
+ pool.addObject(key);
+ expectedMethods.add("addObject");
+
Object o = pool.borrowObject(key);
expectedMethods.add("borrowObject");
@@ -602,6 +648,12 @@ public class TestPoolUtils {
expectedMethods.add("returnObject");
assertEquals(expectedMethods, calledMethods);
+ // the invocation handler always returns 1
+ assertEquals(1, pool.getNumActive());
+ expectedMethods.add("getNumActive");
+ assertEquals(1, pool.getNumIdle());
+ expectedMethods.add("getNumIdle");
+
for (int i=0; i < 5; i ++) {
o = pool.borrowObject(key);
expectedMethods.add("borrowObject");
@@ -625,21 +677,42 @@ public class TestPoolUtils {
pool.returnObject(key, o);
expectedMethods.add("getNumIdle");
expectedMethods.add("invalidateObject");
+ pool.clear();
+ pool.close();
+ expectedMethods.add("clear");
+ expectedMethods.add("close");
assertEquals(expectedMethods, calledMethods);
}
@Test
+ public void testErodingPoolKeyedObjectPoolDefaultFactor() {
+ @SuppressWarnings("unchecked")
+ KeyedObjectPool<Object, Object> internalPool = createProxy(KeyedObjectPool.class, new InvocationHandler() {
+ @Override
+ public Object invoke(Object arg0, Method arg1, Object[] arg2)
+ throws Throwable {
+ return null;
+ }
+ });
+ KeyedObjectPool<Object, Object> pool = PoolUtils.erodingPool(internalPool);
+ String expectedToString = "ErodingKeyedObjectPool{factor=ErodingFactor{factor=1.0, idleHighWaterMark=1}, keyedPool=" + internalPool + "}";
+ // The factor is not exposed, but will be printed in the toString() method
+ // In this case since we didn't pass one, the default 1.0f will be printed
+ assertEquals(expectedToString, pool.toString());
+ }
+
+ @Test
public void testErodingPerKeyKeyedObjectPool() throws Exception {
try {
- PoolUtils.erodingPool((KeyedObjectPool<Object,Object>)null, 1, true);
+ PoolUtils.erodingPool((KeyedObjectPool<Object,Object>)null, 1f, true);
fail("PoolUtils.erodingPool(KeyedObjectPool) must not allow a null pool.");
} catch(IllegalArgumentException iae) {
// expected
}
try {
- PoolUtils.erodingPool((KeyedObjectPool<Object,Object>)null, 0, true);
- fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a non-positive factor.");
+ PoolUtils.erodingPool((KeyedObjectPool<Object,Object>)null, 0f, true);
+ fail("PoolUtils.erodingPool(ObjectPool, float, boolean) must not allow a non-positive factor.");
} catch(IllegalArgumentException iae) {
// expected
}
@@ -708,11 +781,21 @@ public class TestPoolUtils {
expectedMethods.add("getNumIdle");
expectedMethods.add("invalidateObject");
assertEquals(expectedMethods, calledMethods);
-
+
String expectedToString = "ErodingPerKeyKeyedObjectPool{factor="+factor+", keyedPool=null}";
assertEquals(expectedToString, pool.toString());
}
+ /**
+ * Tests the {@link PoolUtils} timer holder.
+ */
+ @Test
+ public void testTimerHolder() {
+ PoolUtils.TimerHolder h = new PoolUtils.TimerHolder();
+ assertNotNull(h);
+ assertNotNull(PoolUtils.TimerHolder.MIN_IDLE_TIMER);
+ }
+
private static List<String> invokeEveryMethod(ObjectPool<Object> op) throws Exception {
op.addObject();
op.borrowObject();
Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java?rev=1552593&r1=1552592&r2=1552593&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java Fri Dec 20 11:03:36 2013
@@ -26,6 +26,7 @@ import static org.junit.Assert.fail;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
+import java.util.List;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
@@ -35,10 +36,11 @@ import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.apache.commons.pool2.BasePooledObjectFactory;
-import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.PoolUtils;
import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.PooledObjectFactory;
+import org.apache.commons.pool2.SwallowedExceptionListener;
import org.apache.commons.pool2.TestBaseObjectPool;
import org.apache.commons.pool2.VisitTracker;
import org.apache.commons.pool2.VisitTrackerFactory;
@@ -748,6 +750,44 @@ public class TestGenericObjectPool exten
@Test(timeout=60000)
public void testSettersAndGetters() throws Exception {
{
+ // The object receives an Exception during its creation to prevent
+ // memory leaks. See BaseGenericObjectPool constructor for more details.
+ assertTrue(false == "".equals(pool.getCreationStackTrace()));
+ }
+ {
+ assertEquals(0, pool.getBorrowedCount());
+ }
+ {
+ assertEquals(0, pool.getReturnedCount());
+ }
+ {
+ assertEquals(0, pool.getCreatedCount());
+ }
+ {
+ assertEquals(0, pool.getDestroyedCount());
+ }
+ {
+ assertEquals(0, pool.getDestroyedByEvictorCount());
+ }
+ {
+ assertEquals(0, pool.getDestroyedByBorrowValidationCount());
+ }
+ {
+ assertEquals(0, pool.getMeanActiveTimeMillis());
+ }
+ {
+ assertEquals(0, pool.getMeanIdleTimeMillis());
+ }
+ {
+ assertEquals(0, pool.getMeanBorrowWaitTimeMillis());
+ }
+ {
+ assertEquals(0, pool.getMaxBorrowWaitTimeMillis());
+ }
+ {
+ assertEquals(0, pool.getNumIdle());
+ }
+ {
pool.setMaxTotal(123);
assertEquals(123,pool.getMaxTotal());
}
@@ -962,7 +1002,33 @@ public class TestGenericObjectPool exten
pool.setMinEvictableIdleTimeMillis(250L);
pool.setTimeBetweenEvictionRunsMillis(500L);
pool.setTestWhileIdle(true);
+
+ // ClassNotFoundException
+ try {
+ pool.setEvictionPolicyClassName(Long.toString(System.currentTimeMillis()));
+ fail("setEvictionPolicyClassName must throw an error if the class name is invalid.");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ // InstantiationException
+ try {
+ pool.setEvictionPolicyClassName(java.io.Serializable.class.getName());
+ fail("setEvictionPolicyClassName must throw an error if the class name is invalid.");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ // IllegalAccessException
+ try {
+ pool.setEvictionPolicyClassName(java.util.Collections.class.getName());
+ fail("setEvictionPolicyClassName must throw an error if the class name is invalid.");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
pool.setEvictionPolicyClassName(TestEvictionPolicy.class.getName());
+ assertEquals(TestEvictionPolicy.class.getName(), pool.getEvictionPolicyClassName());
String[] active = new String[500];
for(int i=0;i<500;i++) {
@@ -1933,6 +1999,18 @@ public class TestGenericObjectPool exten
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> result = mbs.queryNames(oname, null);
Assert.assertEquals(1, result.size());
+ pool.jmxUnregister();
+
+ GenericObjectPoolConfig config = new GenericObjectPoolConfig();
+ config.setJmxEnabled(false);
+ GenericObjectPool<String> poolWithoutJmx = new GenericObjectPool<String>(factory, config);
+ assertNull(poolWithoutJmx.getJmxName());
+ config.setJmxEnabled(true);
+ poolWithoutJmx.jmxUnregister();
+
+ config.setJmxNameBase(null);
+ GenericObjectPool<String> poolWithDefaultJmxNameBase = new GenericObjectPool<String>(factory, config);
+ assertNotNull(poolWithDefaultJmxNameBase.getJmxName());
}
/**
@@ -1994,6 +2072,40 @@ public class TestGenericObjectPool exten
pool.returnObject(obj);
}
+ @Test
+ public void testSwallowedExceptionListener() {
+ pool.setSwallowedExceptionListener(null); // must simply return
+ final List<Exception> swallowedExceptions = new ArrayList<Exception>();
+ /*
+ * A simple listener, that will throw a OOM on 3rd exception.
+ */
+ final SwallowedExceptionListener listener = new SwallowedExceptionListener() {
+ @Override
+ public void onSwallowException(Exception e) {
+ if (swallowedExceptions.size() == 2) {
+ throw new OutOfMemoryError();
+ } else {
+ swallowedExceptions.add(e);
+ }
+ }
+ };
+ pool.setSwallowedExceptionListener(listener);
+
+ Exception e1 = new Exception();
+ Exception e2 = new ArrayIndexOutOfBoundsException();
+
+ pool.swallowException(e1);
+ pool.swallowException(e2);
+
+ try {
+ pool.swallowException(e1);
+ fail("Not supposed to get here");
+ } catch (OutOfMemoryError oom) {
+ // expected
+ }
+
+ assertEquals(2, swallowedExceptions.size());
+ }
private static final class DummyFactory
extends BasePooledObjectFactory<Object> {
Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestPooledSoftReference.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestPooledSoftReference.java?rev=1552593&r1=1552592&r2=1552593&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestPooledSoftReference.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestPooledSoftReference.java Fri Dec 20 11:03:36 2013
@@ -56,4 +56,10 @@ public class TestPooledSoftReference {
softRef.clear();
}
+ @Test
+ public void testToString() {
+ String expected = "Referenced Object: test, State: IDLE";
+ assertEquals(expected, ref.toString());
+ }
+
}