2 // XsdDataContractImporterTest2.cs
5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2012 Xamarin, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 using System.CodeDom.Compiler;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Collections.ObjectModel;
37 using System.Runtime.Serialization;
38 using System.ServiceModel.Description;
39 using System.Web.Services.Discovery;
41 using System.Xml.Schema;
42 using System.Xml.Serialization;
43 using Microsoft.CSharp;
44 using NUnit.Framework;
45 using NUnit.Framework.Constraints;
46 using NUnit.Framework.SyntaxHelpers;
48 using QName = System.Xml.XmlQualifiedName;
50 namespace MonoTests.System.Runtime.Serialization
53 public class XsdDataContractImporterTest2
55 MetadataSet collectionsMetadata;
56 MetadataSet customCollectionsMetadata;
61 collectionsMetadata = WsdlHelper.GetMetadataSet ("collections.wsdl");
62 customCollectionsMetadata = WsdlHelper.GetMetadataSet ("custom-collections.wsdl");
66 public void TestSimpleList ()
68 var options = new ImportOptions ();
70 var ccu = WsdlHelper.Import (collectionsMetadata, options);
72 var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList");
73 Assert.That (method, Is.Not.Null, "#1");
74 Assert.That (method.ReturnType, Is.Not.Null, "#2");
76 Assert.That (method.ReturnType.ArrayRank, Is.EqualTo (1), "#3");
77 Assert.That (method.ReturnType.BaseType, Is.EqualTo ("System.Int32"), "#4");
81 public void TestSimpleList2 ()
83 var options = new ImportOptions ();
84 options.ReferencedCollectionTypes.Add (typeof(LinkedList<>));
86 var ccu = WsdlHelper.Import (collectionsMetadata, options);
88 var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList");
89 Assert.That (method, Is.Not.Null, "#1");
91 var ret = method.ReturnType;
92 Assert.That (ret, Is.Not.Null, "#2");
94 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
95 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.LinkedList`1"), "#4");
96 Assert.That (ret.TypeArguments.Count, Is.EqualTo (1), "#5");
97 Assert.That (ret.TypeArguments [0].BaseType, Is.EqualTo ("System.Int32"), "#6");
101 public void TestSimpleList3 ()
103 var options = new ImportOptions ();
104 options.ReferencedCollectionTypes.Add (typeof (Dictionary<,>));
105 options.ReferencedCollectionTypes.Add (typeof (ObservableCollection<>));
107 var ccu = WsdlHelper.Import (collectionsMetadata, options);
109 var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList");
110 Assert.That (method, Is.Not.Null, "#1");
112 var ret = method.ReturnType;
113 Assert.That (ret, Is.Not.Null, "#2");
115 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
116 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.ObjectModel.ObservableCollection`1"), "#4");
117 Assert.That (ret.TypeArguments.Count, Is.EqualTo (1), "#5");
118 Assert.That (ret.TypeArguments [0].BaseType, Is.EqualTo ("System.Int32"), "#6");
122 public void TestListOfFoo ()
124 var options = new ImportOptions ();
125 options.ReferencedCollectionTypes.Add (typeof (List<>));
127 var ccu = WsdlHelper.Import (collectionsMetadata, options);
129 var method = ccu.FindMethod ("MyServiceClient", "GetListOfFoo");
130 Assert.That (method, Is.Not.Null, "#1");
132 var ret = method.ReturnType;
133 Assert.That (ret, Is.Not.Null, "#2");
135 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
136 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.List`1"), "#4");
137 Assert.That (ret.TypeArguments.Count, Is.EqualTo (1), "#5");
138 Assert.That (ret.TypeArguments [0].BaseType, Is.EqualTo ("TestWCF.Model.Foo"), "#6");
142 public void TestListOfStringArray ()
144 var options = new ImportOptions ();
145 options.ReferencedCollectionTypes.Add (typeof (List<>));
147 var ccu = WsdlHelper.Import (collectionsMetadata, options);
149 var method = ccu.FindMethod ("MyServiceClient", "GetListOfStringArray");
150 Assert.That (method, Is.Not.Null, "#1");
152 var ret = method.ReturnType;
153 Assert.That (ret, Is.Not.Null, "#2");
155 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
156 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.List`1"), "#4");
157 Assert.That (ret.TypeArguments.Count, Is.EqualTo (1), "#5");
159 var baseType = ret.TypeArguments [0];
160 Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.List`1"), "#6");
161 Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#7");
162 Assert.That (baseType.TypeArguments [0].BaseType, Is.EqualTo ("System.String"), "#8");
166 public void TestSimpleDictionary ()
168 var options = new ImportOptions ();
169 options.ReferencedCollectionTypes.Add (typeof (List<>));
171 var ccu = WsdlHelper.Import (collectionsMetadata, options);
173 var method = ccu.FindMethod ("MyServiceClient", "GetSimpleDictionary");
174 Assert.That (method, Is.Not.Null, "#1");
176 var ret = method.ReturnType;
177 Assert.That (ret, Is.Not.Null, "#2");
179 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
180 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.Dictionary`2"), "#4");
181 Assert.That (ret.TypeArguments.Count, Is.EqualTo (2), "#5");
183 var keyType = ret.TypeArguments [0];
184 Assert.That (keyType.BaseType, Is.EqualTo ("System.Int32"), "#6");
185 var valueType = ret.TypeArguments [1];
186 Assert.That (valueType.BaseType, Is.EqualTo ("System.String"), "#7");
190 public void TestSimpleDictionary2 ()
192 var options = new ImportOptions ();
193 options.ReferencedCollectionTypes.Add (typeof (SortedList<,>));
195 var ccu = WsdlHelper.Import (collectionsMetadata, options);
197 var method = ccu.FindMethod ("MyServiceClient", "GetSimpleDictionary");
198 Assert.That (method, Is.Not.Null, "#1");
200 var ret = method.ReturnType;
201 Assert.That (ret, Is.Not.Null, "#2");
203 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
204 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.SortedList`2"), "#4");
205 Assert.That (ret.TypeArguments.Count, Is.EqualTo (2), "#5");
207 var keyType = ret.TypeArguments [0];
208 Assert.That (keyType.BaseType, Is.EqualTo ("System.Int32"), "#6");
209 var valueType = ret.TypeArguments [1];
210 Assert.That (valueType.BaseType, Is.EqualTo ("System.String"), "#7");
214 public void TestCustomCollection ()
216 var options = new ImportOptions ();
218 var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
220 var method = ccu.FindMethod ("MyServiceClient", "GetCustomCollection");
221 Assert.That (method, Is.Not.Null, "#1");
223 var ret = method.ReturnType;
224 Assert.That (ret, Is.Not.Null, "#2");
226 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
227 Assert.That (ret.BaseType, Is.EqualTo ("TestWCF.Model.MyCollection"), "#4");
228 Assert.That (ret.TypeArguments.Count, Is.EqualTo (0), "#5");
232 public void TestCustomCollection2 ()
234 var options = new ImportOptions ();
236 var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
238 var method = ccu.FindMethod ("MyServiceClient", "GetCustomCollection2");
239 Assert.That (method, Is.Not.Null, "#1");
241 var ret = method.ReturnType;
242 Assert.That (ret, Is.Not.Null, "#2");
244 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
245 Assert.That (ret.BaseType, Is.EqualTo ("TestWCF.Model1.MyCollectionOfdouble"), "#4");
246 Assert.That (ret.TypeArguments.Count, Is.EqualTo (0), "#5");
250 public void TestCustomCollection3 ()
252 var options = new ImportOptions ();
254 var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
256 var type = ccu.FindType ("MyCollection");
257 Assert.That (type, Is.Not.Null, "#1a");
258 Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2a");
260 var baseType = type.BaseTypes[0];
261 Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.List`1"), "#3a");
262 Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#4a");
263 Assert.That (baseType.TypeArguments[0].BaseType, Is.EqualTo ("System.String"), "#5a");
265 var attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
266 Assert.That (attr, Is.Not.Null, "#6a");
268 var nameArg = attr.FindArgument ("Name");
269 Assert.That (nameArg, Is.Not.Null, "#7a");
270 Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollection"), "#8a");
272 var nsArg = attr.FindArgument ("Namespace");
273 Assert.That (nsArg, Is.Not.Null, "#9a");
274 Assert.That (((CodePrimitiveExpression)nsArg.Value).Value, Is.EqualTo ("http://schemas.datacontract.org/2004/07/TestWCF.Model"), "#10a");
276 var itemArg = attr.FindArgument ("ItemName");
277 Assert.That (itemArg, Is.Not.Null);
278 Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("string"), "#11a");
280 type = ccu.FindType ("MyCollectionOfdouble");
281 Assert.That (type, Is.Not.Null, "#1b");
282 Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2b");
284 baseType = type.BaseTypes[0];
285 Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.List`1"), "#3b");
286 Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#4b");
287 Assert.That (baseType.TypeArguments[0].BaseType, Is.EqualTo ("System.Double"), "#5b");
289 attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
290 Assert.That (attr, Is.Not.Null, "#6b");
292 nameArg = attr.FindArgument ("Name");
293 Assert.That (nameArg, Is.Not.Null, "#7b");
294 Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollectionOfdouble"), "#8b");
296 nsArg = attr.FindArgument ("Namespace");
297 Assert.That (nsArg, Is.Not.Null, "#9b");
298 Assert.That (((CodePrimitiveExpression)nsArg.Value).Value, Is.EqualTo ("http://schemas.datacontract.org/2004/07/TestWCF.Model"), "#10b");
300 itemArg = attr.FindArgument ("ItemName");
301 Assert.That (itemArg, Is.Not.Null);
302 Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("double"), "#11b");
306 public void TestCustomCollection4 ()
308 var options = new ImportOptions ();
309 options.ReferencedCollectionTypes.Add (typeof (LinkedList<>));
311 var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
313 var type = ccu.FindType ("MyCollection");
314 Assert.That (type, Is.Not.Null, "#1a");
315 Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2a");
317 var baseType = type.BaseTypes[0];
318 Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.LinkedList`1"), "#3a");
319 Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#4a");
320 Assert.That (baseType.TypeArguments[0].BaseType, Is.EqualTo ("System.String"), "#5a");
322 var attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
323 Assert.That (attr, Is.Not.Null, "#6a");
325 var nameArg = attr.FindArgument ("Name");
326 Assert.That (nameArg, Is.Not.Null, "#7a");
327 Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollection"), "#8a");
329 var nsArg = attr.FindArgument ("Namespace");
330 Assert.That (nsArg, Is.Not.Null, "#9a");
331 Assert.That (((CodePrimitiveExpression)nsArg.Value).Value, Is.EqualTo ("http://schemas.datacontract.org/2004/07/TestWCF.Model"), "#10a");
333 var itemArg = attr.FindArgument ("ItemName");
334 Assert.That (itemArg, Is.Not.Null);
335 Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("string"), "#11a");
337 type = ccu.FindType ("MyCollectionOfdouble");
338 Assert.That (type, Is.Not.Null, "#1b");
339 Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2b");
341 baseType = type.BaseTypes[0];
342 Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.LinkedList`1"), "#3b");
343 Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#4b");
344 Assert.That (baseType.TypeArguments[0].BaseType, Is.EqualTo ("System.Double"), "#5b");
346 attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
347 Assert.That (attr, Is.Not.Null, "#6b");
349 nameArg = attr.FindArgument ("Name");
350 Assert.That (nameArg, Is.Not.Null, "#7b");
351 Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollectionOfdouble"), "#8b");
353 nsArg = attr.FindArgument ("Namespace");
354 Assert.That (nsArg, Is.Not.Null, "#9b");
355 Assert.That (((CodePrimitiveExpression)nsArg.Value).Value, Is.EqualTo ("http://schemas.datacontract.org/2004/07/TestWCF.Model"), "#10b");
357 itemArg = attr.FindArgument ("ItemName");
358 Assert.That (itemArg, Is.Not.Null);
359 Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("double"), "#11b");