Example usage for org.apache.commons.pool MethodCallPoolableObjectFactory setDestroyObjectFail

List of usage examples for org.apache.commons.pool MethodCallPoolableObjectFactory setDestroyObjectFail

Introduction

In this page you can find the example usage for org.apache.commons.pool MethodCallPoolableObjectFactory setDestroyObjectFail.

Prototype

public void setDestroyObjectFail(final boolean destroyObjectFail) 

Source Link

Usage

From source file:edu.illinois.enforcemop.examples.apache.pool.TestObjectPool.java

public void testPOFClearUsages() throws Exception {
    final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
    final ObjectPool pool;
    try {/*www . j a va  2  s  .  c  om*/
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();

    /// Test correct behavior code paths
    PoolUtils.prefill(pool, 5);
    pool.clear();

    //// Test exception handling clear should swallow destory object failures
    reset(pool, factory, expectedMethods);
    factory.setDestroyObjectFail(true);
    PoolUtils.prefill(pool, 5);
    pool.clear();
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestObjectPool.java

public void testPOFCloseUsages() throws Exception {
    final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
    ObjectPool pool;/*from  ww  w  .  ja va  2 s  .  c  om*/
    try {
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();

    /// Test correct behavior code paths
    PoolUtils.prefill(pool, 5);
    pool.close();

    //// Test exception handling close should swallow failures
    try {
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    reset(pool, factory, expectedMethods);
    factory.setDestroyObjectFail(true);
    PoolUtils.prefill(pool, 5);
    pool.close();
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestObjectPool.java

public void testPOFInvalidateObjectUsages() throws Exception {
    final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
    final ObjectPool pool;
    try {//  w w w .  j a  va2 s  . co  m
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();
    Object obj;

    /// Test correct behavior code paths

    obj = pool.borrowObject();
    clear(factory, expectedMethods);

    // invalidated object should be destroyed
    pool.invalidateObject(obj);
    expectedMethods.add(new MethodCall("destroyObject", obj));
    assertEquals(expectedMethods, factory.getMethodCalls());

    //// Test exception handling of invalidateObject
    reset(pool, factory, expectedMethods);
    obj = pool.borrowObject();
    clear(factory, expectedMethods);
    factory.setDestroyObjectFail(true);
    try {
        pool.invalidateObject(obj);
        fail("Expecting destroy exception to propagate");
    } catch (PrivateException ex) {
        // Expected
    }
    Thread.sleep(250); // could be defered
    removeDestroyObjectCall(factory.getMethodCalls());
    assertEquals(expectedMethods, factory.getMethodCalls());
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestObjectPool.java

public void testPOFReturnObjectUsages() throws Exception {
    final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
    final ObjectPool pool;
    try {/* ww w .  j  a  v a2s. c  o  m*/
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();
    Object obj;

    /// Test correct behavior code paths
    obj = pool.borrowObject();
    clear(factory, expectedMethods);

    // returned object should be passivated
    pool.returnObject(obj);
    // StackObjectPool, SoftReferenceObjectPool also validate on return
    if (pool instanceof StackObjectPool || pool instanceof SoftReferenceObjectPool) {
        expectedMethods.add(new MethodCall("validateObject", obj).returned(Boolean.TRUE));
    }
    expectedMethods.add(new MethodCall("passivateObject", obj));
    assertEquals(expectedMethods, factory.getMethodCalls());

    //// Test exception handling of returnObject
    reset(pool, factory, expectedMethods);
    pool.addObject();
    pool.addObject();
    pool.addObject();
    assertEquals(3, pool.getNumIdle());
    // passivateObject should swallow exceptions and not add the object to the pool
    obj = pool.borrowObject();
    pool.borrowObject();
    assertEquals(1, pool.getNumIdle());
    assertEquals(2, pool.getNumActive());
    clear(factory, expectedMethods);
    factory.setPassivateObjectFail(true);
    pool.returnObject(obj);
    // StackObjectPool, SoftReferenceObjectPool also validate on return
    if (pool instanceof StackObjectPool || pool instanceof SoftReferenceObjectPool) {
        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.
    assertEquals(expectedMethods, factory.getMethodCalls());
    assertEquals(1, pool.getNumIdle()); // Not returned
    assertEquals(1, pool.getNumActive()); // But not in active count

    // destroyObject should swallow exceptions too
    reset(pool, factory, expectedMethods);
    obj = pool.borrowObject();
    clear(factory, expectedMethods);
    factory.setPassivateObjectFail(true);
    factory.setDestroyObjectFail(true);
    pool.returnObject(obj);
}