This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-pool.git
The following commit(s) were added to refs/heads/master by this push:
new b5bf4b1 Use assertThrows.
b5bf4b1 is described below
commit b5bf4b1d8c08c74992a961b388e5542b75397d91
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Aug 13 21:19:47 2021 -0400
Use assertThrows.
---
.../apache/commons/pool2/TestBaseObjectPool.java | 25 +--
.../apache/commons/pool2/TestKeyedObjectPool.java | 54 +----
.../org/apache/commons/pool2/TestObjectPool.java | 66 ++----
.../org/apache/commons/pool2/TestPoolUtils.java | 227 ++++++---------------
.../pool2/impl/TestGenericKeyedObjectPool.java | 51 +----
.../commons/pool2/impl/TestGenericObjectPool.java | 138 +++----------
.../pool2/impl/TestLinkedBlockingDeque.java | 140 +++----------
7 files changed, 166 insertions(+), 535 deletions(-)
diff --git a/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java
b/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java
index 5a8f524..faa1f70 100644
--- a/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java
@@ -17,13 +17,12 @@
package org.apache.commons.pool2;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
-
-
/**
*/
public class TestBaseObjectPool extends TestObjectPool {
@@ -203,12 +202,7 @@ public class TestBaseObjectPool extends TestObjectPool {
pool.returnObject(obj);
pool.close();
- try {
- pool.borrowObject();
- fail("Expected IllegalStateException");
- } catch(final IllegalStateException e) {
- // expected
- }
+ assertThrows(IllegalStateException.class, pool::borrowObject);
}
@Test
@@ -277,19 +271,8 @@ public class TestBaseObjectPool extends TestObjectPool {
assertTrue( pool.getNumIdle() < 0,"Negative expected.");
assertTrue( pool.getNumActive() < 0,"Negative expected.");
- try {
- pool.clear();
- fail("Expected UnsupportedOperationException");
- } catch (final UnsupportedOperationException e) {
- // expected
- }
-
- try {
- pool.addObject();
- fail("Expected UnsupportedOperationException");
- } catch (final UnsupportedOperationException e) {
- // expected
- }
+ assertThrows(UnsupportedOperationException.class, pool::clear);
+ assertThrows(UnsupportedOperationException.class, pool::addObject);
}
}
}
diff --git a/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java
b/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java
index 7b7f1b5..7fbb573 100644
--- a/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java
@@ -17,6 +17,7 @@
package org.apache.commons.pool2;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.ArrayList;
@@ -481,19 +482,11 @@ public abstract class TestKeyedObjectPool {
pool.close();
- try {
- pool.addObject(KEY);
- fail("A closed pool must throw an IllegalStateException when
addObject is called.");
- } catch (final IllegalStateException ise) {
- // expected
- }
+ assertThrows(IllegalStateException.class, () -> pool.addObject(KEY),
+ "A closed pool must throw an IllegalStateException when
addObject is called.");
- try {
- pool.borrowObject(KEY);
- fail("A closed pool must throw an IllegalStateException when
borrowObject is called.");
- } catch (final IllegalStateException ise) {
- // expected
- }
+ assertThrows(IllegalStateException.class, () -> pool.borrowObject(KEY),
+ "A closed pool must throw an IllegalStateException when
borrowObject is called.");
// The following should not throw exceptions just because the pool is
closed.
assertEquals( 0, pool.getNumIdle(KEY),"A closed pool shouldn't have
any idle objects.");
@@ -531,12 +524,7 @@ public abstract class TestKeyedObjectPool {
// makeObject Exceptions should be propagated to client code from
addObject
factory.setMakeObjectFail(true);
- try {
- pool.addObject(KEY);
- fail("Expected addObject to propagate makeObject exception.");
- } catch (final PrivateException pe) {
- // expected
- }
+ assertThrows(PrivateException.class, () -> pool.addObject(KEY),
"Expected addObject to propagate makeObject exception.");
expectedMethods.add(new MethodCall("makeObject", KEY));
assertEquals(expectedMethods, factory.getMethodCalls());
@@ -545,12 +533,7 @@ public abstract class TestKeyedObjectPool {
// passivateObject Exceptions should be propagated to client code from
addObject
factory.setMakeObjectFail(false);
factory.setPassivateObjectFail(true);
- try {
- pool.addObject(KEY);
- fail("Expected addObject to propagate passivateObject exception.");
- } catch (final PrivateException pe) {
- // expected
- }
+ assertThrows(PrivateException.class, () -> pool.addObject(KEY),
"Expected addObject to propagate passivateObject exception.");
expectedMethods.add(new MethodCall("makeObject", KEY).returned(ONE));
expectedMethods.add(new MethodCall("passivateObject", KEY, ONE));
assertEquals(expectedMethods, factory.getMethodCalls());
@@ -606,12 +589,7 @@ public abstract class TestKeyedObjectPool {
factory.setActivateObjectFail(true);
expectedMethods.add(new MethodCall("activateObject", KEY, obj));
- try {
- pool.borrowObject(KEY);
- fail("Expecting NoSuchElementException");
- } catch (final NoSuchElementException e) {
- //Activate should fail
- }
+ assertThrows(NoSuchElementException.class, () ->
pool.borrowObject(KEY));
// After idle object fails validation, new on is created and activation
// fails again for the new one.
expectedMethods.add(new MethodCall("makeObject", KEY).returned(ONE));
@@ -627,12 +605,7 @@ public abstract class TestKeyedObjectPool {
factory.setValidateObjectFail(true);
// testOnBorrow is on, so this will throw when the newly created
instance
// fails validation
- try {
- pool.borrowObject(KEY);
- fail("Expecting NoSuchElementException");
- } catch (final NoSuchElementException ex) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
pool.borrowObject(KEY));
// Activate, then validate for idle instance
expectedMethods.add(new MethodCall("activateObject", KEY, ZERO));
expectedMethods.add(new MethodCall("validateObject", KEY, ZERO));
@@ -716,15 +689,10 @@ public abstract class TestKeyedObjectPool {
//// Test exception handling of invalidateObject
reset(pool, factory, expectedMethods);
- obj = pool.borrowObject(KEY);
+ final Object obj2 = pool.borrowObject(KEY);
clear(factory, expectedMethods);
factory.setDestroyObjectFail(true);
- try {
- pool.invalidateObject(KEY, obj);
- fail("Expecting destroy exception to propagate");
- } catch (final PrivateException ex) {
- // Expected
- }
+ assertThrows(PrivateException.class, () -> pool.invalidateObject(KEY,
obj2), "Expecting destroy exception to propagate");
Thread.sleep(250); // could be defered
TestObjectPool.removeDestroyObjectCall(factory.getMethodCalls());
assertEquals(expectedMethods, factory.getMethodCalls());
diff --git a/src/test/java/org/apache/commons/pool2/TestObjectPool.java
b/src/test/java/org/apache/commons/pool2/TestObjectPool.java
index 5726630..2b1bc89 100644
--- a/src/test/java/org/apache/commons/pool2/TestObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/TestObjectPool.java
@@ -18,6 +18,7 @@ package org.apache.commons.pool2;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -82,19 +83,9 @@ public abstract class TestObjectPool {
pool.close();
- try {
- pool.addObject();
- fail("A closed pool must throw an IllegalStateException when
addObject is called.");
- } catch (final IllegalStateException ise) {
- // expected
- }
+ assertThrows(IllegalStateException.class, pool::addObject, "A closed
pool must throw an IllegalStateException when addObject is called.");
- try {
- pool.borrowObject();
- fail("A closed pool must throw an IllegalStateException when
borrowObject is called.");
- } catch (final IllegalStateException ise) {
- // expected
- }
+ assertThrows(IllegalStateException.class, pool::borrowObject, "A
closed pool must throw an IllegalStateException when borrowObject is called.");
// The following should not throw exceptions just because the pool is
closed.
if (pool.getNumIdle() >= 0) {
@@ -152,12 +143,7 @@ public abstract class TestObjectPool {
// makeObject Exceptions should be propagated to client code from
addObject
factory.setMakeObjectFail(true);
- try {
- pool.addObject();
- fail("Expected addObject to propagate makeObject exception.");
- } catch (final PrivateException pe) {
- // expected
- }
+ assertThrows(PrivateException.class, pool::addObject, "Expected
addObject to propagate makeObject exception.");
expectedMethods.add(new MethodCall("makeObject"));
assertEquals(expectedMethods, factory.getMethodCalls());
@@ -166,17 +152,11 @@ public abstract class TestObjectPool {
// passivateObject Exceptions should be propagated to client code from
addObject
factory.setMakeObjectFail(false);
factory.setPassivateObjectFail(true);
- try {
- pool.addObject();
- fail("Expected addObject to propagate passivateObject exception.");
- } catch (final PrivateException pe) {
- // expected
- }
+ assertThrows(PrivateException.class, pool::addObject, "Expected
addObject to propagate passivateObject exception.");
expectedMethods.add(new MethodCall("makeObject").returned(ONE));
// StackObjectPool, SofReferenceObjectPool also validate on add
if (pool instanceof SoftReferenceObjectPool) {
- expectedMethods.add(new MethodCall(
- "validateObject", ONE).returned(Boolean.TRUE));
+ expectedMethods.add(new MethodCall("validateObject",
ONE).returned(Boolean.TRUE));
}
expectedMethods.add(new MethodCall("passivateObject", ONE));
assertEquals(expectedMethods, factory.getMethodCalls());
@@ -214,12 +194,7 @@ public abstract class TestObjectPool {
// makeObject Exceptions should be propagated to client code from
borrowObject
factory.setMakeObjectFail(true);
- try {
- obj = pool.borrowObject();
- fail("Expected borrowObject to propagate makeObject exception.");
- } catch (final PrivateException pe) {
- // expected
- }
+ assertThrows(PrivateException.class, pool::borrowObject, "Expected
borrowObject to propagate makeObject exception.");
expectedMethods.add(new MethodCall("makeObject"));
assertEquals(expectedMethods, factory.getMethodCalls());
@@ -231,12 +206,8 @@ public abstract class TestObjectPool {
factory.setActivateObjectFail(true);
expectedMethods.add(new MethodCall("activateObject", obj));
- try {
- pool.borrowObject();
- fail("Expecting NoSuchElementException");
- } catch (final NoSuchElementException ex) {
- // Expected - newly created object will also fail to activate
- }
+ // Expected NoSuchElementException - newly created object will also
fail to activate
+ assertThrows(NoSuchElementException.class, pool::borrowObject,
"Expecting NoSuchElementException");
// Idle object fails activation, new one created, also fails
expectedMethods.add(new MethodCall("makeObject").returned(ONE));
expectedMethods.add(new MethodCall("activateObject", ONE));
@@ -251,11 +222,8 @@ public abstract class TestObjectPool {
factory.setValidateObjectFail(true);
expectedMethods.add(new MethodCall("activateObject", ZERO));
expectedMethods.add(new MethodCall("validateObject", ZERO));
- try {
- pool.borrowObject();
- } catch (final NoSuchElementException ex) {
- // Expected - newly created object will also fail to validate
- }
+ // Expected NoSuchElementException - newly created object will also
fail to validate
+ assertThrows(NoSuchElementException.class, pool::borrowObject,
"Expecting NoSuchElementException");
// Idle object is activated, but fails validation.
// New instance is created, activated and then fails validation
expectedMethods.add(new MethodCall("makeObject").returned(ONE));
@@ -342,15 +310,10 @@ public abstract class TestObjectPool {
//// Test exception handling of invalidateObject
reset(pool, factory, expectedMethods);
- obj = pool.borrowObject();
+ final Object obj2 = pool.borrowObject();
clear(factory, expectedMethods);
factory.setDestroyObjectFail(true);
- try {
- pool.invalidateObject(obj);
- fail("Expecting destroy exception to propagate");
- } catch (final PrivateException ex) {
- // Expected
- }
+ assertThrows(PrivateException.class, () ->
pool.invalidateObject(obj2));
Thread.sleep(250); // could be deferred
removeDestroyObjectCall(factory.getMethodCalls());
assertEquals(expectedMethods, factory.getMethodCalls());
@@ -399,8 +362,7 @@ public abstract class TestObjectPool {
pool.returnObject(obj);
// StackObjectPool, SoftReferenceObjectPool also validate on return
if (pool instanceof SoftReferenceObjectPool) {
- expectedMethods.add(new MethodCall(
- "validateObject", obj).returned(Boolean.TRUE));
+ expectedMethods.add(new MethodCall("validateObject",
obj).returned(Boolean.TRUE));
}
expectedMethods.add(new MethodCall("passivateObject", obj));
removeDestroyObjectCall(factory.getMethodCalls()); // The exact timing
of destroyObject is flexible here.
diff --git a/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
b/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
index e365e18..de0efb2 100644
--- a/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
+++ b/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
@@ -19,6 +19,7 @@ package org.apache.commons.pool2;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.InvocationHandler;
@@ -156,25 +157,17 @@ public class TestPoolUtils {
@Test
public void testCheckMinIdleKeyedObjectPool() throws Exception {
- try {
- PoolUtils.checkMinIdle(null, new Object(), 1, 1);
- fail("PoolUtils.checkMinIdle(KeyedObjectPool,Object,int,long) must
not allow null pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ 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<Object,Object> pool =
createProxy(KeyedObjectPool.class, (List<String>)null)) {
- PoolUtils.checkMinIdle(pool, (Object)null, 1, 1);
- fail("PoolUtils.checkMinIdle(KeyedObjectPool,Object,int,long) must
not accept null keys.");
- } catch (final IllegalArgumentException iae) {
- // expected
+ final KeyedObjectPool<Object, Object> pool =
createProxy(KeyedObjectPool.class, (List<String>) 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<Object,Object> pool =
createProxy(KeyedObjectPool.class, (List<String>)null)) {
- PoolUtils.checkMinIdle(pool, new Object(), -1, 1);
- fail("PoolUtils.checkMinIdle(KeyedObjectPool,Object,int,long) must
not accept negative min idle values.");
- } catch (final IllegalArgumentException iae) {
- // expected
+ final KeyedObjectPool<Object, Object> pool =
createProxy(KeyedObjectPool.class, (List<String>) null)) {
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.checkMinIdle(pool, new Object(), -1, 1),
+ "PoolUtils.checkMinIdle(KeyedObjectPool,Object,int,long)
must not accept negative min idle values.");
}
final List<String> calledMethods = new ArrayList<>();
@@ -182,10 +175,8 @@ public class TestPoolUtils {
// Test that the minIdle check doesn't add too many idle objects
@SuppressWarnings("unchecked")
- final KeyedPooledObjectFactory<Object,Object> kpof =
- createProxy(KeyedPooledObjectFactory.class, calledMethods);
- try (final KeyedObjectPool<Object,Object> kop =
- new GenericKeyedObjectPool<>(kpof)) {
+ final KeyedPooledObjectFactory<Object, Object> kpof =
createProxy(KeyedPooledObjectFactory.class, calledMethods);
+ try (final KeyedObjectPool<Object, Object> kop = new
GenericKeyedObjectPool<>(kpof)) {
PoolUtils.checkMinIdle(kop, key, 2, 100);
Thread.sleep(400);
assertEquals(2, kop.getNumIdle(key));
@@ -199,7 +190,7 @@ public class TestPoolUtils {
makeObjectCount++;
}
}
- assertEquals( 2, makeObjectCount,"makeObject should have been called
two time");
+ assertEquals(2, makeObjectCount, "makeObject should have been called
two time");
// Because this isn't deterministic and you can get false failures,
try more than once.
AssertionFailedError afe = null;
@@ -273,15 +264,13 @@ public class TestPoolUtils {
@Test
public void testCheckMinIdleKeyedObjectPoolKeysNulls() {
try (@SuppressWarnings("unchecked")
- final KeyedObjectPool<Object,Object> pool =
createProxy(KeyedObjectPool.class, (List<String>)null)) {
- PoolUtils.checkMinIdle(pool, (Collection<?>) null, 1, 1);
- fail("PoolUtils.checkMinIdle(KeyedObjectPool,Collection,int,long)
must not accept null keys.");
- } catch (final IllegalArgumentException iae) {
- // expected
+ final KeyedObjectPool<Object, Object> pool =
createProxy(KeyedObjectPool.class, (List<String>) 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<Object,Object> pool =
createProxy(KeyedObjectPool.class, (List<String>)null)) {
+ final KeyedObjectPool<Object, Object> pool =
createProxy(KeyedObjectPool.class, (List<String>) null)) {
PoolUtils.checkMinIdle(pool, (Collection<?>)
Collections.emptyList(), 1, 1);
} catch (final IllegalArgumentException iae) {
fail("PoolUtils.checkMinIdle(KeyedObjectPool,Collection,int,long)
must accept empty lists.");
@@ -290,18 +279,12 @@ public class TestPoolUtils {
@Test
public void testCheckMinIdleObjectPool() throws Exception {
- try {
- PoolUtils.checkMinIdle(null, 1, 1);
- fail("PoolUtils.checkMinIdle(ObjectPool,,) must not allow null
pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.checkMinIdle(null, 1, 1),
+ "PoolUtils.checkMinIdle(ObjectPool,,) must not allow null
pool.");
try (@SuppressWarnings("unchecked")
- final ObjectPool<Object> pool = createProxy(ObjectPool.class,
(List<String>) null)) {
- PoolUtils.checkMinIdle(pool, -1, 1);
- fail("PoolUtils.checkMinIdle(ObjectPool,,) must not accept
negative min idle values.");
- } catch (final IllegalArgumentException iae) {
- // expected
+ final ObjectPool<Object> pool = createProxy(ObjectPool.class,
(List<String>) null)) {
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.checkMinIdle(pool, -1, 1),
+ "PoolUtils.checkMinIdle(ObjectPool,,) must not accept
negative min idle values.");
}
final List<String> calledMethods = new ArrayList<>();
@@ -322,7 +305,7 @@ public class TestPoolUtils {
makeObjectCount++;
}
}
- assertEquals( 2, makeObjectCount,"makeObject should have been called
two time");
+ assertEquals(2, makeObjectCount, "makeObject should have been called
two time");
// Because this isn't deterministic and you can get false failures,
try more than once.
AssertionFailedError afe = null;
@@ -332,7 +315,7 @@ public class TestPoolUtils {
try {
calledMethods.clear();
try (@SuppressWarnings("unchecked")
- final ObjectPool<Object> pool =
createProxy(ObjectPool.class, calledMethods)) {
+ final ObjectPool<Object> 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.
@@ -396,26 +379,14 @@ public class TestPoolUtils {
@Test
public void testErodingPerKeyKeyedObjectPool() throws Exception {
- try (final KeyedObjectPool<Object, Object> erodingPool = PoolUtils
- .erodingPool((KeyedObjectPool<Object, Object>) null, 1f,
true)) {
- fail("PoolUtils.erodingPool(KeyedObjectPool) must not allow a null
pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null, 1f, true),
+ "PoolUtils.erodingPool(KeyedObjectPool) must not allow a null
pool.");
- try (final KeyedObjectPool<Object, Object> erodingPool = PoolUtils
- .erodingPool((KeyedObjectPool<Object, Object>) null, 0f,
true)) {
- fail("PoolUtils.erodingPool(ObjectPool, float, boolean) must not
allow a non-positive factor.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null, 0f, true),
+ "PoolUtils.erodingPool(ObjectPool, float, boolean) must not
allow a non-positive factor.");
- try (final KeyedObjectPool<Object, Object> erodingPool = PoolUtils
- .erodingPool((KeyedObjectPool<Object, Object>) null, 1f,
true)) {
- fail("PoolUtils.erodingPool(KeyedObjectPool, float, boolean) must
not allow a null pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null, 1f, true),
+ "PoolUtils.erodingPool(KeyedObjectPool, float, boolean) must
not allow a null pool.");
final List<String> calledMethods = new ArrayList<>();
final InvocationHandler handler = new MethodCallLogger(calledMethods) {
@@ -433,8 +404,7 @@ public class TestPoolUtils {
// 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<Object, Object> pool =
PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler),
- factor, true)) {
+ final KeyedObjectPool<Object, Object> pool =
PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), factor,
true)) {
final List<String> expectedMethods = new ArrayList<>();
assertEquals(expectedMethods, calledMethods);
@@ -481,26 +451,14 @@ public class TestPoolUtils {
@Test
public void testErodingPoolKeyedObjectPool() throws Exception {
- try (final KeyedObjectPool<Object, Object> erodingPool = PoolUtils
- .erodingPool((KeyedObjectPool<Object, Object>) null)) {
- fail("PoolUtils.erodingPool(KeyedObjectPool) must not allow a null
pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null),
+ "PoolUtils.erodingPool(KeyedObjectPool) must not allow a null
pool.");
- try (final KeyedObjectPool<Object, Object> erodingPool = PoolUtils
- .erodingPool((KeyedObjectPool<Object, Object>) null, 1f)) {
- fail("PoolUtils.erodingPool(KeyedObjectPool, float) must not allow
a null pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null, 1f),
+ "PoolUtils.erodingPool(KeyedObjectPool, float) must not allow
a null pool.");
- try (final KeyedObjectPool<Object, Object> erodingPool = PoolUtils
- .erodingPool((KeyedObjectPool<Object, Object>) null, 1f,
true)) {
- fail("PoolUtils.erodingPool(KeyedObjectPool, float, boolean) must
not allow a null pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null, 1f, true),
+ "PoolUtils.erodingPool(KeyedObjectPool, float, boolean) must
not allow a null pool.");
final List<String> calledMethods = new ArrayList<>();
final InvocationHandler handler = new MethodCallLogger(calledMethods) {
@@ -515,26 +473,17 @@ public class TestPoolUtils {
}
};
- try (@SuppressWarnings({ "unchecked" })
- final KeyedObjectPool<?, ?> o =
PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), 0f)) {
- fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a
non-positive factor.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), 0f),
+ "PoolUtils.erodingPool(ObjectPool, float) must not allow a
non-positive factor.");
- try (@SuppressWarnings({ "unchecked" })
- final KeyedObjectPool<?, ?> o =
PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), 0f, false)) {
- fail("PoolUtils.erodingPool(ObjectPool, float, boolean) must not
allow a non-positive factor.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), 0f, false),
+ "PoolUtils.erodingPool(ObjectPool, float, boolean) must not
allow a non-positive factor.");
// 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
final List<String> expectedMethods = new ArrayList<>();
try (@SuppressWarnings("unchecked")
- final KeyedObjectPool<Object, Object> pool =
PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler),
- factor)) {
+ final KeyedObjectPool<Object, Object> pool =
PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), factor)) {
assertEquals(expectedMethods, calledMethods);
@@ -603,17 +552,11 @@ public class TestPoolUtils {
@Test
public void testErodingPoolObjectPool() throws Exception {
- try (final ObjectPool<Object> erodingPool =
PoolUtils.erodingPool((ObjectPool<Object>) null)) {
- fail("PoolUtils.erodingPool(ObjectPool) must not allow a null
pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool((ObjectPool<Object>) null),
+ "PoolUtils.erodingPool(ObjectPool) must not allow a null
pool.");
- try (final ObjectPool<Object> erodingPool =
PoolUtils.erodingPool((ObjectPool<Object>) null, 1f)) {
- fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a
null pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool((ObjectPool<Object>) null, 1f),
+ "PoolUtils.erodingPool(ObjectPool, float) must not allow a
null pool.");
final List<String> calledMethods = new ArrayList<>();
final InvocationHandler handler = new MethodCallLogger(calledMethods) {
@@ -628,12 +571,8 @@ public class TestPoolUtils {
}
};
- try (@SuppressWarnings({ "unchecked" })
- final ObjectPool<?> o =
PoolUtils.erodingPool(createProxy(ObjectPool.class, handler), -1f)) {
- fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a
non-positive factor.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.erodingPool(createProxy(ObjectPool.class, handler), -1f),
+ "PoolUtils.erodingPool(ObjectPool, float) must not allow a
non-positive factor.");
// 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
@@ -698,23 +637,17 @@ public class TestPoolUtils {
@SuppressWarnings("deprecation")
@Test
public void testPrefillKeyedObjectPool() throws Exception {
- try {
- PoolUtils.prefill(null, new Object(), 1);
- fail("PoolUtils.prefill(KeyedObjectPool,Object,int) must not
accept null pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
- try (final KeyedObjectPool<Object, String> pool = new
GenericKeyedObjectPool<>(
- new TestGenericKeyedObjectPool.SimpleFactory<>())) {
- PoolUtils.prefill(pool, (Object) null, 1);
- fail("PoolUtils.prefill(KeyedObjectPool,Object,int) must not
accept null key.");
- } catch (final IllegalArgumentException iae) {
- // expected
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.prefill(null, new Object(), 1),
+ "PoolUtils.prefill(KeyedObjectPool,Object,int) must not accept
null pool.");
+
+ try (final KeyedObjectPool<Object, String> 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<String> calledMethods = new ArrayList<>();
try (@SuppressWarnings("unchecked")
- final KeyedObjectPool<Object, Object> pool =
createProxy(KeyedObjectPool.class, calledMethods)) {
+ final KeyedObjectPool<Object, Object> pool =
createProxy(KeyedObjectPool.class, calledMethods)) {
PoolUtils.prefill(pool, new Object(), 0);
final List<String> expectedMethods = new ArrayList<>();
@@ -732,10 +665,8 @@ public class TestPoolUtils {
public void testPrefillKeyedObjectPoolCollection() throws Exception {
try (@SuppressWarnings("unchecked")
final KeyedObjectPool<String, String> pool =
createProxy(KeyedObjectPool.class, (List<String>) null)) {
- PoolUtils.prefill(pool, (Collection<String>) null, 1);
- fail("PoolUtils.prefill(KeyedObjectPool,Collection,int) must not
accept null keys.");
- } catch (final IllegalArgumentException iae) {
- // expected
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.prefill(pool, (Collection<String>) null, 1),
+ "PoolUtils.prefill(KeyedObjectPool,Collection,int) must
not accept null keys.");
}
final List<String> calledMethods = new ArrayList<>();
@@ -761,16 +692,11 @@ public class TestPoolUtils {
@SuppressWarnings("deprecation")
@Test
public void testPrefillObjectPool() throws Exception {
- try {
- PoolUtils.prefill(null, 1);
- fail("PoolUtils.prefill(ObjectPool,int) must not allow null
pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.prefill(null, 1), "PoolUtils.prefill(ObjectPool,int) must not allow
null pool.");
final List<String> calledMethods = new ArrayList<>();
try (@SuppressWarnings("unchecked")
- final ObjectPool<Object> pool = createProxy(ObjectPool.class,
calledMethods)) {
+ final ObjectPool<Object> pool = createProxy(ObjectPool.class,
calledMethods)) {
PoolUtils.prefill(pool, 0);
final List<String> expectedMethods = new ArrayList<>();
@@ -785,17 +711,12 @@ public class TestPoolUtils {
@Test
public void testSynchronizedPoolableFactoryKeyedPoolableObjectFactory()
throws Exception {
- try {
-
PoolUtils.synchronizedKeyedPooledFactory((KeyedPooledObjectFactory<Object,
Object>) null);
-
fail("PoolUtils.synchronizedPoolableFactory(KeyedPoolableObjectFactory) must
not allow a null factory.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.synchronizedKeyedPooledFactory((KeyedPooledObjectFactory<Object,
Object>) null),
+
"PoolUtils.synchronizedPoolableFactory(KeyedPoolableObjectFactory) must not
allow a null factory.");
final List<String> calledMethods = new ArrayList<>();
@SuppressWarnings("unchecked")
- final KeyedPooledObjectFactory<Object, Object> kpof =
createProxy(KeyedPooledObjectFactory.class,
- calledMethods);
+ final KeyedPooledObjectFactory<Object, Object> kpof =
createProxy(KeyedPooledObjectFactory.class, calledMethods);
final KeyedPooledObjectFactory<Object, Object> skpof =
PoolUtils.synchronizedKeyedPooledFactory(kpof);
final List<String> expectedMethods = invokeEveryMethod(skpof);
@@ -806,12 +727,8 @@ public class TestPoolUtils {
@Test
public void testSynchronizedPoolableFactoryPoolableObjectFactory() throws
Exception {
- try {
- PoolUtils.synchronizedPooledFactory((PooledObjectFactory<Object>)
null);
- fail("PoolUtils.synchronizedPoolableFactory(PoolableObjectFactory)
must not allow a null factory.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.synchronizedPooledFactory((PooledObjectFactory<Object>) null),
+ "PoolUtils.synchronizedPoolableFactory(PoolableObjectFactory)
must not allow a null factory.");
final List<String> calledMethods = new ArrayList<>();
@SuppressWarnings("unchecked")
@@ -826,12 +743,8 @@ public class TestPoolUtils {
@Test
public void testSynchronizedPoolKeyedObjectPool() throws Exception {
- try (final KeyedObjectPool<Object, Object> synchronizedPool = PoolUtils
- .synchronizedPool((KeyedObjectPool<Object, Object>) null)) {
- fail("PoolUtils.synchronizedPool(KeyedObjectPool) must not allow a
null pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.synchronizedPool((KeyedObjectPool<Object, Object>) null),
+ "PoolUtils.synchronizedPool(KeyedObjectPool) must not allow a
null pool.");
final List<String> calledMethods = new ArrayList<>();
try (@SuppressWarnings("unchecked")
@@ -846,16 +759,12 @@ public class TestPoolUtils {
@Test
public void testSynchronizedPoolObjectPool() throws Exception {
- try (final ObjectPool<Object> synchronizedPool =
PoolUtils.synchronizedPool((ObjectPool<Object>) null)) {
- fail("PoolUtils.synchronizedPool(ObjectPool) must not allow a null
pool.");
- } catch (final IllegalArgumentException iae) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
PoolUtils.synchronizedPool((ObjectPool<Object>) null),
+ "PoolUtils.synchronizedPool(ObjectPool) must not allow a null
pool.");
final List<String> calledMethods = new ArrayList<>();
try (@SuppressWarnings("unchecked")
- final ObjectPool<Object> op = createProxy(ObjectPool.class,
calledMethods);
- final ObjectPool<Object> sop = PoolUtils.synchronizedPool(op))
{
+ final ObjectPool<Object> op = createProxy(ObjectPool.class,
calledMethods); final ObjectPool<Object> sop = PoolUtils.synchronizedPool(op)) {
final List<String> expectedMethods = invokeEveryMethod(sop);
assertEquals(expectedMethods, calledMethods);
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 91bb642..1fa9a91 100644
---
a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
+++
b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
@@ -308,8 +308,7 @@ public class TestGenericKeyedObjectPool extends
TestKeyedObjectPool {
@Override
public void run() {
try {
- final T obj = pool.borrowObject(key);
- pool.returnObject(key, obj);
+ pool.returnObject(key, pool.borrowObject(key));
} catch (final Exception e) {
// Ignore
}
@@ -1436,12 +1435,7 @@ public class TestGenericKeyedObjectPool extends
TestKeyedObjectPool {
gkoPool.returnObject("one", obj);
simpleFactory.setThrowExceptionOnValidate(true);
- try {
- gkoPool.evict();
- fail("Expecting RuntimeException");
- } catch (final RuntimeException e) {
- // expected
- }
+ assertThrows(RuntimeException.class, gkoPool::evict);
assertEquals(0, gkoPool.getNumActive());
assertEquals(0, gkoPool.getNumIdle());
}
@@ -1467,12 +1461,7 @@ public class TestGenericKeyedObjectPool extends
TestKeyedObjectPool {
simpleFactory.setValid(false);
// Validation will now fail on activation when borrowObject returns
// an idle instance, and then when attempting to create a new instance
- try {
- gkoPool.borrowObject("one");
- fail("Expecting NoSuchElementException");
- } catch (final NoSuchElementException ex) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
gkoPool.borrowObject("one"));
assertEquals(0, gkoPool.getNumActive("one"));
assertEquals(0, gkoPool.getNumIdle("one"));
assertEquals(0, gkoPool.getNumActive());
@@ -1488,12 +1477,7 @@ public class TestGenericKeyedObjectPool extends
TestKeyedObjectPool {
gkoPool.setTestOnBorrow(true);
gkoPool.borrowObject("one");
simpleFactory.setValid(false); // Make validation fail on next borrow
attempt
- try {
- gkoPool.borrowObject("one");
- fail("Expecting NoSuchElementException");
- } catch (final NoSuchElementException ex) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
gkoPool.borrowObject("one"));
assertEquals(1, gkoPool.getNumActive("one"));
assertEquals(0, gkoPool.getNumIdle("one"));
assertEquals(1, gkoPool.getNumActive());
@@ -1716,11 +1700,7 @@ public class TestGenericKeyedObjectPool extends
TestKeyedObjectPool {
pool.setMaxTotalPerKey(1);
pool.setBlockWhenExhausted(false);
factory.exceptionOnCreate = true;
- try {
- pool.borrowObject("One");
- } catch (final Exception ex) {
- // expected
- }
+ assertThrows(Exception.class, () -> pool.borrowObject("One"));
factory.exceptionOnCreate = false;
pool.borrowObject("One");
}
@@ -1794,12 +1774,7 @@ public class TestGenericKeyedObjectPool extends
TestKeyedObjectPool {
assertNotNull(o2);
final String o3 = gkoPool.borrowObject("b");
assertNotNull(o3);
- try {
- gkoPool.borrowObject("c");
- fail("Expected NoSuchElementException");
- } catch(final NoSuchElementException e) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
gkoPool.borrowObject("c"));
assertEquals(0, gkoPool.getNumIdle());
@@ -1904,12 +1879,7 @@ public class TestGenericKeyedObjectPool extends
TestKeyedObjectPool {
gkoPool.borrowObject("");
gkoPool.borrowObject("");
gkoPool.borrowObject("");
- try {
- gkoPool.borrowObject("");
- fail("Expected NoSuchElementException");
- } catch(final NoSuchElementException e) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
gkoPool.borrowObject(""));
}
@Test
@@ -1918,12 +1888,7 @@ public class TestGenericKeyedObjectPool extends
TestKeyedObjectPool {
gkoPool.setMaxTotalPerKey(0);
gkoPool.setBlockWhenExhausted(false);
- try {
- gkoPool.borrowObject("a");
- fail("Expected NoSuchElementException");
- } catch(final NoSuchElementException e) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
gkoPool.borrowObject("a"));
}
/**
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 4b7b980..2daa884 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
@@ -1110,16 +1110,12 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
}
// Wait for threads to finish
- for(int i=0;i<numThreads;i++) {
- while(!(threads[i]).complete()) {
- try {
- Thread.sleep(500L);
- } catch(final InterruptedException e) {
- // ignored
- }
+ for (int i = 0; i < numThreads; i++) {
+ while (!(threads[i]).complete()) {
+ Waiter.sleepQuietly(500L);
}
- if(threads[i].failed()) {
- fail("Thread "+i+" failed: "+threads[i].error.toString());
+ if (threads[i].failed()) {
+ fail("Thread " + i + " failed: " +
threads[i].error.toString());
}
}
}
@@ -1127,10 +1123,11 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
/**
* On first borrow, first object fails validation, second object is OK.
* Subsequent borrows are OK. This was POOL-152.
+ * @throws Exception
*/
@Test
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
- public void testBrokenFactoryShouldNotBlockPool() {
+ public void testBrokenFactoryShouldNotBlockPool() throws Exception {
final int maxTotal = 1;
simpleFactory.setMaxTotal(maxTotal);
@@ -1157,17 +1154,9 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
simpleFactory.setValid(true);
// Subsequent borrows should be OK
- try {
- obj = genericObjectPool.borrowObject();
- } catch (final Exception e1) {
- fail();
- }
+ obj = genericObjectPool.borrowObject();
assertNotNull(obj);
- try {
- genericObjectPool.returnObject(obj);
- } catch (final Exception e) {
- fail();
- }
+ genericObjectPool.returnObject(obj);
}
// POOL-259
@@ -1613,36 +1602,19 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
genericObjectPool.setTestWhileIdle(true);
// ClassNotFoundException
- try {
-
genericObjectPool.setEvictionPolicyClassName(Long.toString(System.currentTimeMillis()));
- fail("setEvictionPolicyClassName must throw an error if the class
name is invalid.");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
genericObjectPool.setEvictionPolicyClassName(Long.toString(System.currentTimeMillis())),
+ "setEvictionPolicyClassName must throw an error if the class
name is invalid.");
// InstantiationException
- try {
-
genericObjectPool.setEvictionPolicyClassName(java.io.Serializable.class.getName());
- fail("setEvictionPolicyClassName must throw an error if the class
name is invalid.");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
genericObjectPool.setEvictionPolicyClassName(java.io.Serializable.class.getName()),
+ "setEvictionPolicyClassName must throw an error if the class
name is invalid.");
// IllegalAccessException
- try {
-
genericObjectPool.setEvictionPolicyClassName(java.util.Collections.class.getName());
- fail("setEvictionPolicyClassName must throw an error if the class
name is invalid.");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
genericObjectPool.setEvictionPolicyClassName(java.util.Collections.class.getName()),
+ "setEvictionPolicyClassName must throw an error if the class
name is invalid.");
- try {
-
genericObjectPool.setEvictionPolicyClassName(java.lang.String.class.getName());
- fail("setEvictionPolicyClassName must throw an error if a class
that does not "
- + "implement EvictionPolicy is specified.");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () ->
genericObjectPool.setEvictionPolicyClassName(java.lang.String.class.getName()),
+ () -> "setEvictionPolicyClassName must throw an error if a
class that does not implement EvictionPolicy is specified.");
genericObjectPool.setEvictionPolicy(new TestEvictionPolicy<>());
assertEquals(TestEvictionPolicy.class.getName(),
genericObjectPool.getEvictionPolicyClassName());
@@ -1793,12 +1765,7 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
simpleFactory.setThrowExceptionOnValidate(true);
- try {
- genericObjectPool.evict();
- fail("Expecting RuntimeException");
- } catch (final RuntimeException e) {
- // expected
- }
+ assertThrows(RuntimeException.class, () -> genericObjectPool.evict());
assertEquals(0, genericObjectPool.getNumActive());
assertEquals(0, genericObjectPool.getNumIdle());
}
@@ -1822,12 +1789,7 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
simpleFactory.setValid(false);
// Validation will now fail on activation when borrowObject returns
// an idle instance, and then when attempting to create a new instance
- try {
- genericObjectPool.borrowObject();
- fail("Expecting NoSuchElementException");
- } catch (final NoSuchElementException ex) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
genericObjectPool.borrowObject());
assertEquals(0, genericObjectPool.getNumActive());
assertEquals(0, genericObjectPool.getNumIdle());
}
@@ -1839,12 +1801,7 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
genericObjectPool.setTestOnBorrow(true);
genericObjectPool.borrowObject();
simpleFactory.setValid(false); // Make validation fail on next borrow
attempt
- try {
- genericObjectPool.borrowObject();
- fail("Expecting NoSuchElementException");
- } catch (final NoSuchElementException ex) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
genericObjectPool.borrowObject());
assertEquals(1, genericObjectPool.getNumActive());
assertEquals(0, genericObjectPool.getNumIdle());
}
@@ -2209,12 +2166,7 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
genericObjectPool.borrowObject();
genericObjectPool.borrowObject();
genericObjectPool.borrowObject();
- try {
- genericObjectPool.borrowObject();
- fail("Expected NoSuchElementException");
- } catch(final NoSuchElementException e) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
genericObjectPool.borrowObject());
}
/**
@@ -2305,13 +2257,7 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
public void testMaxTotalZero() throws Exception {
genericObjectPool.setMaxTotal(0);
genericObjectPool.setBlockWhenExhausted(false);
-
- try {
- genericObjectPool.borrowObject();
- fail("Expected NoSuchElementException");
- } catch(final NoSuchElementException e) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
genericObjectPool.borrowObject());
}
/*
@@ -2609,12 +2555,8 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
Thread.sleep(100);
// another one tries borrowObject. It should return within
maxWaitMillis.
- try {
- createSlowObjectFactoryPool.borrowObject(maxWaitMillis);
- fail("borrowObject must fail due to timeout by maxWaitMillis");
- } catch (final NoSuchElementException e) {
- // ignore
- }
+ assertThrows(NoSuchElementException.class, () ->
createSlowObjectFactoryPool.borrowObject(maxWaitMillis),
+ "borrowObject must fail due to timeout by maxWaitMillis");
assertTrue(thread1.isAlive());
}
@@ -2832,12 +2774,7 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
genericObjectPool.swallowException(e1);
genericObjectPool.swallowException(e2);
- try {
- genericObjectPool.swallowException(e1);
- fail("Not supposed to get here");
- } catch (final OutOfMemoryError oom) {
- // expected
- }
+ assertThrows(OutOfMemoryError.class, () ->
genericObjectPool.swallowException(e1));
assertEquals(2, swallowedExceptions.size());
}
@@ -2859,12 +2796,7 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
genericObjectPool.setBlockWhenExhausted(true);
final String obj = genericObjectPool.borrowObject();
final String obj2 = genericObjectPool.borrowObject();
- try {
- genericObjectPool.borrowObject();
- fail("Expecting NoSuchElementException");
- } catch (final NoSuchElementException ex) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
genericObjectPool.borrowObject());
genericObjectPool.returnObject(obj2);
genericObjectPool.returnObject(obj);
@@ -2970,12 +2902,7 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
genericObjectPool.setMaxWaitMillis(10L);
final String obj1 = genericObjectPool.borrowObject();
assertNotNull(obj1);
- try {
- genericObjectPool.borrowObject();
- fail("Expected NoSuchElementException");
- } catch(final NoSuchElementException e) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
genericObjectPool.borrowObject());
genericObjectPool.returnObject(obj1);
genericObjectPool.close();
}
@@ -3043,9 +2970,9 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
genericObjectPool.setMaxWaitMillis(10L);
String obj2 = null;
try {
- obj2 = genericObjectPool.borrowObject();
+ obj2 = genericObjectPool.borrowObject();
assertNotNull(obj2);
- } catch(final NoSuchElementException e) {
+ } catch (final NoSuchElementException e) {
// Not expected
fail("NoSuchElementException not expected");
}
@@ -3061,12 +2988,7 @@ public class TestGenericObjectPool extends
TestBaseObjectPool {
genericObjectPool.setBlockWhenExhausted(false);
final String obj1 = genericObjectPool.borrowObject();
assertNotNull(obj1);
- try {
- genericObjectPool.borrowObject();
- fail("Expected NoSuchElementException");
- } catch(final NoSuchElementException e) {
- // expected
- }
+ assertThrows(NoSuchElementException.class, () ->
genericObjectPool.borrowObject());
genericObjectPool.returnObject(obj1);
assertEquals(1, genericObjectPool.getNumIdle());
genericObjectPool.close();
diff --git
a/src/test/java/org/apache/commons/pool2/impl/TestLinkedBlockingDeque.java
b/src/test/java/org/apache/commons/pool2/impl/TestLinkedBlockingDeque.java
index 0916b5d..08615b9 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestLinkedBlockingDeque.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestLinkedBlockingDeque.java
@@ -16,10 +16,10 @@
*/
package org.apache.commons.pool2.impl;
-
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -35,7 +35,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
-
/**
* Tests for {@link LinkedBlockingDeque}.
*/
@@ -57,14 +56,8 @@ public class TestLinkedBlockingDeque {
public void testAdd() {
assertTrue(deque.add(ONE));
assertTrue(deque.add(TWO));
- try {
- assertTrue(deque.add(THREE));
- fail("Not supposed to get here");
- } catch (final IllegalStateException e) {}
- try {
- assertTrue(deque.add(null));
- fail("Not supposed to get here");
- } catch (final NullPointerException e) {}
+ assertThrows(IllegalStateException.class, () -> deque.add(THREE));
+ assertThrows(NullPointerException.class, () -> deque.add(null));
}
@Test
@@ -72,10 +65,7 @@ public class TestLinkedBlockingDeque {
deque.addFirst(ONE);
deque.addFirst(TWO);
assertEquals(2, deque.size());
- try {
- deque.addFirst(THREE);
- fail("Not supposed to get here");
- } catch (final IllegalStateException e) {}
+ assertThrows(IllegalStateException.class, () -> deque.add(THREE));
assertEquals(Integer.valueOf(2), deque.pop());
}
@@ -84,10 +74,7 @@ public class TestLinkedBlockingDeque {
deque.addLast(ONE);
deque.addLast(TWO);
assertEquals(2, deque.size());
- try {
- deque.addLast(THREE);
- fail("Not supposed to get here");
- } catch (final IllegalStateException e) {}
+ assertThrows(IllegalStateException.class, () -> deque.add(THREE));
assertEquals(Integer.valueOf(1), deque.pop());
}
@@ -111,12 +98,7 @@ public class TestLinkedBlockingDeque {
deque = new LinkedBlockingDeque<>(Arrays.asList(ONE, TWO));
assertEquals(2, deque.size());
- try {
- deque = new LinkedBlockingDeque<>(Arrays.asList(ONE, null));
- fail("Not supposed to get here");
- } catch (final NullPointerException npe) {
- // OK
- }
+ assertThrows(NullPointerException.class, () -> new
LinkedBlockingDeque<>(Arrays.asList(ONE, null)));
}
@Test
@@ -132,10 +114,7 @@ public class TestLinkedBlockingDeque {
@Test
public void testDescendingIterator() {
- try {
- deque.descendingIterator().next();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e) {}
+ assertThrows(NoSuchElementException.class, () ->
deque.descendingIterator().next());
deque.add(ONE);
deque.add(TWO);
final Iterator<Integer> iter = deque.descendingIterator();
@@ -163,10 +142,7 @@ public class TestLinkedBlockingDeque {
@Test
public void testElement() {
- try {
- deque.element();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e){}
+ assertThrows(NoSuchElementException.class, () -> deque.element());
deque.add(ONE);
deque.add(TWO);
assertEquals(Integer.valueOf(1), deque.element());
@@ -174,10 +150,7 @@ public class TestLinkedBlockingDeque {
@Test
public void testGetFirst() {
- try {
- deque.getFirst();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e){}
+ assertThrows(NoSuchElementException.class, () -> deque.getFirst());
deque.add(ONE);
deque.add(TWO);
assertEquals(Integer.valueOf(1), deque.getFirst());
@@ -185,10 +158,7 @@ public class TestLinkedBlockingDeque {
@Test
public void testGetLast() {
- try {
- deque.getLast();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e){}
+ assertThrows(NoSuchElementException.class, () -> deque.getLast());
deque.add(ONE);
deque.add(TWO);
assertEquals(Integer.valueOf(2), deque.getLast());
@@ -196,10 +166,7 @@ public class TestLinkedBlockingDeque {
@Test
public void testIterator() {
- try {
- deque.iterator().next();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e) {}
+ assertThrows(NoSuchElementException.class, () ->
deque.iterator().next());
deque.add(ONE);
deque.add(TWO);
final Iterator<Integer> iter = deque.iterator();
@@ -213,10 +180,7 @@ public class TestLinkedBlockingDeque {
assertTrue(deque.offer(ONE));
assertTrue(deque.offer(TWO));
assertFalse(deque.offer(THREE));
- try {
- deque.offer(null);
- fail("Not supposed to get here");
- } catch (final NullPointerException e) {}
+ assertThrows(NullPointerException.class, () -> deque.offer(null));
}
@Test
@@ -224,19 +188,13 @@ public class TestLinkedBlockingDeque {
deque.offerFirst(ONE);
deque.offerFirst(TWO);
assertEquals(2, deque.size());
- try {
- deque.offerFirst(null);
- fail("Not supposed to get here");
- } catch (final NullPointerException e) {}
+ assertThrows(NullPointerException.class, () -> deque.offerFirst(null));
assertEquals(Integer.valueOf(2), deque.pop());
}
@Test
public void testOfferFirstWithTimeout() throws InterruptedException {
- try {
- deque.offerFirst(null);
- fail("Not supposed to get here");
- } catch (final NullPointerException e) {}
+ assertThrows(NullPointerException.class, () -> deque.offerFirst(null,
TIMEOUT_50_MILLIS));
assertTrue(deque.offerFirst(ONE, TIMEOUT_50_MILLIS));
assertTrue(deque.offerFirst(TWO, TIMEOUT_50_MILLIS));
assertFalse(deque.offerFirst(THREE, TIMEOUT_50_MILLIS));
@@ -247,19 +205,13 @@ public class TestLinkedBlockingDeque {
deque.offerLast(ONE);
deque.offerLast(TWO);
assertEquals(2, deque.size());
- try {
- deque.offerLast(null);
- fail("Not supposed to get here");
- } catch (final NullPointerException e) {}
+ assertThrows(NullPointerException.class, () -> deque.offerLast(null));
assertEquals(Integer.valueOf(1), deque.pop());
}
@Test
public void testOfferLastWithTimeout() throws InterruptedException {
- try {
- deque.offerLast(null);
- fail("Not supposed to get here");
- } catch (final NullPointerException e) {}
+ assertThrows(NullPointerException.class, () -> deque.offerLast(null,
TIMEOUT_50_MILLIS));
assertTrue(deque.offerLast(ONE, TIMEOUT_50_MILLIS));
assertTrue(deque.offerLast(TWO, TIMEOUT_50_MILLIS));
assertFalse(deque.offerLast(THREE, TIMEOUT_50_MILLIS));
@@ -270,10 +222,7 @@ public class TestLinkedBlockingDeque {
assertTrue(deque.offer(ONE, TIMEOUT_50_MILLIS));
assertTrue(deque.offer(TWO, TIMEOUT_50_MILLIS));
assertFalse(deque.offer(THREE, TIMEOUT_50_MILLIS));
- try {
- deque.offer(null, TIMEOUT_50_MILLIS);
- fail("Not supposed to get here");
- } catch (final NullPointerException e) {}
+ assertThrows(NullPointerException.class, () -> deque.offer(null,
TIMEOUT_50_MILLIS));
}
@Test
@@ -336,18 +285,14 @@ public class TestLinkedBlockingDeque {
@Test
public void testPop() {
- try {
- deque.pop();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e) {}
+ assertThrows(NoSuchElementException.class, () -> deque.pop());
deque.add(ONE);
deque.add(TWO);
assertEquals(Integer.valueOf(1), deque.pop());
- try {
+ assertThrows(NoSuchElementException.class, () -> {
deque.pop();
deque.pop();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e) {}
+ });
}
/*
@@ -382,29 +327,20 @@ public class TestLinkedBlockingDeque {
deque.push(ONE);
deque.push(TWO);
assertEquals(2, deque.size());
- try {
- deque.push(THREE);
- fail("Not supposed to get here");
- } catch (final IllegalStateException e) {}
+ assertThrows(IllegalStateException.class, () -> deque.push(THREE));
assertEquals(Integer.valueOf(2), deque.pop());
}
@Test
public void testPut() throws InterruptedException {
- try {
- deque.put(null);
- fail("Not supposed to get here");
- } catch (final NullPointerException e) {}
+ assertThrows(NullPointerException.class, () -> deque.put(null));
deque.put(ONE);
deque.put(TWO);
}
@Test
public void testPutFirst() throws InterruptedException {
- try {
- deque.putFirst(null);
- fail("Not supposed to get here");
- } catch (final NullPointerException e) {}
+ assertThrows(NullPointerException.class, () -> deque.putFirst(null));
deque.putFirst(ONE);
deque.putFirst(TWO);
assertEquals(2, deque.size());
@@ -413,10 +349,7 @@ public class TestLinkedBlockingDeque {
@Test
public void testPutLast() throws InterruptedException {
- try {
- deque.putLast(null);
- fail("Not supposed to get here");
- } catch (final NullPointerException e) {}
+ assertThrows(NullPointerException.class, () -> deque.putLast(null));
deque.putLast(ONE);
deque.putLast(TWO);
assertEquals(2, deque.size());
@@ -425,10 +358,7 @@ public class TestLinkedBlockingDeque {
@Test
public void testRemove() {
- try {
- deque.remove();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e) {}
+ assertThrows(NoSuchElementException.class, deque::remove);
deque.add(ONE);
deque.add(TWO);
assertEquals(Integer.valueOf(1), deque.remove());
@@ -436,34 +366,26 @@ public class TestLinkedBlockingDeque {
@Test
public void testRemoveFirst() {
- try {
- deque.removeFirst();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e) {}
+ assertThrows(NoSuchElementException.class, deque::removeFirst);
deque.add(ONE);
deque.add(TWO);
assertEquals(Integer.valueOf(1), deque.removeFirst());
- try {
+ assertThrows(NoSuchElementException.class, () -> {
deque.removeFirst();
deque.removeFirst();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e) {}
+ });
}
@Test
public void testRemoveLast() {
- try {
- deque.removeLast();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e) {}
+ assertThrows(NoSuchElementException.class, deque::removeLast);
deque.add(ONE);
deque.add(TWO);
assertEquals(Integer.valueOf(2), deque.removeLast());
- try {
+ assertThrows(NoSuchElementException.class, () -> {
deque.removeLast();
deque.removeLast();
- fail("Not supposed to get here");
- } catch (final NoSuchElementException e) {}
+ });
}
@Test