Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System.ServiceModel.Discovery / Test / System.ServiceModel.Discovery / EndpointDiscoveryBehaviorTest.cs
1 //
2 // Author: Atsushi Enomoto <atsushi@ximian.com>
3 //
4 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 // 
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 // 
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25 using System;
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
28 using System.ServiceModel;
29 using System.ServiceModel.Channels;
30 using System.ServiceModel.Description;
31 using System.ServiceModel.Discovery;
32 using System.ServiceModel.Dispatcher;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.ServiceModel.Discovery
36 {
37         [TestFixture]
38         public class EndpointDiscoveryBehaviorTest
39         {
40                 [Test]
41                 public void Use ()
42                 {
43                         // Without ServiceDiscoveryBehavior.
44                         var b = new EndpointDiscoveryBehavior ();
45                         IEndpointBehavior eb = b;
46                         var host = new ServiceHost (typeof (TestService));
47                         var se = host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), new Uri ("http://localhost:37564"));
48                         se.Behaviors.Add (b);
49
50                         var bc = new BindingParameterCollection ();
51                         eb.AddBindingParameters (se, bc);
52                         Assert.AreEqual (0, bc.Count, "#1");
53                         Assert.AreEqual (0, host.Extensions.Count, "#1-2");
54
55                         eb.Validate (se);
56                         // This behavior itself does not populate discovery extension. It just configures the extension.
57                         Assert.AreEqual (0, host.Extensions.Count, "#2-2");
58                 }
59
60                 [Test]
61                 [Category ("NotWorking")]
62                 public void Use2 ()
63                 {
64                         // This time with ServiceDiscoveryBehavior.
65                         var b = new EndpointDiscoveryBehavior ();
66                         IEndpointBehavior eb = b;
67                         var host = new ServiceHost (typeof (TestService));
68                         var se = host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), new Uri ("http://localhost:37564"));
69                         var sdb = new ServiceDiscoveryBehavior ();
70                         sdb.AnnouncementEndpoints.Add (new UdpAnnouncementEndpoint ());
71                         IServiceBehavior sb = sdb;
72                         se.Behaviors.Add (b);
73
74                         var bc = new BindingParameterCollection ();
75                         sb.AddBindingParameters (host.Description, host, host.Description.Endpoints, bc);
76                         eb.AddBindingParameters (se, bc);
77                         Assert.AreEqual (0, bc.Count, "#1");
78                         Assert.AreEqual (0, host.Extensions.Count, "#1-2");
79
80                         sb.Validate (host.Description, host);
81                         eb.Validate (se);
82                         // ... should "validate" not "apply dispatch behavior" do "add host extension" job? I doubt that.
83                         Assert.AreEqual (1, host.Extensions.Count, "#2-2");
84                         var dse = host.Extensions.Find<DiscoveryServiceExtension> ();
85
86                         Assert.IsNotNull (dse, "#2-3");
87                         Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#2-4");
88                         Assert.AreEqual (2, se.Behaviors.Count, "#2-5"); // EndpointDiscoveryBehavior + discovery initializer.
89
90                         Assert.AreEqual (0, host.ChannelDispatchers.Count, "#3-1");
91                         Assert.AreEqual (1, host.Description.Endpoints.Count, "#3-2");
92                         Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-4");
93
94                         // The IEndpointBehavior from EndpointDiscoveryBehavior, when ApplyDispatchBehavior() is invoked, publishes an endpoint.
95                         sb.ApplyDispatchBehavior (host.Description, host);
96                         Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-5"); // not yet published
97                         eb.ApplyDispatchBehavior (se, new EndpointDispatcher (new EndpointAddress ("http://localhost:37564"), "ITestService", "http://tempuri.org/"));
98                         Assert.AreEqual (2, host.ChannelDispatchers.Count, "#3-6-1"); // for online and offline announcements
99                         Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-6-2"); // still not published.
100
101                         host.Open ();
102                         try {
103                                 Assert.AreEqual (3, host.ChannelDispatchers.Count, "#4-1"); // for online and offline announcements
104                                 Assert.AreEqual (1, dse.PublishedEndpoints.Count, "#4-2"); // The endpoint is published again. (Not sure if it's worthy of testing.)
105                         } finally {
106                                 host.Close ();
107                         }
108                 }
109         }
110 }