Example usage for org.apache.commons.pool PoolableObjectFactory validateObject

List of usage examples for org.apache.commons.pool PoolableObjectFactory validateObject

Introduction

In this page you can find the example usage for org.apache.commons.pool PoolableObjectFactory validateObject.

Prototype

boolean validateObject(Object p0);

Source Link

Usage

From source file:gov.nih.nci.firebird.proxy.PoolingHandlerTest.java

@SuppressWarnings("unchecked")
private PoolableObjectFactory<Object> makeMockFactory() {
    PoolableObjectFactory<Object> factory = mock(PoolableObjectFactory.class);
    when(factory.validateObject(any())).thenReturn(Boolean.TRUE);
    try {//w ww  . jav a  2s  . c  o m
        when(factory.makeObject()).thenAnswer(new Answer<ITestClient>() {
            @Override
            public ITestClient answer(InvocationOnMock invocation) throws Throwable {
                return makeBaseMockClient();
            }
        });
    } catch (Exception e) {
        //will never happen here.
    }
    return factory;
}

From source file:gov.nih.nci.firebird.proxy.PoolingHandlerTest.java

@Test
@SuppressWarnings("unchecked")
public void testNormalInvocation() throws Exception {
    final ITestClient rootMock = makeBaseMockClient();
    PoolableObjectFactory<Object> factory = mock(PoolableObjectFactory.class);
    when(factory.validateObject(any())).thenReturn(Boolean.TRUE);
    when(factory.makeObject()).thenAnswer(new Answer<ITestClient>() {
        @Override//  ww  w.  ja  v  a  2 s .c o m
        public ITestClient answer(InvocationOnMock invocation) throws Throwable {
            return new RelayClient(rootMock);
        }
    });
    ITestClient proxy = newProxy(factory);
    assertEquals(1, proxy.doSomethingUseful("foo"));
    try {
        proxy.doSomethingBad(1);
        fail();
    } catch (IllegalArgumentException e) {
        // expected
    }
    try {
        proxy.doSomethingWorse(false);
        fail();
    } catch (UnsupportedOperationException e) {
        // expected
    }
    verify(rootMock).doSomethingUseful("foo");
    verify(rootMock).doSomethingBad(1);
    verify(rootMock).doSomethingWorse(false);
    verifyNoMoreInteractions(rootMock);
}