Merge pull request #2807 from akoeplinger/gchandle
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / ChannelFactoryTest.cs
1 //
2 // ChannelFactoryTest.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.ServiceModel;
31 using System.ServiceModel.Channels;
32 using System.ServiceModel.Description;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.ServiceModel
36 {
37         [TestFixture]
38         public class ChannelFactoryTest
39         {
40                 class SimpleChannelFactory
41                         : ChannelFactory
42                 {
43                         public SimpleChannelFactory ()
44                                 : base ()
45                         {
46                         }
47
48                         protected override TimeSpan DefaultCloseTimeout {
49                                 get { return TimeSpan.FromMinutes (1); }
50                         }
51
52                         protected override TimeSpan DefaultOpenTimeout {
53                                 get { return TimeSpan.FromMinutes (1); }
54                         }
55
56                         protected override ServiceEndpoint CreateDescription ()
57                         {
58                                 return new ServiceEndpoint (ContractDescription.GetContract (typeof(ICtorUseCase2)));
59                         }
60
61                         public void InitEndpoint (Binding b, EndpointAddress addr)
62                         {
63                                 base.InitializeEndpoint (b, addr);
64                         }
65
66                         public void InitEndpoint (string configName, EndpointAddress addr)
67                         {
68                                 base.InitializeEndpoint (configName, addr);
69                         }
70
71                         public void ApplyConfig (string configName)
72                         {
73                                 base.ApplyConfiguration (configName);
74                         }
75                 }
76
77                 [Test]
78                 public void InitializeEndpointTest1 ()
79                 {
80                         SimpleChannelFactory factory = new SimpleChannelFactory ();
81                         Assert.AreEqual (null, factory.Endpoint, "#01");
82                         Binding b = new WSHttpBinding ();
83                         factory.InitEndpoint (b, null);
84                         Assert.AreEqual (b, factory.Endpoint.Binding, "#02");
85                         Assert.AreEqual (null, factory.Endpoint.Address, "#03");
86                         Assert.AreEqual (typeof (ICtorUseCase2), factory.Endpoint.Contract.ContractType, "#04");
87                 }
88
89                 [Test]
90                 [Ignore ("fails under .NET; I never bothered to fix the test")]
91                 public void InitializeEndpointTest2 ()
92                 {
93                         SimpleChannelFactory factory = new SimpleChannelFactory ();
94                         Assert.AreEqual (null, factory.Endpoint, "#01");
95                         factory.InitEndpoint ("CtorUseCase2_1", null);
96                         Assert.AreEqual (typeof (BasicHttpBinding), factory.Endpoint.Binding.GetType (), "#02");
97                         Assert.AreEqual (new EndpointAddress ("http://test2_1"), factory.Endpoint.Address, "#03");
98                         Assert.AreEqual (typeof (ICtorUseCase2), factory.Endpoint.Contract.ContractType, "#04");
99                 }
100
101                 [Test]
102                 [Ignore ("fails under .NET; I never bothered to fix the test")]
103                 public void InitializeEndpointTest3 ()
104                 {
105                         SimpleChannelFactory factory = new SimpleChannelFactory ();
106                         Assert.AreEqual (null, factory.Endpoint, "#01");
107                         factory.InitEndpoint ("CtorUseCase2_1", null);
108                         Binding b = new WSHttpBinding ();
109                         factory.InitEndpoint (b, null);
110                         Assert.AreEqual (b, factory.Endpoint.Binding, "#02");
111                         Assert.AreEqual (null, factory.Endpoint.Address, "#03");
112                 }
113
114                 [Test]
115                 [Ignore ("fails under .NET; I never bothered to fix the test")]
116                 public void ApplyConfigurationTest1 ()
117                 {
118                         SimpleChannelFactory factory = new SimpleChannelFactory ();
119                         Binding b = new WSHttpBinding ();
120                         factory.InitEndpoint (b, null);
121                         factory.ApplyConfig ("CtorUseCase2_1");
122                         Assert.AreEqual (new EndpointAddress ("http://test2_1"), factory.Endpoint.Address, "#03");
123                         Assert.AreEqual (b, factory.Endpoint.Binding, "#02");
124                 }
125
126                 [Test]
127                 [Ignore ("fails under .NET; I never bothered to fix the test")]
128                 public void ApplyConfigurationTest2 ()
129                 {
130                         SimpleChannelFactory factory = new SimpleChannelFactory ();
131                         Binding b = new WSHttpBinding ();
132                         factory.InitEndpoint (b, new EndpointAddress ("http://test"));
133                         factory.ApplyConfig ("CtorUseCase2_2");
134                         Assert.AreEqual (new EndpointAddress ("http://test"), factory.Endpoint.Address, "#03");
135                         Assert.IsNotNull (factory.Endpoint.Behaviors.Find <CallbackDebugBehavior> (), "#04");
136                         Assert.AreEqual (true, factory.Endpoint.Behaviors.Find <CallbackDebugBehavior> ().IncludeExceptionDetailInFaults, "#04");
137                         factory.ApplyConfig ("CtorUseCase2_3");
138                         Assert.IsNotNull (factory.Endpoint.Behaviors.Find <CallbackDebugBehavior> (), "#04");
139                         Assert.AreEqual (false, factory.Endpoint.Behaviors.Find <CallbackDebugBehavior> ().IncludeExceptionDetailInFaults, "#04");
140                 }
141
142                 [Test]
143                 public void DescriptionProperties ()
144                 {
145                         Binding b = new BasicHttpBinding ();
146                         ChannelFactory<IFoo> f = new ChannelFactory<IFoo> (b);
147
148                         // FIXME: it's not working now (though this test is silly to me.)
149                         //Assert.IsNull (f.Description.ChannelType, "ChannelType");
150
151                         // FIXME: it's not working now
152                         //Assert.AreEqual (1, f.Endpoint.Behaviors.Count, "Behaviors.Count");
153                         //ClientCredentials cred = f.Endpoint.Behaviors [0] as ClientCredentials;
154                         //Assert.IsNotNull (cred, "Behaviors contains ClientCredentials");
155
156                         Assert.IsNotNull (f.Endpoint, "Endpoint");
157                         Assert.AreEqual (b, f.Endpoint.Binding, "Endpoint.Binding");
158                         Assert.IsNull (f.Endpoint.Address, "Endpoint.Address");
159                         // You can examine this silly test on .NET.
160                         // Funky, ContractDescription.GetContract(
161                         //   typeof (IRequestChannel)) also fails to raise an 
162                         // error.
163                         //Assert.AreEqual ("IRequestChannel", f.Description.Endpoint.Contract.Name, "Endpoint.Contract");
164                 }
165
166                 public class MyChannelFactory<TChannel> 
167                         : ChannelFactory<TChannel>
168                 {
169                         public MyChannelFactory (Type type)
170                                 : base (type)
171                         {
172                         }
173                 }
174
175                 [ServiceContract]
176                 public interface IFoo
177                 {
178                         [OperationContract]
179                         string Echo (string msg);
180                 }
181
182                 public class Foo : IFoo
183                 {
184                         public string Echo (string msg)
185                         {
186                                 return msg;
187                         }
188                 }
189
190                 [Test]
191                 [ExpectedException (typeof (InvalidOperationException))]
192                 public void ArgumentTypeNotInterface ()
193                 {
194                         new MyChannelFactory<IFoo> (typeof (Foo));
195                 }
196         }
197 }