XmlSchemaTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 TestSimpleMutualImport ()
  152. {
  153. XmlReader r = new XmlTextReader ("Test/XmlFiles/xsd/inter-inc-1.xsd");
  154. try {
  155. XmlSchema.Read (r, null).Compile (null);
  156. } finally {
  157. r.Close ();
  158. }
  159. }
  160. [Test]
  161. public void TestQualification ()
  162. {
  163. XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
  164. schema.Compile (null);
  165. XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
  166. AssertNotNull (el);
  167. XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
  168. XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
  169. XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
  170. AssertEquals (QName ("Bar", ""), elp.QualifiedName);
  171. schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
  172. schema.Compile (null);
  173. el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
  174. AssertNotNull (el);
  175. ct = el.ElementType as XmlSchemaComplexType;
  176. seq = ct.ContentTypeParticle as XmlSchemaSequence;
  177. elp = seq.Items [0] as XmlSchemaElement;
  178. AssertEquals (QName ("Bar", "urn:bar"), elp.QualifiedName);
  179. }
  180. [Test]
  181. public void TestWriteNamespaces ()
  182. {
  183. XmlDocument doc = new XmlDocument ();
  184. XmlSchema xs;
  185. StringWriter sw;
  186. XmlTextWriter xw;
  187. // empty
  188. xs = new XmlSchema ();
  189. sw = new StringWriter ();
  190. xw = new XmlTextWriter (sw);
  191. xs.Write (xw);
  192. doc.LoadXml (sw.ToString ());
  193. AssertEquals ("#1", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  194. // TargetNamespace
  195. xs = new XmlSchema ();
  196. sw = new StringWriter ();
  197. xw = new XmlTextWriter (sw);
  198. xs.TargetNamespace = "urn:foo";
  199. xs.Write (xw);
  200. doc.LoadXml (sw.ToString ());
  201. AssertEquals ("#2", "<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  202. // Zero-length TargetNamespace
  203. xs = new XmlSchema ();
  204. sw = new StringWriter ();
  205. xw = new XmlTextWriter (sw);
  206. xs.TargetNamespace = string.Empty;
  207. xs.Write (xw);
  208. doc.LoadXml (sw.ToString ());
  209. AssertEquals ("#2b", "<xs:schema targetNamespace=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  210. // XmlSerializerNamespaces
  211. xs = new XmlSchema ();
  212. sw = new StringWriter ();
  213. xw = new XmlTextWriter (sw);
  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 ("#3", "<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  219. // TargetNamespace + XmlSerializerNamespaces
  220. xs = new XmlSchema ();
  221. sw = new StringWriter ();
  222. xw = new XmlTextWriter (sw);
  223. xs.TargetNamespace = "urn:foo";
  224. xs.Namespaces.Add ("hoge", "urn:hoge");
  225. xs.Write (xw);
  226. doc.LoadXml (sw.ToString ());
  227. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  228. // AssertEquals ("#4", "<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  229. // Add XmlSchema.Namespace to XmlSerializerNamespaces
  230. xs = new XmlSchema ();
  231. sw = new StringWriter ();
  232. xw = new XmlTextWriter (sw);
  233. xs.Namespaces.Add ("a", XmlSchema.Namespace);
  234. xs.Write (xw);
  235. doc.LoadXml (sw.ToString ());
  236. AssertEquals ("#5", "<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  237. // UnhandledAttributes + XmlSerializerNamespaces
  238. xs = new XmlSchema ();
  239. sw = new StringWriter ();
  240. xw = new XmlTextWriter (sw);
  241. XmlAttribute attr = doc.CreateAttribute ("hoge");
  242. xs.UnhandledAttributes = new XmlAttribute [] {attr};
  243. xs.Namespaces.Add ("hoge", "urn:hoge");
  244. xs.Write (xw);
  245. doc.LoadXml (sw.ToString ());
  246. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  247. // AssertEquals ("#6", "<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  248. // Adding xmlns to UnhandledAttributes -> no output
  249. xs = new XmlSchema ();
  250. sw = new StringWriter ();
  251. xw = new XmlTextWriter (sw);
  252. attr = doc.CreateAttribute ("xmlns");
  253. attr.Value = "urn:foo";
  254. xs.UnhandledAttributes = new XmlAttribute [] {attr};
  255. xs.Write (xw);
  256. doc.LoadXml (sw.ToString ());
  257. AssertEquals ("#7", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  258. }
  259. [Category ("NotWorking")]
  260. [Test]
  261. public void TestWriteNamespaces2 ()
  262. {
  263. string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
  264. XmlSchema xs = new XmlSchema ();
  265. XmlSerializerNamespaces nss =
  266. new XmlSerializerNamespaces ();
  267. StringWriter sw;
  268. sw = new StringWriter ();
  269. xs.Write (new XmlTextWriter (sw));
  270. AssertEquals ("#1", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  271. xs.Namespaces = nss;
  272. sw = new StringWriter ();
  273. xs.Write (new XmlTextWriter (sw));
  274. AssertEquals ("#2", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  275. nss.Add ("foo", "urn:foo");
  276. sw = new StringWriter ();
  277. xs.Write (new XmlTextWriter (sw));
  278. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  279. // AssertEquals ("#3", xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  280. nss.Add ("", "urn:foo");
  281. sw = new StringWriter ();
  282. xs.Write (new XmlTextWriter (sw));
  283. // commenting out. .NET 2.0 outputs xs:schema instead of q1:schema, that also makes sense.
  284. // AssertEquals ("#4", xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  285. nss.Add ("q1", "urn:q1");
  286. sw = new StringWriter ();
  287. xs.Write (new XmlTextWriter (sw));
  288. //Not sure if testing for exact order of these name spaces is
  289. // relevent, so using less strict test that passes on MS.NET
  290. //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 ());
  291. Assert("q1", sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1);
  292. }
  293. [Test]
  294. public void ReaderPositionAfterRead ()
  295. {
  296. string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'> <xs:element name='test' type='xs:integer'/></xs:schema>";
  297. XmlTextReader xtr = new XmlTextReader (xsd, XmlNodeType.Document, null);
  298. xtr.Read ();
  299. XmlSchema xs = XmlSchema.Read (xtr, null);
  300. AssertEquals (XmlNodeType.EndElement, xtr.NodeType);
  301. }
  302. [Test]
  303. // bug #76865
  304. public void AmbiguityDetectionOnChameleonAnyOther ()
  305. {
  306. string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  307. <xs:complexType name='TestType'>
  308. <xs:sequence>
  309. <xs:any namespace='##other' minOccurs='0' />
  310. <xs:element name='Item' />
  311. <xs:any namespace='##other' minOccurs='0' />
  312. </xs:sequence>
  313. </xs:complexType>
  314. </xs:schema>";
  315. XmlSchema.Read (new XmlTextReader (xsd, XmlNodeType.Document, null), null);
  316. }
  317. [Test]
  318. // bug #77685
  319. public void ReadDoesNotIgnoreDocumentationEmptyElement ()
  320. {
  321. string schemaxml = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  322. <xs:element name='choice'>
  323. <xs:annotation><xs:documentation /></xs:annotation>
  324. </xs:element>
  325. </xs:schema>";
  326. XmlTextReader tr = new XmlTextReader (
  327. schemaxml, XmlNodeType.Document, null);
  328. XmlSchema schema = XmlSchema.Read (tr, null);
  329. XmlSchemaElement element =
  330. schema.Items [0] as XmlSchemaElement;
  331. XmlSchemaAnnotation annotation = element.Annotation;
  332. XmlSchemaDocumentation doc =
  333. annotation.Items [0] as XmlSchemaDocumentation;
  334. AssertEquals (0, doc.Markup.Length);
  335. }
  336. [Test]
  337. // bug #77687
  338. public void CompileFillsSchemaPropertyInExternal ()
  339. {
  340. string schemaFileName = "Test/XmlFiles/xsd/77687.xsd";
  341. XmlTextReader tr = new XmlTextReader (schemaFileName);
  342. XmlSchema schema = XmlSchema.Read (tr, null);
  343. XmlSchemaInclude inc = (XmlSchemaInclude) schema.Includes [0];
  344. AssertNull (inc.Schema);
  345. schema.Compile (null);
  346. tr.Close ();
  347. AssertNotNull (inc.Schema);
  348. }
  349. [Test]
  350. // bug #78985 (contains two identical field path "@key" in
  351. // two different keys where one is in scope within another)
  352. public void DuplicateKeyFieldAttributePath ()
  353. {
  354. string schemaFileName = "Test/XmlFiles/xsd/78985.xsd";
  355. string xmlFileName = "Test/XmlFiles/xsd/78985.xml";
  356. XmlTextReader tr = new XmlTextReader (schemaFileName);
  357. XmlValidatingReader vr = new XmlValidatingReader (
  358. new XmlTextReader (xmlFileName));
  359. vr.Schemas.Add (XmlSchema.Read (tr, null));
  360. while (!vr.EOF)
  361. vr.Read ();
  362. }
  363. }
  364. }