}
public void testJtaSessionSynchronizationWithSuspendedTransaction() throws Exception {
MockControl tmControl = MockControl.createControl(TransactionManager.class);
TransactionManager tm = (TransactionManager) tmControl.getMock();
MockJtaTransaction transaction1 = new MockJtaTransaction();
MockJtaTransaction transaction2 = new MockJtaTransaction();
tm.getTransaction();
tmControl.setReturnValue(transaction1, 2);
tm.getTransaction();
tmControl.setReturnValue(transaction2, 3);
MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
final MockControl session1Control = MockControl.createControl(Session.class);
final Session session1 = (Session) session1Control.getMock();
final MockControl session2Control = MockControl.createControl(Session.class);
final Session session2 = (Session) session2Control.getMock();
sf.openSession();
sfControl.setReturnValue(session1, 1);
sf.openSession();
sfControl.setReturnValue(session2, 1);
sf.getTransactionManager();
sfControl.setReturnValue(tm, 5);
session1.getFlushMode();
session1Control.setReturnValue(FlushMode.AUTO, 1);
session2.getFlushMode();
session2Control.setReturnValue(FlushMode.AUTO, 1);
session1.flush();
session1Control.setVoidCallable(1);
session2.flush();
session2Control.setVoidCallable(1);
session1.close();
session1Control.setReturnValue(null, 1);
session2.close();
session2Control.setReturnValue(null, 1);
tmControl.replay();
sfControl.replay();
session1Control.replay();
session2Control.replay();
assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setExposeNativeSession(true);
ht.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session sess) {
assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
assertEquals(session1, sess);
return null;
}
});
ht.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session sess) {
assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
assertEquals(session2, sess);
return null;
}
});
Synchronization synchronization2 = transaction2.getSynchronization();
assertTrue("JTA synchronization registered", synchronization2 != null);
synchronization2.beforeCompletion();
synchronization2.afterCompletion(Status.STATUS_COMMITTED);
Synchronization synchronization1 = transaction1.getSynchronization();