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