Search in sources :

Example 1 with JdbcObserver

use of org.hibernate.resource.jdbc.spi.JdbcObserver in project hibernate-orm by hibernate.

the class JdbcCoordinatorTest method testConnectionClose.

@Test
public void testConnectionClose() throws NoSuchFieldException, IllegalAccessException, SQLException {
    Connection connection = Mockito.mock(Connection.class);
    JdbcSessionOwner sessionOwner = Mockito.mock(JdbcSessionOwner.class);
    JdbcConnectionAccess jdbcConnectionAccess = Mockito.mock(JdbcConnectionAccess.class);
    when(jdbcConnectionAccess.obtainConnection()).thenReturn(connection);
    when(jdbcConnectionAccess.supportsAggressiveRelease()).thenReturn(false);
    JdbcSessionContext sessionContext = Mockito.mock(JdbcSessionContext.class);
    when(sessionOwner.getJdbcSessionContext()).thenReturn(sessionContext);
    when(sessionOwner.getJdbcConnectionAccess()).thenReturn(jdbcConnectionAccess);
    ServiceRegistry serviceRegistry = Mockito.mock(ServiceRegistry.class);
    when(sessionContext.getServiceRegistry()).thenReturn(serviceRegistry);
    when(sessionContext.getPhysicalConnectionHandlingMode()).thenReturn(PhysicalConnectionHandlingMode.IMMEDIATE_ACQUISITION_AND_HOLD);
    JdbcObserver jdbcObserver = Mockito.mock(JdbcObserver.class);
    when(sessionContext.getObserver()).thenReturn(jdbcObserver);
    JdbcServices jdbcServices = Mockito.mock(JdbcServices.class);
    when(serviceRegistry.getService(eq(JdbcServices.class))).thenReturn(jdbcServices);
    ConfigurationService configurationService = Mockito.mock(ConfigurationService.class);
    when(serviceRegistry.getService(eq(ConfigurationService.class))).thenReturn(configurationService);
    when(configurationService.getSetting(eq(AvailableSettings.CONNECTION_PROVIDER_DISABLES_AUTOCOMMIT), same(StandardConverters.BOOLEAN), eq(false))).thenReturn(false);
    SqlExceptionHelper sqlExceptionHelper = Mockito.mock(SqlExceptionHelper.class);
    when(jdbcServices.getSqlExceptionHelper()).thenReturn(sqlExceptionHelper);
    JdbcCoordinatorImpl jdbcCoordinator = new JdbcCoordinatorImpl(null, sessionOwner);
    Batch currentBatch = Mockito.mock(Batch.class);
    Field currentBatchField = JdbcCoordinatorImpl.class.getDeclaredField("currentBatch");
    currentBatchField.setAccessible(true);
    currentBatchField.set(jdbcCoordinator, currentBatch);
    doThrow(IllegalStateException.class).when(currentBatch).release();
    try {
        jdbcCoordinator.close();
        fail("Should throw IllegalStateException");
    } catch (Exception expected) {
        assertEquals(IllegalStateException.class, expected.getClass());
    }
    verify(jdbcConnectionAccess, times(1)).releaseConnection(same(connection));
}
Also used : JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) Connection(java.sql.Connection) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) SQLException(java.sql.SQLException) Field(java.lang.reflect.Field) JdbcObserver(org.hibernate.resource.jdbc.spi.JdbcObserver) Batch(org.hibernate.engine.jdbc.batch.spi.Batch) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) ServiceRegistry(org.hibernate.service.ServiceRegistry) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) Test(org.junit.Test)

Aggregations

Field (java.lang.reflect.Field)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)1 Batch (org.hibernate.engine.jdbc.batch.spi.Batch)1 JdbcConnectionAccess (org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 SqlExceptionHelper (org.hibernate.engine.jdbc.spi.SqlExceptionHelper)1 JdbcObserver (org.hibernate.resource.jdbc.spi.JdbcObserver)1 JdbcSessionContext (org.hibernate.resource.jdbc.spi.JdbcSessionContext)1 JdbcSessionOwner (org.hibernate.resource.jdbc.spi.JdbcSessionOwner)1 ServiceRegistry (org.hibernate.service.ServiceRegistry)1 Test (org.junit.Test)1