/mcs/class/System.XML/Test/System.Xml.Serialization/XmlSchemaExporterTests.cs

https://github.com/iainlane/mono · C# · 1060 lines · 915 code · 133 blank · 12 comment · 0 complexity · 3b674d9f8408e61034189542568f3123 MD5 · raw file

  1. //
  2. // System.Xml.Serialization.XmlSchemaExporterTests
  3. //
  4. // Author:
  5. // Gert Driesen (drieseng@users.sourceforge.net)
  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. const string ANamespace = "some:urn";
  24. const string AnotherNamespace = "another:urn";
  25. string Infoset (string source)
  26. {
  27. return XmlSerializerTests.Infoset (source);
  28. }
  29. private XmlSchemas Export (Type type)
  30. {
  31. return Export (type, string.Empty);
  32. }
  33. private XmlSchemas Export (Type type, string defaultNamespace)
  34. {
  35. XmlReflectionImporter ri = new XmlReflectionImporter (defaultNamespace);
  36. XmlSchemas schemas = new XmlSchemas ();
  37. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  38. XmlTypeMapping tm = ri.ImportTypeMapping (type);
  39. sx.ExportTypeMapping (tm);
  40. return schemas;
  41. }
  42. private XmlSchemas Export (Type type, XmlAttributeOverrides overrides)
  43. {
  44. return Export (type, overrides, string.Empty);
  45. }
  46. private XmlSchemas Export (Type type, XmlAttributeOverrides overrides, string defaultNamespace)
  47. {
  48. XmlReflectionImporter ri = new XmlReflectionImporter (overrides, defaultNamespace);
  49. XmlSchemas schemas = new XmlSchemas ();
  50. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  51. XmlTypeMapping tm = ri.ImportTypeMapping (type);
  52. sx.ExportTypeMapping (tm);
  53. return schemas;
  54. }
  55. [Test]
  56. [Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
  57. public void ExportStruct ()
  58. {
  59. XmlSchemas schemas = Export (typeof (TimeSpan), "NSTimeSpan");
  60. Assert.AreEqual (1, schemas.Count, "#1");
  61. StringWriter sw = new StringWriter ();
  62. schemas[0].Write (sw);
  63. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  64. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  65. "<xs:schema xmlns:tns=\"NSTimeSpan\" elementFormDefault=\"qualified\" targetNamespace=\"NSTimeSpan\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  66. " <xs:element name=\"TimeSpan\" type=\"tns:TimeSpan\" />{0}" +
  67. " <xs:complexType name=\"TimeSpan\" />{0}" +
  68. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  69. schemas = Export (typeof (TimeSpan));
  70. Assert.AreEqual (1, schemas.Count, "#3");
  71. sw.GetStringBuilder ().Length = 0;
  72. schemas[0].Write (sw);
  73. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  74. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  75. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  76. " <xs:element name=\"TimeSpan\" type=\"TimeSpan\" />{0}" +
  77. " <xs:complexType name=\"TimeSpan\" />{0}" +
  78. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  79. }
  80. [Test]
  81. [Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
  82. public void ExportStruct_Array ()
  83. {
  84. XmlSchemas schemas = Export (typeof (TimeSpan[]), "NSTimeSpanArray");
  85. Assert.AreEqual (1, schemas.Count, "#1");
  86. StringWriter sw = new StringWriter ();
  87. schemas[0].Write (sw);
  88. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  89. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  90. "<xs:schema xmlns:tns=\"NSTimeSpanArray\" elementFormDefault=\"qualified\" targetNamespace=\"NSTimeSpanArray\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  91. " <xs:element name=\"ArrayOfTimeSpan\" nillable=\"true\" type=\"tns:ArrayOfTimeSpan\" />{0}" +
  92. " <xs:complexType name=\"ArrayOfTimeSpan\">{0}" +
  93. " <xs:sequence>{0}" +
  94. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"TimeSpan\" type=\"tns:TimeSpan\" />{0}" +
  95. " </xs:sequence>{0}" +
  96. " </xs:complexType>{0}" +
  97. " <xs:complexType name=\"TimeSpan\" />{0}" +
  98. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  99. schemas = Export (typeof (TimeSpan[]));
  100. Assert.AreEqual (1, schemas.Count, "#3");
  101. sw.GetStringBuilder ().Length = 0;
  102. schemas[0].Write (sw);
  103. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  104. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  105. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  106. " <xs:element name=\"ArrayOfTimeSpan\" nillable=\"true\" type=\"ArrayOfTimeSpan\" />{0}" +
  107. " <xs:complexType name=\"ArrayOfTimeSpan\">{0}" +
  108. " <xs:sequence>{0}" +
  109. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"TimeSpan\" type=\"TimeSpan\" />{0}" +
  110. " </xs:sequence>{0}" +
  111. " </xs:complexType>{0}" +
  112. " <xs:complexType name=\"TimeSpan\" />{0}" +
  113. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  114. }
  115. [Test]
  116. public void ExportClass_SimpleClass ()
  117. {
  118. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  119. XmlAttributes attr = new XmlAttributes ();
  120. XmlElementAttribute element = new XmlElementAttribute ();
  121. element.ElementName = "saying";
  122. element.IsNullable = true;
  123. attr.XmlElements.Add (element);
  124. overrides.Add (typeof (SimpleClass), "something", attr);
  125. XmlSchemas schemas = Export (typeof (SimpleClass), overrides, "NSSimpleClass");
  126. Assert.AreEqual (1, schemas.Count, "#1");
  127. StringWriter sw = new StringWriter ();
  128. schemas[0].Write (sw);
  129. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  130. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  131. "<xs:schema xmlns:tns=\"NSSimpleClass\" elementFormDefault=\"qualified\" targetNamespace=\"NSSimpleClass\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  132. " <xs:element name=\"SimpleClass\" nillable=\"true\" type=\"tns:SimpleClass\" />{0}" +
  133. " <xs:complexType name=\"SimpleClass\">{0}" +
  134. " <xs:sequence>{0}" +
  135. " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"saying\" nillable=\"true\" type=\"xs:string\" />{0}" +
  136. " </xs:sequence>{0}" +
  137. " </xs:complexType>{0}" +
  138. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  139. schemas = Export (typeof (SimpleClass), overrides);
  140. Assert.AreEqual (1, schemas.Count, "#3");
  141. sw.GetStringBuilder ().Length = 0;
  142. schemas[0].Write (sw);
  143. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  144. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  145. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  146. " <xs:element name=\"SimpleClass\" nillable=\"true\" type=\"SimpleClass\" />{0}" +
  147. " <xs:complexType name=\"SimpleClass\">{0}" +
  148. " <xs:sequence>{0}" +
  149. " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"saying\" nillable=\"true\" type=\"xs:string\" />{0}" +
  150. " </xs:sequence>{0}" +
  151. " </xs:complexType>{0}" +
  152. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  153. }
  154. [Test]
  155. public void ExportClass_StringCollection ()
  156. {
  157. XmlSchemas schemas = Export (typeof (StringCollection), "NSStringCollection");
  158. Assert.AreEqual (1, schemas.Count, "#1");
  159. StringWriter sw = new StringWriter ();
  160. schemas[0].Write (sw);
  161. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  162. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  163. "<xs:schema xmlns:tns=\"NSStringCollection\" elementFormDefault=\"qualified\" targetNamespace=\"NSStringCollection\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  164. " <xs:element name=\"ArrayOfString\" nillable=\"true\" type=\"tns:ArrayOfString\" />{0}" +
  165. " <xs:complexType name=\"ArrayOfString\">{0}" +
  166. " <xs:sequence>{0}" +
  167. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"string\" nillable=\"true\" type=\"xs:string\" />{0}" +
  168. " </xs:sequence>{0}" +
  169. " </xs:complexType>{0}" +
  170. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  171. schemas = Export (typeof (StringCollection));
  172. Assert.AreEqual (1, schemas.Count, "#3");
  173. sw.GetStringBuilder ().Length = 0;
  174. schemas[0].Write (sw);
  175. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  176. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  177. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  178. " <xs:element name=\"ArrayOfString\" nillable=\"true\" type=\"ArrayOfString\" />{0}" +
  179. " <xs:complexType name=\"ArrayOfString\">{0}" +
  180. " <xs:sequence>{0}" +
  181. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"string\" nillable=\"true\" type=\"xs:string\" />{0}" +
  182. " </xs:sequence>{0}" +
  183. " </xs:complexType>{0}" +
  184. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  185. }
  186. [Test]
  187. public void ExportClass_StringCollectionContainer ()
  188. {
  189. XmlSchemas schemas = Export (typeof (StringCollectionContainer), "NSStringCollectionContainer");
  190. Assert.AreEqual (1, schemas.Count, "#1");
  191. StringWriter sw = new StringWriter ();
  192. schemas[0].Write (sw);
  193. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  194. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  195. "<xs:schema xmlns:tns=\"NSStringCollectionContainer\" elementFormDefault=\"qualified\" targetNamespace=\"NSStringCollectionContainer\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  196. " <xs:element name=\"StringCollectionContainer\" nillable=\"true\" type=\"tns:StringCollectionContainer\" />{0}" +
  197. " <xs:complexType name=\"StringCollectionContainer\">{0}" +
  198. " <xs:sequence>{0}" +
  199. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Messages\" type=\"tns:ArrayOfString\" />{0}" +
  200. " </xs:sequence>{0}" +
  201. " </xs:complexType>{0}" +
  202. " <xs:complexType name=\"ArrayOfString\">{0}" +
  203. " <xs:sequence>{0}" +
  204. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"string\" nillable=\"true\" type=\"xs:string\" />{0}" +
  205. " </xs:sequence>{0}" +
  206. " </xs:complexType>{0}" +
  207. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  208. schemas = Export (typeof (StringCollectionContainer));
  209. Assert.AreEqual (1, schemas.Count, "#3");
  210. sw.GetStringBuilder ().Length = 0;
  211. schemas[0].Write (sw);
  212. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  213. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  214. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  215. " <xs:element name=\"StringCollectionContainer\" nillable=\"true\" type=\"StringCollectionContainer\" />{0}" +
  216. " <xs:complexType name=\"StringCollectionContainer\">{0}" +
  217. " <xs:sequence>{0}" +
  218. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Messages\" type=\"ArrayOfString\" />{0}" +
  219. " </xs:sequence>{0}" +
  220. " </xs:complexType>{0}" +
  221. " <xs:complexType name=\"ArrayOfString\">{0}" +
  222. " <xs:sequence>{0}" +
  223. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"string\" nillable=\"true\" type=\"xs:string\" />{0}" +
  224. " </xs:sequence>{0}" +
  225. " </xs:complexType>{0}" +
  226. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  227. }
  228. [Test]
  229. public void ExportClass_ArrayContainer ()
  230. {
  231. XmlSchemas schemas = Export (typeof (ArrayContainer), "NSArrayContainer");
  232. Assert.AreEqual (1, schemas.Count, "#1");
  233. StringWriter sw = new StringWriter ();
  234. schemas[0].Write (sw);
  235. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  236. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  237. "<xs:schema xmlns:tns=\"NSArrayContainer\" elementFormDefault=\"qualified\" targetNamespace=\"NSArrayContainer\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  238. " <xs:element name=\"ArrayContainer\" nillable=\"true\" type=\"tns:ArrayContainer\" />{0}" +
  239. " <xs:complexType name=\"ArrayContainer\">{0}" +
  240. " <xs:sequence>{0}" +
  241. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"items\" type=\"tns:ArrayOfAnyType\" />{0}" +
  242. " </xs:sequence>{0}" +
  243. " </xs:complexType>{0}" +
  244. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  245. " <xs:sequence>{0}" +
  246. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  247. " </xs:sequence>{0}" +
  248. " </xs:complexType>{0}" +
  249. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  250. schemas = Export (typeof (ArrayContainer));
  251. Assert.AreEqual (1, schemas.Count, "#3");
  252. sw.GetStringBuilder ().Length = 0;
  253. schemas[0].Write (sw);
  254. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  255. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  256. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  257. " <xs:element name=\"ArrayContainer\" nillable=\"true\" type=\"ArrayContainer\" />{0}" +
  258. " <xs:complexType name=\"ArrayContainer\">{0}" +
  259. " <xs:sequence>{0}" +
  260. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"items\" type=\"ArrayOfAnyType\" />{0}" +
  261. " </xs:sequence>{0}" +
  262. " </xs:complexType>{0}" +
  263. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  264. " <xs:sequence>{0}" +
  265. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  266. " </xs:sequence>{0}" +
  267. " </xs:complexType>{0}" +
  268. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  269. }
  270. [Test]
  271. public void ExportClass_ClassArrayContainer ()
  272. {
  273. XmlSchemas schemas = Export (typeof (ClassArrayContainer), "NSClassArrayContainer");
  274. Assert.AreEqual (1, schemas.Count, "#1");
  275. StringWriter sw = new StringWriter ();
  276. schemas[0].Write (sw);
  277. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  278. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  279. "<xs:schema xmlns:tns=\"NSClassArrayContainer\" elementFormDefault=\"qualified\" targetNamespace=\"NSClassArrayContainer\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  280. " <xs:element name=\"ClassArrayContainer\" nillable=\"true\" type=\"tns:ClassArrayContainer\" />{0}" +
  281. " <xs:complexType name=\"ClassArrayContainer\">{0}" +
  282. " <xs:sequence>{0}" +
  283. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"items\" type=\"tns:ArrayOfSimpleClass\" />{0}" +
  284. " </xs:sequence>{0}" +
  285. " </xs:complexType>{0}" +
  286. " <xs:complexType name=\"ArrayOfSimpleClass\">{0}" +
  287. " <xs:sequence>{0}" +
  288. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"SimpleClass\" nillable=\"true\" type=\"tns:SimpleClass\" />{0}" +
  289. " </xs:sequence>{0}" +
  290. " </xs:complexType>{0}" +
  291. " <xs:complexType name=\"SimpleClass\">{0}" +
  292. " <xs:sequence>{0}" +
  293. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"something\" type=\"xs:string\" />{0}" +
  294. " </xs:sequence>{0}" +
  295. " </xs:complexType>{0}" +
  296. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  297. schemas = Export (typeof (ClassArrayContainer));
  298. Assert.AreEqual (1, schemas.Count, "#3");
  299. sw.GetStringBuilder ().Length = 0;
  300. schemas[0].Write (sw);
  301. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  302. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  303. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  304. " <xs:element name=\"ClassArrayContainer\" nillable=\"true\" type=\"ClassArrayContainer\" />{0}" +
  305. " <xs:complexType name=\"ClassArrayContainer\">{0}" +
  306. " <xs:sequence>{0}" +
  307. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"items\" type=\"ArrayOfSimpleClass\" />{0}" +
  308. " </xs:sequence>{0}" +
  309. " </xs:complexType>{0}" +
  310. " <xs:complexType name=\"ArrayOfSimpleClass\">{0}" +
  311. " <xs:sequence>{0}" +
  312. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"SimpleClass\" nillable=\"true\" type=\"SimpleClass\" />{0}" +
  313. " </xs:sequence>{0}" +
  314. " </xs:complexType>{0}" +
  315. " <xs:complexType name=\"SimpleClass\">{0}" +
  316. " <xs:sequence>{0}" +
  317. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"something\" type=\"xs:string\" />{0}" +
  318. " </xs:sequence>{0}" +
  319. " </xs:complexType>{0}" +
  320. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  321. }
  322. [Test]
  323. public void ExportClass_SimpleClassWithXmlAttributes ()
  324. {
  325. XmlSchemas schemas = Export (typeof (SimpleClassWithXmlAttributes), "NSSimpleClassWithXmlAttributes");
  326. Assert.AreEqual (1, schemas.Count, "#1");
  327. StringWriter sw = new StringWriter ();
  328. schemas[0].Write (sw);
  329. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  330. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  331. "<xs:schema xmlns:tns=\"NSSimpleClassWithXmlAttributes\" elementFormDefault=\"qualified\" targetNamespace=\"NSSimpleClassWithXmlAttributes\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  332. " <xs:element name=\"simple\" nillable=\"true\" type=\"tns:SimpleClassWithXmlAttributes\" />{0}" +
  333. " <xs:complexType name=\"SimpleClassWithXmlAttributes\">{0}" +
  334. " <xs:attribute name=\"member\" type=\"xs:string\" />{0}" +
  335. " </xs:complexType>{0}" +
  336. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  337. schemas = Export (typeof (SimpleClassWithXmlAttributes));
  338. Assert.AreEqual (1, schemas.Count, "#3");
  339. sw.GetStringBuilder ().Length = 0;
  340. schemas[0].Write (sw);
  341. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  342. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  343. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  344. " <xs:element name=\"simple\" nillable=\"true\" type=\"SimpleClassWithXmlAttributes\" />{0}" +
  345. " <xs:complexType name=\"SimpleClassWithXmlAttributes\">{0}" +
  346. " <xs:attribute name=\"member\" type=\"xs:string\" />{0}" +
  347. " </xs:complexType>{0}" +
  348. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  349. }
  350. [Test]
  351. public void ExportClass_Field ()
  352. {
  353. XmlSchemas schemas = Export (typeof (Field), "NSField");
  354. Assert.AreEqual (1, schemas.Count, "#1");
  355. StringWriter sw = new StringWriter ();
  356. schemas[0].Write (sw);
  357. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  358. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  359. "<xs:schema xmlns:tns=\"NSField\" elementFormDefault=\"qualified\" targetNamespace=\"NSField\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  360. " <xs:element name=\"field\" nillable=\"true\" type=\"tns:Field\" />{0}" +
  361. " <xs:complexType name=\"Field\">{0}" +
  362. " <xs:attribute default=\"one\" name=\"flag1\" type=\"tns:FlagEnum\" />{0}" +
  363. " <xs:attribute default=\"one\" name=\"flag2\" type=\"tns:FlagEnum\" />{0}" +
  364. " <xs:attribute default=\"one two\" form=\"qualified\" name=\"flag3\" type=\"tns:FlagEnum\" />{0}" +
  365. " <xs:attribute name=\"flag4\" type=\"tns:FlagEnum\" use=\"required\" />{0}" +
  366. " <xs:attribute name=\"modifiers\" type=\"tns:MapModifiers\" use=\"required\" />{0}" +
  367. " <xs:attribute form=\"unqualified\" name=\"modifiers2\" type=\"tns:MapModifiers\" use=\"required\" />{0}" +
  368. " <xs:attribute default=\"public\" name=\"modifiers3\" type=\"tns:MapModifiers\" />{0}" +
  369. " <xs:attribute default=\"protected\" form=\"unqualified\" name=\"modifiers4\" type=\"tns:MapModifiers\" />{0}" +
  370. " <xs:attribute default=\"public\" form=\"qualified\" name=\"modifiers5\" type=\"tns:MapModifiers\" />{0}" +
  371. " <xs:attribute name=\"names\">{0}" +
  372. " <xs:simpleType>{0}" +
  373. " <xs:list itemType=\"xs:string\" />{0}" +
  374. " </xs:simpleType>{0}" +
  375. " </xs:attribute>{0}" +
  376. " <xs:attribute name=\"street\" type=\"xs:string\" />{0}" +
  377. " </xs:complexType>{0}" +
  378. " <xs:simpleType name=\"FlagEnum\">{0}" +
  379. " <xs:list>{0}" +
  380. " <xs:simpleType>{0}" +
  381. " <xs:restriction base=\"xs:string\">{0}" +
  382. " <xs:enumeration value=\"one\" />{0}" +
  383. " <xs:enumeration value=\"two\" />{0}" +
  384. " <xs:enumeration value=\"four\" />{0}" +
  385. " </xs:restriction>{0}" +
  386. " </xs:simpleType>{0}" +
  387. " </xs:list>{0}" +
  388. " </xs:simpleType>{0}" +
  389. " <xs:simpleType name=\"MapModifiers\">{0}" +
  390. " <xs:list>{0}" +
  391. " <xs:simpleType>{0}" +
  392. " <xs:restriction base=\"xs:string\">{0}" +
  393. " <xs:enumeration value=\"public\" />{0}" +
  394. " <xs:enumeration value=\"protected\" />{0}" +
  395. " </xs:restriction>{0}" +
  396. " </xs:simpleType>{0}" +
  397. " </xs:list>{0}" +
  398. " </xs:simpleType>{0}" +
  399. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  400. schemas = Export (typeof (Field));
  401. Assert.AreEqual (1, schemas.Count, "#3");
  402. sw.GetStringBuilder ().Length = 0;
  403. schemas[0].Write (sw);
  404. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  405. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  406. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  407. " <xs:element name=\"field\" nillable=\"true\" type=\"Field\" />{0}" +
  408. " <xs:complexType name=\"Field\">{0}" +
  409. " <xs:attribute default=\"one\" name=\"flag1\" type=\"FlagEnum\" />{0}" +
  410. " <xs:attribute default=\"one\" name=\"flag2\" type=\"FlagEnum\" />{0}" +
  411. " <xs:attribute default=\"one two\" form=\"qualified\" name=\"flag3\" type=\"FlagEnum\" />{0}" +
  412. " <xs:attribute name=\"flag4\" type=\"FlagEnum\" use=\"required\" />{0}" +
  413. " <xs:attribute name=\"modifiers\" type=\"MapModifiers\" use=\"required\" />{0}" +
  414. " <xs:attribute form=\"unqualified\" name=\"modifiers2\" type=\"MapModifiers\" use=\"required\" />{0}" +
  415. " <xs:attribute default=\"public\" name=\"modifiers3\" type=\"MapModifiers\" />{0}" +
  416. " <xs:attribute default=\"protected\" form=\"unqualified\" name=\"modifiers4\" type=\"MapModifiers\" />{0}" +
  417. " <xs:attribute default=\"public\" form=\"qualified\" name=\"modifiers5\" type=\"MapModifiers\" />{0}" +
  418. " <xs:attribute name=\"names\">{0}" +
  419. " <xs:simpleType>{0}" +
  420. " <xs:list itemType=\"xs:string\" />{0}" +
  421. " </xs:simpleType>{0}" +
  422. " </xs:attribute>{0}" +
  423. " <xs:attribute name=\"street\" type=\"xs:string\" />{0}" +
  424. " </xs:complexType>{0}" +
  425. " <xs:simpleType name=\"FlagEnum\">{0}" +
  426. " <xs:list>{0}" +
  427. " <xs:simpleType>{0}" +
  428. " <xs:restriction base=\"xs:string\">{0}" +
  429. " <xs:enumeration value=\"one\" />{0}" +
  430. " <xs:enumeration value=\"two\" />{0}" +
  431. " <xs:enumeration value=\"four\" />{0}" +
  432. " </xs:restriction>{0}" +
  433. " </xs:simpleType>{0}" +
  434. " </xs:list>{0}" +
  435. " </xs:simpleType>{0}" +
  436. " <xs:simpleType name=\"MapModifiers\">{0}" +
  437. " <xs:list>{0}" +
  438. " <xs:simpleType>{0}" +
  439. " <xs:restriction base=\"xs:string\">{0}" +
  440. " <xs:enumeration value=\"public\" />{0}" +
  441. " <xs:enumeration value=\"protected\" />{0}" +
  442. " </xs:restriction>{0}" +
  443. " </xs:simpleType>{0}" +
  444. " </xs:list>{0}" +
  445. " </xs:simpleType>{0}" +
  446. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  447. }
  448. [Test]
  449. public void ExportClass_MyList ()
  450. {
  451. XmlSchemas schemas = Export (typeof (MyList), "NSMyList");
  452. Assert.AreEqual (1, schemas.Count, "#1");
  453. StringWriter sw = new StringWriter ();
  454. schemas[0].Write (sw);
  455. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  456. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  457. "<xs:schema xmlns:tns=\"NSMyList\" elementFormDefault=\"qualified\" targetNamespace=\"NSMyList\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  458. " <xs:element name=\"ArrayOfAnyType\" nillable=\"true\" type=\"tns:ArrayOfAnyType\" />{0}" +
  459. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  460. " <xs:sequence>{0}" +
  461. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  462. " </xs:sequence>{0}" +
  463. " </xs:complexType>{0}" +
  464. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  465. schemas = Export (typeof (MyList));
  466. Assert.AreEqual (1, schemas.Count, "#3");
  467. sw.GetStringBuilder ().Length = 0;
  468. schemas[0].Write (sw);
  469. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  470. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  471. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  472. " <xs:element name=\"ArrayOfAnyType\" nillable=\"true\" type=\"ArrayOfAnyType\" />{0}" +
  473. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  474. " <xs:sequence>{0}" +
  475. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  476. " </xs:sequence>{0}" +
  477. " </xs:complexType>{0}" +
  478. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  479. }
  480. [Test]
  481. public void ExportClass_Container ()
  482. {
  483. XmlSchemas schemas = Export (typeof (Container), "NSContainer");
  484. Assert.AreEqual (1, schemas.Count, "#1");
  485. StringWriter sw = new StringWriter ();
  486. schemas[0].Write (sw);
  487. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  488. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  489. "<xs:schema xmlns:tns=\"NSContainer\" elementFormDefault=\"qualified\" targetNamespace=\"NSContainer\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  490. " <xs:element name=\"Container\" nillable=\"true\" type=\"tns:Container\" />{0}" +
  491. " <xs:complexType name=\"Container\">{0}" +
  492. " <xs:sequence>{0}" +
  493. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Items\" type=\"tns:ArrayOfAnyType\" />{0}" +
  494. " </xs:sequence>{0}" +
  495. " </xs:complexType>{0}" +
  496. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  497. " <xs:sequence>{0}" +
  498. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  499. " </xs:sequence>{0}" +
  500. " </xs:complexType>{0}" +
  501. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  502. schemas = Export (typeof (Container));
  503. Assert.AreEqual (1, schemas.Count, "#3");
  504. sw.GetStringBuilder ().Length = 0;
  505. schemas[0].Write (sw);
  506. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  507. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  508. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  509. " <xs:element name=\"Container\" nillable=\"true\" type=\"Container\" />{0}" +
  510. " <xs:complexType name=\"Container\">{0}" +
  511. " <xs:sequence>{0}" +
  512. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Items\" type=\"ArrayOfAnyType\" />{0}" +
  513. " </xs:sequence>{0}" +
  514. " </xs:complexType>{0}" +
  515. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  516. " <xs:sequence>{0}" +
  517. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  518. " </xs:sequence>{0}" +
  519. " </xs:complexType>{0}" +
  520. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  521. }
  522. [Test]
  523. public void ExportClass_Container2 ()
  524. {
  525. XmlSchemas schemas = Export (typeof (Container2), "NSContainer2");
  526. Assert.AreEqual (1, schemas.Count, "#1");
  527. StringWriter sw = new StringWriter ();
  528. schemas[0].Write (sw);
  529. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  530. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  531. "<xs:schema xmlns:tns=\"NSContainer2\" elementFormDefault=\"qualified\" targetNamespace=\"NSContainer2\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  532. " <xs:element name=\"Container2\" nillable=\"true\" type=\"tns:Container2\" />{0}" +
  533. " <xs:complexType name=\"Container2\">{0}" +
  534. " <xs:sequence>{0}" +
  535. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Items\" type=\"tns:ArrayOfAnyType\" />{0}" +
  536. " </xs:sequence>{0}" +
  537. " </xs:complexType>{0}" +
  538. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  539. " <xs:sequence>{0}" +
  540. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  541. " </xs:sequence>{0}" +
  542. " </xs:complexType>{0}" +
  543. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  544. schemas = Export (typeof (Container2));
  545. Assert.AreEqual (1, schemas.Count, "#3");
  546. sw.GetStringBuilder ().Length = 0;
  547. schemas[0].Write (sw);
  548. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  549. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  550. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  551. " <xs:element name=\"Container2\" nillable=\"true\" type=\"Container2\" />{0}" +
  552. " <xs:complexType name=\"Container2\">{0}" +
  553. " <xs:sequence>{0}" +
  554. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Items\" type=\"ArrayOfAnyType\" />{0}" +
  555. " </xs:sequence>{0}" +
  556. " </xs:complexType>{0}" +
  557. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  558. " <xs:sequence>{0}" +
  559. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  560. " </xs:sequence>{0}" +
  561. " </xs:complexType>{0}" +
  562. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  563. }
  564. [Test]
  565. [Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
  566. [ExpectedException (typeof (InvalidOperationException))] // Cannot use wildcards at the top level of a schema.
  567. public void ExportClass_MyElem ()
  568. {
  569. Export (typeof (MyElem), "NSMyElem");
  570. }
  571. [Test]
  572. public void ExportClass_CDataContainer ()
  573. {
  574. XmlSchemas schemas = Export (typeof (CDataContainer), "NSCDataContainer");
  575. Assert.AreEqual (1, schemas.Count, "#1");
  576. StringWriter sw = new StringWriter ();
  577. schemas[0].Write (sw);
  578. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  579. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  580. "<xs:schema xmlns:tns=\"NSCDataContainer\" elementFormDefault=\"qualified\" targetNamespace=\"NSCDataContainer\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  581. " <xs:element name=\"CDataContainer\" nillable=\"true\" type=\"tns:CDataContainer\" />{0}" +
  582. " <xs:complexType name=\"CDataContainer\">{0}" +
  583. " <xs:sequence>{0}" +
  584. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"cdata\">{0}" +
  585. " <xs:complexType mixed=\"true\">{0}" +
  586. " <xs:sequence>{0}" +
  587. " <xs:any />{0}" +
  588. " </xs:sequence>{0}" +
  589. " </xs:complexType>{0}" +
  590. " </xs:element>{0}" +
  591. " </xs:sequence>{0}" +
  592. " </xs:complexType>{0}" +
  593. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#2");
  594. schemas = Export (typeof (CDataContainer));
  595. Assert.AreEqual (1, schemas.Count, "#3");
  596. sw.GetStringBuilder ().Length = 0;
  597. schemas[0].Write (sw);
  598. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  599. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  600. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  601. " <xs:element name=\"CDataContainer\" nillable=\"true\" type=\"CDataContainer\" />{0}" +
  602. " <xs:complexType name=\"CDataContainer\">{0}" +
  603. " <xs:sequence>{0}" +
  604. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"cdata\">{0}" +
  605. " <xs:complexType mixed=\"true\">{0}" +
  606. " <xs:sequence>{0}" +
  607. " <xs:any />{0}" +
  608. " </xs:sequence>{0}" +
  609. " </xs:complexType>{0}" +
  610. " </xs:element>{0}" +
  611. " </xs:sequence>{0}" +
  612. " </xs:complexType>{0}" +
  613. "</xs:schema>", Environment.NewLine)), Infoset (sw.ToString ()), "#4");
  614. }
  615. [Test]
  616. public void ExportClass_NodeContainer ()
  617. {
  618. XmlSchemas schemas = Export (typeof (NodeContainer), "NSNodeContainer");
  619. Assert.AreEqual (1, schemas.Count, "#1");
  620. StringWriter sw = new StringWriter ();
  621. schemas[0].Write (sw);
  622. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  623. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  624. "<xs:schema xmlns:tns=\"NSNodeContainer\" elementFormDefault=\"qualified\" targetNamespace=\"NSNodeContainer\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  625. " <xs:element name=\"NodeContainer\" nillable=\"true\" type=\"tns:NodeContainer\" />{0}" +
  626. " <xs:complexType name=\"NodeContainer\">{0}" +
  627. " <xs:sequence>{0}" +
  628. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"node\">{0}" +
  629. " <xs:complexType mixed=\"true\">{0}" +
  630. " <xs:sequence>{0}" +
  631. " <xs:any />{0}" +
  632. " </xs:sequence>{0}" +
  633. " </xs:complexType>{0}" +
  634. " </xs:element>{0}" +
  635. " </xs:sequence>{0}" +
  636. " </xs:complexType>{0}" +
  637. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  638. schemas = Export (typeof (NodeContainer));
  639. Assert.AreEqual (1, schemas.Count, "#3");
  640. sw.GetStringBuilder ().Length = 0;
  641. schemas[0].Write (sw);
  642. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  643. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  644. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  645. " <xs:element name=\"NodeContainer\" nillable=\"true\" type=\"NodeContainer\" />{0}" +
  646. " <xs:complexType name=\"NodeContainer\">{0}" +
  647. " <xs:sequence>{0}" +
  648. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"node\">{0}" +
  649. " <xs:complexType mixed=\"true\">{0}" +
  650. " <xs:sequence>{0}" +
  651. " <xs:any />{0}" +
  652. " </xs:sequence>{0}" +
  653. " </xs:complexType>{0}" +
  654. " </xs:element>{0}" +
  655. " </xs:sequence>{0}" +
  656. " </xs:complexType>{0}" +
  657. "</xs:schema>", Environment.NewLine), sw.ToString (), "#4");
  658. }
  659. [Test]
  660. [Category ("NotWorking")] // Mono does not generate the <xs:choice> node
  661. [Category ("NotDotNet")] // MS.NET randomly modifies the order of the elements! -> Of course. There is nothing strange. The test is rather strange.
  662. public void ExportClass_Choices ()
  663. {
  664. XmlSchemas schemas = Export (typeof (Choices), "NSChoices");
  665. Assert.AreEqual (1, schemas.Count, "#1");
  666. StringWriter sw = new StringWriter ();
  667. schemas[0].Write (sw);
  668. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  669. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  670. "<xs:schema xmlns:tns=\"NSChoices\" elementFormDefault=\"qualified\" targetNamespace=\"NSChoices\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  671. " <xs:element name=\"Choices\" type=\"tns:Choices\" />{0}" +
  672. " <xs:complexType name=\"Choices\">{0}" +
  673. " <xs:sequence>{0}" +
  674. " <xs:choice minOccurs=\"1\" maxOccurs=\"1\">{0}" +
  675. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ChoiceOne\" type=\"xs:string\" />{0}" +
  676. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ChoiceTwo\" type=\"xs:string\" />{0}" +
  677. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ChoiceZero\" type=\"xs:string\" />{0}" +
  678. " </xs:choice>{0}" +
  679. " </xs:sequence>{0}" +
  680. " </xs:complexType>{0}" +
  681. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  682. schemas = Export (typeof (Choices));
  683. Assert.AreEqual (1, schemas.Count, "#3");
  684. sw.GetStringBuilder ().Length = 0;
  685. schemas[0].Write (sw);
  686. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  687. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  688. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  689. " <xs:element name=\"Choices\" type=\"Choices\" />{0}" +
  690. " <xs:complexType name=\"Choices\">{0}" +
  691. " <xs:sequence>{0}" +
  692. " <xs:choice minOccurs=\"1\" maxOccurs=\"1\">{0}" +
  693. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ChoiceOne\" type=\"xs:string\" />{0}" +
  694. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ChoiceTwo\" type=\"xs:string\" />{0}" +
  695. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ChoiceZero\" type=\"xs:string\" />{0}" +
  696. " </xs:choice>{0}" +
  697. " </xs:sequence>{0}" +
  698. " </xs:complexType>{0}" +
  699. "</xs:schema>", Environment.NewLine), sw.ToString (), "#4");
  700. }
  701. [Test]
  702. #if ONLY_1_1
  703. [Category ("NotDotNet")] // MS.NET 1.x does not escape spaces in a type name, bug is fixed in .NET 2.0
  704. #endif
  705. public void ExportClass_TestSpace ()
  706. {
  707. XmlSchemas schemas = Export (typeof (TestSpace), "NSTestSpace");
  708. Assert.AreEqual (1, schemas.Count, "#1");
  709. StringWriter sw = new StringWriter ();
  710. schemas[0].Write (sw);
  711. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  712. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  713. "<xs:schema xmlns:tns=\"NSTestSpace\" elementFormDefault=\"qualified\" targetNamespace=\"NSTestSpace\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  714. " <xs:element name=\"Type_x0020_with_x0020_space\" nillable=\"true\" type=\"tns:Type_x0020_with_x0020_space\" />{0}" +
  715. " <xs:complexType name=\"Type_x0020_with_x0020_space\">{0}" +
  716. " <xs:sequence>{0}" +
  717. " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"Element_x0020_with_x0020_space\" type=\"xs:int\" />{0}" +
  718. " </xs:sequence>{0}" +
  719. " <xs:attribute name=\"Attribute_x0020_with_x0020_space\" type=\"xs:int\" use=\"required\" />{0}" +
  720. " </xs:complexType>{0}" +
  721. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  722. schemas = Export (typeof (TestSpace));
  723. Assert.AreEqual (1, schemas.Count, "#3");
  724. sw.GetStringBuilder ().Length = 0;
  725. schemas[0].Write (sw);
  726. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  727. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  728. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  729. " <xs:element name=\"Type_x0020_with_x0020_space\" nillable=\"true\" type=\"Type_x0020_with_x0020_space\" />{0}" +
  730. " <xs:complexType name=\"Type_x0020_with_x0020_space\">{0}" +
  731. " <xs:sequence>{0}" +
  732. " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"Element_x0020_with_x0020_space\" type=\"xs:int\" />{0}" +
  733. " </xs:sequence>{0}" +
  734. " <xs:attribute name=\"Attribute_x0020_with_x0020_space\" type=\"xs:int\" use=\"required\" />{0}" +
  735. " </xs:complexType>{0}" +
  736. "</xs:schema>", Environment.NewLine), sw.ToString (), "#4");
  737. }
  738. [Test]
  739. public void ExportClass_OptionalValueTypeContainer ()
  740. {
  741. XmlAttributeOverrides overrides;
  742. XmlAttributes attr;
  743. XmlSchemas schemas = Export (typeof (OptionalValueTypeContainer));
  744. Assert.AreEqual (2, schemas.Count, "#1");
  745. StringWriter sw = new StringWriter ();
  746. schemas[0].Write (sw);
  747. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  748. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  749. "<xs:schema xmlns:tns=\"{1}\" elementFormDefault=\"qualified\" targetNamespace=\"{1}\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  750. " <xs:import namespace=\"{2}\" />{0}" +
  751. " <xs:element name=\"optionalValue\" xmlns:q1=\"{2}\" type=\"q1:optionalValueType\" />{0}" +
  752. "</xs:schema>", Environment.NewLine, AnotherNamespace, ANamespace),
  753. sw.ToString (), "#2");
  754. sw.GetStringBuilder ().Length = 0;
  755. schemas[1].Write (sw);
  756. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  757. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  758. "<xs:schema xmlns:tns=\"{1}\" elementFormDefault=\"qualified\" targetNamespace=\"{1}\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  759. " <xs:complexType name=\"optionalValueType\">{0}" +
  760. " <xs:sequence>{0}" +
  761. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" default=\"one four\" name=\"Attributes\" type=\"tns:FlagEnum\" />{0}" +
  762. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" default=\"one\" name=\"Flags\" type=\"tns:FlagEnum\" />{0}" +
  763. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" default=\"false\" name=\"IsEmpty\" type=\"xs:boolean\" />{0}" +
  764. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" default=\"false\" name=\"IsNull\" type=\"xs:boolean\" />{0}" +
  765. " </xs:sequence>{0}" +
  766. " </xs:complexType>{0}" +
  767. " <xs:simpleType name=\"FlagEnum\">{0}" +
  768. " <xs:list>{0}" +
  769. " <xs:simpleType>{0}" +
  770. " <xs:restriction base=\"xs:string\">{0}" +
  771. " <xs:enumeration value=\"one\" />{0}" +
  772. " <xs:enumeration value=\"two\" />{0}" +
  773. " <xs:enumeration value=\"four\" />{0}" +
  774. " </xs:restriction>{0}" +
  775. " </xs:simpleType>{0}" +
  776. " </xs:list>{0}" +
  777. " </xs:simpleType>{0}" +
  778. "</xs:schema>", Environment.NewLine, ANamespace), sw.ToString (), "#3");
  779. overrides = new XmlAttributeOverrides ();
  780. attr = new XmlAttributes ();
  781. // remove the DefaultValue attribute on the Flags member
  782. overrides.Add (typeof (OptionalValueTypeContainer), "Flags", attr);
  783. // remove the DefaultValue attribute on the Attributes member
  784. overrides.Add (typeof (OptionalValueTypeContainer), "Attributes", attr);
  785. // remove the DefaultValue attribute on the IsEmpty member
  786. overrides.Add (typeof (OptionalValueTypeContainer), "IsEmpty", attr);
  787. // remove the DefaultValue attribute on the IsNull member
  788. overrides.Add (typeof (OptionalValueTypeContainer), "IsNull", attr);
  789. schemas = Export (typeof (OptionalValueTypeContainer), overrides, "urn:myNS");
  790. Assert.AreEqual (2, schemas.Count, "#4");
  791. sw.GetStringBuilder ().Length = 0;
  792. schemas[0].Write (sw);
  793. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  794. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  795. "<xs:schema xmlns:tns=\"{1}\" elementFormDefault=\"qualified\" targetNamespace=\"{1}\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  796. " <xs:import namespace=\"{2}\" />{0}" +
  797. " <xs:element name=\"optionalValue\" xmlns:q1=\"{2}\" type=\"q1:optionalValueType\" />{0}" +
  798. "</xs:schema>", Environment.NewLine, AnotherNamespace, ANamespace),
  799. sw.ToString (), "#5");
  800. sw.GetStringBuilder ().Length = 0;
  801. schemas[1].Write (sw);
  802. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  803. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  804. "<xs:schema xmlns:tns=\"{1}\" elementFormDefault=\"qualified\" targetNamespace=\"{1}\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  805. " <xs:complexType name=\"optionalValueType\">{0}" +
  806. " <xs:sequence>{0}" +
  807. " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"Attributes\" type=\"tns:FlagEnum\" />{0}" +
  808. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Flags\" type=\"tns:FlagEnum\" />{0}" +
  809. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"IsEmpty\" type=\"xs:boolean\" />{0}" +
  810. " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"IsNull\" type=\"xs:boolean\" />{0}" +
  811. " </xs:sequence>{0}" +
  812. " </xs:complexType>{0}" +
  813. " <xs:simpleType name=\"FlagEnum\">{0}" +
  814. " <xs:list>{0}" +
  815. " <xs:simpleType>{0}" +
  816. " <xs:restriction base=\"xs:string\">{0}" +
  817. " <xs:enumeration value=\"one\" />{0}" +
  818. " <xs:enumeration value=\"two\" />{0}" +
  819. " <xs:enumeration value=\"four\" />{0}" +
  820. " </xs:restriction>{0}" +
  821. " </xs:simpleType>{0}" +
  822. " </xs:list>{0}" +
  823. " </xs:simpleType>{0}" +
  824. "</xs:schema>", Environment.NewLine, ANamespace), sw.ToString (), "#6");
  825. }
  826. [Test]
  827. public void ExportClass_ReadOnlyProperties ()
  828. {
  829. XmlSchemas schemas = Export (typeof (ReadOnlyProperties), "NSReadOnlyProperties");
  830. Assert.AreEqual (1, schemas.Count, "#1");
  831. StringWriter sw = new StringWriter ();
  832. schemas[0].Write (sw);
  833. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  834. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  835. "<xs:schema xmlns:tns=\"NSReadOnlyProperties\" elementFormDefault=\"qualified\" targetNamespace=\"NSReadOnlyProperties\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  836. " <xs:element name=\"ReadOnlyProperties\" nillable=\"true\" type=\"tns:ReadOnlyProperties\" />{0}" +
  837. " <xs:complexType name=\"ReadOnlyProperties\" />{0}" +
  838. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  839. schemas = Export (typeof (ReadOnlyProperties));
  840. Assert.AreEqual (1, schemas.Count, "#3");
  841. sw.GetStringBuilder ().Length = 0;
  842. schemas[0].Write (sw);
  843. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  844. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  845. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  846. " <xs:element name=\"ReadOnlyProperties\" nillable=\"true\" type=\"ReadOnlyProperties\" />{0}" +
  847. " <xs:complexType name=\"ReadOnlyProperties\" />{0}" +
  848. "</xs:schema>", Environment.NewLine), sw.ToString (), "#4");
  849. }
  850. [Test]
  851. [Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
  852. public void ExportClass_ListDefaults ()
  853. {
  854. XmlSchemas schemas = Export (typeof (ListDefaults), "NSListDefaults");
  855. Assert.AreEqual (1, schemas.Count, "#1");
  856. StringWriter sw = new StringWriter ();
  857. schemas[0].Write (sw);
  858. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  859. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  860. "<xs:schema xmlns:tns=\"NSListDefaults\" elementFormDefault=\"qualified\" targetNamespace=\"NSListDefaults\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  861. " <xs:element name=\"root\" nillable=\"true\" type=\"tns:ListDefaults\" />{0}" +
  862. " <xs:complexType name=\"ListDefaults\">{0}" +
  863. " <xs:sequence>{0}" +
  864. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"list2\" type=\"tns:ArrayOfAnyType\" />{0}" +
  865. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"list3\" type=\"tns:ArrayOfAnyType\" />{0}" +
  866. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"list4\" type=\"tns:ArrayOfString\" />{0}" +
  867. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"e\" type=\"tns:SimpleClass\" />{0}" +
  868. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ed\" type=\"tns:SimpleClass\" />{0}" +
  869. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"str\" type=\"xs:string\" />{0}" +
  870. " </xs:sequence>{0}" +
  871. " </xs:complexType>{0}" +
  872. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  873. " <xs:sequence>{0}" +
  874. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  875. " </xs:sequence>{0}" +
  876. " </xs:complexType>{0}" +
  877. " <xs:complexType name=\"ArrayOfString\">{0}" +
  878. " <xs:sequence>{0}" +
  879. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"string\" nillable=\"true\" type=\"xs:string\" />{0}" +
  880. " </xs:sequence>{0}" +
  881. " </xs:complexType>{0}" +
  882. " <xs:complexType name=\"SimpleClass\">{0}" +
  883. " <xs:sequence>{0}" +
  884. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"something\" type=\"xs:string\" />{0}" +
  885. " </xs:sequence>{0}" +
  886. " </xs:complexType>{0}" +
  887. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  888. schemas = Export (typeof (ListDefaults));
  889. Assert.AreEqual (1, schemas.Count, "#3");
  890. sw.GetStringBuilder ().Length = 0;
  891. schemas[0].Write (sw);
  892. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  893. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  894. "<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  895. " <xs:element name=\"root\" nillable=\"true\" type=\"ListDefaults\" />{0}" +
  896. " <xs:complexType name=\"ListDefaults\">{0}" +
  897. " <xs:sequence>{0}" +
  898. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"list2\" type=\"ArrayOfAnyType\" />{0}" +
  899. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"list3\" type=\"ArrayOfAnyType\" />{0}" +
  900. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"list4\" type=\"ArrayOfString\" />{0}" +
  901. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"e\" type=\"SimpleClass\" />{0}" +
  902. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ed\" type=\"SimpleClass\" />{0}" +
  903. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"str\" type=\"xs:string\" />{0}" +
  904. " </xs:sequence>{0}" +
  905. " </xs:complexType>{0}" +
  906. " <xs:complexType name=\"ArrayOfAnyType\">{0}" +
  907. " <xs:sequence>{0}" +
  908. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"anyType\" nillable=\"true\" />{0}" +
  909. " </xs:sequence>{0}" +
  910. " </xs:complexType>{0}" +
  911. " <xs:complexType name=\"ArrayOfString\">{0}" +
  912. " <xs:sequence>{0}" +
  913. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"string\" nillable=\"true\" type=\"xs:string\" />{0}" +
  914. " </xs:sequence>{0}" +
  915. " </xs:complexType>{0}" +
  916. " <xs:complexType name=\"SimpleClass\">{0}" +
  917. " <xs:sequence>{0}" +
  918. " <xs:element minOccurs=\"0\" maxOccurs=\"1\" name=\"something\" type=\"xs:string\" />{0}" +
  919. " </xs:sequence>{0}" +
  920. " </xs:complexType>{0}" +
  921. "</xs:schema>", Environment.NewLine), sw.ToString (), "#4");
  922. }
  923. [Test]
  924. public void ExportClass_ClsPerson ()
  925. {
  926. XmlSchemas schemas = Export (typeof (clsPerson), "NSClsPerson");
  927. Assert.AreEqual (1, schemas.Co