diff --git a/src/main/java/org/apache/commons/pool2/BaseKeyedPooledObjectFactory.java b/src/main/java/org/apache/commons/pool2/BaseKeyedPooledObjectFactory.java index b375ce2d0..a1762c893 100644 --- a/src/main/java/org/apache/commons/pool2/BaseKeyedPooledObjectFactory.java +++ b/src/main/java/org/apache/commons/pool2/BaseKeyedPooledObjectFactory.java @@ -29,11 +29,11 @@ * * @param The type of keys managed by this factory. * @param Type of element managed by this factory. + * @param Type of exception thrown by this factory. * * @since 2.0 */ -public abstract class BaseKeyedPooledObjectFactory extends BaseObject - implements KeyedPooledObjectFactory { +public abstract class BaseKeyedPooledObjectFactory extends BaseObject implements KeyedPooledObjectFactory { /** * Reinitialize an instance to be returned by the pool. @@ -45,8 +45,7 @@ public abstract class BaseKeyedPooledObjectFactory extends BaseObject * @param p a {@code PooledObject} wrapping the instance to be activated */ @Override - public void activateObject(final K key, final PooledObject p) - throws Exception { + public void activateObject(final K key, final PooledObject p) throws E { // The default implementation is a no-op. } @@ -56,11 +55,10 @@ public void activateObject(final K key, final PooledObject p) * @param key the key used when constructing the object * @return an instance that can be served by the pool * - * @throws Exception if there is a problem creating a new instance, + * @throws E if there is a problem creating a new instance, * this will be propagated to the code requesting an object. */ - public abstract V create(K key) - throws Exception; + public abstract V create(K key) throws E; /** * Destroy an instance no longer needed by the pool. @@ -72,13 +70,12 @@ public abstract V create(K key) * @param p a {@code PooledObject} wrapping the instance to be destroyed */ @Override - public void destroyObject(final K key, final PooledObject p) - throws Exception { + public void destroyObject(final K key, final PooledObject p) throws E { // The default implementation is a no-op. } @Override - public PooledObject makeObject(final K key) throws Exception { + public PooledObject makeObject(final K key) throws E { return wrap(create(key)); } @@ -92,8 +89,7 @@ public PooledObject makeObject(final K key) throws Exception { * @param p a {@code PooledObject} wrapping the instance to be passivated */ @Override - public void passivateObject(final K key, final PooledObject p) - throws Exception { + public void passivateObject(final K key, final PooledObject p) throws E { // The default implementation is a no-op. } diff --git a/src/main/java/org/apache/commons/pool2/BaseObjectPool.java b/src/main/java/org/apache/commons/pool2/BaseObjectPool.java index 53c7deb23..62b94d4b8 100644 --- a/src/main/java/org/apache/commons/pool2/BaseObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/BaseObjectPool.java @@ -25,10 +25,11 @@ *

* * @param Type of element pooled in this pool. + * @param Type of exception thrown by this pool. * * @since 2.0 */ -public abstract class BaseObjectPool extends BaseObject implements ObjectPool { +public abstract class BaseObjectPool extends BaseObject implements ObjectPool { private volatile boolean closed; @@ -40,7 +41,7 @@ public abstract class BaseObjectPool extends BaseObject implements ObjectPool * method */ @Override - public void addObject() throws Exception, UnsupportedOperationException { + public void addObject() throws E, UnsupportedOperationException { throw new UnsupportedOperationException(); } @@ -59,7 +60,7 @@ protected final void assertOpen() throws IllegalStateException { } @Override - public abstract T borrowObject() throws Exception; + public abstract T borrowObject() throws E; /** * Not supported in this base implementation. @@ -68,7 +69,7 @@ protected final void assertOpen() throws IllegalStateException { * method */ @Override - public void clear() throws Exception, UnsupportedOperationException { + public void clear() throws E, UnsupportedOperationException { throw new UnsupportedOperationException(); } @@ -105,7 +106,7 @@ public int getNumIdle() { } @Override - public abstract void invalidateObject(T obj) throws Exception; + public abstract void invalidateObject(T obj) throws E; /** * Has this pool instance been closed. @@ -117,7 +118,7 @@ public final boolean isClosed() { } @Override - public abstract void returnObject(T obj) throws Exception; + public abstract void returnObject(T obj) throws E; @Override protected void toStringAppendFields(final StringBuilder builder) { diff --git a/src/main/java/org/apache/commons/pool2/BasePooledObjectFactory.java b/src/main/java/org/apache/commons/pool2/BasePooledObjectFactory.java index 6302f1747..d7ae8de66 100644 --- a/src/main/java/org/apache/commons/pool2/BasePooledObjectFactory.java +++ b/src/main/java/org/apache/commons/pool2/BasePooledObjectFactory.java @@ -24,13 +24,14 @@ * This class is immutable, and therefore thread-safe * * @param Type of element managed in this factory. + * @param Type of exception thrown in this factory. * * @see PooledObjectFactory * @see BaseKeyedPooledObjectFactory * * @since 2.0 */ -public abstract class BasePooledObjectFactory extends BaseObject implements PooledObjectFactory { +public abstract class BasePooledObjectFactory extends BaseObject implements PooledObjectFactory { /** * No-op. @@ -38,7 +39,7 @@ public abstract class BasePooledObjectFactory extends BaseObject implements P * @param p ignored */ @Override - public void activateObject(final PooledObject p) throws Exception { + public void activateObject(final PooledObject p) throws E { // The default implementation is a no-op. } @@ -49,10 +50,10 @@ public void activateObject(final PooledObject p) throws Exception { * * @return an instance to be served by the pool * - * @throws Exception if there is a problem creating a new instance, + * @throws E if there is a problem creating a new instance, * this will be propagated to the code requesting an object. */ - public abstract T create() throws Exception; + public abstract T create() throws E; /** * No-op. @@ -61,12 +62,12 @@ public void activateObject(final PooledObject p) throws Exception { */ @Override public void destroyObject(final PooledObject p) - throws Exception { + throws E { // The default implementation is a no-op. } @Override - public PooledObject makeObject() throws Exception { + public PooledObject makeObject() throws E { return wrap(create()); } @@ -77,7 +78,7 @@ public PooledObject makeObject() throws Exception { */ @Override public void passivateObject(final PooledObject p) - throws Exception { + throws E { // The default implementation is a no-op. } diff --git a/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java b/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java index 7b0960c0b..88a4fbc91 100644 --- a/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java @@ -61,6 +61,7 @@ * * @param The type of keys maintained by this pool. * @param Type of element pooled in this pool. + * @param Type of exception thrown by this pool. * * @see KeyedPooledObjectFactory * @see ObjectPool @@ -68,7 +69,7 @@ * * @since 2.0 */ -public interface KeyedObjectPool extends Closeable { +public interface KeyedObjectPool extends Closeable { /** * Creates an object using the {@link KeyedPooledObjectFactory factory} or @@ -78,15 +79,14 @@ public interface KeyedObjectPool extends Closeable { * * @param key the key a new instance should be added to * - * @throws Exception + * @throws E * when {@link KeyedPooledObjectFactory#makeObject} fails. * @throws IllegalStateException * after {@link #close} has been called on this pool. * @throws UnsupportedOperationException * when this pool cannot add new idle objects. */ - void addObject(K key) throws Exception, IllegalStateException, - UnsupportedOperationException; + void addObject(K key) throws E, IllegalStateException, UnsupportedOperationException; /** * Calls {@link KeyedObjectPool#addObject(Object)} with each @@ -98,14 +98,14 @@ void addObject(K key) throws Exception, IllegalStateException, * {@link Collection} of keys to add objects for. * @param count * the number of idle objects to add for each {@code key}. - * @throws Exception + * @throws E * when {@link KeyedObjectPool#addObject(Object)} fails. * @throws IllegalArgumentException * when {@code keyedPool}, {@code keys}, or any value * in {@code keys} is {@code null}. * @see #addObjects(Object, int) */ - default void addObjects(final Collection keys, final int count) throws Exception, IllegalArgumentException { + default void addObjects(final Collection keys, final int count) throws E, IllegalArgumentException { if (keys == null) { throw new IllegalArgumentException(PoolUtils.MSG_NULL_KEYS); } @@ -122,13 +122,13 @@ default void addObjects(final Collection keys, final int count) throws Except * the key to add objects for. * @param count * the number of idle objects to add for {@code key}. - * @throws Exception + * @throws E * when {@link KeyedObjectPool#addObject(Object)} fails. * @throws IllegalArgumentException * when {@code key} is {@code null}. * @since 2.8.0 */ - default void addObjects(final K key, final int count) throws Exception, IllegalArgumentException { + default void addObjects(final K key, final int count) throws E, IllegalArgumentException { if (key == null) { throw new IllegalArgumentException(PoolUtils.MSG_NULL_KEY); } @@ -166,14 +166,14 @@ default void addObjects(final K key, final int count) throws Exception, IllegalA * * @throws IllegalStateException * after {@link #close close} has been called on this pool - * @throws Exception + * @throws E * when {@link KeyedPooledObjectFactory#makeObject * makeObject} throws an exception * @throws NoSuchElementException * when the pool is exhausted and cannot or will not return * another instance */ - V borrowObject(K key) throws Exception, NoSuchElementException, IllegalStateException; + V borrowObject(K key) throws E, NoSuchElementException, IllegalStateException; /** * Clears the pool, removing all pooled instances (optional operation). @@ -181,9 +181,9 @@ default void addObjects(final K key, final int count) throws Exception, IllegalA * @throws UnsupportedOperationException when this implementation doesn't * support the operation * - * @throws Exception if the pool cannot be cleared + * @throws E if the pool cannot be cleared */ - void clear() throws Exception, UnsupportedOperationException; + void clear() throws E, UnsupportedOperationException; /** * Clears the specified pool, removing all pooled instances corresponding to @@ -194,9 +194,9 @@ default void addObjects(final K key, final int count) throws Exception, IllegalA * @throws UnsupportedOperationException when this implementation doesn't * support the operation * - * @throws Exception if the key cannot be cleared + * @throws E if the key cannot be cleared */ - void clear(K key) throws Exception, UnsupportedOperationException; + void clear(K key) throws E, UnsupportedOperationException; /** * Closes this pool, and free any resources associated with it. @@ -277,9 +277,9 @@ default List getKeys() { * @param key the key used to obtain the object * @param obj a {@link #borrowObject borrowed} instance to be returned. * - * @throws Exception if the instance cannot be invalidated + * @throws E if the instance cannot be invalidated */ - void invalidateObject(K key, V obj) throws Exception; + void invalidateObject(K key, V obj) throws E; /** * Invalidates an object from the pool, using the provided @@ -300,10 +300,10 @@ default List getKeys() { * @param obj a {@link #borrowObject borrowed} instance to be returned. * @param destroyMode destroy activation context provided to the factory * - * @throws Exception if the instance cannot be invalidated + * @throws E if the instance cannot be invalidated * @since 2.9.0 */ - default void invalidateObject(final K key, final V obj, final DestroyMode destroyMode) throws Exception { + default void invalidateObject(final K key, final V obj, final DestroyMode destroyMode) throws E { invalidateObject(key, obj); } @@ -324,7 +324,7 @@ default void invalidateObject(final K key, final V obj, final DestroyMode destro * to return an object that was never borrowed from the pool * will trigger this exception. * - * @throws Exception if an instance cannot be returned to the pool + * @throws E if an instance cannot be returned to the pool */ - void returnObject(K key, V obj) throws Exception; + void returnObject(K key, V obj) throws E; } diff --git a/src/main/java/org/apache/commons/pool2/KeyedPooledObjectFactory.java b/src/main/java/org/apache/commons/pool2/KeyedPooledObjectFactory.java index a7f75a304..0a61497e5 100644 --- a/src/main/java/org/apache/commons/pool2/KeyedPooledObjectFactory.java +++ b/src/main/java/org/apache/commons/pool2/KeyedPooledObjectFactory.java @@ -71,10 +71,11 @@ * * @param The type of keys managed by this factory. * @param Type of element managed by this factory. + * @param Type of exception thrown by this factory. * * @since 2.0 */ -public interface KeyedPooledObjectFactory { +public interface KeyedPooledObjectFactory { /** * Reinitializes an instance to be returned by the pool. @@ -82,12 +83,12 @@ public interface KeyedPooledObjectFactory { * @param key the key used when selecting the object * @param p a {@code PooledObject} wrapping the instance to be activated * - * @throws Exception if there is a problem activating {@code obj}, + * @throws E if there is a problem activating {@code obj}, * this exception may be swallowed by the pool. * * @see #destroyObject */ - void activateObject(K key, PooledObject p) throws Exception; + void activateObject(K key, PooledObject p) throws E; /** * Destroys an instance no longer needed by the pool. @@ -104,13 +105,13 @@ public interface KeyedPooledObjectFactory { * @param key the key used when selecting the instance * @param p a {@code PooledObject} wrapping the instance to be destroyed * - * @throws Exception should be avoided as it may be swallowed by + * @throws E should be avoided as it may be swallowed by * the pool implementation. * * @see #validateObject * @see KeyedObjectPool#invalidateObject */ - void destroyObject(K key, PooledObject p) throws Exception; + void destroyObject(K key, PooledObject p) throws E; /** * Destroys an instance no longer needed by the pool, using the provided {@link DestroyMode}. @@ -119,7 +120,7 @@ public interface KeyedPooledObjectFactory { * @param p a {@code PooledObject} wrapping the instance to be destroyed * @param destroyMode DestroyMode providing context to the factory * - * @throws Exception should be avoided as it may be swallowed by + * @throws E should be avoided as it may be swallowed by * the pool implementation. * * @see #validateObject @@ -128,7 +129,7 @@ public interface KeyedPooledObjectFactory { * @see DestroyMode * @since 2.9.0 */ - default void destroyObject(final K key, final PooledObject p, final DestroyMode destroyMode) throws Exception { + default void destroyObject(final K key, final PooledObject p, final DestroyMode destroyMode) throws E { destroyObject(key, p); } @@ -141,10 +142,10 @@ default void destroyObject(final K key, final PooledObject p, final DestroyMo * @return a {@code PooledObject} wrapping an instance that can * be served by the pool. * - * @throws Exception if there is a problem creating a new instance, + * @throws E if there is a problem creating a new instance, * this will be propagated to the code requesting an object. */ - PooledObject makeObject(K key) throws Exception; + PooledObject makeObject(K key) throws E; /** * Uninitializes an instance to be returned to the idle object pool. @@ -152,12 +153,12 @@ default void destroyObject(final K key, final PooledObject p, final DestroyMo * @param key the key used when selecting the object * @param p a {@code PooledObject} wrapping the instance to be passivated * - * @throws Exception if there is a problem passivating {@code obj}, + * @throws E if there is a problem passivating {@code obj}, * this exception may be swallowed by the pool. * * @see #destroyObject */ - void passivateObject(K key, PooledObject p) throws Exception; + void passivateObject(K key, PooledObject p) throws E; /** * Ensures that the instance is safe to be returned by the pool. diff --git a/src/main/java/org/apache/commons/pool2/ObjectPool.java b/src/main/java/org/apache/commons/pool2/ObjectPool.java index 8aa2d58aa..04f8e11d4 100644 --- a/src/main/java/org/apache/commons/pool2/ObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/ObjectPool.java @@ -50,6 +50,7 @@ *

* * @param Type of element pooled in this pool. + * @param Type of exception thrown by this pool. * * @see PooledObjectFactory * @see KeyedObjectPool @@ -57,7 +58,7 @@ * * @since 2.0 */ -public interface ObjectPool extends Closeable { +public interface ObjectPool extends Closeable { /** * Creates an object using the {@link PooledObjectFactory factory} or other @@ -65,15 +66,14 @@ public interface ObjectPool extends Closeable { * the idle object pool. {@code addObject} is useful for "pre-loading" * a pool with idle objects. (Optional operation). * - * @throws Exception + * @throws E * when {@link PooledObjectFactory#makeObject} fails. * @throws IllegalStateException * after {@link #close} has been called on this pool. * @throws UnsupportedOperationException * when this pool cannot add new idle objects. */ - void addObject() throws Exception, IllegalStateException, - UnsupportedOperationException; + void addObject() throws E, IllegalStateException, UnsupportedOperationException; /** * Calls {@link ObjectPool#addObject()} {@code count} @@ -81,11 +81,10 @@ void addObject() throws Exception, IllegalStateException, * * @param count * the number of idle objects to add. - * @throws Exception - * when {@link ObjectPool#addObject()} fails. + * @throws E See {@link ObjectPool#addObject()}. * @since 2.8.0 */ - default void addObjects(final int count) throws Exception { + default void addObjects(final int count) throws E { for (int i = 0; i < count; i++) { addObject(); } @@ -115,15 +114,14 @@ default void addObjects(final int count) throws Exception { * * @throws IllegalStateException * after {@link #close close} has been called on this pool. - * @throws Exception + * @throws E * when {@link PooledObjectFactory#makeObject} throws an * exception. * @throws NoSuchElementException * when the pool is exhausted and cannot or will not return * another instance. */ - T borrowObject() throws Exception, NoSuchElementException, - IllegalStateException; + T borrowObject() throws E, NoSuchElementException, IllegalStateException; /** * Clears any objects sitting idle in the pool, releasing any associated @@ -133,9 +131,9 @@ T borrowObject() throws Exception, NoSuchElementException, * @throws UnsupportedOperationException * if this implementation does not support the operation * - * @throws Exception if the pool cannot be cleared + * @throws E if the pool cannot be cleared */ - void clear() throws Exception, UnsupportedOperationException; + void clear() throws E, UnsupportedOperationException; /** * Closes this pool, and free any resources associated with it. @@ -180,9 +178,9 @@ T borrowObject() throws Exception, NoSuchElementException, * * @param obj a {@link #borrowObject borrowed} instance to be disposed. * - * @throws Exception if the instance cannot be invalidated + * @throws E if the instance cannot be invalidated */ - void invalidateObject(T obj) throws Exception; + void invalidateObject(T obj) throws E; /** * Invalidates an object from the pool, using the provided @@ -199,11 +197,10 @@ T borrowObject() throws Exception, NoSuchElementException, * * @param obj a {@link #borrowObject borrowed} instance to be disposed. * @param destroyMode destroy activation context provided to the factory - * - * @throws Exception if the instance cannot be invalidated + * @throws E if the instance cannot be invalidated * @since 2.9.0 */ - default void invalidateObject(final T obj, final DestroyMode destroyMode) throws Exception { + default void invalidateObject(final T obj, final DestroyMode destroyMode) throws E { invalidateObject(obj); } @@ -221,8 +218,8 @@ default void invalidateObject(final T obj, final DestroyMode destroyMode) throws * to return an object that was never borrowed from the pool * will trigger this exception. * - * @throws Exception if an instance cannot be returned to the pool + * @throws E if an instance cannot be returned to the pool */ - void returnObject(T obj) throws Exception; + void returnObject(T obj) throws E; } diff --git a/src/main/java/org/apache/commons/pool2/PoolUtils.java b/src/main/java/org/apache/commons/pool2/PoolUtils.java index 023b040c7..7d64d9ae4 100644 --- a/src/main/java/org/apache/commons/pool2/PoolUtils.java +++ b/src/main/java/org/apache/commons/pool2/PoolUtils.java @@ -110,12 +110,12 @@ public void update(final long nowMillis, final int numIdle) { * * @param object pool key type * @param object pool value type + * @param exception thrown by this pool */ - private static class ErodingKeyedObjectPool implements - KeyedObjectPool { + private static class ErodingKeyedObjectPool implements KeyedObjectPool { /** Underlying pool */ - private final KeyedObjectPool keyedPool; + private final KeyedObjectPool keyedPool; /** Erosion factor */ private final ErodingFactor erodingFactor; @@ -131,7 +131,7 @@ private static class ErodingKeyedObjectPool implements * events * @see #erodingFactor */ - protected ErodingKeyedObjectPool(final KeyedObjectPool keyedPool, + protected ErodingKeyedObjectPool(final KeyedObjectPool keyedPool, final ErodingFactor erodingFactor) { if (keyedPool == null) { throw new IllegalArgumentException( @@ -152,7 +152,7 @@ protected ErodingKeyedObjectPool(final KeyedObjectPool keyedPool, * events * @see #erodingFactor */ - public ErodingKeyedObjectPool(final KeyedObjectPool keyedPool, + public ErodingKeyedObjectPool(final KeyedObjectPool keyedPool, final float factor) { this(keyedPool, new ErodingFactor(factor)); } @@ -161,8 +161,7 @@ public ErodingKeyedObjectPool(final KeyedObjectPool keyedPool, * {@inheritDoc} */ @Override - public void addObject(final K key) throws Exception, - IllegalStateException, UnsupportedOperationException { + public void addObject(final K key) throws E, IllegalStateException, UnsupportedOperationException { keyedPool.addObject(key); } @@ -170,8 +169,7 @@ public void addObject(final K key) throws Exception, * {@inheritDoc} */ @Override - public V borrowObject(final K key) throws Exception, - NoSuchElementException, IllegalStateException { + public V borrowObject(final K key) throws E, NoSuchElementException, IllegalStateException { return keyedPool.borrowObject(key); } @@ -179,7 +177,7 @@ public V borrowObject(final K key) throws Exception, * {@inheritDoc} */ @Override - public void clear() throws Exception, UnsupportedOperationException { + public void clear() throws E, UnsupportedOperationException { keyedPool.clear(); } @@ -187,8 +185,7 @@ public void clear() throws Exception, UnsupportedOperationException { * {@inheritDoc} */ @Override - public void clear(final K key) throws Exception, - UnsupportedOperationException { + public void clear(final K key) throws E, UnsupportedOperationException { keyedPool.clear(key); } @@ -220,7 +217,7 @@ protected ErodingFactor getErodingFactor(final K key) { * * @return the keyed pool that this ErodingKeyedObjectPool wraps */ - protected KeyedObjectPool getKeyedPool() { + protected KeyedObjectPool getKeyedPool() { return keyedPool; } @@ -290,7 +287,7 @@ public void invalidateObject(final K key, final V obj) { * @see #erodingFactor */ @Override - public void returnObject(final K key, final V obj) throws Exception { + public void returnObject(final K key, final V obj) throws E { boolean discard = false; final long nowMillis = System.currentTimeMillis(); final ErodingFactor factor = getErodingFactor(key); @@ -324,17 +321,19 @@ public String toString() { erodingFactor + ", keyedPool=" + keyedPool + '}'; } } + /** * Decorates an object pool, adding "eroding" behavior. Based on the * configured {@link #factor erosion factor}, objects returning to the pool * may be invalidated instead of being added to idle capacity. * * @param type of objects in the pool + * @param type of exceptions from the pool */ - private static class ErodingObjectPool implements ObjectPool { + private static class ErodingObjectPool implements ObjectPool { /** Underlying object pool */ - private final ObjectPool pool; + private final ObjectPool pool; /** Erosion factor */ private final ErodingFactor factor; @@ -350,7 +349,7 @@ private static class ErodingObjectPool implements ObjectPool { * events * @see #factor */ - public ErodingObjectPool(final ObjectPool pool, final float factor) { + public ErodingObjectPool(final ObjectPool pool, final float factor) { this.pool = pool; this.factor = new ErodingFactor(factor); } @@ -359,8 +358,7 @@ public ErodingObjectPool(final ObjectPool pool, final float factor) { * {@inheritDoc} */ @Override - public void addObject() throws Exception, IllegalStateException, - UnsupportedOperationException { + public void addObject() throws E, IllegalStateException, UnsupportedOperationException{ pool.addObject(); } @@ -368,8 +366,7 @@ public void addObject() throws Exception, IllegalStateException, * {@inheritDoc} */ @Override - public T borrowObject() throws Exception, NoSuchElementException, - IllegalStateException { + public T borrowObject() throws E, NoSuchElementException, IllegalStateException { return pool.borrowObject(); } @@ -377,7 +374,7 @@ public T borrowObject() throws Exception, NoSuchElementException, * {@inheritDoc} */ @Override - public void clear() throws Exception, UnsupportedOperationException { + public void clear() throws E, UnsupportedOperationException { pool.clear(); } @@ -474,9 +471,9 @@ public String toString() { * * @param object pool key type * @param object pool value type + * @param exception thrown by this pool */ - private static final class ErodingPerKeyKeyedObjectPool extends - ErodingKeyedObjectPool { + private static final class ErodingPerKeyKeyedObjectPool extends ErodingKeyedObjectPool { /** Erosion factor - same for all pools */ private final float factor; @@ -493,8 +490,7 @@ private static final class ErodingPerKeyKeyedObjectPool extends * @param factor * erosion factor */ - public ErodingPerKeyKeyedObjectPool( - final KeyedObjectPool keyedPool, final float factor) { + public ErodingPerKeyKeyedObjectPool(final KeyedObjectPool keyedPool, final float factor) { super(keyedPool, null); this.factor = factor; } @@ -531,9 +527,9 @@ public String toString() { * * @param object pool key type * @param object pool value type + * @param exception thrown by this pool */ - private static final class KeyedObjectPoolMinIdleTimerTask extends - TimerTask { + private static final class KeyedObjectPoolMinIdleTimerTask extends TimerTask { /** Minimum number of idle instances. Not the same as pool.getMinIdle(). */ private final int minIdle; @@ -542,7 +538,7 @@ private static final class KeyedObjectPoolMinIdleTimerTask extends private final K key; /** Keyed object pool */ - private final KeyedObjectPool keyedPool; + private final KeyedObjectPool keyedPool; /** * Creates a new KeyedObjecPoolMinIdleTimerTask. @@ -556,7 +552,7 @@ private static final class KeyedObjectPoolMinIdleTimerTask extends * @throws IllegalArgumentException * if the key is null */ - KeyedObjectPoolMinIdleTimerTask(final KeyedObjectPool keyedPool, + KeyedObjectPoolMinIdleTimerTask(final KeyedObjectPool keyedPool, final K key, final int minIdle) throws IllegalArgumentException { if (keyedPool == null) { throw new IllegalArgumentException( @@ -610,14 +606,15 @@ public String toString() { * as the pool's minIdle setting. * * @param type of objects in the pool + * @param type of exceptions from the pool */ - private static final class ObjectPoolMinIdleTimerTask extends TimerTask { + private static final class ObjectPoolMinIdleTimerTask extends TimerTask { /** Minimum number of idle instances. Not the same as pool.getMinIdle(). */ private final int minIdle; /** Object pool */ - private final ObjectPool pool; + private final ObjectPool pool; /** * Constructs a new ObjectPoolMinIdleTimerTask for the given pool with the @@ -630,7 +627,7 @@ private static final class ObjectPoolMinIdleTimerTask extends TimerTask { * @throws IllegalArgumentException * if the pool is null */ - ObjectPoolMinIdleTimerTask(final ObjectPool pool, final int minIdle) + ObjectPoolMinIdleTimerTask(final ObjectPool pool, final int minIdle) throws IllegalArgumentException { if (pool == null) { throw new IllegalArgumentException(MSG_NULL_POOL); @@ -689,9 +686,9 @@ public String toString() { * * @param object pool key type * @param object pool value type + * @param exception thrown by this pool */ - static final class SynchronizedKeyedObjectPool implements - KeyedObjectPool { + static final class SynchronizedKeyedObjectPool implements KeyedObjectPool { /** * Object whose monitor is used to synchronize methods on the wrapped @@ -700,7 +697,7 @@ static final class SynchronizedKeyedObjectPool implements private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); /** Underlying object pool */ - private final KeyedObjectPool keyedPool; + private final KeyedObjectPool keyedPool; /** * Creates a new SynchronizedKeyedObjectPool wrapping the given pool @@ -710,7 +707,7 @@ static final class SynchronizedKeyedObjectPool implements * @throws IllegalArgumentException * if keyedPool is null */ - SynchronizedKeyedObjectPool(final KeyedObjectPool keyedPool) + SynchronizedKeyedObjectPool(final KeyedObjectPool keyedPool) throws IllegalArgumentException { if (keyedPool == null) { throw new IllegalArgumentException( @@ -723,8 +720,7 @@ static final class SynchronizedKeyedObjectPool implements * {@inheritDoc} */ @Override - public void addObject(final K key) throws Exception, - IllegalStateException, UnsupportedOperationException { + public void addObject(final K key) throws E, IllegalStateException, UnsupportedOperationException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { @@ -738,8 +734,7 @@ public void addObject(final K key) throws Exception, * {@inheritDoc} */ @Override - public V borrowObject(final K key) throws Exception, - NoSuchElementException, IllegalStateException { + public V borrowObject(final K key) throws E, NoSuchElementException, IllegalStateException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { @@ -753,7 +748,7 @@ public V borrowObject(final K key) throws Exception, * {@inheritDoc} */ @Override - public void clear() throws Exception, UnsupportedOperationException { + public void clear() throws E, UnsupportedOperationException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { @@ -767,8 +762,7 @@ public void clear() throws Exception, UnsupportedOperationException { * {@inheritDoc} */ @Override - public void clear(final K key) throws Exception, - UnsupportedOperationException { + public void clear(final K key) throws E, UnsupportedOperationException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { @@ -920,16 +914,16 @@ public String toString() { *

* * @param pooled object factory key type - * @param pooled object factory key value + * @param pooled object factory value type + * @param pooled object factory exception type */ - private static final class SynchronizedKeyedPooledObjectFactory - implements KeyedPooledObjectFactory { + private static final class SynchronizedKeyedPooledObjectFactory implements KeyedPooledObjectFactory { /** Synchronization lock */ private final WriteLock writeLock = new ReentrantReadWriteLock().writeLock(); /** Wrapped factory */ - private final KeyedPooledObjectFactory keyedFactory; + private final KeyedPooledObjectFactory keyedFactory; /** * Creates a SynchronizedKeyedPoolableObjectFactory wrapping the given @@ -940,9 +934,7 @@ private static final class SynchronizedKeyedPooledObjectFactory * @throws IllegalArgumentException * if the factory is null */ - SynchronizedKeyedPooledObjectFactory( - final KeyedPooledObjectFactory keyedFactory) - throws IllegalArgumentException { + SynchronizedKeyedPooledObjectFactory(final KeyedPooledObjectFactory keyedFactory) throws IllegalArgumentException { if (keyedFactory == null) { throw new IllegalArgumentException( "keyedFactory must not be null."); @@ -954,7 +946,7 @@ private static final class SynchronizedKeyedPooledObjectFactory * {@inheritDoc} */ @Override - public void activateObject(final K key, final PooledObject p) throws Exception { + public void activateObject(final K key, final PooledObject p) throws E { writeLock.lock(); try { keyedFactory.activateObject(key, p); @@ -967,7 +959,7 @@ public void activateObject(final K key, final PooledObject p) throws Exceptio * {@inheritDoc} */ @Override - public void destroyObject(final K key, final PooledObject p) throws Exception { + public void destroyObject(final K key, final PooledObject p) throws E { writeLock.lock(); try { keyedFactory.destroyObject(key, p); @@ -980,7 +972,7 @@ public void destroyObject(final K key, final PooledObject p) throws Exception * {@inheritDoc} */ @Override - public PooledObject makeObject(final K key) throws Exception { + public PooledObject makeObject(final K key) throws E { writeLock.lock(); try { return keyedFactory.makeObject(key); @@ -993,7 +985,7 @@ public PooledObject makeObject(final K key) throws Exception { * {@inheritDoc} */ @Override - public void passivateObject(final K key, final PooledObject p) throws Exception { + public void passivateObject(final K key, final PooledObject p) throws E { writeLock.lock(); try { keyedFactory.passivateObject(key, p); @@ -1041,8 +1033,9 @@ public boolean validateObject(final K key, final PooledObject p) { *

* * @param type of objects in the pool + * @param type of exceptions from the pool */ - private static final class SynchronizedObjectPool implements ObjectPool { + private static final class SynchronizedObjectPool implements ObjectPool { /** * Object whose monitor is used to synchronize methods on the wrapped @@ -1051,7 +1044,7 @@ private static final class SynchronizedObjectPool implements ObjectPool { private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); /** the underlying object pool */ - private final ObjectPool pool; + private final ObjectPool pool; /** * Creates a new SynchronizedObjectPool wrapping the given pool. @@ -1062,7 +1055,7 @@ private static final class SynchronizedObjectPool implements ObjectPool { * @throws IllegalArgumentException * if the pool is null */ - SynchronizedObjectPool(final ObjectPool pool) + SynchronizedObjectPool(final ObjectPool pool) throws IllegalArgumentException { if (pool == null) { throw new IllegalArgumentException(MSG_NULL_POOL); @@ -1074,8 +1067,7 @@ private static final class SynchronizedObjectPool implements ObjectPool { * {@inheritDoc} */ @Override - public void addObject() throws Exception, IllegalStateException, - UnsupportedOperationException { + public void addObject() throws E, IllegalStateException, UnsupportedOperationException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { @@ -1089,8 +1081,7 @@ public void addObject() throws Exception, IllegalStateException, * {@inheritDoc} */ @Override - public T borrowObject() throws Exception, NoSuchElementException, - IllegalStateException { + public T borrowObject() throws E, NoSuchElementException, IllegalStateException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { @@ -1104,7 +1095,7 @@ public T borrowObject() throws Exception, NoSuchElementException, * {@inheritDoc} */ @Override - public void clear() throws Exception, UnsupportedOperationException { + public void clear() throws E, UnsupportedOperationException { final WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { @@ -1214,15 +1205,16 @@ public String toString() { *

* * @param pooled object factory type + * @param exception type */ - private static final class SynchronizedPooledObjectFactory implements - PooledObjectFactory { + private static final class SynchronizedPooledObjectFactory implements + PooledObjectFactory { /** Synchronization lock */ private final WriteLock writeLock = new ReentrantReadWriteLock().writeLock(); /** Wrapped factory */ - private final PooledObjectFactory factory; + private final PooledObjectFactory factory; /** * Creates a SynchronizedPoolableObjectFactory wrapping the given @@ -1233,7 +1225,7 @@ private static final class SynchronizedPooledObjectFactory implements * @throws IllegalArgumentException * if the factory is null */ - SynchronizedPooledObjectFactory(final PooledObjectFactory factory) + SynchronizedPooledObjectFactory(final PooledObjectFactory factory) throws IllegalArgumentException { if (factory == null) { throw new IllegalArgumentException("factory must not be null."); @@ -1245,7 +1237,7 @@ private static final class SynchronizedPooledObjectFactory implements * {@inheritDoc} */ @Override - public void activateObject(final PooledObject p) throws Exception { + public void activateObject(final PooledObject p) throws E { writeLock.lock(); try { factory.activateObject(p); @@ -1258,7 +1250,7 @@ public void activateObject(final PooledObject p) throws Exception { * {@inheritDoc} */ @Override - public void destroyObject(final PooledObject p) throws Exception { + public void destroyObject(final PooledObject p) throws E { writeLock.lock(); try { factory.destroyObject(p); @@ -1271,7 +1263,7 @@ public void destroyObject(final PooledObject p) throws Exception { * {@inheritDoc} */ @Override - public PooledObject makeObject() throws Exception { + public PooledObject makeObject() throws E { writeLock.lock(); try { return factory.makeObject(); @@ -1284,7 +1276,7 @@ public PooledObject makeObject() throws Exception { * {@inheritDoc} */ @Override - public void passivateObject(final PooledObject p) throws Exception { + public void passivateObject(final PooledObject p) throws E { writeLock.lock(); try { factory.passivateObject(p); @@ -1356,6 +1348,7 @@ static class TimerHolder { * keyedPool, see {@link Timer#schedule(TimerTask, long, long)}. * @param the type of the pool key * @param the type of pool entries + * @param the type of exception thrown by a pool * @return a {@link Map} of key and {@link TimerTask} pairs that will * periodically check the pools idle object count. * @throws IllegalArgumentException @@ -1365,8 +1358,8 @@ static class TimerHolder { * {@link Timer#schedule(TimerTask, long, long)}. * @see #checkMinIdle(KeyedObjectPool, Object, int, long) */ - public static Map checkMinIdle( - final KeyedObjectPool keyedPool, final Collection keys, + public static Map checkMinIdle( + final KeyedObjectPool keyedPool, final Collection keys, final int minIdle, final long periodMillis) throws IllegalArgumentException { if (keys == null) { @@ -1398,6 +1391,7 @@ public static Map checkMinIdle( * keyedPool, see {@link Timer#schedule(TimerTask, long, long)}. * @param the type of the pool key * @param the type of pool entries + * @param the type of exception thrown by a pool * @return the {@link TimerTask} that will periodically check the pools idle * object count. * @throws IllegalArgumentException @@ -1405,8 +1399,8 @@ public static Map checkMinIdle( * when {@code minIdle} is negative or when {@code period} isn't * valid for {@link Timer#schedule(TimerTask, long, long)}. */ - public static TimerTask checkMinIdle( - final KeyedObjectPool keyedPool, final K key, + public static TimerTask checkMinIdle( + final KeyedObjectPool keyedPool, final K key, final int minIdle, final long periodMillis) throws IllegalArgumentException { if (keyedPool == null) { @@ -1438,6 +1432,7 @@ public static TimerTask checkMinIdle( * the frequency in milliseconds to check the number of idle objects in a pool, * see {@link Timer#schedule(TimerTask, long, long)}. * @param the type of objects in the pool + * @param type of exceptions from the pool * @return the {@link TimerTask} that will periodically check the pools idle * object count. * @throws IllegalArgumentException @@ -1445,7 +1440,7 @@ public static TimerTask checkMinIdle( * negative or when {@code period} isn't valid for * {@link Timer#schedule(TimerTask, long, long)} */ - public static TimerTask checkMinIdle(final ObjectPool pool, + public static TimerTask checkMinIdle(final ObjectPool pool, final int minIdle, final long periodMillis) throws IllegalArgumentException { if (pool == null) { @@ -1494,6 +1489,7 @@ public static void checkRethrow(final Throwable t) { * count when possible. * @param the type of the pool key * @param the type of pool entries + * @param the type of exception thrown by a pool * @throws IllegalArgumentException * when {@code keyedPool} is {@code null}. * @return a pool that adaptively decreases its size when idle objects are @@ -1501,8 +1497,7 @@ public static void checkRethrow(final Throwable t) { * @see #erodingPool(KeyedObjectPool, float) * @see #erodingPool(KeyedObjectPool, float, boolean) */ - public static KeyedObjectPool erodingPool( - final KeyedObjectPool keyedPool) { + public static KeyedObjectPool erodingPool(final KeyedObjectPool keyedPool) { return erodingPool(keyedPool, 1f); } @@ -1529,6 +1524,7 @@ public static KeyedObjectPool erodingPool( * shrinks less aggressively. * @param the type of the pool key * @param the type of pool entries + * @param the type of exception thrown by a pool * @throws IllegalArgumentException * when {@code keyedPool} is {@code null} or when {@code factor} * is not positive. @@ -1536,8 +1532,7 @@ public static KeyedObjectPool erodingPool( * no longer needed. * @see #erodingPool(KeyedObjectPool, float, boolean) */ - public static KeyedObjectPool erodingPool( - final KeyedObjectPool keyedPool, final float factor) { + public static KeyedObjectPool erodingPool(final KeyedObjectPool keyedPool, final float factor) { return erodingPool(keyedPool, factor, false); } @@ -1572,6 +1567,7 @@ public static KeyedObjectPool erodingPool( * when true, each key is treated independently. * @param the type of the pool key * @param the type of pool entries + * @param the type of exception thrown by a pool * @throws IllegalArgumentException * when {@code keyedPool} is {@code null} or when {@code factor} * is not positive. @@ -1580,8 +1576,8 @@ public static KeyedObjectPool erodingPool( * @see #erodingPool(KeyedObjectPool) * @see #erodingPool(KeyedObjectPool, float) */ - public static KeyedObjectPool erodingPool( - final KeyedObjectPool keyedPool, final float factor, + public static KeyedObjectPool erodingPool( + final KeyedObjectPool keyedPool, final float factor, final boolean perKey) { if (keyedPool == null) { throw new IllegalArgumentException(MSG_NULL_KEYED_POOL); @@ -1606,13 +1602,14 @@ public static KeyedObjectPool erodingPool( * the ObjectPool to be decorated so it shrinks its idle count * when possible. * @param the type of objects in the pool + * @param type of exceptions from the pool * @throws IllegalArgumentException * when {@code pool} is {@code null}. * @return a pool that adaptively decreases its size when idle objects are * no longer needed. * @see #erodingPool(ObjectPool, float) */ - public static ObjectPool erodingPool(final ObjectPool pool) { + public static ObjectPool erodingPool(final ObjectPool pool) { return erodingPool(pool, 1f); } @@ -1638,6 +1635,7 @@ public static ObjectPool erodingPool(final ObjectPool pool) { * shrinks more aggressively. If 1 < factor then the pool * shrinks less aggressively. * @param the type of objects in the pool + * @param type of exceptions from the pool * @throws IllegalArgumentException * when {@code pool} is {@code null} or when {@code factor} is * not positive. @@ -1645,8 +1643,7 @@ public static ObjectPool erodingPool(final ObjectPool pool) { * no longer needed. * @see #erodingPool(ObjectPool) */ - public static ObjectPool erodingPool(final ObjectPool pool, - final float factor) { + public static ObjectPool erodingPool(final ObjectPool pool, final float factor) { if (pool == null) { throw new IllegalArgumentException(MSG_NULL_POOL); } @@ -1679,7 +1676,8 @@ private static Timer getMinIdleTimer() { * the number of idle objects to add for each {@code key}. * @param the type of the pool key * @param the type of pool entries - * @throws Exception + * @param the type of exception thrown by a pool + * @throws E * when {@link KeyedObjectPool#addObject(Object)} fails. * @throws IllegalArgumentException * when {@code keyedPool}, {@code keys}, or any value in @@ -1688,8 +1686,8 @@ private static Timer getMinIdleTimer() { * @deprecated Use {@link KeyedObjectPool#addObjects(Collection, int)}. */ @Deprecated - public static void prefill(final KeyedObjectPool keyedPool, - final Collection keys, final int count) throws Exception, + public static void prefill(final KeyedObjectPool keyedPool, + final Collection keys, final int count) throws E, IllegalArgumentException { if (keys == null) { throw new IllegalArgumentException(MSG_NULL_KEYS); @@ -1709,15 +1707,16 @@ public static void prefill(final KeyedObjectPool keyedPool, * the number of idle objects to add for {@code key}. * @param the type of the pool key * @param the type of pool entries - * @throws Exception + * @param the type of exception thrown by a pool + * @throws E * when {@link KeyedObjectPool#addObject(Object)} fails. * @throws IllegalArgumentException * when {@code keyedPool} or {@code key} is {@code null}. * @deprecated Use {@link KeyedObjectPool#addObjects(Object, int)}. */ @Deprecated - public static void prefill(final KeyedObjectPool keyedPool, - final K key, final int count) throws Exception, + public static void prefill(final KeyedObjectPool keyedPool, + final K key, final int count) throws E, IllegalArgumentException { if (keyedPool == null) { throw new IllegalArgumentException(MSG_NULL_KEYED_POOL); @@ -1734,15 +1733,16 @@ public static void prefill(final KeyedObjectPool keyedPool, * @param count * the number of idle objects to add. * @param the type of objects in the pool - * @throws Exception + * @param type of exceptions from the pool + * @throws E * when {@link ObjectPool#addObject()} fails. * @throws IllegalArgumentException * when {@code pool} is {@code null}. * @deprecated Use {@link ObjectPool#addObjects(int)}. */ @Deprecated - public static void prefill(final ObjectPool pool, final int count) - throws Exception, IllegalArgumentException { + public static void prefill(final ObjectPool pool, final int count) + throws E, IllegalArgumentException { if (pool == null) { throw new IllegalArgumentException(MSG_NULL_POOL); } @@ -1758,10 +1758,11 @@ public static void prefill(final ObjectPool pool, final int count) * synchronized KeyedPooledObjectFactory. * @param the type of the pool key * @param the type of pool entries + * @param the type of pool exceptions * @return a synchronized view of the specified KeyedPooledObjectFactory. */ - public static KeyedPooledObjectFactory synchronizedKeyedPooledFactory( - final KeyedPooledObjectFactory keyedFactory) { + public static KeyedPooledObjectFactory synchronizedKeyedPooledFactory( + final KeyedPooledObjectFactory keyedFactory) { return new SynchronizedKeyedPooledObjectFactory<>(keyedFactory); } @@ -1782,10 +1783,10 @@ public static KeyedPooledObjectFactory synchronizedKeyedPooledFacto * KeyedObjectPool. * @param the type of the pool key * @param the type of pool entries + * @param the type of exception thrown by a pool * @return a synchronized view of the specified KeyedObjectPool. */ - public static KeyedObjectPool synchronizedPool( - final KeyedObjectPool keyedPool) { + public static KeyedObjectPool synchronizedPool(final KeyedObjectPool keyedPool) { /* * assert !(keyedPool instanceof GenericKeyedObjectPool) : * "GenericKeyedObjectPool is already thread-safe"; assert !(keyedPool @@ -1810,14 +1811,15 @@ public static KeyedObjectPool synchronizedPool( * deadlock. *

* + * @param the type of objects in the pool + * @param the type of exceptions thrown by the pool * @param pool * the ObjectPool to be "wrapped" in a synchronized ObjectPool. - * @param the type of objects in the pool * @throws IllegalArgumentException * when {@code pool} is {@code null}. * @return a synchronized view of the specified ObjectPool. */ - public static ObjectPool synchronizedPool(final ObjectPool pool) { + public static ObjectPool synchronizedPool(final ObjectPool pool) { if (pool == null) { throw new IllegalArgumentException(MSG_NULL_POOL); } @@ -1844,10 +1846,10 @@ public static ObjectPool synchronizedPool(final ObjectPool pool) { * the PooledObjectFactory to be "wrapped" in a synchronized * PooledObjectFactory. * @param the type of objects in the pool + * @param the type of exceptions thrown by the pool * @return a synchronized view of the specified PooledObjectFactory. */ - public static PooledObjectFactory synchronizedPooledFactory( - final PooledObjectFactory factory) { + public static PooledObjectFactory synchronizedPooledFactory(final PooledObjectFactory factory) { return new SynchronizedPooledObjectFactory<>(factory); } diff --git a/src/main/java/org/apache/commons/pool2/PooledObjectFactory.java b/src/main/java/org/apache/commons/pool2/PooledObjectFactory.java index 839e86676..a302a2889 100644 --- a/src/main/java/org/apache/commons/pool2/PooledObjectFactory.java +++ b/src/main/java/org/apache/commons/pool2/PooledObjectFactory.java @@ -65,24 +65,25 @@ *

* * @param Type of element managed in this factory. + * @param Type of exception thrown in this factory. * * @see ObjectPool * * @since 2.0 */ -public interface PooledObjectFactory { +public interface PooledObjectFactory { /** * Reinitializes an instance to be returned by the pool. * * @param p a {@code PooledObject} wrapping the instance to be activated * - * @throws Exception if there is a problem activating {@code obj}, + * @throws E if there is a problem activating {@code obj}, * this exception may be swallowed by the pool. * * @see #destroyObject */ - void activateObject(PooledObject p) throws Exception; + void activateObject(PooledObject p) throws E; /** * Destroys an instance no longer needed by the pool, using the default (NORMAL) @@ -99,13 +100,13 @@ public interface PooledObjectFactory { * * @param p a {@code PooledObject} wrapping the instance to be destroyed * - * @throws Exception should be avoided as it may be swallowed by + * @throws E should be avoided as it may be swallowed by * the pool implementation. * * @see #validateObject * @see ObjectPool#invalidateObject */ - void destroyObject(PooledObject p) throws Exception; + void destroyObject(PooledObject p) throws E; /** * Destroys an instance no longer needed by the pool, using the provided @@ -114,7 +115,7 @@ public interface PooledObjectFactory { * @param p a {@code PooledObject} wrapping the instance to be destroyed * @param destroyMode DestroyMode providing context to the factory * - * @throws Exception should be avoided as it may be swallowed by + * @throws E should be avoided as it may be swallowed by * the pool implementation. * * @see #validateObject @@ -123,7 +124,7 @@ public interface PooledObjectFactory { * @see DestroyMode * @since 2.9.0 */ - default void destroyObject(final PooledObject p, final DestroyMode destroyMode) throws Exception { + default void destroyObject(final PooledObject p, final DestroyMode destroyMode) throws E { destroyObject(p); } @@ -133,22 +134,22 @@ default void destroyObject(final PooledObject p, final DestroyMode destroyMod * * @return a {@code PooledObject} wrapping an instance that can be served by the pool * - * @throws Exception if there is a problem creating a new instance, + * @throws E if there is a problem creating a new instance, * this will be propagated to the code requesting an object. */ - PooledObject makeObject() throws Exception; + PooledObject makeObject() throws E; /** * Uninitializes an instance to be returned to the idle object pool. * * @param p a {@code PooledObject} wrapping the instance to be passivated * - * @throws Exception if there is a problem passivating {@code obj}, + * @throws E if there is a problem passivating {@code obj}, * this exception may be swallowed by the pool. * * @see #destroyObject */ - void passivateObject(PooledObject p) throws Exception; + void passivateObject(PooledObject p) throws E; /** * Ensures that the instance is safe to be returned by the pool. diff --git a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java index 77a414861..d23b03001 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java @@ -55,12 +55,13 @@ * reduce code duplication between the two pool implementations. * * @param Type of element pooled in this pool. + * @param Type of exception thrown in this pool. * * This class is intended to be thread-safe. * * @since 2.0 */ -public abstract class BaseGenericObjectPool extends BaseObject { +public abstract class BaseGenericObjectPool extends BaseObject { /** * The idle object eviction iterator. Holds a reference to the idle objects. @@ -128,7 +129,7 @@ void cancel() { scheduledFuture.cancel(false); } - BaseGenericObjectPool owner() { + BaseGenericObjectPool owner() { return BaseGenericObjectPool.this; } @@ -443,6 +444,18 @@ final void assertOpen() throws IllegalStateException { } } + /** + * Casts the given throwable to E. + * + * @param throwable the throwable. + * @return the input. + * @since 2.12.0 + */ + @SuppressWarnings("unchecked") + protected E cast(final Throwable throwable) { + return (E) throwable; + } + /** * Closes the pool, destroys the remaining idle objects and, if registered * in JMX, deregisters it. @@ -473,9 +486,9 @@ ArrayList> createRemoveList(final AbandonedConfig abandonedConfi /** * Tries to ensure that the configured minimum number of idle instances are * available in the pool. - * @throws Exception if an error occurs creating idle instances + * @throws E if an error occurs creating idle instances */ - abstract void ensureMinIdle() throws Exception; + abstract void ensureMinIdle() throws E; /** * Perform {@code numTests} idle object eviction tests, evicting @@ -485,9 +498,9 @@ ArrayList> createRemoveList(final AbandonedConfig abandonedConfi * have been idle for more than {@code minEvicableIdleTimeMillis} * are removed. * - * @throws Exception when there is a problem evicting idle objects. + * @throws E when there is a problem evicting idle objects. */ - public abstract void evict() throws Exception; + public abstract void evict() throws E; /** * Gets whether to block when the {@code borrowObject()} method is diff --git a/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java b/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java index cfe5aee55..24e162df3 100644 --- a/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java +++ b/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java @@ -74,7 +74,8 @@ private static class Reaper implements Runnable { @Override public void run() { synchronized (EvictionTimer.class) { - for (final Entry.Evictor>, WeakRunner.Evictor>> entry : TASK_MAP.entrySet()) { + for (final Entry.Evictor>, WeakRunner.Evictor>> entry : TASK_MAP + .entrySet()) { if (entry.getKey().get() == null) { executor.remove(entry.getValue()); TASK_MAP.remove(entry.getKey()); @@ -125,8 +126,8 @@ public void run() { /** Keys are weak references to tasks, values are runners managed by executor. */ private static final HashMap< - WeakReference.Evictor>, - WeakRunner.Evictor>> TASK_MAP = new HashMap<>(); // @GuardedBy("EvictionTimer.class") + WeakReference.Evictor>, + WeakRunner.Evictor>> TASK_MAP = new HashMap<>(); // @GuardedBy("EvictionTimer.class") /** * Removes the specified eviction task from the timer. @@ -137,7 +138,7 @@ public void run() { * terminate? * @param restarting The state of the evictor. */ - static synchronized void cancel(final BaseGenericObjectPool.Evictor evictor, final Duration timeout, + static synchronized void cancel(final BaseGenericObjectPool.Evictor evictor, final Duration timeout, final boolean restarting) { if (evictor != null) { evictor.cancel(); @@ -177,7 +178,7 @@ static synchronized int getNumTasks() { * * @return the task map. */ - static HashMap.Evictor>, WeakRunner.Evictor>> getTaskMap() { + static HashMap.Evictor>, WeakRunner.Evictor>> getTaskMap() { return TASK_MAP; } @@ -187,8 +188,8 @@ static HashMap.Evictor>, WeakRunner.Evictor evictor) { - for (final Entry.Evictor>, WeakRunner.Evictor>> entry : TASK_MAP.entrySet()) { + private static void remove(final BaseGenericObjectPool.Evictor evictor) { + for (final Entry.Evictor>, WeakRunner.Evictor>> entry : TASK_MAP.entrySet()) { if (entry.getKey().get() == evictor) { executor.remove(entry.getValue()); TASK_MAP.remove(entry.getKey()); @@ -209,14 +210,14 @@ private static void remove(final BaseGenericObjectPool.Evictor evictor) { * @param period Time in milliseconds between executions. */ static synchronized void schedule( - final BaseGenericObjectPool.Evictor task, final Duration delay, final Duration period) { + final BaseGenericObjectPool.Evictor task, final Duration delay, final Duration period) { if (null == executor) { executor = new ScheduledThreadPoolExecutor(1, new EvictorThreadFactory()); executor.setRemoveOnCancelPolicy(true); executor.scheduleAtFixedRate(new Reaper(), delay.toMillis(), period.toMillis(), TimeUnit.MILLISECONDS); } - final WeakReference.Evictor> ref = new WeakReference<>(task); - final WeakRunner.Evictor> runner = new WeakRunner<>(ref); + final WeakReference.Evictor> ref = new WeakReference<>(task); + final WeakRunner.Evictor> runner = new WeakRunner<>(ref); final ScheduledFuture scheduledFuture = executor.scheduleWithFixedDelay(runner, delay.toMillis(), period.toMillis(), TimeUnit.MILLISECONDS); task.setScheduledFuture(scheduledFuture); diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java index 02c68eaa4..81aeb837a 100644 --- a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java @@ -84,11 +84,12 @@ * * @param The type of keys maintained by this pool. * @param Type of element pooled in this pool. + * @param Type of exception thrown in this pool. * * @since 2.0 */ -public class GenericKeyedObjectPool extends BaseGenericObjectPool - implements KeyedObjectPool, GenericKeyedObjectPoolMXBean, UsageTracking { +public class GenericKeyedObjectPool extends BaseGenericObjectPool + implements KeyedObjectPool, GenericKeyedObjectPoolMXBean, UsageTracking { /** * Maintains information on the per key queue for a given key. @@ -203,7 +204,7 @@ public String toString() { private volatile int maxTotalPerKey = GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL_PER_KEY; - private final KeyedPooledObjectFactory factory; + private final KeyedPooledObjectFactory factory; private final boolean fairness; @@ -243,7 +244,7 @@ public String toString() { * {@link GenericKeyedObjectPoolConfig}. * @param factory the factory to be used to create entries */ - public GenericKeyedObjectPool(final KeyedPooledObjectFactory factory) { + public GenericKeyedObjectPool(final KeyedPooledObjectFactory factory) { this(factory, new GenericKeyedObjectPoolConfig<>()); } @@ -257,7 +258,7 @@ public GenericKeyedObjectPool(final KeyedPooledObjectFactory factory) { * the configuration object will not be reflected in the * pool. */ - public GenericKeyedObjectPool(final KeyedPooledObjectFactory factory, + public GenericKeyedObjectPool(final KeyedPooledObjectFactory factory, final GenericKeyedObjectPoolConfig config) { super(config, ONAME_BASE, config.getJmxNamePrefix()); @@ -286,7 +287,7 @@ public GenericKeyedObjectPool(final KeyedPooledObjectFactory factory, * and removal. The configuration is used by value. * @since 2.10.0 */ - public GenericKeyedObjectPool(final KeyedPooledObjectFactory factory, + public GenericKeyedObjectPool(final KeyedPooledObjectFactory factory, final GenericKeyedObjectPoolConfig config, final AbandonedConfig abandonedConfig) { this(factory, config); setAbandonedConfig(abandonedConfig); @@ -298,9 +299,9 @@ public GenericKeyedObjectPool(final KeyedPooledObjectFactory factory, * @param key The key to associate with the idle object * @param p The wrapped object to add. * - * @throws Exception If the associated factory fails to passivate the object + * @throws E If the associated factory fails to passivate the object */ - private void addIdleObject(final K key, final PooledObject p) throws Exception { + private void addIdleObject(final K key, final PooledObject p) throws E { if (p != null) { factory.passivateObject(key, p); @@ -323,11 +324,11 @@ private void addIdleObject(final K key, final PooledObject p) throws Exceptio * * @param key the key a new instance should be added to * - * @throws Exception when {@link KeyedPooledObjectFactory#makeObject} + * @throws E when {@link KeyedPooledObjectFactory#makeObject} * fails. */ @Override - public void addObject(final K key) throws Exception { + public void addObject(final K key) throws E { assertOpen(); register(key); try { @@ -346,7 +347,7 @@ public void addObject(final K key) throws Exception { * {@inheritDoc} */ @Override - public T borrowObject(final K key) throws Exception { + public T borrowObject(final K key) throws E { return borrowObject(key, getMaxWaitDuration().toMillis()); } @@ -409,10 +410,10 @@ public T borrowObject(final K key) throws Exception { * @throws NoSuchElementException if a keyed object instance cannot be * returned because the pool is exhausted. * - * @throws Exception if a keyed object instance cannot be returned due to an + * @throws E if a keyed object instance cannot be returned due to an * error */ - public T borrowObject(final K key, final long borrowMaxWaitMillis) throws Exception { + public T borrowObject(final K key, final long borrowMaxWaitMillis) throws E { assertOpen(); final AbandonedConfig ac = this.abandonedConfig; @@ -443,10 +444,11 @@ public T borrowObject(final K key, final long borrowMaxWaitMillis) throws Except } if (blockWhenExhausted) { if (p == null) { - if (borrowMaxWaitMillis < 0) { - p = objectDeque.getIdleObjects().takeFirst(); - } else { - p = objectDeque.getIdleObjects().pollFirst(borrowMaxWaitMillis, TimeUnit.MILLISECONDS); + try { + p = borrowMaxWaitMillis < 0 ? objectDeque.getIdleObjects().takeFirst() : + objectDeque.getIdleObjects().pollFirst(borrowMaxWaitMillis, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + throw cast(e); } } if (p == null) { @@ -724,9 +726,9 @@ public void close() { * * @return The new, wrapped pooled object * - * @throws Exception If the objection creation fails + * @throws E If the objection creation fails */ - private PooledObject create(final K key) throws Exception { + private PooledObject create(final K key) throws E { int maxTotalPerKeySave = getMaxTotalPerKey(); // Per key if (maxTotalPerKeySave < 0) { maxTotalPerKeySave = Integer.MAX_VALUE; @@ -776,7 +778,11 @@ private PooledObject create(final K key) throws Exception { // bring the pool to capacity. Those calls might also // fail so wait until they complete and then re-test if // the pool is at capacity or not. - objectDeque.makeObjectCountLock.wait(); + try { + objectDeque.makeObjectCountLock.wait(); + } catch (InterruptedException e) { + throw cast(e); + } } } else { // The pool is not at capacity. Create a new object. @@ -864,10 +870,9 @@ private void deregister(final K k) { * @param destroyMode DestroyMode context provided to the factory * * @return {@code true} if the object was destroyed, otherwise {@code false} - * @throws Exception If the object destruction failed + * @throws E If the object destruction failed */ - private boolean destroy(final K key, final PooledObject toDestroy, final boolean always, final DestroyMode destroyMode) - throws Exception { + private boolean destroy(final K key, final PooledObject toDestroy, final boolean always, final DestroyMode destroyMode) throws E { final ObjectDeque objectDeque = register(key); @@ -903,7 +908,7 @@ private boolean destroy(final K key, final PooledObject toDestroy, final bool @Override - void ensureMinIdle() throws Exception { + void ensureMinIdle() throws E { final int minIdlePerKeySave = getMinIdlePerKey(); if (minIdlePerKeySave < 1) { return; @@ -920,9 +925,9 @@ void ensureMinIdle() throws Exception { * * @param key The key to check for idle objects * - * @throws Exception If a new object is required and cannot be created + * @throws E If a new object is required and cannot be created */ - private void ensureMinIdle(final K key) throws Exception { + private void ensureMinIdle(final K key) throws E { // Calculate current pool objects ObjectDeque objectDeque = poolMap.get(key); @@ -956,7 +961,7 @@ private void ensureMinIdle(final K key) throws Exception { *

*/ @Override - public void evict() throws Exception { + public void evict() throws E { assertOpen(); if (getNumIdle() > 0) { @@ -1102,7 +1107,7 @@ public void evict() throws Exception { * * @return the factory */ - public KeyedPooledObjectFactory getFactory() { + public KeyedPooledObjectFactory getFactory() { return factory; } @@ -1305,13 +1310,13 @@ private boolean hasBorrowWaiters() { * @param key pool key * @param obj instance to invalidate * - * @throws Exception if an exception occurs destroying the + * @throws E if an exception occurs destroying the * object * @throws IllegalStateException if obj does not belong to the pool * under the given key */ @Override - public void invalidateObject(final K key, final T obj) throws Exception { + public void invalidateObject(final K key, final T obj) throws E { invalidateObject(key, obj, DestroyMode.NORMAL); } @@ -1326,14 +1331,14 @@ public void invalidateObject(final K key, final T obj) throws Exception { * @param obj instance to invalidate * @param destroyMode DestroyMode context provided to factory * - * @throws Exception if an exception occurs destroying the + * @throws E if an exception occurs destroying the * object * @throws IllegalStateException if obj does not belong to the pool * under the given key * @since 2.9.0 */ @Override - public void invalidateObject(final K key, final T obj, final DestroyMode destroyMode) throws Exception { + public void invalidateObject(final K key, final T obj, final DestroyMode destroyMode) throws E { final ObjectDeque objectDeque = poolMap.get(key); final PooledObject p = objectDeque.getAllObjects().get(new IdentityWrapper<>(obj)); if (p == null) { @@ -1377,9 +1382,9 @@ public Map> listAllObjects() { * * @param key - The key to register for pool control. * - * @throws Exception If the associated factory throws an exception + * @throws E If the associated factory throws an exception */ - public void preparePool(final K key) throws Exception { + public void preparePool(final K key) throws E { final int minIdlePerKeySave = getMinIdlePerKey(); if (minIdlePerKeySave < 1) { return; @@ -1579,7 +1584,7 @@ private void reuseCapacity() { int maxQueueLength = 0; LinkedBlockingDeque> mostLoaded = null; K loadedKey = null; - for (final Entry.ObjectDeque> entry : poolMap.entrySet()) { + for (final Entry.ObjectDeque> entry : poolMap.entrySet()) { final K k = entry.getKey(); final ObjectDeque deque = entry.getValue(); if (deque != null) { diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java index ee9739a8c..f68e99f2b 100644 --- a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java @@ -76,11 +76,12 @@ * @see GenericKeyedObjectPool * * @param Type of element pooled in this pool. + * @param Type of exception thrown in this pool. * * @since 2.0 */ -public class GenericObjectPool extends BaseGenericObjectPool - implements ObjectPool, GenericObjectPoolMXBean, UsageTracking { +public class GenericObjectPool extends BaseGenericObjectPool + implements ObjectPool, GenericObjectPoolMXBean, UsageTracking { // JMX specific attributes private static final String ONAME_BASE = @@ -92,7 +93,7 @@ public class GenericObjectPool extends BaseGenericObjectPool private volatile int minIdle = GenericObjectPoolConfig.DEFAULT_MIN_IDLE; - private final PooledObjectFactory factory; + private final PooledObjectFactory factory; /* * All of the objects currently associated with this pool in any state. It @@ -126,7 +127,7 @@ public class GenericObjectPool extends BaseGenericObjectPool * @param factory The object factory to be used to create object instances * used by this pool */ - public GenericObjectPool(final PooledObjectFactory factory) { + public GenericObjectPool(final PooledObjectFactory factory) { this(factory, new GenericObjectPoolConfig<>()); } @@ -141,7 +142,7 @@ public GenericObjectPool(final PooledObjectFactory factory) { * the configuration object will not be reflected in the * pool. */ - public GenericObjectPool(final PooledObjectFactory factory, + public GenericObjectPool(final PooledObjectFactory factory, final GenericObjectPoolConfig config) { super(config, ONAME_BASE, config.getJmxNamePrefix()); @@ -170,7 +171,7 @@ public GenericObjectPool(final PooledObjectFactory factory, * @param abandonedConfig Configuration for abandoned object identification * and removal. The configuration is used by value. */ - public GenericObjectPool(final PooledObjectFactory factory, + public GenericObjectPool(final PooledObjectFactory factory, final GenericObjectPoolConfig config, final AbandonedConfig abandonedConfig) { this(factory, config); setAbandonedConfig(abandonedConfig); @@ -183,9 +184,9 @@ public GenericObjectPool(final PooledObjectFactory factory, * * @param p The object to make idle * - * @throws Exception If the factory fails to passivate the object + * @throws E If the factory fails to passivate the object */ - private void addIdleObject(final PooledObject p) throws Exception { + private void addIdleObject(final PooledObject p) throws E { if (p != null) { factory.passivateObject(p); if (getLifo()) { @@ -204,7 +205,7 @@ private void addIdleObject(final PooledObject p) throws Exception { * (no exception, no impact to the pool).

*/ @Override - public void addObject() throws Exception { + public void addObject() throws E { assertOpen(); if (factory == null) { throw new IllegalStateException("Cannot add objects without a factory."); @@ -219,7 +220,7 @@ public void addObject() throws Exception { * {@inheritDoc} */ @Override - public T borrowObject() throws Exception { + public T borrowObject() throws E { return borrowObject(getMaxWaitDuration()); } @@ -266,14 +267,11 @@ public T borrowObject() throws Exception { * to become available * * @return object instance from the pool - * * @throws NoSuchElementException if an instance cannot be returned - * - * @throws Exception if an object instance cannot be returned due to an - * error + * @throws E if an object instance cannot be returned due to an error * @since 2.10.0 */ - public T borrowObject(final Duration borrowMaxWaitDuration) throws Exception { + public T borrowObject(final Duration borrowMaxWaitDuration) throws E { assertOpen(); final AbandonedConfig ac = this.abandonedConfig; @@ -302,11 +300,12 @@ public T borrowObject(final Duration borrowMaxWaitDuration) throws Exception { } if (blockWhenExhausted) { if (p == null) { - if (borrowMaxWaitDuration.isNegative()) { - p = idleObjects.takeFirst(); - } else { - p = idleObjects.pollFirst(borrowMaxWaitDuration); - } + try { + p = borrowMaxWaitDuration.isNegative() ? idleObjects.takeFirst() : idleObjects.pollFirst(borrowMaxWaitDuration); + } catch (final InterruptedException e) { + // Don't surface exception type of internal locking mechanism. + throw cast(e); + } } if (p == null) { throw new NoSuchElementException(appendStats( @@ -415,10 +414,10 @@ public T borrowObject(final Duration borrowMaxWaitDuration) throws Exception { * * @throws NoSuchElementException if an instance cannot be returned * - * @throws Exception if an object instance cannot be returned due to an + * @throws E if an object instance cannot be returned due to an * error */ - public T borrowObject(final long borrowMaxWaitMillis) throws Exception { + public T borrowObject(final long borrowMaxWaitMillis) throws E { return borrowObject(Duration.ofMillis(borrowMaxWaitMillis)); } @@ -492,15 +491,14 @@ public void close() { /** * Attempts to create a new wrapped pooled object. *

- * If there are {@link #getMaxTotal()} objects already in circulation - * or in process of being created, this method returns null. + * If there are {@link #getMaxTotal()} objects already in circulation or in process of being created, this method + * returns null. *

* * @return The new wrapped pooled object - * - * @throws Exception if the object factory's {@code makeObject} fails + * @throws E if the object factory's {@code makeObject} fails */ - private PooledObject create() throws Exception { + private PooledObject create() throws E { int localMaxTotal = getMaxTotal(); // This simplifies the code later in this method if (localMaxTotal < 0) { @@ -533,7 +531,12 @@ private PooledObject create() throws Exception { // bring the pool to capacity. Those calls might also // fail so wait until they complete and then re-test if // the pool is at capacity or not. - makeObjectCountLock.wait(localMaxWaitTimeMillis); + try { + makeObjectCountLock.wait(localMaxWaitTimeMillis); + } catch (final InterruptedException e) { + // Don't surface exception type of internal locking mechanism. + throw cast(e); + } } } else { // The pool is not at capacity. Create a new object. @@ -588,10 +591,10 @@ private PooledObject create() throws Exception { * @param toDestroy The wrapped pooled object to destroy * @param destroyMode DestroyMode context provided to the factory * - * @throws Exception If the factory fails to destroy the pooled object + * @throws E If the factory fails to destroy the pooled object * cleanly */ - private void destroy(final PooledObject toDestroy, final DestroyMode destroyMode) throws Exception { + private void destroy(final PooledObject toDestroy, final DestroyMode destroyMode) throws E { toDestroy.invalidate(); idleObjects.remove(toDestroy); allObjects.remove(new IdentityWrapper<>(toDestroy.getObject())); @@ -614,9 +617,9 @@ private void destroy(final PooledObject toDestroy, final DestroyMode destroyM * * @param idleCount the number of idle instances desired * @param always true means create instances even if the pool has no threads waiting - * @throws Exception if the factory's makeObject throws + * @throws E if the factory's makeObject throws */ - private void ensureIdle(final int idleCount, final boolean always) throws Exception { + private void ensureIdle(final int idleCount, final boolean always) throws E { if (idleCount < 1 || isClosed() || (!always && !idleObjects.hasTakeWaiters())) { return; } @@ -643,7 +646,7 @@ private void ensureIdle(final int idleCount, final boolean always) throws Except } @Override - void ensureMinIdle() throws Exception { + void ensureMinIdle() throws E { ensureIdle(getMinIdle(), true); } @@ -655,7 +658,7 @@ void ensureMinIdle() throws Exception { *

*/ @Override - public void evict() throws Exception { + public void evict() throws E { assertOpen(); if (!idleObjects.isEmpty()) { @@ -774,7 +777,7 @@ public void evict() throws Exception { * * @return the factory */ - public PooledObjectFactory getFactory() { + public PooledObjectFactory getFactory() { return factory; } @@ -898,33 +901,31 @@ String getStatsString() { /** * {@inheritDoc} *

- * Activation of this method decrements the active count and attempts to - * destroy the instance, using the default (NORMAL) {@link DestroyMode}. + * Activation of this method decrements the active count and attempts to destroy the instance, using the default + * (NORMAL) {@link DestroyMode}. *

* - * @throws Exception if an exception occurs destroying the - * object + * @throws E if an exception occurs destroying the * @throws IllegalStateException if obj does not belong to this pool */ @Override - public void invalidateObject(final T obj) throws Exception { + public void invalidateObject(final T obj) throws E { invalidateObject(obj, DestroyMode.NORMAL); } /** * {@inheritDoc} *

- * Activation of this method decrements the active count and attempts to - * destroy the instance, using the provided {@link DestroyMode}. + * Activation of this method decrements the active count and attempts to destroy the instance, using the provided + * {@link DestroyMode}. *

- * - * @throws Exception if an exception occurs destroying the - * object + * + * @throws E if an exception occurs destroying the object * @throws IllegalStateException if obj does not belong to this pool * @since 2.9.0 */ @Override - public void invalidateObject(final T obj, final DestroyMode destroyMode) throws Exception { + public void invalidateObject(final T obj, final DestroyMode destroyMode) throws E { final PooledObject p = getPooledObject(obj); if (p == null) { if (isAbandonedConfig()) { @@ -961,10 +962,10 @@ public Set listAllObjects() { * Tries to ensure that {@link #getMinIdle()} idle instances are available * in the pool. * - * @throws Exception If the associated factory throws an exception + * @throws E If the associated factory throws an exception * @since 2.4 */ - public void preparePool() throws Exception { + public void preparePool() throws E { if (getMinIdle() < 1) { return; } @@ -1177,5 +1178,5 @@ public void use(final T pooledObject) { getPooledObject(pooledObject).use(); } } - + } diff --git a/src/main/java/org/apache/commons/pool2/impl/SoftReferenceObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/SoftReferenceObjectPool.java index 868bfcfff..2fe90d9dc 100644 --- a/src/main/java/org/apache/commons/pool2/impl/SoftReferenceObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/SoftReferenceObjectPool.java @@ -37,13 +37,15 @@ * * @param * Type of element pooled in this pool. + * @param + * Type of exception thrown by this pool. * * @since 2.0 */ -public class SoftReferenceObjectPool extends BaseObjectPool { +public class SoftReferenceObjectPool extends BaseObjectPool { /** Factory to source pooled objects */ - private final PooledObjectFactory factory; + private final PooledObjectFactory factory; /** * Queue of broken references that might be able to be removed from @@ -75,7 +77,7 @@ public class SoftReferenceObjectPool extends BaseObjectPool { * * @param factory object factory to use. */ - public SoftReferenceObjectPool(final PooledObjectFactory factory) { + public SoftReferenceObjectPool(final PooledObjectFactory factory) { this.factory = factory; } @@ -98,12 +100,12 @@ public SoftReferenceObjectPool(final PooledObjectFactory factory) { * * @throws IllegalStateException * if invoked on a {@link #close() closed} pool - * @throws Exception + * @throws E * when the {@link #getFactory() factory} has a problem creating * or passivating an object. */ @Override - public synchronized void addObject() throws Exception { + public synchronized void addObject() throws E { assertOpen(); if (factory == null) { throw new IllegalStateException( @@ -169,13 +171,13 @@ public synchronized void addObject() throws Exception { * if a valid object cannot be provided * @throws IllegalStateException * if invoked on a {@link #close() closed} pool - * @throws Exception + * @throws E * if an exception occurs creating a new instance * @return a valid, activated object instance */ @SuppressWarnings("null") // ref cannot be null @Override - public synchronized T borrowObject() throws Exception { + public synchronized T borrowObject() throws E { assertOpen(); T obj = null; boolean newlyCreated = false; @@ -268,9 +270,9 @@ public void close() { * * @param toDestroy PooledSoftReference to destroy * - * @throws Exception If an error occurs while trying to destroy the object + * @throws E If an error occurs while trying to destroy the object */ - private void destroy(final PooledSoftReference toDestroy) throws Exception { + private void destroy(final PooledSoftReference toDestroy) throws E { toDestroy.invalidate(); idleReferences.remove(toDestroy); allReferences.remove(toDestroy); @@ -300,7 +302,7 @@ private PooledSoftReference findReference(final T obj) { * * @return the factory */ - public synchronized PooledObjectFactory getFactory() { + public synchronized PooledObjectFactory getFactory() { return factory; } @@ -330,7 +332,7 @@ public synchronized int getNumIdle() { * {@inheritDoc} */ @Override - public synchronized void invalidateObject(final T obj) throws Exception { + public synchronized void invalidateObject(final T obj) throws E { final PooledSoftReference ref = findReference(obj); if (ref == null) { throw new IllegalStateException( @@ -392,7 +394,7 @@ private void removeClearedReferences(final Iterator> iter * if obj is not currently part of this pool */ @Override - public synchronized void returnObject(final T obj) throws Exception { + public synchronized void returnObject(final T obj) throws E { boolean success = !isClosed(); final PooledSoftReference ref = findReference(obj); if (ref == null) { diff --git a/src/main/java/org/apache/commons/pool2/proxy/ProxiedKeyedObjectPool.java b/src/main/java/org/apache/commons/pool2/proxy/ProxiedKeyedObjectPool.java index 96f8ad863..38a6c4a59 100644 --- a/src/main/java/org/apache/commons/pool2/proxy/ProxiedKeyedObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/proxy/ProxiedKeyedObjectPool.java @@ -30,12 +30,13 @@ * * @param type of the key * @param type of the pooled object + * @param type of exception thrown by this pool * * @since 2.0 */ -public class ProxiedKeyedObjectPool implements KeyedObjectPool { +public class ProxiedKeyedObjectPool implements KeyedObjectPool { - private final KeyedObjectPool pool; + private final KeyedObjectPool pool; private final ProxySource proxySource; @@ -45,7 +46,7 @@ public class ProxiedKeyedObjectPool implements KeyedObjectPool { * @param pool The object pool to wrap * @param proxySource The source of the proxy objects */ - public ProxiedKeyedObjectPool(final KeyedObjectPool pool, + public ProxiedKeyedObjectPool(final KeyedObjectPool pool, final ProxySource proxySource) { this.pool = pool; this.proxySource = proxySource; @@ -53,14 +54,14 @@ public ProxiedKeyedObjectPool(final KeyedObjectPool pool, @Override - public void addObject(final K key) throws Exception, IllegalStateException, + public void addObject(final K key) throws E, IllegalStateException, UnsupportedOperationException { pool.addObject(key); } @SuppressWarnings("unchecked") @Override - public V borrowObject(final K key) throws Exception, NoSuchElementException, + public V borrowObject(final K key) throws E, NoSuchElementException, IllegalStateException { UsageTracking usageTracking = null; if (pool instanceof UsageTracking) { @@ -70,12 +71,12 @@ public V borrowObject(final K key) throws Exception, NoSuchElementException, } @Override - public void clear() throws Exception, UnsupportedOperationException { + public void clear() throws E, UnsupportedOperationException { pool.clear(); } @Override - public void clear(final K key) throws Exception, UnsupportedOperationException { + public void clear(final K key) throws E, UnsupportedOperationException { pool.clear(key); } @@ -110,12 +111,12 @@ public int getNumIdle(final K key) { } @Override - public void invalidateObject(final K key, final V proxy) throws Exception { + public void invalidateObject(final K key, final V proxy) throws E { pool.invalidateObject(key, proxySource.resolveProxy(proxy)); } @Override - public void returnObject(final K key, final V proxy) throws Exception { + public void returnObject(final K key, final V proxy) throws E { pool.returnObject(key, proxySource.resolveProxy(proxy)); } diff --git a/src/main/java/org/apache/commons/pool2/proxy/ProxiedObjectPool.java b/src/main/java/org/apache/commons/pool2/proxy/ProxiedObjectPool.java index e725d228c..e0b7cd7a2 100644 --- a/src/main/java/org/apache/commons/pool2/proxy/ProxiedObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/proxy/ProxiedObjectPool.java @@ -28,12 +28,13 @@ * object to the pool. * * @param type of the pooled object + * @param type of the exception * * @since 2.0 */ -public class ProxiedObjectPool implements ObjectPool { +public class ProxiedObjectPool implements ObjectPool { - private final ObjectPool pool; + private final ObjectPool pool; private final ProxySource proxySource; @@ -43,22 +44,20 @@ public class ProxiedObjectPool implements ObjectPool { * @param pool The object pool to wrap * @param proxySource The source of the proxy objects */ - public ProxiedObjectPool(final ObjectPool pool, final ProxySource proxySource) { + public ProxiedObjectPool(final ObjectPool pool, final ProxySource proxySource) { this.pool = pool; this.proxySource = proxySource; } @Override - public void addObject() throws Exception, IllegalStateException, - UnsupportedOperationException { + public void addObject() throws E, IllegalStateException, UnsupportedOperationException { pool.addObject(); } @SuppressWarnings("unchecked") @Override - public T borrowObject() throws Exception, NoSuchElementException, - IllegalStateException { + public T borrowObject() throws E, NoSuchElementException, IllegalStateException { UsageTracking usageTracking = null; if (pool instanceof UsageTracking) { usageTracking = (UsageTracking) pool; @@ -68,7 +67,7 @@ public T borrowObject() throws Exception, NoSuchElementException, @Override - public void clear() throws Exception, UnsupportedOperationException { + public void clear() throws E, UnsupportedOperationException { pool.clear(); } @@ -92,13 +91,13 @@ public int getNumIdle() { @Override - public void invalidateObject(final T proxy) throws Exception { + public void invalidateObject(final T proxy) throws E { pool.invalidateObject(proxySource.resolveProxy(proxy)); } @Override - public void returnObject(final T proxy) throws Exception { + public void returnObject(final T proxy) throws E { pool.returnObject(proxySource.resolveProxy(proxy)); } diff --git a/src/test/java/org/apache/commons/pool2/MethodCallPoolableObjectFactory.java b/src/test/java/org/apache/commons/pool2/MethodCallPoolableObjectFactory.java index 654b80af3..7ab670973 100644 --- a/src/test/java/org/apache/commons/pool2/MethodCallPoolableObjectFactory.java +++ b/src/test/java/org/apache/commons/pool2/MethodCallPoolableObjectFactory.java @@ -27,7 +27,7 @@ * * @see MethodCall */ -public class MethodCallPoolableObjectFactory implements PooledObjectFactory { +public class MethodCallPoolableObjectFactory implements PooledObjectFactory { private final List methodCalls = new ArrayList<>(); private int count; private boolean valid = true; @@ -38,7 +38,7 @@ public class MethodCallPoolableObjectFactory implements PooledObjectFactory obj) throws Exception { + public void activateObject(final PooledObject obj) { methodCalls.add(new MethodCall("activateObject", obj.getObject())); if (activateObjectFail) { throw new PrivateException("activateObject"); @@ -46,7 +46,7 @@ public void activateObject(final PooledObject obj) throws Exception { } @Override - public void destroyObject(final PooledObject obj) throws Exception { + public void destroyObject(final PooledObject obj) { methodCalls.add(new MethodCall("destroyObject", obj.getObject())); if (destroyObjectFail) { throw new PrivateException("destroyObject"); @@ -86,7 +86,7 @@ public boolean isValidateObjectFail() { } @Override - public PooledObject makeObject() throws Exception { + public PooledObject makeObject() { final MethodCall call = new MethodCall("makeObject"); methodCalls.add(call); final int originalCount = this.count++; @@ -100,7 +100,7 @@ public PooledObject makeObject() throws Exception { } @Override - public void passivateObject(final PooledObject obj) throws Exception { + public void passivateObject(final PooledObject obj) { methodCalls.add(new MethodCall("passivateObject", obj.getObject())); if (passivateObjectFail) { throw new PrivateException("passivateObject"); diff --git a/src/test/java/org/apache/commons/pool2/ObjectPoolIssue326.java b/src/test/java/org/apache/commons/pool2/ObjectPoolIssue326.java index 120c2f049..9123eaa61 100644 --- a/src/test/java/org/apache/commons/pool2/ObjectPoolIssue326.java +++ b/src/test/java/org/apache/commons/pool2/ObjectPoolIssue326.java @@ -39,9 +39,9 @@ * negatively since you need to run it for a while. */ public final class ObjectPoolIssue326 { - private class ObjectFactory extends BaseKeyedPooledObjectFactory { + private class ObjectFactory extends BaseKeyedPooledObjectFactory { @Override - public Object create(final Integer s) throws Exception { + public Object create(final Integer s) { return new TestObject(); } @@ -51,11 +51,11 @@ public PooledObject wrap(final Object o) { } } - private class Task implements Callable { - private final GenericKeyedObjectPool m_pool; + private class Task implements Callable { + private final GenericKeyedObjectPool m_pool; private final int m_key; - Task(final GenericKeyedObjectPool pool, final int count) { + Task(final GenericKeyedObjectPool pool, final int count) { m_pool = pool; m_key = count % 20; } @@ -99,10 +99,10 @@ public static void main(final String[] args) { } } - private List createTasks(final GenericKeyedObjectPool pool) { - final List tasks = new ArrayList<>(); + private List> createTasks(final GenericKeyedObjectPool pool) { + final List> tasks = new ArrayList<>(); for (int i = 0; i < 250; i++) { - tasks.add(new Task(pool, i)); + tasks.add(new Task<>(pool, i)); } return tasks; } @@ -130,7 +130,7 @@ private void run() throws Exception { poolConfig.setJmxNameBase(null); poolConfig.setJmxNamePrefix(null); - final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(new ObjectFactory(), poolConfig); + final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(new ObjectFactory(), poolConfig); // number of threads to reproduce is finicky. this count seems to be best for my // 4 core box. @@ -144,7 +144,7 @@ private void run() throws Exception { if (testIter % 1000 == 0) { System.out.println(testIter); } - final List tasks = createTasks(pool); + final List> tasks = createTasks(pool); final List> futures = service.invokeAll(tasks); for (final Future future : futures) { future.get(); diff --git a/src/test/java/org/apache/commons/pool2/PoolTest.java b/src/test/java/org/apache/commons/pool2/PoolTest.java index 0290a7d4a..99e2c3e7b 100644 --- a/src/test/java/org/apache/commons/pool2/PoolTest.java +++ b/src/test/java/org/apache/commons/pool2/PoolTest.java @@ -33,24 +33,24 @@ public class PoolTest { private static class Foo { } - private static class PooledFooFactory implements PooledObjectFactory { + private static class PooledFooFactory implements PooledObjectFactory { private static final long VALIDATION_WAIT_IN_MILLIS = 1000; @Override - public void activateObject(final PooledObject pooledObject) throws Exception { + public void activateObject(final PooledObject pooledObject) { } @Override - public void destroyObject(final PooledObject pooledObject) throws Exception { + public void destroyObject(final PooledObject pooledObject) { } @Override - public PooledObject makeObject() throws Exception { + public PooledObject makeObject() { return new DefaultPooledObject<>(new Foo()); } @Override - public void passivateObject(final PooledObject pooledObject) throws Exception { + public void passivateObject(final PooledObject pooledObject) { } @Override @@ -73,7 +73,7 @@ public void testPool() throws Exception { final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig<>(); poolConfig.setTestWhileIdle(true /* testWhileIdle */); final PooledFooFactory pooledFooFactory = new PooledFooFactory(); - try (GenericObjectPool pool = new GenericObjectPool<>(pooledFooFactory, poolConfig)) { + try (GenericObjectPool pool = new GenericObjectPool<>(pooledFooFactory, poolConfig)) { pool.setTimeBetweenEvictionRunsMillis(EVICTION_PERIOD_IN_MILLIS); assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getDurationBetweenEvictionRuns().toMillis()); assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getTimeBetweenEvictionRuns().toMillis()); diff --git a/src/test/java/org/apache/commons/pool2/TestBaseKeyedPoolableObjectFactory.java b/src/test/java/org/apache/commons/pool2/TestBaseKeyedPoolableObjectFactory.java index e939cca82..fe8500a18 100644 --- a/src/test/java/org/apache/commons/pool2/TestBaseKeyedPoolableObjectFactory.java +++ b/src/test/java/org/apache/commons/pool2/TestBaseKeyedPoolableObjectFactory.java @@ -25,10 +25,9 @@ */ public class TestBaseKeyedPoolableObjectFactory { - private static class TestFactory - extends BaseKeyedPooledObjectFactory { + private static class TestFactory extends BaseKeyedPooledObjectFactory { @Override - public Object create(final Object key) throws Exception { + public Object create(final Object key) { return null; } @Override @@ -38,8 +37,8 @@ public PooledObject wrap(final Object value) { } @Test - public void testDefaultMethods() throws Exception { - final KeyedPooledObjectFactory factory = new TestFactory(); + public void testDefaultMethods() { + final KeyedPooledObjectFactory factory = new TestFactory(); factory.activateObject("key",null); // a no-op factory.passivateObject("key",null); // a no-op diff --git a/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java b/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java index 71b99da38..edb4da2eb 100644 --- a/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java @@ -26,20 +26,23 @@ /** */ public class TestBaseObjectPool extends TestObjectPool { - private static class TestObjectPool extends BaseObjectPool { + private static class TestObjectPool extends BaseObjectPool { + @Override public Object borrowObject() { return null; } + @Override public void invalidateObject(final Object obj) { } + @Override public void returnObject(final Object obj) { } } - private ObjectPool pool; + private ObjectPool pool; /** * @param n Ignored by this implemented. Used by sub-classes. @@ -72,7 +75,7 @@ protected boolean isLifo() { * * @return A newly created empty pool */ - protected ObjectPool makeEmptyPool(final int minCapacity) { + protected ObjectPool makeEmptyPool(final int minCapacity) { if (this.getClass() != TestBaseObjectPool.class) { fail("Subclasses of TestBaseObjectPool must reimplement this method."); } @@ -80,7 +83,7 @@ protected ObjectPool makeEmptyPool(final int minCapacity) { } @Override - protected ObjectPool makeEmptyPool(final PooledObjectFactory factory) { + protected ObjectPool makeEmptyPool(final PooledObjectFactory factory) { if (this.getClass() != TestBaseObjectPool.class) { fail("Subclasses of TestBaseObjectPool must reimplement this method."); } @@ -254,7 +257,7 @@ public void testBaseNumActiveNumIdle() throws Exception { @Test public void testClose() { @SuppressWarnings("resource") - final ObjectPool pool = new TestObjectPool(); + final ObjectPool pool = new TestObjectPool(); pool.close(); pool.close(); // should not error as of Pool 2.0. @@ -266,7 +269,7 @@ public void testUnsupportedOperations() { if (!getClass().equals(TestBaseObjectPool.class)) { return; // skip redundant tests } - try (final ObjectPool pool = new TestObjectPool()) { + try (final ObjectPool pool = new TestObjectPool()) { assertTrue( pool.getNumIdle() < 0,"Negative expected."); assertTrue( pool.getNumActive() < 0,"Negative expected."); diff --git a/src/test/java/org/apache/commons/pool2/TestBasePoolableObjectFactory.java b/src/test/java/org/apache/commons/pool2/TestBasePoolableObjectFactory.java index 43935d526..ceff13cbe 100644 --- a/src/test/java/org/apache/commons/pool2/TestBasePoolableObjectFactory.java +++ b/src/test/java/org/apache/commons/pool2/TestBasePoolableObjectFactory.java @@ -28,9 +28,9 @@ */ public class TestBasePoolableObjectFactory { - private static class TestFactory extends BasePooledObjectFactory { + private static class TestFactory extends BasePooledObjectFactory { @Override - public AtomicInteger create() throws Exception { + public AtomicInteger create() { return new AtomicInteger(0); } @Override @@ -46,8 +46,8 @@ public PooledObject wrap(final AtomicInteger value) { } @Test - public void testDefaultMethods() throws Exception { - final PooledObjectFactory factory = new TestFactory(); + public void testDefaultMethods() { + final PooledObjectFactory factory = new TestFactory(); factory.activateObject(null); // a no-op factory.passivateObject(null); // a no-op @@ -60,11 +60,11 @@ public void testDefaultMethods() throws Exception { * increments the value. Verify that destroy with no mode does default, * destroy with ABANDONED mode increments. * - * @throws Exception May occur in some failure modes + * @throws RuntimeException May occur in some failure modes */ @Test - public void testDestroyModes() throws Exception { - final PooledObjectFactory factory = new TestFactory(); + public void testDestroyModes() { + final PooledObjectFactory factory = new TestFactory(); final PooledObject pooledObj = factory.makeObject(); final AtomicInteger obj = pooledObj.getObject(); factory.destroyObject(pooledObj); diff --git a/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java index 7fbb573ba..ef955579b 100644 --- a/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java @@ -34,7 +34,7 @@ */ public abstract class TestKeyedObjectPool { - protected static class FailingKeyedPooledObjectFactory implements KeyedPooledObjectFactory { + protected static class FailingKeyedPooledObjectFactory implements KeyedPooledObjectFactory { private final List methodCalls = new ArrayList<>(); private int count; private boolean makeObjectFail; @@ -47,7 +47,7 @@ public FailingKeyedPooledObjectFactory() { } @Override - public void activateObject(final Object key, final PooledObject obj) throws Exception { + public void activateObject(final Object key, final PooledObject obj) { methodCalls.add(new MethodCall("activateObject", key, obj.getObject())); if (activateObjectFail) { throw new PrivateException("activateObject"); @@ -55,7 +55,7 @@ public void activateObject(final Object key, final PooledObject obj) thr } @Override - public void destroyObject(final Object key, final PooledObject obj) throws Exception { + public void destroyObject(final Object key, final PooledObject obj) { methodCalls.add(new MethodCall("destroyObject", key, obj.getObject())); if (destroyObjectFail) { throw new PrivateException("destroyObject"); @@ -91,7 +91,7 @@ public boolean isValidateObjectFail() { } @Override - public PooledObject makeObject(final Object key) throws Exception { + public PooledObject makeObject(final Object key) { final MethodCall call = new MethodCall("makeObject", key); methodCalls.add(call); final int originalCount = this.count++; @@ -106,7 +106,7 @@ public PooledObject makeObject(final Object key) throws Exception { } @Override - public void passivateObject(final Object key, final PooledObject obj) throws Exception { + public void passivateObject(final Object key, final PooledObject obj) { methodCalls.add(new MethodCall("passivateObject", key, obj.getObject())); if (passivateObjectFail) { throw new PrivateException("passivateObject"); @@ -160,10 +160,9 @@ public boolean validateObject(final Object key, final PooledObject obj) } } - private static class TestFactory - extends BaseKeyedPooledObjectFactory { + private static class TestFactory extends BaseKeyedPooledObjectFactory { @Override - public Object create(final Object key) throws Exception { + public Object create(final Object key) { return new Object(); } @Override @@ -174,7 +173,7 @@ public PooledObject wrap(final Object value) { protected static final String KEY = "key"; - private KeyedObjectPool pool; + private KeyedObjectPool pool; // Deliberate choice to create a new object in case future unit tests check // for a specific object. @@ -212,7 +211,7 @@ private void clear(final FailingKeyedPooledObjectFactory factory, final List makeEmptyPool(int minCapacity); + protected abstract KeyedObjectPool makeEmptyPool(int minCapacity); /** * Creates an {@code KeyedObjectPool} with the specified factory. @@ -220,14 +219,16 @@ private void clear(final FailingKeyedPooledObjectFactory factory, final List The type of exception thrown by the pool * @param factory Factory to use to associate with the pool * @return The newly created empty pool */ - protected abstract KeyedObjectPool makeEmptyPool(KeyedPooledObjectFactory factory); + protected abstract KeyedObjectPool makeEmptyPool(KeyedPooledObjectFactory factory); protected abstract Object makeKey(int n); - private void reset(final KeyedObjectPool pool, final FailingKeyedPooledObjectFactory factory, final List expectedMethods) throws Exception { + private void reset(final KeyedObjectPool pool, final FailingKeyedPooledObjectFactory factory, + final List expectedMethods) throws E { pool.clear(); clear(factory, expectedMethods); factory.reset(); @@ -469,8 +470,8 @@ public void testBaseNumActiveNumIdle2() throws Exception { } @Test - public void testClosedPoolBehavior() throws Exception { - final KeyedObjectPool pool; + public void testClosedPoolBehavior() { + final KeyedObjectPool pool; try { pool = makeEmptyPool(new TestFactory()); } catch(final UnsupportedOperationException uoe) { @@ -503,9 +504,9 @@ public void testClosedPoolBehavior() throws Exception { } @Test - public void testKPOFAddObjectUsage() throws Exception { + public void testKPOFAddObjectUsage() { final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory(); - final KeyedObjectPool pool; + final KeyedObjectPool pool; try { pool = makeEmptyPool(factory); } catch(final UnsupportedOperationException uoe) { @@ -541,9 +542,9 @@ public void testKPOFAddObjectUsage() throws Exception { } @Test - public void testKPOFBorrowObjectUsages() throws Exception { + public void testKPOFBorrowObjectUsages() { final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory(); - final KeyedObjectPool pool; + final KeyedObjectPool pool; try { pool = makeEmptyPool(factory); } catch(final UnsupportedOperationException uoe) { @@ -553,7 +554,7 @@ public void testKPOFBorrowObjectUsages() throws Exception { Object obj; if (pool instanceof GenericKeyedObjectPool) { - ((GenericKeyedObjectPool) pool).setTestOnBorrow(true); + ((GenericKeyedObjectPool) pool).setTestOnBorrow(true); } /// Test correct behavior code paths @@ -619,9 +620,9 @@ public void testKPOFBorrowObjectUsages() throws Exception { } @Test - public void testKPOFClearUsages() throws Exception { + public void testKPOFClearUsages() { final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory(); - final KeyedObjectPool pool; + final KeyedObjectPool pool; try { pool = makeEmptyPool(factory); } catch(final UnsupportedOperationException uoe) { @@ -643,9 +644,9 @@ public void testKPOFClearUsages() throws Exception { @Test - public void testKPOFCloseUsages() throws Exception { + public void testKPOFCloseUsages() { final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory(); - KeyedObjectPool pool; + KeyedObjectPool pool; try { pool = makeEmptyPool(factory); } catch (final UnsupportedOperationException uoe) { @@ -658,7 +659,7 @@ public void testKPOFCloseUsages() throws Exception { pool.close(); //// Test exception handling close should swallow failures - try (final KeyedObjectPool pool2 = makeEmptyPool(factory)) { + try (final KeyedObjectPool pool2 = makeEmptyPool(factory)) { reset(pool2, factory, expectedMethods); factory.setDestroyObjectFail(true); pool2.addObjects(KEY, 5); @@ -668,7 +669,7 @@ public void testKPOFCloseUsages() throws Exception { @Test public void testKPOFInvalidateObjectUsages() throws Exception { final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory(); - final KeyedObjectPool pool; + final KeyedObjectPool pool; try { pool = makeEmptyPool(factory); } catch(final UnsupportedOperationException uoe) { @@ -700,9 +701,9 @@ public void testKPOFInvalidateObjectUsages() throws Exception { } @Test - public void testKPOFReturnObjectUsages() throws Exception { + public void testKPOFReturnObjectUsages() { final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory(); - final KeyedObjectPool pool; + final KeyedObjectPool pool; try { pool = makeEmptyPool(factory); } catch(final UnsupportedOperationException uoe) { @@ -759,11 +760,10 @@ public void testKPOFReturnObjectUsages() throws Exception { @Test public void testToString() { - final FailingKeyedPooledObjectFactory factory = - new FailingKeyedPooledObjectFactory(); - try (final KeyedObjectPool pool = makeEmptyPool(factory)) { + final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory(); + try (final KeyedObjectPool pool = makeEmptyPool(factory)) { pool.toString(); - } catch(final UnsupportedOperationException uoe) { + } catch (final UnsupportedOperationException uoe) { return; // test not supported } } diff --git a/src/test/java/org/apache/commons/pool2/TestObjectPool.java b/src/test/java/org/apache/commons/pool2/TestObjectPool.java index b2f4ad04b..cc04b00aa 100644 --- a/src/test/java/org/apache/commons/pool2/TestObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/TestObjectPool.java @@ -43,7 +43,7 @@ static void removeDestroyObjectCall(final List calls) { calls.removeIf(call -> "destroyObject".equals(call.getName())); } - private static void reset(final ObjectPool pool, final MethodCallPoolableObjectFactory factory, final List expectedMethods) throws Exception { + private static void reset(final ObjectPool pool, final MethodCallPoolableObjectFactory factory, final List expectedMethods) throws Exception { pool.clear(); clear(factory, expectedMethods); factory.reset(); @@ -60,6 +60,7 @@ private static void reset(final ObjectPool pool, final MethodCallPoolabl * behaviors described in {@link ObjectPool}. * Generally speaking there should be no limits on the various object counts. * + * @param The exception type throws by the pool * @param factory The factory to be used by the object pool * * @return the newly created empty pool @@ -67,11 +68,11 @@ private static void reset(final ObjectPool pool, final MethodCallPoolabl * @throws UnsupportedOperationException if the pool being tested does not * follow pool contracts. */ - protected abstract ObjectPool makeEmptyPool(PooledObjectFactory factory) throws UnsupportedOperationException; + protected abstract ObjectPool makeEmptyPool(PooledObjectFactory factory) throws UnsupportedOperationException; @Test public void testClosedPoolBehavior() throws Exception { - final ObjectPool pool; + final ObjectPool pool; try { pool = makeEmptyPool(new MethodCallPoolableObjectFactory()); } catch (final UnsupportedOperationException uoe) { @@ -114,7 +115,7 @@ public void testClosedPoolBehavior() throws Exception { @Test public void testPOFAddObjectUsage() throws Exception { final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory(); - final ObjectPool pool; + final ObjectPool pool; try { pool = makeEmptyPool(factory); } catch(final UnsupportedOperationException uoe) { @@ -165,14 +166,14 @@ public void testPOFAddObjectUsage() throws Exception { @Test public void testPOFBorrowObjectUsages() throws Exception { final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory(); - final ObjectPool pool; + final ObjectPool pool; try { pool = makeEmptyPool(factory); } catch (final UnsupportedOperationException uoe) { return; // test not supported } if (pool instanceof GenericObjectPool) { - ((GenericObjectPool) pool).setTestOnBorrow(true); + ((GenericObjectPool) pool).setTestOnBorrow(true); } final List expectedMethods = new ArrayList<>(); Object obj; @@ -237,7 +238,7 @@ public void testPOFBorrowObjectUsages() throws Exception { @Test public void testPOFClearUsages() throws Exception { final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory(); - final ObjectPool pool; + final ObjectPool pool; try { pool = makeEmptyPool(factory); } catch (final UnsupportedOperationException uoe) { @@ -260,7 +261,7 @@ public void testPOFClearUsages() throws Exception { @Test public void testPOFCloseUsages() throws Exception { final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory(); - ObjectPool pool; + ObjectPool pool; try { pool = makeEmptyPool(factory); } catch (final UnsupportedOperationException uoe) { @@ -288,7 +289,7 @@ public void testPOFCloseUsages() throws Exception { @Test public void testPOFInvalidateObjectUsages() throws Exception { final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory(); - final ObjectPool pool; + final ObjectPool pool; try { pool = makeEmptyPool(factory); } catch (final UnsupportedOperationException uoe) { @@ -322,7 +323,7 @@ public void testPOFInvalidateObjectUsages() throws Exception { @Test public void testPOFReturnObjectUsages() throws Exception { final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory(); - final ObjectPool pool; + final ObjectPool pool; try { pool = makeEmptyPool(factory); } catch (final UnsupportedOperationException uoe) { @@ -381,7 +382,7 @@ public void testPOFReturnObjectUsages() throws Exception { @Test public void testToString() { - final ObjectPool pool; + final ObjectPool pool; try { pool = makeEmptyPool(new MethodCallPoolableObjectFactory()); } catch (final UnsupportedOperationException uoe) { diff --git a/src/test/java/org/apache/commons/pool2/TestPoolUtils.java b/src/test/java/org/apache/commons/pool2/TestPoolUtils.java index ceace74ec..ccbcb54ee 100644 --- a/src/test/java/org/apache/commons/pool2/TestPoolUtils.java +++ b/src/test/java/org/apache/commons/pool2/TestPoolUtils.java @@ -100,7 +100,7 @@ private static T createProxy(final Class clazz, final List logger return createProxy(clazz, new MethodCallLogger(logger)); } - private static List invokeEveryMethod(final KeyedObjectPool kop) throws Exception { + private static List invokeEveryMethod(final KeyedObjectPool kop) { kop.addObject(null); kop.borrowObject(null); kop.clear(); @@ -119,7 +119,7 @@ private static List invokeEveryMethod(final KeyedObjectPool List invokeEveryMethod(final KeyedPooledObjectFactory kpof) throws Exception { + private static List invokeEveryMethod(final KeyedPooledObjectFactory kpof) { kpof.activateObject(null, null); kpof.destroyObject(null, null); kpof.makeObject(null); @@ -130,7 +130,7 @@ private static List invokeEveryMethod(final KeyedPooledObjectFact return Arrays.asList("activateObject", "destroyObject", "makeObject", "passivateObject", "validateObject", "toString"); } - private static List invokeEveryMethod(final ObjectPool op) throws Exception { + private static List invokeEveryMethod(final ObjectPool op) { op.addObject(); op.borrowObject(); op.clear(); @@ -145,7 +145,7 @@ private static List invokeEveryMethod(final ObjectPool op) throw "returnObject", "toString"); } - private static List invokeEveryMethod(final PooledObjectFactory pof) throws Exception { + private static List invokeEveryMethod(final PooledObjectFactory pof) throws E { pof.activateObject(null); pof.destroyObject(null); pof.makeObject(); @@ -161,12 +161,12 @@ public void testCheckMinIdleKeyedObjectPool() throws Exception { assertThrows(IllegalArgumentException.class, () -> PoolUtils.checkMinIdle(null, new Object(), 1, 1), "PoolUtils.checkMinIdle(KeyedObjectPool,Object,int,long) must not allow null pool."); try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, (List) null)) { + final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, (List) null)) { assertThrows(IllegalArgumentException.class, () -> PoolUtils.checkMinIdle(pool, (Object) null, 1, 1), "PoolUtils.checkMinIdle(KeyedObjectPool,Object,int,long) must not accept null keys."); } try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, (List) null)) { + final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, (List) null)) { assertThrows(IllegalArgumentException.class, () -> PoolUtils.checkMinIdle(pool, new Object(), -1, 1), "PoolUtils.checkMinIdle(KeyedObjectPool,Object,int,long) must not accept negative min idle values."); } @@ -176,8 +176,8 @@ public void testCheckMinIdleKeyedObjectPool() throws Exception { // Test that the minIdle check doesn't add too many idle objects @SuppressWarnings("unchecked") - final KeyedPooledObjectFactory kpof = createProxy(KeyedPooledObjectFactory.class, calledMethods); - try (final KeyedObjectPool kop = new GenericKeyedObjectPool<>(kpof)) { + final KeyedPooledObjectFactory kpof = createProxy(KeyedPooledObjectFactory.class, calledMethods); + try (final KeyedObjectPool kop = new GenericKeyedObjectPool<>(kpof)) { PoolUtils.checkMinIdle(kop, key, 2, 100); Thread.sleep(400); assertEquals(2, kop.getNumIdle(key)); @@ -201,7 +201,7 @@ public void testCheckMinIdleKeyedObjectPool() throws Exception { try { calledMethods.clear(); try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, calledMethods)) { + final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, calledMethods)) { // checks minIdle immediately final TimerTask task = PoolUtils.checkMinIdle(pool, key, 1, CHECK_PERIOD); @@ -227,7 +227,7 @@ public void testCheckMinIdleKeyedObjectPool() throws Exception { } @Test - public void testCheckMinIdleKeyedObjectPoolKeys() throws Exception { + public void testCheckMinIdleKeyedObjectPoolKeys() throws InterruptedException { // Because this isn't deterministic and you can get false failures, try more than once. AssertionFailedError afe = null; int triesLeft = 3; @@ -235,7 +235,7 @@ public void testCheckMinIdleKeyedObjectPoolKeys() throws Exception { afe = null; final List calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, calledMethods)) { + final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, calledMethods)) { final Collection keys = new ArrayList<>(2); keys.add("one"); keys.add("two"); @@ -265,13 +265,13 @@ public void testCheckMinIdleKeyedObjectPoolKeys() throws Exception { @Test public void testCheckMinIdleKeyedObjectPoolKeysNulls() { try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, (List) null)) { + final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, (List) null)) { assertThrows(IllegalArgumentException.class, () -> PoolUtils.checkMinIdle(pool, (Collection) null, 1, 1), "PoolUtils.checkMinIdle(KeyedObjectPool,Collection,int,long) must not accept null keys."); } try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, (List) null)) { + final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, (List) null)) { PoolUtils.checkMinIdle(pool, (Collection) Collections.emptyList(), 1, 1); } catch (final IllegalArgumentException iae) { fail("PoolUtils.checkMinIdle(KeyedObjectPool,Collection,int,long) must accept empty lists."); @@ -279,11 +279,11 @@ public void testCheckMinIdleKeyedObjectPoolKeysNulls() { } @Test - public void testCheckMinIdleObjectPool() throws Exception { + public void testCheckMinIdleObjectPool() throws InterruptedException { assertThrows(IllegalArgumentException.class, () -> PoolUtils.checkMinIdle(null, 1, 1), "PoolUtils.checkMinIdle(ObjectPool,,) must not allow null pool."); try (@SuppressWarnings("unchecked") - final ObjectPool pool = createProxy(ObjectPool.class, (List) null)) { + final ObjectPool pool = createProxy(ObjectPool.class, (List) null)) { assertThrows(IllegalArgumentException.class, () -> PoolUtils.checkMinIdle(pool, -1, 1), "PoolUtils.checkMinIdle(ObjectPool,,) must not accept negative min idle values."); } @@ -292,8 +292,8 @@ public void testCheckMinIdleObjectPool() throws Exception { // Test that the minIdle check doesn't add too many idle objects @SuppressWarnings("unchecked") - final PooledObjectFactory pof = createProxy(PooledObjectFactory.class, calledMethods); - try (final ObjectPool op = new GenericObjectPool<>(pof)) { + final PooledObjectFactory pof = createProxy(PooledObjectFactory.class, calledMethods); + try (final ObjectPool op = new GenericObjectPool<>(pof)) { PoolUtils.checkMinIdle(op, 2, 100); Thread.sleep(1000); assertEquals(2, op.getNumIdle()); @@ -316,7 +316,7 @@ public void testCheckMinIdleObjectPool() throws Exception { try { calledMethods.clear(); try (@SuppressWarnings("unchecked") - final ObjectPool pool = createProxy(ObjectPool.class, calledMethods)) { + final ObjectPool pool = createProxy(ObjectPool.class, calledMethods)) { final TimerTask task = PoolUtils.checkMinIdle(pool, 1, CHECK_PERIOD); // checks minIdle immediately Thread.sleep(CHECK_SLEEP_PERIOD); // will check CHECK_COUNT more times. @@ -368,8 +368,8 @@ public void testCheckRethrow() { @Test public void testErodingObjectPoolDefaultFactor() { try (@SuppressWarnings("unchecked") - final ObjectPool internalPool = createProxy(ObjectPool.class, (arg0, arg1, arg2) -> null); - final ObjectPool pool = PoolUtils.erodingPool(internalPool)) { + final ObjectPool internalPool = createProxy(ObjectPool.class, (arg0, arg1, arg2) -> null); + final ObjectPool pool = PoolUtils.erodingPool(internalPool)) { final 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 @@ -379,14 +379,14 @@ public void testErodingObjectPoolDefaultFactor() { } @Test - public void testErodingPerKeyKeyedObjectPool() throws Exception { - assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null, 1f, true), + public void testErodingPerKeyKeyedObjectPool() throws InterruptedException { + assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null, 1f, true), "PoolUtils.erodingPool(KeyedObjectPool) must not allow a null pool."); - assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null, 0f, true), + assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null, 0f, true), "PoolUtils.erodingPool(ObjectPool, float, boolean) must not allow a non-positive factor."); - assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null, 1f, true), + assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null, 1f, true), "PoolUtils.erodingPool(KeyedObjectPool, float, boolean) must not allow a null pool."); final List calledMethods = new ArrayList<>(); @@ -405,7 +405,7 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg // If the logic behind PoolUtils.erodingPool changes then this will need to be tweaked. final float factor = 0.01f; // about ~9 seconds until first discard try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), factor, true)) { + final KeyedObjectPool pool = PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), factor, true)) { final List expectedMethods = new ArrayList<>(); assertEquals(expectedMethods, calledMethods); @@ -451,14 +451,14 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg } @Test - public void testErodingPoolKeyedObjectPool() throws Exception { - assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null), + public void testErodingPoolKeyedObjectPool() throws InterruptedException { + assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null), "PoolUtils.erodingPool(KeyedObjectPool) must not allow a null pool."); - assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null, 1f), + assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null, 1f), "PoolUtils.erodingPool(KeyedObjectPool, float) must not allow a null pool."); - assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null, 1f, true), + assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((KeyedObjectPool) null, 1f, true), "PoolUtils.erodingPool(KeyedObjectPool, float, boolean) must not allow a null pool."); final List calledMethods = new ArrayList<>(); @@ -484,7 +484,7 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg final float factor = 0.01f; // about ~9 seconds until first discard final List expectedMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), factor)) { + final KeyedObjectPool pool = PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), factor)) { assertEquals(expectedMethods, calledMethods); @@ -540,9 +540,9 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg @Test public void testErodingPoolKeyedObjectPoolDefaultFactor() { try (@SuppressWarnings("unchecked") - final KeyedObjectPool internalPool = createProxy(KeyedObjectPool.class, + final KeyedObjectPool internalPool = createProxy(KeyedObjectPool.class, (arg0, arg1, arg2) -> null); - final KeyedObjectPool pool = PoolUtils.erodingPool(internalPool)) { + final KeyedObjectPool pool = PoolUtils.erodingPool(internalPool)) { final 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 @@ -553,10 +553,10 @@ public void testErodingPoolKeyedObjectPoolDefaultFactor() { @Test public void testErodingPoolObjectPool() throws Exception { - assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((ObjectPool) null), + assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((ObjectPool) null), "PoolUtils.erodingPool(ObjectPool) must not allow a null pool."); - assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((ObjectPool) null, 1f), + assertThrows(IllegalArgumentException.class, () -> PoolUtils.erodingPool((ObjectPool) null, 1f), "PoolUtils.erodingPool(ObjectPool, float) must not allow a null pool."); final List calledMethods = new ArrayList<>(); @@ -579,7 +579,7 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg final float factor = 0.01f; // about ~9 seconds until first discard final List expectedMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") - final ObjectPool pool = PoolUtils.erodingPool(createProxy(ObjectPool.class, handler), factor)) { + final ObjectPool pool = PoolUtils.erodingPool(createProxy(ObjectPool.class, handler), factor)) { assertEquals(expectedMethods, calledMethods); @@ -641,14 +641,14 @@ public void testPrefillKeyedObjectPool() throws Exception { assertThrows(IllegalArgumentException.class, () -> PoolUtils.prefill(null, new Object(), 1), "PoolUtils.prefill(KeyedObjectPool,Object,int) must not accept null pool."); - try (final KeyedObjectPool pool = new GenericKeyedObjectPool<>(new TestGenericKeyedObjectPool.SimpleFactory<>())) { + try (final KeyedObjectPool pool = new GenericKeyedObjectPool<>(new TestGenericKeyedObjectPool.SimpleFactory<>())) { assertThrows(IllegalArgumentException.class, () -> PoolUtils.prefill(pool, (Object) null, 1), "PoolUtils.prefill(KeyedObjectPool,Object,int) must not accept null key."); } final List calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, calledMethods)) { + final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, calledMethods)) { PoolUtils.prefill(pool, new Object(), 0); final List expectedMethods = new ArrayList<>(); @@ -663,16 +663,16 @@ public void testPrefillKeyedObjectPool() throws Exception { @SuppressWarnings("deprecation") @Test - public void testPrefillKeyedObjectPoolCollection() throws Exception { + public void testPrefillKeyedObjectPoolCollection() { try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, (List) null)) { + final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, (List) null)) { assertThrows(IllegalArgumentException.class, () -> PoolUtils.prefill(pool, (Collection) null, 1), "PoolUtils.prefill(KeyedObjectPool,Collection,int) must not accept null keys."); } final List calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") - final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, calledMethods)) { + final KeyedObjectPool pool = createProxy(KeyedObjectPool.class, calledMethods)) { final Set keys = new HashSet<>(); PoolUtils.prefill(pool, keys, 0); @@ -692,12 +692,12 @@ public void testPrefillKeyedObjectPoolCollection() throws Exception { @SuppressWarnings("deprecation") @Test - public void testPrefillObjectPool() throws Exception { + public void testPrefillObjectPool() { assertThrows(IllegalArgumentException.class, () -> PoolUtils.prefill(null, 1), "PoolUtils.prefill(ObjectPool,int) must not allow null pool."); final List calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") - final ObjectPool pool = createProxy(ObjectPool.class, calledMethods)) { + final ObjectPool pool = createProxy(ObjectPool.class, calledMethods)) { PoolUtils.prefill(pool, 0); final List expectedMethods = new ArrayList<>(); @@ -711,15 +711,16 @@ public void testPrefillObjectPool() throws Exception { } @Test - public void testSynchronizedPoolableFactoryKeyedPoolableObjectFactory() throws Exception { - assertThrows(IllegalArgumentException.class, () -> PoolUtils.synchronizedKeyedPooledFactory((KeyedPooledObjectFactory) null), - "PoolUtils.synchronizedPoolableFactory(KeyedPoolableObjectFactory) must not allow a null factory."); + public void testSynchronizedPoolableFactoryKeyedPoolableObjectFactory() { + assertThrows(IllegalArgumentException.class, + () -> PoolUtils.synchronizedKeyedPooledFactory((KeyedPooledObjectFactory) null), + "PoolUtils.synchronizedPoolableFactory(KeyedPoolableObjectFactory) must not allow a null factory."); final List calledMethods = new ArrayList<>(); @SuppressWarnings("unchecked") - final KeyedPooledObjectFactory kpof = createProxy(KeyedPooledObjectFactory.class, calledMethods); + final KeyedPooledObjectFactory kpof = createProxy(KeyedPooledObjectFactory.class, calledMethods); - final KeyedPooledObjectFactory skpof = PoolUtils.synchronizedKeyedPooledFactory(kpof); + final KeyedPooledObjectFactory skpof = PoolUtils.synchronizedKeyedPooledFactory(kpof); final List expectedMethods = invokeEveryMethod(skpof); assertEquals(expectedMethods, calledMethods); @@ -728,14 +729,14 @@ public void testSynchronizedPoolableFactoryKeyedPoolableObjectFactory() throws E @Test public void testSynchronizedPoolableFactoryPoolableObjectFactory() throws Exception { - assertThrows(IllegalArgumentException.class, () -> PoolUtils.synchronizedPooledFactory((PooledObjectFactory) null), + assertThrows(IllegalArgumentException.class, () -> PoolUtils.synchronizedPooledFactory((PooledObjectFactory) null), "PoolUtils.synchronizedPoolableFactory(PoolableObjectFactory) must not allow a null factory."); final List calledMethods = new ArrayList<>(); @SuppressWarnings("unchecked") - final PooledObjectFactory pof = createProxy(PooledObjectFactory.class, calledMethods); + final PooledObjectFactory pof = createProxy(PooledObjectFactory.class, calledMethods); - final PooledObjectFactory spof = PoolUtils.synchronizedPooledFactory(pof); + final PooledObjectFactory spof = PoolUtils.synchronizedPooledFactory(pof); final List expectedMethods = invokeEveryMethod(spof); assertEquals(expectedMethods, calledMethods); @@ -743,14 +744,14 @@ public void testSynchronizedPoolableFactoryPoolableObjectFactory() throws Except } @Test - public void testSynchronizedPoolKeyedObjectPool() throws Exception { - assertThrows(IllegalArgumentException.class, () -> PoolUtils.synchronizedPool((KeyedObjectPool) null), + public void testSynchronizedPoolKeyedObjectPool() { + assertThrows(IllegalArgumentException.class, () -> PoolUtils.synchronizedPool((KeyedObjectPool) null), "PoolUtils.synchronizedPool(KeyedObjectPool) must not allow a null pool."); final List calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") - final KeyedObjectPool kop = createProxy(KeyedObjectPool.class, calledMethods); - final KeyedObjectPool skop = PoolUtils.synchronizedPool(kop)) { + final KeyedObjectPool kop = createProxy(KeyedObjectPool.class, calledMethods); + final KeyedObjectPool skop = PoolUtils.synchronizedPool(kop)) { final List expectedMethods = invokeEveryMethod(skop); assertEquals(expectedMethods, calledMethods); } @@ -759,13 +760,13 @@ public void testSynchronizedPoolKeyedObjectPool() throws Exception { } @Test - public void testSynchronizedPoolObjectPool() throws Exception { - assertThrows(IllegalArgumentException.class, () -> PoolUtils.synchronizedPool((ObjectPool) null), + public void testSynchronizedPoolObjectPool() { + assertThrows(IllegalArgumentException.class, () -> PoolUtils.synchronizedPool((ObjectPool) null), "PoolUtils.synchronizedPool(ObjectPool) must not allow a null pool."); final List calledMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") - final ObjectPool op = createProxy(ObjectPool.class, calledMethods); final ObjectPool sop = PoolUtils.synchronizedPool(op)) { + final ObjectPool op = createProxy(ObjectPool.class, calledMethods); final ObjectPool sop = PoolUtils.synchronizedPool(op)) { final List expectedMethods = invokeEveryMethod(sop); assertEquals(expectedMethods, calledMethods); diff --git a/src/test/java/org/apache/commons/pool2/VisitTrackerFactory.java b/src/test/java/org/apache/commons/pool2/VisitTrackerFactory.java index fd2844bb0..94497a829 100644 --- a/src/test/java/org/apache/commons/pool2/VisitTrackerFactory.java +++ b/src/test/java/org/apache/commons/pool2/VisitTrackerFactory.java @@ -24,19 +24,19 @@ * */ public class VisitTrackerFactory - implements PooledObjectFactory>, KeyedPooledObjectFactory> { + implements PooledObjectFactory, RuntimeException>, KeyedPooledObjectFactory, RuntimeException> { private int nextId; public VisitTrackerFactory() { } @Override - public void activateObject(final K key, final PooledObject> ref) throws Exception { + public void activateObject(final K key, final PooledObject> ref) { ref.getObject().activate(); } @Override - public void activateObject(final PooledObject> ref) throws Exception { + public void activateObject(final PooledObject> ref) { ref.getObject().activate(); } @@ -61,12 +61,12 @@ public PooledObject> makeObject(final K key) { } @Override - public void passivateObject(final K key, final PooledObject> ref) throws Exception { + public void passivateObject(final K key, final PooledObject> ref) { ref.getObject().passivate(); } @Override - public void passivateObject(final PooledObject> ref) throws Exception { + public void passivateObject(final PooledObject> ref) { ref.getObject().passivate(); } diff --git a/src/test/java/org/apache/commons/pool2/WaiterFactory.java b/src/test/java/org/apache/commons/pool2/WaiterFactory.java index 05d76619e..3a1903914 100644 --- a/src/test/java/org/apache/commons/pool2/WaiterFactory.java +++ b/src/test/java/org/apache/commons/pool2/WaiterFactory.java @@ -29,10 +29,10 @@ * If the factory's maxActive / maxActivePerKey are set to match those of the * pool, makeObject will throw IllegalStateException if the number of makes - destroys * (per key) exceeds the configured max. - * + * + * @param The type of keys managed by this factory. */ -public class WaiterFactory implements PooledObjectFactory, -KeyedPooledObjectFactory { +public class WaiterFactory implements PooledObjectFactory, KeyedPooledObjectFactory { /** Latency of activateObject */ private final long activateLatency; @@ -97,18 +97,18 @@ public WaiterFactory(final long activateLatency, final long destroyLatency, } @Override - public void activateObject(final K key, final PooledObject obj) throws Exception { + public void activateObject(final K key, final PooledObject obj) { activateObject(obj); } @Override - public void activateObject(final PooledObject obj) throws Exception { + public void activateObject(final PooledObject obj) { doWait(activateLatency); obj.getObject().setActive(true); } @Override - public void destroyObject(final K key,final PooledObject obj) throws Exception { + public void destroyObject(final K key,final PooledObject obj) { destroyObject(obj); synchronized (this) { final Integer count = activeCounts.get(key); @@ -117,7 +117,7 @@ public void destroyObject(final K key,final PooledObject obj) throws Exc } @Override - public void destroyObject(final PooledObject obj) throws Exception { + public void destroyObject(final PooledObject obj) { doWait(destroyLatency); obj.getObject().setValid(false); obj.getObject().setActive(false); @@ -142,7 +142,7 @@ public synchronized long getMaxActive() { } @Override - public PooledObject makeObject() throws Exception { + public PooledObject makeObject() { // Increment and test *before* make synchronized (this) { if (activeCount >= maxActive) { @@ -156,7 +156,7 @@ public PooledObject makeObject() throws Exception { } @Override - public PooledObject makeObject(final K key) throws Exception { + public PooledObject makeObject(final K key) { synchronized (this) { Integer count = activeCounts.get(key); if (count == null) { @@ -178,12 +178,12 @@ public PooledObject makeObject(final K key) throws Exception { // KeyedPoolableObjectFactory methods @Override - public void passivateObject(final K key, final PooledObject obj) throws Exception { + public void passivateObject(final K key, final PooledObject obj) { passivateObject(obj); } @Override - public void passivateObject(final PooledObject obj) throws Exception { + public void passivateObject(final PooledObject obj) { obj.getObject().setActive(false); doWait(passivateLatency); if (Math.random() < passivateInvalidationProbability) { diff --git a/src/test/java/org/apache/commons/pool2/impl/AtomicIntegerFactory.java b/src/test/java/org/apache/commons/pool2/impl/AtomicIntegerFactory.java index 2f66560ae..e970765cd 100644 --- a/src/test/java/org/apache/commons/pool2/impl/AtomicIntegerFactory.java +++ b/src/test/java/org/apache/commons/pool2/impl/AtomicIntegerFactory.java @@ -30,7 +30,7 @@ * */ public class AtomicIntegerFactory - extends BasePooledObjectFactory { + extends BasePooledObjectFactory { private long activateLatency; private long passivateLatency; diff --git a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedKeyedObjectPool.java index 7e93c5bd1..3c2f23d84 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedKeyedObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedKeyedObjectPool.java @@ -78,7 +78,7 @@ public void run() { } } - private static class SimpleFactory implements KeyedPooledObjectFactory { + private static class SimpleFactory implements KeyedPooledObjectFactory { private final long destroyLatency; private final long validateLatency; @@ -99,12 +99,12 @@ public void activateObject(final Integer key, final PooledObject obj) throws Exception { + public void destroyObject(final Integer key, final PooledObject obj) throws InterruptedException { destroyObject(key, obj, DestroyMode.NORMAL); } @Override - public void destroyObject(final Integer key, final PooledObject obj, final DestroyMode destroyMode) throws Exception { + public void destroyObject(final Integer key, final PooledObject obj, final DestroyMode destroyMode) throws InterruptedException { obj.getObject().setActive(false); // while destroying instances, yield control to other threads // helps simulate threading errors @@ -132,7 +132,7 @@ public boolean validateObject(final Integer key, final PooledObject pool; + private GenericKeyedObjectPool pool; private AbandonedConfig abandonedConfig; @@ -211,6 +211,9 @@ public void testAbandonedInvalidate() throws Exception { obj = pool.borrowObject(0); } Thread.sleep(1000); // abandon checked out instances and let evictor start + if (!pool.getKeys().contains(0)) { + Thread.sleep(1000); // Wait a little more. + } pool.invalidateObject(0, obj); // Should not trigger another destroy / decrement Thread.sleep(2000); // give evictor time to finish destroys assertEquals(0, pool.getNumActive()); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java index bae22abec..b403f9049 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java @@ -158,7 +158,7 @@ public void run() { } } - private static class SimpleFactory implements PooledObjectFactory { + private static class SimpleFactory implements PooledObjectFactory { private final long destroyLatency; private final long validateLatency; @@ -179,12 +179,12 @@ public void activateObject(final PooledObject obj) { } @Override - public void destroyObject(final PooledObject obj) throws Exception { + public void destroyObject(final PooledObject obj) throws InterruptedException { destroyObject(obj, DestroyMode.NORMAL); } @Override - public void destroyObject(final PooledObject obj, final DestroyMode destroyMode) throws Exception { + public void destroyObject(final PooledObject obj, final DestroyMode destroyMode) throws InterruptedException { obj.getObject().setActive(false); // while destroying instances, yield control to other threads // helps simulate threading errors @@ -212,7 +212,7 @@ public boolean validateObject(final PooledObject obj) { } } - private GenericObjectPool pool; + private GenericObjectPool pool; private AbandonedConfig abandonedConfig; diff --git a/src/test/java/org/apache/commons/pool2/impl/TestBaseGenericObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestBaseGenericObjectPool.java index d36fd1744..8bd820aa2 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestBaseGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestBaseGenericObjectPool.java @@ -31,7 +31,7 @@ */ public class TestBaseGenericObjectPool { - BaseGenericObjectPool pool; + BaseGenericObjectPool pool; SimpleFactory factory; @BeforeEach @@ -83,7 +83,7 @@ public void testBorrowWaitStatisticsMax() { public void testEvictionTimerMultiplePools() throws InterruptedException { final AtomicIntegerFactory factory = new AtomicIntegerFactory(); factory.setValidateLatency(50); - try (final GenericObjectPool evictingPool = new GenericObjectPool<>(factory)) { + try (final GenericObjectPool evictingPool = new GenericObjectPool<>(factory)) { evictingPool.setTimeBetweenEvictionRuns(Duration.ofMillis(100)); evictingPool.setNumTestsPerEvictionRun(5); evictingPool.setTestWhileIdle(true); @@ -97,7 +97,7 @@ public void testEvictionTimerMultiplePools() throws InterruptedException { } for (int i = 0; i < 1000; i++) { - try (final GenericObjectPool nonEvictingPool = new GenericObjectPool<>(factory)) { + try (final GenericObjectPool nonEvictingPool = new GenericObjectPool<>(factory)) { // empty } } diff --git a/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java b/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java index fd05ad49c..897765f42 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java @@ -34,7 +34,7 @@ public void testGetLastBorrowTrace() throws Exception { abandonedConfig.setRemoveAbandonedOnBorrow(true); abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); abandonedConfig.setLogAbandoned(true); - try (final GenericObjectPool pool = new GenericObjectPool<>(new SimpleFactory(), + try (final GenericObjectPool pool = new GenericObjectPool<>(new SimpleFactory(), new GenericObjectPoolConfig<>(), abandonedConfig)) { pool.borrowObject(); @@ -50,7 +50,7 @@ public void testGetLastBorrowTrace() throws Exception { @Test public void testGetPooledObjectToString() throws Exception { - try (final GenericObjectPool pool = new GenericObjectPool<>(new SimpleFactory())) { + try (final GenericObjectPool pool = new GenericObjectPool<>(new SimpleFactory())) { final String s1 = pool.borrowObject(); @@ -66,7 +66,7 @@ public void testGetPooledObjectToString() throws Exception { @Test public void testGetPooledObjectType() throws Exception { - try (final GenericObjectPool pool = new GenericObjectPool<>(new SimpleFactory())) { + try (final GenericObjectPool pool = new GenericObjectPool<>(new SimpleFactory())) { pool.borrowObject(); @@ -82,7 +82,7 @@ public void testGetPooledObjectType() throws Exception { @Test public void testTiming() throws Exception { - try (final GenericObjectPool pool = new GenericObjectPool<>(new SimpleFactory())) { + try (final GenericObjectPool pool = new GenericObjectPool<>(new SimpleFactory())) { final long t1Millis = System.currentTimeMillis(); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestEvictionTimer.java b/src/test/java/org/apache/commons/pool2/impl/TestEvictionTimer.java index a45e189ff..f904ff59d 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestEvictionTimer.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestEvictionTimer.java @@ -38,10 +38,10 @@ public class TestEvictionTimer { @Test public void testStartStopEvictionTimer() throws Exception { - try (final GenericObjectPool pool = new GenericObjectPool<>(new BasePooledObjectFactory() { + try (final GenericObjectPool pool = new GenericObjectPool<>(new BasePooledObjectFactory() { @Override - public String create() throws Exception { + public String create() { return null; } @@ -52,7 +52,7 @@ public PooledObject wrap(final String obj) { })) { // Start evictor #1 - final BaseGenericObjectPool.Evictor evictor1 = pool.new Evictor(); + final BaseGenericObjectPool.Evictor evictor1 = pool.new Evictor(); EvictionTimer.schedule(evictor1, TestConstants.ONE_MINUTE_DURATION, TestConstants.ONE_MINUTE_DURATION); // Assert that eviction objects are correctly allocated @@ -70,7 +70,7 @@ public PooledObject wrap(final String obj) { assertEquals(1, EvictionTimer.getNumTasks()); // Start evictor #2 - final BaseGenericObjectPool.Evictor evictor2 = pool.new Evictor(); + final BaseGenericObjectPool.Evictor evictor2 = pool.new Evictor(); EvictionTimer.schedule(evictor2, TestConstants.ONE_MINUTE_DURATION, TestConstants.ONE_MINUTE_DURATION); // Assert that eviction objects are correctly allocated diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java index e5e3d471f..662884b8d 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java @@ -79,10 +79,9 @@ public Thread newThread(final Runnable r) { } } - private static class DummyFactory - extends BaseKeyedPooledObjectFactory { + private static class DummyFactory extends BaseKeyedPooledObjectFactory { @Override - public Object create(final Object key) throws Exception { + public Object create(final Object key) { return null; } @Override @@ -97,9 +96,9 @@ public PooledObject wrap(final Object value) { * 1) Instances are mutable and mutation can cause change in identity / hashcode. */ private static final class HashSetFactory - extends BaseKeyedPooledObjectFactory> { + extends BaseKeyedPooledObjectFactory, RuntimeException> { @Override - public HashSet create(final String key) throws Exception { + public HashSet create(final String key) { return new HashSet<>(); } @Override @@ -112,11 +111,13 @@ public PooledObject> wrap(final HashSet value) { * Attempts to invalidate an object, swallowing IllegalStateException. */ static class InvalidateThread implements Runnable { + private final String obj; - private final KeyedObjectPool pool; + private final KeyedObjectPool pool; private final String key; private boolean done; - public InvalidateThread(final KeyedObjectPool pool, final String key, final String obj) { + + public InvalidateThread(final KeyedObjectPool pool, final String key, final String obj) { this.obj = obj; this.pool = pool; this.key = key; @@ -139,11 +140,10 @@ public void run() { } private static class ObjectFactory - extends BaseKeyedPooledObjectFactory { + extends BaseKeyedPooledObjectFactory { @Override - public Object create(final Integer key) - throws Exception { + public Object create(final Integer key) { return new Object(); } @@ -152,7 +152,7 @@ public PooledObject wrap(final Object value) { return new DefaultPooledObject<>(value); } } - public static class SimpleFactory implements KeyedPooledObjectFactory { + public static class SimpleFactory implements KeyedPooledObjectFactory { volatile int counter; final boolean valid; int activeCount; @@ -271,12 +271,11 @@ public boolean validateObject(final K key, final PooledObject obj) { return valid; } } - private static class SimplePerKeyFactory - extends BaseKeyedPooledObjectFactory { - final ConcurrentHashMap map = - new ConcurrentHashMap<>(); + private static class SimplePerKeyFactory extends BaseKeyedPooledObjectFactory { + final ConcurrentHashMap map = new ConcurrentHashMap<>(); + @Override - public Object create(final Object key) throws Exception { + public Object create(final Object key) { int counter = 0; final AtomicInteger Counter = map.get(key); if(null != Counter) { @@ -287,6 +286,7 @@ public Object create(final Object key) throws Exception { } return String.valueOf(key) + String.valueOf(counter); } + @Override public PooledObject wrap(final Object value) { return new DefaultPooledObject<>(value); @@ -296,11 +296,11 @@ public PooledObject wrap(final Object value) { * Very simple test thread that just tries to borrow an object from * the provided pool with the specified key and returns it */ - static class SimpleTestThread implements Runnable { - private final KeyedObjectPool pool; + static class SimpleTestThread implements Runnable { + private final KeyedObjectPool pool; private final String key; - public SimpleTestThread(final KeyedObjectPool pool, final String key) { + public SimpleTestThread(final KeyedObjectPool pool, final String key) { this.pool = pool; this.key = key; } @@ -337,11 +337,11 @@ public boolean evict(final EvictionConfig config, final PooledObject underTes } } - static class TestThread implements Runnable { + static class TestThread implements Runnable { private final java.util.Random random = new java.util.Random(); /** GKOP to hit */ - private final KeyedObjectPool pool; + private final KeyedObjectPool pool; /** number of borrow/return iterations */ private final int iter; /** delay before borrow */ @@ -359,19 +359,19 @@ static class TestThread implements Runnable { private volatile boolean failed; private volatile Exception exception; - public TestThread(final KeyedObjectPool pool) { + public TestThread(final KeyedObjectPool pool) { this(pool, 100, 50, 50, true, null, null); } - public TestThread(final KeyedObjectPool pool, final int iter) { + public TestThread(final KeyedObjectPool pool, final int iter) { this(pool, iter, 50, 50, true, null, null); } - public TestThread(final KeyedObjectPool pool, final int iter, final int delay) { + public TestThread(final KeyedObjectPool pool, final int iter, final int delay) { this(pool, iter, delay, delay, true, null, null); } - public TestThread(final KeyedObjectPool pool, final int iter, final int startDelay, + public TestThread(final KeyedObjectPool pool, final int iter, final int startDelay, final int holdTime, final boolean randomDelay, final T expectedObject, final String key) { this.pool = pool; this.iter = iter; @@ -431,8 +431,8 @@ public void run() { * Very simple test thread that just tries to borrow an object from * the provided pool with the specified key and returns it after a wait */ - static class WaitingTestThread extends Thread { - private final KeyedObjectPool pool; + static class WaitingTestThread extends Thread { + private final KeyedObjectPool pool; private final String key; private final long pause; private Throwable thrown; @@ -443,7 +443,7 @@ static class WaitingTestThread extends Thread { private long endedMillis; private String objectId; - public WaitingTestThread(final KeyedObjectPool pool, final String key, final long pause) { + public WaitingTestThread(final KeyedObjectPool pool, final String key, final long pause) { this.pool = pool; this.key = key; this.pause = pause; @@ -481,14 +481,14 @@ public void run() { // @see https://issues.apache.org/jira/browse/SUREFIRE-121 /** setUp(): {@code new GenericKeyedObjectPool(factory)} */ - private GenericKeyedObjectPool gkoPool; + private GenericKeyedObjectPool gkoPool; /** setUp(): {@code new SimpleFactory()} */ private SimpleFactory simpleFactory; private void checkEvictionOrder(final boolean lifo) throws Exception { final SimpleFactory intFactory = new SimpleFactory<>(); - try (final GenericKeyedObjectPool intPool = new GenericKeyedObjectPool<>(intFactory)) { + try (final GenericKeyedObjectPool intPool = new GenericKeyedObjectPool<>(intFactory)) { intPool.setNumTestsPerEvictionRun(2); intPool.setMinEvictableIdleTime(Duration.ofMillis(100)); intPool.setLifo(lifo); @@ -599,7 +599,7 @@ private void checkEvictionOrder(final boolean lifo) throws Exception { private void checkEvictorVisiting(final boolean lifo) throws Exception { VisitTrackerFactory trackerFactory = new VisitTrackerFactory<>(); - try (GenericKeyedObjectPool> intPool = new GenericKeyedObjectPool<>( + try (GenericKeyedObjectPool, RuntimeException> intPool = new GenericKeyedObjectPool<>( trackerFactory)) { intPool.setNumTestsPerEvictionRun(2); intPool.setMinEvictableIdleTime(Duration.ofMillis(-1)); @@ -683,7 +683,7 @@ private void checkEvictorVisiting(final boolean lifo) throws Exception { // Can't use clear as some objects are still active so create // a new pool trackerFactory = new VisitTrackerFactory<>(); - try (GenericKeyedObjectPool> intPool = new GenericKeyedObjectPool<>( + try (GenericKeyedObjectPool, RuntimeException> intPool = new GenericKeyedObjectPool<>( trackerFactory)) { intPool.setMaxIdlePerKey(-1); intPool.setMaxTotalPerKey(-1); @@ -799,19 +799,18 @@ protected boolean isLifo() { return true; } + @SuppressWarnings("unchecked") @Override - protected KeyedObjectPool makeEmptyPool(final int minCapacity) { - final KeyedPooledObjectFactory perKeyFactory = - new SimplePerKeyFactory(); - final GenericKeyedObjectPool perKeyPool = - new GenericKeyedObjectPool<>(perKeyFactory); + protected KeyedObjectPool makeEmptyPool(final int minCapacity) { + final KeyedPooledObjectFactory perKeyFactory = new SimplePerKeyFactory(); + final GenericKeyedObjectPool perKeyPool = new GenericKeyedObjectPool<>(perKeyFactory); perKeyPool.setMaxTotalPerKey(minCapacity); perKeyPool.setMaxIdlePerKey(minCapacity); - return perKeyPool; + return (KeyedObjectPool) perKeyPool; } @Override - protected KeyedObjectPool makeEmptyPool(final KeyedPooledObjectFactory fac) { + protected KeyedObjectPool makeEmptyPool(final KeyedPooledObjectFactory fac) { return new GenericKeyedObjectPool<>(fac); } @@ -831,15 +830,15 @@ protected Object makeKey(final int n) { * @param delay Maximum delay between iterations * @param gkopPool The keyed object pool to use */ - public void runTestThreads(final int numThreads, final int iterations, final int delay, final GenericKeyedObjectPool gkopPool) { - final ArrayList> threads = new ArrayList<>(); + public void runTestThreads(final int numThreads, final int iterations, final int delay, final GenericKeyedObjectPool gkopPool) { + final ArrayList> threads = new ArrayList<>(); for(int i=0;i testThread = new TestThread<>(gkopPool, iterations, delay); + final TestThread testThread = new TestThread<>(gkopPool, iterations, delay); threads.add(testThread); final Thread t = new Thread(testThread); t.start(); } - for (final TestThread testThread : threads) { + for (final TestThread testThread : threads) { while(!(testThread.complete())) { Waiter.sleepQuietly(500L); } @@ -890,7 +889,7 @@ public void tearDownJmx() throws Exception { public void testAppendStats() { assertFalse(gkoPool.getMessageStatistics()); assertEquals("foo", (gkoPool.appendStats("foo"))); - try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(new SimpleFactory<>())) { + try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(new SimpleFactory<>())) { pool.setMessagesStatistics(true); assertNotEquals("foo", (pool.appendStats("foo"))); pool.setMessagesStatistics(false); @@ -1025,7 +1024,7 @@ public void testClear() throws Exception { public void testClearOldest() throws Exception { // Make destroy have some latency so clearOldest takes some time final WaiterFactory waiterFactory = new WaiterFactory<>(0, 20, 0, 0, 0, 0, 50, 5, 0); - try (final GenericKeyedObjectPool waiterPool = new GenericKeyedObjectPool<>(waiterFactory)) { + try (final GenericKeyedObjectPool waiterPool = new GenericKeyedObjectPool<>(waiterFactory)) { waiterPool.setMaxTotalPerKey(5); waiterPool.setMaxTotal(50); waiterPool.setLifo(false); @@ -1041,7 +1040,7 @@ public void testClearOldest() throws Exception { // Now set up a race - one thread wants a new instance, triggering clearOldest // Other goes after an element on death row // See if we end up with dead man walking - final SimpleTestThread t2 = new SimpleTestThread<>(waiterPool, "51"); + final SimpleTestThread t2 = new SimpleTestThread<>(waiterPool, "51"); final Thread thread2 = new Thread(t2); thread2.start(); // Triggers clearOldest, killing all of the 0's and the 2 oldest 1's Thread.sleep(50); // Wait for clearOldest to kick off, but not long enough to reach the 1's @@ -1119,25 +1118,25 @@ public void testClearUnblocksWaiters() { config.setMaxIdlePerKey(-1); config.setMaxTotal(-1); config.setMaxWait(Duration.ofMillis(5)); - GenericKeyedObjectPool testPool = new GenericKeyedObjectPool<>( - new KeyedPooledObjectFactory() { + GenericKeyedObjectPool testPool = new GenericKeyedObjectPool<>( + new KeyedPooledObjectFactory() { @Override - public void activateObject(Integer key, PooledObject p) throws Exception { + public void activateObject(Integer key, PooledObject p) { // do nothing } @Override - public void destroyObject(Integer key, PooledObject p) throws Exception { + public void destroyObject(Integer key, PooledObject p) throws InterruptedException { Thread.sleep(500); } @Override - public PooledObject makeObject(Integer key) throws Exception { + public PooledObject makeObject(Integer key) { return new DefaultPooledObject<>(10); } @Override - public void passivateObject(Integer key, PooledObject p) throws Exception { + public void passivateObject(Integer key, PooledObject p) { // do nothing } @@ -1178,7 +1177,7 @@ public void testClientWaitStats() throws Exception { final SimpleFactory factory = new SimpleFactory<>(); // Give makeObject a little latency factory.setMakeLatency(200); - try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory, + try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory, new GenericKeyedObjectPoolConfig<>())) { final String s = pool.borrowObject("one"); // First borrow waits on create, so wait time should be at least 200 ms @@ -1233,7 +1232,7 @@ public void testConcurrentInvalidate() throws Exception { // Launch nThreads threads all trying to invalidate the target for (int i = 0; i < nThreads; i++) { threads[i] = - new InvalidateThread(gkoPool,key, obj[targ.intValue()]); + new InvalidateThread(gkoPool, key, obj[targ.intValue()]); } for (int i = 0; i < nThreads; i++) { new Thread(threads[i]).start(); @@ -1277,9 +1276,9 @@ public void testConstructors() { final long timeBetweenEvictionRunsMillis = 8; final boolean blockWhenExhausted = false; final boolean lifo = false; - final KeyedPooledObjectFactory dummyFactory = new DummyFactory(); + final KeyedPooledObjectFactory dummyFactory = new DummyFactory(); - try (GenericKeyedObjectPool objPool = new GenericKeyedObjectPool<>(dummyFactory)) { + try (GenericKeyedObjectPool objPool = new GenericKeyedObjectPool<>(dummyFactory)) { assertEquals(GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL_PER_KEY, objPool.getMaxTotalPerKey()); assertEquals(GenericKeyedObjectPoolConfig.DEFAULT_MAX_IDLE_PER_KEY, objPool.getMaxIdlePerKey()); assertEquals(BaseObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS, objPool.getMaxWaitMillis()); @@ -1327,7 +1326,7 @@ public void testConstructors() { config.setTestWhileIdle(testWhileIdle); config.setTimeBetweenEvictionRuns(Duration.ofMillis(timeBetweenEvictionRunsMillis)); config.setBlockWhenExhausted(blockWhenExhausted); - try (GenericKeyedObjectPool objPool = new GenericKeyedObjectPool<>(dummyFactory, config)) { + try (GenericKeyedObjectPool objPool = new GenericKeyedObjectPool<>(dummyFactory, config)) { assertEquals(maxTotalPerKey, objPool.getMaxTotalPerKey()); assertEquals(maxIdle, objPool.getMaxIdlePerKey()); assertEquals(maxWaitDuration, objPool.getMaxWaitDuration()); @@ -1359,7 +1358,7 @@ public void testContructorEvictionConfig() throws Exception { config.setTimeBetweenEvictionRuns(Duration.ofMillis(500)); config.setMinEvictableIdleTime(Duration.ofMillis(50)); config.setNumTestsPerEvictionRun(5); - try (final GenericKeyedObjectPool p = new GenericKeyedObjectPool<>(simpleFactory, config)) { + try (final GenericKeyedObjectPool p = new GenericKeyedObjectPool<>(simpleFactory, config)) { for (int i = 0; i < 5; i++) { p.addObject("one"); } @@ -1379,7 +1378,7 @@ public void testContructorEvictionConfig() throws Exception { @Test public void testEqualsIndiscernible() throws Exception { final HashSetFactory factory = new HashSetFactory(); - try (final GenericKeyedObjectPool> pool = new GenericKeyedObjectPool<>(factory, + try (final GenericKeyedObjectPool, RuntimeException> pool = new GenericKeyedObjectPool<>(factory, new GenericKeyedObjectPoolConfig<>())) { final HashSet s1 = pool.borrowObject("a"); final HashSet s2 = pool.borrowObject("a"); @@ -1678,7 +1677,7 @@ public void testGetStatsString() { @Test public void testInvalidateFreesCapacity() throws Exception { final SimpleFactory factory = new SimpleFactory<>(); - try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory)) { + try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory)) { pool.setMaxTotalPerKey(2); pool.setMaxWaitMillis(500); // Borrow an instance and hold if for 5 seconds @@ -1735,8 +1734,7 @@ public void testInvalidateWaiting() config.setTestWhileIdle(true); config.setTimeBetweenEvictionRuns(Duration.ofMillis(-1)); - try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(new ObjectFactory(), - config)) { + try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(new ObjectFactory(), config)) { // Allocate both objects with this thread pool.borrowObject(Integer.valueOf(1)); // object1 @@ -1843,7 +1841,7 @@ public void testLivenessPerKey() throws Exception { @Test public void testMakeObjectException() throws Exception { final SimpleFactory factory = new SimpleFactory<>(); - try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory)) { + try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory)) { pool.setMaxTotalPerKey(1); pool.setBlockWhenExhausted(false); factory.exceptionOnCreate = true; @@ -1862,7 +1860,7 @@ public void testMaxActivePerKeyExceeded() { final WaiterFactory waiterFactory = new WaiterFactory<>(0, 20, 0, 0, 0, 0, 8, 5, 0); // TODO Fix this. Can't use local pool since runTestThreads uses the // protected pool field - try (final GenericKeyedObjectPool waiterPool = new GenericKeyedObjectPool<>(waiterFactory)) { + try (final GenericKeyedObjectPool waiterPool = new GenericKeyedObjectPool<>(waiterFactory)) { waiterPool.setMaxTotalPerKey(5); waiterPool.setMaxTotal(8); waiterPool.setTestOnBorrow(true); @@ -2054,8 +2052,8 @@ public void testMaxTotalWithThreads() throws Exception { final int holdTime = 2000; - final TestThread testA = new TestThread<>(gkoPool, 1, 0, holdTime, false, null, "a"); - final TestThread testB = new TestThread<>(gkoPool, 1, 0, holdTime, false, null, "b"); + final TestThread testA = new TestThread<>(gkoPool, 1, 0, holdTime, false, null, "a"); + final TestThread testB = new TestThread<>(gkoPool, 1, 0, holdTime, false, null, "b"); final Thread threadA = new Thread(testA); final Thread threadB = new Thread(testB); @@ -2276,7 +2274,7 @@ public void testMinIdleNoPreparePool() throws Exception { @Test public void testMultipleReturn() throws Exception { final WaiterFactory factory = new WaiterFactory<>(0, 0, 0, 0, 0, 0); - try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory)) { + try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory)) { pool.setTestOnReturn(true); final Waiter waiter = pool.borrowObject("a"); pool.returnObject("a", waiter); @@ -2302,7 +2300,7 @@ public void testMultipleReturn() throws Exception { @Test public void testMutable() throws Exception { final HashSetFactory factory = new HashSetFactory(); - try (final GenericKeyedObjectPool> pool = new GenericKeyedObjectPool<>(factory, + try (final GenericKeyedObjectPool, RuntimeException> pool = new GenericKeyedObjectPool<>(factory, new GenericKeyedObjectPoolConfig<>())) { final HashSet s1 = pool.borrowObject("a"); final HashSet s2 = pool.borrowObject("a"); @@ -2376,7 +2374,7 @@ public void testNumActiveNumIdle2() throws Exception { @Test public void testReturnObjectThrowsIllegalStateException() { - try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(new SimpleFactory<>())) { + try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(new SimpleFactory<>())) { assertThrows(IllegalStateException.class, () -> pool.returnObject("Foo", "Bar")); } @@ -2393,8 +2391,8 @@ public void testReturnObjectWithBlockWhenExhausted() throws Exception { gkoPool.returnObject("0", obj); // Test return object with a take waiter - final TestThread testA = new TestThread<>(gkoPool, 1, 0, 500, false, null, "0"); - final TestThread testB = new TestThread<>(gkoPool, 1, 0, 0, false, null, "1"); + final TestThread testA = new TestThread<>(gkoPool, 1, 0, 500, false, null, "0"); + final TestThread testB = new TestThread<>(gkoPool, 1, 0, 0, false, null, "1"); final Thread threadA = new Thread(testA); final Thread threadB = new Thread(testB); threadA.start(); @@ -2425,7 +2423,7 @@ public void testReturnToHead() throws Exception { final SimpleFactory factory = new SimpleFactory<>(); factory.setValidateLatency(100); factory.setValid(true); // Validation always succeeds - try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory)) { + try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory)) { pool.setMaxWaitMillis(1000); pool.setTestWhileIdle(true); pool.setMaxTotalPerKey(2); @@ -2572,7 +2570,7 @@ public void testValidationFailureOnReturnFreesCapacity() final SimpleFactory factory = new SimpleFactory<>(); factory.setValid(false); // Validate will always fail factory.setValidationEnabled(true); - try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory)) { + try (final GenericKeyedObjectPool pool = new GenericKeyedObjectPool<>(factory)) { pool.setMaxTotalPerKey(2); pool.setMaxWaitMillis(1500); pool.setTestOnReturn(true); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java index 9f104948e..afb7dae60 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java @@ -92,12 +92,12 @@ public void run() { } } - private static class CreateErrorFactory extends BasePooledObjectFactory { + private static class CreateErrorFactory extends BasePooledObjectFactory { private final Semaphore semaphore = new Semaphore(0); @Override - public String create() throws Exception { + public String create() throws InterruptedException { semaphore.acquire(); throw new UnknownError("wiggle"); } @@ -116,12 +116,12 @@ public PooledObject wrap(final String obj) { } } - private static class CreateFailFactory extends BasePooledObjectFactory { + private static class CreateFailFactory extends BasePooledObjectFactory { private final Semaphore semaphore = new Semaphore(0); @Override - public String create() throws Exception { + public String create() throws InterruptedException { semaphore.acquire(); throw new UnsupportedCharsetException("wibble"); } @@ -141,9 +141,9 @@ public PooledObject wrap(final String obj) { } private static final class DummyFactory - extends BasePooledObjectFactory { + extends BasePooledObjectFactory { @Override - public Object create() throws Exception { + public Object create() { return null; } @Override @@ -152,11 +152,11 @@ public PooledObject wrap(final Object value) { } } - private static class EvictionThread extends Thread { + private static class EvictionThread extends Thread { - private final GenericObjectPool pool; + private final GenericObjectPool pool; - public EvictionThread(final GenericObjectPool pool) { + public EvictionThread(final GenericObjectPool pool) { this.pool = pool; } @@ -176,9 +176,9 @@ public void run() { * 1) Instances are mutable and mutation can cause change in identity / hashcode. */ private static final class HashSetFactory - extends BasePooledObjectFactory> { + extends BasePooledObjectFactory, RuntimeException> { @Override - public HashSet create() throws Exception { + public HashSet create() { return new HashSet<>(); } @Override @@ -192,9 +192,9 @@ public PooledObject> wrap(final HashSet value) { */ static class InvalidateThread implements Runnable { private final String obj; - private final ObjectPool pool; + private final ObjectPool pool; private boolean done; - public InvalidateThread(final ObjectPool pool, final String obj) { + public InvalidateThread(final ObjectPool pool, final String obj) { this.obj = obj; this.pool = pool; } @@ -216,10 +216,10 @@ public void run() { } private static class InvalidFactory - extends BasePooledObjectFactory { + extends BasePooledObjectFactory { @Override - public Object create() throws Exception { + public Object create() { return new Object(); } @Override @@ -234,7 +234,7 @@ public PooledObject wrap(final Object value) { } } - public static class SimpleFactory implements PooledObjectFactory { + public static class SimpleFactory implements PooledObjectFactory { int makeCounter; int activationCounter; @@ -437,13 +437,13 @@ public boolean evict(final EvictionConfig config, final PooledObject underTes } } - static class TestThread implements Runnable { + static class TestThread implements Runnable { /** source of random delay times */ private final java.util.Random random; /** pool to borrow from */ - private final ObjectPool pool; + private final ObjectPool pool; /** number of borrow attempts */ private final int iter; @@ -464,29 +464,29 @@ static class TestThread implements Runnable { private volatile boolean failed; private volatile Throwable error; - public TestThread(final ObjectPool pool) { + public TestThread(final ObjectPool pool) { this(pool, 100, 50, true, null); } - public TestThread(final ObjectPool pool, final int iter) { + public TestThread(final ObjectPool pool, final int iter) { this(pool, iter, 50, true, null); } - public TestThread(final ObjectPool pool, final int iter, final int delay) { + public TestThread(final ObjectPool pool, final int iter, final int delay) { this(pool, iter, delay, true, null); } - public TestThread(final ObjectPool pool, final int iter, final int delay, + public TestThread(final ObjectPool pool, final int iter, final int delay, final boolean randomDelay) { this(pool, iter, delay, randomDelay, null); } - public TestThread(final ObjectPool pool, final int iter, final int delay, + public TestThread(final ObjectPool pool, final int iter, final int delay, final boolean randomDelay, final Object obj) { this(pool, iter, delay, delay, randomDelay, obj); } - public TestThread(final ObjectPool pool, final int iter, final int startDelay, + public TestThread(final ObjectPool pool, final int iter, final int startDelay, final int holdTime, final boolean randomDelay, final Object obj) { this.pool = pool; this.iter = iter; @@ -546,8 +546,8 @@ public void run() { * Very simple test thread that just tries to borrow an object from * the provided pool returns it after a wait */ - static class WaitingTestThread extends Thread { - private final GenericObjectPool pool; + static class WaitingTestThread extends Thread { + private final GenericObjectPool pool; private final long pause; private Throwable thrown; @@ -557,7 +557,7 @@ static class WaitingTestThread extends Thread { private long endedMillis; private String objectId; - public WaitingTestThread(final GenericObjectPool pool, final long pause) { + public WaitingTestThread(final GenericObjectPool pool, final long pause) { this.pool = pool; this.pause = pause; this.thrown = null; @@ -587,12 +587,12 @@ public void run() { // mvn test -DargLine="-DTestGenericObjectPool.display.thread.details=true" // @see https://issues.apache.org/jira/browse/SUREFIRE-121 - protected GenericObjectPool genericObjectPool; + protected GenericObjectPool genericObjectPool; private SimpleFactory simpleFactory; @SuppressWarnings("deprecation") - private void assertConfiguration(final GenericObjectPoolConfig expected, final GenericObjectPool actual) { + private void assertConfiguration(final GenericObjectPoolConfig expected, final GenericObjectPool actual) { assertEquals(Boolean.valueOf(expected.getTestOnCreate()), Boolean.valueOf(actual.getTestOnCreate()), "testOnCreate"); assertEquals(Boolean.valueOf(expected.getTestOnBorrow()), Boolean.valueOf(actual.getTestOnBorrow()), @@ -696,7 +696,7 @@ private void checkEvictionOrderPart2(final boolean lifo) throws Exception { private void checkEvictorVisiting(final boolean lifo) throws Exception { VisitTracker obj; VisitTrackerFactory trackerFactory = new VisitTrackerFactory<>(); - try (GenericObjectPool> trackerPool = new GenericObjectPool<>(trackerFactory)) { + try (GenericObjectPool,RuntimeException> trackerPool = new GenericObjectPool<>(trackerFactory)) { trackerPool.setNumTestsPerEvictionRun(2); trackerPool.setMinEvictableIdleTime(Duration.ofMillis(-1)); trackerPool.setTestWhileIdle(true); @@ -727,7 +727,7 @@ private void checkEvictorVisiting(final boolean lifo) throws Exception { } trackerFactory = new VisitTrackerFactory<>(); - try (GenericObjectPool> trackerPool = new GenericObjectPool<>(trackerFactory)) { + try (GenericObjectPool, RuntimeException> trackerPool = new GenericObjectPool<>(trackerFactory)) { trackerPool.setNumTestsPerEvictionRun(3); trackerPool.setMinEvictableIdleTime(Duration.ofMillis(-1)); trackerPool.setTestWhileIdle(true); @@ -770,7 +770,7 @@ private void checkEvictorVisiting(final boolean lifo) throws Exception { random.setSeed(System.currentTimeMillis()); for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { - try (GenericObjectPool> trackerPool = new GenericObjectPool<>(trackerFactory)) { + try (GenericObjectPool, RuntimeException> trackerPool = new GenericObjectPool<>(trackerFactory)) { trackerPool.setNumTestsPerEvictionRun(smallPrimes[i]); trackerPool.setMinEvictableIdleTime(Duration.ofMillis(-1)); trackerPool.setTestWhileIdle(true); @@ -808,8 +808,8 @@ private void checkEvictorVisiting(final boolean lifo) throws Exception { } } - private BasePooledObjectFactory createDefaultPooledObjectFactory() { - return new BasePooledObjectFactory() { + private BasePooledObjectFactory createDefaultPooledObjectFactory() { + return new BasePooledObjectFactory() { @Override public String create() { // fake @@ -824,8 +824,8 @@ public PooledObject wrap(final String obj) { }; } - private BasePooledObjectFactory createNullPooledObjectFactory() { - return new BasePooledObjectFactory() { + private BasePooledObjectFactory createNullPooledObjectFactory() { + return new BasePooledObjectFactory() { @Override public String create() { // fake @@ -840,10 +840,10 @@ public PooledObject wrap(final String obj) { }; } - private BasePooledObjectFactory createSlowObjectFactory(final long elapsedTimeMillis) { - return new BasePooledObjectFactory() { + private BasePooledObjectFactory createSlowObjectFactory(final long elapsedTimeMillis) { + return new BasePooledObjectFactory() { @Override - public String create() throws Exception { + public String create() throws InterruptedException { Thread.sleep(elapsedTimeMillis); return "created"; } @@ -872,17 +872,15 @@ protected boolean isLifo() { } @Override - protected ObjectPool makeEmptyPool(final int minCap) { - final GenericObjectPool mtPool = - new GenericObjectPool<>(new SimpleFactory()); - mtPool.setMaxTotal(minCap); + protected ObjectPool makeEmptyPool(final int minCap) { + final GenericObjectPool mtPool = new GenericObjectPool<>(new SimpleFactory()); + mtPool.setMaxTotal(minCap); mtPool.setMaxIdle(minCap); return mtPool; } @Override - protected ObjectPool makeEmptyPool( - final PooledObjectFactory fac) { + protected ObjectPool makeEmptyPool(final PooledObjectFactory fac) { return new GenericObjectPool<>(fac); } @@ -891,13 +889,10 @@ protected ObjectPool makeEmptyPool( * borrow-return cycles with random delay times <= delay * in between. */ - @SuppressWarnings({ - "rawtypes", "unchecked" - }) - private void runTestThreads(final int numThreads, final int iterations, final int delay, final GenericObjectPool testPool) { - final TestThread[] threads = new TestThread[numThreads]; + private void runTestThreads(final int numThreads, final int iterations, final int delay, final GenericObjectPool testPool) { + final TestThread[] threads = new TestThread[numThreads]; for (int i = 0; i < numThreads; i++) { - threads[i] = new TestThread(testPool, iterations, delay); + threads[i] = new TestThread<>(testPool, iterations, delay); final Thread t = new Thread(threads[i]); t.start(); } @@ -961,12 +956,12 @@ public void tearDown() throws Exception { public void testAbandonedPool() throws Exception { final GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); config.setJmxEnabled(false); - GenericObjectPool abandoned = new GenericObjectPool<>(simpleFactory, config); + GenericObjectPool abandoned = new GenericObjectPool<>(simpleFactory, config); abandoned.setTimeBetweenEvictionRuns(Duration.ofMillis(100)); // Starts evictor assertEquals(abandoned.getRemoveAbandonedTimeout(), abandoned.getRemoveAbandonedTimeoutDuration().getSeconds()); // This is ugly, but forces GC to hit the pool - final WeakReference> ref = new WeakReference<>(abandoned); + final WeakReference> ref = new WeakReference<>(abandoned); abandoned = null; while (ref.get() != null) { System.gc(); @@ -994,7 +989,7 @@ public void testAddObject() throws Exception { public void testAppendStats() { assertFalse(genericObjectPool.getMessageStatistics()); assertEquals("foo", (genericObjectPool.appendStats("foo"))); - try (final GenericObjectPool pool = new GenericObjectPool<>(new SimpleFactory())) { + try (final GenericObjectPool pool = new GenericObjectPool<>(new SimpleFactory())) { pool.setMessagesStatistics(true); assertNotEquals("foo", (pool.appendStats("foo"))); pool.setMessagesStatistics(false); @@ -1165,7 +1160,7 @@ public void testClientWaitStats() throws Exception { final SimpleFactory factory = new SimpleFactory(); // Give makeObject a little latency factory.setMakeLatency(200); - try (final GenericObjectPool pool = new GenericObjectPool<>(factory, new GenericObjectPoolConfig<>())) { + try (final GenericObjectPool pool = new GenericObjectPool<>(factory, new GenericObjectPoolConfig<>())) { final String s = pool.borrowObject(); // First borrow waits on create, so wait time should be at least 200 ms // Allow 100ms error in clock times @@ -1183,7 +1178,7 @@ public void testClientWaitStats() throws Exception { @Test @Timeout(value = 60000, unit = TimeUnit.MILLISECONDS) public void testCloseMultiplePools1() { - try (final GenericObjectPool genericObjectPool2 = new GenericObjectPool<>(simpleFactory)) { + try (final GenericObjectPool genericObjectPool2 = new GenericObjectPool<>(simpleFactory)) { genericObjectPool.setTimeBetweenEvictionRuns(TestConstants.ONE_MILLISECOND_DURATION); genericObjectPool2.setTimeBetweenEvictionRuns(TestConstants.ONE_MILLISECOND_DURATION); } @@ -1193,7 +1188,7 @@ public void testCloseMultiplePools1() { @Test @Timeout(value = 60000, unit = TimeUnit.MILLISECONDS) public void testCloseMultiplePools2() throws Exception { - try (final GenericObjectPool genericObjectPool2 = new GenericObjectPool<>(simpleFactory)) { + try (final GenericObjectPool genericObjectPool2 = new GenericObjectPool<>(simpleFactory)) { // Ensure eviction takes a long time, during which time EvictionTimer.executor's queue is empty simpleFactory.setDestroyLatency(1000L); // Ensure there is an object to evict, so that above latency takes effect @@ -1316,8 +1311,8 @@ public void testConstructors() { final long timeBetweenEvictionRunsMillis = 8; final boolean blockWhenExhausted = false; final boolean lifo = false; - final PooledObjectFactory dummyFactory = new DummyFactory(); - try (GenericObjectPool dummyPool = new GenericObjectPool<>(dummyFactory)) { + final PooledObjectFactory dummyFactory = new DummyFactory(); + try (GenericObjectPool dummyPool = new GenericObjectPool<>(dummyFactory)) { assertEquals(GenericObjectPoolConfig.DEFAULT_MAX_IDLE, dummyPool.getMaxIdle()); assertEquals(BaseObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS, dummyPool.getMaxWaitMillis()); assertEquals(GenericObjectPoolConfig.DEFAULT_MIN_IDLE, dummyPool.getMinIdle()); @@ -1362,7 +1357,7 @@ public void testConstructors() { config.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); assertEquals(timeBetweenEvictionRunsMillis, config.getTimeBetweenEvictionRuns().toMillis()); config.setBlockWhenExhausted(blockWhenExhausted); - try (GenericObjectPool dummyPool = new GenericObjectPool<>(dummyFactory, config)) { + try (GenericObjectPool dummyPool = new GenericObjectPool<>(dummyFactory, config)) { assertEquals(maxIdle, dummyPool.getMaxIdle()); assertEquals(maxWaitDuration, dummyPool.getMaxWaitDuration()); assertEquals(maxWaitMillis, dummyPool.getMaxWaitMillis()); @@ -1394,7 +1389,7 @@ public void testDefaultConfiguration() { @Test public void testEqualsIndiscernible() throws Exception { final HashSetFactory factory = new HashSetFactory(); - try (final GenericObjectPool> pool = new GenericObjectPool<>(factory, + try (final GenericObjectPool, RuntimeException> pool = new GenericObjectPool<>(factory, new GenericObjectPoolConfig<>())) { final HashSet s1 = pool.borrowObject(); final HashSet s2 = pool.borrowObject(); @@ -1407,7 +1402,7 @@ public void testEqualsIndiscernible() throws Exception { public void testErrorFactoryDoesNotBlockThreads() throws Exception { final CreateErrorFactory factory = new CreateErrorFactory(); - try (final GenericObjectPool createFailFactoryPool = new GenericObjectPool<>(factory)) { + try (final GenericObjectPool createFailFactoryPool = new GenericObjectPool<>(factory)) { createFailFactoryPool.setMaxTotal(1); @@ -1468,7 +1463,7 @@ public void testEvictAddObjects() throws Exception { genericObjectPool.borrowObject(); // numActive = 1, numIdle = 0 // Create a test thread that will run once and try a borrow after // 150ms fixed delay - final TestThread borrower = new TestThread<>(genericObjectPool, 1, 150, false); + final TestThread borrower = new TestThread<>(genericObjectPool, 1, 150, false); final Thread borrowerThread = new Thread(borrower); // Set evictor to run in 100 ms - will create idle instance genericObjectPool.setTimeBetweenEvictionRuns(Duration.ofMillis(100)); @@ -1539,7 +1534,7 @@ public void testEviction() throws Exception { @Timeout(value = 60000, unit = TimeUnit.MILLISECONDS) public void testEvictionInvalid() throws Exception { - try (final GenericObjectPool invalidFactoryPool = new GenericObjectPool<>(new InvalidFactory())) { + try (final GenericObjectPool invalidFactoryPool = new GenericObjectPool<>(new InvalidFactory())) { invalidFactoryPool.setMaxIdle(1); invalidFactoryPool.setMaxTotal(1); @@ -1644,7 +1639,7 @@ public void testEvictionPolicy() throws Exception { @Test @Timeout(value = 60000, unit = TimeUnit.MILLISECONDS) public void testEvictionSoftMinIdle() throws Exception { - class TimeTest extends BasePooledObjectFactory { + class TimeTest extends BasePooledObjectFactory { private final long createTimeMillis; public TimeTest() { @@ -1652,7 +1647,7 @@ public TimeTest() { } @Override - public TimeTest create() throws Exception { + public TimeTest create() { return new TimeTest(); } @@ -1666,7 +1661,7 @@ public PooledObject wrap(final TimeTest value) { } } - try (final GenericObjectPool timePool = new GenericObjectPool<>(new TimeTest())) { + try (final GenericObjectPool timePool = new GenericObjectPool<>(new TimeTest())) { timePool.setMaxIdle(5); timePool.setMaxTotal(5); @@ -1832,7 +1827,7 @@ public void testExceptionOnPassivateDuringReturn() throws Exception { public void testFailingFactoryDoesNotBlockThreads() throws Exception { final CreateFailFactory factory = new CreateFailFactory(); - try (final GenericObjectPool createFailFactoryPool = new GenericObjectPool<>(factory)) { + try (final GenericObjectPool createFailFactoryPool = new GenericObjectPool<>(factory)) { createFailFactoryPool.setMaxTotal(1); @@ -1896,21 +1891,21 @@ public void testFIFO() throws Exception { @Test public void testGetFactoryType_DefaultPooledObjectFactory() { - try (final GenericObjectPool pool = new GenericObjectPool<>(createDefaultPooledObjectFactory())) { + try (final GenericObjectPool pool = new GenericObjectPool<>(createDefaultPooledObjectFactory())) { assertNotNull((pool.getFactoryType())); } } @Test public void testGetFactoryType_NullPooledObjectFactory() { - try (final GenericObjectPool pool = new GenericObjectPool<>(createNullPooledObjectFactory())) { + try (final GenericObjectPool pool = new GenericObjectPool<>(createNullPooledObjectFactory())) { assertNotNull((pool.getFactoryType())); } } @Test public void testGetFactoryType_PoolUtilsSynchronizedDefaultPooledFactory() { - try (final GenericObjectPool pool = new GenericObjectPool<>( + try (final GenericObjectPool pool = new GenericObjectPool<>( PoolUtils.synchronizedPooledFactory(createDefaultPooledObjectFactory()))) { assertNotNull((pool.getFactoryType())); } @@ -1918,7 +1913,7 @@ public void testGetFactoryType_PoolUtilsSynchronizedDefaultPooledFactory() { @Test public void testGetFactoryType_PoolUtilsSynchronizedNullPooledFactory() { - try (final GenericObjectPool pool = new GenericObjectPool<>( + try (final GenericObjectPool pool = new GenericObjectPool<>( PoolUtils.synchronizedPooledFactory(createNullPooledObjectFactory()))) { assertNotNull((pool.getFactoryType())); } @@ -1926,7 +1921,7 @@ public void testGetFactoryType_PoolUtilsSynchronizedNullPooledFactory() { @Test public void testGetFactoryType_SynchronizedDefaultPooledObjectFactory() { - try (final GenericObjectPool pool = new GenericObjectPool<>( + try (final GenericObjectPool pool = new GenericObjectPool<>( new TestSynchronizedPooledObjectFactory<>(createDefaultPooledObjectFactory()))) { assertNotNull((pool.getFactoryType())); } @@ -1934,7 +1929,7 @@ public void testGetFactoryType_SynchronizedDefaultPooledObjectFactory() { @Test public void testGetFactoryType_SynchronizedNullPooledObjectFactory() { - try (final GenericObjectPool pool = new GenericObjectPool<>( + try (final GenericObjectPool pool = new GenericObjectPool<>( new TestSynchronizedPooledObjectFactory<>(createNullPooledObjectFactory()))) { assertNotNull((pool.getFactoryType())); } @@ -1942,7 +1937,7 @@ public void testGetFactoryType_SynchronizedNullPooledObjectFactory() { @Test public void testGetStatsString() { - try (final GenericObjectPool pool = new GenericObjectPool<>( + try (final GenericObjectPool pool = new GenericObjectPool<>( new TestSynchronizedPooledObjectFactory<>(createNullPooledObjectFactory()))) { assertNotNull(pool.getStatsString()); } @@ -1959,7 +1954,7 @@ public void testGetStatsString() { @Test public void testInvalidateFreesCapacity() throws Exception { final SimpleFactory factory = new SimpleFactory(); - try (final GenericObjectPool pool = new GenericObjectPool<>(factory)) { + try (final GenericObjectPool pool = new GenericObjectPool<>(factory)) { pool.setMaxTotal(2); pool.setMaxWaitMillis(500); // Borrow an instance and hold if for 5 seconds @@ -1994,14 +1989,14 @@ public void testJmxRegistration() { final GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); config.setJmxEnabled(false); - try (final GenericObjectPool poolWithoutJmx = new GenericObjectPool<>(simpleFactory, config)) { + try (final GenericObjectPool poolWithoutJmx = new GenericObjectPool<>(simpleFactory, config)) { assertNull(poolWithoutJmx.getJmxName()); config.setJmxEnabled(true); poolWithoutJmx.jmxUnregister(); } config.setJmxNameBase(null); - try (final GenericObjectPool poolWithDefaultJmxNameBase = new GenericObjectPool<>(simpleFactory, config)) { + try (final GenericObjectPool poolWithDefaultJmxNameBase = new GenericObjectPool<>(simpleFactory, config)) { assertNotNull(poolWithDefaultJmxNameBase.getJmxName()); } } @@ -2407,7 +2402,7 @@ public void testMinIdleMaxTotal() throws Exception { @Test public void testMultipleReturn() throws Exception { final WaiterFactory factory = new WaiterFactory<>(0, 0, 0, 0, 0, 0); - try (final GenericObjectPool pool = new GenericObjectPool<>(factory)) { + try (final GenericObjectPool pool = new GenericObjectPool<>(factory)) { pool.setTestOnReturn(true); final Waiter waiter = pool.borrowObject(); pool.returnObject(waiter); @@ -2427,7 +2422,7 @@ public void testMultipleReturn() throws Exception { // POOL-248 @Test public void testMultipleReturnOfSameObject() throws Exception { - try (final GenericObjectPool pool = new GenericObjectPool<>(simpleFactory, new GenericObjectPoolConfig<>())) { + try (final GenericObjectPool pool = new GenericObjectPool<>(simpleFactory, new GenericObjectPoolConfig<>())) { assertEquals(0, pool.getNumActive()); assertEquals(0, pool.getNumIdle()); @@ -2459,7 +2454,7 @@ public void testMultipleReturnOfSameObject() throws Exception { @Test public void testMutable() throws Exception { final HashSetFactory factory = new HashSetFactory(); - try (final GenericObjectPool> pool = new GenericObjectPool<>(factory, + try (final GenericObjectPool, RuntimeException> pool = new GenericObjectPool<>(factory, new GenericObjectPoolConfig<>())) { final HashSet s1 = pool.borrowObject(); final HashSet s2 = pool.borrowObject(); @@ -2490,7 +2485,7 @@ public void testNoInstanceOverlap() { final int delay = 1; final int iterations = 1000; final AtomicIntegerFactory factory = new AtomicIntegerFactory(); - try (final GenericObjectPool pool = new GenericObjectPool<>(factory)) { + try (final GenericObjectPool pool = new GenericObjectPool<>(factory)) { pool.setMaxTotal(maxTotal); pool.setMaxIdle(maxTotal); pool.setTestOnBorrow(true); @@ -2542,7 +2537,7 @@ public void testPreparePool() throws Exception { public void testReturnBorrowObjectWithingMaxWaitMillis() throws Exception { final long maxWaitMillis = 500; - try (final GenericObjectPool createSlowObjectFactoryPool = new GenericObjectPool<>( + try (final GenericObjectPool createSlowObjectFactoryPool = new GenericObjectPool<>( createSlowObjectFactory(60000))) { createSlowObjectFactoryPool.setMaxTotal(1); createSlowObjectFactoryPool.setMaxWaitMillis(maxWaitMillis); @@ -2850,7 +2845,7 @@ public void testValidationFailureOnReturnFreesCapacity() throws Exception { final SimpleFactory factory = new SimpleFactory(); factory.setValid(false); // Validate will always fail factory.setValidationEnabled(true); - try (final GenericObjectPool pool = new GenericObjectPool<>(factory)) { + try (final GenericObjectPool pool = new GenericObjectPool<>(factory)) { pool.setMaxTotal(2); pool.setMaxWaitMillis(1500); pool.setTestOnReturn(true); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolClassLoaders.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolClassLoaders.java index 71915233e..8298255a5 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolClassLoaders.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolClassLoaders.java @@ -47,7 +47,7 @@ public URL findResource(final String name) { } private static class CustomClassLoaderObjectFactory extends - BasePooledObjectFactory { + BasePooledObjectFactory { private final int n; CustomClassLoaderObjectFactory(final int n) { @@ -55,7 +55,7 @@ private static class CustomClassLoaderObjectFactory extends } @Override - public URL create() throws Exception { + public URL create() { final URL url = Thread.currentThread().getContextClassLoader() .getResource("test" + n); if (url == null) { @@ -81,7 +81,7 @@ public void testContextClassLoader() throws Exception { try (final CustomClassLoader cl1 = new CustomClassLoader(1)) { Thread.currentThread().setContextClassLoader(cl1); final CustomClassLoaderObjectFactory factory1 = new CustomClassLoaderObjectFactory(1); - try (final GenericObjectPool pool1 = new GenericObjectPool<>(factory1)) { + try (final GenericObjectPool pool1 = new GenericObjectPool<>(factory1)) { pool1.setMinIdle(1); pool1.setTimeBetweenEvictionRuns(Duration.ofMillis(100)); int counter = 0; @@ -94,7 +94,7 @@ public void testContextClassLoader() throws Exception { try (final CustomClassLoader cl2 = new CustomClassLoader(2)) { Thread.currentThread().setContextClassLoader(cl2); final CustomClassLoaderObjectFactory factory2 = new CustomClassLoaderObjectFactory(2); - try (final GenericObjectPool pool2 = new GenericObjectPool<>(factory2)) { + try (final GenericObjectPool pool2 = new GenericObjectPool<>(factory2)) { pool2.setMinIdle(1); pool2.addObject(); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolFactoryCreateFailure.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolFactoryCreateFailure.java index a625787e4..96eaf95f1 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolFactoryCreateFailure.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolFactoryCreateFailure.java @@ -35,7 +35,7 @@ */ public class TestGenericObjectPoolFactoryCreateFailure { - private static class SingleObjectFactory extends BasePooledObjectFactory { + private static class SingleObjectFactory extends BasePooledObjectFactory { private final AtomicBoolean created = new AtomicBoolean(); @Override @@ -60,8 +60,8 @@ public PooledObject wrap(final Object obj) { private static class WinnerRunnable implements Runnable { private final CountDownLatch barrier; private final AtomicBoolean failed; - private final GenericObjectPool pool; - private WinnerRunnable(final GenericObjectPool pool, final CountDownLatch barrier, final AtomicBoolean failed) { + private final GenericObjectPool pool; + private WinnerRunnable(final GenericObjectPool pool, final CountDownLatch barrier, final AtomicBoolean failed) { this.pool = pool; this.failed = failed; this.barrier = barrier; @@ -113,7 +113,7 @@ public void testBorrowObjectStuck() { config.setSoftMinEvictableIdleTime(NEG_ONE_DURATION); config.setMaxWait(NEG_ONE_DURATION); - try (GenericObjectPool pool = new GenericObjectPool<>(factory, config)) { + try (GenericObjectPool pool = new GenericObjectPool<>(factory, config)) { final AtomicBoolean failed = new AtomicBoolean(); final CountDownLatch barrier = new CountDownLatch(1); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestPoolImplUtils.java b/src/test/java/org/apache/commons/pool2/impl/TestPoolImplUtils.java index 98b80e2ac..03ea82402 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestPoolImplUtils.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestPoolImplUtils.java @@ -30,7 +30,7 @@ public class TestPoolImplUtils { @SuppressWarnings("unused") - private abstract static class FactoryAB extends BasePooledObjectFactory { + private abstract static class FactoryAB extends BasePooledObjectFactory { // empty by design } @@ -53,7 +53,7 @@ private abstract static class FactoryF extends FactoryDE { private static class NotSimpleFactory extends FactoryF { @Override - public Long create() throws Exception { + public Long create() { return null; } @@ -63,9 +63,9 @@ public PooledObject wrap(final Long obj) { } } - private static class SimpleFactory extends BasePooledObjectFactory { + private static class SimpleFactory extends BasePooledObjectFactory { @Override - public String create() throws Exception { + public String create() { return null; } diff --git a/src/test/java/org/apache/commons/pool2/impl/TestSoftRefOutOfMemory.java b/src/test/java/org/apache/commons/pool2/impl/TestSoftRefOutOfMemory.java index 3bd3a67d9..55044f3b6 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestSoftRefOutOfMemory.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestSoftRefOutOfMemory.java @@ -34,7 +34,7 @@ /** */ public class TestSoftRefOutOfMemory { - public static class LargePoolableObjectFactory extends BasePooledObjectFactory { + public static class LargePoolableObjectFactory extends BasePooledObjectFactory { private final String buffer; private int counter; @@ -56,7 +56,7 @@ public PooledObject wrap(final String value) { } } - private static class OomeFactory extends BasePooledObjectFactory { + private static class OomeFactory extends BasePooledObjectFactory { private final OomeTrigger trigger; @@ -65,7 +65,7 @@ public OomeFactory(final OomeTrigger trigger) { } @Override - public String create() throws Exception { + public String create() { if (trigger.equals(OomeTrigger.CREATE)) { throw new OutOfMemoryError(); } @@ -78,7 +78,7 @@ public String create() throws Exception { } @Override - public void destroyObject(final PooledObject p) throws Exception { + public void destroyObject(final PooledObject p) { if (trigger.equals(OomeTrigger.DESTROY)) { throw new OutOfMemoryError(); } @@ -105,7 +105,7 @@ private enum OomeTrigger { DESTROY } - public static class SmallPoolableObjectFactory extends BasePooledObjectFactory { + public static class SmallPoolableObjectFactory extends BasePooledObjectFactory { private int counter; @Override @@ -124,7 +124,7 @@ public PooledObject wrap(final String value) { } } - private SoftReferenceObjectPool pool; + private SoftReferenceObjectPool pool; @AfterEach public void tearDown() { @@ -135,7 +135,6 @@ public void tearDown() { System.gc(); } - @Test public void testOutOfMemory() throws Exception { pool = new SoftReferenceObjectPool<>(new SmallPoolableObjectFactory()); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestSoftReferenceObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestSoftReferenceObjectPool.java index f29cc6c60..a12b7dacd 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestSoftReferenceObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestSoftReferenceObjectPool.java @@ -26,7 +26,7 @@ */ public class TestSoftReferenceObjectPool extends TestBaseObjectPool { - private static class SimpleFactory extends BasePooledObjectFactory { + private static class SimpleFactory extends BasePooledObjectFactory { int counter; @Override public String create() { @@ -54,13 +54,13 @@ protected boolean isLifo() { } @Override - protected ObjectPool makeEmptyPool(final int cap) { - return new SoftReferenceObjectPool<>(new SimpleFactory()); + protected ObjectPool makeEmptyPool(final int cap) { + return (ObjectPool) new SoftReferenceObjectPool<>(new SimpleFactory()); } @Override - protected ObjectPool makeEmptyPool(final PooledObjectFactory factory) { + protected ObjectPool makeEmptyPool(final PooledObjectFactory factory) { return new SoftReferenceObjectPool<>(factory); } } diff --git a/src/test/java/org/apache/commons/pool2/impl/TestSynchronizedPooledObjectFactory.java b/src/test/java/org/apache/commons/pool2/impl/TestSynchronizedPooledObjectFactory.java index 53d7fe3c1..ba5f7f923 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestSynchronizedPooledObjectFactory.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestSynchronizedPooledObjectFactory.java @@ -34,13 +34,13 @@ * library. *

*/ -final class TestSynchronizedPooledObjectFactory implements PooledObjectFactory { +final class TestSynchronizedPooledObjectFactory implements PooledObjectFactory { /** Synchronization lock */ private final WriteLock writeLock = new ReentrantReadWriteLock().writeLock(); /** Wrapped factory */ - private final PooledObjectFactory factory; + private final PooledObjectFactory factory; /** * Constructs a SynchronizedPoolableObjectFactory wrapping the given factory. @@ -50,7 +50,7 @@ final class TestSynchronizedPooledObjectFactory implements PooledObjectFactor * @throws IllegalArgumentException * if the factory is null */ - TestSynchronizedPooledObjectFactory(final PooledObjectFactory factory) throws IllegalArgumentException { + TestSynchronizedPooledObjectFactory(final PooledObjectFactory factory) throws IllegalArgumentException { if (factory == null) { throw new IllegalArgumentException("factory must not be null."); } @@ -61,7 +61,7 @@ final class TestSynchronizedPooledObjectFactory implements PooledObjectFactor * {@inheritDoc} */ @Override - public void activateObject(final PooledObject p) throws Exception { + public void activateObject(final PooledObject p) throws E { writeLock.lock(); try { factory.activateObject(p); @@ -74,7 +74,7 @@ public void activateObject(final PooledObject p) throws Exception { * {@inheritDoc} */ @Override - public void destroyObject(final PooledObject p) throws Exception { + public void destroyObject(final PooledObject p) throws E { writeLock.lock(); try { factory.destroyObject(p); @@ -87,7 +87,7 @@ public void destroyObject(final PooledObject p) throws Exception { * {@inheritDoc} */ @Override - public PooledObject makeObject() throws Exception { + public PooledObject makeObject() throws E { writeLock.lock(); try { return factory.makeObject(); @@ -100,7 +100,7 @@ public PooledObject makeObject() throws Exception { * {@inheritDoc} */ @Override - public void passivateObject(final PooledObject p) throws Exception { + public void passivateObject(final PooledObject p) throws E { writeLock.lock(); try { factory.passivateObject(p); diff --git a/src/test/java/org/apache/commons/pool2/performance/PerformanceTest.java b/src/test/java/org/apache/commons/pool2/performance/PerformanceTest.java index 0b19772fd..03eb38f0b 100644 --- a/src/test/java/org/apache/commons/pool2/performance/PerformanceTest.java +++ b/src/test/java/org/apache/commons/pool2/performance/PerformanceTest.java @@ -126,7 +126,7 @@ public static void main(final String[] args) { private int nrIterations = 5; - private GenericObjectPool pool; + private GenericObjectPool pool; private void run(final int iterations, final int nrThreads, final int maxTotal, final int maxIdle) { this.nrIterations = iterations; diff --git a/src/test/java/org/apache/commons/pool2/performance/SleepingObjectFactory.java b/src/test/java/org/apache/commons/pool2/performance/SleepingObjectFactory.java index bcf0165bb..a12b664ec 100644 --- a/src/test/java/org/apache/commons/pool2/performance/SleepingObjectFactory.java +++ b/src/test/java/org/apache/commons/pool2/performance/SleepingObjectFactory.java @@ -25,13 +25,13 @@ /** * Sleepy ObjectFactory (everything takes a while longer) */ -public class SleepingObjectFactory implements PooledObjectFactory { +public class SleepingObjectFactory implements PooledObjectFactory { private int counter; private boolean debug; @Override - public void activateObject(final PooledObject obj) throws Exception { + public void activateObject(final PooledObject obj) { debug("activateObject", obj); Waiter.sleepQuietly(10); } @@ -44,7 +44,7 @@ private void debug(final String method, final Object obj) { } @Override - public void destroyObject(final PooledObject obj) throws Exception { + public void destroyObject(final PooledObject obj) { debug("destroyObject", obj); Waiter.sleepQuietly(250); } @@ -54,7 +54,7 @@ public boolean isDebug() { } @Override - public PooledObject makeObject() throws Exception { + public PooledObject makeObject() { // Deliberate choice to create a new object in case future unit tests // check for a specific object. final Integer obj = Integer.valueOf(counter++); @@ -64,7 +64,7 @@ public PooledObject makeObject() throws Exception { } @Override - public void passivateObject(final PooledObject obj) throws Exception { + public void passivateObject(final PooledObject obj) { debug("passivateObject", obj); Waiter.sleepQuietly(10); } diff --git a/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java index b62264cf9..7c64922b5 100644 --- a/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java @@ -40,18 +40,19 @@ public abstract class BaseTestProxiedKeyedObjectPool { - private static class TestKeyedObjectFactory extends - BaseKeyedPooledObjectFactory { + private static class TestKeyedObjectFactory extends BaseKeyedPooledObjectFactory { @Override - public TestObject create(final String key) throws Exception { + public TestObject create(final String key) { return new TestObjectImpl(); } + @Override public PooledObject wrap(final TestObject value) { return new DefaultPooledObject<>(value); } } + protected interface TestObject { String getData(); void setData(String data); @@ -79,7 +80,7 @@ public void setData(final String data) { private static final Duration ABANDONED_TIMEOUT_SECS = Duration.ofSeconds(3); - private KeyedObjectPool pool; + private KeyedObjectPool pool; private StringWriter log; @@ -102,13 +103,10 @@ public void setUp() { final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig<>(); config.setMaxTotal(3); - final KeyedPooledObjectFactory factory = - new TestKeyedObjectFactory(); + final KeyedPooledObjectFactory factory = new TestKeyedObjectFactory(); @SuppressWarnings("resource") - final KeyedObjectPool innerPool = - new GenericKeyedObjectPool<>( - factory, config, abandonedConfig); + final KeyedObjectPool innerPool = new GenericKeyedObjectPool<>(factory, config, abandonedConfig); pool = new ProxiedKeyedObjectPool<>(innerPool, getproxySource()); } diff --git a/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java b/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java index 84e6cac63..d560fe07f 100644 --- a/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java @@ -44,10 +44,10 @@ protected interface TestObject { void setData(String data); } private static class TestObjectFactory extends - BasePooledObjectFactory { + BasePooledObjectFactory { @Override - public TestObject create() throws Exception { + public TestObject create() { return new TestObjectImpl(); } @Override @@ -75,7 +75,7 @@ public void setData(final String data) { private static final Duration ABANDONED_TIMEOUT_SECS = Duration.ofSeconds(3); - private ObjectPool pool; + private ObjectPool pool; private StringWriter log; @@ -98,11 +98,10 @@ public void setUp() { final GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); config.setMaxTotal(3); - final PooledObjectFactory factory = new TestObjectFactory(); + final PooledObjectFactory factory = new TestObjectFactory(); @SuppressWarnings("resource") - final ObjectPool innerPool = - new GenericObjectPool<>(factory, config, abandonedConfig); + final ObjectPool innerPool = new GenericObjectPool<>(factory, config, abandonedConfig); pool = new ProxiedObjectPool<>(innerPool, getproxySource()); }