Merge pull request #4212 from BrzVlad/fix-ephemeron-leak
[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                 [Category ("MacNotWorking")] // https://bugzilla.xamarin.com/show_bug.cgi?id=51254
28                 public void SimpleWrite ()
29                 {
30                         ServiceDescription sd = new ServiceDescription ();
31                         Assert.IsNull (sd.Name);
32                         sd.Write (TextWriter.Null);
33                 }
34
35                 [Test]
36                 public void Ctor ()
37                 {
38                         ServiceDescription sd = new ServiceDescription ();
39                         Assert.IsNotNull (sd.Bindings);
40                         Assert.IsNotNull (sd.Extensions);
41                         Assert.IsNotNull (sd.Imports);
42                         Assert.IsNotNull (sd.Messages);
43                         Assert.IsNotNull (sd.PortTypes);
44                         Assert.IsNotNull (sd.Services);
45                         Assert.IsNotNull (sd.Types);
46
47                         Assert.IsNull (sd.ServiceDescriptions);
48                         Assert.IsNull (sd.TargetNamespace);
49                 }
50
51                 [Test]
52                 public void ReadAndRetrievalUrl ()
53                 {
54                         Assert.AreEqual (String.Empty, new ServiceDescription ().RetrievalUrl, "#1");
55                         ServiceDescription sd = ServiceDescription.Read ("Test/System.Web.Services.Description/test2.wsdl");
56                         Assert.AreEqual (String.Empty, sd.RetrievalUrl, "#2");
57                 }
58
59                 [Test]
60                 public void Namespaces ()
61                 {
62                         FileStream fs = new FileStream ("Test/System.Web.Services.Description/test.wsdl", FileMode.Open, FileAccess.Read);
63                         XmlTextReader xtr = new XmlTextReader (fs);
64
65                         ServiceDescription sd = ServiceDescription.Read (xtr);
66                         fs.Close ();
67
68                         Assert.IsNotNull (sd.Namespaces);
69                         Assert.AreEqual (8, sd.Namespaces.Count, "#n0");
70
71                         ArrayList list = new ArrayList (sd.Namespaces.ToArray ());
72                         list.Sort (new qname_comparer ());
73
74                         Assert.AreEqual (new XmlQualifiedName ("", "http://schemas.xmlsoap.org/wsdl/"), list [0]);
75                         Assert.AreEqual (new XmlQualifiedName ("http", "http://schemas.xmlsoap.org/wsdl/http/"), list [1]);
76                         Assert.AreEqual (new XmlQualifiedName ("mime", "http://schemas.xmlsoap.org/wsdl/mime/"), list [2]);
77                         Assert.AreEqual (new XmlQualifiedName ("s", "http://www.w3.org/2001/XMLSchema"), list [3]);
78                         Assert.AreEqual (new XmlQualifiedName ("s0", "http://tempuri.org/"), list [4]);
79                         Assert.AreEqual (new XmlQualifiedName ("soap", "http://schemas.xmlsoap.org/wsdl/soap/"), list [5]);
80                         Assert.AreEqual (new XmlQualifiedName ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/"), list [6]);
81                         Assert.AreEqual (new XmlQualifiedName ("tm", "http://microsoft.com/wsdl/mime/textMatching/"), list [7]);
82                 }
83
84                 [Test]
85                 public void ExtensibleAttributes ()
86                 {
87                     FileStream fs = new FileStream ("Test/System.Web.Services.Description/test.wsdl", FileMode.Open, FileAccess.Read);
88                     XmlTextReader xtr = new XmlTextReader(fs);
89
90                     ServiceDescription sd = ServiceDescription.Read(xtr);
91                     CheckEA (sd, "sdAtt", "sdVal");
92                     CheckEA (sd.Messages [0], "msgAtt", "msgVal");
93                     CheckEA (sd.Messages [0].Parts [0], "partAtt", "partVal");
94
95                     CheckEA (sd.PortTypes [0], "ptAtt", "ptVal");
96                     CheckEA (sd.PortTypes [0].Operations [0], "opAtt", "opVal");
97                     CheckEA (sd.PortTypes [0].Operations [0].Messages[0], "opmsgAtt", "opmsgVal");
98
99                     CheckEA (sd.Services [0], "svcAtt", "svcVal");
100                     CheckEA (sd.Services [0].Ports [0], "portAtt", "portVal");
101
102                     fs.Close ();
103                 }
104
105                 [Test]
106                 public void Extensions ()
107                 {
108                         FileStream fs = new FileStream("Test/System.Web.Services.Description/test.wsdl", FileMode.Open, FileAccess.Read);
109                     XmlTextReader xtr = new XmlTextReader(fs);
110
111                     ServiceDescription sd = ServiceDescription.Read(xtr);
112                     fs.Close ();
113
114                     Assert.IsNotNull (sd.Extensions);
115                     Assert.AreEqual (1, sd.Extensions.Count);
116
117                     CheckExtensions (sd, "sdElem", "sdVal");
118                     CheckExtensions (sd.Messages [0], "msgElem", "msgVal");
119                     CheckExtensions (sd.Messages [0].Parts [0], "partElem", "partVal");
120
121                     CheckExtensions (sd.PortTypes [0], "ptElem", "ptVal");
122                     CheckExtensions (sd.PortTypes [0].Operations [0], "opElem", "opVal");
123
124                     //Binding [0]
125                     Assert.IsNotNull (sd.Bindings [0].Extensions);
126                     Assert.AreEqual (2, sd.Bindings [0].Extensions.Count);
127                     CheckXmlElement (sd.Bindings [0].Extensions [0], "binElem");
128                     Assert.AreEqual (typeof (SoapBinding), sd.Bindings [0].Extensions [1].GetType ());
129                 
130                         //Binding [0].Operations [0]
131                         Assert.IsNotNull (sd.Bindings [0].Operations [0].Extensions);
132                         Assert.AreEqual (1, sd.Bindings [0].Operations [0].Extensions.Count);
133                         Assert.AreEqual (typeof (SoapOperationBinding), sd.Bindings [0].Operations [0].Extensions [0].GetType ());
134
135                     //Service
136                     CheckExtensions (sd.Services [0], "svcElem", "svcVal");
137
138                     //Service.Port
139                     Assert.IsNotNull (sd.Services [0].Ports [0].Extensions);
140                     Assert.AreEqual (2, sd.Services [0].Ports [0].Extensions.Count);
141                     Assert.AreEqual (typeof (SoapAddressBinding), sd.Services [0].Ports [0].Extensions [0].GetType ()); 
142                     CheckXmlElement (sd.Services [0].Ports [0].Extensions [1], "portElem");
143
144                     string out_file = Path.GetTempFileName ();
145                     try {
146                             using (FileStream out_fs = new FileStream(out_file, FileMode.Create))
147                                     sd.Write (out_fs);
148                     } finally {
149                             if (!String.IsNullOrEmpty (out_file))
150                                     File.Delete (out_file);
151                     }
152                 }
153
154                 void CheckExtensions (DocumentableItem di, string elemName, string val)
155                 {
156                         Assert.IsNotNull (di.Extensions);
157
158                         Assert.AreEqual (1, di.Extensions.Count);
159
160                         Assert.AreEqual (typeof (XmlElement), di.Extensions [0].GetType ());
161                         Assert.AreEqual (elemName, ((XmlElement) di.Extensions [0]).Name);
162                         Assert.AreEqual (val, ((XmlElement) di.Extensions [0]).InnerText);
163                 }
164
165                 void CheckXmlElement (object o, string name)
166                 {
167                         Assert.AreEqual (typeof (XmlElement), o.GetType ());
168                         Assert.AreEqual (name, ((XmlElement) o).Name);
169                 }
170
171                 void CheckEA (DocumentableItem di, string att, string val)
172                 {
173                         Assert.IsNotNull (di.ExtensibleAttributes);
174                         
175                         Assert.AreEqual (1, di.ExtensibleAttributes.Length);
176                         Assert.AreEqual (att, di.ExtensibleAttributes [0].Name);
177                         Assert.AreEqual (val, di.ExtensibleAttributes [0].Value);
178                 }
179
180                 [Test]
181                 public void ReadInvalid ()
182                 {
183                         ServiceDescription sd = ServiceDescription.Read (XmlReader.Create (new StringReader ("<definitions xmlns='http://schemas.xmlsoap.org/wsdl/'><hoge/></definitions>")));
184                 }
185
186                 [Test]
187                 public void ValidatingRead ()
188                 {
189                         ServiceDescription sd = ServiceDescription.Read (XmlReader.Create (new StringReader ("<definitions xmlns='http://schemas.xmlsoap.org/wsdl/'><hoge/></definitions>")), true);
190                         Assert.IsTrue (sd.ValidationWarnings.Count > 0);
191                 }
192
193     }
194
195         class qname_comparer : IComparer
196         {
197                 public int Compare (object x, object y)
198                 {
199                         XmlQualifiedName a = (XmlQualifiedName) x;
200                         XmlQualifiedName b = (XmlQualifiedName) y;
201
202                         return String.Compare (a.Name, b.Name);
203                 }
204         }
205 }
206