Search in sources :

Example 1 with FirebirdConnection

use of org.firebirdsql.jdbc.FirebirdConnection in project jaybird by FirebirdSQL.

the class TestPooledConnectionHandlerMock method testClosedHandler_throwsException.

/**
 * Calling any Connection method (except isClosed() and close()) on a proxy
 * that was closed by closing the handler should throw an SQLException
 * mentioning the connection was forcibly closed; the owner should not be
 * notified of the exception.
 *
 * TODO: Consider testing for all Connection methods
 *
 * @throws SQLException
 */
@Test
public void testClosedHandler_throwsException() throws SQLException {
    final FirebirdConnection physicalConnection = context.mock(FirebirdConnection.class);
    final FBPooledConnection pooled = context.mock(FBPooledConnection.class);
    final PooledConnectionHandler handler = new PooledConnectionHandler(physicalConnection, pooled);
    context.checking(new Expectations() {

        {
            connectionHandleReleaseExpectations(this, physicalConnection);
            allowing(pooled).releaseConnectionHandler(handler);
            allowing(pooled).fireConnectionClosed();
            never(pooled).fireConnectionError(with(any(SQLException.class)));
        }
    });
    Connection proxy = handler.getProxy();
    handler.close();
    expectedException.expect(SQLException.class);
    expectedException.expectMessage(PooledConnectionHandler.FORCIBLY_CLOSED_MESSAGE);
    proxy.clearWarnings();
}
Also used : Expectations(org.jmock.Expectations) SQLException(java.sql.SQLException) FBSQLException(org.firebirdsql.jdbc.FBSQLException) Connection(java.sql.Connection) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) Test(org.junit.Test)

Example 2 with FirebirdConnection

use of org.firebirdsql.jdbc.FirebirdConnection in project jaybird by FirebirdSQL.

the class TestPooledConnectionHandlerMock method testDoubleClose_allowed.

/**
 * Calling close() on a closed proxy should not throw an exception.
 *
 * @throws SQLException
 */
@Test
public void testDoubleClose_allowed() throws SQLException {
    final FirebirdConnection physicalConnection = context.mock(FirebirdConnection.class);
    final FBPooledConnection pooled = context.mock(FBPooledConnection.class);
    final PooledConnectionHandler handler = new PooledConnectionHandler(physicalConnection, pooled);
    context.checking(new Expectations() {

        {
            ignoring(physicalConnection);
            ignoring(pooled);
        }
    });
    Connection proxy = handler.getProxy();
    proxy.close();
    // Expectation: no exception for double close
    proxy.close();
}
Also used : Expectations(org.jmock.Expectations) Connection(java.sql.Connection) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) Test(org.junit.Test)

Example 3 with FirebirdConnection

use of org.firebirdsql.jdbc.FirebirdConnection in project jaybird by FirebirdSQL.

the class TestPooledConnectionHandlerMock method testProxyClose_IsClosed.

/**
 * The isClosed() method of PooledConnectionHandler and its proxy should
 * report <code>true</code> after proxy close.
 *
 * @throws SQLException
 */
@Test
public void testProxyClose_IsClosed() throws SQLException {
    final FirebirdConnection physicalConnection = context.mock(FirebirdConnection.class);
    final FBPooledConnection pooled = context.mock(FBPooledConnection.class);
    final PooledConnectionHandler handler = new PooledConnectionHandler(physicalConnection, pooled);
    context.checking(new Expectations() {

        {
            connectionHandleReleaseExpectations(this, physicalConnection);
            allowing(pooled).releaseConnectionHandler(handler);
            allowing(pooled).fireConnectionClosed();
        }
    });
    Connection proxy = handler.getProxy();
    proxy.close();
    assertTrue("Handler of closed proxy should report isClosed() true", handler.isClosed());
    assertTrue("Closed proxy should report isClosed() true", proxy.isClosed());
}
Also used : Expectations(org.jmock.Expectations) Connection(java.sql.Connection) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) Test(org.junit.Test)

Example 4 with FirebirdConnection

use of org.firebirdsql.jdbc.FirebirdConnection in project jaybird by FirebirdSQL.

the class TestPooledConnectionHandlerMock method testHandlerClose_NoNotify.

/**
 * Closing the PooledConnectionHandler should not notify the owner and not
 * close the physical connection.
 *
 * @throws SQLException
 */
@Test
public void testHandlerClose_NoNotify() throws SQLException {
    final FirebirdConnection physicalConnection = context.mock(FirebirdConnection.class);
    final FBPooledConnection pooled = context.mock(FBPooledConnection.class);
    final PooledConnectionHandler handler = new PooledConnectionHandler(physicalConnection, pooled);
    context.checking(new Expectations() {

        {
            connectionHandleReleaseExpectations(this, physicalConnection);
            allowing(pooled).releaseConnectionHandler(handler);
            never(physicalConnection).close();
            never(pooled).fireConnectionClosed();
        }
    });
    handler.close();
}
Also used : Expectations(org.jmock.Expectations) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) Test(org.junit.Test)

Example 5 with FirebirdConnection

use of org.firebirdsql.jdbc.FirebirdConnection in project jaybird by FirebirdSQL.

the class TestPooledConnectionHandlerMock method testProxyClose_Notify.

/**
 * Closing the Proxy of the PooledConnectionHandler should notify the owner
 * but not close the physical connection.
 *
 * @throws SQLException
 */
@Test
public void testProxyClose_Notify() throws SQLException {
    final FirebirdConnection physicalConnection = context.mock(FirebirdConnection.class);
    final FBPooledConnection pooled = context.mock(FBPooledConnection.class);
    final PooledConnectionHandler handler = new PooledConnectionHandler(physicalConnection, pooled);
    context.checking(new Expectations() {

        {
            connectionHandleReleaseExpectations(this, physicalConnection);
            allowing(pooled).releaseConnectionHandler(handler);
            never(physicalConnection).close();
            oneOf(pooled).fireConnectionClosed();
        }
    });
    handler.getProxy().close();
}
Also used : Expectations(org.jmock.Expectations) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) Test(org.junit.Test)

Aggregations

FirebirdConnection (org.firebirdsql.jdbc.FirebirdConnection)9 Expectations (org.jmock.Expectations)9 Test (org.junit.Test)9 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)3 FBSQLException (org.firebirdsql.jdbc.FBSQLException)3 Sequence (org.jmock.Sequence)2