NMockAwareImplementationResolver.cs :  » Build-Systems » CruiseControl.NET » Objection » 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 » Build Systems » CruiseControl.NET 
CruiseControl.NET » Objection » NMockAwareImplementationResolver.cs
using System;
using System.Collections;
using System.Reflection;

namespace Objection{
  public class LoadedTypeList
  {
    private readonly ArrayList namesOfCachedAssemblies = new ArrayList();
    private ArrayList types = null;

    public ArrayList GetTypes()
    {
      if (types == null)
      {
        types = GetTypeListForNewLoadedAssemblies();
      }
      return types;
    }
    
    public ArrayList GetNewTypes()
    {
      ArrayList newTypes = GetTypeListForNewLoadedAssemblies();
      types.AddRange(newTypes);
      return newTypes;
    }
    
    public ArrayList CheckedAssemblies()
    {
      return namesOfCachedAssemblies;
    }
    
    private ArrayList GetTypeListForNewLoadedAssemblies()
    {
      ArrayList newTypes = new ArrayList();
      foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
      {
        string assemblyName = assembly.GetName().Name;        
        if (!namesOfCachedAssemblies.Contains(assemblyName))
        {
          namesOfCachedAssemblies.Add(assemblyName);
          foreach (Type type in assembly.GetTypes())
          {
            newTypes.Add(type);
          }
        }
      }
      return newTypes;
    }    
  }
  
  public class NMockAwareImplementationResolver : ImplementationResolver
  {
    private bool ignoreNMockImplementations = false;
    private readonly LoadedTypeList loadedTypesList = new LoadedTypeList();

    public Type ResolveImplementation(Type baseType)
    {
      Type candidateType = FindTypeAssignableToBaseType(baseType, loadedTypesList.GetTypes());
      
      if (candidateType == null)
      {
        candidateType = FindTypeAssignableToBaseType(baseType, loadedTypesList.GetNewTypes());
      }
      
      if (candidateType == null)
      {
        ThrowExceptionForUnfoundImplementation(baseType);
      }
      return candidateType;
    }

    private void ThrowExceptionForUnfoundImplementation(Type baseType)
    {
      string message = "Unable to find implementation for " + baseType.FullName + ". Looked in assemblies: ";
      foreach (string assemblyName in loadedTypesList.CheckedAssemblies())
      {
        message += assemblyName;
        message += " ";
      }
      throw new Exception(message);
    }

    private Type FindTypeAssignableToBaseType(Type baseType, ArrayList types)
    {
      Type candidateType = null;
      foreach (Type type in types)
      {
        if (baseType.IsAssignableFrom(type) && baseType != type)
        {
          if (! IgnoreType(type))
          {
            if (candidateType != null)
            {
              throw new Exception(string.Format("Ambiguous type {0}, implemented by {1} and {2}", baseType.FullName, candidateType.FullName, type.FullName));  
            }            
            candidateType = type;
          }
        }
      }
      return candidateType;
    }

    public bool IgnoreNMockImplementations
    {
      set { ignoreNMockImplementations = value; }
    }

    private bool IgnoreType(Type typeToCheck)
    {
      if (ignoreNMockImplementations)
      {
        return IsNMockProxyType(typeToCheck);  
      }
      return false;
    }

    // ToDo - In theory we should think about Full Names here in case someone is actually using the name 'Proxy' as part of their class or Namespace name
    private bool IsNMockProxyType(Type typeToCheck)
    {
      // NMock Types are of the format 'ProxyMyOriginalType_4' where the original type was 'MyOriginalType'
      string typeNameToCheck = typeToCheck.Name;

      if (typeNameToCheck.StartsWith("Proxy"))
      {
        int indexOfLastUnderscoreInName = FindLastUnderscore(typeNameToCheck);
        if (indexOfLastUnderscoreInName > -1 && indexOfLastUnderscoreInName < (typeNameToCheck.Length - 1) )
        {
          try
          {
            int.Parse(typeNameToCheck.Substring(indexOfLastUnderscoreInName + 1));
            return true;
          }
          catch (FormatException) 
          { 
            // Not a NMock type in this case
          }
        }
      }

      return false;
    }

    private int FindLastUnderscore(string nameToCheck)
    {
      return nameToCheck.LastIndexOf('_');
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.