XmlSchemaSetTests.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // System.Xml.XmlSchemaSetTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2004 Novell Inc.
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Text;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. using NUnit.Framework;
  16. using MonoTests.Helpers;
  17. namespace MonoTests.System.Xml
  18. {
  19. [TestFixture]
  20. public class XmlSchemaSetTests
  21. {
  22. [Test]
  23. public void Add ()
  24. {
  25. XmlSchemaSet ss = new XmlSchemaSet ();
  26. XmlDocument doc = new XmlDocument ();
  27. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
  28. ss.Add (null, new XmlNodeReader (doc)); // null targetNamespace
  29. ss.Compile ();
  30. // same document, different targetNamespace
  31. ss.Add ("ab", new XmlNodeReader (doc));
  32. // Add(null, xmlReader) -> targetNamespace in the schema
  33. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:foo' />");
  34. ss.Add (null, new XmlNodeReader (doc));
  35. Assert.AreEqual (3, ss.Count);
  36. bool chameleon = false;
  37. bool ab = false;
  38. bool urnfoo = false;
  39. foreach (XmlSchema schema in ss.Schemas ()) {
  40. if (schema.TargetNamespace == null)
  41. chameleon = true;
  42. else if (schema.TargetNamespace == "ab")
  43. ab = true;
  44. else if (schema.TargetNamespace == "urn:foo")
  45. urnfoo = true;
  46. }
  47. Assert.IsTrue (chameleon, "chameleon schema missing");
  48. Assert.IsTrue (ab, "target-remapped schema missing");
  49. Assert.IsTrue (urnfoo, "target specified in the schema ignored");
  50. }
  51. [Test]
  52. [ExpectedException (typeof (XmlSchemaException))]
  53. public void AddWrongTargetNamespace ()
  54. {
  55. string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='el' type='xs:int' /></xs:schema>";
  56. string xml = "<el xmlns='urn:foo'>a</el>";
  57. XmlSchemaSet xss = new XmlSchemaSet ();
  58. // unlike null, "" is regarded as an explicit
  59. // empty namespace indication.
  60. xss.Add ("", new XmlTextReader (new StringReader (xsd)));
  61. }
  62. [Test]
  63. public void AddSchemaThenReader ()
  64. {
  65. XmlSchemaSet ss = new XmlSchemaSet ();
  66. XmlDocument doc = new XmlDocument ();
  67. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
  68. XmlSchema xs = new XmlSchema ();
  69. xs.TargetNamespace = "ab";
  70. ss.Add (xs);
  71. ss.Add ("ab", new XmlNodeReader (doc));
  72. }
  73. [Test]
  74. [Category ("NotWorking")] // How can we differentiate this
  75. // case and the testcase above?
  76. [ExpectedException (typeof (ArgumentException))]
  77. public void AddReaderTwice ()
  78. {
  79. XmlSchemaSet ss = new XmlSchemaSet ();
  80. XmlDocument doc = new XmlDocument ();
  81. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
  82. ss.Add ("ab", new XmlNodeReader (doc));
  83. ss.Add ("ab", new XmlNodeReader (doc));
  84. }
  85. [Test]
  86. public void AddSchemaTwice ()
  87. {
  88. XmlSchemaSet ss = new XmlSchemaSet ();
  89. XmlDocument doc = new XmlDocument ();
  90. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:ab' />");
  91. ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
  92. ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
  93. }
  94. [Test]
  95. public void CompilationSettings ()
  96. {
  97. Assert.IsNotNull (new XmlSchemaSet ().CompilationSettings);
  98. new XmlSchemaSet ().CompilationSettings = null;
  99. }
  100. [Test]
  101. public void DisableUpaCheck ()
  102. {
  103. string schema = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  104. <xs:complexType name='Foo'>
  105. <xs:sequence>
  106. <xs:choice minOccurs='0'>
  107. <xs:element name='el'/>
  108. </xs:choice>
  109. <xs:element name='el' />
  110. </xs:sequence>
  111. </xs:complexType>
  112. </xs:schema>";
  113. XmlSchema xs = XmlSchema.Read (new XmlTextReader (
  114. schema, XmlNodeType.Document, null), null);
  115. XmlSchemaSet xss = new XmlSchemaSet ();
  116. xss.Add (xs);
  117. xss.CompilationSettings.EnableUpaCheck = false;
  118. xss.Compile ();
  119. }
  120. [Test]
  121. public void AddRollbackIsCompiled ()
  122. {
  123. XmlSchemaSet ss = new XmlSchemaSet ();
  124. ss.Add (new XmlSchema ());
  125. ss.Compile ();
  126. Assert.IsTrue (ss.IsCompiled, "#1");
  127. XmlSchema sc = new XmlSchema (); // compiled one
  128. sc.Compile (null);
  129. ss.Add (sc);
  130. Assert.IsFalse (ss.IsCompiled, "#2");
  131. ss.Add (new XmlSchema ()); // not-compiled one
  132. Assert.IsFalse (ss.IsCompiled, "#3");
  133. XmlSchema s;
  134. s = new XmlSchema ();
  135. s.TargetNamespace = "urn:foo";
  136. XmlSchemaElement el;
  137. el = new XmlSchemaElement ();
  138. el.Name = "root";
  139. s.Items.Add (el);
  140. ss.Add (s);
  141. s = new XmlSchema ();
  142. s.TargetNamespace = "urn:foo";
  143. el = new XmlSchemaElement ();
  144. el.Name = "foo";
  145. s.Items.Add (el);
  146. ss.Add (s);
  147. ss.Compile ();
  148. Assert.IsTrue (ss.IsCompiled, "#4");
  149. ss.RemoveRecursive (s);
  150. Assert.IsFalse (ss.IsCompiled, "#5");
  151. }
  152. [Test] // bug #77489
  153. public void CrossSchemaReferences ()
  154. {
  155. string schema1 = @"<xsd:schema id=""Base.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  156. <xsd:complexType name=""itemBase"" abstract=""true"">
  157. <xsd:attribute name=""id"" type=""xsd:string""
  158. use=""required""/>
  159. <xsd:attribute name=""type"" type=""xsd:string""
  160. use=""required""/>
  161. </xsd:complexType>
  162. </xsd:schema>";
  163. string schema2 = @"<xsd:schema id=""Sub.Schema"" elementFormDefault=""qualified"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  164. <xsd:complexType name=""item"">
  165. <xsd:complexContent>
  166. <xsd:extension base=""itemBase"">
  167. <xsd:attribute name=""itemName""
  168. type=""xsd:string"" use=""required""/>
  169. </xsd:extension>
  170. </xsd:complexContent>
  171. </xsd:complexType>
  172. </xsd:schema>";
  173. XmlSchemaSet schemas = new XmlSchemaSet ();
  174. schemas.Add (XmlSchema.Read (new StringReader (schema1), null));
  175. schemas.Add (XmlSchema.Read (new StringReader (schema2), null));
  176. schemas.Compile ();
  177. }
  178. [Test]
  179. public void ImportSubstitutionGroupDBR ()
  180. {
  181. // This bug happened when
  182. // 1) a schema imports another schema,
  183. // 2) there is a substitutionGroup which is involved in
  184. // complexContent schema conformance check, and
  185. // 3) the included schema is already added to XmlSchemaSet.
  186. XmlSchemaSet xss = new XmlSchemaSet ();
  187. xss.Add (null, TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/import-subst-dbr-base.xsd"));
  188. xss.Add (null, TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/import-subst-dbr-ext.xsd"));
  189. // should not result in lack of substitutionGroup
  190. // (and conformance error as its result)
  191. xss.Compile ();
  192. }
  193. [Test]
  194. public void AddWithNullTargetNS () // bug #571650
  195. {
  196. var xsdraw = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='foo' /></xs:schema>";
  197. var schemas = new XmlSchemaSet ();
  198. var xsd = schemas.Add ("", XmlReader.Create (new StringReader (xsdraw)));
  199. Assert.IsNull (xsd.TargetNamespace, "#1");
  200. }
  201. [Test] // part of bug #670945
  202. public void TwoSchemasInSameDocumentUri ()
  203. {
  204. string xsd1 = @"
  205. <xs:schema
  206. targetNamespace='http://www.onvif.org/ver10/schema'
  207. elementFormDefault='qualified'
  208. xmlns:xs='http://www.w3.org/2001/XMLSchema'
  209. xmlns:tt='http://www.onvif.org/ver10/schema'>
  210. <xs:complexType name='SystemDateTime'>
  211. <xs:sequence>
  212. <xs:element name='foobar' type='xs:string' minOccurs='0' />
  213. <xs:element name='Extension' type='tt:SystemDateTimeExtension' minOccurs='0'/>
  214. </xs:sequence>
  215. <!-- xs:anyAttribute processContents='lax'/ -->
  216. </xs:complexType>
  217. <xs:complexType name='SystemDateTimeExtension'>
  218. <xs:sequence>
  219. <xs:any namespace='##any' processContents='lax' minOccurs='0' maxOccurs='unbounded'/>
  220. </xs:sequence>
  221. </xs:complexType>
  222. </xs:schema>";
  223. string xsd2 = @"
  224. <xs:schema
  225. targetNamespace='http://www.onvif.org/ver10/device/wsdl'
  226. xmlns:xs='http://www.w3.org/2001/XMLSchema'
  227. xmlns:tt='http://www.onvif.org/ver10/schema'
  228. xmlns:tds='http://www.onvif.org/ver10/device/wsdl'
  229. elementFormDefault='qualified'>
  230. <xs:element name='GetSystemDateAndTime'>
  231. <xs:complexType>
  232. <xs:sequence/>
  233. </xs:complexType>
  234. </xs:element>
  235. <xs:element name='GetSystemDateAndTimeResponse'>
  236. <xs:complexType>
  237. <xs:sequence>
  238. <xs:element name='SystemDateAndTime' type='tt:SystemDateTime' />
  239. </xs:sequence>
  240. </xs:complexType>
  241. </xs:element>
  242. </xs:schema>";
  243. var xss = new XmlSchemaSet ();
  244. var xs1 = XmlSchema.Read (new StringReader (xsd1), null);
  245. xs1.SourceUri = "http://localhost:8080/dummy.wsdl";
  246. xs1.LineNumber = 5;
  247. xss.Add (xs1);
  248. var xs2 = XmlSchema.Read (new StringReader (xsd2), null);
  249. xs2.SourceUri = "http://localhost:8080/dummy.wsdl";
  250. xs2.LineNumber = 50;
  251. xss.Add (xs2);
  252. xss.Compile ();
  253. Assert.IsNotNull (xss.GlobalElements [new XmlQualifiedName ("GetSystemDateAndTimeResponse", "http://www.onvif.org/ver10/device/wsdl")], "#1");
  254. }
  255. [Test] // bug #13716
  256. public void ResolveSchemaUriUsingXmlResolver ()
  257. {
  258. var resolver = new Bug13716XmlResolver ();
  259. string xml = "<people xmlns='testschema'><person name='Ian'><books><book>Clean Code</book></books></person></people>";
  260. string ns = "testschema";
  261. string xsdPath = "my.xsd";
  262. var readerSettings = new XmlReaderSettings ();
  263. //readerSettings.XmlResolver = resolver;
  264. readerSettings.Schemas.XmlResolver = resolver;
  265. readerSettings.Schemas.Add (ns, xsdPath);
  266. readerSettings.ValidationType = ValidationType.Schema;
  267. using (var xr = XmlReader.Create (new StringReader (xml), readerSettings))
  268. {
  269. while (!xr.EOF)
  270. xr.Read ();
  271. }
  272. }
  273. public class Bug13716XmlResolver : XmlUrlResolver
  274. {
  275. public override object GetEntity(Uri absoluteUri, string role, Type typeOfObjectToReturn)
  276. {
  277. string xsd = @"
  278. <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='testschema'>
  279. <xs:element name='people' />
  280. </xs:schema>";
  281. return new MemoryStream (Encoding.UTF8.GetBytes (xsd));
  282. }
  283. }
  284. }
  285. }