BaseProcessingAttribute.cs :  » Testing » XT-Unit » TeamAgile » ApplicationBlocks » Interception » 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 » XT Unit 
XT Unit » TeamAgile » ApplicationBlocks » Interception » BaseProcessingAttribute.cs
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Threading;

namespace TeamAgile.ApplicationBlocks.Interception{

  /// <summary>
  ///     This is the base class for custom attributes
  ///     that you would like to add to your tests.
  ///     just inherit from it and implement the two simple abstract methods.
  ///     then you can start using your new attribute right away in your tests.
  /// </summary>
  /// <remarks>
  ///     
  /// </remarks>
  /// 
  [Serializable]
  [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property,AllowMultiple=true,Inherited=true)]
  public abstract class BaseProcessingAttribute : Attribute
  {

    protected void OutputDebugMessage (string message)
    {
      Debug.WriteLine(message);
      //Trace.WriteLine(message);
      //Console.WriteLine(message);
    }

    [DebuggerStepThrough]
    public void PreProcess(object sender,PreProcessEventArgs args)
    {
      OnPreProcess(sender,args);
    }

    [DebuggerStepThrough]
    protected abstract void OnPreProcess(object sender, PreProcessEventArgs args);
    [DebuggerStepThrough]
    protected abstract void OnPostProcess(object sender,PostProcessEventArgs args);


    /// <summary>
    ///     Because PreProcess and PostProcess can instantiate new instances of your attribute
    ///     you cannot keep local varibales in it to share between these two actions.
    ///     use this method to save a setting that can be shared between those actions
    /// </summary>
    /// <param name="settingName" type="string">
    ///     <para>
    ///         the key name of the setting to be used later to get the value
    ///     </para>
    /// </param>
    /// <param name="settingValue" type="object">
    ///     <para>
    ///         the value to be retrieved later
    ///     </para>
    /// </param>
    /// <returns>
    ///     A void value...
    /// </returns>
    protected void SaveSettingForPostProcess (string settingName ,object settingValue)
    {
      string fullSettingName = buildFullSettingName (settingName);
      LocalDataStoreSlot slot = Thread.AllocateNamedDataSlot(fullSettingName);
      Thread.SetData(slot,settingValue);
    }

    private static string buildFullSettingName (string settingName)
    {
      return "XtUnit.AttributeSettings." + settingName;
    }

    /// <summary>
    ///     use this method to retrieve data saved earlier using saveSettingForPostProcess method.
    /// </summary>
    /// <param name="settingName" type="string">
    ///     <para>
    ///         the key name of the setting
    ///     </para>
    /// </param>
    /// <returns>
    ///     A object value...
    /// </returns>
    protected object GetSettingFromPreProcess (string settingName)
    {
      string fullSettingName = buildFullSettingName (settingName);;
      LocalDataStoreSlot slot = Thread.GetNamedDataSlot(fullSettingName);
      return Thread.GetData(slot);
    }

    [DebuggerStepThrough]
    public void PostProcess(object sender, PostProcessEventArgs args)
    {
      OnPostProcess(sender,args);
      
      //return message could have changed on the post processing
      //for example - swallowing an exception
      //returnMessage = methodReturnMessage;
    }

    /// <summary>
    ///     Executes the method that this attribute was declared on 
    ///     with the same parameter values
    /// </summary>
    /// 
    /// <returns>
    ///     A void value...
    /// </returns>
    protected void invokeDeclaringMethod (ProcessEventArgs args)
    {
      Invoker invoker =new Invoker();
      invoker.Invoke(args);
    }
    
    protected void FlagCurrentMethodToBeSkipped (ProcessEventArgs args)
    {
      IMethodCallMessage methodCallMessage = args.MethodCallMessage;

      ReturnMessage customMessage = new ReturnMessage(
        1,
        new object[]{}, 
        0, 
        methodCallMessage.LogicalCallContext, 
        methodCallMessage);
  
      methodCallMessage.LogicalCallContext.SetData("CustomReturnMessage",customMessage) ;
    }


  }

  [Serializable]
  class Invoker:MarshalByRefObject
  {

    public void Invoke (ProcessEventArgs args)
    {
      //Debug.WriteLine("** Invoke called.....");
      RemotingServices.ExecuteMessage(args.TargetObject,args.MethodCallMessage);
    }

    private Assembly domain_AssemblyResolve (object sender, ResolveEventArgs args)
    {
      string assemblyName = args.Name.Split(',')[1];
      string path = Path.GetDirectoryName( Assembly.GetExecutingAssembly ().FullName);
      string fullPath = Path.Combine(path,assemblyName);

      Debug.WriteLine("Resolving assembly name " + args.Name + " to :\n " + fullPath);

      return Assembly.LoadFrom(fullPath);
      
    }

  }

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