2006-11-14 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web.Services / Test / System.Web.Services.Description / ServiceDescriptionTest.cs
1 //
2 // ServiceDescriptionTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //      Ankit Jain  <jankit@novell.com>
7 //
8 // Copyright (C) 2005 Novell, Inc.
9 // Copyright (C) 2006 Novell, Inc.
10 //
11
12 using NUnit.Framework;
13
14 using System;
15 using System.IO;
16 using System.Web.Services.Description;
17 using System.Xml;
18 using System.Xml.Serialization;
19 using System.Collections;
20
21 namespace MonoTests.System.Web.Services.Description
22 {
23         [TestFixture]
24         public class ServiceDescriptionTest
25         {
26                 [Test]
27                 public void SimpleWrite ()
28                 {
29                         ServiceDescription sd = new ServiceDescription ();
30                         Assert.IsNull (sd.Name);
31                         sd.Write (TextWriter.Null);
32                 }
33
34                 [Test]
35                 public void Ctor ()
36                 {
37                         ServiceDescription sd = new ServiceDescription ();
38                         Assert.IsNotNull (sd.Bindings);
39                         Assert.IsNotNull (sd.Extensions);
40                         Assert.IsNotNull (sd.Imports);
41                         Assert.IsNotNull (sd.Messages);
42                         Assert.IsNotNull (sd.PortTypes);
43                         Assert.IsNotNull (sd.Services);
44                         Assert.IsNotNull (sd.Types);
45
46                         Assert.IsNull (sd.ServiceDescriptions);
47                         Assert.IsNull (sd.TargetNamespace);
48                 }
49
50 #if NET_2_0
51                 [Test]
52                 public void Namespaces ()
53                 {
54                         FileStream fs = new FileStream ("Test/System.Web.Services.Description/test.wsdl", FileMode.Open);
55                         XmlTextReader xtr = new XmlTextReader (fs);
56
57                         ServiceDescription sd = ServiceDescription.Read (xtr);
58                         fs.Close ();
59
60                         Assert.IsNotNull (sd.Namespaces);
61                         Assert.AreEqual (8, sd.Namespaces.Count, "#n0");
62
63                         ArrayList list = new ArrayList (sd.Namespaces.ToArray ());
64                         list.Sort (new qname_comparer ());
65
66                         Assert.AreEqual (new XmlQualifiedName ("", "http://schemas.xmlsoap.org/wsdl/"), list [0]);
67                         Assert.AreEqual (new XmlQualifiedName ("http", "http://schemas.xmlsoap.org/wsdl/http/"), list [1]);
68                         Assert.AreEqual (new XmlQualifiedName ("mime", "http://schemas.xmlsoap.org/wsdl/mime/"), list [2]);
69                         Assert.AreEqual (new XmlQualifiedName ("s", "http://www.w3.org/2001/XMLSchema"), list [3]);
70                         Assert.AreEqual (new XmlQualifiedName ("s0", "http://tempuri.org/"), list [4]);
71                         Assert.AreEqual (new XmlQualifiedName ("soap", "http://schemas.xmlsoap.org/wsdl/soap/"), list [5]);
72                         Assert.AreEqual (new XmlQualifiedName ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/"), list [6]);
73                         Assert.AreEqual (new XmlQualifiedName ("tm", "http://microsoft.com/wsdl/mime/textMatching/"), list [7]);
74                 }
75
76                 [Test]
77                 public void ExtensibleAttributes ()
78                 {
79                     FileStream fs = new FileStream("Test/System.Web.Services.Description/test.wsdl", FileMode.Open);
80                     XmlTextReader xtr = new XmlTextReader(fs);
81
82                     ServiceDescription sd = ServiceDescription.Read(xtr);
83                     CheckEA (sd, "sdAtt", "sdVal");
84                     CheckEA (sd.Messages [0], "msgAtt", "msgVal");
85                     CheckEA (sd.Messages [0].Parts [0], "partAtt", "partVal");
86
87                     CheckEA (sd.PortTypes [0], "ptAtt", "ptVal");
88                     CheckEA (sd.PortTypes [0].Operations [0], "opAtt", "opVal");
89                     CheckEA (sd.PortTypes [0].Operations [0].Messages[0], "opmsgAtt", "opmsgVal");
90
91                     CheckEA (sd.Services [0], "svcAtt", "svcVal");
92                     CheckEA (sd.Services [0].Ports [0], "portAtt", "portVal");
93
94                     fs.Close ();
95                 }
96
97                 [Test]
98                 public void Extensions ()
99                 {
100                     FileStream fs = new FileStream("Test/System.Web.Services.Description/test.wsdl", FileMode.Open);
101                     XmlTextReader xtr = new XmlTextReader(fs);
102
103                     ServiceDescription sd = ServiceDescription.Read(xtr);
104                     fs.Close ();
105
106                     Assert.IsNotNull (sd.Extensions);
107                     Assert.AreEqual (1, sd.Extensions.Count);
108
109                     CheckExtensions (sd, "sdElem", "sdVal");
110                     CheckExtensions (sd.Messages [0], "msgElem", "msgVal");
111                     CheckExtensions (sd.Messages [0].Parts [0], "partElem", "partVal");
112
113                     CheckExtensions (sd.PortTypes [0], "ptElem", "ptVal");
114                     CheckExtensions (sd.PortTypes [0].Operations [0], "opElem", "opVal");
115
116                     //Binding [0]
117                     Assert.IsNotNull (sd.Bindings [0].Extensions);
118                     Assert.AreEqual (2, sd.Bindings [0].Extensions.Count);
119                     CheckXmlElement (sd.Bindings [0].Extensions [0], "binElem");
120                     Assert.AreEqual (typeof (SoapBinding), sd.Bindings [0].Extensions [1].GetType ());
121                 
122                         //Binding [0].Operations [0]
123                         Assert.IsNotNull (sd.Bindings [0].Operations [0].Extensions);
124                         Assert.AreEqual (1, sd.Bindings [0].Operations [0].Extensions.Count);
125                         Assert.AreEqual (typeof (SoapOperationBinding), sd.Bindings [0].Operations [0].Extensions [0].GetType ());
126
127                     //Service
128                     CheckExtensions (sd.Services [0], "svcElem", "svcVal");
129
130                     //Service.Port
131                     Assert.IsNotNull (sd.Services [0].Ports [0].Extensions);
132                     Assert.AreEqual (2, sd.Services [0].Ports [0].Extensions.Count);
133                     Assert.AreEqual (typeof (SoapAddressBinding), sd.Services [0].Ports [0].Extensions [0].GetType ()); 
134                     CheckXmlElement (sd.Services [0].Ports [0].Extensions [1], "portElem");
135
136                 }
137
138                 void CheckExtensions (DocumentableItem di, string elemName, string val)
139                 {
140                         Assert.IsNotNull (di.Extensions);
141
142                         Assert.AreEqual (1, di.Extensions.Count);
143
144                         Assert.AreEqual (typeof (XmlElement), di.Extensions [0].GetType ());
145                         Assert.AreEqual (elemName, ((XmlElement) di.Extensions [0]).Name);
146                         Assert.AreEqual (val, ((XmlElement) di.Extensions [0]).InnerText);
147                 }
148
149                 void CheckXmlElement (object o, string name)
150                 {
151                         Assert.AreEqual (typeof (XmlElement), o.GetType ());
152                         Assert.AreEqual (name, ((XmlElement) o).Name);
153                 }
154
155                 void CheckEA (DocumentableItem di, string att, string val)
156                 {
157                         Assert.IsNotNull (di.ExtensibleAttributes);
158                         
159                         Assert.AreEqual (1, di.ExtensibleAttributes.Length);
160                         Assert.AreEqual (att, di.ExtensibleAttributes [0].Name);
161                         Assert.AreEqual (val, di.ExtensibleAttributes [0].Value);
162                 }
163
164                 [Test]
165                 public void ReadInvalid ()
166                 {
167                         ServiceDescription sd = ServiceDescription.Read (XmlReader.Create (new StringReader ("<definitions xmlns='http://schemas.xmlsoap.org/wsdl/'><hoge/></definitions>")));
168                 }
169
170                 [Test]
171                 public void ValidatingRead ()
172                 {
173                         ServiceDescription sd = ServiceDescription.Read (XmlReader.Create (new StringReader ("<definitions xmlns='http://schemas.xmlsoap.org/wsdl/'><hoge/></definitions>")), true);
174                         Assert.IsTrue (sd.ValidationWarnings.Count > 0);
175                 }
176 #endif
177
178     }
179
180         class qname_comparer : IComparer
181         {
182                 public int Compare (object x, object y)
183                 {
184                         XmlQualifiedName a = (XmlQualifiedName) x;
185                         XmlQualifiedName b = (XmlQualifiedName) y;
186
187                         return String.Compare (a.Name, b.Name);
188                 }
189         }
190 }
191