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

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

Introduction

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

Prototype

public List getMethodCalls() 

Source Link

Usage

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

private static void clear(final MethodCallPoolableObjectFactory factory, final List expectedMethods) {
    factory.getMethodCalls().clear();
    expectedMethods.clear();//  ww w.ja  v  a2  s .c  om
}

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 {//from ww w.ja v a2  s  .  c  om
        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 testPOFBorrowObjectUsages() throws Exception {
    final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
    final ObjectPool pool;
    try {/*w  w  w  . j ava  2 s  .c o  m*/
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    if (pool instanceof GenericObjectPool) {
        ((GenericObjectPool) pool).setTestOnBorrow(true);
    }
    final List expectedMethods = new ArrayList();
    Object obj;

    /// Test correct behavior code paths

    // existing idle object should be activated and validated
    pool.addObject();
    clear(factory, expectedMethods);
    obj = pool.borrowObject();
    expectedMethods.add(new MethodCall("activateObject", ZERO));
    expectedMethods.add(new MethodCall("validateObject", ZERO).returned(Boolean.TRUE));
    assertEquals(expectedMethods, factory.getMethodCalls());
    pool.returnObject(obj);

    //// Test exception handling of borrowObject
    reset(pool, factory, expectedMethods);

    // 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 (PrivateException pe) {
        // expected
    }
    expectedMethods.add(new MethodCall("makeObject"));
    assertEquals(expectedMethods, factory.getMethodCalls());

    // when activateObject fails in borrowObject, a new object should be borrowed/created
    reset(pool, factory, expectedMethods);
    pool.addObject();
    clear(factory, expectedMethods);

    factory.setActivateObjectFail(true);
    expectedMethods.add(new MethodCall("activateObject", obj));
    try {
        obj = pool.borrowObject();
        fail("Expecting NoSuchElementException");
    } catch (NoSuchElementException ex) {
        // Expected - newly created object will also fail to activate
    }
    // Idle object fails activation, new one created, also fails
    expectedMethods.add(new MethodCall("makeObject").returned(ONE));
    expectedMethods.add(new MethodCall("activateObject", ONE));
    removeDestroyObjectCall(factory.getMethodCalls()); // The exact timing of destroyObject is flexible here.
    assertEquals(expectedMethods, factory.getMethodCalls());

    // when validateObject fails in borrowObject, a new object should be borrowed/created
    reset(pool, factory, expectedMethods);
    pool.addObject();
    clear(factory, expectedMethods);

    factory.setValidateObjectFail(true);
    expectedMethods.add(new MethodCall("activateObject", ZERO));
    expectedMethods.add(new MethodCall("validateObject", ZERO));
    try {
        obj = pool.borrowObject();
    } catch (NoSuchElementException ex) {
        // Expected - newly created object will also fail to validate
    }
    // Idle object is activated, but fails validation.
    // New instance is created, activated and then fails validation
    expectedMethods.add(new MethodCall("makeObject").returned(ONE));
    expectedMethods.add(new MethodCall("activateObject", ONE));
    expectedMethods.add(new MethodCall("validateObject", ONE));
    removeDestroyObjectCall(factory.getMethodCalls()); // The exact timing of destroyObject is flexible here.
    // Second activate and validate are missing from expectedMethods
    assertTrue(factory.getMethodCalls().containsAll(expectedMethods));
}

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 {// w  w  w  . j  a v  a 2 s.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);
}

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

public void testPOFAddObjectUsage() throws Exception {
    final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
    final ObjectPool pool;
    try {/*from   w w w  . j  ava  2 s.c  o m*/
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();

    assertEquals(0, pool.getNumActive());
    assertEquals(0, pool.getNumIdle());
    // addObject should make a new object, pasivate it and put it in the pool
    pool.addObject();
    assertEquals(0, pool.getNumActive());
    assertEquals(1, pool.getNumIdle());
    expectedMethods.add(new MethodCall("makeObject").returned(ZERO));
    // StackObjectPool, SoftReferenceObjectPool also validate on add
    if (pool instanceof StackObjectPool || pool instanceof SoftReferenceObjectPool) {
        expectedMethods.add(new MethodCall("validateObject", ZERO).returned(Boolean.TRUE));
    }
    expectedMethods.add(new MethodCall("passivateObject", ZERO));
    assertEquals(expectedMethods, factory.getMethodCalls());

    //// Test exception handling of addObject
    reset(pool, factory, expectedMethods);

    // makeObject Exceptions should be propagated to client code from addObject
    factory.setMakeObjectFail(true);
    try {
        pool.addObject();
        fail("Expected addObject to propagate makeObject exception.");
    } catch (PrivateException pe) {
        // expected
    }
    expectedMethods.add(new MethodCall("makeObject"));
    assertEquals(expectedMethods, factory.getMethodCalls());

    clear(factory, expectedMethods);

    // 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 (PrivateException pe) {
        // expected
    }
    expectedMethods.add(new MethodCall("makeObject").returned(ONE));
    // StackObjectPool, SofReferenceObjectPool also validate on add
    if (pool instanceof StackObjectPool || pool instanceof SoftReferenceObjectPool) {
        expectedMethods.add(new MethodCall("validateObject", ONE).returned(Boolean.TRUE));
    }
    expectedMethods.add(new MethodCall("passivateObject", ONE));
    assertEquals(expectedMethods, factory.getMethodCalls());
}