private void DoTestRollbackOnException(Exception exception, bool shouldRollback, bool rollbackException)
        {
            ITransactionAttribute txatt = new ConfigurableTransactionAttribute(shouldRollback);


            MethodInfo mi = typeof(ITestObject).GetMethod("Exceptional");

            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();

            tas.AddTransactionalMethod(mi, txatt);
            ITransactionStatus status = TransactionStatusForNewTransaction();

            IPlatformTransactionManager ptm =
                (IPlatformTransactionManager)mocks.DynamicMock(typeof(IPlatformTransactionManager));


            Expect.On(ptm).Call(ptm.GetTransaction(txatt)).Return(status);


            if (shouldRollback)
            {
                ptm.Rollback(status);
            }
            else
            {
                ptm.Commit(status);
            }
            TransactionSystemException tex = new TransactionSystemException("system exception");

            if (rollbackException)
            {
                LastCall.On(ptm).Throw(tex).Repeat.Once();
            }
            else
            {
                LastCall.On(ptm).Repeat.Once();
            }
            mocks.ReplayAll();

            TestObject  to  = new TestObject();
            ITestObject ito = (ITestObject)Advised(to, ptm, tas);

            try
            {
                ito.Exceptional(exception);
                Assert.Fail("Should have thrown exception");
            } catch (Exception e)
            {
                if (rollbackException)
                {
                    Assert.AreEqual(tex, e);
                }
                else
                {
                    Assert.AreEqual(exception, e);
                }
            }

            mocks.VerifyAll();
        }
        public void CannotCommitTransaction()
        {
            ITransactionAttribute txatt = new DefaultTransactionAttribute();
            MethodInfo m = typeof (ITestObject).GetMethod("GetDescription");
            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();
            tas.AddTransactionalMethod(m, txatt);


            IPlatformTransactionManager ptm = PlatformTxManagerForNewTransaction();

            ITransactionStatus status = TransactionStatusForNewTransaction();
            Expect.On(ptm).Call(ptm.GetTransaction(txatt)).Return(status);
            UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
            ptm.Commit(status);
            LastCall.On(ptm).Throw(ex);
            mocks.ReplayAll();

            TestObject to = new TestObject();
            ITestObject ito = (ITestObject) Advised(to, ptm, tas);

            try
            {
                ito.GetDescription();
                Assert.Fail("Shouldn't have succeeded");
            } catch (UnexpectedRollbackException thrown)
            {
                Assert.IsTrue(thrown == ex);
            }

            mocks.VerifyAll();


            
        }
        public void CannotCommitTransaction()
        {
            ITransactionAttribute txatt             = new DefaultTransactionAttribute();
            MethodInfo            m                 = typeof(ITestObject).GetMethod("GetDescription");
            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();

            tas.AddTransactionalMethod(m, txatt);


            IPlatformTransactionManager ptm = PlatformTxManagerForNewTransaction();

            ITransactionStatus status = TransactionStatusForNewTransaction();

            Expect.On(ptm).Call(ptm.GetTransaction(txatt)).Return(status);
            UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);

            ptm.Commit(status);
            LastCall.On(ptm).Throw(ex);
            mocks.ReplayAll();

            TestObject  to  = new TestObject();
            ITestObject ito = (ITestObject)Advised(to, ptm, tas);

            try
            {
                ito.GetDescription();
                Assert.Fail("Shouldn't have succeeded");
            } catch (UnexpectedRollbackException thrown)
            {
                Assert.IsTrue(thrown == ex);
            }

            mocks.VerifyAll();
        }
        public void ProgrammaticRollback()
        {
            ITransactionAttribute txatt = new DefaultTransactionAttribute();
            MethodInfo            m     = typeof(RollbackTestObject).GetMethod("GetDescription");

            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();

            tas.AddTransactionalMethod(m, txatt);

            ITransactionStatus status = TransactionStatusForNewTransaction();

            IPlatformTransactionManager ptm = PlatformTxManagerForNewTransaction();

            Expect.Call(ptm.GetTransaction(txatt)).Return(status).Repeat.Once();
            ptm.Commit(status);
            LastCall.On(ptm).Repeat.Once();

            mocks.ReplayAll();

            RollbackTestObject to = new RollbackTestObject();

            ITestObject ito = (ITestObject)Advised(to, ptm, tas);

            Assert.AreEqual("test description", ito.GetDescription());

            mocks.VerifyAll();
        }
        public void CannotCommitTransaction()
        {
            ITransactionAttribute txatt             = new DefaultTransactionAttribute();
            MethodInfo            m                 = typeof(ITestObject).GetMethod("GetDescription");
            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();

            tas.AddTransactionalMethod(m, txatt);

            IPlatformTransactionManager ptm    = A.Fake <IPlatformTransactionManager>();
            ITransactionStatus          status = A.Fake <ITransactionStatus>();

            A.CallTo(() => ptm.GetTransaction(txatt)).Returns(status);
            UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);

            A.CallTo(() => ptm.Commit(status)).Throws(ex);

            TestObject  to  = new TestObject();
            ITestObject ito = (ITestObject)Advised(to, ptm, tas);

            try
            {
                ito.GetDescription();
                Assert.Fail("Shouldn't have succeeded");
            }
            catch (UnexpectedRollbackException thrown)
            {
                Assert.IsTrue(thrown == ex);
            }
        }
        private void DoTestRollbackOnException(Exception exception, bool shouldRollback, bool rollbackException)
        {
            ITransactionAttribute txatt = new ConfigurableTransactionAttribute(shouldRollback);


            MethodInfo mi = typeof(ITestObject).GetMethod("Exceptional");

            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();

            tas.AddTransactionalMethod(mi, txatt);
            ITransactionStatus status = A.Fake <ITransactionStatus>();

            IPlatformTransactionManager ptm = A.Fake <IPlatformTransactionManager>();

            A.CallTo(() => ptm.GetTransaction(txatt)).Returns(status);


            TransactionSystemException tex = new TransactionSystemException("system exception");

            if (rollbackException)
            {
                A.CallTo(() => ptm.Rollback(A <ITransactionStatus> ._)).Throws(tex);
            }

            TestObject  to  = new TestObject();
            ITestObject ito = (ITestObject)Advised(to, ptm, tas);

            try
            {
                ito.Exceptional(exception);
                Assert.Fail("Should have thrown exception");
            }
            catch (Exception e)
            {
                if (rollbackException && shouldRollback)
                {
                    Assert.AreEqual(tex, e);
                }
                else
                {
                    Assert.AreEqual(exception, e);
                }
            }

            if (shouldRollback)
            {
                A.CallTo(() => ptm.Rollback(status)).MustHaveHappenedOnceExactly();
            }
            else
            {
                A.CallTo(() => ptm.Commit(status)).MustHaveHappenedOnceExactly();
            }
        }
        public void MethodMapTransactionAttributeSource()
        {
            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();
            IDictionary methodMap = new Hashtable();
            methodMap.Add("System.Object.GetHashCode, mscorlib", "PROPAGATION_REQUIRED");
            methodMap.Add("System.Object.ToString, mscorlib",
                          new DefaultTransactionAttribute(TransactionPropagation.Supports));
            tas.MethodMap = methodMap;

            ITransactionAttribute ta = tas.ReturnTransactionAttribute(typeof(object).GetMethod("GetHashCode"), null);
            Assert.IsNotNull(ta);
            Assert.AreEqual(TransactionPropagation.Required, ta.PropagationBehavior);

            ta = tas.ReturnTransactionAttribute(typeof(object).GetMethod("ToString"), null);
            Assert.IsNotNull(ta);
            Assert.AreEqual(TransactionPropagation.Supports, ta.PropagationBehavior);
        }
        public void MethodMapTransactionAttributeSource()
        {
            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();
            IDictionary methodMap = new Hashtable();

            methodMap.Add("System.Object.GetHashCode, mscorlib", "PROPAGATION_REQUIRED");
            methodMap.Add("System.Object.ToString, mscorlib",
                          new DefaultTransactionAttribute(TransactionPropagation.Supports));
            tas.MethodMap = methodMap;

            ITransactionAttribute ta = tas.ReturnTransactionAttribute(typeof(object).GetMethod("GetHashCode"), null);

            Assert.IsNotNull(ta);
            Assert.AreEqual(TransactionPropagation.Required, ta.PropagationBehavior);

            ta = tas.ReturnTransactionAttribute(typeof(object).GetMethod("ToString"), null);
            Assert.IsNotNull(ta);
            Assert.AreEqual(TransactionPropagation.Supports, ta.PropagationBehavior);
        }
        /// <summary>
        /// Parses the input properties <see cref="System.String"/> into a valid
        /// <see cref="Spring.Transaction.Interceptor.ITransactionAttributeSource"/>
        /// instance
        /// </summary>
        /// <param name="attributeSource">The properties string to be parsed.</param>
        public void SetAsText(string attributeSource)
        {
            MethodMapTransactionAttributeSource source = new MethodMapTransactionAttributeSource();

            if (attributeSource == null || attributeSource.Length == 0)
            {
                _attributeSource = null;
            }
            else
            {
                PropertiesEditor           editor = new PropertiesEditor(attributeSource);
                TransactionAttributeEditor tae    = new TransactionAttributeEditor();

                foreach (string name in editor.Keys)
                {
                    string value = editor[name];
                    tae.SetAsText(value);
                    ITransactionAttribute transactionAttribute = tae.Value;
                    source.AddTransactionalMethod(name, transactionAttribute);
                }
            }
            _attributeSource = source;
        }
        public void ProgrammaticRollback()
        {
            ITransactionAttribute txatt = new DefaultTransactionAttribute();
            MethodInfo            m     = typeof(RollbackTestObject).GetMethod("GetDescription");

            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();

            tas.AddTransactionalMethod(m, txatt);

            ITransactionStatus status = A.Fake <ITransactionStatus>();

            IPlatformTransactionManager ptm = A.Fake <IPlatformTransactionManager>();

            A.CallTo(() => ptm.GetTransaction(txatt)).Returns(status).Once();

            RollbackTestObject to = new RollbackTestObject();

            ITestObject ito = (ITestObject)Advised(to, ptm, tas);

            Assert.AreEqual("test description", ito.GetDescription());

            A.CallTo(() => ptm.Commit(status)).MustHaveHappenedOnceExactly();
        }
        private void DoTestRollbackOnException(Exception exception, bool shouldRollback, bool rollbackException)
        {
            ITransactionAttribute txatt = new ConfigurableTransactionAttribute(shouldRollback);


            MethodInfo mi = typeof (ITestObject).GetMethod("Exceptional");

            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();
            tas.AddTransactionalMethod(mi, txatt);
            ITransactionStatus status = TransactionStatusForNewTransaction();

            IPlatformTransactionManager ptm =
                (IPlatformTransactionManager) mocks.DynamicMock(typeof (IPlatformTransactionManager));

            
            Expect.On(ptm).Call(ptm.GetTransaction(txatt)).Return(status);
            

            if (shouldRollback)
            {
                ptm.Rollback(status);
            }
            else
            {
                ptm.Commit(status);
            }
            TransactionSystemException tex = new TransactionSystemException("system exception");
            if (rollbackException)
            {
                LastCall.On(ptm).Throw(tex).Repeat.Once();
            }
            else
            {
                LastCall.On(ptm).Repeat.Once();
            }
            mocks.ReplayAll();

            TestObject to = new TestObject();
            ITestObject ito = (ITestObject) Advised(to, ptm, tas);

            try
            {
                ito.Exceptional(exception);
                Assert.Fail("Should have thrown exception");
            } catch (Exception e)
            {
                if (rollbackException)
                {
                    Assert.AreEqual(tex, e);
                }
                else
                {
                    Assert.AreEqual(exception, e);
                }
            }

            mocks.VerifyAll();

        }
        public void ProgrammaticRollback()
        {
            ITransactionAttribute txatt = new DefaultTransactionAttribute();
            MethodInfo m = typeof(RollbackTestObject).GetMethod("GetDescription");

            MethodMapTransactionAttributeSource tas = new MethodMapTransactionAttributeSource();
            tas.AddTransactionalMethod(m, txatt);

            ITransactionStatus status = TransactionStatusForNewTransaction();

            IPlatformTransactionManager ptm = PlatformTxManagerForNewTransaction();

            Expect.Call(ptm.GetTransaction(txatt)).Return(status).Repeat.Once();
            ptm.Commit(status);
            LastCall.On(ptm).Repeat.Once();

            mocks.ReplayAll();
            
            RollbackTestObject to = new RollbackTestObject();

            ITestObject ito = (ITestObject) Advised(to, ptm, tas);

            Assert.AreEqual("test description", ito.GetDescription());

            mocks.VerifyAll();


        }
		/// <summary>
		/// Parses the input properties <see cref="System.String"/> into a valid
		/// <see cref="Spring.Transaction.Interceptor.ITransactionAttributeSource"/>
		/// instance
		/// </summary>
		/// <param name="attributeSource">The properties string to be parsed.</param>
		public void SetAsText( string attributeSource )
		{
			MethodMapTransactionAttributeSource source = new MethodMapTransactionAttributeSource();
			if ( attributeSource == null || attributeSource.Length == 0 )
			{
				_attributeSource = null;
			} else
			{
				PropertiesEditor editor = new PropertiesEditor(attributeSource);
				TransactionAttributeEditor tae = new TransactionAttributeEditor();

				foreach ( string name in editor.Keys )
				{
					string value = editor[name];
					tae.SetAsText( value );
					ITransactionAttribute transactionAttribute = tae.Value;
					source.AddTransactionalMethod( name, transactionAttribute );
				}
			}
			_attributeSource = source;
		}