XmlSchemaExporterTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. //
  2. // System.Xml.Serialization.XmlSchemaExporterTests
  3. //
  4. // Author:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (C) 2005 Novell
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. using System.Xml.Serialization;
  16. using NUnit.Framework;
  17. using MonoTests.System.Xml.TestClasses;
  18. namespace MonoTests.System.XmlSerialization
  19. {
  20. [TestFixture]
  21. public class XmlSchemaExporterTests
  22. {
  23. [Test]
  24. [Category ("NotWorking")] // on Mono, element is output before type
  25. public void ExportStruct ()
  26. {
  27. XmlReflectionImporter ri = new XmlReflectionImporter ("NSTimeSpan");
  28. XmlSchemas schemas = new XmlSchemas ();
  29. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  30. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (TimeSpan));
  31. sx.ExportTypeMapping (tm);
  32. Assert.AreEqual (1, schemas.Count, "#1");
  33. StringWriter sw = new StringWriter ();
  34. schemas[0].Write (sw);
  35. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  36. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  37. "<xs:schema xmlns:tns=\"NSTimeSpan\" elementFormDefault=\"qualified\" targetNamespace=\"NSTimeSpan\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  38. " <xs:element name=\"TimeSpan\" type=\"tns:TimeSpan\" />{0}" +
  39. " <xs:complexType name=\"TimeSpan\" />{0}" +
  40. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  41. }
  42. [Test]
  43. [Category ("NotWorking")] // on Mono, element is output before type
  44. public void ExportStruct_Array ()
  45. {
  46. XmlReflectionImporter ri = new XmlReflectionImporter ("NSTimeSpanArray");
  47. XmlSchemas schemas = new XmlSchemas ();
  48. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  49. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (TimeSpan[]));
  50. sx.ExportTypeMapping (tm);
  51. Assert.AreEqual (1, schemas.Count, "#1");
  52. StringWriter sw = new StringWriter ();
  53. schemas[0].Write (sw);
  54. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  55. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  56. "<xs:schema xmlns:tns=\"NSTimeSpanArray\" elementFormDefault=\"qualified\" targetNamespace=\"NSTimeSpanArray\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  57. " <xs:element name=\"ArrayOfTimeSpan\" nillable=\"true\" type=\"tns:ArrayOfTimeSpan\" />{0}" +
  58. " <xs:complexType name=\"ArrayOfTimeSpan\">{0}" +
  59. " <xs:sequence>{0}" +
  60. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"TimeSpan\" type=\"tns:TimeSpan\" />{0}" +
  61. " </xs:sequence>{0}" +
  62. " </xs:complexType>{0}" +
  63. " <xs:complexType name=\"TimeSpan\" />{0}" +
  64. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  65. }
  66. [Test]
  67. [Category ("NotWorking")] // on Mono, element is output before type
  68. public void ExportClass ()
  69. {
  70. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  71. XmlAttributes attr = new XmlAttributes ();
  72. XmlElementAttribute element = new XmlElementAttribute ();
  73. element.ElementName = "saying";
  74. element.IsNullable = true;
  75. attr.XmlElements.Add (element);
  76. overrides.Add (typeof (SimpleClass), "something", attr);
  77. XmlReflectionImporter ri = new XmlReflectionImporter (overrides, "NS1");
  78. XmlSchemas schemas = new XmlSchemas ();
  79. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  80. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (SimpleClass));
  81. sx.ExportTypeMapping (tm);
  82. Assert.AreEqual (1, schemas.Count, "#1");
  83. StringWriter sw = new StringWriter ();
  84. schemas[0].Write (sw);
  85. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  86. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  87. "<xs:schema xmlns:tns=\"NS1\" elementFormDefault=\"qualified\" targetNamespace=\"NS1\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  88. " <xs:element name=\"SimpleClass\" nillable=\"true\" type=\"tns:SimpleClass\" />{0}" +
  89. " <xs:complexType name=\"SimpleClass\">{0}" +
  90. " <xs:sequence>{0}" +
  91. " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"saying\" nillable=\"true\" type=\"xs:string\" />{0}" +
  92. " </xs:sequence>{0}" +
  93. " </xs:complexType>{0}" +
  94. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  95. }
  96. [Test]
  97. [Category ("NotWorking")]
  98. [ExpectedException (typeof (InvalidOperationException))] // Cannot use wildcards at the top level of a schema.
  99. public void ExportClass_XmlElement ()
  100. {
  101. XmlReflectionImporter ri = new XmlReflectionImporter ("NS1");
  102. XmlSchemas schemas = new XmlSchemas ();
  103. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  104. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (XmlElement));
  105. sx.ExportTypeMapping (tm);
  106. }
  107. [Test]
  108. [Category ("NotWorking")] // on Mono, element is output before type
  109. public void ExportClass_Array ()
  110. {
  111. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  112. XmlAttributes attr = new XmlAttributes ();
  113. XmlElementAttribute element = new XmlElementAttribute ();
  114. element.ElementName = "saying";
  115. element.IsNullable = true;
  116. attr.XmlElements.Add (element);
  117. overrides.Add (typeof (SimpleClass), "something", attr);
  118. XmlReflectionImporter ri = new XmlReflectionImporter (overrides, "NSSimpleClassArray");
  119. XmlSchemas schemas = new XmlSchemas ();
  120. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  121. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (SimpleClass[]));
  122. sx.ExportTypeMapping (tm);
  123. Assert.AreEqual (1, schemas.Count, "#1");
  124. StringWriter sw = new StringWriter ();
  125. schemas[0].Write (sw);
  126. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  127. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  128. "<xs:schema xmlns:tns=\"NSSimpleClassArray\" elementFormDefault=\"qualified\" targetNamespace=\"NSSimpleClassArray\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  129. " <xs:element name=\"ArrayOfSimpleClass\" nillable=\"true\" type=\"tns:ArrayOfSimpleClass\" />{0}" +
  130. " <xs:complexType name=\"ArrayOfSimpleClass\">{0}" +
  131. " <xs:sequence>{0}" +
  132. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"SimpleClass\" nillable=\"true\" type=\"tns:SimpleClass\" />{0}" +
  133. " </xs:sequence>{0}" +
  134. " </xs:complexType>{0}" +
  135. " <xs:complexType name=\"SimpleClass\">{0}" +
  136. " <xs:sequence>{0}" +
  137. " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"saying\" nillable=\"true\" type=\"xs:string\" />{0}" +
  138. " </xs:sequence>{0}" +
  139. " </xs:complexType>{0}" +
  140. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  141. }
  142. [Test]
  143. [Category ("NotWorking")] // on Mono, element is output before type
  144. public void ExportEnum ()
  145. {
  146. XmlReflectionImporter ri = new XmlReflectionImporter ("NSEnumDefaultValue");
  147. XmlSchemas schemas = new XmlSchemas ();
  148. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  149. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (EnumDefaultValue));
  150. sx.ExportTypeMapping (tm);
  151. Assert.AreEqual (1, schemas.Count, "#1");
  152. StringWriter sw = new StringWriter ();
  153. schemas[0].Write (sw);
  154. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  155. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  156. "<xs:schema xmlns:tns=\"NSEnumDefaultValue\" elementFormDefault=\"qualified\" targetNamespace=\"NSEnumDefaultValue\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  157. " <xs:element name=\"EnumDefaultValue\" type=\"tns:EnumDefaultValue\" />{0}" +
  158. " <xs:simpleType name=\"EnumDefaultValue\">{0}" +
  159. " <xs:list>{0}" +
  160. " <xs:simpleType>{0}" +
  161. " <xs:restriction base=\"xs:string\">{0}" +
  162. " <xs:enumeration value=\"e1\" />{0}" +
  163. " <xs:enumeration value=\"e2\" />{0}" +
  164. " <xs:enumeration value=\"e3\" />{0}" +
  165. " </xs:restriction>{0}" +
  166. " </xs:simpleType>{0}" +
  167. " </xs:list>{0}" +
  168. " </xs:simpleType>{0}" +
  169. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  170. ri = new XmlReflectionImporter ("NSEnumDefaultValueNF");
  171. schemas = new XmlSchemas ();
  172. sx = new XmlSchemaExporter (schemas);
  173. tm = ri.ImportTypeMapping (typeof (EnumDefaultValueNF));
  174. sx.ExportTypeMapping (tm);
  175. Assert.AreEqual (1, schemas.Count, "#3");
  176. sw = new StringWriter ();
  177. schemas[0].Write (sw);
  178. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  179. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  180. "<xs:schema xmlns:tns=\"NSEnumDefaultValueNF\" elementFormDefault=\"qualified\" targetNamespace=\"NSEnumDefaultValueNF\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  181. " <xs:element name=\"EnumDefaultValueNF\" type=\"tns:EnumDefaultValueNF\" />{0}" +
  182. " <xs:simpleType name=\"EnumDefaultValueNF\">{0}" +
  183. " <xs:restriction base=\"xs:string\">{0}" +
  184. " <xs:enumeration value=\"e1\" />{0}" +
  185. " <xs:enumeration value=\"e2\" />{0}" +
  186. " <xs:enumeration value=\"e3\" />{0}" +
  187. " </xs:restriction>{0}" +
  188. " </xs:simpleType>{0}" +
  189. "</xs:schema>", Environment.NewLine), sw.ToString (), "#4");
  190. }
  191. [Test]
  192. [Category ("NotWorking")] // on Mono, element is output before type
  193. public void ExportXmlSerializable ()
  194. {
  195. XmlReflectionImporter ri = new XmlReflectionImporter ("NS3");
  196. XmlSchemas schemas = new XmlSchemas ();
  197. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  198. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (Employee));
  199. sx.ExportTypeMapping (tm);
  200. Assert.AreEqual (1, schemas.Count, "#1");
  201. StringWriter sw = new StringWriter ();
  202. schemas[0].Write (sw);
  203. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  204. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  205. "<xs:schema xmlns:tns=\"NS3\" elementFormDefault=\"qualified\" targetNamespace=\"NS3\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  206. " <xs:import namespace=\"http://www.w3.org/2001/XMLSchema\" />{0}" +
  207. " <xs:element name=\"Employee\" nillable=\"true\">{0}" +
  208. " <xs:complexType>{0}" +
  209. " <xs:sequence>{0}" +
  210. " <xs:element ref=\"xs:schema\" />{0}" +
  211. " <xs:any />{0}" +
  212. " </xs:sequence>{0}" +
  213. " </xs:complexType>{0}" +
  214. " </xs:element>{0}" +
  215. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  216. ri = new XmlReflectionImporter ("NS4");
  217. schemas = new XmlSchemas ();
  218. sx = new XmlSchemaExporter (schemas);
  219. tm = ri.ImportTypeMapping (typeof (EmployeeSchema));
  220. sx.ExportTypeMapping (tm);
  221. Assert.AreEqual (1, schemas.Count, "#3");
  222. sw = new StringWriter ();
  223. schemas[0].Write (sw);
  224. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  225. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  226. "<xs:schema xmlns:tns=\"NS4\" elementFormDefault=\"qualified\" targetNamespace=\"NS4\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  227. #if NET_2_0
  228. " <xs:import namespace=\"urn:types-devx-com\" />{0}" +
  229. " <xs:element name=\"employeeRoot\" nillable=\"true\" xmlns:q1=\"urn:types-devx-com\" type=\"q1:employeeRoot\" />{0}" +
  230. #else
  231. " <xs:import namespace=\"http://www.w3.org/2001/XMLSchema\" />{0}" +
  232. " <xs:element name=\"EmployeeSchema\" nillable=\"true\">{0}" +
  233. " <xs:complexType>{0}" +
  234. " <xs:sequence>{0}" +
  235. " <xs:element ref=\"xs:schema\" />{0}" +
  236. " <xs:any />{0}" +
  237. " </xs:sequence>{0}" +
  238. " </xs:complexType>{0}" +
  239. " </xs:element>{0}" +
  240. #endif
  241. "</xs:schema>", Environment.NewLine), sw.ToString (), "#4");
  242. }
  243. [Test]
  244. [Category ("NotWorking")] // bug #77117
  245. public void ExportXsdPrimitive ()
  246. {
  247. ArrayList types = new ArrayList ();
  248. types.Add (new TypeDescription (typeof (byte), true, "unsignedByte", "Byte"));
  249. types.Add (new TypeDescription (typeof (sbyte), true, "byte", "Byte"));
  250. types.Add (new TypeDescription (typeof (bool), true, "boolean", "Boolean"));
  251. types.Add (new TypeDescription (typeof (short), true, "short", "Short"));
  252. types.Add (new TypeDescription (typeof (int), true, "int", "Int"));
  253. types.Add (new TypeDescription (typeof (long), true, "long", "Long"));
  254. types.Add (new TypeDescription (typeof (float), true, "float", "Float"));
  255. types.Add (new TypeDescription (typeof (double), true, "double", "Double"));
  256. types.Add (new TypeDescription (typeof (decimal), true, "decimal", "Decimal"));
  257. types.Add (new TypeDescription (typeof (ushort), true, "unsignedShort", "UnsignedShort"));
  258. types.Add (new TypeDescription (typeof (uint), true, "unsignedInt", "UnsignedInt"));
  259. types.Add (new TypeDescription (typeof (ulong), true, "unsignedLong", "UnsignedLong"));
  260. types.Add (new TypeDescription (typeof (DateTime), true, "dateTime", "DateTime"));
  261. #if NET_2_0
  262. types.Add (new TypeDescription (typeof (XmlQualifiedName), true, "QName", "QName", true));
  263. #else
  264. types.Add (new TypeDescription (typeof (XmlQualifiedName), true, "QName", "QName"));
  265. #endif
  266. types.Add (new TypeDescription (typeof (string), true, "string", "String", true));
  267. foreach (TypeDescription typeDesc in types) {
  268. XmlReflectionImporter ri = new XmlReflectionImporter (typeDesc.Type.Name);
  269. XmlSchemas schemas = new XmlSchemas ();
  270. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  271. XmlTypeMapping tm = ri.ImportTypeMapping (typeDesc.Type);
  272. sx.ExportTypeMapping (tm);
  273. Assert.AreEqual (1, schemas.Count, typeDesc.Type.FullName + "#1");
  274. StringWriter sw = new StringWriter ();
  275. schemas[0].Write (sw);
  276. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  277. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  278. "<xs:schema xmlns:tns=\"{1}\" elementFormDefault=\"qualified\" targetNamespace=\"{1}\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  279. " <xs:element name=\"{2}\" {3}type=\"xs:{2}\" />{0}" +
  280. "</xs:schema>", Environment.NewLine, typeDesc.Type.Name, typeDesc.XmlType, typeDesc.IsNillable ? "nillable=\"true\" " : ""),
  281. sw.ToString (), typeDesc.Type.FullName + "#2");
  282. }
  283. }
  284. [Test]
  285. [Category ("NotWorking")] // bug #77117
  286. public void ExportXsdPrimitive_Object ()
  287. {
  288. XmlReflectionImporter ri = new XmlReflectionImporter ("NSAnyType");
  289. XmlSchemas schemas = new XmlSchemas ();
  290. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  291. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (object));
  292. sx.ExportTypeMapping (tm);
  293. Assert.AreEqual (1, schemas.Count, "#1");
  294. StringWriter sw = new StringWriter ();
  295. schemas[0].Write (sw);
  296. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  297. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  298. "<xs:schema xmlns:tns=\"NSAnyType\" elementFormDefault=\"qualified\" targetNamespace=\"NSAnyType\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  299. " <xs:element name=\"anyType\" nillable=\"true\" />{0}" +
  300. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  301. }
  302. [Test]
  303. [Category ("NotWorking")] // bug #77117
  304. public void ExportXsdPrimitive_ByteArray ()
  305. {
  306. XmlReflectionImporter ri = new XmlReflectionImporter ("ByteArray");
  307. XmlSchemas schemas = new XmlSchemas ();
  308. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  309. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (byte[]));
  310. sx.ExportTypeMapping (tm);
  311. Assert.AreEqual (1, schemas.Count, "#1");
  312. StringWriter sw = new StringWriter ();
  313. schemas[0].Write (sw);
  314. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  315. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  316. "<xs:schema xmlns:tns=\"ByteArray\" elementFormDefault=\"qualified\" targetNamespace=\"ByteArray\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  317. " <xs:element name=\"base64Binary\" nillable=\"true\" type=\"xs:base64Binary\" />{0}" +
  318. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  319. }
  320. [Test]
  321. [Category ("NotWorking")] // bug #77117
  322. public void ExportXsdPrimitive_Arrays ()
  323. {
  324. ArrayList types = new ArrayList ();
  325. types.Add (new TypeDescription (typeof (sbyte[]), true, "byte", "Byte"));
  326. types.Add (new TypeDescription (typeof (bool[]), true, "boolean", "Boolean"));
  327. types.Add (new TypeDescription (typeof (short[]), true, "short", "Short"));
  328. types.Add (new TypeDescription (typeof (int[]), true, "int", "Int"));
  329. types.Add (new TypeDescription (typeof (long[]), true, "long", "Long"));
  330. types.Add (new TypeDescription (typeof (float[]), true, "float", "Float"));
  331. types.Add (new TypeDescription (typeof (double[]), true, "double", "Double"));
  332. types.Add (new TypeDescription (typeof (decimal[]), true, "decimal", "Decimal"));
  333. types.Add (new TypeDescription (typeof (ushort[]), true, "unsignedShort", "UnsignedShort"));
  334. types.Add (new TypeDescription (typeof (uint[]), true, "unsignedInt", "UnsignedInt"));
  335. types.Add (new TypeDescription (typeof (ulong[]), true, "unsignedLong", "UnsignedLong"));
  336. types.Add (new TypeDescription (typeof (DateTime[]), true, "dateTime", "DateTime"));
  337. #if NET_2_0
  338. types.Add (new TypeDescription (typeof (XmlQualifiedName[]), true, "QName", "QName", true));
  339. #else
  340. types.Add (new TypeDescription (typeof (XmlQualifiedName[]), true, "QName", "QName"));
  341. #endif
  342. types.Add (new TypeDescription (typeof (string[]), true, "string", "String", true));
  343. foreach (TypeDescription typeDesc in types) {
  344. XmlReflectionImporter ri = new XmlReflectionImporter (typeDesc.Type.Name);
  345. XmlSchemas schemas = new XmlSchemas ();
  346. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  347. XmlTypeMapping tm = ri.ImportTypeMapping (typeDesc.Type);
  348. sx.ExportTypeMapping (tm);
  349. Assert.AreEqual (1, schemas.Count, typeDesc.Type.FullName + "#1");
  350. StringWriter sw = new StringWriter ();
  351. schemas[0].Write (sw);
  352. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  353. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  354. "<xs:schema xmlns:tns=\"{1}\" elementFormDefault=\"qualified\" targetNamespace=\"{1}\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  355. " <xs:element name=\"ArrayOf{2}\" nillable=\"true\" type=\"tns:ArrayOf{2}\" />{0}" +
  356. " <xs:complexType name=\"ArrayOf{2}\">{0}" +
  357. " <xs:sequence>{0}" +
  358. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"{3}\" {5}type=\"{4}:{3}\" />{0}" +
  359. " </xs:sequence>{0}" +
  360. " </xs:complexType>{0}" +
  361. "</xs:schema>", Environment.NewLine, typeDesc.Type.Name, typeDesc.ArrayType, typeDesc.XmlType,
  362. typeDesc.XsdType ? "xs" : "tns", typeDesc.IsNillable ? "nillable=\"true\" " : ""),
  363. sw.ToString (), typeDesc.Type.FullName + "#2");
  364. }
  365. }
  366. [Test]
  367. [Category ("NotWorking")] // bug #77117
  368. public void ExportXsdPrimitive_Object_Arrays ()
  369. {
  370. XmlReflectionImporter ri = new XmlReflectionImporter ("NSArrayOfAnyType");
  371. XmlSchemas schemas = new XmlSchemas ();
  372. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  373. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (object[]));
  374. sx.ExportTypeMapping (tm);
  375. Assert.AreEqual (1, schemas.Count, "#1");
  376. StringWriter sw = new StringWriter ();
  377. schemas[0].Write (sw);
  378. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  379. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  380. "<xs:schema xmlns:tns=\"NSArrayOfAnyType\" elementFormDefault=\"qualified\" targetNamespace=\"NSArrayOfAnyType\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  381. " <xs:element name=\"ArrayOfAnyType\" nillable=\"true\" type=\"tns:ArrayOfAnyType\" />{0}" +
  382. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  383. " <xs:sequence>{0}" +
  384. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  385. " </xs:sequence>{0}" +
  386. " </xs:complexType>{0}" +
  387. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  388. }
  389. [Test]
  390. [Category ("NotWorking")] // bug #77117
  391. public void ExportNonXsdPrimitive_Guid ()
  392. {
  393. XmlReflectionImporter ri = new XmlReflectionImporter ("NSPrimGuid");
  394. XmlSchemas schemas = new XmlSchemas ();
  395. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  396. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (Guid));
  397. sx.ExportTypeMapping (tm);
  398. Assert.AreEqual (2, schemas.Count, "#1");
  399. StringWriter sw = new StringWriter ();
  400. schemas[0].Write (sw);
  401. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  402. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  403. "<xs:schema xmlns:tns=\"NSPrimGuid\" elementFormDefault=\"qualified\" targetNamespace=\"NSPrimGuid\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  404. " <xs:import namespace=\"http://microsoft.com/wsdl/types/\" />{0}" +
  405. " <xs:element name=\"guid\" xmlns:q1=\"http://microsoft.com/wsdl/types/\" type=\"q1:guid\" />{0}" +
  406. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  407. sw = new StringWriter ();
  408. schemas[1].Write (sw);
  409. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  410. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  411. "<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}" +
  412. " <xs:simpleType name=\"guid\">{0}" +
  413. " <xs:restriction base=\"xs:string\">{0}" +
  414. " <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}" +
  415. " </xs:restriction>{0}" +
  416. " </xs:simpleType>{0}" +
  417. "</xs:schema>", Environment.NewLine), sw.ToString (), "#3");
  418. }
  419. [Test]
  420. [Category ("NotWorking")] // bug #77117
  421. public void ExportNonXsdPrimitive_Char ()
  422. {
  423. XmlReflectionImporter ri = new XmlReflectionImporter ("NSPrimChar");
  424. XmlSchemas schemas = new XmlSchemas ();
  425. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  426. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (Char));
  427. sx.ExportTypeMapping (tm);
  428. Assert.AreEqual (2, schemas.Count, "#1");
  429. StringWriter sw = new StringWriter ();
  430. schemas[0].Write (sw);
  431. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  432. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  433. "<xs:schema xmlns:tns=\"NSPrimChar\" elementFormDefault=\"qualified\" targetNamespace=\"NSPrimChar\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  434. " <xs:import namespace=\"http://microsoft.com/wsdl/types/\" />{0}" +
  435. " <xs:element name=\"char\" xmlns:q1=\"http://microsoft.com/wsdl/types/\" type=\"q1:char\" />{0}" +
  436. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  437. sw = new StringWriter ();
  438. schemas[1].Write (sw);
  439. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  440. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  441. "<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}" +
  442. " <xs:simpleType name=\"char\">{0}" +
  443. " <xs:restriction base=\"xs:unsignedShort\" />{0}" +
  444. " </xs:simpleType>{0}" +
  445. "</xs:schema>", Environment.NewLine), sw.ToString (), "#3");
  446. }
  447. public class Employee : IXmlSerializable
  448. {
  449. private string _firstName;
  450. private string _lastName;
  451. private string _address;
  452. public XmlSchema GetSchema ()
  453. {
  454. return null;
  455. }
  456. public void WriteXml (XmlWriter writer)
  457. {
  458. writer.WriteStartElement ("employee", "urn:devx-com");
  459. writer.WriteAttributeString ("firstName", _firstName);
  460. writer.WriteAttributeString ("lastName", _lastName);
  461. writer.WriteAttributeString ("address", _address);
  462. writer.WriteEndElement ();
  463. }
  464. public void ReadXml (XmlReader reader)
  465. {
  466. XmlNodeType type = reader.MoveToContent ();
  467. if (type == XmlNodeType.Element && reader.LocalName == "employee") {
  468. _firstName = reader["firstName"];
  469. _lastName = reader["lastName"];
  470. _address = reader["address"];
  471. }
  472. }
  473. }
  474. #if NET_2_0
  475. [XmlSchemaProvider ("CreateEmployeeSchema")]
  476. #endif
  477. public class EmployeeSchema : Employee
  478. {
  479. #if NET_2_0
  480. public static XmlQualifiedName CreateEmployeeSchema (XmlSchemaSet schemaSet)
  481. {
  482. XmlSchema schema = new XmlSchema();
  483. schema.Id = "EmployeeSchema";
  484. schema.TargetNamespace = "urn:types-devx-com";
  485. XmlSchemaComplexType type = new XmlSchemaComplexType();
  486. type.Name = "employeeRoot";
  487. XmlSchemaAttribute firstNameAttr = new XmlSchemaAttribute();
  488. firstNameAttr.Name = "firstName";
  489. type.Attributes.Add(firstNameAttr);
  490. XmlSchemaAttribute lastNameAttr = new XmlSchemaAttribute();
  491. lastNameAttr.Name = "lastName";
  492. type.Attributes.Add(lastNameAttr);
  493. XmlSchemaAttribute addressAttr = new XmlSchemaAttribute();
  494. addressAttr.Name = "address";
  495. type.Attributes.Add(addressAttr);
  496. XmlSchemaElement employeeElement = new XmlSchemaElement();
  497. employeeElement.Name = "employee";
  498. XmlQualifiedName name = new XmlQualifiedName("employeeRoot", "urn:types-devx-com");
  499. employeeElement.SchemaTypeName = name;
  500. schema.Items.Add(type);
  501. schema.Items.Add(employeeElement);
  502. schemaSet.Add(schema);
  503. return name;
  504. }
  505. #endif
  506. }
  507. private class TypeDescription
  508. {
  509. public TypeDescription (Type type, bool xsdType, string xmlType, string arrayType) : this (type, xsdType, xmlType, arrayType, false)
  510. {
  511. }
  512. public TypeDescription (Type type, bool xsdType, string xmlType, string arrayType, bool isNillable)
  513. {
  514. _type = type;
  515. _xsdType = xsdType;
  516. _xmlType = xmlType;
  517. _arrayType = arrayType;
  518. _isNillable = isNillable;
  519. }
  520. public Type Type {
  521. get { return _type; }
  522. }
  523. public string XmlType {
  524. get { return _xmlType; }
  525. }
  526. public string ArrayType {
  527. get { return _arrayType; }
  528. }
  529. public bool XsdType {
  530. get { return _xsdType; }
  531. }
  532. public bool IsNillable {
  533. get { return _isNillable; }
  534. }
  535. private Type _type;
  536. private bool _xsdType;
  537. private string _xmlType;
  538. private string _arrayType;
  539. private bool _isNillable;
  540. }
  541. }
  542. }