ExecutorCallback.cs :  » Testing » xUnit.net » Xunit » Sdk » 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 » xUnit.net 
xUnit.net » Xunit » Sdk » ExecutorCallback.cs
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.Remoting.Messaging;

namespace Xunit.Sdk{
    /// <summary>
    /// This class supports the xUnit.net infrastructure and is not intended to be used
    /// directly from your code.
    /// </summary>
    public abstract class ExecutorCallback
    {
        static Type typeICallbackEventHandler = Type.GetType("System.Web.UI.ICallbackEventHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false);

        /// <summary>
        /// This API supports the xUnit.net infrastructure and is not intended to be used
        /// directly from your code.
        /// </summary>
        public static ExecutorCallback Wrap(object handler)
        {
            if (handler == null)
                return new NullCallback();

            IMessageSink messageSink = handler as IMessageSink;
            if (messageSink != null)
                return new MessageSinkCallback(messageSink);

            if (typeICallbackEventHandler != null)
                foreach (Type iface in handler.GetType().GetInterfaces())
                    if (iface == typeICallbackEventHandler)
                        return new CallbackEventHandlerCallback(handler);

            throw new ArgumentException("Handler must implement System.Runtime.Remoting.Messaging.IMessageSink or System.Web.UI.ICallbackEventHandler.", "handler");
        }

        /// <summary>
        /// This API supports the xUnit.net infrastructure and is not intended to be used
        /// directly from your code.
        /// </summary>
        public abstract void Notify(string value);

        /// <summary>
        /// This API supports the xUnit.net infrastructure and is not intended to be used
        /// directly from your code.
        /// </summary>
        public abstract bool ShouldContinue();

        // Used when the user wants a callback through a Predicate<string>.

        class MessageSinkCallback : ExecutorCallback
        {
            bool shouldContinue = true;
            IMessageSink messageSink;

            public MessageSinkCallback(IMessageSink messageSink)
            {
                this.messageSink = messageSink;
            }

            public override void Notify(string value)
            {
                OutgoingMessage message = new OutgoingMessage(value);
                IMessage response = messageSink.SyncProcessMessage(message);
                if (response != null && response.Properties.Contains("data"))
                    shouldContinue = Convert.ToBoolean(response.Properties["data"]);
            }

            public override bool ShouldContinue()
            {
                return shouldContinue;
            }

            class OutgoingMessage : MarshalByRefObject, IMessage
            {
                Hashtable values = new Hashtable();

                public OutgoingMessage(string value)
                {
                    values["data"] = value;
                }

                IDictionary IMessage.Properties
                {
                    get { return values; }
                }
            }
        }

        // Used when the user wants a callback through the older callback
        // mechanism (System.Web.UI.ICallbackEventHandler). Since we don't want
        // an explicit reference to System.Web.dll, we do this invocation via
        // reflection, and throw when the user only has the client profile.

        class CallbackEventHandlerCallback : ExecutorCallback
        {
            object handler;
            static MethodInfo raiseCallbackEvent;
            static MethodInfo getCallbackResult;

            static CallbackEventHandlerCallback()
            {
                raiseCallbackEvent = typeICallbackEventHandler.GetMethod("RaiseCallbackEvent");
                getCallbackResult = typeICallbackEventHandler.GetMethod("GetCallbackResult");
            }

            public CallbackEventHandlerCallback(object handler)
            {
                this.handler = handler;
            }

            public override void Notify(string value)
            {
                raiseCallbackEvent.Invoke(handler, new object[] { value });
            }

            public override bool ShouldContinue()
            {
                var result = getCallbackResult.Invoke(handler, new object[0]);
                return result == null ? true : Boolean.Parse(result as String);
            }
        }

        // Used when the user does not want a callback

        class NullCallback : ExecutorCallback
        {
            public override void Notify(string value) { }

            public override bool ShouldContinue()
            {
                return true;
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.