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

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

Introduction

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

Prototype

int getNumActive() throws UnsupportedOperationException;

Source Link

Document

Returns the total number of instances current borrowed from this pool but not yet returned (optional operation).

Usage

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 w w .  j av  a2s .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

}

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

public void testClosedPoolBehavior() throws Exception {
    final KeyedObjectPool pool;
    try {//from   w  w  w  .jav a2 s .  co  m
        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();
}