public void ParticipatingTransactionWithCommit()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = (IConnection)mocks.CreateMock(typeof(IConnection));
            ISession           session           = (ISession)mocks.CreateMock(typeof(ISession));

            using (mocks.Ordered())
            {
                SetupCommitExpectations(connection, connectionFactory, session);
            }

            mocks.ReplayAll();


            EmsTransactionManager tm = new EmsTransactionManager(connectionFactory);
            ITransactionStatus    ts = tm.GetTransaction(new DefaultTransactionDefinition());
            EmsTemplate           nt = new EmsTemplate(connectionFactory);

            nt.Execute(new AssertSessionCallback(session));

            TransactionTemplate tt = new TransactionTemplate(tm);

            tt.Execute(delegate(ITransactionStatus status)
            {
                nt.Execute(new AssertSessionCallback(session));
                return(null);
            });

            tm.Commit(ts);

            mocks.VerifyAll();
        }
        public void TransactionSuspension()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = (IConnection)mocks.CreateMock(typeof(IConnection));
            ISession           session           = (ISession)mocks.CreateMock(typeof(ISession));
            ISession           session2          = (ISession)mocks.CreateMock(typeof(ISession));


            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Twice();
            Expect.Call(connection.CreateSession(true, Session.SESSION_TRANSACTED)).Return(session).Repeat.Once();
            Expect.Call(connection.CreateSession(true, Session.SESSION_TRANSACTED)).Return(session2).Repeat.Once();

            session.Commit();
            LastCall.On(session).Repeat.Once();
            session2.Commit();
            LastCall.On(session2).Repeat.Once();

            session.Close();
            LastCall.On(session).Repeat.Once();
            session2.Close();
            LastCall.On(session2).Repeat.Once();

            connection.Close();
            LastCall.On(connection).Repeat.Twice();

            mocks.ReplayAll();

            EmsTransactionManager tm = new EmsTransactionManager(connectionFactory);
            ITransactionStatus    ts = tm.GetTransaction(new DefaultTransactionDefinition());
            EmsTemplate           nt = new EmsTemplate(connectionFactory);


            TransactionTemplate tt = new TransactionTemplate(tm);

            tt.PropagationBehavior = TransactionPropagation.RequiresNew;
            tt.Execute(delegate(ITransactionStatus status)
            {
                nt.Execute(new AssertNotSameSessionCallback(session));
                return(null);
            });

            nt.Execute(new AssertSessionCallback(session));

            tm.Commit(ts);

            mocks.VerifyAll();
        }
        public void TransactionRollback()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory) mocks.CreateMock(typeof (IConnectionFactory));
            IConnection connection = (IConnection) mocks.CreateMock(typeof (IConnection));
            ISession session = (ISession) mocks.CreateMock(typeof (ISession));

            SetupRollbackExpectations(connection, connectionFactory, session);

            mocks.ReplayAll();

            EmsTransactionManager tm = new EmsTransactionManager(connectionFactory);
            ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition());
            EmsTemplate nt = new EmsTemplate(connectionFactory);
            nt.Execute(new AssertSessionCallback(session));
            tm.Rollback(ts);

            mocks.VerifyAll();
        }
        public void TransactionRollback()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = (IConnection)mocks.CreateMock(typeof(IConnection));
            ISession           session           = (ISession)mocks.CreateMock(typeof(ISession));

            SetupRollbackExpectations(connection, connectionFactory, session);

            mocks.ReplayAll();

            EmsTransactionManager tm = new EmsTransactionManager(connectionFactory);
            ITransactionStatus    ts = tm.GetTransaction(new DefaultTransactionDefinition());
            EmsTemplate           nt = new EmsTemplate(connectionFactory);

            nt.Execute(new AssertSessionCallback(session));
            tm.Rollback(ts);

            mocks.VerifyAll();
        }
        public void ParticipatingTransactionWithRollback()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = (IConnection)mocks.CreateMock(typeof(IConnection));
            ISession           session           = (ISession)mocks.CreateMock(typeof(ISession));

            using (mocks.Ordered())
            {
                SetupRollbackExpectations(connection, connectionFactory, session);
            }

            mocks.ReplayAll();

            EmsTransactionManager tm = new EmsTransactionManager(connectionFactory);
            ITransactionStatus    ts = tm.GetTransaction(new DefaultTransactionDefinition());
            EmsTemplate           nt = new EmsTemplate(connectionFactory);

            nt.Execute(new AssertSessionCallback(session));

            TransactionTemplate tt = new TransactionTemplate(tm);

            tt.Execute(delegate(ITransactionStatus status)
            {
                nt.Execute(new AssertSessionCallback(session));
                status.SetRollbackOnly();
                return(null);
            });
            try
            {
                tm.Commit(ts);
                Assert.Fail("Should have thrown UnexpectedRollbackException");
            } catch (UnexpectedRollbackException)
            {
            }

            mocks.VerifyAll();
        }
        public void ParticipatingTransactionWithCommit()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection));
            ISession session = (ISession)mocks.CreateMock(typeof(ISession));

            using (mocks.Ordered())
            {
                SetupCommitExpectations(connection, connectionFactory, session);
            }

            mocks.ReplayAll();


            EmsTransactionManager tm = new EmsTransactionManager(connectionFactory);
            ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition());
            EmsTemplate nt = new EmsTemplate(connectionFactory);
            nt.Execute(new AssertSessionCallback(session));

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.Execute(delegate(ITransactionStatus status)
                           {
                               nt.Execute(new AssertSessionCallback(session));
                               return null;
                           });

            tm.Commit(ts);

            mocks.VerifyAll();

        }
        public void TransactionSuspension()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection));
            ISession session = (ISession)mocks.CreateMock(typeof(ISession));
            ISession session2 = (ISession)mocks.CreateMock(typeof(ISession));


            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Twice();
            Expect.Call(connection.CreateSession(true, Session.SESSION_TRANSACTED)).Return(session).Repeat.Once();
            Expect.Call(connection.CreateSession(true, Session.SESSION_TRANSACTED)).Return(session2).Repeat.Once();

            session.Commit();
            LastCall.On(session).Repeat.Once();
            session2.Commit();
            LastCall.On(session2).Repeat.Once();

            session.Close();
            LastCall.On(session).Repeat.Once();
            session2.Close();
            LastCall.On(session2).Repeat.Once();

            connection.Close();
            LastCall.On(connection).Repeat.Twice();

            mocks.ReplayAll();

            EmsTransactionManager tm = new EmsTransactionManager(connectionFactory);
            ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition());
            EmsTemplate nt = new EmsTemplate(connectionFactory);


            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.RequiresNew;
            tt.Execute(delegate(ITransactionStatus status)
                           {
                               nt.Execute(new AssertNotSameSessionCallback(session));
                               return null;
                           });

            nt.Execute(new AssertSessionCallback(session));

            tm.Commit(ts);

            mocks.VerifyAll();

        }
        public void ParticipatingTransactionWithRollback()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory) mocks.CreateMock(typeof (IConnectionFactory));
            IConnection connection = (IConnection) mocks.CreateMock(typeof (IConnection));
            ISession session = (ISession) mocks.CreateMock(typeof (ISession));

            using (mocks.Ordered())
            {
                SetupRollbackExpectations(connection, connectionFactory, session);
            }

            mocks.ReplayAll();

            EmsTransactionManager tm = new EmsTransactionManager(connectionFactory);
            ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition());
            EmsTemplate nt = new EmsTemplate(connectionFactory);
            nt.Execute(new AssertSessionCallback(session));

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.Execute(delegate(ITransactionStatus status)
                           {
                               nt.Execute(new AssertSessionCallback(session));
                               status.SetRollbackOnly();
                               return null;
                           });
            try
            {
                tm.Commit(ts);
                Assert.Fail("Should have thrown UnexpectedRollbackException");
            } catch (UnexpectedRollbackException)
            {
                
            }

            mocks.VerifyAll();
        }