From: Choy R. <ch...@us...> - 2005-01-29 08:49:23
|
Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock/TestFramework In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9962/DotNetMock/TestFramework Modified Files: Tag: RFE_1098585 AbstractStubMaker.cs MbUnitStubMaker.cs NUnitStubMaker.cs csUnitStubMaker.cs Added Files: Tag: RFE_1098585 IDynamicLinker.cs Implementation.cs ImplementationFactory.cs SystemDynamicLinker.cs Log Message: Moved the ITestFramework implementation resolution policy/algorithm into the DotNetMock.TestFramework namespace. Keep it separate and testable. Index: MbUnitStubMaker.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/TestFramework/Attic/MbUnitStubMaker.cs,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -C2 -d -r1.1.2.2 -r1.1.2.3 *** MbUnitStubMaker.cs 26 Jan 2005 01:21:36 -0000 1.1.2.2 --- MbUnitStubMaker.cs 29 Jan 2005 08:49:13 -0000 1.1.2.3 *************** *** 20,25 **** /// </summary> /// <param name="providerAssembly">MbUnit.Core assembly</param> ! public MbUnitStubMaker( Assembly providerAssembly ) ! : base(providerAssembly, "MbUnit.Core.Framework.Assert") { } --- 20,29 ---- /// </summary> /// <param name="providerAssembly">MbUnit.Core assembly</param> ! /// <param name="linker">reflection linkage provider</param> ! public MbUnitStubMaker( ! Assembly providerAssembly, ! IDynamicLinker linker ! ) ! : base("MbUnit.Core.Framework.Assert", providerAssembly, linker) { } --- NEW FILE: SystemDynamicLinker.cs --- #region License // Copyright (c) 2004 Choy Rim. All rights reserved. #endregion #region Imports using System; using System.Collections; using System.IO; using System.Reflection; using DotNetMock.Core; #endregion namespace DotNetMock.TestFramework { /// <summary> /// Summary description for SystemDynamicLinker. /// </summary> public class SystemDynamicLinker : IDynamicLinker { #region IDynamicLinker Members /// <summary> /// Try to load an <see cref="Assembly"/> by name. /// </summary> /// <param name="name">simple/partial name of desired /// <see cref="Assembly"/></param> /// <returns>desired <see cref="Assembly"/> or null if /// cannot be found</returns> public Assembly LoadAssembly(string name) { try { return Assembly.Load(name); } catch (FileNotFoundException) { return null; } } /// <summary> /// Try to load an <see cref="Assembly"/> by partial name. /// </summary> /// <param name="name">simple/partial name of desired /// <see cref="Assembly"/></param> /// <returns>desired <see cref="Assembly"/> or null if /// cannot be found</returns> public Assembly LoadAssemblyWithPartialName(string name) { try { return Assembly.LoadWithPartialName(name); } catch (FileNotFoundException) { return null; } } /// <summary> /// Get type by name from an assembly. /// </summary> /// <param name="typeName">full name of type</param> /// <param name="assembly">assembly to get it from</param> /// <returns></returns> public Type GetType(string typeName, Assembly assembly) { return assembly.GetType(typeName); } /// <summary> /// Create instance of type. /// </summary> /// <param name="type">desired <see cref="Type"/></param> /// <returns>new instance</returns> public object CreateInstance(Type type) { return Activator.CreateInstance(type); } #endregion } } Index: AbstractStubMaker.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/TestFramework/Attic/AbstractStubMaker.cs,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -C2 -d -r1.1.2.1 -r1.1.2.2 *** AbstractStubMaker.cs 25 Jan 2005 06:20:50 -0000 1.1.2.1 --- AbstractStubMaker.cs 29 Jan 2005 08:49:13 -0000 1.1.2.2 *************** *** 38,50 **** /// Initialize a stub maker. /// </summary> /// <param name="providerAssembly">Assembly that provides /// assertion class</param> ! /// <param name="_providerClassName">name of assertion ! /// class</param> public AbstractStubMaker( Assembly providerAssembly, ! string _providerClassName ) : ! this(providerAssembly.GetType(_providerClassName, true)) { } --- 38,52 ---- /// Initialize a stub maker. /// </summary> + /// <param name="providerClassName">name of assertion + /// class</param> /// <param name="providerAssembly">Assembly that provides /// assertion class</param> ! /// <param name="linker">reflection linkage provider</param> public AbstractStubMaker( + string providerClassName, Assembly providerAssembly, ! IDynamicLinker linker ) : ! this(linker.GetType(providerClassName, providerAssembly)) { } Index: csUnitStubMaker.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/TestFramework/Attic/csUnitStubMaker.cs,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -C2 -d -r1.1.2.1 -r1.1.2.2 *** csUnitStubMaker.cs 26 Jan 2005 01:24:58 -0000 1.1.2.1 --- csUnitStubMaker.cs 29 Jan 2005 08:49:13 -0000 1.1.2.2 *************** *** 20,25 **** /// </summary> /// <param name="providerAssembly">csUnit assembly</param> ! public csUnitStubMaker( Assembly providerAssembly ) ! : base(providerAssembly, "csUnit.Assert") { } --- 20,29 ---- /// </summary> /// <param name="providerAssembly">csUnit assembly</param> ! /// <param name="linker">reflection linkage provider</param> ! public csUnitStubMaker( ! Assembly providerAssembly, ! IDynamicLinker linker ! ) ! : base("csUnit.Assert", providerAssembly, linker) { } --- NEW FILE: Implementation.cs --- #region License // Copyright (c) 2004 Choy Rim. All rights reserved. #endregion #region Imports using System; using System.Collections; using DotNetMock.Core; #endregion namespace DotNetMock.TestFramework { /// <summary> /// Responsible for obtaining an implementation /// of <see cref="ITestFramework"/> for DotNetMock to use. /// </summary> public class Implementation { private static ITestFramework _instance = null; private static object _lock = new object(); /// <summary> /// Global instance of <see cref="ITestFramework"/> /// implementation used by the rest of DotNetMock. /// </summary> public static ITestFramework Instance { get { // the synchronization here may be overkill // but just in case ... lock ( _lock ) { if ( _instance==null ) { IDictionary env = Environment.GetEnvironmentVariables(); IDynamicLinker linker = new SystemDynamicLinker(); ImplementationFactory factory = new ImplementationFactory(env, linker); _instance = factory.NewImplementation(); } return _instance; } } } } } Index: NUnitStubMaker.cs =================================================================== RCS file: /cvsroot/dotnetmock/dotnetmock/DotNetMock/TestFramework/Attic/NUnitStubMaker.cs,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -C2 -d -r1.1.2.1 -r1.1.2.2 *** NUnitStubMaker.cs 25 Jan 2005 06:20:50 -0000 1.1.2.1 --- NUnitStubMaker.cs 29 Jan 2005 08:49:13 -0000 1.1.2.2 *************** *** 20,25 **** /// </summary> /// <param name="providerAssembly">nunit.framework assembly</param> ! public NUnitStubMaker( Assembly providerAssembly ) ! : base(providerAssembly, "NUnit.Framework.Assertion") { } --- 20,29 ---- /// </summary> /// <param name="providerAssembly">nunit.framework assembly</param> ! /// <param name="linker">reflection linkage provider</param> ! public NUnitStubMaker( ! Assembly providerAssembly, ! IDynamicLinker linker ! ) ! : base("NUnit.Framework.Assertion", providerAssembly, linker) { } --- NEW FILE: IDynamicLinker.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> /// Interface for dynamically loading assemblies, accessing their /// types and creating instances of those types. /// </summary> public interface IDynamicLinker { /// <summary> /// Try to load an <see cref="Assembly"/> by simple name. /// </summary> /// <param name="name">simple name of desired /// <see cref="Assembly"/></param> /// <returns>desired <see cref="Assembly"/> or null if /// cannot be found</returns> Assembly LoadAssembly(string name); /// <summary> /// Try to load an <see cref="Assembly"/> by partial name. /// </summary> /// <param name="name">partial name of desired /// <see cref="Assembly"/></param> /// <returns>desired <see cref="Assembly"/> or null if /// cannot be found</returns> Assembly LoadAssemblyWithPartialName(string name); /// <summary> /// Get type by name from an assembly. /// </summary> /// <param name="typeName">full name of type</param> /// <param name="assembly">assembly to get it from</param> /// <returns></returns> Type GetType(string typeName, Assembly assembly); /// <summary> /// Create instance of type. /// </summary> /// <param name="type">desired <see cref="Type"/></param> /// <returns>new instance</returns> object CreateInstance(Type type); } } --- NEW FILE: 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; } } } |