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;
}
Aggregations