XmlSchemaTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //
  2. // System.Xml.XmlSchemaTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2002 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Xml;
  12. using System.Xml.Schema;
  13. using System.Xml.Serialization;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Xml
  16. {
  17. [TestFixture]
  18. public class XmlSchemaTests : XmlSchemaAssertion
  19. {
  20. [Test]
  21. public void TestRead ()
  22. {
  23. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
  24. AssertEquals (6, schema.Items.Count);
  25. bool fooValidated = false;
  26. bool barValidated = false;
  27. string ns = "urn:bar";
  28. foreach (XmlSchemaObject obj in schema.Items) {
  29. XmlSchemaElement element = obj as XmlSchemaElement;
  30. if (element == null)
  31. continue;
  32. if (element.Name == "Foo") {
  33. AssertElement (element, "Foo",
  34. XmlQualifiedName.Empty, null,
  35. QName ("string", XmlSchema.Namespace), null);
  36. fooValidated = true;
  37. }
  38. if (element.Name == "Bar") {
  39. AssertElement (element, "Bar",
  40. XmlQualifiedName.Empty, null, QName ("FugaType", ns), null);
  41. barValidated = true;
  42. }
  43. }
  44. Assert (fooValidated);
  45. Assert (barValidated);
  46. }
  47. [Test]
  48. public void TestReadFlags ()
  49. {
  50. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
  51. schema.Compile (null);
  52. XmlSchemaElement el = schema.Items [0] as XmlSchemaElement;
  53. AssertNotNull (el);
  54. AssertEquals (XmlSchemaDerivationMethod.Extension, el.Block);
  55. el = schema.Items [1] as XmlSchemaElement;
  56. AssertNotNull (el);
  57. AssertEquals (XmlSchemaDerivationMethod.Extension |
  58. XmlSchemaDerivationMethod.Restriction, el.Block);
  59. }
  60. [Test]
  61. public void TestWriteFlags ()
  62. {
  63. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
  64. StringWriter sw = new StringWriter ();
  65. XmlTextWriter xtw = new XmlTextWriter (sw);
  66. schema.Write (xtw);
  67. }
  68. [Test]
  69. public void TestCompile ()
  70. {
  71. XmlQualifiedName qname;
  72. XmlSchemaComplexContentExtension xccx;
  73. XmlSchemaComplexType cType;
  74. XmlSchemaSequence seq;
  75. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
  76. // Assert (!schema.IsCompiled);
  77. schema.Compile (null);
  78. Assert (schema.IsCompiled);
  79. string ns = "urn:bar";
  80. XmlSchemaElement foo = (XmlSchemaElement) schema.Elements [QName ("Foo", ns)];
  81. AssertNotNull (foo);
  82. XmlSchemaDatatype stringDatatype = foo.ElementType as XmlSchemaDatatype;
  83. AssertNotNull (stringDatatype);
  84. // HogeType
  85. qname = QName ("HogeType", ns);
  86. cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
  87. AssertNotNull (cType);
  88. AssertNull (cType.ContentModel);
  89. AssertCompiledComplexType (cType, qname, 0, 0,
  90. false, null, true, XmlSchemaContentType.ElementOnly);
  91. seq = cType.ContentTypeParticle as XmlSchemaSequence;
  92. AssertNotNull (seq);
  93. AssertEquals (2, seq.Items.Count);
  94. XmlSchemaElement refFoo = seq.Items [0] as XmlSchemaElement;
  95. AssertCompiledElement (refFoo, QName ("Foo", ns), stringDatatype);
  96. // FugaType
  97. qname = QName ("FugaType", ns);
  98. cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
  99. AssertNotNull (cType);
  100. xccx = cType.ContentModel.Content as XmlSchemaComplexContentExtension;
  101. AssertCompiledComplexContentExtension (
  102. xccx, 0, false, QName ("HogeType", ns));
  103. AssertCompiledComplexType (cType, qname, 0, 0,
  104. false, typeof (XmlSchemaComplexContent),
  105. true, XmlSchemaContentType.ElementOnly);
  106. AssertNotNull (cType.BaseSchemaType);
  107. seq = xccx.Particle as XmlSchemaSequence;
  108. AssertNotNull (seq);
  109. AssertEquals (1, seq.Items.Count);
  110. XmlSchemaElement refBaz = seq.Items [0] as XmlSchemaElement;
  111. AssertNotNull (refBaz);
  112. AssertCompiledElement (refBaz, QName ("Baz", ""), stringDatatype);
  113. qname = QName ("Bar", ns);
  114. XmlSchemaElement element = schema.Elements [qname] as XmlSchemaElement;
  115. AssertCompiledElement (element, qname, cType);
  116. }
  117. [Test]
  118. [ExpectedException (typeof (XmlSchemaException))]
  119. public void TestCompile_ZeroLength_TargetNamespace ()
  120. {
  121. XmlSchema schema = new XmlSchema ();
  122. schema.TargetNamespace = string.Empty;
  123. Assert (!schema.IsCompiled);
  124. // MS.NET 1.x: The Namespace '' is an invalid URI.
  125. // MS.NET 2.0: The targetNamespace attribute cannot have empty string as its value.
  126. schema.Compile (null);
  127. }
  128. [Test]
  129. [ExpectedException (typeof (XmlSchemaException))]
  130. public void TestCompileNonSchema ()
  131. {
  132. XmlTextReader xtr = new XmlTextReader ("<root/>", XmlNodeType.Document, null);
  133. XmlSchema schema = XmlSchema.Read (xtr, null);
  134. xtr.Close ();
  135. }
  136. [Test]
  137. public void TestSimpleImport ()
  138. {
  139. XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/3.xsd"), null);
  140. AssertEquals ("urn:foo", schema.TargetNamespace);
  141. XmlSchemaImport import = schema.Includes [0] as XmlSchemaImport;
  142. AssertNotNull (import);
  143. schema.Compile (null);
  144. AssertEquals (4, schema.Elements.Count);
  145. AssertNotNull (schema.Elements [QName ("Foo", "urn:foo")]);
  146. AssertNotNull (schema.Elements [QName ("Bar", "urn:foo")]);
  147. AssertNotNull (schema.Elements [QName ("Foo", "urn:bar")]);
  148. AssertNotNull (schema.Elements [QName ("Bar", "urn:bar")]);
  149. }
  150. [Test]
  151. public void TestQualification ()
  152. {
  153. XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
  154. schema.Compile (null);
  155. XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
  156. AssertNotNull (el);
  157. XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
  158. XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
  159. XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
  160. AssertEquals (QName ("Bar", ""), elp.QualifiedName);
  161. schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
  162. schema.Compile (null);
  163. el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
  164. AssertNotNull (el);
  165. ct = el.ElementType as XmlSchemaComplexType;
  166. seq = ct.ContentTypeParticle as XmlSchemaSequence;
  167. elp = seq.Items [0] as XmlSchemaElement;
  168. AssertEquals (QName ("Bar", "urn:bar"), elp.QualifiedName);
  169. }
  170. [Test]
  171. public void TestWriteNamespaces ()
  172. {
  173. XmlDocument doc = new XmlDocument ();
  174. XmlSchema xs;
  175. StringWriter sw;
  176. XmlTextWriter xw;
  177. // empty
  178. xs = new XmlSchema ();
  179. sw = new StringWriter ();
  180. xw = new XmlTextWriter (sw);
  181. xs.Write (xw);
  182. doc.LoadXml (sw.ToString ());
  183. AssertEquals ("#1", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  184. // TargetNamespace
  185. xs = new XmlSchema ();
  186. sw = new StringWriter ();
  187. xw = new XmlTextWriter (sw);
  188. xs.TargetNamespace = "urn:foo";
  189. xs.Write (xw);
  190. doc.LoadXml (sw.ToString ());
  191. AssertEquals ("#2", "<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  192. // Zero-length TargetNamespace
  193. xs = new XmlSchema ();
  194. sw = new StringWriter ();
  195. xw = new XmlTextWriter (sw);
  196. xs.TargetNamespace = string.Empty;
  197. xs.Write (xw);
  198. doc.LoadXml (sw.ToString ());
  199. AssertEquals ("#2b", "<xs:schema targetNamespace=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  200. // XmlSerializerNamespaces
  201. xs = new XmlSchema ();
  202. sw = new StringWriter ();
  203. xw = new XmlTextWriter (sw);
  204. xs.Namespaces.Add ("hoge", "urn:hoge");
  205. xs.Write (xw);
  206. doc.LoadXml (sw.ToString ());
  207. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  208. // AssertEquals ("#3", "<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  209. // TargetNamespace + XmlSerializerNamespaces
  210. xs = new XmlSchema ();
  211. sw = new StringWriter ();
  212. xw = new XmlTextWriter (sw);
  213. xs.TargetNamespace = "urn:foo";
  214. xs.Namespaces.Add ("hoge", "urn:hoge");
  215. xs.Write (xw);
  216. doc.LoadXml (sw.ToString ());
  217. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  218. // AssertEquals ("#4", "<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  219. // Add XmlSchema.Namespace to XmlSerializerNamespaces
  220. xs = new XmlSchema ();
  221. sw = new StringWriter ();
  222. xw = new XmlTextWriter (sw);
  223. xs.Namespaces.Add ("a", XmlSchema.Namespace);
  224. xs.Write (xw);
  225. doc.LoadXml (sw.ToString ());
  226. AssertEquals ("#5", "<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  227. // UnhandledAttributes + XmlSerializerNamespaces
  228. xs = new XmlSchema ();
  229. sw = new StringWriter ();
  230. xw = new XmlTextWriter (sw);
  231. XmlAttribute attr = doc.CreateAttribute ("hoge");
  232. xs.UnhandledAttributes = new XmlAttribute [] {attr};
  233. xs.Namespaces.Add ("hoge", "urn:hoge");
  234. xs.Write (xw);
  235. doc.LoadXml (sw.ToString ());
  236. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  237. // AssertEquals ("#6", "<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  238. // Adding xmlns to UnhandledAttributes -> no output
  239. xs = new XmlSchema ();
  240. sw = new StringWriter ();
  241. xw = new XmlTextWriter (sw);
  242. attr = doc.CreateAttribute ("xmlns");
  243. attr.Value = "urn:foo";
  244. xs.UnhandledAttributes = new XmlAttribute [] {attr};
  245. xs.Write (xw);
  246. doc.LoadXml (sw.ToString ());
  247. AssertEquals ("#7", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  248. }
  249. [Category ("NotWorking")]
  250. [Test]
  251. public void TestWriteNamespaces2 ()
  252. {
  253. string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
  254. XmlSchema xs = new XmlSchema ();
  255. XmlSerializerNamespaces nss =
  256. new XmlSerializerNamespaces ();
  257. StringWriter sw;
  258. sw = new StringWriter ();
  259. xs.Write (new XmlTextWriter (sw));
  260. AssertEquals ("#1", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  261. xs.Namespaces = nss;
  262. sw = new StringWriter ();
  263. xs.Write (new XmlTextWriter (sw));
  264. AssertEquals ("#2", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  265. nss.Add ("foo", "urn:foo");
  266. sw = new StringWriter ();
  267. xs.Write (new XmlTextWriter (sw));
  268. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  269. // AssertEquals ("#3", xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  270. nss.Add ("", "urn:foo");
  271. sw = new StringWriter ();
  272. xs.Write (new XmlTextWriter (sw));
  273. // commenting out. .NET 2.0 outputs xs:schema instead of q1:schema, that also makes sense.
  274. // AssertEquals ("#4", xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  275. nss.Add ("q1", "urn:q1");
  276. sw = new StringWriter ();
  277. xs.Write (new XmlTextWriter (sw));
  278. //Not sure if testing for exact order of these name spaces is
  279. // relevent, so using less strict test that passes on MS.NET
  280. //AssertEquals (xmldecl + "<q2:schema xmlns:foo=\"urn:foo\" xmlns:q1=\"urn:q1\" xmlns=\"urn:foo\" xmlns:q2=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  281. Assert("q1", sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1);
  282. }
  283. [Test]
  284. public void ReaderPositionAfterRead ()
  285. {
  286. string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'> <xs:element name='test' type='xs:integer'/></xs:schema>";
  287. XmlTextReader xtr = new XmlTextReader (xsd, XmlNodeType.Document, null);
  288. xtr.Read ();
  289. XmlSchema xs = XmlSchema.Read (xtr, null);
  290. AssertEquals (XmlNodeType.EndElement, xtr.NodeType);
  291. }
  292. [Test]
  293. // bug #76865
  294. public void AmbiguityDetectionOnChameleonAnyOther ()
  295. {
  296. string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  297. <xs:complexType name='TestType'>
  298. <xs:sequence>
  299. <xs:any namespace='##other' minOccurs='0' />
  300. <xs:element name='Item' />
  301. <xs:any namespace='##other' minOccurs='0' />
  302. </xs:sequence>
  303. </xs:complexType>
  304. </xs:schema>";
  305. XmlSchema.Read (new XmlTextReader (xsd, XmlNodeType.Document, null), null);
  306. }
  307. [Test]
  308. // bug #77685
  309. public void ReadDoesNotIgnoreDocumentationEmptyElement ()
  310. {
  311. string schemaxml = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  312. <xs:element name='choice'>
  313. <xs:annotation><xs:documentation /></xs:annotation>
  314. </xs:element>
  315. </xs:schema>";
  316. XmlTextReader tr = new XmlTextReader (
  317. schemaxml, XmlNodeType.Document, null);
  318. XmlSchema schema = XmlSchema.Read (tr, null);
  319. XmlSchemaElement element =
  320. schema.Items [0] as XmlSchemaElement;
  321. XmlSchemaAnnotation annotation = element.Annotation;
  322. XmlSchemaDocumentation doc =
  323. annotation.Items [0] as XmlSchemaDocumentation;
  324. AssertEquals (0, doc.Markup.Length);
  325. }
  326. [Test]
  327. // bug #77687
  328. public void CompileFillsSchemaPropertyInExternal ()
  329. {
  330. string schemaFileName = "Test/XmlFiles/xsd/77687.xsd";
  331. XmlTextReader tr = new XmlTextReader (schemaFileName);
  332. XmlSchema schema = XmlSchema.Read (tr, null);
  333. XmlSchemaInclude inc = (XmlSchemaInclude) schema.Includes [0];
  334. AssertNull (inc.Schema);
  335. schema.Compile (null);
  336. tr.Close ();
  337. AssertNotNull (inc.Schema);
  338. }
  339. }
  340. }