2 // BindingContextTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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;
36 namespace MonoTests.System.ServiceModel.Channels
39 public class BindingContextTest
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void TestCtorBindingNull ()
45 new BindingContext (null,
46 new BindingParameterCollection ());
50 [ExpectedException (typeof (ArgumentNullException))]
51 public void TestCtorParametersNull ()
53 new BindingContext (new CustomBinding (), null);
57 //[ExpectedException (typeof (ArgumentNullException))]
58 public void TestCtorListenUriBaseAddressNull ()
60 new BindingContext (new CustomBinding (),
61 new BindingParameterCollection (),
63 "http://localhost:8080",
64 ListenUriMode.Unique);
68 //[ExpectedException (typeof (ArgumentNullException))]
69 public void TestCtorListenUriRelativeAddressNull ()
71 new BindingContext (new CustomBinding (),
72 new BindingParameterCollection (),
73 new Uri ("http://localhost:8080"),
75 ListenUriMode.Unique);
79 // Now this test is mostly useless ...
80 public void ConsumeBindingElements ()
82 BindingContext ctx = new BindingContext (
84 new TextMessageEncodingBindingElement (),
85 new HttpTransportBindingElement ()),
86 new BindingParameterCollection ());
88 HttpTransportBindingElement be =
89 new HttpTransportBindingElement ();
90 be.BuildChannelFactory<IRequestChannel> (ctx);
94 [Category ("NotWorking")]
95 [ExpectedException (typeof (ArgumentException))]
96 public void DuplicateMesageEncodingBindingElementError ()
98 BindingContext ctx = new BindingContext (
100 new TextMessageEncodingBindingElement (),
101 new HttpTransportBindingElement ()),
102 new BindingParameterCollection ());
104 TextMessageEncodingBindingElement te =
105 new TextMessageEncodingBindingElement ();
106 te.BuildChannelFactory<IRequestChannel> (ctx);
110 public void GetInnerPropertyIsNothingToDoWithParameters ()
112 BindingParameterCollection pl =
113 new BindingParameterCollection ();
114 pl.Add (new ClientCredentials ());
116 new BindingContext (new CustomBinding (), pl);
117 Assert.IsNull (ctx.GetInnerProperty<ClientCredentials> ());
119 CustomBinding binding = new CustomBinding (new HttpTransportBindingElement ());
120 ctx = new BindingContext (binding, pl);
121 Assert.IsNull (ctx.GetInnerProperty<ClientCredentials> ());
125 public void RemainingBindingElements ()
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");
137 public void RemainingBindingElements2 ()
139 var b = new CustomBinding (
140 new BindingElement2 (),
141 new InterceptorBindingElement3 (),
142 new BindingElement3 ());
144 Assert.AreEqual (3, new BindingContext (b, new BindingParameterCollection ()).RemainingBindingElements.Count, "New BindingContext element count");
145 Assert.IsTrue (b.CanBuildChannelFactory<IRequestChannel> (), "supports IRequestChannel?");
148 public class InterceptorBindingElement2 : BindingElement
150 public override bool CanBuildChannelFactory<TChannel> (BindingContext bc)
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);
162 public override BindingElement Clone ()
164 return new InterceptorBindingElement2 ();
167 public override T GetProperty<T> (BindingContext bc)
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");
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");
185 return base.CanBuildChannelFactory<TChannel> (bc);
187 public override BindingElement Clone () {
188 return new InterceptorBindingElement3 ();
191 public override T GetProperty<T> (BindingContext bc) {
196 public class BindingElement2 : InterceptorBindingElement3 {
198 public override BindingElement Clone () {
199 return new BindingElement2 ();
201 public override bool CanBuildChannelFactory<TChannel> (BindingContext bc) {
202 return base.CanBuildChannelFactory<TChannel> (bc);
207 public class BindingElement3 : HttpTransportBindingElement {
209 public override BindingElement Clone () {
210 return new BindingElement3 ();
212 public override bool CanBuildChannelFactory<TChannel> (BindingContext bc) {
213 return base.CanBuildChannelFactory<TChannel> (bc);