New tests.
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Description / WsdlExporterTest.cs
1 //
2 // WsdlExporterTest.cs
3 //
4 // Author:
5 //      Ankit Jain <JAnkit@novell.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
29 using System;
30 using System.Collections.Generic;
31 using System.Text;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
34 using NUnit.Framework;
35 using System.Runtime.Serialization;
36 using System.ServiceModel;
37 using System.Web.Services;
38
39 using WSServiceDescription = System.Web.Services.Description.ServiceDescription;
40 using WSMessage = System.Web.Services.Description.Message;
41 using WSBinding = System.Web.Services.Description.Binding;
42 using QName = System.Xml.XmlQualifiedName;
43
44 using SMMessage = System.ServiceModel.Channels.Message;
45
46 using System.Xml;
47 using System.Web.Services.Description;
48 using System.Xml.Schema;
49 using System.Xml.Serialization;
50 using System.Reflection;
51
52 namespace MonoTests.System.ServiceModel.Description
53 {
54         [TestFixture]
55         public class WsdlExporterTest
56         {
57                 [Test]
58                 [Category ("NotWorking")]
59                 public void Ctor1 ()
60                 {
61                         WsdlExporter we = new WsdlExporter ();
62
63                         Assert.IsNotNull (we.GetGeneratedMetadata ());
64                         Assert.IsNotNull (we.GeneratedWsdlDocuments, "#c1");
65                         Assert.AreEqual (0, we.GeneratedWsdlDocuments.Count, "#c2");
66
67                         Assert.IsNotNull (we.GeneratedXmlSchemas, "#c3");
68                         Assert.AreEqual (0, we.GeneratedXmlSchemas.Count, "#c4");
69                 }
70
71                 [Test]
72                 public void ExportEndpointTest ()
73                 {
74                         WsdlExporter we = new WsdlExporter ();
75
76                         ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IEchoService)));
77                         se.Binding = new BasicHttpBinding ();
78                         se.Address = new EndpointAddress ("http://localhost:8080");
79                         //TEST Invalid name: 5se.Name = "Service#1";
80                         //se.Name = "Service0";
81                         //se.ListenUri = new Uri ("http://localhost:8080/svc");
82
83                         we.ExportEndpoint (se);
84
85                         MetadataSet ms = we.GetGeneratedMetadata ();
86                         Assert.AreEqual (6, ms.MetadataSections.Count);
87                         CheckContract_IEchoService (ms, "#eet01");
88
89                         WSServiceDescription sd = GetServiceDescription (ms, "http://tempuri.org/", "ExportEndpointTest");
90                         CheckServicePort (GetService (sd, "service", "ExportEndpointTest"),
91                                 "BasicHttpBinding_IEchoService", new XmlQualifiedName ("BasicHttpBinding_IEchoService", "http://tempuri.org/"),
92                                 "http://localhost:8080/", "#eet02");
93
94                         CheckBasicHttpBinding (sd, "BasicHttpBinding_IEchoService", new XmlQualifiedName ("IEchoService", "http://myns/echo"),
95                                 "Echo", "http://myns/echo/IEchoService/Echo", true, true, "#eet03");
96                 }
97
98                 [Test]
99                 public void ExportEndpointTest2 ()
100                 {
101                         WsdlExporter we = new WsdlExporter ();
102
103                         ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IEchoService2)));
104                         se.Binding = new BasicHttpBinding ();
105                         se.Address = new EndpointAddress ("http://localhost:8080");
106                         we.ExportEndpoint (se);
107
108                         MetadataSet ms = we.GetGeneratedMetadata ();
109                         Assert.AreEqual (5, ms.MetadataSections.Count);
110
111                         WSServiceDescription sd = ms.MetadataSections [0].Metadata as WSServiceDescription;
112                         CheckContract_IEchoService2 (ms, "#eet20");
113                         CheckServicePort (GetService (GetServiceDescription (ms, "http://tempuri.org/", "#eet21"), "service", "ExportEndpointTest"),
114                                 "BasicHttpBinding_ThisIsEchoService", new XmlQualifiedName ("BasicHttpBinding_ThisIsEchoService", "http://tempuri.org/"),
115                                 "http://localhost:8080/", "#eet22");
116
117                         CheckBasicHttpBinding (sd, "BasicHttpBinding_ThisIsEchoService",
118                                 new XmlQualifiedName ("ThisIsEchoService", "http://tempuri.org/"),
119                                 "Echo", "http://tempuri.org/ThisIsEchoService/Echo", true, true, "#eet03");
120
121                         //FIXME: CheckXmlSchema
122                 }
123
124                 [Test]
125                 public void ExportEndpointTest3 ()
126                 {
127                         WsdlExporter we = new WsdlExporter ();
128                         /*ContractDescription contract =*/ ContractDescription.GetContract (typeof (IEchoService2));
129
130                         ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IEchoService2)));
131                         se.Binding = new BasicHttpBinding ();
132                         se.Address = new EndpointAddress ("http://localhost:8080");
133                         we.ExportEndpoint (se);
134
135                         se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IEchoService)));
136                         se.Binding = new BasicHttpBinding ();
137                         se.Address = new EndpointAddress ("http://somehost");
138                         we.ExportEndpoint (se);
139
140                         MetadataSet ms = we.GetGeneratedMetadata ();
141                         Assert.AreEqual (7, ms.MetadataSections.Count);
142
143                         Service svc = GetService (
144                                         GetServiceDescription (ms, "http://tempuri.org/", "ExportEndpointTest"),
145                                         "service", "ExportEndpointTest");
146
147                         CheckContract_IEchoService (ms, "#eet31");
148                         CheckServicePort (svc, "BasicHttpBinding_IEchoService",
149                                 new XmlQualifiedName ("BasicHttpBinding_IEchoService", "http://tempuri.org/"),
150                                 "http://somehost/", "#eet32");
151
152                         CheckContract_IEchoService2 (ms, "#eet33");
153                         CheckServicePort (svc, "BasicHttpBinding_ThisIsEchoService",
154                                 new XmlQualifiedName ("BasicHttpBinding_ThisIsEchoService", "http://tempuri.org/"),
155                                 "http://localhost:8080/", "#eet34");
156
157
158                         WSServiceDescription sd = ms.MetadataSections [0].Metadata as WSServiceDescription;
159                         CheckBasicHttpBinding (sd, "BasicHttpBinding_IEchoService", new XmlQualifiedName ("IEchoService", "http://myns/echo"),
160                                 "Echo", "http://myns/echo/IEchoService/Echo", true, true, "#eet35");
161
162                         CheckBasicHttpBinding (sd, "BasicHttpBinding_ThisIsEchoService", new XmlQualifiedName ("ThisIsEchoService", "http://tempuri.org/"),
163                                 "Echo", "http://tempuri.org/ThisIsEchoService/Echo", true, true, "#eet36");
164
165
166                         //FIXME: CheckXmlSchema
167                 }
168
169                 [Test]
170                 public void ExportContractInvalid1 ()
171                 {
172                         WsdlExporter we = new WsdlExporter ();
173
174                         we.ExportContract (ContractDescription.GetContract (typeof (IEchoService2)));
175                         //Duplicate contract QNames not allowed
176                         ExportContractExpectException (we, ContractDescription.GetContract (typeof (IEchoService2)),
177                                 typeof (ArgumentException), "ExportContractInvalid1");
178                 }
179
180                 [Test]
181                 public void ExportContractInvalid2 ()
182                 {
183                         WsdlExporter we = new WsdlExporter ();
184
185                         we.ExportContract (ContractDescription.GetContract (typeof (IEchoService2)));
186                         //Invalid as IEchoService3.Echo is http://tempuri.org/Echo message which has already been exported
187                         //Even though, the service name is different
188                         ExportContractExpectException (we, ContractDescription.GetContract (typeof (IEchoService3)),
189                                 typeof (InvalidOperationException), "ExportContractInvalid2");
190                 }
191
192                 [Test]
193                 public void ExportContract1 ()
194                 {
195                         WsdlExporter we = new WsdlExporter ();
196                         we.ExportContract (ContractDescription.GetContract (typeof (IEchoService)));
197
198                         MetadataSet ms = we.GetGeneratedMetadata ();
199                         Assert.AreEqual (5, ms.MetadataSections.Count);
200
201                         CheckContract_IEchoService (ms, "ExportContract1");
202                 }
203
204                 [Test]
205                 public void ExportContract2 ()
206                 {
207                         WsdlExporter we = new WsdlExporter ();
208                         we.ExportContract (ContractDescription.GetContract (typeof (IFoo1)));
209
210                         MetadataSet ms = we.GetGeneratedMetadata ();
211                         Assert.AreEqual (5, ms.MetadataSections.Count);
212                 }
213
214                 [Test]
215                 public void ExportContract2a ()
216                 {
217                         WsdlExporter we = new WsdlExporter ();
218                         we.ExportContract (ContractDescription.GetContract (typeof (IFoo1)));
219                         //IFoo1a.Op1 is the same operations as IFoo1.Op1, so cant be exported
220                         //the message element for both is the same
221                         //(Compared by names not signature)
222                         ExportContractExpectException (we, ContractDescription.GetContract (typeof (IFoo1a)),
223                                 typeof (InvalidOperationException), "ExportContract2a");
224                 }
225
226                 [Test]
227                 public void ExportMessageContract ()
228                 {
229                         WsdlExporter we = new WsdlExporter ();
230                         ContractDescription cd = ContractDescription.GetContract (typeof (IFoo2));
231                         we.ExportContract (cd);
232                 }
233
234                 [Test]
235                 [Category ("NotWorking")]
236                 //FIXME: One check not working, BeginGetResult
237                 public void ExportMexContract ()
238                 {
239                         WsdlExporter we = new WsdlExporter ();
240                         ContractDescription cd = ContractDescription.GetContract (typeof (IMetadataExchange));
241                         we.ExportContract (cd);
242
243                         MetadataSet ms = we.GetGeneratedMetadata ();
244
245                         WSServiceDescription sd = GetServiceDescription (ms, "http://schemas.microsoft.com/2006/04/mex", "ExportMexContract");
246
247                         CheckMessage (sd, "IMetadataExchange_Get_InputMessage", "request", "http://schemas.microsoft.com/Message:MessageBody", true, "#exc0");
248                         CheckMessage (sd, "IMetadataExchange_Get_OutputMessage", "GetResult", "http://schemas.microsoft.com/Message:MessageBody", true, "#exc1");
249
250                         //PortType
251                         PortType port_type = sd.PortTypes ["IMetadataExchange"];
252                         Assert.IsNotNull (port_type, "#exc2, PortType named IMetadataExchange not found.");
253
254                         Assert.AreEqual (1, port_type.Operations.Count, "#exc3");
255                         Operation op = port_type.Operations [0];
256                         Assert.AreEqual ("Get", op.Name, "#exc4");
257
258                         Assert.AreEqual (2, op.Messages.Count, "#exc5");
259                         CheckOperationMessage (op.Messages [0], "http://schemas.microsoft.com/2006/04/mex:IMetadataExchange_Get_InputMessage", 
260                                 typeof (OperationInput), "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
261                         
262                         CheckOperationMessage (op.Messages [1], "http://schemas.microsoft.com/2006/04/mex:IMetadataExchange_Get_OutputMessage", 
263                                 typeof (OperationOutput), "http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse");
264
265                         CheckSpecialMessage (ms, "#exc6");
266
267                         Assert.AreEqual (1, we.GeneratedWsdlDocuments.Count, "GeneratedWsdlDocuments.Count");
268                         Assert.AreEqual (1, we.GeneratedXmlSchemas.Count, "GeneratedXmlSchemas.Count");
269                 }
270
271                 [Test]
272                 public void ExportBar1Contract ()
273                 {
274                         WsdlExporter we = new WsdlExporter ();
275                         ContractDescription cd = ContractDescription.GetContract (typeof (Bar1));
276                         we.ExportContract (cd);
277                 }
278
279                 //Helper methods
280
281                 //Checks the ComplexType emitted for CLR type Message
282                 void CheckSpecialMessage (MetadataSet ms, string label)
283                 {
284                         //Check ComplexType MessageBody
285                         XmlSchema xs = GetXmlSchema (ms, "http://schemas.microsoft.com/Message", label + " #csm0");
286                         foreach (XmlSchemaObject o in xs.SchemaTypes.Values) {
287                                 XmlSchemaComplexType complex_type = o as XmlSchemaComplexType;
288                                 if (complex_type == null)
289                                         continue;
290                                 if (complex_type.Name != "MessageBody")
291                                         continue;
292
293                                 //MessageBody
294                                 Assert.IsNotNull (complex_type.Particle, label + " #cms1");
295                                 Assert.AreEqual (typeof (XmlSchemaSequence), complex_type.Particle.GetType (), label + " #cms2");
296                                 XmlSchemaSequence seq = (XmlSchemaSequence) complex_type.Particle;
297                         
298                                 Assert.AreEqual (1, seq.Items.Count, label + " #cms3");
299                                 Assert.AreEqual (typeof (XmlSchemaAny), seq.Items [0].GetType (), label + " #cms4");
300                                 XmlSchemaAny any = (XmlSchemaAny) seq.Items [0];
301
302                                 Assert.AreEqual ("##any", any.Namespace, label + " #cms5");
303                                 Assert.AreEqual (0, any.MinOccurs, label + " #cms6");
304                                 Assert.AreEqual ("unbounded", any.MaxOccursString, label + " #cms6");
305                         }
306                 }
307
308                 //somebody fix this name!
309                 void ExportContractExpectException (WsdlExporter we, ContractDescription cd, Type exception_type, string msg)
310                 {
311                         try {
312                                 we.ExportContract (cd);
313                         } catch (Exception e) {
314                                 if (e.GetType () == exception_type)
315                                         return;
316                                 Assert.Fail (String.Format ("[{0}] Expected {1}, but got : {2}", msg, exception_type, e));
317                         }
318
319                         Assert.Fail (String.Format ("[{0}] Expected {1}", msg, exception_type));
320                 }
321
322                 WSServiceDescription GetServiceDescription (MetadataSet ms, string ns, string msg)
323                 {
324                         foreach (MetadataSection section in ms.MetadataSections) {
325                                 WSServiceDescription sd = section.Metadata as WSServiceDescription;
326                                 if (sd == null)
327                                         continue;
328                                 if (sd.TargetNamespace == ns) {
329                                         /*Assert.AreEqual ("http://schemas.xmlsoap.org/wsdl/", section.Dialect, msg + " Dialect");
330                                         Assert.AreEqual (id, section.Identifier, msg + "Identifier");
331                                         Assert.AreEqual (0, section.Attributes.Count, "#cw4");*/
332
333                                         return sd;
334                                 }
335                         }
336
337                         Assert.Fail (String.Format ("[{0}] ServiceDescription for ns : {1} not found.", msg, ns));
338                         return null;
339                 }
340
341                 XmlSchema GetXmlSchema (MetadataSet ms, string ns, string msg)
342                 {
343                         foreach (MetadataSection section in ms.MetadataSections) {
344                                 XmlSchema xs = section.Metadata as XmlSchema;
345                                 if (xs == null)
346                                         continue;
347                                 if (xs.TargetNamespace == ns) {
348                                         /*Assert.AreEqual ("http://schemas.xmlsoap.org/wxsl/", section.Dialect, msg + " Dialect");
349                                         Assert.AreEqual (id, section.Identifier, msg + "Identifier");
350                                         Assert.AreEqual (0, section.Attributes.Count, "#cw4");*/
351
352                                         return xs;
353                                 }
354                         }
355
356                         Assert.Fail (String.Format ("[{0}] XmlSchema for tns : {1} not found.", msg, ns));
357                         return null;
358                 }
359                 Service GetService (WSServiceDescription sd, string name, string label)
360                 {
361                         Service ret = sd.Services [name];
362                         if (ret == null)
363                                 Assert.Fail (String.Format ("[{0}] Service named '{1}' not found.", label, name));
364                         return ret;
365                 }
366
367                 WSBinding GetBinding (WSServiceDescription sd, string name, string label)
368                 {
369                         WSBinding ret = sd.Bindings [name];
370                         if (ret == null)
371                                 Assert.Fail (String.Format ("[{0}] Binding named '{1}' not found.", label, name));
372                         return ret;
373                 }
374
375                 OperationBinding GetOperationBinding (WSBinding b, string name, string label)
376                 {
377                         foreach (OperationBinding op in b.Operations)
378                                 if (op.Name == name)
379                                         return op;
380
381                         Assert.Fail (String.Format ("[{0}] OperationBinding named '{1}' not found.", label, name));
382                         return null;
383                 }
384
385                 void CheckBasicHttpBinding (WSServiceDescription wsd, string binding_name, XmlQualifiedName binding_type,
386                         string operation_name, string action, bool has_input, bool has_output, string label)
387                 {
388                         WSBinding b = GetBinding (wsd, binding_name, label);
389                         OperationBinding op = GetOperationBinding (b, operation_name, label + " CheckBasicHttpBinding");
390
391                         Assert.AreEqual (binding_type, b.Type, label + " #cbh0");
392
393                         if (has_input) {
394                                 InputBinding inb = op.Input;
395                                 Assert.IsNotNull (inb, label + " #cbh1");
396                                 Assert.AreEqual (1, inb.Extensions.Count, label + " #cbh2");
397
398                                 Assert.AreEqual (typeof (SoapBodyBinding), inb.Extensions [0].GetType (), label + " #cbh3");
399                                 SoapBodyBinding soap_binding = (SoapBodyBinding) inb.Extensions [0];
400                                 Assert.AreEqual (SoapBindingUse.Literal, soap_binding.Use, label + " #cbh4");
401
402                                 if (action != null) {
403                                         Assert.AreEqual (1, op.Extensions.Count, label + " #chb5");
404                                         Assert.AreEqual (typeof (SoapOperationBinding), op.Extensions [0].GetType (), label + " #cbh6");
405                                         SoapOperationBinding sopb = (SoapOperationBinding) op.Extensions [0];
406                                         Assert.AreEqual (action, sopb.SoapAction, label + " #cbh7");
407                                 }
408                         }
409
410                         if (has_output) {
411                                 OutputBinding outb = op.Output;
412                                 Assert.IsNotNull (outb, label + " #cbh10");
413                                 Assert.AreEqual (1, outb.Extensions.Count, label + " #cbh11");
414
415                                 Assert.AreEqual (typeof (SoapBodyBinding), outb.Extensions [0].GetType (), label + " #cbh12");
416                                 SoapBodyBinding soap_binding = (SoapBodyBinding) outb.Extensions [0];
417                                 Assert.AreEqual (SoapBindingUse.Literal, soap_binding.Use, label + " #cbh13");
418                         }
419
420                         Assert.AreEqual (1, b.Extensions.Count, label + " #cbh20");
421                         Assert.AreEqual (typeof (SoapBinding), b.Extensions [0].GetType (), label + " #cbh21");
422                         SoapBinding sb = (SoapBinding) b.Extensions [0];
423                         Assert.AreEqual (SoapBinding.HttpTransport, sb.Transport, label + " #cbh22");
424                 }
425
426                 void CheckServicePort (Service svc, string port_name, XmlQualifiedName binding_name, string address, string label)
427                 {
428                         Port port = svc.Ports [port_name];
429                         Assert.IsNotNull (port, label + " #csp0");
430                         Assert.AreEqual (port.Binding, binding_name, label + " #csp1");
431
432                         Assert.AreEqual (1, port.Extensions.Count, label + " #csp2");
433                         Assert.AreEqual (typeof (SoapAddressBinding), port.Extensions [0].GetType (), label + " #csp3");
434                         SoapAddressBinding sab = (SoapAddressBinding) port.Extensions [0];
435                         Assert.AreEqual (address, sab.Location, label + " #csp3");
436                 }
437
438                 void CheckContract_IEchoService2 (MetadataSet ms, string label)
439                 {
440                         WSServiceDescription wsd = GetServiceDescription (ms, "http://tempuri.org/", label + "#a1");
441                         Assert.AreEqual (3, wsd.Messages.Count, "#cw5");
442
443                         Assert.IsNotNull (wsd.Messages [0]);
444                         // WSMessage m = wsd.Messages [0];
445                         CheckMessage (wsd, "ThisIsEchoService_Echo_InputMessage", "http://tempuri.org/:Echo");
446                         CheckMessage (wsd, "ThisIsEchoService_Echo_OutputMessage", "http://tempuri.org/:EchoResponse");
447
448                         CheckMessage (wsd, "ThisIsEchoService_DoubleIt_InputMessage", "http://tempuri.org/:DoubleIt");
449
450                         //PortTypes
451                         Assert.AreEqual (1, wsd.PortTypes.Count, "#cw6");
452                         PortType port = wsd.PortTypes [0];
453                         Assert.AreEqual ("ThisIsEchoService", port.Name, "#cw7");
454
455                         //Operations
456                         Assert.AreEqual (2, port.Operations.Count, "#cw8");
457                         //Operations [0]
458                         Operation op = port.Operations [0];
459                         Assert.AreEqual ("Echo", op.Name, "#co1");
460                         Assert.AreEqual (0, op.Extensions.Count, "#co2");
461                         Assert.IsNull (op.ParameterOrder, "#co3");
462                         Assert.AreEqual ("", op.ParameterOrderString, "#co4");
463                         Assert.AreEqual (0, op.Faults.Count, "#co5");
464
465                         //OperationMessages
466                         Assert.AreEqual (2, op.Messages.Count, "#co6");
467                         Assert.AreEqual (OperationFlow.RequestResponse, op.Messages.Flow, "#co7");
468
469                         CheckOperationMessage (op.Messages [0], "http://tempuri.org/:ThisIsEchoService_Echo_InputMessage",
470                                 typeof (OperationInput), "http://tempuri.org/ThisIsEchoService/Echo");
471                         CheckOperationMessage (op.Messages [1], "http://tempuri.org/:ThisIsEchoService_Echo_OutputMessage",
472                                 typeof (OperationOutput), "http://tempuri.org/ThisIsEchoService/EchoResponse");
473
474                         op = port.Operations [1];
475                         Assert.AreEqual ("DoubleIt", op.Name, "#co8");
476                         Assert.AreEqual (0, op.Extensions.Count, "#co9");
477                         Assert.IsNull (op.ParameterOrder, "#co10");
478                         Assert.AreEqual ("", op.ParameterOrderString, "#co11");
479                         Assert.AreEqual (0, op.Faults.Count, "#co12");
480
481                         //OperationMessages
482                         Assert.AreEqual (1, op.Messages.Count, "#co13");
483                         Assert.AreEqual (OperationFlow.OneWay, op.Messages.Flow, "#co14");
484
485                         CheckOperationMessage (op.Messages [0], "http://tempuri.org/:ThisIsEchoService_DoubleIt_InputMessage",
486                                 typeof (OperationInput), "http://tempuri.org/ThisIsEchoService/DoubleIt");
487
488                         /* FIXME: Assert.AreEqual (1, wsd.Types.Schemas.Count, "#co20");
489                         XmlSchema xs = wsd.Types.Schemas [0];
490
491                         Assert.AreEqual (4, xs.Includes.Count);
492                         //FIXME: Check the imports.. */
493                 }
494
495                 void CheckContract_IEchoService (MetadataSet ms, string label)
496                 {
497                         WSServiceDescription wsd = GetServiceDescription (ms, "http://myns/echo", label + "#a0");
498                         Assert.AreEqual (4, wsd.Messages.Count, "#cw5");
499
500                         Assert.IsNotNull (wsd.Messages [0]);
501                         //WSMessage m = wsd.Messages [0];
502
503                         CheckMessage (wsd, "IEchoService_Echo_InputMessage", "http://myns/echo:Echo");
504                         CheckMessage (wsd, "IEchoService_Echo_OutputMessage", "http://myns/echo:EchoResponse");
505
506                         CheckMessage (wsd, "IEchoService_DoubleIt_InputMessage", "http://myns/echo:DoubleIt");
507                         CheckMessage (wsd, "IEchoService_DoubleIt_OutputMessage", "http://myns/echo:DoubleItResponse");
508
509                         //PortTypes
510                         Assert.AreEqual (1, wsd.PortTypes.Count, "#cw6");
511                         PortType port = wsd.PortTypes [0];
512                         Assert.AreEqual ("IEchoService", port.Name, "#cw7");
513
514                         //Operations
515                         Assert.AreEqual (2, port.Operations.Count, "#cw8");
516                         //Operations [0]
517                         Operation op = port.Operations [0];
518                         Assert.AreEqual ("Echo", op.Name, "#co1");
519                         Assert.AreEqual (0, op.Extensions.Count, "#co2");
520                         Assert.IsNull (op.ParameterOrder, "#co3");
521                         Assert.AreEqual ("", op.ParameterOrderString, "#co4");
522                         Assert.AreEqual (0, op.Faults.Count, "#co5");
523
524                         //OperationMessages
525                         Assert.AreEqual (2, op.Messages.Count, "#co6");
526                         Assert.AreEqual (OperationFlow.RequestResponse, op.Messages.Flow, "#co7");
527
528                         CheckOperationMessage (op.Messages [0], "http://myns/echo:IEchoService_Echo_InputMessage", typeof (OperationInput), "http://myns/echo/IEchoService/Echo");
529                         CheckOperationMessage (op.Messages [1], "http://myns/echo:IEchoService_Echo_OutputMessage", typeof (OperationOutput), "http://myns/echo/IEchoService/EchoResponse");
530
531                         op = port.Operations [1];
532                         Assert.AreEqual ("DoubleIt", op.Name, "#co8");
533                         Assert.AreEqual (0, op.Extensions.Count, "#co9");
534                         Assert.IsNull (op.ParameterOrder, "#co10");
535                         Assert.AreEqual ("", op.ParameterOrderString, "#co11");
536                         Assert.AreEqual (0, op.Faults.Count, "#co12");
537
538                         //OperationMessages
539                         Assert.AreEqual (2, op.Messages.Count, "#co13");
540                         Assert.AreEqual (OperationFlow.RequestResponse, op.Messages.Flow, "#co14");
541
542                         CheckOperationMessage (op.Messages [0], "http://myns/echo:IEchoService_DoubleIt_InputMessage", typeof (OperationInput), "http://myns/echo/IEchoService/DoubleIt");
543                         CheckOperationMessage (op.Messages [1], "http://myns/echo:IEchoService_DoubleIt_OutputMessage", typeof (OperationOutput), "http://myns/echo/IEchoService/DoubleItResponse");
544
545                         /* FIXME: Assert.AreEqual (1, wsd.Types.Schemas.Count, "#co20");
546                         XmlSchema xs = wsd.Types.Schemas [0];
547
548                         Assert.AreEqual (4, xs.Includes.Count);
549                         //FIXME: Check the imports.. */
550                 }
551
552                 void CheckOperationMessage (OperationMessage opmsg, string msg, Type type, string action)
553                 {
554                         Assert.AreEqual (type, opmsg.GetType (), "#com1");
555                         Assert.AreEqual (msg, opmsg.Message.ToString (), "#com2");
556                         Assert.AreEqual (0, opmsg.Extensions.Count, "#com3");
557                         Assert.AreEqual (1, opmsg.ExtensibleAttributes.Length, "#com4");
558                         Assert.IsNull (opmsg.Name, "#com5");
559
560                         XmlAttribute attr = opmsg.ExtensibleAttributes [0];
561                         Assert.AreEqual ("Action", attr.LocalName, "#ca1");
562                         Assert.AreEqual ("http://www.w3.org/2006/05/addressing/wsdl", attr.NamespaceURI, "#ca2");
563                         Assert.AreEqual (action, attr.Value, "#ca3");
564                 }
565
566                 void CheckMessage (WSServiceDescription sd, string msg_name, string part_type)
567                 {
568                         CheckMessage (sd, msg_name, "parameters", part_type, false, "");
569                 }
570
571                 void CheckMessage (WSServiceDescription sd, string msg_name, string part_name, string part_type, bool is_type, string label)
572                 {
573                         WSMessage m = sd.Messages [msg_name];
574                         Assert.IsNotNull (m, label + " : Message named " + msg_name + " not found.");
575
576                         Assert.AreEqual (msg_name, m.Name, label + " #cm1");
577                         Assert.AreEqual (0, m.Extensions.Count, label + " #cm2");
578
579                         Assert.IsNull (m.ExtensibleAttributes, label + " #cm3a");
580                         Assert.AreEqual (1, m.Parts.Count, label + " #cm3");
581
582                         Assert.AreEqual (part_name, m.Parts [0].Name, label + " #cm9");
583
584                         if (is_type) {
585                                 Assert.AreEqual ("", m.Parts [0].Element.ToString (), label + " #cm4");
586                                 Assert.AreEqual (part_type, m.Parts [0].Type.ToString (), label + " #cm4a");
587                         } else {
588                                 Assert.AreEqual ("", m.Parts [0].Type.ToString (), label + " #cm5");
589                                 Assert.AreEqual (part_type, m.Parts [0].Element.ToString (), label + " #cm5a");
590                         }
591
592                         Assert.IsNull (m.Parts [0].ExtensibleAttributes, label + " #cm6");
593                         Assert.AreEqual (0, m.Parts [0].Extensions.Count, label + " #cm7");
594                 }
595
596                 void WriteTo (WsdlExporter we, string name)
597                 {
598                         using (XmlTextWriter xw = new XmlTextWriter (name, Encoding.UTF8)) {
599                                 xw.Formatting = Formatting.Indented;
600                                 we.GetGeneratedMetadata ().WriteTo (xw);
601                         }
602                 }
603
604                 [Test]
605                 public void ExportEndpointTest5 () {
606                         WsdlExporter we = new WsdlExporter ();
607
608                         ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IEchoService)));
609                         se.Binding = new BasicHttpBinding ();
610                         se.Address = new EndpointAddress ("http://localhost:8080");
611
612                         we.ExportEndpoint (se);
613
614                         MetadataSet ms = we.GetGeneratedMetadata ();
615                         Assert.AreEqual (6, ms.MetadataSections.Count);
616                         WSServiceDescription wsd = GetServiceDescription (ms, "http://tempuri.org/", "ExportEndpointTest5#1");
617
618                         SoapBinding soapBinding = (SoapBinding) wsd.Bindings [0].Extensions [0];
619                         Assert.AreEqual (SoapBindingStyle.Document, soapBinding.Style, "soapBinding.Style");
620                         Assert.AreEqual (SoapBinding.HttpTransport, soapBinding.Transport, "soapBinding.Transport");
621                 }
622
623         }
624
625         [DataContract]
626         public class dc
627         {
628                 [DataMember]
629                 string foo;
630
631                 /*  [DataMember]
632                         dc me;
633
634                         [DataMember]
635                         some_enum en;
636
637                         [DataMember]
638                         NotReferenced nr;*/
639
640                 [DataMember]
641                 public FooNS.bar bb;
642         }
643
644         [ServiceContract (Namespace = "http://myns/echo")]
645         public interface IEchoService
646         {
647
648                 [OperationContract]
649                 string Echo (string msg, int num, dc d);
650
651                 [OperationContract]
652                 void DoubleIt (int it, string prefix);
653
654                 /*[OperationContract]
655                 void foo ();*/
656
657         }
658
659         [ServiceContract (Name = "ThisIsEchoService")]
660         public interface IEchoService2
661         {
662
663                 [OperationContract]
664                 string Echo (string msg, int num, dc d);
665
666                 [OperationContract (IsOneWay = true)]
667                 void DoubleIt (int it, string prefix);
668
669                 /*[OperationContract]
670                 void foo ();*/
671         }
672
673         [ServiceContract (Name = "AnotherService")]
674         public interface IEchoService3
675         {
676                 [OperationContract]
677                 string Echo (string msg, int num, dc d);
678
679                 [OperationContract (IsOneWay = true)]
680                 void DoubleIt (int it, string prefix);
681
682                 /*[OperationContract]
683                 void foo ();*/
684         }
685
686         [ServiceContract]
687         public class Bar1
688         {
689                 [OperationContract]
690                 public void Foo (SMMessage msg, string s)
691                 {
692                 }
693         }
694
695         [ServiceContract]
696         public interface IFoo1
697         {
698                 //Same DataContract used in different operations
699
700                 [OperationContract]
701                 void Op1 (dc d); 
702
703                 [OperationContract]
704                 void Op2 (dc d); 
705         }
706
707         //Used to check whether both IFoo1 & IFoo1a
708         //can be exported
709         //Operations are not Service scoped
710         [ServiceContract]
711         public interface IFoo1a
712         {
713                 [OperationContract]
714                 void Op1 (string s);
715         }
716
717         [ServiceContract]
718         public interface IFoo2
719         {
720                 // FIXME: it does not pass yet
721                 [OperationContract]
722                 OregoMessage Nanoda (OregoMessage msg);
723
724                 // FIXME: it does not pass yet
725                 [OperationContract]
726                 Mona NewMona (Mona source);
727         }
728
729         [MessageContract]
730         public class OregoMessage
731         {
732                 [MessageBodyMember]
733                 public string Neutral;
734
735                 [MessageBodyMember]
736                 public Assembly Huh;
737
738                 [MessageBodyMember] // it should be ignored ...
739                 public string Setter { set { } }
740
741                 public string NonMember;
742         }
743
744         [DataContract]
745         public class Mona
746         {
747                 [DataMember]
748                 public string OmaeMona;
749
750                 [DataMember]
751                 public string OreMona;
752         }
753
754 }
755 namespace FooNS
756 {
757         [DataContract]
758         public class bar
759         {
760                 [DataMember]
761                 public string foo;
762         }
763 }
764
765