Package org.springframework.tests.transaction

Examples of org.springframework.tests.transaction.MockJtaTransaction


  }

  public void testJtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction tx = new MockJtaTransaction();

    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.getTransaction()).willReturn(tx);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
      @Override
      protected void doInTransactionWithoutResult(TransactionStatus status) {
        assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
        TransactionSynchronizationManager.registerSynchronization(synch);
        status.setRollbackOnly();
      }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertNotNull(tx.getSynchronization());
    tx.getSynchronization().beforeCompletion();
    tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK);

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
  }
View Full Code Here


    Object result = tt.execute(new TransactionCallback() {
      @Override
      public Object doInTransaction(TransactionStatus status) {
        try {
          MockJtaTransaction transaction = new MockJtaTransaction();
          given(tm.suspend()).willReturn(transaction);
        }
        catch (Exception ex) {
        }
View Full Code Here

    } else {
      given(ut.getStatus()).willReturn(status);
    }

    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction transaction = new MockJtaTransaction();
    given(tm.getTransaction()).willReturn(transaction);

    final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
    final Session session = mock(Session.class);
    given(sf.getTransactionManager()).willReturn(tm);
View Full Code Here

    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION,
        Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

    TransactionManager tm = mock(TransactionManager.class);
    if (jtaTm) {
      MockJtaTransaction transaction = new MockJtaTransaction();
      given(tm.getTransaction()).willReturn(transaction);
    }

    final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
    final ExtendedSession session = mock(ExtendedSession.class);
View Full Code Here

    final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
    final Session session1 = mock(Session.class);
    final Session session2 = mock(Session.class);

    MockJtaTransaction transaction1 = new MockJtaTransaction();
    MockJtaTransaction transaction2 = new MockJtaTransaction();
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION,
        Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE,
        Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    given(tm.getTransaction()).willReturn(transaction1);
    given(tm.suspend()).willReturn(tx1);
View Full Code Here

  @Test
  @SuppressWarnings("rawtypes")
  public void testJtaSessionSynchronization() throws Exception {

    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction transaction = new MockJtaTransaction();
    given(tm.getTransaction()).willReturn(transaction);
    final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
    final Session session = mock(Session.class);
    given(sf.openSession()).willReturn(session);
    given(sf.getTransactionManager()).willReturn(tm);
    given(session.isOpen()).willReturn(true);
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    HibernateTemplate ht = new HibernateTemplate(sf);
    ht.setExposeNativeSession(true);
    for (int i = 0; i < 5; i++) {
      ht.executeFind(new HibernateCallback() {

        @Override
        public Object doInHibernate(org.hibernate.Session sess) {
          assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
          assertEquals(session, sess);
          return null;
        }
      });
    }

    Synchronization synchronization = transaction.getSynchronization();
    assertTrue("JTA synchronization registered", synchronization != null);
    synchronization.beforeCompletion();
    synchronization.afterCompletion(Status.STATUS_COMMITTED);

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
View Full Code Here

  @Test
  @SuppressWarnings("rawtypes")
  public void testJtaSessionSynchronizationWithRollback() throws Exception {

    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction transaction = new MockJtaTransaction();
    given(tm.getTransaction()).willReturn(transaction);
    final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
    final Session session = mock(Session.class);
    given(sf.openSession()).willReturn(session);
    given(sf.getTransactionManager()).willReturn(tm);
    given(session.isOpen()).willReturn(true);

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    HibernateTemplate ht = new HibernateTemplate(sf);
    ht.setExposeNativeSession(true);
    for (int i = 0; i < 5; i++) {
      ht.executeFind(new HibernateCallback() {

        @Override
        public Object doInHibernate(org.hibernate.Session sess) {
          assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
          assertEquals(session, sess);
          return null;
        }
      });
    }

    Synchronization synchronization = transaction.getSynchronization();
    assertTrue("JTA synchronization registered", synchronization != null);
    synchronization.afterCompletion(Status.STATUS_ROLLEDBACK);

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
View Full Code Here

  @Test
  @SuppressWarnings("rawtypes")
  public void testJtaSessionSynchronizationWithRollbackByOtherThread() throws Exception {

    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction transaction = new MockJtaTransaction();
    given(tm.getTransaction()).willReturn(transaction);
    given(tm.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
    final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
    final Session session = mock(Session.class);
    given(sf.openSession()).willReturn(session);
    given(sf.getTransactionManager()).willReturn(tm);
    given(session.isOpen()).willReturn(true);

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    final HibernateTemplate ht = new HibernateTemplate(sf);
    ht.setExposeNativeSession(true);
    for (int i = 0; i < 5; i++) {
      ht.executeFind(new HibernateCallback() {

        @Override
        public Object doInHibernate(org.hibernate.Session sess) {
          assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
          assertEquals(session, sess);
          return null;
        }
      });
    }

    final Synchronization synchronization = transaction.getSynchronization();
    assertTrue("JTA synchronization registered", synchronization != null);
    Thread thread = new Thread() {

      @Override
      public void run() {
View Full Code Here

  @Test
  @SuppressWarnings("rawtypes")
  public void testJtaSessionSynchronizationWithFlushFailure() throws Exception {

    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction transaction = new MockJtaTransaction();
    given(tm.getTransaction()).willReturn(transaction);
    final HibernateException flushEx = new HibernateException("flush failure");
    final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
    final Session session = mock(Session.class);
    given(sf.openSession()).willReturn(session);
    given(sf.getTransactionManager()).willReturn(tm);
    given(session.isOpen()).willReturn(true);
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);
    willThrow(flushEx).given(session).flush();

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    HibernateTemplate ht = new HibernateTemplate(sf);
    ht.setExposeNativeSession(true);
    for (int i = 0; i < 5; i++) {
      ht.executeFind(new HibernateCallback() {

        @Override
        public Object doInHibernate(org.hibernate.Session sess) {
          assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
          assertEquals(session, sess);
          return null;
        }
      });
    }

    Synchronization synchronization = transaction.getSynchronization();
    assertTrue("JTA synchronization registered", synchronization != null);
    try {
      synchronization.beforeCompletion();
      fail("Should have thrown HibernateSystemException");
    }
View Full Code Here

  @Test
  @SuppressWarnings("rawtypes")
  public void testJtaSessionSynchronizationWithSuspendedTransaction() throws Exception {

    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction transaction1 = new MockJtaTransaction();
    MockJtaTransaction transaction2 = new MockJtaTransaction();
    given(tm.getTransaction()).willReturn(transaction1, transaction1, transaction2, transaction2,
        transaction2);

    final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
    final Session session1 = mock(Session.class);
    final Session session2 = mock(Session.class);
    given(sf.openSession()).willReturn(session1, session2);
    given(sf.getTransactionManager()).willReturn(tm);
    given(session1.getFlushMode()).willReturn(FlushMode.AUTO);
    given(session2.getFlushMode()).willReturn(FlushMode.AUTO);

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    HibernateTemplate ht = new HibernateTemplate(sf);
    ht.setExposeNativeSession(true);
    ht.executeFind(new HibernateCallback() {

      @Override
      public Object doInHibernate(org.hibernate.Session sess) {
        assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
        assertEquals(session1, sess);
        return null;
      }
    });
    ht.executeFind(new HibernateCallback() {

      @Override
      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();
View Full Code Here

TOP

Related Classes of org.springframework.tests.transaction.MockJtaTransaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.