DelegatingIntroductionInterceptorTests.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Aop » Support » 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 » Support » DelegatingIntroductionInterceptorTests.cs
#region License
/*
 * Copyright 2002-2004 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.Collections;

using NUnit.Framework;
using DotNetMock.Dynamic;

using AopAlliance.Aop;
using Spring.Aop.Framework;
using Spring.Objects;
#endregion

namespace Spring.Aop.Support{
  /// <summary>
  /// Translation of DelegatingIntroductionInterceptor unit tests to Spring.NET.
  /// </summary>
  /// <remarks>
  /// Spring.NET doesn't have a DelegatingIntroductionInterceptor because it handles
  /// introductions without using interception. So all the unit tests show how similar
  /// things can be done in Spring.NET.
  /// </remarks>
  /// <author>Rod Johnson</author>
  /// <author>Choy Rim (.NET)</author>
  [TestFixture]
  public class DelegatingIntroductionInterceptorTests
  {
    private static readonly DateTime EXPECTED_TIMESTAMP = new DateTime(2004,8,1);

    [Test]
    [ExpectedException(typeof(ArgumentNullException))]
    public void testNullTarget()
    {
      DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(null, typeof(ITimeStamped));
    }

    public interface ITimeStampedIntroduction: ITimeStamped, IAdvice
    {
    }

    [Test]
    public void testIntroductionInterceptorWithDelegation()
    {
      TestObject raw = new TestObject();
      Assert.IsTrue(! (raw is ITimeStamped));
      ProxyFactory factory = new ProxyFactory(raw);
  
      IDynamicMock tsControl = new DynamicMock(typeof(ITimeStampedIntroduction));
      ITimeStampedIntroduction ts = (ITimeStampedIntroduction) tsControl.Object;
      tsControl.ExpectAndReturn("TimeStamp", EXPECTED_TIMESTAMP);

      DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts);
      factory.AddIntroduction(advisor);

      ITimeStamped tsp = (ITimeStamped) factory.GetProxy();
      Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP);
  
      tsControl.Verify();
    }

    // we have to mark the ISubTimeStamped interface with the IAdvice marker
    // in order to use it as an introduction.
    public interface ISubTimeStampedIntroduction: ISubTimeStamped, IAdvice
    {
    }
  
    public void testIntroductionInterceptorWithInterfaceHierarchy() 
    {
      TestObject raw = new TestObject();
      Assert.IsTrue(! (raw is ISubTimeStamped));
      ProxyFactory factory = new ProxyFactory(raw);

      IDynamicMock tsControl = new DynamicMock(typeof(ISubTimeStampedIntroduction));
      ISubTimeStampedIntroduction ts = (ISubTimeStampedIntroduction) tsControl.Object;
      tsControl.ExpectAndReturn("TimeStamp", EXPECTED_TIMESTAMP);

      DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts);
      // we must add introduction, not an advisor
      factory.AddIntroduction(advisor);

      object proxy = factory.GetProxy();
      ISubTimeStamped tsp = (ISubTimeStamped) proxy;
      Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP);

      tsControl.Verify();
    }

    public void testIntroductionInterceptorWithSuperInterface()  
    {
      TestObject raw = new TestObject();
      Assert.IsTrue(! (raw is ITimeStamped));
      ProxyFactory factory = new ProxyFactory(raw);

      IDynamicMock tsControl = new DynamicMock(typeof(ISubTimeStampedIntroduction));
      ISubTimeStamped ts = (ISubTimeStamped) tsControl.Object;
      tsControl.ExpectAndReturn("TimeStamp", EXPECTED_TIMESTAMP);

      factory.AddIntroduction(0, new DefaultIntroductionAdvisor(
        (ISubTimeStampedIntroduction)ts,
        typeof(ITimeStamped))
        );

      ITimeStamped tsp = (ITimeStamped) factory.GetProxy();
      Assert.IsTrue(!(tsp is ISubTimeStamped));
      Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP);

      tsControl.Verify();
    }

    /// <summary>
    /// test introduction.
    /// <note>It must include the IAdvice marker interface to be a
    /// valid introduction.</note>
    /// </summary>
    private class Test : ITimeStamped, ITest, IAdvice
    {
      private DateTime _timestamp;

      public Test(DateTime timestamp) 
      {
        _timestamp = timestamp;
      }
      public void foo() 
      {
      }
      public DateTime TimeStamp 
      {
        get 
        {
          return _timestamp;
        }
      }
    }

    public void testAutomaticInterfaceRecognitionInDelegate() 
    {
      IIntroductionAdvisor ia = new DefaultIntroductionAdvisor(new Test(EXPECTED_TIMESTAMP));
    
      TestObject target = new TestObject();
      ProxyFactory pf = new ProxyFactory(target);
      pf.AddIntroduction(0, ia);

      ITimeStamped ts = (ITimeStamped) pf.GetProxy();
    
      Assert.IsTrue(ts.TimeStamp == EXPECTED_TIMESTAMP);
      ((ITest) ts).foo();
    
      int age = ((ITestObject) ts).Age;
    }

    /*
     * The rest of the tests in the original tested subclassing the
     * DelegatingIntroductionInterceptor.
     * 
     * Since we don't need to subclass anything to make a delegating
     * introduction, the rest of the tests are not necessary.
     */

    // must be public to be used for AOP
    // AOP creates a new assembly which must have access to the
    // interfaces that it intends to expose.
    public interface ITest 
    {
      void foo();
    }

    public interface ISubTimeStamped : ITimeStamped 
    {
    }

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