Merge pull request #3796 from ntherning/windows-backend-for-MemoryMappedFile
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Runtime.Serialization / XsdDataContractImporterTest2.cs
1 //
2 // XsdDataContractImporterTest2.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2012 Xamarin, Inc.
8 //
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:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
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
25 // THE SOFTWARE.
26
27 #if !MOBILE
28
29 using System;
30 using System.CodeDom;
31 using System.CodeDom.Compiler;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Collections.ObjectModel;
35 using System.IO;
36 using System.Linq;
37 using System.Runtime.Serialization;
38 using System.ServiceModel.Description;
39 using System.Web.Services.Discovery;
40 using System.Xml;
41 using System.Xml.Schema;
42 using System.Xml.Serialization;
43 using Microsoft.CSharp;
44 using NUnit.Framework;
45 using NUnit.Framework.Constraints;
46
47 using QName = System.Xml.XmlQualifiedName;
48
49 namespace MonoTests.System.Runtime.Serialization
50 {
51         [TestFixture]
52         public class XsdDataContractImporterTest2
53         {
54                 MetadataSet collectionsMetadata;
55                 MetadataSet customCollectionsMetadata;
56
57                 [SetUp]
58                 public void Setup ()
59                 {
60                         collectionsMetadata = WsdlHelper.GetMetadataSet ("collections.wsdl");
61                         customCollectionsMetadata = WsdlHelper.GetMetadataSet ("custom-collections.wsdl");
62                 }
63                 
64                 [Test]
65                 public void TestSimpleList ()
66                 {
67                         var options = new ImportOptions ();
68                         
69                         var ccu = WsdlHelper.Import (collectionsMetadata, options);
70                         
71                         var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList");
72                         Assert.That (method, Is.Not.Null, "#1");
73                         Assert.That (method.ReturnType, Is.Not.Null, "#2");
74                         
75                         Assert.That (method.ReturnType.ArrayRank, Is.EqualTo (1), "#3");
76                         Assert.That (method.ReturnType.BaseType, Is.EqualTo ("System.Int32"), "#4");
77                 }
78                 
79                 [Test]
80                 public void TestSimpleList2 ()
81                 {
82                         var options = new ImportOptions ();
83                         options.ReferencedCollectionTypes.Add (typeof(LinkedList<>));
84                         
85                         var ccu = WsdlHelper.Import (collectionsMetadata, options);
86                         
87                         var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList");
88                         Assert.That (method, Is.Not.Null, "#1");
89                         
90                         var ret = method.ReturnType;
91                         Assert.That (ret, Is.Not.Null, "#2");
92                         
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");
97                 }
98
99                 [Test]
100                 public void TestSimpleList3 ()
101                 {
102                         var options = new ImportOptions ();
103                         options.ReferencedCollectionTypes.Add (typeof (Dictionary<,>));
104                         options.ReferencedCollectionTypes.Add (typeof (ObservableCollection<>));
105                         
106                         var ccu = WsdlHelper.Import (collectionsMetadata, options);
107                         
108                         var method = ccu.FindMethod ("MyServiceClient", "GetSimpleList");
109                         Assert.That (method, Is.Not.Null, "#1");
110                         
111                         var ret = method.ReturnType;
112                         Assert.That (ret, Is.Not.Null, "#2");
113                         
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");
118                 }
119                 
120                 [Test]
121                 public void TestListOfFoo ()
122                 {
123                         var options = new ImportOptions ();
124                         options.ReferencedCollectionTypes.Add (typeof (List<>));
125                         
126                         var ccu = WsdlHelper.Import (collectionsMetadata, options);
127                         
128                         var method = ccu.FindMethod ("MyServiceClient", "GetListOfFoo");
129                         Assert.That (method, Is.Not.Null, "#1");
130                         
131                         var ret = method.ReturnType;
132                         Assert.That (ret, Is.Not.Null, "#2");
133                         
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");
138                 }
139                 
140                 [Test]
141                 public void TestListOfStringArray ()
142                 {
143                         var options = new ImportOptions ();
144                         options.ReferencedCollectionTypes.Add (typeof (List<>));
145                         
146                         var ccu = WsdlHelper.Import (collectionsMetadata, options);
147                         
148                         var method = ccu.FindMethod ("MyServiceClient", "GetListOfStringArray");
149                         Assert.That (method, Is.Not.Null, "#1");
150                         
151                         var ret = method.ReturnType;
152                         Assert.That (ret, Is.Not.Null, "#2");
153                         
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");
157                         
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");
162                 }
163                 
164                 [Test]
165                 public void TestSimpleDictionary ()
166                 {
167                         var options = new ImportOptions ();
168                         options.ReferencedCollectionTypes.Add (typeof (List<>));
169                         
170                         var ccu = WsdlHelper.Import (collectionsMetadata, options);
171                         
172                         var method = ccu.FindMethod ("MyServiceClient", "GetSimpleDictionary");
173                         Assert.That (method, Is.Not.Null, "#1");
174                         
175                         var ret = method.ReturnType;
176                         Assert.That (ret, Is.Not.Null, "#2");
177                         
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");
181                         
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");
186                 }
187                 
188                 [Test]
189                 public void TestSimpleDictionary2 ()
190                 {
191                         var options = new ImportOptions ();
192                         options.ReferencedCollectionTypes.Add (typeof (SortedList<,>));
193                         
194                         var ccu = WsdlHelper.Import (collectionsMetadata, options);
195                         
196                         var method = ccu.FindMethod ("MyServiceClient", "GetSimpleDictionary");
197                         Assert.That (method, Is.Not.Null, "#1");
198                         
199                         var ret = method.ReturnType;
200                         Assert.That (ret, Is.Not.Null, "#2");
201                         
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");
205                         
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");
210                 }
211
212                 [Test]
213                 public void TestCustomCollection ()
214                 {
215                         var options = new ImportOptions ();
216                         
217                         var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
218                         
219                         var method = ccu.FindMethod ("MyServiceClient", "GetCustomCollection");
220                         Assert.That (method, Is.Not.Null, "#1");
221
222                         var ret = method.ReturnType;
223                         Assert.That (ret, Is.Not.Null, "#2");
224                         
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");
228                 }
229
230                 [Test]
231                 public void TestCustomCollection2 ()
232                 {
233                         var options = new ImportOptions ();
234
235                         var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
236                         
237                         var method = ccu.FindMethod ("MyServiceClient", "GetCustomCollection2");
238                         Assert.That (method, Is.Not.Null, "#1");
239                         
240                         var ret = method.ReturnType;
241                         Assert.That (ret, Is.Not.Null, "#2");
242                         
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");
246                 }
247
248                 [Test]
249                 public void TestCustomCollection3 ()
250                 {
251                         var options = new ImportOptions ();
252
253                         var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
254                         
255                         var type = ccu.FindType ("MyCollection");
256                         Assert.That (type, Is.Not.Null, "#1a");
257                         Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2a");
258                         
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");
263
264                         var attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
265                         Assert.That (attr, Is.Not.Null, "#6a");
266
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");
270
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");
274                         
275                         var itemArg = attr.FindArgument ("ItemName");
276                         Assert.That (itemArg, Is.Not.Null);
277                         Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("string"), "#11a");
278
279                         type = ccu.FindType ("MyCollectionOfdouble");
280                         Assert.That (type, Is.Not.Null, "#1b");
281                         Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2b");
282
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");
287                         
288                         attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
289                         Assert.That (attr, Is.Not.Null, "#6b");
290                         
291                         nameArg = attr.FindArgument ("Name");
292                         Assert.That (nameArg, Is.Not.Null, "#7b");
293                         Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollectionOfdouble"), "#8b");
294                         
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");
298                         
299                         itemArg = attr.FindArgument ("ItemName");
300                         Assert.That (itemArg, Is.Not.Null);
301                         Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("double"), "#11b");
302                 }
303
304                 [Test]
305                 public void TestCustomCollection4 ()
306                 {
307                         var options = new ImportOptions ();
308                         options.ReferencedCollectionTypes.Add (typeof (LinkedList<>));
309
310                         var ccu = WsdlHelper.Import (customCollectionsMetadata, options);
311                         
312                         var type = ccu.FindType ("MyCollection");
313                         Assert.That (type, Is.Not.Null, "#1a");
314                         Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2a");
315                         
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");
320                         
321                         var attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
322                         Assert.That (attr, Is.Not.Null, "#6a");
323                         
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");
327                         
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");
331                         
332                         var itemArg = attr.FindArgument ("ItemName");
333                         Assert.That (itemArg, Is.Not.Null);
334                         Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("string"), "#11a");
335                         
336                         type = ccu.FindType ("MyCollectionOfdouble");
337                         Assert.That (type, Is.Not.Null, "#1b");
338                         Assert.That (type.BaseTypes.Count, Is.EqualTo (1), "#2b");
339                         
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");
344                         
345                         attr = type.FindAttribute ("System.Runtime.Serialization.CollectionDataContractAttribute");
346                         Assert.That (attr, Is.Not.Null, "#6b");
347                         
348                         nameArg = attr.FindArgument ("Name");
349                         Assert.That (nameArg, Is.Not.Null, "#7b");
350                         Assert.That (((CodePrimitiveExpression)nameArg.Value).Value, Is.EqualTo ("MyCollectionOfdouble"), "#8b");
351                         
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");
355                         
356                         itemArg = attr.FindArgument ("ItemName");
357                         Assert.That (itemArg, Is.Not.Null);
358                         Assert.That (((CodePrimitiveExpression)itemArg.Value).Value, Is.EqualTo ("double"), "#11b");
359                 }
360         }
361 }
362
363 #endif