Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Runtime.Serialization / XsdDataContractExporterTest2.cs
1 //
2 // XsdDataContractExporterTest2.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 using System;
27 using System.CodeDom;
28 using System.CodeDom.Compiler;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.IO;
33 using System.Linq;
34 using System.Runtime.Serialization;
35 using System.ServiceModel;
36 using System.ServiceModel.Description;
37 using System.Web.Services.Discovery;
38 using System.Xml;
39 using System.Xml.Schema;
40 using System.Xml.Serialization;
41 using Microsoft.CSharp;
42 using NUnit.Framework;
43 using NUnit.Framework.Constraints;
44 using NUnit.Framework.SyntaxHelpers;
45
46 using QName = System.Xml.XmlQualifiedName;
47
48 namespace MonoTests.System.Runtime.Serialization
49 {
50         [TestFixture]
51         public class XsdDataContractExporterTest2
52         {
53                 internal const string MSArraysNamespace =
54                         "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
55
56                 [Test]
57                 public void ExportList ()
58                 {
59                         var exporter = new XsdDataContractExporter ();
60                         Assert.That (exporter.CanExport (typeof(MyService)), Is.True, "#1");
61                         exporter.Export (typeof(MyService));
62
63                         var typeName = exporter.GetSchemaTypeName (typeof(MyService));
64                         var type = exporter.Schemas.GlobalTypes [typeName];
65
66                         Assert.That (type, Is.Not.Null, "#2");
67                         Assert.That (type, Is.InstanceOfType (typeof (XmlSchemaComplexType)), "#3");
68
69                         var complex = (XmlSchemaComplexType)type;
70                         Assert.That (complex.Annotation, Is.Null, "#4");
71
72                         var sequence = complex.Particle as XmlSchemaSequence;
73                         Assert.That (sequence, Is.Not.Null, "#5");
74                         Assert.That (sequence.Items.Count, Is.EqualTo (3), "#5a");
75                         Assert.That (sequence.Annotation, Is.Null, "#5b");
76                         Assert.That (sequence.MinOccursString, Is.Null, "#5c");
77                         Assert.That (sequence.MaxOccursString, Is.Null, "#5d");
78
79                         var list = GetElement (sequence, "list");
80                         Assert.That (list, Is.Not.Null, "#6");
81                         Assert.That (list.Annotation, Is.Null, "#6a");
82                         Assert.That (list.Name, Is.EqualTo ("list"), "#6b");
83                         Assert.That (list.ElementSchemaType, Is.InstanceOfType (typeof (XmlSchemaComplexType)), "#6c");
84
85                         var listElement = (XmlSchemaComplexType)list.ElementSchemaType;
86                         Assert.That (listElement.QualifiedName.Namespace, Is.EqualTo (MSArraysNamespace), "#6d");
87                         Assert.That (listElement.QualifiedName.Name, Is.EqualTo ("ArrayOfint"), "#6e");
88
89                         Assert.That (listElement.Particle, Is.InstanceOfType (typeof(XmlSchemaSequence)), "#7");
90                         var listSeq = (XmlSchemaSequence)listElement.Particle;
91                         Assert.That (listSeq.Items.Count, Is.EqualTo (1), "#7b");
92                         Assert.That (listSeq.Items[0], Is.InstanceOfType (typeof(XmlSchemaElement)), "#7c");
93                         Assert.That (listSeq.Annotation, Is.Null, "#7d");
94
95                         var listSeqElement = (XmlSchemaElement)listSeq.Items[0];
96                         Assert.That (listSeqElement.MaxOccursString, Is.EqualTo ("unbounded"), "#7e");
97                         Assert.That (listSeqElement.MinOccursString, Is.EqualTo ("0"), "#7f");
98
99                         var dict = GetElement (sequence, "dictionary");
100                         Assert.That (dict, Is.Not.Null, "#8");
101                         Assert.That (dict.Annotation, Is.Null, "#8a");
102                         Assert.That (dict.Name, Is.EqualTo ("dictionary"), "#8b");
103                         Assert.That (dict.ElementSchemaType, Is.InstanceOfType (typeof (XmlSchemaComplexType)), "#8c");
104                         
105                         var dictElement = (XmlSchemaComplexType)dict.ElementSchemaType;
106                         Assert.That (dictElement.QualifiedName.Namespace, Is.EqualTo (MSArraysNamespace), "#8d");
107                         Assert.That (dictElement.QualifiedName.Name, Is.EqualTo ("ArrayOfKeyValueOfstringdouble"), "#8e");
108
109                         Assert.That (dictElement.Particle, Is.InstanceOfType (typeof(XmlSchemaSequence)), "#9");
110                         var dictSeq = (XmlSchemaSequence)dictElement.Particle;
111                         Assert.That (dictSeq.Items.Count, Is.EqualTo (1), "#9b");
112                         Assert.That (dictSeq.Items[0], Is.InstanceOfType (typeof(XmlSchemaElement)), "#9c");
113                         Assert.That (dictSeq.Annotation, Is.Null, "#9d");
114                         
115                         var dictSeqElement = (XmlSchemaElement)dictSeq.Items[0];
116                         Assert.That (listSeqElement.MaxOccursString, Is.EqualTo ("unbounded"), "#9e");
117                         Assert.That (listSeqElement.MinOccursString, Is.EqualTo ("0"), "#9f");
118
119
120                         var custom = GetElement (sequence, "customCollection");
121                         Assert.That (custom, Is.Not.Null, "#10");
122                         Assert.That (custom.Annotation, Is.Null, "#10a");
123                         Assert.That (custom.Name, Is.EqualTo ("customCollection"), "#10b");
124                         Assert.That (custom.ElementSchemaType, Is.InstanceOfType (typeof (XmlSchemaComplexType)), "#10c");
125                         
126                         var customElement = (XmlSchemaComplexType)custom.ElementSchemaType;
127                         var customEQN = customElement.QualifiedName;
128                         Assert.That (customEQN.Namespace, Is.EqualTo (typeName.Namespace), "#10d");
129                         Assert.That (customEQN.Name.StartsWith ("XsdDataContractExporterTest2.MyCollectionOfstring", StringComparison.InvariantCultureIgnoreCase),
130                                      Is.True, "#10e");
131
132                         Assert.That (customElement.Particle, Is.InstanceOfType (typeof(XmlSchemaSequence)), "#11");
133                         var customSeq = (XmlSchemaSequence)customElement.Particle;
134                         Assert.That (customSeq.Items.Count, Is.EqualTo (1), "#11b");
135                         Assert.That (customSeq.Items[0], Is.InstanceOfType (typeof(XmlSchemaElement)), "#11c");
136                         Assert.That (customSeq.Annotation, Is.Null, "#11d");
137                         
138                         var customSeqElement = (XmlSchemaElement)customSeq.Items[0];
139                         Assert.That (customSeqElement.MaxOccursString, Is.EqualTo ("unbounded"), "#11e");
140                         Assert.That (customSeqElement.MinOccursString, Is.EqualTo ("0"), "#11f");
141                 }
142
143                 static XmlSchemaElement GetElement (XmlSchemaSequence sequence, string name)
144                 {
145                         foreach (XmlSchemaElement item in sequence.Items) {
146                                 if (item.Name.Equals (name))
147                                         return item;
148                         }
149
150                         return null;
151                 }
152
153                 [ServiceContract]
154                 public class MyService
155                 {
156                         [DataMember]
157                         public List<int> list;
158
159                         [DataMember]
160                         public Dictionary<string,double> dictionary;
161
162                         [DataMember]
163                         public MyCollection<string> customCollection;
164                 }
165
166                 [CollectionDataContract]
167                 public class MyCollection<T> : List<T>
168                 {
169                 }
170         }
171 }