Search in sources :

Example 1 with JdbcResourceTransactionMock

use of org.infinispan.test.hibernate.cache.commons.util.JdbcResourceTransactionMock in project infinispan by infinispan.

the class TestSessionAccessImpl method mockSession.

@Override
public Object mockSession(Class<? extends JtaPlatform> jtaPlatform, ControlledTimeService timeService, RegionFactory regionFactory) {
    SessionMock session = mock(SessionMock.class);
    when(session.isClosed()).thenReturn(false);
    when(session.isOpen()).thenReturn(true);
    when(session.getTransactionStartTimestamp()).thenReturn(timeService.wallClockTime());
    TransactionCoordinator txCoord;
    if (jtaPlatform == BatchModeJtaPlatform.class) {
        BatchModeTransactionCoordinator batchModeTxCoord = new BatchModeTransactionCoordinator();
        txCoord = batchModeTxCoord;
        when(session.getTransactionCoordinator()).thenReturn(txCoord);
        when(session.beginTransaction()).then(invocation -> {
            Transaction tx = batchModeTxCoord.newTransaction();
            tx.begin();
            return tx;
        });
    } else if (jtaPlatform == null || jtaPlatform == NoJtaPlatform.class) {
        Connection connection = mock(Connection.class);
        JdbcConnectionAccess jdbcConnectionAccess = mock(JdbcConnectionAccess.class);
        try {
            when(jdbcConnectionAccess.obtainConnection()).thenReturn(connection);
        } catch (SQLException e) {
        // never thrown from mock
        }
        JdbcSessionOwner jdbcSessionOwner = mock(JdbcSessionOwner.class);
        when(jdbcSessionOwner.getJdbcConnectionAccess()).thenReturn(jdbcConnectionAccess);
        SqlExceptionHelper sqlExceptionHelper = mock(SqlExceptionHelper.class);
        JdbcServices jdbcServices = mock(JdbcServices.class);
        when(jdbcServices.getSqlExceptionHelper()).thenReturn(sqlExceptionHelper);
        ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
        when(serviceRegistry.getService(JdbcServices.class)).thenReturn(jdbcServices);
        JdbcSessionContext jdbcSessionContext = mock(JdbcSessionContext.class);
        when(jdbcSessionContext.getServiceRegistry()).thenReturn(serviceRegistry);
        JpaCompliance jpaCompliance = mock(JpaCompliance.class);
        when(jpaCompliance.isJpaTransactionComplianceEnabled()).thenReturn(true);
        SessionFactoryImplementor sessionFactory = mock(SessionFactoryImplementor.class);
        SessionFactoryOptions sessionFactoryOptions = mock(SessionFactoryOptions.class);
        when(sessionFactoryOptions.getJpaCompliance()).thenReturn(jpaCompliance);
        when(sessionFactory.getSessionFactoryOptions()).thenReturn(sessionFactoryOptions);
        when(jdbcSessionContext.getSessionFactory()).thenReturn(sessionFactory);
        when(jdbcSessionOwner.getJdbcSessionContext()).thenReturn(jdbcSessionContext);
        when(session.getSessionFactory()).thenReturn(sessionFactory);
        when(session.getFactory()).thenReturn(sessionFactory);
        NonJtaTransactionCoordinator txOwner = mock(NonJtaTransactionCoordinator.class);
        when(txOwner.getResourceLocalTransaction()).thenReturn(new JdbcResourceTransactionMock());
        when(txOwner.getJdbcSessionOwner()).thenReturn(jdbcSessionOwner);
        when(txOwner.isActive()).thenReturn(true);
        txCoord = JdbcResourceLocalTransactionCoordinatorBuilderImpl.INSTANCE.buildTransactionCoordinator(txOwner, null);
        when(session.getTransactionCoordinator()).thenReturn(txCoord);
        when(session.beginTransaction()).then(invocation -> {
            Transaction tx = new TransactionImpl(txCoord, session.getExceptionConverter(), session);
            tx.begin();
            return tx;
        });
    } else {
        throw new IllegalStateException("Unknown JtaPlatform: " + jtaPlatform);
    }
    Sync sync = new Sync(regionFactory);
    TestSynchronization synchronization = new TestSynchronization(sync);
    when(session.getCacheTransactionSynchronization()).thenAnswer(invocation -> {
        if (!synchronization.registered) {
            txCoord.getLocalSynchronizations().registerSynchronization(synchronization);
            synchronization.registered = true;
        }
        return sync;
    });
    return session;
}
Also used : Connection(java.sql.Connection) TestSessionAccess(org.infinispan.test.hibernate.cache.commons.util.TestSessionAccess) Sync(org.infinispan.hibernate.cache.v53.impl.Sync) JdbcResourceTransactionMock(org.infinispan.test.hibernate.cache.commons.util.JdbcResourceTransactionMock) BatchModeJtaPlatform(org.infinispan.test.hibernate.cache.commons.util.BatchModeJtaPlatform) Transaction(org.hibernate.Transaction) Query(org.hibernate.query.Query) AccessType(org.hibernate.cache.spi.access.AccessType) SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Synchronization(javax.transaction.Synchronization) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) JdbcResourceTransactionAccess(org.hibernate.resource.transaction.backend.jdbc.spi.JdbcResourceTransactionAccess) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) SoftLock(org.hibernate.cache.spi.access.SoftLock) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) ControlledTimeService(org.infinispan.util.ControlledTimeService) List(java.util.List) CacheException(org.hibernate.cache.CacheException) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Mockito.mock(org.mockito.Mockito.mock) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) DomainDataRegionImpl(org.infinispan.hibernate.cache.v53.impl.DomainDataRegionImpl) DomainDataCachingConfig(org.hibernate.cache.cfg.spi.DomainDataCachingConfig) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) Session(org.hibernate.Session) MetaInfServices(org.kohsuke.MetaInfServices) SessionFactoryOptions(org.hibernate.boot.spi.SessionFactoryOptions) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess) DirectAccessRegion(org.hibernate.cache.spi.DirectAccessRegion) CachedDomainDataAccess(org.hibernate.cache.spi.access.CachedDomainDataAccess) SQLException(java.sql.SQLException) RegionFactory(org.hibernate.cache.spi.RegionFactory) TransactionCoordinatorOwner(org.hibernate.resource.transaction.spi.TransactionCoordinatorOwner) NoJtaPlatform(org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform) AbstractSharedSessionContract(org.hibernate.internal.AbstractSharedSessionContract) FlushMode(org.hibernate.FlushMode) CacheImplementor(org.hibernate.cache.spi.CacheImplementor) JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) Mockito.when(org.mockito.Mockito.when) ServiceRegistry(org.hibernate.service.ServiceRegistry) InfinispanBaseRegion(org.infinispan.hibernate.cache.commons.InfinispanBaseRegion) Status(javax.transaction.Status) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) SessionCreationOptions(org.hibernate.internal.SessionCreationOptions) JpaCompliance(org.hibernate.jpa.spi.JpaCompliance) JdbcResourceTransactionMock(org.infinispan.test.hibernate.cache.commons.util.JdbcResourceTransactionMock) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) Connection(java.sql.Connection) JpaCompliance(org.hibernate.jpa.spi.JpaCompliance) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) Transaction(org.hibernate.Transaction) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) SessionFactoryOptions(org.hibernate.boot.spi.SessionFactoryOptions) ServiceRegistry(org.hibernate.service.ServiceRegistry) Sync(org.infinispan.hibernate.cache.v53.impl.Sync)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Status (javax.transaction.Status)1 Synchronization (javax.transaction.Synchronization)1 FlushMode (org.hibernate.FlushMode)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 SessionFactoryOptions (org.hibernate.boot.spi.SessionFactoryOptions)1 CacheException (org.hibernate.cache.CacheException)1 DomainDataCachingConfig (org.hibernate.cache.cfg.spi.DomainDataCachingConfig)1 CacheImplementor (org.hibernate.cache.spi.CacheImplementor)1 DirectAccessRegion (org.hibernate.cache.spi.DirectAccessRegion)1 RegionFactory (org.hibernate.cache.spi.RegionFactory)1 AccessType (org.hibernate.cache.spi.access.AccessType)1 CachedDomainDataAccess (org.hibernate.cache.spi.access.CachedDomainDataAccess)1 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)1 SoftLock (org.hibernate.cache.spi.access.SoftLock)1