public void NoHandlerMethodForThrowable()
        {
            MyThrowsHandler         th = new MyThrowsHandler();
            ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);

            Assert.AreEqual(2, ti.HandlerMethodCount);
            Exception ex = new Exception();

            MockRepository    repository = new MockRepository();
            IMethodInvocation mi         = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation));

            Expect.Call(mi.Proceed()).Throw(ex);
            repository.ReplayAll();
            try
            {
                ti.Invoke(mi);
                Assert.Fail();
            }
            catch (Exception caught)
            {
                Assert.AreEqual(ex, caught);
            }
            Assert.AreEqual(0, th.GetCalls());
            repository.VerifyAll();
        }
        public void HandlerMethodThrowsException()
        {
            Exception               exception   = new Exception();
            MyThrowsHandler         handler     = new ThrowingMyHandler(exception);
            ThrowsAdviceInterceptor interceptor = new ThrowsAdviceInterceptor(handler);
            // extends RemotingException...
            RemotingTimeoutException ex = new RemotingTimeoutException();

            MockRepository    repository = new MockRepository();
            IMethodInvocation mi         = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation));

            Expect.Call(mi.Proceed()).Throw(ex);
            repository.ReplayAll();
            try
            {
                interceptor.Invoke(mi);
                Assert.Fail("Should not have reached this point, should have thrown an exception.");
            }
            catch (Exception caught)
            {
                Assert.AreEqual(exception, caught);
            }
            Assert.AreEqual(1, handler.GetCalls());
            Assert.AreEqual(1, handler.GetCalls("RemotingException"));
            repository.VerifyAll();
        }
        public void CorrectHandlerUsedForSubclass()
        {
            MyThrowsHandler         th = new MyThrowsHandler();
            ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
            // Extends RemotingException
            RemotingTimeoutException ex = new RemotingTimeoutException();

            MockRepository    repository = new MockRepository();
            IMethodInvocation mi         = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation));

            Expect.Call(mi.Proceed()).Throw(ex);
            repository.ReplayAll();
            try
            {
                ti.Invoke(mi);
                Assert.Fail();
            }
            catch (Exception caught)
            {
                Assert.AreEqual(ex, caught);
            }
            Assert.AreEqual(1, th.GetCalls());
            Assert.AreEqual(1, th.GetCalls("RemotingException"));

            repository.VerifyAll();
        }
        public void NestedInnerExceptionsAreNotPickedUp()
        {
            MyThrowsHandler         throwsHandler     = new MyThrowsHandler();
            ThrowsAdviceInterceptor throwsInterceptor = new ThrowsAdviceInterceptor(throwsHandler);
            // nest the exceptions; make sure the advice gets applied because of the inner exception...
            Exception         exception  = new FormatException("Parent", new HttpException("Inner"));
            MockRepository    repository = new MockRepository();
            IMethodInvocation invocation = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation));

            Expect.Call(invocation.Proceed()).Throw(exception);
            repository.ReplayAll();
            try
            {
                throwsInterceptor.Invoke(invocation);
                Assert.Fail("Must have failed (by throwing an exception by this point - check the mock).");
            }
            catch (Exception caught)
            {
                Assert.AreEqual(exception, caught);
            }
            Assert.AreEqual(0, throwsHandler.GetCalls(),
                            "Must NOT have been handled, 'cos the HttpException was wrapped by " +
                            "another Exception that did not have a handler.");
            Assert.AreEqual(0, throwsHandler.GetCalls("HttpException"),
                            "Similarly, must NOT have been handled, 'cos the HttpException was wrapped by " +
                            "another Exception that did not have a handler.");
            repository.VerifyAll();
        }
        public void CorrectHandlerUsed()
        {
            MyThrowsHandler         th = new MyThrowsHandler();
            ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
            HttpException           ex = new HttpException();

            MockRepository    repository = new MockRepository();
            IMethodInvocation mi         = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation));

            Expect.Call(mi.Method).Return(ReflectionUtils.GetMethod(typeof(object), "HashCode", new Type[] {}));
            Expect.Call(mi.Arguments).Return(null);
            Expect.Call(mi.This).Return(new object());
            Expect.Call(mi.Proceed()).Throw(ex);
            repository.ReplayAll();
            try
            {
                ti.Invoke(mi);
                Assert.Fail();
            }
            catch (Exception caught)
            {
                Assert.AreEqual(ex, caught);
            }
            Assert.AreEqual(1, th.GetCalls());
            Assert.AreEqual(1, th.GetCalls("HttpException"));

            repository.VerifyAll();
        }
        public void NotInvoked()
        {
            MockRepository    repository = new MockRepository();
            IMethodInvocation mi         = (IMethodInvocation)repository.CreateMock(typeof(IMethodInvocation));

            MyThrowsHandler         th = new MyThrowsHandler();
            ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
            object ret = new object();

            Expect.Call(mi.Proceed()).Return(ret);
            repository.ReplayAll();
            Assert.AreEqual(ret, ti.Invoke(mi));
            Assert.AreEqual(0, th.GetCalls());
            repository.VerifyAll();
        }