2007-05-08 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web.Services / Test / System.Web.Services.Description / ServiceDescriptionReflectorTest.cs
1 //
2 // ServiceDescriptionReflectorTest.cs
3 //
4 // Author:
5 //      Gert Driesen  <drieseng@users.sourceforge.net>
6 //      Atsushi Enomoto  <atsushi@ximian.com>
7 //
8 // Copyright (C) 2007 Gert Driesen
9 // Copyright (C) 2006 Novell, Inc.
10 //
11
12 using NUnit.Framework;
13
14 using System;
15 using System.Globalization;
16 using System.IO;
17 using System.Web.Services;
18 using System.Web.Services.Description;
19 using System.Xml.Schema;
20 using System.Xml.Serialization;
21
22 namespace MonoTests.System.Web.Services.Description
23 {
24         [TestFixture]
25         public class ServiceDescriptionReflectorTest
26         {
27 #if NET_2_0
28                 [Test]
29                 public void ReflectNullableInt ()
30                 {
31                         ServiceDescriptionReflector r =
32                                 new ServiceDescriptionReflector ();
33                         r.Reflect (typeof (NullableContainer), null);
34                         ServiceDescription sd = r.ServiceDescriptions [0];
35                         XmlSchema xs = sd.Types.Schemas [0];
36                         XmlSchemaElement el = null;
37                         foreach (XmlSchemaElement e in xs.Items) {
38                                 if (e.Name != "GetNullResponse")
39                                         continue;
40                                 el = e;
41                                 break;
42                         }
43                         XmlSchemaComplexType ct =
44                                 el.SchemaType as XmlSchemaComplexType;
45                         XmlSchemaSequence s = ct.Particle as XmlSchemaSequence;
46                         XmlSchemaElement e2 = s.Items [0] as XmlSchemaElement;
47                         Assert.IsTrue (e2.IsNillable);
48                 }
49 #endif
50                 [Test]
51                 [Category ("NotWorking")]
52                 public void IncludeTest ()
53                 {
54                         ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
55                         reflector.Reflect (typeof (IncludeTestServices), "http://localhost/IncludeTestServices.asmx");
56
57                         Assert.AreEqual (0, reflector.Schemas.Count, "#1");
58                         Assert.AreEqual (1, reflector.ServiceDescriptions.Count, "#2");
59
60                         ServiceDescription sd = reflector.ServiceDescriptions[0];
61
62                         Assert.IsNull (sd.Name, "#3");
63                         Assert.AreEqual (1, sd.Types.Schemas.Count, "#4");
64
65                         StringWriter sw = new StringWriter ();
66                         sd.Write (sw);
67
68                         Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
69                                 "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
70 #if NET_2_0
71                                 "<wsdl:definitions xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tm=\"http://microsoft.com/wsdl/mime/textMatching/\""  +
72                                 " xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\"" +
73                                 " xmlns:tns=\"http://tempuri.org/\" xmlns:s=\"http://www.w3.org/2001/XMLSchema\"" +
74                                 " xmlns:soap12=\"http://schemas.xmlsoap.org/wsdl/soap12/\"" +
75                                 " xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\" targetNamespace=\"http://tempuri.org/\"" +
76                                 " xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\">{0}" +
77 #else
78                                 "<wsdl:definitions xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\"" +
79                                 " xmlns:s=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\"" +
80                                 " xmlns:tns=\"http://tempuri.org/\" xmlns:tm=\"http://microsoft.com/wsdl/mime/textMatching/\"" +
81                                 " xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\" targetNamespace=\"http://tempuri.org/\"" +
82                                 " xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\">{0}" +
83 #endif
84                                 "  <wsdl:types>{0}" +
85                                 "    <s:schema elementFormDefault=\"qualified\" targetNamespace=\"http://tempuri.org/\">{0}" +
86                                 "      <s:element name=\"EchoString\">{0}" +
87                                 "        <s:complexType>{0}" +
88                                 "          <s:sequence>{0}" +
89                                 "            <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"strval\" type=\"s:string\" />{0}" +
90                                 "          </s:sequence>{0}" +
91                                 "        </s:complexType>{0}" +
92                                 "      </s:element>{0}" +
93                                 "      <s:element name=\"EchoStringResponse\">{0}" +
94                                 "        <s:complexType>{0}" +
95                                 "          <s:sequence>{0}" +
96                                 "            <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"MyTime\" type=\"s:time\" />{0}" +
97                                 "          </s:sequence>{0}" +
98                                 "        </s:complexType>{0}" +
99                                 "      </s:element>{0}" +
100                                 "      <s:element name=\"Vehicle\">{0}" +
101                                 "        <s:complexType>{0}" +
102                                 "          <s:sequence>{0}" +
103                                 "            <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"licenseNumber\" type=\"s:string\" />{0}" +
104                                 "          </s:sequence>{0}" +
105                                 "        </s:complexType>{0}" +
106                                 "      </s:element>{0}" +
107                                 "      <s:element name=\"VehicleResponse\">{0}" +
108                                 "        <s:complexType>{0}" +
109                                 "          <s:sequence>{0}" +
110                                 "            <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"NewVehicle\" nillable=\"true\" type=\"tns:Vehicle\" />{0}" +
111                                 "          </s:sequence>{0}" +
112                                 "        </s:complexType>{0}" +
113                                 "      </s:element>{0}" +
114                                 "      <s:complexType name=\"Vehicle\" abstract=\"true\">{0}" +
115                                 "        <s:sequence>{0}" +
116                                 "          <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"licenseNumber\" type=\"s:string\" />{0}" +
117                                 "          <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"make\" type=\"s:dateTime\" />{0}" +
118                                 "          <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"age\" type=\"tns:TimeSpan\" />{0}" +
119                                 "        </s:sequence>{0}" +
120                                 "      </s:complexType>{0}" +
121                                 "      <s:complexType name=\"TimeSpan\" />{0}" +
122                                 "      <s:complexType name=\"Car\">{0}" +
123                                 "        <s:complexContent mixed=\"false\">{0}" +
124                                 "          <s:extension base=\"tns:Vehicle\" />{0}" +
125                                 "        </s:complexContent>{0}" +
126                                 "      </s:complexType>{0}" +
127                                 "    </s:schema>{0}" +
128                                 "  </wsdl:types>{0}" +
129                                 "  <wsdl:message name=\"EchoStringSoapIn\">{0}" +
130                                 "    <wsdl:part name=\"parameters\" element=\"tns:EchoString\" />{0}" +
131                                 "  </wsdl:message>{0}" +
132                                 "  <wsdl:message name=\"EchoStringSoapOut\">{0}" +
133                                 "    <wsdl:part name=\"parameters\" element=\"tns:EchoStringResponse\" />{0}" +
134                                 "  </wsdl:message>{0}" +
135                                 "  <wsdl:message name=\"VehicleSoapIn\">{0}" +
136                                 "    <wsdl:part name=\"parameters\" element=\"tns:Vehicle\" />{0}" +
137                                 "  </wsdl:message>{0}" +
138                                 "  <wsdl:message name=\"VehicleSoapOut\">{0}" +
139                                 "    <wsdl:part name=\"parameters\" element=\"tns:VehicleResponse\" />{0}" +
140                                 "  </wsdl:message>{0}" +
141                                 "  <wsdl:portType name=\"IncludeTestServicesSoap\">{0}" +
142                                 "    <wsdl:operation name=\"EchoString\">{0}" +
143                                 "      <wsdl:input message=\"tns:EchoStringSoapIn\" />{0}" +
144                                 "      <wsdl:output message=\"tns:EchoStringSoapOut\" />{0}" +
145                                 "    </wsdl:operation>{0}" +
146                                 "    <wsdl:operation name=\"Vehicle\">{0}" +
147                                 "      <wsdl:input message=\"tns:VehicleSoapIn\" />{0}" +
148                                 "      <wsdl:output message=\"tns:VehicleSoapOut\" />{0}" +
149                                 "    </wsdl:operation>{0}" +
150                                 "  </wsdl:portType>{0}" +
151                                 "  <wsdl:binding name=\"IncludeTestServicesSoap\" type=\"tns:IncludeTestServicesSoap\">{0}" +
152 #if NET_2_0
153                                 "    <soap:binding transport=\"http://schemas.xmlsoap.org/soap/http\" />{0}" +
154 #else
155                                 "    <soap:binding transport=\"http://schemas.xmlsoap.org/soap/http\" style=\"document\" />{0}" +
156 #endif
157                                 "    <wsdl:operation name=\"EchoString\">{0}" +
158                                 "      <soap:operation soapAction=\"http://tempuri.org/EchoString\" style=\"document\" />{0}" +
159                                 "      <wsdl:input>{0}" +
160                                 "        <soap:body use=\"literal\" />{0}" +
161                                 "      </wsdl:input>{0}" +
162                                 "      <wsdl:output>{0}" +
163                                 "        <soap:body use=\"literal\" />{0}" +
164                                 "      </wsdl:output>{0}" +
165                                 "    </wsdl:operation>{0}" +
166                                 "    <wsdl:operation name=\"Vehicle\">{0}" +
167                                 "      <soap:operation soapAction=\"http://tempuri.org/Vehicle\" style=\"document\" />{0}" +
168                                 "      <wsdl:input>{0}" +
169                                 "        <soap:body use=\"literal\" />{0}" +
170                                 "      </wsdl:input>{0}" +
171                                 "      <wsdl:output>{0}" +
172                                 "        <soap:body use=\"literal\" />{0}" +
173                                 "      </wsdl:output>{0}" +
174                                 "    </wsdl:operation>{0}" +
175                                 "  </wsdl:binding>{0}" +
176 #if NET_2_0
177                                 "  <wsdl:binding name=\"IncludeTestServicesSoap12\" type=\"tns:IncludeTestServicesSoap\">{0}" +
178                                 "    <soap12:binding transport=\"http://schemas.xmlsoap.org/soap/http\" />{0}" +
179                                 "    <wsdl:operation name=\"EchoString\">{0}" +
180                                 "      <soap12:operation soapAction=\"http://tempuri.org/EchoString\" style=\"document\" />{0}" +
181                                 "      <wsdl:input>{0}" +
182                                 "        <soap12:body use=\"literal\" />{0}" +
183                                 "      </wsdl:input>{0}" +
184                                 "      <wsdl:output>{0}" +
185                                 "        <soap12:body use=\"literal\" />{0}" +
186                                 "      </wsdl:output>{0}" +
187                                 "    </wsdl:operation>{0}" +
188                                 "    <wsdl:operation name=\"Vehicle\">{0}" +
189                                 "      <soap12:operation soapAction=\"http://tempuri.org/Vehicle\" style=\"document\" />{0}" +
190                                 "      <wsdl:input>{0}" +
191                                 "        <soap12:body use=\"literal\" />{0}" +
192                                 "      </wsdl:input>{0}" +
193                                 "      <wsdl:output>{0}" +
194                                 "        <soap12:body use=\"literal\" />{0}" +
195                                 "      </wsdl:output>{0}" +
196                                 "    </wsdl:operation>{0}" +
197                                 "  </wsdl:binding>{0}" +
198 #endif
199                                 "  <wsdl:service name=\"IncludeTestServices\">{0}" +
200 #if ONLY_1_1
201                                 "    <documentation xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />{0}" +
202 #endif
203                                 "    <wsdl:port name=\"IncludeTestServicesSoap\" binding=\"tns:IncludeTestServicesSoap\">{0}" +
204                                 "      <soap:address location=\"http://localhost/IncludeTestServices.asmx\" />{0}" +
205                                 "    </wsdl:port>{0}" +
206 #if NET_2_0
207                                 "    <wsdl:port name=\"IncludeTestServicesSoap12\" binding=\"tns:IncludeTestServicesSoap12\">{0}" +
208                                 "      <soap12:address location=\"http://localhost/IncludeTestServices.asmx\" />{0}" +
209                                 "    </wsdl:port>{0}" +
210 #endif
211                                 "  </wsdl:service>{0}" +
212                                 "</wsdl:definitions>", Environment.NewLine), sw.ToString (), "#5");
213                 }
214
215                 public class IncludeTestServices : WebService
216                 {
217                         [WebMethod ()]
218                         [return: XmlElement ("MyTime", DataType = "time")]
219                         public DateTime EchoString ([XmlElement (DataType = "string")] string strval)
220                         {
221                                 return DateTime.Now;
222                         }
223
224                         [WebMethod ()]
225                         [XmlInclude (typeof (Car))]
226                         public Vehicle Vehicle (string licenseNumber)
227                         {
228                                 if (licenseNumber == "0") {
229                                         Vehicle v = new Car ();
230                                         v.licenseNumber = licenseNumber;
231                                         return v;
232                                 } else {
233                                         return null;
234                                 }
235                         }
236                 }
237                 [XmlRoot ("NewVehicle")]
238                 public abstract class Vehicle
239                 {
240                         public string licenseNumber;
241                         public DateTime make;
242                         public TimeSpan age;
243                 }
244
245                 public class Car : Vehicle
246                 {
247                 }
248
249 #if NET_2_0
250                 public class NullableContainer
251                 {
252                         [WebMethod (Description="Test nullables")]
253                         public int? GetNull ()
254                         {
255                                 return null;
256                         }
257                 }
258 #endif
259         }
260 }