Merge pull request #1804 from esdrubal/processmodule
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / ClientBase_InteractiveChannelInitializerTest.cs
1 //
2 // ClientBase_InteractiveChannelInitializerTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.ObjectModel;
30 using System.IO;
31 using System.Net.Sockets;
32 using System.Reflection;
33 using System.ServiceModel;
34 using System.ServiceModel.Channels;
35 using System.ServiceModel.Description;
36 using System.ServiceModel.Dispatcher;
37 using System.Xml;
38 using NUnit.Framework;
39
40 using MonoTests.Helpers;
41
42 namespace MonoTests.System.ServiceModel
43 {
44         [TestFixture]
45         // FIXME: it does not contain testcase for successful initialization yet
46         // (MyChannelInitializer implementation hangs on .NET)
47         public class ClientBase_InteractiveChannelInitializerTest
48         {
49                 [Test]
50                 [ExpectedException (typeof (InvalidOperationException))]
51                 public void NotAllowedInitializationUI ()
52                 {
53                         var f = new FooProxy (new BasicHttpBinding (), new EndpointAddress ("http://localhost:" + NetworkHelpers.FindFreePort ()));
54                         f.Endpoint.Contract.Behaviors.Add (new MyContractBehavior ());
55                         f.InnerChannel.AllowInitializationUI = false;
56                         f.DisplayInitializationUI ();
57                 }
58
59                 [Test]
60                 [ExpectedException (typeof (InvalidOperationException))]
61                 public void OpenBeforeDisplayInitializationUI ()
62                 {
63                         var f = new FooProxy (new BasicHttpBinding (), new EndpointAddress ("http://localhost:" + NetworkHelpers.FindFreePort ()));
64                         f.Endpoint.Contract.Behaviors.Add (new MyContractBehavior ());
65                         f.Open ();
66                 }
67
68                 public class MyContractBehavior2 : MyContractBehavior
69                 {
70                 }
71
72                 public class MyContractBehavior : IContractBehavior
73                 {
74                         public void AddBindingParameters (ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
75                         {
76                         }
77
78                         public void ApplyClientBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
79                         {
80                                 clientRuntime.InteractiveChannelInitializers.Add (new MyChannelInitializer ());
81                         }
82
83                         public void ApplyDispatchBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
84                         {
85                         }
86
87                         public void Validate (ContractDescription contractDescription, ServiceEndpoint endpoint)
88                         {
89                         }
90                 }
91
92                 public class MyChannelInitializer : IInteractiveChannelInitializer
93                 {
94                         delegate void DoWork ();
95                         DoWork d;
96                         static int instance;
97                         int number;
98
99                         public MyChannelInitializer ()
100                         {
101                                 number = instance++;
102                         }
103
104                         public IAsyncResult BeginDisplayInitializationUI (IClientChannel channel, AsyncCallback callback, object state)
105                         {
106                                 Console.WriteLine ("Begin");
107                                 d = new DoWork (DisplayInitializationUI);
108                                 return d.BeginInvoke (null, null);
109                         }
110
111                         public void EndDisplayInitializationUI (IAsyncResult result)
112                         {
113                                 Console.WriteLine ("End");
114                                 d.EndInvoke (result);
115                         }
116
117                         public void DisplayInitializationUI ()
118                         {
119                                 Console.WriteLine ("Core: " + number);
120                         }
121                 }
122
123                 public class FooProxy : ClientBase<IFoo>, IFoo
124                 {
125                         public FooProxy (Binding binding, EndpointAddress address)
126                                 : base (binding, address)
127                         {
128                         }
129
130                         public string Echo (string msg)
131                         {
132                                 return Channel.Echo (msg);
133                         }
134                 }
135
136                 [ServiceContract]
137                 public interface IFoo
138                 {
139                         [OperationContract]
140                         string Echo (string input);
141                 }
142
143                 public class FooService : IFoo
144                 {
145                         public string Echo (string input)
146                         {
147                                 return input;
148                         }
149                 }
150         }
151 }