Examples of MockJtaTransaction


Examples of org.springframework.transaction.MockJtaTransaction

  }

  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

Examples of org.springframework.transaction.MockJtaTransaction

  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

Examples of org.springframework.transaction.MockJtaTransaction

  }

  private void doTestJtaSessionSynchronizationWithPreBound(boolean flushNever) 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.getTransactionManager();
    sfControl.setReturnValue(tm, 6);
    session.isOpen();
    sessionControl.setReturnValue(true, 5);
    session.getFlushMode();
    if (flushNever) {
      sessionControl.setReturnValue(FlushMode.NEVER, 1);
      session.setFlushMode(FlushMode.AUTO);
      sessionControl.setVoidCallable(1);
    }
    else {
      sessionControl.setReturnValue(FlushMode.AUTO, 1);
    }

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

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    try {
      assertTrue("Has 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;
          }
        });
      }

      sessionControl.verify();
      sessionControl.reset();
      session.getFlushMode();
      sessionControl.setReturnValue(FlushMode.AUTO, 1);
      session.flush();
      sessionControl.setVoidCallable(1);
      if (flushNever) {
        session.setFlushMode(FlushMode.NEVER);
        sessionControl.setVoidCallable(1);
      }
      session.disconnect();
      sessionControl.setReturnValue(null, 1);
      sessionControl.replay();

      Synchronization synchronization = transaction.getSynchronization();
      assertTrue("JTA synchronization registered", synchronization != null);
      synchronization.beforeCompletion();
      synchronization.afterCompletion(Status.STATUS_COMMITTED);
      assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
    }
View Full Code Here

Examples of org.springframework.transaction.MockJtaTransaction

  }

  public void testJtaSessionSynchronizationWithRemoteTransaction() throws Exception {
    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    TransactionManager tm = (TransactionManager) tmControl.getMock();
    MockJtaTransaction transaction = new MockJtaTransaction();

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

    for (int j = 0; j < 2; j++) {
      tmControl.reset();
      sfControl.reset();
      sessionControl.reset();

      tm.getTransaction();
      tmControl.setReturnValue(transaction, 6);

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

      if (j == 0) {
        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
      }
      else {
        assertTrue("Has 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;
          }
        });
      }

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

      // Call synchronization in a new thread, to simulate a synchronization
      // triggered by a new remote call from a remote transaction coordinator.
      Thread synch = new Thread() {
View Full Code Here

Examples of org.springframework.transaction.MockJtaTransaction

        try {
          utControl.verify();
          utControl.reset();
          ut.getStatus();
          utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
          MockJtaTransaction transaction = new MockJtaTransaction();
          tm.suspend();
          tmControl.setReturnValue(transaction, 1);
          ut.begin();
          utControl.setVoidCallable(1);
          ut.getStatus();
View Full Code Here

Examples of org.springframework.transaction.MockJtaTransaction

      utControl.setReturnValue(status, 1);
    }

    MockControl tmControl = MockControl.createControl(TransactionManager.class);
    TransactionManager tm = (TransactionManager) tmControl.getMock();
    MockJtaTransaction transaction = new MockJtaTransaction();
    tm.getStatus();
    tmControl.setReturnValue(Status.STATUS_ACTIVE, 6);
    tm.getTransaction();
    tmControl.setReturnValue(transaction, 6);
View Full Code Here

Examples of org.springframework.transaction.MockJtaTransaction

  }

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

    lobHandler.getClobAsString(rs, "column");
    lobHandlerControl.setReturnValue("content");
    lobCreator.setClobAsString(ps, 1, "content");
    lobCreatorControl.setVoidCallable(1);

    lobHandlerControl.replay();
    lobCreatorControl.replay();

    ClobStringType type = new ClobStringType(lobHandler, tm);
    assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
    tmControl.replay();
    type.nullSafeSet(ps, "content", 1);
    Synchronization synch = transaction.getSynchronization();
    assertNotNull(synch);
    synch.beforeCompletion();
    synch.afterCompletion(Status.STATUS_COMMITTED);
    tmControl.verify();
  }
View Full Code Here

Examples of org.springframework.transaction.MockJtaTransaction

  }

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

    lobHandler.getClobAsString(rs, "column");
    lobHandlerControl.setReturnValue("content");
    lobCreator.setClobAsString(ps, 1, "content");
    lobCreatorControl.setVoidCallable(1);

    lobHandlerControl.replay();
    lobCreatorControl.replay();

    ClobStringType type = new ClobStringType(lobHandler, tm);
    assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
    tmControl.replay();
    type.nullSafeSet(ps, "content", 1);
    Synchronization synch = transaction.getSynchronization();
    assertNotNull(synch);
    synch.afterCompletion(Status.STATUS_ROLLEDBACK);
    tmControl.verify();
  }
View Full Code Here

Examples of org.springframework.transaction.MockJtaTransaction

  }

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

    String content = "content";
    byte[] contentBytes = content.getBytes();
    lobHandler.getBlobAsBytes(rs, "column");
    lobHandlerControl.setReturnValue(contentBytes);
    lobCreator.setBlobAsBytes(ps, 1, contentBytes);
    lobCreatorControl.setMatcher(new ArrayMatcher());

    lobHandlerControl.replay();
    lobCreatorControl.replay();

    BlobStringType type = new BlobStringType(lobHandler, tm);
    assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
    tmControl.replay();
    type.nullSafeSet(ps, content, 1);
    Synchronization synch = transaction.getSynchronization();
    assertNotNull(synch);
    synch.beforeCompletion();
    synch.afterCompletion(Status.STATUS_COMMITTED);
    tmControl.verify();
  }
View Full Code Here

Examples of org.springframework.transaction.MockJtaTransaction

  }

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

    String content = "content";
    byte[] contentBytes = content.getBytes();
    lobHandler.getBlobAsBytes(rs, "column");
    lobHandlerControl.setReturnValue(contentBytes);
    lobCreator.setBlobAsBytes(ps, 1, contentBytes);
    lobCreatorControl.setMatcher(new ArrayMatcher());

    lobHandlerControl.replay();
    lobCreatorControl.replay();

    BlobStringType type = new BlobStringType(lobHandler, tm);
    assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
    tmControl.replay();
    type.nullSafeSet(ps, content, 1);
    Synchronization synch = transaction.getSynchronization();
    assertNotNull(synch);
    synch.afterCompletion(Status.STATUS_ROLLEDBACK);
    tmControl.verify();
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.