ImplementationFactoryTests.cs :  » Testing » MockObjects » DotNetMock » TestFramework » Tests » 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 » Testing » MockObjects 
MockObjects » DotNetMock » TestFramework » Tests » ImplementationFactoryTests.cs
#region License
// Copyright (c) 2004 Choy Rim. All rights reserved.
#endregion
#region Imports
using System;
using System.Collections;
using System.Reflection;
using DotNetMock.Core;
using NUnit.Framework;

using DotNetMock.TestFramework;
#endregion

namespace DotNetMock.TestFramework.Tests{
  [TestFixture]
  public class ImplementationFactoryTests 
  {
    const string EXPECTED_STATIC_ASSEMBLY_NAME = "Assembly";
    static readonly string EXPECTED_STATIC_TYPE_NAME = typeof(TestStaticImplementation).FullName;

    MockLinker linker;
    TestStaticImplementation implementation;
    Hashtable env;

    [Test] public void UseStaticImplementation() 
    {
      linker.ExpectedAssemblyName = EXPECTED_STATIC_ASSEMBLY_NAME;
      linker.ExpectedAssembly = Assembly.GetExecutingAssembly();
      linker.ExpectedType = typeof(TestStaticImplementation);

      env["DotNetMock_TestingAssembly"] = EXPECTED_STATIC_ASSEMBLY_NAME;
      env["DotNetMock_TestingComponent"] = EXPECTED_STATIC_TYPE_NAME;

      ImplementationFactory factory = new ImplementationFactory(env, linker);
      ITestFramework tf = factory.NewImplementation();
      Assert.IsNotNull(tf);
      Assert.AreSame(implementation, tf);

      Assert.AreEqual(1, linker.ActualAssemblyNames.Count);
      Assert.AreEqual("Assembly", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual(EXPECTED_STATIC_TYPE_NAME, linker.ActualTypeName);
      Assert.AreSame(Assembly.GetExecutingAssembly(), linker.ActualAssembly);
      Assert.AreEqual(typeof(TestStaticImplementation), linker.ActualType);
    }
    [Test] public void UseDynamicNUnitImplementation() 
    {
      linker.ExpectedAssemblyName = "nunit.framework";
      linker.ExpectedAssembly = typeof(Assertion).Module.Assembly;
      linker.ExpectedType = typeof(Assertion);

      ImplementationFactory factory = new ImplementationFactory(env, linker);
      ITestFramework tf = factory.NewImplementation();
      Assert.IsNotNull(tf);
      Assert.AreSame(implementation, tf);

      Assert.AreEqual(1, linker.ActualAssemblyNames.Count);
      Assert.AreEqual("nunit.framework", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("NUnit.Framework.Assertion", linker.ActualTypeName);
      Assert.AreSame(typeof(Assertion).Module.Assembly, linker.ActualAssembly);
      Assert.AreEqual("ProviderStub", linker.ActualType.FullName);
    }
    [Test] public void UseDynamicMbUnitImplementation() 
    {
      linker.ExpectedAssemblyName = "MbUnit.Core";
      linker.ExpectedAssembly = Assembly.GetExecutingAssembly();
      linker.ExpectedType = typeof(MbUnit.Core.Framework.Assert);

      ImplementationFactory factory = new ImplementationFactory(env, linker);
      ITestFramework tf = factory.NewImplementation();
      Assert.IsNotNull(tf);
      Assert.AreSame(implementation, tf);

      Assert.AreEqual(2, linker.ActualAssemblyNames.Count);
      Assert.AreEqual("MbUnit.Core", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("nunit.framework", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("MbUnit.Core.Framework.Assert", linker.ActualTypeName);
      Assert.AreSame(Assembly.GetExecutingAssembly(), linker.ActualAssembly);
      Assert.AreEqual("ProviderStub", linker.ActualType.FullName);
    }
    [Test] public void UseDynamicCsUnitImplementation() 
    {
      linker.ExpectedAssemblyName = "csUnit";
      linker.ExpectedAssembly = Assembly.GetExecutingAssembly();
      linker.ExpectedType = typeof(csUnit.Assert);

      ImplementationFactory factory = new ImplementationFactory(env, linker);
      ITestFramework tf = factory.NewImplementation();
      Assert.IsNotNull(tf);
      Assert.AreSame(implementation, tf);

      Assert.AreEqual(3, linker.ActualAssemblyNames.Count);
      Assert.AreEqual("csUnit", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("MbUnit.Core", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("nunit.framework", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("csUnit.Assert", linker.ActualTypeName);
      Assert.AreSame(Assembly.GetExecutingAssembly(), linker.ActualAssembly);
      Assert.AreEqual("ProviderStub", linker.ActualType.FullName);
    }
    [Test] public void UseDynamicNUnitImplementationViaPartialName() 
    {
      linker.ExpectedAssemblyName = "xxx";
      linker.ExpectedPartialAssemblyName = "nunit.framework";
      linker.ExpectedAssembly = typeof(Assertion).Module.Assembly;
      linker.ExpectedType = typeof(Assertion);

      ImplementationFactory factory = new ImplementationFactory(env, linker);
      ITestFramework tf = factory.NewImplementation();
      Assert.IsNotNull(tf);
      Assert.AreSame(implementation, tf);

      Assert.AreEqual(4, linker.ActualAssemblyNames.Count);
      Assert.AreEqual("nunit.framework", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("csUnit", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("MbUnit.Core", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("nunit.framework", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("NUnit.Framework.Assertion", linker.ActualTypeName);
      Assert.AreSame(typeof(Assertion).Module.Assembly, linker.ActualAssembly);
      Assert.AreEqual("ProviderStub", linker.ActualType.FullName);
    }
    [Test] public void UseDynamicMbUnitImplementationViaPartialName() 
    {
      linker.ExpectedAssemblyName = "xxx";
      linker.ExpectedPartialAssemblyName = "MbUnit.Core";
      linker.ExpectedAssembly = Assembly.GetExecutingAssembly();
      linker.ExpectedType = typeof(MbUnit.Core.Framework.Assert);

      ImplementationFactory factory = new ImplementationFactory(env, linker);
      ITestFramework tf = factory.NewImplementation();
      Assert.IsNotNull(tf);
      Assert.AreSame(implementation, tf);

      Assert.AreEqual(5, linker.ActualAssemblyNames.Count);
      Assert.AreEqual("MbUnit.Core", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("nunit.framework", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("csUnit", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("MbUnit.Core", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("nunit.framework", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("MbUnit.Core.Framework.Assert", linker.ActualTypeName);
      Assert.AreSame(Assembly.GetExecutingAssembly(), linker.ActualAssembly);
      Assert.AreEqual("ProviderStub", linker.ActualType.FullName);
    }
    [Test] public void UseDynamicCsUnitImplementationViaPartialName() 
    {
      linker.ExpectedAssemblyName = "xxx";
      linker.ExpectedPartialAssemblyName = "csUnit";
      linker.ExpectedAssembly = Assembly.GetExecutingAssembly();
      linker.ExpectedType = typeof(csUnit.Assert);

      ImplementationFactory factory = new ImplementationFactory(env, linker);
      ITestFramework tf = factory.NewImplementation();
      Assert.IsNotNull(tf);
      Assert.AreSame(implementation, tf);

      Assert.AreEqual(6, linker.ActualAssemblyNames.Count);
      Assert.AreEqual("csUnit", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("MbUnit.Core", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("nunit.framework", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("csUnit", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("MbUnit.Core", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("nunit.framework", linker.ActualAssemblyNames.Pop());
      Assert.AreEqual("csUnit.Assert", linker.ActualTypeName);
      Assert.AreSame(Assembly.GetExecutingAssembly(), linker.ActualAssembly);
      Assert.AreEqual("ProviderStub", linker.ActualType.FullName);
    }
    [SetUp]
    public void BeforeEachTest() 
    {
      linker = new MockLinker();
      implementation = new TestStaticImplementation();
      env = new Hashtable();

      linker.ExpectedInstance = implementation;
    }

    public class TestStaticImplementation : ITestFramework
    {
      #region ITestFramework Members

      public void Assert(bool assertion)
      {
        // TODO:  Add TestStaticImplementation.Assert implementation
      }

      void DotNetMock.Core.ITestFramework.Assert(string message, bool assertion)
      {
        // TODO:  Add TestStaticImplementation.DotNetMock.Core.ITestFramework.Assert implementation
      }

      public void AssertNotNull(object assertion)
      {
        // TODO:  Add TestStaticImplementation.AssertNotNull implementation
      }

      void DotNetMock.Core.ITestFramework.AssertNotNull(string message, object assertion)
      {
        // TODO:  Add TestStaticImplementation.DotNetMock.Core.ITestFramework.AssertNotNull implementation
      }

      public void AssertEquals(object expectedObject, object actualObject)
      {
        // TODO:  Add TestStaticImplementation.AssertEquals implementation
      }

      void DotNetMock.Core.ITestFramework.AssertEquals(string message, object expectedObject, object actualObject)
      {
        // TODO:  Add TestStaticImplementation.DotNetMock.Core.ITestFramework.AssertEquals implementation
      }

      public void Fail(string message)
      {
        // TODO:  Add TestStaticImplementation.Fail implementation
      }

      void DotNetMock.Core.ITestFramework.Fail()
      {
        // TODO:  Add TestStaticImplementation.DotNetMock.Core.ITestFramework.Fail implementation
      }

      public void AssertNull(object assertion)
      {
        // TODO:  Add TestStaticImplementation.AssertNull implementation
      }

      void DotNetMock.Core.ITestFramework.AssertNull(string message, object assertion)
      {
        // TODO:  Add TestStaticImplementation.DotNetMock.Core.ITestFramework.AssertNull implementation
      }

      #endregion
    }

    class MockLinker :
      IDynamicLinker
    {
      public Stack ActualAssemblyNames = new Stack();
      public string ExpectedAssemblyName = null;
      public string ExpectedPartialAssemblyName = null;
      public Assembly ExpectedAssembly = null;
      public string ActualTypeName = null;
      public Assembly ActualAssembly = null;
      public Type ExpectedType = null;
      public Type ActualType = null;
      public object ExpectedInstance = null;

      #region IDynamicLinker Members

      public Assembly LoadAssembly(string name)
      {
        ActualAssemblyNames.Push(name);
        if ( name!=ExpectedAssemblyName ) 
        {
          return null;
        }
        return ExpectedAssembly;
      }
      public Assembly LoadAssemblyWithPartialName(string name)
      {
        ActualAssemblyNames.Push(name);
        if ( name!=ExpectedPartialAssemblyName ) 
        {
          return null;
        }
        return ExpectedAssembly;
      }
      public Type GetType(string typeName, Assembly assembly) 
      {
        ActualTypeName = typeName;
        ActualAssembly = assembly;
        return ExpectedType;
      }
      public object CreateInstance(Type type)
      {
        ActualType = type;
        return ExpectedInstance;
      }

      #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.