ImplementationFactory.cs :  » Testing » MockObjects » DotNetMock » TestFramework » 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 » ImplementationFactory.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;

#endregion

namespace DotNetMock.TestFramework{
  /// <summary>
  /// Creates an implementation of <see cref="ITestFramework"/>
  /// based on settings in the environment.
  /// </summary>
  public class ImplementationFactory
  {
    const string STATIC_IMPLEMENTATION_ASSEMBLY_KEY =
      "DotNetMock_TestingAssembly";
    const string STATIC_IMPLEMENTATION_TYPE_KEY =
      "DotNetMock_TestingComponent";

    const string NUNIT_ASSEMBLY_NAME = "nunit.framework";
    const string MBUNIT_ASSEMBLY_NAME = "MbUnit.Core";
    const string CSUNIT_ASSEMBLY_NAME = "csUnit";

    private IDictionary _env;
    private IDynamicLinker _linker;

    /// <summary>
    /// Create an implementation factory.
    /// </summary>
    /// <param name="env"><see cref="IDictionary"/> of environment
    /// variable name value entries</param>
    /// <param name="linker">provider of reflection services</param>
    public ImplementationFactory(IDictionary env, IDynamicLinker linker)
    {
      _env    = env;
      _linker = linker;
    }
    /// <summary>
    /// Create an appropriate implementation for the given
    /// environment.
    /// </summary>
    /// <returns>a new <see cref="ITestFramework"/></returns>
    public ITestFramework NewImplementation() 
    {
      Type implementationType = getStaticImplementationType();
      if ( implementationType==null ) 
      {
        implementationType = getDynamicImplementationType();
      }
      if ( implementationType==null ) 
      {
        throw new SystemException(
          "Cannot find an appropriate test framework implementation."
          );
      }
      ITestFramework implementation = (ITestFramework)
        _linker.CreateInstance(implementationType);
      return implementation;
    }
    private Type getStaticImplementationType() 
    {
      string assemblyName = (string)
        _env[STATIC_IMPLEMENTATION_ASSEMBLY_KEY];
      if ( (assemblyName==null) || (assemblyName.Equals(String.Empty)) ) 
      {
        return null;
      }
      string typeName = (string)
        _env[STATIC_IMPLEMENTATION_TYPE_KEY];
      if ( (typeName==null) || (typeName.Equals(String.Empty)) ) 
      {
        return null;
      }
      Assembly assembly =
        _linker.LoadAssembly(assemblyName);
      Type type =
        _linker.GetType(typeName, assembly);
      return type;
    }
    private Type getDynamicImplementationType() 
    {
      StubClassMaker classMaker = new StubClassMaker();
      IStubMaker stubMaker = null;
      Assembly assembly = null;
      if ( (assembly=_linker.LoadAssembly(NUNIT_ASSEMBLY_NAME))!=null ) 
      {
        stubMaker = new NUnitStubMaker(assembly, _linker);
      }
      else if ( (assembly=_linker.LoadAssembly(MBUNIT_ASSEMBLY_NAME))!=null ) 
      {
        stubMaker = new MbUnitStubMaker(assembly, _linker);
      }
      else if ( (assembly=_linker.LoadAssembly(CSUNIT_ASSEMBLY_NAME))!=null ) 
      {
        stubMaker = new csUnitStubMaker(assembly, _linker);
      } 
      else if ( (assembly=_linker.LoadAssemblyWithPartialName(NUNIT_ASSEMBLY_NAME))!=null ) 
      {
        stubMaker = new NUnitStubMaker(assembly, _linker);
      }
      else if ( (assembly=_linker.LoadAssemblyWithPartialName(MBUNIT_ASSEMBLY_NAME))!=null ) 
      {
        stubMaker = new MbUnitStubMaker(assembly, _linker);
      }
      else if ( (assembly=_linker.LoadAssemblyWithPartialName(CSUNIT_ASSEMBLY_NAME))!=null ) 
      {
        stubMaker = new csUnitStubMaker(assembly, _linker);
      } 
      else 
      {
        return null;
      }
      Type stubClass = classMaker.MakeStubClass(
        typeof(ITestFramework),
        stubMaker
        );
      return stubClass;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.