Skip to content
Permalink
Browse files
Use assertThrows.
  • Loading branch information
Gary Gregory committed Aug 14, 2021
1 parent 057eed6 commit b5bf4b1d8c08c74992a961b388e5542b75397d91
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 535 deletions.
@@ -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 void testBaseClosePool() throws Exception {
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 void testUnsupportedOperations() throws Exception {
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);
}
}
}
@@ -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 void testClosedPoolBehavior() throws Exception {

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 void testKPOFAddObjectUsage() throws Exception {

// 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 void testKPOFAddObjectUsage() throws Exception {
// 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 void testKPOFBorrowObjectUsages() throws Exception {

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 void testKPOFBorrowObjectUsages() throws Exception {
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 void testKPOFInvalidateObjectUsages() throws Exception {

//// 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());
@@ -18,6 +18,7 @@


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 void testClosedPoolBehavior() throws Exception {

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 void testPOFAddObjectUsage() throws Exception {

// 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 void testPOFAddObjectUsage() throws Exception {
// 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 void testPOFBorrowObjectUsages() throws Exception {

// 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 void testPOFBorrowObjectUsages() throws Exception {

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 void testPOFBorrowObjectUsages() throws Exception {
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 void testPOFInvalidateObjectUsages() throws Exception {

//// 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 void testPOFReturnObjectUsages() throws Exception {
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.

0 comments on commit b5bf4b1

Please sign in to comment.