示例#1
0
    public static void CustomChannel_InteractiveInitializer_Can_Be_Blocked()
    {
        string  testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage    = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // Create a mock interactive channel initializer to observe whether
        // its methods are invoked.
        bool beginInitializeCalled = false;
        bool endInitializeCalled   = false;
        MockInteractiveChannelInitializer mockInitializer = new MockInteractiveChannelInitializer();

        mockInitializer.BeginDisplayInitializationUIOverride = (chnl, callback, state) =>
        {
            beginInitializeCalled = true;
            return(mockInitializer.DefaultBeginDisplayInitializationUI(chnl, callback, state));
        };

        mockInitializer.EndDisplayInitializationUIOverride = (ar) =>
        {
            endInitializeCalled = true;
            mockInitializer.DefaultEndDisplayInitializationUI(ar);
        };

        // Create a mock endpoint behavior that adds the mock interactive initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();

        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.InteractiveChannelInitializers.Add(mockInitializer);
        };

        // Attach our mock endpoint behavior to the ServiceEndpoint
        // so that our mock channel initializer is registered.
        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // block attempts to use interactive UI
        ((IClientChannel)channel).AllowInitializationUI = false;

        // *** EXECUTE *** \\
        Assert.Throws <InvalidOperationException>(() => ((IClientChannel)channel).DisplayInitializationUI());

        // *** VALIDATE *** \\
        Assert.False(beginInitializeCalled, "BeginDisplayInitializationUI should not have been called.");
        Assert.False(endInitializeCalled, "EndDisplayInitializationUI should not have been called.");
    }
示例#2
0
    public static void CustomChannel_InteractiveInitializer_Error_Blocks_Open()
    {
        string  testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage    = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // Create a mock interactive channel initializer to throw during initialization
        InvalidOperationException         thrownException = new InvalidOperationException("ui initialization failed");
        MockInteractiveChannelInitializer mockInitializer = new MockInteractiveChannelInitializer();

        mockInitializer.EndDisplayInitializationUIOverride = (ar) =>
        {
            // Throw from the EndDisplayInitializationUI call to simulate a UI
            // action that throws an error to prevent the open.
            mockInitializer.DefaultEndDisplayInitializationUI(ar);
            throw thrownException;
        };

        // Create a mock endpoint behavior that adds the mock interactive initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();

        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.InteractiveChannelInitializers.Add(mockInitializer);
        };

        // Attach our mock endpoint behavior to the ServiceEndpoint
        // so that our mock channel initializer is registered.
        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        // The implicit open does the call to display interactive UI.
        InvalidOperationException caughtException =
            Assert.Throws <InvalidOperationException>(() => channel.Process(inputMessage));

        // *** VALIDATE *** \\
        Assert.True(string.Equals(thrownException.Message, caughtException.Message),
                    String.Format("Expected exception message to be '{0}' but actual was '{1}'",
                                  thrownException.Message, caughtException.Message));
    }
示例#3
0
    public static void CustomChannel_ChannelInitializer_Failed_Initialize_Throws()
    {
        string  testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage    = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // Create a mock channel initializer to fault inject an exception
        InvalidOperationException thrownException = new InvalidOperationException("test threw this");
        MockChannelInitializer    mockInitializer = new MockChannelInitializer();

        mockInitializer.InitializeOverride = (chnl) =>
        {
            mockInitializer.DefaultInitialize(chnl);
            throw thrownException;
        };

        // Add an endpoint behavior that registers the mock initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();

        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.ChannelInitializers.Add(mockInitializer);
        };

        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        // *** EXECUTE *** \\
        InvalidOperationException caughtException =
            Assert.Throws <InvalidOperationException>(() => factory.CreateChannel());

        // *** VALIDATE *** \\
        Assert.True(String.Equals(caughtException.Message, thrownException.Message),
                    String.Format("Expected exception message '{0}' but actual was '{1}'",
                                  thrownException.Message, caughtException.Message));
    }
示例#4
0
    public static void CustomChannel_ChannelInitializer_CreateChannel_Calls_Initialize()
    {
        string  testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage    = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding   binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var             factory = new ChannelFactory <ICustomChannelServiceInterface>(binding, address);

        // Create a mock channel initializer to observe whether
        // its method is invoked.
        bool initializeCalled = false;
        MockChannelInitializer mockInitializer = new MockChannelInitializer();

        mockInitializer.InitializeOverride = (chnl) =>
        {
            initializeCalled = true;
            mockInitializer.DefaultInitialize(chnl);
        };

        // Add an endpoint behavior that registers the mock initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();

        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.ChannelInitializers.Add(mockInitializer);
        };

        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        // *** EXECUTE *** \\
        factory.CreateChannel();

        // *** VALIDATE *** \\
        Assert.True(initializeCalled, "Initialize was not called.");
    }
示例#5
0
    public static void CustomChannel_InteractiveInitializer_Error_Blocks_Open()
    {
        string testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

        // Create a mock interactive channel initializer to throw during initialization
        InvalidOperationException thrownException = new InvalidOperationException("ui initialization failed");
        MockInteractiveChannelInitializer mockInitializer = new MockInteractiveChannelInitializer();
        mockInitializer.EndDisplayInitializationUIOverride = (ar) =>
        {
            // Throw from the EndDisplayInitializationUI call to simulate a UI
            // action that throws an error to prevent the open.
            mockInitializer.DefaultEndDisplayInitializationUI(ar);
            throw thrownException;
        };

        // Create a mock endpoint behavior that adds the mock interactive initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();
        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.InteractiveChannelInitializers.Add(mockInitializer);
        };

        // Attach our mock endpoint behavior to the ServiceEndpoint
        // so that our mock channel initializer is registered.
        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // *** EXECUTE *** \\
        // The implicit open does the call to display interactive UI.
        InvalidOperationException caughtException = 
            Assert.Throws<InvalidOperationException>(() => channel.Process(inputMessage));

        // *** VALIDATE *** \\
        Assert.True(string.Equals(thrownException.Message, caughtException.Message),
                    String.Format("Expected exception message to be '{0}' but actual was '{1}'",
                                  thrownException.Message, caughtException.Message));
    }
示例#6
0
    public static void CustomChannel_InteractiveInitializer_Can_Be_Blocked()
    {
        string testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

        // Create a mock interactive channel initializer to observe whether
        // its methods are invoked.
        bool beginInitializeCalled = false;
        bool endInitializeCalled = false;
        MockInteractiveChannelInitializer mockInitializer = new MockInteractiveChannelInitializer();
        mockInitializer.BeginDisplayInitializationUIOverride = (chnl, callback, state) =>
        {
            beginInitializeCalled = true;
            return mockInitializer.DefaultBeginDisplayInitializationUI(chnl, callback, state);
        };

        mockInitializer.EndDisplayInitializationUIOverride = (ar) =>
        {
            endInitializeCalled = true;
            mockInitializer.DefaultEndDisplayInitializationUI(ar);
        };

        // Create a mock endpoint behavior that adds the mock interactive initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();
        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.InteractiveChannelInitializers.Add(mockInitializer);
        };

        // Attach our mock endpoint behavior to the ServiceEndpoint
        // so that our mock channel initializer is registered.
        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        ICustomChannelServiceInterface channel = factory.CreateChannel();

        // block attempts to use interactive UI
        ((IClientChannel)channel).AllowInitializationUI = false;

        // *** EXECUTE *** \\
        Assert.Throws<InvalidOperationException>(() => ((IClientChannel)channel).DisplayInitializationUI());

        // *** VALIDATE *** \\
        Assert.False(beginInitializeCalled, "BeginDisplayInitializationUI should not have been called.");
        Assert.False(endInitializeCalled, "EndDisplayInitializationUI should not have been called.");
    }
示例#7
0
    public static void CustomChannel_ChannelInitializer_Failed_Initialize_Throws()
    {
        string testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

        // Create a mock channel initializer to fault inject an exception
        InvalidOperationException thrownException = new InvalidOperationException("test threw this");
        MockChannelInitializer mockInitializer = new MockChannelInitializer();
        mockInitializer.InitializeOverride = (chnl) =>
        {
            mockInitializer.DefaultInitialize(chnl);
            throw thrownException;
        };

        // Add an endpoint behavior that registers the mock initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();
        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.ChannelInitializers.Add(mockInitializer);
        };

        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        // *** EXECUTE *** \\
        InvalidOperationException caughtException =
            Assert.Throws<InvalidOperationException>(() => factory.CreateChannel());

        // *** VALIDATE *** \\
        Assert.True(String.Equals(caughtException.Message, thrownException.Message), 
                    String.Format("Expected exception message '{0}' but actual was '{1}'",
                                    thrownException.Message, caughtException.Message));
    }
示例#8
0
    public static void CustomChannel_ChannelInitializer_CreateChannel_Calls_Initialize()
    {
        string testMessageBody = "CustomChannelTest_Sync";
        Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

        // *** SETUP *** \\
        MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
        CustomBinding binding = new CustomBinding(mockBindingElement);
        EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
        var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

        // Create a mock channel initializer to observe whether
        // its method is invoked.
        bool initializeCalled = false;
        MockChannelInitializer mockInitializer = new MockChannelInitializer();
        mockInitializer.InitializeOverride = (chnl) =>
        {
            initializeCalled = true;
            mockInitializer.DefaultInitialize(chnl);
        };

        // Add an endpoint behavior that registers the mock initializer
        MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();
        mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
        {
            clientRuntime.ChannelInitializers.Add(mockInitializer);
        };

        factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

        // *** EXECUTE *** \\
        factory.CreateChannel();

        // *** VALIDATE *** \\
        Assert.True(initializeCalled, "Initialize was not called.");
    }