XmlSchemaSetTests.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. 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.IsFalse (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. [Test]
  178. public void ImportSubstitutionGroupDBR ()
  179. {
  180. // This bug happened when
  181. // 1) a schema imports another schema,
  182. // 2) there is a substitutionGroup which is involved in
  183. // complexContent schema conformance check, and
  184. // 3) the included schema is already added to XmlSchemaSet.
  185. XmlSchemaSet xss = new XmlSchemaSet ();
  186. xss.Add (null, "Test/XmlFiles/xsd/import-subst-dbr-base.xsd");
  187. xss.Add (null, "Test/XmlFiles/xsd/import-subst-dbr-ext.xsd");
  188. // should not result in lack of substitutionGroup
  189. // (and conformance error as its result)
  190. xss.Compile ();
  191. }
  192. [Test]
  193. public void AddWithNullTargetNS () // bug #571650
  194. {
  195. var xsdraw = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='foo' /></xs:schema>";
  196. var schemas = new XmlSchemaSet ();
  197. var xsd = schemas.Add ("", XmlReader.Create (new StringReader (xsdraw)));
  198. Assert.IsNull (xsd.TargetNamespace, "#1");
  199. }
  200. [Test] // part of bug #670945
  201. public void TwoSchemasInSameDocumentUri ()
  202. {
  203. string xsd1 = @"
  204. <xs:schema
  205. targetNamespace='http://www.onvif.org/ver10/schema'
  206. elementFormDefault='qualified'
  207. xmlns:xs='http://www.w3.org/2001/XMLSchema'
  208. xmlns:tt='http://www.onvif.org/ver10/schema'>
  209. <xs:complexType name='SystemDateTime'>
  210. <xs:sequence>
  211. <xs:element name='foobar' type='xs:string' minOccurs='0' />
  212. <xs:element name='Extension' type='tt:SystemDateTimeExtension' minOccurs='0'/>
  213. </xs:sequence>
  214. <!-- xs:anyAttribute processContents='lax'/ -->
  215. </xs:complexType>
  216. <xs:complexType name='SystemDateTimeExtension'>
  217. <xs:sequence>
  218. <xs:any namespace='##any' processContents='lax' minOccurs='0' maxOccurs='unbounded'/>
  219. </xs:sequence>
  220. </xs:complexType>
  221. </xs:schema>";
  222. string xsd2 = @"
  223. <xs:schema
  224. targetNamespace='http://www.onvif.org/ver10/device/wsdl'
  225. xmlns:xs='http://www.w3.org/2001/XMLSchema'
  226. xmlns:tt='http://www.onvif.org/ver10/schema'
  227. xmlns:tds='http://www.onvif.org/ver10/device/wsdl'
  228. elementFormDefault='qualified'>
  229. <xs:element name='GetSystemDateAndTime'>
  230. <xs:complexType>
  231. <xs:sequence/>
  232. </xs:complexType>
  233. </xs:element>
  234. <xs:element name='GetSystemDateAndTimeResponse'>
  235. <xs:complexType>
  236. <xs:sequence>
  237. <xs:element name='SystemDateAndTime' type='tt:SystemDateTime' />
  238. </xs:sequence>
  239. </xs:complexType>
  240. </xs:element>
  241. </xs:schema>";
  242. var xss = new XmlSchemaSet ();
  243. var xs1 = XmlSchema.Read (new StringReader (xsd1), null);
  244. xs1.SourceUri = "http://localhost:8080/dummy.wsdl";
  245. xs1.LineNumber = 5;
  246. xss.Add (xs1);
  247. var xs2 = XmlSchema.Read (new StringReader (xsd2), null);
  248. xs2.SourceUri = "http://localhost:8080/dummy.wsdl";
  249. xs2.LineNumber = 50;
  250. xss.Add (xs2);
  251. xss.Compile ();
  252. Assert.IsNotNull (xss.GlobalElements [new XmlQualifiedName ("GetSystemDateAndTimeResponse", "http://www.onvif.org/ver10/device/wsdl")], "#1");
  253. }
  254. [Test] // bug #13716
  255. public void ResolveSchemaUriUsingXmlResolver ()
  256. {
  257. var resolver = new Bug13716XmlResolver ();
  258. string xml = "<people xmlns='testschema'><person name='Ian'><books><book>Clean Code</book></books></person></people>";
  259. string ns = "testschema";
  260. string xsdPath = "my.xsd";
  261. var readerSettings = new XmlReaderSettings ();
  262. //readerSettings.XmlResolver = resolver;
  263. readerSettings.Schemas.XmlResolver = resolver;
  264. readerSettings.Schemas.Add (ns, xsdPath);
  265. readerSettings.ValidationType = ValidationType.Schema;
  266. using (var xr = XmlReader.Create (new StringReader (xml), readerSettings))
  267. {
  268. while (!xr.EOF)
  269. xr.Read ();
  270. }
  271. }
  272. public class Bug13716XmlResolver : XmlUrlResolver
  273. {
  274. public override object GetEntity(Uri absoluteUri, string role, Type typeOfObjectToReturn)
  275. {
  276. string xsd = @"
  277. <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='testschema'>
  278. <xs:element name='people' />
  279. </xs:schema>";
  280. return new MemoryStream (Encoding.UTF8.GetBytes (xsd));
  281. }
  282. }
  283. }
  284. }