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;
47 using QName = System.Xml.XmlQualifiedName;
49 namespace MonoTests.System.Runtime.Serialization
52 public class XsdDataContractImporterTest2
54 MetadataSet collectionsMetadata;
55 MetadataSet customCollectionsMetadata;
60 collectionsMetadata = WsdlHelper.GetMetadataSet ("collections.wsdl");
61 customCollectionsMetadata = WsdlHelper.GetMetadataSet ("custom-collections.wsdl");
65 public void TestSimpleList ()
67 var options = new ImportOptions ();
69 var ccu = WsdlHelper.Import (collectionsMetadata, options);
71 var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList");
72 Assert.That (method, Is.Not.Null, "#1");
73 Assert.That (method.ReturnType, Is.Not.Null, "#2");
75 Assert.That (method.ReturnType.ArrayRank, Is.EqualTo (1), "#3");
76 Assert.That (method.ReturnType.BaseType, Is.EqualTo ("System.Int32"), "#4");
80 public void TestSimpleList2 ()
82 var options = new ImportOptions ();
83 options.ReferencedCollectionTypes.Add (typeof(LinkedList<>));
85 var ccu = WsdlHelper.Import (collectionsMetadata, options);
87 var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList");
88 Assert.That (method, Is.Not.Null, "#1");
90 var ret = method.ReturnType;
91 Assert.That (ret, Is.Not.Null, "#2");
93 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
94 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.LinkedList`1"), "#4");
95 Assert.That (ret.TypeArguments.Count, Is.EqualTo (1), "#5");
96 Assert.That (ret.TypeArguments [0].BaseType, Is.EqualTo ("System.Int32"), "#6");
100 public void TestSimpleList3 ()
102 var options = new ImportOptions ();
103 options.ReferencedCollectionTypes.Add (typeof (Dictionary<,>));
104 options.ReferencedCollectionTypes.Add (typeof (ObservableCollection<>));
106 var ccu = WsdlHelper.Import (collectionsMetadata, options);
108 var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList");
109 Assert.That (method, Is.Not.Null, "#1");
111 var ret = method.ReturnType;
112 Assert.That (ret, Is.Not.Null, "#2");
114 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
115 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.ObjectModel.ObservableCollection`1"), "#4");
116 Assert.That (ret.TypeArguments.Count, Is.EqualTo (1), "#5");
117 Assert.That (ret.TypeArguments [0].BaseType, Is.EqualTo ("System.Int32"), "#6");
121 public void TestListOfFoo ()
123 var options = new ImportOptions ();
124 options.ReferencedCollectionTypes.Add (typeof (List<>));
126 var ccu = WsdlHelper.Import (collectionsMetadata, options);
128 var method = ccu.FindMethod ("MyServiceClient", "GetListOfFoo");
129 Assert.That (method, Is.Not.Null, "#1");
131 var ret = method.ReturnType;
132 Assert.That (ret, Is.Not.Null, "#2");
134 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
135 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.List`1"), "#4");
136 Assert.That (ret.TypeArguments.Count, Is.EqualTo (1), "#5");
137 Assert.That (ret.TypeArguments [0].BaseType, Is.EqualTo ("TestWCF.Model.Foo"), "#6");
141 public void TestListOfStringArray ()
143 var options = new ImportOptions ();
144 options.ReferencedCollectionTypes.Add (typeof (List<>));
146 var ccu = WsdlHelper.Import (collectionsMetadata, options);
148 var method = ccu.FindMethod ("MyServiceClient", "GetListOfStringArray");
149 Assert.That (method, Is.Not.Null, "#1");
151 var ret = method.ReturnType;
152 Assert.That (ret, Is.Not.Null, "#2");
154 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
155 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.List`1"), "#4");
156 Assert.That (ret.TypeArguments.Count, Is.EqualTo (1), "#5");
158 var baseType = ret.TypeArguments [0];
159 Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.List`1"), "#6");
160 Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#7");
161 Assert.That (baseType.TypeArguments [0].BaseType, Is.EqualTo ("System.String"), "#8");
165 public void TestSimpleDictionary ()
167 var options = new ImportOptions ();
168 options.ReferencedCollectionTypes.Add (typeof (List<>));
170 var ccu = WsdlHelper.Import (collectionsMetadata, options);
172 var method = ccu.FindMethod ("MyServiceClient", "GetSimpleDictionary");
173 Assert.That (method, Is.Not.Null, "#1");
175 var ret = method.ReturnType;
176 Assert.That (ret, Is.Not.Null, "#2");
178 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
179 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.Dictionary`2"), "#4");
180 Assert.That (ret.TypeArguments.Count, Is.EqualTo (2), "#5");
182 var keyType = ret.TypeArguments [0];
183 Assert.That (keyType.BaseType, Is.EqualTo ("System.Int32"), "#6");
184 var valueType = ret.TypeArguments [1];
185 Assert.That (valueType.BaseType, Is.EqualTo ("System.String"), "#7");
189 public void TestSimpleDictionary2 ()
191 var options = new ImportOptions ();
192 options.ReferencedCollectionTypes.Add (typeof (SortedList<,>));
194 var ccu = WsdlHelper.Import (collectionsMetadata, options);
196 var method = ccu.FindMethod ("MyServiceClient", "GetSimpleDictionary");
197 Assert.That (method, Is.Not.Null, "#1");
199 var ret = method.ReturnType;
200 Assert.That (ret, Is.Not.Null, "#2");
202 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
203 Assert.That (ret.BaseType, Is.EqualTo ("System.Collections.Generic.SortedList`2"), "#4");
204 Assert.That (ret.TypeArguments.Count, Is.EqualTo (2), "#5");
206 var keyType = ret.TypeArguments [0];
207 Assert.That (keyType.BaseType, Is.EqualTo ("System.Int32"), "#6");
208 var valueType = ret.TypeArguments [1];
209 Assert.That (valueType.BaseType, Is.EqualTo ("System.String"), "#7");
213 public void TestCustomCollection ()
215 var options = new ImportOptions ();
217 var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
219 var method = ccu.FindMethod ("MyServiceClient", "GetCustomCollection");
220 Assert.That (method, Is.Not.Null, "#1");
222 var ret = method.ReturnType;
223 Assert.That (ret, Is.Not.Null, "#2");
225 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
226 Assert.That (ret.BaseType, Is.EqualTo ("TestWCF.Model.MyCollection"), "#4");
227 Assert.That (ret.TypeArguments.Count, Is.EqualTo (0), "#5");
231 public void TestCustomCollection2 ()
233 var options = new ImportOptions ();
235 var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
237 var method = ccu.FindMethod ("MyServiceClient", "GetCustomCollection2");
238 Assert.That (method, Is.Not.Null, "#1");
240 var ret = method.ReturnType;
241 Assert.That (ret, Is.Not.Null, "#2");
243 Assert.That (ret.ArrayRank, Is.EqualTo (0), "#3");
244 Assert.That (ret.BaseType, Is.EqualTo ("TestWCF.Model1.MyCollectionOfdouble"), "#4");
245 Assert.That (ret.TypeArguments.Count, Is.EqualTo (0), "#5");
249 public void TestCustomCollection3 ()
251 var options = new ImportOptions ();
253 var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
255 var type = ccu.FindType ("MyCollection");
256 Assert.That (type, Is.Not.Null, "#1a");
257 Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2a");
259 var baseType = type.BaseTypes[0];
260 Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.List`1"), "#3a");
261 Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#4a");
262 Assert.That (baseType.TypeArguments[0].BaseType, Is.EqualTo ("System.String"), "#5a");
264 var attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
265 Assert.That (attr, Is.Not.Null, "#6a");
267 var nameArg = attr.FindArgument ("Name");
268 Assert.That (nameArg, Is.Not.Null, "#7a");
269 Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollection"), "#8a");
271 var nsArg = attr.FindArgument ("Namespace");
272 Assert.That (nsArg, Is.Not.Null, "#9a");
273 Assert.That (((CodePrimitiveExpression)nsArg.Value).Value, Is.EqualTo ("http://schemas.datacontract.org/2004/07/TestWCF.Model"), "#10a");
275 var itemArg = attr.FindArgument ("ItemName");
276 Assert.That (itemArg, Is.Not.Null);
277 Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("string"), "#11a");
279 type = ccu.FindType ("MyCollectionOfdouble");
280 Assert.That (type, Is.Not.Null, "#1b");
281 Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2b");
283 baseType = type.BaseTypes[0];
284 Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.List`1"), "#3b");
285 Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#4b");
286 Assert.That (baseType.TypeArguments[0].BaseType, Is.EqualTo ("System.Double"), "#5b");
288 attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
289 Assert.That (attr, Is.Not.Null, "#6b");
291 nameArg = attr.FindArgument ("Name");
292 Assert.That (nameArg, Is.Not.Null, "#7b");
293 Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollectionOfdouble"), "#8b");
295 nsArg = attr.FindArgument ("Namespace");
296 Assert.That (nsArg, Is.Not.Null, "#9b");
297 Assert.That (((CodePrimitiveExpression)nsArg.Value).Value, Is.EqualTo ("http://schemas.datacontract.org/2004/07/TestWCF.Model"), "#10b");
299 itemArg = attr.FindArgument ("ItemName");
300 Assert.That (itemArg, Is.Not.Null);
301 Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("double"), "#11b");
305 public void TestCustomCollection4 ()
307 var options = new ImportOptions ();
308 options.ReferencedCollectionTypes.Add (typeof (LinkedList<>));
310 var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
312 var type = ccu.FindType ("MyCollection");
313 Assert.That (type, Is.Not.Null, "#1a");
314 Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2a");
316 var baseType = type.BaseTypes[0];
317 Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.LinkedList`1"), "#3a");
318 Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#4a");
319 Assert.That (baseType.TypeArguments[0].BaseType, Is.EqualTo ("System.String"), "#5a");
321 var attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
322 Assert.That (attr, Is.Not.Null, "#6a");
324 var nameArg = attr.FindArgument ("Name");
325 Assert.That (nameArg, Is.Not.Null, "#7a");
326 Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollection"), "#8a");
328 var nsArg = attr.FindArgument ("Namespace");
329 Assert.That (nsArg, Is.Not.Null, "#9a");
330 Assert.That (((CodePrimitiveExpression)nsArg.Value).Value, Is.EqualTo ("http://schemas.datacontract.org/2004/07/TestWCF.Model"), "#10a");
332 var itemArg = attr.FindArgument ("ItemName");
333 Assert.That (itemArg, Is.Not.Null);
334 Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("string"), "#11a");
336 type = ccu.FindType ("MyCollectionOfdouble");
337 Assert.That (type, Is.Not.Null, "#1b");
338 Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2b");
340 baseType = type.BaseTypes[0];
341 Assert.That (baseType.BaseType, Is.EqualTo ("System.Collections.Generic.LinkedList`1"), "#3b");
342 Assert.That (baseType.TypeArguments.Count, Is.EqualTo (1), "#4b");
343 Assert.That (baseType.TypeArguments[0].BaseType, Is.EqualTo ("System.Double"), "#5b");
345 attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
346 Assert.That (attr, Is.Not.Null, "#6b");
348 nameArg = attr.FindArgument ("Name");
349 Assert.That (nameArg, Is.Not.Null, "#7b");
350 Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollectionOfdouble"), "#8b");
352 nsArg = attr.FindArgument ("Namespace");
353 Assert.That (nsArg, Is.Not.Null, "#9b");
354 Assert.That (((CodePrimitiveExpression)nsArg.Value).Value, Is.EqualTo ("http://schemas.datacontract.org/2004/07/TestWCF.Model"), "#10b");
356 itemArg = attr.FindArgument ("ItemName");
357 Assert.That (itemArg, Is.Not.Null);
358 Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("double"), "#11b");