public void PropagationRequiresNewWithExistingTransaction()
        {
            ITransactionScopeAdapter txAdapter = (ITransactionScopeAdapter)mocks.CreateMock(typeof(ITransactionScopeAdapter));

            using (mocks.Ordered())
            {
                Expect.Call(txAdapter.IsExistingTransaction).Return(false);
                TransactionOptions txOptions = new TransactionOptions();
                txOptions.IsolationLevel = IsolationLevel.Unspecified;
                txAdapter.CreateTransactionScope(TransactionScopeOption.RequiresNew, txOptions, EnterpriseServicesInteropOption.None);

                //inner tx actions
                Expect.Call(txAdapter.IsExistingTransaction).Return(true);
                txAdapter.CreateTransactionScope(TransactionScopeOption.RequiresNew, txOptions, EnterpriseServicesInteropOption.None);
                txAdapter.Dispose();
                //end inner tx actions

                Expect.Call(txAdapter.RollbackOnly).Return(false);
                txAdapter.Complete();
                txAdapter.Dispose();
            }
            mocks.ReplayAll();

            TxScopeTransactionManager tm = new TxScopeTransactionManager(txAdapter);

            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);

            tt.PropagationBehavior = TransactionPropagation.RequiresNew;
            tt.Execute(status =>
            {
                Assert.IsTrue(status.IsNewTransaction, "Is new transaction");
                Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive, "Synchronization active");
                Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);

                tt.Execute(status2 =>
                {
                    Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive, "Synchronization active");
                    Assert.IsTrue(status2.IsNewTransaction, "Is new transaction");
                    Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                    Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);
                    status2.SetRollbackOnly();
                    return(null);
                });


                Assert.IsTrue(status.IsNewTransaction, "Is new transaction");
                Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);
                return(null);
            });

            mocks.VerifyAll();
        }
        public void TransactionCommit()
        {
            ITransactionScopeAdapter txAdapter = (ITransactionScopeAdapter)mocks.CreateMock(typeof(ITransactionScopeAdapter));

            using (mocks.Ordered())
            {
                Expect.Call(txAdapter.IsExistingTransaction).Return(false);
                TransactionOptions txOptions = new TransactionOptions();
                txOptions.IsolationLevel = IsolationLevel.Unspecified;
                txAdapter.CreateTransactionScope(TransactionScopeOption.Required, txOptions, EnterpriseServicesInteropOption.None);

                Expect.Call(txAdapter.RollbackOnly).Return(false);
                txAdapter.Complete();
                txAdapter.Dispose();
            }
            mocks.ReplayAll();

            TxScopeTransactionManager tm = new TxScopeTransactionManager(txAdapter);

            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);

            tt.Execute(status =>
            {
                Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive);
                Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                return(null);
            });

            Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive);
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);

            mocks.VerifyAll();
        }
        public void TransactionCommit()
        {
            ITransactionScopeAdapter txAdapter = A.Fake <ITransactionScopeAdapter>();

            A.CallTo(() => txAdapter.IsExistingTransaction).Returns(false);

            A.CallTo(() => txAdapter.RollbackOnly).Returns(false);

            TxScopeTransactionManager tm = new TxScopeTransactionManager(txAdapter);

            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);

            tt.Execute(status =>
            {
                Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive);
                Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                return(null);
            });

            Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive);
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);

            TransactionOptions txOptions = new TransactionOptions();

            txOptions.IsolationLevel = IsolationLevel.ReadCommitted;
            txAdapter.CreateTransactionScope(TransactionScopeOption.Required, txOptions, TransactionScopeAsyncFlowOption.Enabled);
            txAdapter.Complete();
            txAdapter.Dispose();
        }
        public void PropagationRequiresNewWithExistingTransaction()
        {
            ITransactionScopeAdapter txAdapter = A.Fake <ITransactionScopeAdapter>();

            A.CallTo(() => txAdapter.IsExistingTransaction).Returns(false).Once();

            TransactionOptions txOptions = new TransactionOptions();

            txOptions.IsolationLevel = IsolationLevel.ReadCommitted;

            //inner tx actions
            A.CallTo(() => txAdapter.IsExistingTransaction).Returns(true).Once();
            //end inner tx actions

            A.CallTo(() => txAdapter.RollbackOnly).Returns(false);

            TxScopeTransactionManager tm = new TxScopeTransactionManager(txAdapter);

            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);

            tt.PropagationBehavior = TransactionPropagation.RequiresNew;
            tt.Execute(status =>
            {
                Assert.IsTrue(status.IsNewTransaction, "Is new transaction");
                Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive, "Synchronization active");
                Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);

                tt.Execute(status2 =>
                {
                    Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive, "Synchronization active");
                    Assert.IsTrue(status2.IsNewTransaction, "Is new transaction");
                    Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                    Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);
                    status2.SetRollbackOnly();
                    return(null);
                });


                Assert.IsTrue(status.IsNewTransaction, "Is new transaction");
                Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);
                return(null);
            });

            A.CallTo(() => txAdapter.CreateTransactionScope(TransactionScopeOption.RequiresNew, txOptions, TransactionScopeAsyncFlowOption.Enabled)).MustHaveHappenedTwiceExactly();
            A.CallTo(() => txAdapter.Dispose()).MustHaveHappenedTwiceExactly();
            A.CallTo(() => txAdapter.Complete()).MustHaveHappenedOnceExactly();
        }
        public void TransactionRollback()
        {
            ITransactionScopeAdapter txAdapter = (ITransactionScopeAdapter)mocks.CreateMock(typeof(ITransactionScopeAdapter));

            using (mocks.Ordered())
            {
                Expect.Call(txAdapter.IsExistingTransaction).Return(false);
                TransactionOptions txOptions = new TransactionOptions();
                txOptions.IsolationLevel = IsolationLevel.Unspecified;
                txAdapter.CreateTransactionScope(TransactionScopeOption.Required, txOptions, EnterpriseServicesInteropOption.None);
                txAdapter.Dispose();
            }
            mocks.ReplayAll();

            TxScopeTransactionManager tm = new TxScopeTransactionManager(txAdapter);

            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);

            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            Exception ex = new ArgumentException("test exception");

            try
            {
                tt.Execute(status =>
                {
                    Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive);
                    Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                    Assert.IsTrue(status.IsNewTransaction, "Is new transaction");
                    if (ex != null)
                    {
                        throw ex;
                    }
                    return(null);
                });
                Assert.Fail("Should have thrown exception");
            }
            catch (ArgumentException e)
            {
                Assert.AreEqual(ex, e);
            }

            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");


            mocks.VerifyAll();
        }
        public void TransactionRollback()
        {
            ITransactionScopeAdapter txAdapter = A.Fake <ITransactionScopeAdapter>();

            A.CallTo(() => txAdapter.IsExistingTransaction).Returns(false);
            TransactionOptions txOptions = new TransactionOptions();

            txOptions.IsolationLevel = IsolationLevel.ReadCommitted;

            TxScopeTransactionManager tm = new TxScopeTransactionManager(txAdapter);

            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);

            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            Exception ex = new ArgumentException("test exception");

            try
            {
                tt.Execute(status =>
                {
                    Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive);
                    Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                    Assert.IsTrue(status.IsNewTransaction, "Is new transaction");
                    if (ex != null)
                    {
                        throw ex;
                    }
                    return(null);
                });
                Assert.Fail("Should have thrown exception");
            }
            catch (ArgumentException e)
            {
                Assert.AreEqual(ex, e);
            }

            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");
            A.CallTo(() => txAdapter.CreateTransactionScope(TransactionScopeOption.Required, txOptions, TransactionScopeAsyncFlowOption.Enabled)).MustHaveHappenedOnceExactly();
            A.CallTo(() => txAdapter.Dispose()).MustHaveHappenedOnceExactly();
        }