Merge pull request #704 from jgagnon/master
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Channels / BindingContextTest.cs
1 //
2 // BindingContextTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 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.Net.Security;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.ServiceModel.Channels
37 {
38         [TestFixture]
39         public class BindingContextTest
40         {
41                 [Test]
42                 [ExpectedException (typeof (ArgumentNullException))]
43                 public void TestCtorBindingNull ()
44                 {
45                         new BindingContext (null,
46                                 new BindingParameterCollection ());
47                 }
48
49                 [Test]
50                 [ExpectedException (typeof (ArgumentNullException))]
51                 public void TestCtorParametersNull ()
52                 {
53                         new BindingContext (new CustomBinding (), null);
54                 }
55
56                 [Test]
57                 //[ExpectedException (typeof (ArgumentNullException))]
58                 public void TestCtorListenUriBaseAddressNull ()
59                 {
60                         new BindingContext (new CustomBinding (),
61                                 new BindingParameterCollection (),
62                                 null,
63                                 "http://localhost:8080",
64                                 ListenUriMode.Unique);
65                 }
66
67                 [Test]
68                 //[ExpectedException (typeof (ArgumentNullException))]
69                 public void TestCtorListenUriRelativeAddressNull ()
70                 {
71                         new BindingContext (new CustomBinding (),
72                                 new BindingParameterCollection (),
73                                 new Uri ("http://localhost:8080"),
74                                 null,
75                                 ListenUriMode.Unique);
76                 }
77
78                 [Test]
79                 // Now this test is mostly useless ...
80                 public void ConsumeBindingElements ()
81                 {
82                         BindingContext ctx = new BindingContext (
83                                 new CustomBinding (
84                                         new TextMessageEncodingBindingElement (),
85                                         new HttpTransportBindingElement ()),
86                                 new BindingParameterCollection ());
87
88                         HttpTransportBindingElement be =
89                                 new HttpTransportBindingElement ();
90                         be.BuildChannelFactory<IRequestChannel> (ctx);
91                 }
92
93                 [Test]
94                 [Category ("NotWorking")]
95                 [ExpectedException (typeof (ArgumentException))]
96                 public void DuplicateMesageEncodingBindingElementError ()
97                 {
98                         BindingContext ctx = new BindingContext (
99                                 new CustomBinding (
100                                         new TextMessageEncodingBindingElement (),
101                                         new HttpTransportBindingElement ()),
102                                 new BindingParameterCollection ());
103
104                         TextMessageEncodingBindingElement te =
105                                 new TextMessageEncodingBindingElement ();
106                         te.BuildChannelFactory<IRequestChannel> (ctx);
107                 }
108
109                 [Test]
110                 public void GetInnerPropertyIsNothingToDoWithParameters ()
111                 {
112                         BindingParameterCollection pl =
113                                 new BindingParameterCollection ();
114                         pl.Add (new ClientCredentials ());
115                         BindingContext ctx =
116                                 new BindingContext (new CustomBinding (), pl);
117                         Assert.IsNull (ctx.GetInnerProperty<ClientCredentials> ());
118
119                         CustomBinding binding = new CustomBinding (new HttpTransportBindingElement ());
120                         ctx = new BindingContext (binding, pl);
121                         Assert.IsNull (ctx.GetInnerProperty<ClientCredentials> ());
122                 }
123
124                 [Test]
125                 public void RemainingBindingElements ()
126                 {
127                         var b = new CustomBinding (
128                                 new BinaryMessageEncodingBindingElement (),
129                                 new InterceptorBindingElement2 (),
130                                 new HttpTransportBindingElement ());
131                         Assert.AreEqual (3, new BindingContext (b, new BindingParameterCollection ()).RemainingBindingElements.Count, "#1");
132                         Assert.IsTrue (b.CanBuildChannelFactory<IRequestChannel> (), "#2");
133                         
134                 }
135                 
136                 [Test]
137                 public void RemainingBindingElements2 ()
138                 {
139                         var b = new CustomBinding (
140                                 new BindingElement2 (),
141                                 new InterceptorBindingElement3 (),
142                                 new BindingElement3 ());
143
144                         Assert.AreEqual (3, new BindingContext (b, new BindingParameterCollection ()).RemainingBindingElements.Count, "New BindingContext element count");
145                         Assert.IsTrue (b.CanBuildChannelFactory<IRequestChannel> (), "supports IRequestChannel?");
146                 }
147
148                 public class InterceptorBindingElement2 : BindingElement
149                 {
150                         public override bool CanBuildChannelFactory<TChannel> (BindingContext bc)
151                         {
152                                 Assert.AreEqual (1, bc.Clone ().RemainingBindingElements.Count, "#i1");
153                                 Assert.IsNull (bc.GetInnerProperty<MessageEncodingBindingElement> (), "#i2");
154                                 Assert.IsNull (bc.GetInnerProperty<MessageEncoder> (), "#i3");
155                                 Assert.IsNull (bc.GetInnerProperty<MessageEncoderFactory> (), "#i4");
156                                 Assert.AreEqual (1, bc.RemainingBindingElements.Count, "#i5");
157                                 Assert.IsTrue (bc.RemainingBindingElements [0] is HttpTransportBindingElement, "#i6");
158                                 Assert.AreEqual (3, bc.Binding.CreateBindingElements ().Count, "#i7");
159                                 return base.CanBuildChannelFactory<TChannel> (bc);
160                         }
161
162                         public override BindingElement Clone ()
163                         {
164                                 return new InterceptorBindingElement2 ();
165                         }
166
167                         public override T GetProperty<T> (BindingContext bc)
168                         {
169                                 return null;
170                         }
171                 }
172
173                 public class InterceptorBindingElement3 : BindingElement {
174                         public override bool CanBuildChannelFactory<TChannel> (BindingContext bc) {
175                                 if (this is BindingElement2) {
176                                         Assert.AreEqual (2, bc.Clone ().RemainingBindingElements.Count, "1- Cloned context element count");
177                                         Assert.AreEqual (3, bc.Binding.CreateBindingElements ().Count, "1- Original binding element count");
178                                         Assert.IsTrue (bc.RemainingBindingElements [0] is InterceptorBindingElement3, "1- binding element 1 type");
179                                         Assert.IsTrue (bc.RemainingBindingElements [1] is BindingElement3, "1- binding element 2 type");
180                                 } else {
181                                         Assert.AreEqual (1, bc.Clone ().RemainingBindingElements.Count, "2- Cloned context element count");
182                                         Assert.AreEqual (3, bc.Binding.CreateBindingElements ().Count, "2- Original binding element count");
183                                         Assert.IsTrue (bc.RemainingBindingElements [0] is BindingElement3, "2- binding element 1 type");
184                                 }
185                                 return base.CanBuildChannelFactory<TChannel> (bc);
186                         }
187                         public override BindingElement Clone () {
188                                 return new InterceptorBindingElement3 ();
189                         }
190
191                         public override T GetProperty<T> (BindingContext bc) {
192                                 return null;
193                         }
194                 }
195
196                 public class BindingElement2 : InterceptorBindingElement3 {
197
198                         public override BindingElement Clone () {
199                                 return new BindingElement2 ();
200                         }
201                         public override bool CanBuildChannelFactory<TChannel> (BindingContext bc) {
202                                 return base.CanBuildChannelFactory<TChannel> (bc);
203                         }
204
205                 }
206
207                 public class BindingElement3 : HttpTransportBindingElement {
208
209                         public override BindingElement Clone () {
210                                 return new BindingElement3 ();
211                         }
212                         public override bool CanBuildChannelFactory<TChannel> (BindingContext bc) {
213                                 return base.CanBuildChannelFactory<TChannel> (bc);
214                         }
215
216                 }
217
218         }
219 }
220