XmlSchemaSetTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // System.Xml.XmlSchemaSetTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2004 Novell Inc.
  8. //
  9. #if NET_2_0
  10. using System;
  11. using System.Collections;
  12. using System.IO;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlSchemaSetTests
  20. {
  21. [Test]
  22. public void Add ()
  23. {
  24. XmlSchemaSet ss = new XmlSchemaSet ();
  25. XmlDocument doc = new XmlDocument ();
  26. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
  27. ss.Add (null, new XmlNodeReader (doc)); // null targetNamespace
  28. ss.Compile ();
  29. // same document, different targetNamespace
  30. ss.Add ("ab", new XmlNodeReader (doc));
  31. // Add(null, xmlReader) -> targetNamespace in the schema
  32. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:foo' />");
  33. ss.Add (null, new XmlNodeReader (doc));
  34. Assert.AreEqual (3, ss.Count);
  35. bool chameleon = false;
  36. bool ab = false;
  37. bool urnfoo = false;
  38. foreach (XmlSchema schema in ss.Schemas ()) {
  39. if (schema.TargetNamespace == null)
  40. chameleon = true;
  41. else if (schema.TargetNamespace == "ab")
  42. ab = true;
  43. else if (schema.TargetNamespace == "urn:foo")
  44. urnfoo = true;
  45. }
  46. Assert.IsTrue (chameleon, "chameleon schema missing");
  47. Assert.IsTrue (ab, "target-remapped schema missing");
  48. Assert.IsTrue (urnfoo, "target specified in the schema ignored");
  49. }
  50. [Test]
  51. [ExpectedException (typeof (XmlSchemaException))]
  52. public void AddWrongTargetNamespace ()
  53. {
  54. string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='el' type='xs:int' /></xs:schema>";
  55. string xml = "<el xmlns='urn:foo'>a</el>";
  56. XmlSchemaSet xss = new XmlSchemaSet ();
  57. // unlike null, "" is regarded as an explicit
  58. // empty namespace indication.
  59. xss.Add ("", new XmlTextReader (new StringReader (xsd)));
  60. }
  61. [Test]
  62. public void AddSchemaThenReader ()
  63. {
  64. XmlSchemaSet ss = new XmlSchemaSet ();
  65. XmlDocument doc = new XmlDocument ();
  66. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
  67. XmlSchema xs = new XmlSchema ();
  68. xs.TargetNamespace = "ab";
  69. ss.Add (xs);
  70. ss.Add ("ab", new XmlNodeReader (doc));
  71. }
  72. [Test]
  73. [Category ("NotWorking")] // How can we differentiate this
  74. // case and the testcase above?
  75. [ExpectedException (typeof (ArgumentException))]
  76. public void AddReaderTwice ()
  77. {
  78. XmlSchemaSet ss = new XmlSchemaSet ();
  79. XmlDocument doc = new XmlDocument ();
  80. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
  81. ss.Add ("ab", new XmlNodeReader (doc));
  82. ss.Add ("ab", new XmlNodeReader (doc));
  83. }
  84. [Test]
  85. public void AddSchemaTwice ()
  86. {
  87. XmlSchemaSet ss = new XmlSchemaSet ();
  88. XmlDocument doc = new XmlDocument ();
  89. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:ab' />");
  90. ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
  91. ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
  92. }
  93. [Test]
  94. public void CompilationSettings ()
  95. {
  96. Assert.IsNotNull (new XmlSchemaSet ().CompilationSettings);
  97. new XmlSchemaSet ().CompilationSettings = null;
  98. }
  99. [Test]
  100. public void DisableUpaCheck ()
  101. {
  102. string schema = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  103. <xs:complexType name='Foo'>
  104. <xs:sequence>
  105. <xs:choice minOccurs='0'>
  106. <xs:element name='el'/>
  107. </xs:choice>
  108. <xs:element name='el' />
  109. </xs:sequence>
  110. </xs:complexType>
  111. </xs:schema>";
  112. XmlSchema xs = XmlSchema.Read (new XmlTextReader (
  113. schema, XmlNodeType.Document, null), null);
  114. XmlSchemaSet xss = new XmlSchemaSet ();
  115. xss.Add (xs);
  116. xss.CompilationSettings.EnableUpaCheck = false;
  117. xss.Compile ();
  118. }
  119. [Test]
  120. public void AddRollbackIsCompiled ()
  121. {
  122. XmlSchemaSet ss = new XmlSchemaSet ();
  123. ss.Add (new XmlSchema ());
  124. ss.Compile ();
  125. Assert.IsTrue (ss.IsCompiled, "#1");
  126. XmlSchema sc = new XmlSchema (); // compiled one
  127. sc.Compile (null);
  128. ss.Add (sc);
  129. Assert.IsFalse (ss.IsCompiled, "#2");
  130. ss.Add (new XmlSchema ()); // not-compiled one
  131. Assert.IsFalse (ss.IsCompiled, "#3");
  132. XmlSchema s;
  133. s = new XmlSchema ();
  134. s.TargetNamespace = "urn:foo";
  135. XmlSchemaElement el;
  136. el = new XmlSchemaElement ();
  137. el.Name = "root";
  138. s.Items.Add (el);
  139. ss.Add (s);
  140. s = new XmlSchema ();
  141. s.TargetNamespace = "urn:foo";
  142. el = new XmlSchemaElement ();
  143. el.Name = "foo";
  144. s.Items.Add (el);
  145. ss.Add (s);
  146. ss.Compile ();
  147. Assert.IsTrue (ss.IsCompiled, "#4");
  148. ss.RemoveRecursive (s);
  149. Assert.IsTrue (ss.IsCompiled, "#5");
  150. }
  151. [Test] // bug #77489
  152. public void CrossSchemaReferences ()
  153. {
  154. string schema1 = @"<xsd:schema id=""Base.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  155. <xsd:complexType name=""itemBase"" abstract=""true"">
  156. <xsd:attribute name=""id"" type=""xsd:string""
  157. use=""required""/>
  158. <xsd:attribute name=""type"" type=""xsd:string""
  159. use=""required""/>
  160. </xsd:complexType>
  161. </xsd:schema>";
  162. string schema2 = @"<xsd:schema id=""Sub.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  163. <xsd:complexType name=""item"">
  164. <xsd:complexContent>
  165. <xsd:extension base=""itemBase"">
  166. <xsd:attribute name=""itemName""
  167. type=""xsd:string"" use=""required""/>
  168. </xsd:extension>
  169. </xsd:complexContent>
  170. </xsd:complexType>
  171. </xsd:schema>";
  172. XmlSchemaSet schemas = new XmlSchemaSet ();
  173. schemas.Add (XmlSchema.Read (new StringReader (schema1), null));
  174. schemas.Add (XmlSchema.Read (new StringReader (schema2), null));
  175. schemas.Compile ();
  176. }
  177. }
  178. }
  179. #endif