Example usage for org.apache.commons.pool KeyedObjectPool close

List of usage examples for org.apache.commons.pool KeyedObjectPool close

Introduction

In this page you can find the example usage for org.apache.commons.pool KeyedObjectPool close.

Prototype

void close() throws Exception;

Source Link

Document

Close this pool, and free any resources associated with it.

Usage

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

public void testKPOFCloseUsages() throws Exception {
    final FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
    KeyedObjectPool pool;
    try {//from ww w  . ja  v  a 2 s.co  m
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();

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

    //// Test exception handling close should swallow failures
    pool = makeEmptyPool(factory);
    reset(pool, factory, expectedMethods);
    factory.setDestroyObjectFail(true);
    PoolUtils.prefill(pool, KEY, 5);
    pool.close();
}

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

public void testClosedPoolBehavior() throws Exception {
    final KeyedObjectPool pool;
    try {/*from  w ww.ja  va 2 s  .com*/
        pool = makeEmptyPool(new BaseKeyedPoolableObjectFactory() {
            public Object makeObject(final Object key) throws Exception {
                return new Object();
            }
        });
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }

    Object o1 = pool.borrowObject(KEY);
    Object o2 = pool.borrowObject(KEY);

    pool.close();

    try {
        pool.addObject(KEY);
        fail("A closed pool must throw an IllegalStateException when addObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    try {
        pool.borrowObject(KEY);
        fail("A closed pool must throw an IllegalStateException when borrowObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    // The following should not throw exceptions just because the pool is closed.
    assertEquals("A closed pool shouldn't have any idle objects.", 0, pool.getNumIdle(KEY));
    assertEquals("A closed pool shouldn't have any idle objects.", 0, pool.getNumIdle());
    pool.getNumActive();
    pool.getNumActive(KEY);
    pool.returnObject(KEY, o1);
    assertEquals("returnObject should not add items back into the idle object pool for a closed pool.", 0,
            pool.getNumIdle(KEY));
    assertEquals("returnObject should not add items back into the idle object pool for a closed pool.", 0,
            pool.getNumIdle());
    pool.invalidateObject(KEY, o2);
    pool.clear(KEY);
    pool.clear();
    pool.close();
}

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

public void testUnsupportedOperations() throws Exception {
    if (!getClass().equals(TestBaseKeyedObjectPool.class)) {
        return; // skip redundant tests
    }//w  ww  .ja va2 s  . c o  m
    KeyedObjectPool pool = new BaseKeyedObjectPool() {
        public Object borrowObject(Object key) {
            return null;
        }

        public void returnObject(Object key, Object obj) {
        }

        public void invalidateObject(Object key, Object obj) {
        }
    };

    try {
        pool.addObject("key");
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
        // expected
    }

    assertTrue("Negative expected.", pool.getNumIdle() < 0);
    assertTrue("Negative expected.", pool.getNumIdle("key") < 0);
    assertTrue("Negative expected.", pool.getNumActive() < 0);
    assertTrue("Negative expected.", pool.getNumActive("key") < 0);

    try {
        pool.clear();
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
        // expected
    }

    try {
        pool.clear("key");
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
        // expected
    }

    try {
        pool.setFactory(null);
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
        // expected
    }

    pool.close(); // a no-op, probably should be remove

}