Package org.springframework.transaction

Examples of org.springframework.transaction.MockJtaTransaction


      utControl.setVoidCallable(1);
    }

    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    TransactionManager tm = (TransactionManager) tmControl.getMock();
    MockJtaTransaction transaction = new MockJtaTransaction();
    tm.getTransaction();
    tmControl.setReturnValue(transaction, 6);

    MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
    final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
View Full Code Here


    utControl.setVoidCallable(1);

    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    TransactionManager tm = (TransactionManager) tmControl.getMock();
    if (jtaTm) {
      MockJtaTransaction transaction = new MockJtaTransaction();
      tm.getTransaction();
      tmControl.setReturnValue(transaction, 1);
    }

    MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
View Full Code Here

    MockControl session1Control = MockControl.createControl(Session.class);
    final Session session1 = (Session) session1Control.getMock();
    MockControl session2Control = MockControl.createControl(Session.class);
    final Session session2 = (Session) session2Control.getMock();

    MockJtaTransaction transaction1 = new MockJtaTransaction();
    MockJtaTransaction transaction2 = new MockJtaTransaction();
    ut.getStatus();
    utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
    ut.getStatus();
    utControl.setReturnValue(Status.STATUS_ACTIVE, 3);
    ut.begin();
View Full Code Here

  }

  public void testJtaSessionSynchronization() throws Exception {
    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    TransactionManager tm = (TransactionManager) tmControl.getMock();
    MockJtaTransaction transaction = new MockJtaTransaction();
    tm.getTransaction();
    tmControl.setReturnValue(transaction, 6);

    MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
    final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
    final MockControl sessionControl = MockControl.createControl(Session.class);
    final Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    sf.getTransactionManager();
    sfControl.setReturnValue(tm, 6);
    session.isOpen();
    sessionControl.setReturnValue(true, 4);
    session.getFlushMode();
    sessionControl.setReturnValue(FlushMode.AUTO, 1);
    session.flush();
    sessionControl.setVoidCallable(1);
    session.close();
    sessionControl.setReturnValue(null, 1);

    tmControl.replay();
    sfControl.replay();
    sessionControl.replay();

    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() {
        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

  }

  public void testJtaSessionSynchronizationWithRollback() throws Exception {
    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    TransactionManager tm = (TransactionManager) tmControl.getMock();
    MockJtaTransaction transaction = new MockJtaTransaction();
    tm.getTransaction();
    tmControl.setReturnValue(transaction, 6);

    MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
    final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
    final MockControl sessionControl = MockControl.createControl(Session.class);
    final Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    sf.getTransactionManager();
    sfControl.setReturnValue(tm, 6);
    session.isOpen();
    sessionControl.setReturnValue(true, 4);
    session.close();
    sessionControl.setReturnValue(null, 1);

    tmControl.replay();
    sfControl.replay();
    sessionControl.replay();

    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() {
        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

  }

  public void testJtaSessionSynchronizationWithRollbackByOtherThread() throws Exception {
    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    TransactionManager tm = (TransactionManager) tmControl.getMock();
    MockJtaTransaction transaction = new MockJtaTransaction();
    tm.getTransaction();
    tmControl.setReturnValue(transaction, 7);
    tm.getStatus();
    tmControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);

    MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
    final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
    final MockControl sessionControl = MockControl.createControl(Session.class);
    final Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 2);
    sf.getTransactionManager();
    sfControl.setReturnValue(tm, 7);
    session.isOpen();
    sessionControl.setReturnValue(true, 8);
    session.setFlushMode(FlushMode.NEVER);
    sessionControl.setVoidCallable(1);
    session.close();
    sessionControl.setReturnValue(null, 2);

    tmControl.replay();
    sfControl.replay();
    sessionControl.replay();

    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() {
        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() {
      public void run() {
        synchronization.afterCompletion(Status.STATUS_ROLLEDBACK);
      }
View Full Code Here

  }

  public void testJtaSessionSynchronizationWithFlushFailure() throws Exception {
    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    TransactionManager tm = (TransactionManager) tmControl.getMock();
    MockJtaTransaction transaction = new MockJtaTransaction();
    tm.getTransaction();
    tmControl.setReturnValue(transaction, 6);
    tm.setRollbackOnly();
    tmControl.setVoidCallable(1);

    final HibernateException flushEx = new HibernateException("flush failure");
    MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
    final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
    final MockControl sessionControl = MockControl.createControl(Session.class);
    final Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    sf.getTransactionManager();
    sfControl.setReturnValue(tm, 6);
    session.isOpen();
    sessionControl.setReturnValue(true, 4);
    session.getFlushMode();
    sessionControl.setReturnValue(FlushMode.AUTO, 1);
    session.flush();
    sessionControl.setThrowable(flushEx, 1);
    session.close();
    sessionControl.setReturnValue(null, 1);

    tmControl.replay();
    sfControl.replay();
    sessionControl.replay();

    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() {
        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

  }

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

  }

  public void testJtaSessionSynchronizationWithNonSessionFactoryImplementor() throws Exception {
    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    TransactionManager tm = (TransactionManager) tmControl.getMock();
    MockJtaTransaction transaction = new MockJtaTransaction();
    tm.getTransaction();
    tmControl.setReturnValue(transaction, 6);

    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    final SessionFactory sf = (SessionFactory) sfControl.getMock();
    final MockControl sessionControl = MockControl.createControl(Session.class);
    final Session session = (Session) sessionControl.getMock();
    MockControl sfiControl = MockControl.createControl(SessionFactoryImplementor.class);
    final SessionFactoryImplementor sfi = (SessionFactoryImplementor) sfiControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sfi, 6);
    sfi.getTransactionManager();
    sfiControl.setReturnValue(tm, 6);
    session.isOpen();
    sessionControl.setReturnValue(true, 4);
    session.getFlushMode();
    sessionControl.setReturnValue(FlushMode.AUTO, 1);
    session.flush();
    sessionControl.setVoidCallable(1);
    session.close();
    sessionControl.setReturnValue(null, 1);

    tmControl.replay();
    sfControl.replay();
    sessionControl.replay();
    sfiControl.replay();

    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() {
        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

  public void testJtaSessionSynchronizationWithSpringTransactionLaterOn() throws Exception {
    MockControl utControl = MockControl.createControl(UserTransaction.class);
    UserTransaction ut = (UserTransaction) utControl.getMock();
    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    TransactionManager tm = (TransactionManager) tmControl.getMock();
    MockJtaTransaction transaction = new MockJtaTransaction();
    ut.getStatus();
    utControl.setReturnValue(Status.STATUS_ACTIVE, 2);
    tm.getTransaction();
    tmControl.setReturnValue(transaction, 6);

    MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
    final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
    final MockControl sessionControl = MockControl.createControl(Session.class);
    final Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    sf.getTransactionManager();
    sfControl.setReturnValue(tm, 6);
    session.isOpen();
    sessionControl.setReturnValue(true, 4);
    session.getFlushMode();
    sessionControl.setReturnValue(FlushMode.AUTO, 1);
    session.flush();
    sessionControl.setVoidCallable(1);
    session.close();
    sessionControl.setReturnValue(null, 1);

    utControl.replay();
    tmControl.replay();
    sfControl.replay();
    sessionControl.replay();

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    final HibernateTemplate ht = new HibernateTemplate(sf);
    ht.setExposeNativeSession(true);
    for (int i = 0; i < 2; i++) {
      ht.executeFind(new HibernateCallback() {
        public Object doInHibernate(org.hibernate.Session sess) {
          assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
          assertEquals(session, sess);
          return null;
        }
      });
    }

    TransactionTemplate tt = new TransactionTemplate(new JtaTransactionManager(ut));
    tt.execute(new TransactionCallbackWithoutResult() {
      protected void doInTransactionWithoutResult(TransactionStatus status) {
        for (int i = 2; i < 5; i++) {
          ht.executeFind(new HibernateCallback() {
            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

TOP

Related Classes of org.springframework.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.