ThrowsAdviceInterceptorTests.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Aop » Framework » Adapter » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Inversion of Control Dependency Injection » Spring.net 
Spring.net » Spring » Aop » Framework » Adapter » ThrowsAdviceInterceptorTests.cs
#region License

/*
 * Copyright  2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

#region Imports

using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Web;
using AopAlliance.Intercept;
using NUnit.Framework;
using Rhino.Mocks;
using Spring.Util;

#endregion

namespace Spring.Aop.Framework.Adapter{
  /// <summary>
  /// Unit tests for the ThrowsAdviceInterceptor class.
  /// </summary>
  /// <author>Rod Johnson</author>
  /// <author>Simon White (.NET)</author>
  [TestFixture]
  public sealed class ThrowsAdviceInterceptorTests
  {
    [Test]
    [ExpectedException(typeof (ArgumentException))]
    public void NoHandlerMethods()
    {
      new ThrowsAdviceInterceptor(new object());
    }

        [Test]
        [ExpectedException(typeof (ArgumentNullException))]
        public void PassNullAdviceToCtor()
        {
            new ThrowsAdviceInterceptor(null);
        }

    [Test]
    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();

            

    }

    [Test]
    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();

    }

    [Test]
    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();

        }

        [Test]
        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();
        }

      [Test]
        [ExpectedException(typeof(ArgumentException))]
      public void ChokesOnHandlerWhereMultipleMethodsAreApplicable()
      {
            object throwsHandler = new MultipleMethodsAreApplicableThrowsHandler();
            new ThrowsAdviceInterceptor(throwsHandler);
      }

    [Test]
    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();
    }

    [Test]
    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();

    }

        #region Helper Classes

        private sealed class MultipleMethodsAreApplicableThrowsHandler
        {
            public void AfterThrowing(
                MethodInfo method, object[] args, object target, RemotingException ex)
            {
            }

            public void AfterThrowing(RemotingException ex)
            {
            }
        }

      private class ThrowingMyHandler : MyThrowsHandler
      {
          private Exception exception;

          public ThrowingMyHandler(Exception ex)
          {
              this.exception = ex;
          }

          public override void AfterThrowing(RemotingException ex)
          {
              base.AfterThrowing(ex);
              throw exception;
          }
      }

      public class MyThrowsHandler : MethodCounter, IThrowsAdvice
      {
          public void AfterThrowing(
              MethodInfo m, object[] args, object target, HttpException ex)
          {
              Count("HttpException");
          }

          public virtual void AfterThrowing(RemotingException ex)
          {
              Count("RemotingException");
          }

          // not valid, wrong number of arguments...
          public void AfterThrowing(MethodInfo m, Exception ex)
          {
              throw new NotSupportedException("Shouldn't be called");
          }
      }

      public interface IEcho
      {
          int A { get; set; }

          int EchoException(int i, Exception t);
      }

      public class Echo : IEcho
      {
          private int a;

          public int A
          {
              get { return a; }
              set { a = value; }
          }

          public virtual int EchoException(int i, Exception ex)
          {
              if (ex != null)
              {
                  throw ex;
              }
              return i;
          }
      }

      #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.