Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XmlSchemaExporterTests.cs « System.Xml.Serialization « Test « System.XML « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d77dd19a5a3ba0a68b7b8b623c433bb3ce55c4e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// System.Xml.Serialization.XmlSchemaExporterTests
//
// Author:
//   Gert Driesen (drieseng@users.sourceforge.net)
//
// (C) 2005 Novell
// 

using System;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

using NUnit.Framework;

using MonoTests.System.Xml.TestClasses;

namespace MonoTests.System.XmlSerialization
{
	[TestFixture]
	public class XmlSchemaExporterTests
	{
		[Test]
		[Category ("NotWorking")] // on Mono, element is output before type
		public void ExportClass ()
		{
			XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
			XmlAttributes attr = new XmlAttributes ();
			XmlElementAttribute element = new XmlElementAttribute ();
			attr.XmlElements.Add (element);
			overrides.Add (typeof (SimpleClass), "something", attr);

			SimpleClass simple = new SimpleClass ();
			element.ElementName = "saying";
			element.IsNullable = true;
			simple.something = null;

			XmlReflectionImporter ri = new XmlReflectionImporter (overrides, "NS1");

			XmlSchemas schemas = new XmlSchemas ();
			XmlSchemaExporter sx = new XmlSchemaExporter (schemas);

			XmlTypeMapping tm = ri.ImportTypeMapping (typeof (SimpleClass));
			sx.ExportTypeMapping (tm);

			Assert.AreEqual (1, schemas.Count, "#1");

			StringWriter sw = new StringWriter ();
			schemas[0].Write (sw);

			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				"<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
				"<xs:schema xmlns:tns=\"NS1\" elementFormDefault=\"qualified\" targetNamespace=\"NS1\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
				"  <xs:element name=\"SimpleClass\" nillable=\"true\" type=\"tns:SimpleClass\" />{0}" +
				"  <xs:complexType name=\"SimpleClass\">{0}" +
				"    <xs:sequence>{0}" +
				"      <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"saying\" nillable=\"true\" type=\"xs:string\" />{0}" +
				"    </xs:sequence>{0}" +
				"  </xs:complexType>{0}" +
				"</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
		}

		[Test]
		[Category ("NotWorking")] // on Mono, element is output before type
		public void ExportEnum ()
		{
			XmlReflectionImporter ri = new XmlReflectionImporter ("NS2");

			XmlSchemas schemas = new XmlSchemas ();
			XmlSchemaExporter sx = new XmlSchemaExporter (schemas);

			XmlTypeMapping tm = ri.ImportTypeMapping (typeof (EnumDefaultValue));
			sx.ExportTypeMapping (tm);

			Assert.AreEqual (1, schemas.Count, "#1");

			StringWriter sw = new StringWriter ();
			schemas[0].Write (sw);

			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				"<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
				"<xs:schema xmlns:tns=\"NS2\" elementFormDefault=\"qualified\" targetNamespace=\"NS2\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
				"  <xs:element name=\"EnumDefaultValue\" type=\"tns:EnumDefaultValue\" />{0}" +
				"  <xs:simpleType name=\"EnumDefaultValue\">{0}" +
				"    <xs:list>{0}" +
				"      <xs:simpleType>{0}" +
				"        <xs:restriction base=\"xs:string\">{0}" +
				"          <xs:enumeration value=\"e1\" />{0}" +
				"          <xs:enumeration value=\"e2\" />{0}" +
				"          <xs:enumeration value=\"e3\" />{0}" +
				"        </xs:restriction>{0}" +
				"      </xs:simpleType>{0}" +
				"    </xs:list>{0}" +
				"  </xs:simpleType>{0}" +
				"</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
		}

		[Test]
		[Category ("NotWorking")] // on Mono, element is output before type
		public void ExportXmlSerializable ()
		{
			XmlReflectionImporter ri = new XmlReflectionImporter ("NS3");

			XmlSchemas schemas = new XmlSchemas ();
			XmlSchemaExporter sx = new XmlSchemaExporter (schemas);

			XmlTypeMapping tm = ri.ImportTypeMapping (typeof (Employee));
			sx.ExportTypeMapping (tm);

			Assert.AreEqual (1, schemas.Count, "#1");

			StringWriter sw = new StringWriter ();
			schemas[0].Write (sw);

			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				"<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
				"<xs:schema xmlns:tns=\"NS3\" elementFormDefault=\"qualified\" targetNamespace=\"NS3\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
				"  <xs:import namespace=\"http://www.w3.org/2001/XMLSchema\" />{0}" +
				"  <xs:element name=\"Employee\" nillable=\"true\">{0}" +
				"    <xs:complexType>{0}" +
				"      <xs:sequence>{0}" +
				"        <xs:element ref=\"xs:schema\" />{0}" +
				"        <xs:any />{0}" +
				"      </xs:sequence>{0}" +
				"    </xs:complexType>{0}" +
				"  </xs:element>{0}" +
				"</xs:schema>", Environment.NewLine), sw.ToString (), "#2");

			ri = new XmlReflectionImporter ("NS4");

			schemas = new XmlSchemas ();
			sx = new XmlSchemaExporter (schemas);

			tm = ri.ImportTypeMapping (typeof (EmployeeSchema));
			sx.ExportTypeMapping (tm);

			Assert.AreEqual (1, schemas.Count, "#3");

			sw = new StringWriter ();
			schemas[0].Write (sw);

			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				"<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
				"<xs:schema xmlns:tns=\"NS4\" elementFormDefault=\"qualified\" targetNamespace=\"NS4\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
#if NET_2_0
				"  <xs:import namespace=\"urn:types-devx-com\" />{0}" +
				"  <xs:element name=\"employeeRoot\" nillable=\"true\" xmlns:q1=\"urn:types-devx-com\" type=\"q1:employeeRoot\" />{0}" +
#else
				"  <xs:import namespace=\"http://www.w3.org/2001/XMLSchema\" />{0}" +
				"  <xs:element name=\"EmployeeSchema\" nillable=\"true\">{0}" +
				"    <xs:complexType>{0}" +
				"      <xs:sequence>{0}" +
				"        <xs:element ref=\"xs:schema\" />{0}" +
				"        <xs:any />{0}" +
				"      </xs:sequence>{0}" +
				"    </xs:complexType>{0}" +
				"  </xs:element>{0}" +
#endif
				"</xs:schema>", Environment.NewLine), sw.ToString (), "#4");
		}

		[Test]
		public void ExportPrimitive_Int ()
		{
			XmlReflectionImporter ri = new XmlReflectionImporter ("NSPrim1");
			XmlSchemas schemas = new XmlSchemas ();
			XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
			XmlTypeMapping tm = ri.ImportTypeMapping (typeof (int));
			sx.ExportTypeMapping (tm);

			Assert.AreEqual (1, schemas.Count, "#1");

			StringWriter sw = new StringWriter ();
			schemas[0].Write (sw);

			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				"<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
				"<xs:schema xmlns:tns=\"NSPrim1\" elementFormDefault=\"qualified\" targetNamespace=\"NSPrim1\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
				"  <xs:element name=\"int\" type=\"xs:int\" />{0}" +
				"</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
		}

		[Test]
		[Category ("NotWorking")] // on Mono, only one schema is created
		public void ExportPrimitive_Guid ()
		{
			XmlReflectionImporter ri = new XmlReflectionImporter ("NSPrimGuid");
			XmlSchemas schemas = new XmlSchemas ();
			XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
			XmlTypeMapping tm = ri.ImportTypeMapping (typeof (int));
			sx.ExportTypeMapping (tm);

			Assert.AreEqual (2, schemas.Count, "#1");

			StringWriter sw = new StringWriter ();
			schemas[0].Write (sw);

			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				"<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
				"<xs:schema xmlns:tns=\"NSPrimGuid\" elementFormDefault=\"qualified\" targetNamespace=\"NSPrimGuid\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
				"  <xs:import namespace=\"http://microsoft.com/wsdl/types/\" />{0}" +
				"  <xs:element name=\"guid\" xmlns:q1=\"http://microsoft.com/wsdl/types/\" type=\"q1:guid\" />{0}" +
				"</xs:schema>", Environment.NewLine), sw.ToString (), "#2");

			sw = new StringWriter ();
			schemas[1].Write (sw);

			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
				"<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
				"<xs:schema xmlns:tns=\"http://microsoft.com/wsdl/types/\" elementFormDefault=\"qualified\" targetNamespace=\"http://microsoft.com/wsdl/types/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
				"  <xs:simpleType name=\"guid\">{0}" +
				"    <xs:restriction base=\"xs:string\">{0}" +
				"      <xs:pattern value=\"[0-9a-fA-F]{{8}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{12}}\" />{0}" +
				"    </xs:restriction>{0}" +
				"  </xs:simpleType>{0}" +
				"</xs:schema>", Environment.NewLine), sw.ToString (), "#3");
		}

		public class Employee : IXmlSerializable
		{
			private string _firstName;
			private string _lastName;
			private string _address;

			public XmlSchema GetSchema ()
			{
				return null;
			}

			public void WriteXml (XmlWriter writer)
			{
				writer.WriteStartElement ("employee", "urn:devx-com");
				writer.WriteAttributeString ("firstName", _firstName);
				writer.WriteAttributeString ("lastName", _lastName);
				writer.WriteAttributeString ("address", _address);
				writer.WriteEndElement ();
			}

			public void ReadXml (XmlReader reader)
			{
				XmlNodeType type = reader.MoveToContent ();
				if (type == XmlNodeType.Element && reader.LocalName == "employee") {
					_firstName = reader["firstName"];
					_lastName = reader["lastName"];
					_address = reader["address"];
				}
			}
		}

#if NET_2_0
		[XmlSchemaProvider ("CreateEmployeeSchema")]
#endif
		public class EmployeeSchema : Employee
		{
#if NET_2_0
			public static XmlQualifiedName CreateEmployeeSchema (XmlSchemaSet schemaSet)
			{
				XmlSchema schema = new XmlSchema();
				schema.Id = "EmployeeSchema";
				schema.TargetNamespace = "urn:types-devx-com";
			
				XmlSchemaComplexType type = new XmlSchemaComplexType();
				type.Name = "employeeRoot";
				XmlSchemaAttribute firstNameAttr = new XmlSchemaAttribute();
				firstNameAttr.Name = "firstName";
				type.Attributes.Add(firstNameAttr);

				XmlSchemaAttribute lastNameAttr = new XmlSchemaAttribute();
				lastNameAttr.Name = "lastName";
				type.Attributes.Add(lastNameAttr);

				XmlSchemaAttribute addressAttr = new XmlSchemaAttribute();
				addressAttr.Name = "address";
				type.Attributes.Add(addressAttr);

				XmlSchemaElement employeeElement = new XmlSchemaElement();
				employeeElement.Name = "employee";
				XmlQualifiedName name = new XmlQualifiedName("employeeRoot", "urn:types-devx-com");
				employeeElement.SchemaTypeName = name;

				schema.Items.Add(type);
				schema.Items.Add(employeeElement);
				schemaSet.Add(schema);
				return name;
			}
#endif
		}
	}
}