XmlSchemaTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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.Net;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using System.Xml.Serialization;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlSchemaTests : XmlSchemaAssertion
  20. {
  21. [Test]
  22. public void TestRead ()
  23. {
  24. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
  25. AssertEquals (6, schema.Items.Count);
  26. bool fooValidated = false;
  27. bool barValidated = false;
  28. string ns = "urn:bar";
  29. foreach (XmlSchemaObject obj in schema.Items) {
  30. XmlSchemaElement element = obj as XmlSchemaElement;
  31. if (element == null)
  32. continue;
  33. if (element.Name == "Foo") {
  34. AssertElement (element, "Foo",
  35. XmlQualifiedName.Empty, null,
  36. QName ("string", XmlSchema.Namespace), null);
  37. fooValidated = true;
  38. }
  39. if (element.Name == "Bar") {
  40. AssertElement (element, "Bar",
  41. XmlQualifiedName.Empty, null, QName ("FugaType", ns), null);
  42. barValidated = true;
  43. }
  44. }
  45. Assert (fooValidated);
  46. Assert (barValidated);
  47. }
  48. [Test]
  49. public void TestReadFlags ()
  50. {
  51. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
  52. schema.Compile (null);
  53. XmlSchemaElement el = schema.Items [0] as XmlSchemaElement;
  54. AssertNotNull (el);
  55. AssertEquals (XmlSchemaDerivationMethod.Extension, el.Block);
  56. el = schema.Items [1] as XmlSchemaElement;
  57. AssertNotNull (el);
  58. AssertEquals (XmlSchemaDerivationMethod.Extension |
  59. XmlSchemaDerivationMethod.Restriction, el.Block);
  60. }
  61. [Test]
  62. public void TestWriteFlags ()
  63. {
  64. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
  65. StringWriter sw = new StringWriter ();
  66. XmlTextWriter xtw = new XmlTextWriter (sw);
  67. schema.Write (xtw);
  68. }
  69. [Test]
  70. public void TestCompile ()
  71. {
  72. XmlQualifiedName qname;
  73. XmlSchemaComplexContentExtension xccx;
  74. XmlSchemaComplexType cType;
  75. XmlSchemaSequence seq;
  76. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
  77. // Assert (!schema.IsCompiled);
  78. schema.Compile (null);
  79. Assert (schema.IsCompiled);
  80. string ns = "urn:bar";
  81. XmlSchemaElement foo = (XmlSchemaElement) schema.Elements [QName ("Foo", ns)];
  82. AssertNotNull (foo);
  83. XmlSchemaDatatype stringDatatype = foo.ElementType as XmlSchemaDatatype;
  84. AssertNotNull (stringDatatype);
  85. // HogeType
  86. qname = QName ("HogeType", ns);
  87. cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
  88. AssertNotNull (cType);
  89. AssertNull (cType.ContentModel);
  90. AssertCompiledComplexType (cType, qname, 0, 0,
  91. false, null, true, XmlSchemaContentType.ElementOnly);
  92. seq = cType.ContentTypeParticle as XmlSchemaSequence;
  93. AssertNotNull (seq);
  94. AssertEquals (2, seq.Items.Count);
  95. XmlSchemaElement refFoo = seq.Items [0] as XmlSchemaElement;
  96. AssertCompiledElement (refFoo, QName ("Foo", ns), stringDatatype);
  97. // FugaType
  98. qname = QName ("FugaType", ns);
  99. cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
  100. AssertNotNull (cType);
  101. xccx = cType.ContentModel.Content as XmlSchemaComplexContentExtension;
  102. AssertCompiledComplexContentExtension (
  103. xccx, 0, false, QName ("HogeType", ns));
  104. AssertCompiledComplexType (cType, qname, 0, 0,
  105. false, typeof (XmlSchemaComplexContent),
  106. true, XmlSchemaContentType.ElementOnly);
  107. AssertNotNull (cType.BaseSchemaType);
  108. seq = xccx.Particle as XmlSchemaSequence;
  109. AssertNotNull (seq);
  110. AssertEquals (1, seq.Items.Count);
  111. XmlSchemaElement refBaz = seq.Items [0] as XmlSchemaElement;
  112. AssertNotNull (refBaz);
  113. AssertCompiledElement (refBaz, QName ("Baz", ""), stringDatatype);
  114. qname = QName ("Bar", ns);
  115. XmlSchemaElement element = schema.Elements [qname] as XmlSchemaElement;
  116. AssertCompiledElement (element, qname, cType);
  117. }
  118. [Test]
  119. [ExpectedException (typeof (XmlSchemaException))]
  120. public void TestCompile_ZeroLength_TargetNamespace ()
  121. {
  122. XmlSchema schema = new XmlSchema ();
  123. schema.TargetNamespace = string.Empty;
  124. Assert (!schema.IsCompiled);
  125. // MS.NET 1.x: The Namespace '' is an invalid URI.
  126. // MS.NET 2.0: The targetNamespace attribute cannot have empty string as its value.
  127. schema.Compile (null);
  128. }
  129. [Test]
  130. [ExpectedException (typeof (XmlSchemaException))]
  131. public void TestCompileNonSchema ()
  132. {
  133. XmlTextReader xtr = new XmlTextReader ("<root/>", XmlNodeType.Document, null);
  134. XmlSchema schema = XmlSchema.Read (xtr, null);
  135. xtr.Close ();
  136. }
  137. [Test]
  138. public void TestSimpleImport ()
  139. {
  140. XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/3.xsd"), null);
  141. AssertEquals ("urn:foo", schema.TargetNamespace);
  142. XmlSchemaImport import = schema.Includes [0] as XmlSchemaImport;
  143. AssertNotNull (import);
  144. schema.Compile (null);
  145. AssertEquals (4, schema.Elements.Count);
  146. AssertNotNull (schema.Elements [QName ("Foo", "urn:foo")]);
  147. AssertNotNull (schema.Elements [QName ("Bar", "urn:foo")]);
  148. AssertNotNull (schema.Elements [QName ("Foo", "urn:bar")]);
  149. AssertNotNull (schema.Elements [QName ("Bar", "urn:bar")]);
  150. }
  151. [Test]
  152. public void TestSimpleMutualImport ()
  153. {
  154. XmlReader r = new XmlTextReader ("Test/XmlFiles/xsd/inter-inc-1.xsd");
  155. try {
  156. XmlSchema.Read (r, null).Compile (null);
  157. } finally {
  158. r.Close ();
  159. }
  160. }
  161. [Test]
  162. public void TestQualification ()
  163. {
  164. XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
  165. schema.Compile (null);
  166. XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
  167. AssertNotNull (el);
  168. XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
  169. XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
  170. XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
  171. AssertEquals (QName ("Bar", ""), elp.QualifiedName);
  172. schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
  173. schema.Compile (null);
  174. el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
  175. AssertNotNull (el);
  176. ct = el.ElementType as XmlSchemaComplexType;
  177. seq = ct.ContentTypeParticle as XmlSchemaSequence;
  178. elp = seq.Items [0] as XmlSchemaElement;
  179. AssertEquals (QName ("Bar", "urn:bar"), elp.QualifiedName);
  180. }
  181. [Test]
  182. public void TestWriteNamespaces ()
  183. {
  184. XmlDocument doc = new XmlDocument ();
  185. XmlSchema xs;
  186. StringWriter sw;
  187. XmlTextWriter xw;
  188. // empty
  189. xs = new XmlSchema ();
  190. sw = new StringWriter ();
  191. xw = new XmlTextWriter (sw);
  192. xs.Write (xw);
  193. doc.LoadXml (sw.ToString ());
  194. AssertEquals ("#1", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  195. // TargetNamespace
  196. xs = new XmlSchema ();
  197. sw = new StringWriter ();
  198. xw = new XmlTextWriter (sw);
  199. xs.TargetNamespace = "urn:foo";
  200. xs.Write (xw);
  201. doc.LoadXml (sw.ToString ());
  202. AssertEquals ("#2", "<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  203. // Zero-length TargetNamespace
  204. xs = new XmlSchema ();
  205. sw = new StringWriter ();
  206. xw = new XmlTextWriter (sw);
  207. xs.TargetNamespace = string.Empty;
  208. xs.Write (xw);
  209. doc.LoadXml (sw.ToString ());
  210. AssertEquals ("#2b", "<xs:schema targetNamespace=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  211. // XmlSerializerNamespaces
  212. xs = new XmlSchema ();
  213. sw = new StringWriter ();
  214. xw = new XmlTextWriter (sw);
  215. xs.Namespaces.Add ("hoge", "urn:hoge");
  216. xs.Write (xw);
  217. doc.LoadXml (sw.ToString ());
  218. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  219. // AssertEquals ("#3", "<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  220. // TargetNamespace + XmlSerializerNamespaces
  221. xs = new XmlSchema ();
  222. sw = new StringWriter ();
  223. xw = new XmlTextWriter (sw);
  224. xs.TargetNamespace = "urn:foo";
  225. xs.Namespaces.Add ("hoge", "urn:hoge");
  226. xs.Write (xw);
  227. doc.LoadXml (sw.ToString ());
  228. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  229. // AssertEquals ("#4", "<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  230. // Add XmlSchema.Namespace to XmlSerializerNamespaces
  231. xs = new XmlSchema ();
  232. sw = new StringWriter ();
  233. xw = new XmlTextWriter (sw);
  234. xs.Namespaces.Add ("a", XmlSchema.Namespace);
  235. xs.Write (xw);
  236. doc.LoadXml (sw.ToString ());
  237. AssertEquals ("#5", "<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  238. // UnhandledAttributes + XmlSerializerNamespaces
  239. xs = new XmlSchema ();
  240. sw = new StringWriter ();
  241. xw = new XmlTextWriter (sw);
  242. XmlAttribute attr = doc.CreateAttribute ("hoge");
  243. xs.UnhandledAttributes = new XmlAttribute [] {attr};
  244. xs.Namespaces.Add ("hoge", "urn:hoge");
  245. xs.Write (xw);
  246. doc.LoadXml (sw.ToString ());
  247. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  248. // AssertEquals ("#6", "<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  249. // Adding xmlns to UnhandledAttributes -> no output
  250. xs = new XmlSchema ();
  251. sw = new StringWriter ();
  252. xw = new XmlTextWriter (sw);
  253. attr = doc.CreateAttribute ("xmlns");
  254. attr.Value = "urn:foo";
  255. xs.UnhandledAttributes = new XmlAttribute [] {attr};
  256. xs.Write (xw);
  257. doc.LoadXml (sw.ToString ());
  258. AssertEquals ("#7", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml);
  259. }
  260. [Category ("NotWorking")]
  261. [Test]
  262. public void TestWriteNamespaces2 ()
  263. {
  264. string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
  265. XmlSchema xs = new XmlSchema ();
  266. XmlSerializerNamespaces nss =
  267. new XmlSerializerNamespaces ();
  268. StringWriter sw;
  269. sw = new StringWriter ();
  270. xs.Write (new XmlTextWriter (sw));
  271. AssertEquals ("#1", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  272. xs.Namespaces = nss;
  273. sw = new StringWriter ();
  274. xs.Write (new XmlTextWriter (sw));
  275. AssertEquals ("#2", xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  276. nss.Add ("foo", "urn:foo");
  277. sw = new StringWriter ();
  278. xs.Write (new XmlTextWriter (sw));
  279. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  280. // AssertEquals ("#3", xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  281. nss.Add ("", "urn:foo");
  282. sw = new StringWriter ();
  283. xs.Write (new XmlTextWriter (sw));
  284. // commenting out. .NET 2.0 outputs xs:schema instead of q1:schema, that also makes sense.
  285. // AssertEquals ("#4", xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  286. nss.Add ("q1", "urn:q1");
  287. sw = new StringWriter ();
  288. xs.Write (new XmlTextWriter (sw));
  289. //Not sure if testing for exact order of these name spaces is
  290. // relevent, so using less strict test that passes on MS.NET
  291. //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 ());
  292. Assert("q1", sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1);
  293. }
  294. [Test]
  295. public void ReaderPositionAfterRead ()
  296. {
  297. string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'> <xs:element name='test' type='xs:integer'/></xs:schema>";
  298. XmlTextReader xtr = new XmlTextReader (xsd, XmlNodeType.Document, null);
  299. xtr.Read ();
  300. XmlSchema xs = XmlSchema.Read (xtr, null);
  301. AssertEquals (XmlNodeType.EndElement, xtr.NodeType);
  302. }
  303. [Test]
  304. // bug #76865
  305. public void AmbiguityDetectionOnChameleonAnyOther ()
  306. {
  307. string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  308. <xs:complexType name='TestType'>
  309. <xs:sequence>
  310. <xs:any namespace='##other' minOccurs='0' />
  311. <xs:element name='Item' />
  312. <xs:any namespace='##other' minOccurs='0' />
  313. </xs:sequence>
  314. </xs:complexType>
  315. </xs:schema>";
  316. XmlSchema.Read (new XmlTextReader (xsd, XmlNodeType.Document, null), null);
  317. }
  318. [Test]
  319. // bug #77685
  320. public void ReadDoesNotIgnoreDocumentationEmptyElement ()
  321. {
  322. string schemaxml = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  323. <xs:element name='choice'>
  324. <xs:annotation><xs:documentation /></xs:annotation>
  325. </xs:element>
  326. </xs:schema>";
  327. XmlTextReader tr = new XmlTextReader (
  328. schemaxml, XmlNodeType.Document, null);
  329. XmlSchema schema = XmlSchema.Read (tr, null);
  330. XmlSchemaElement element =
  331. schema.Items [0] as XmlSchemaElement;
  332. XmlSchemaAnnotation annotation = element.Annotation;
  333. XmlSchemaDocumentation doc =
  334. annotation.Items [0] as XmlSchemaDocumentation;
  335. AssertEquals (0, doc.Markup.Length);
  336. }
  337. [Test]
  338. // bug #77687
  339. public void CompileFillsSchemaPropertyInExternal ()
  340. {
  341. string schemaFileName = "Test/XmlFiles/xsd/77687.xsd";
  342. XmlTextReader tr = new XmlTextReader (schemaFileName);
  343. XmlSchema schema = XmlSchema.Read (tr, null);
  344. XmlSchemaInclude inc = (XmlSchemaInclude) schema.Includes [0];
  345. AssertNull (inc.Schema);
  346. schema.Compile (null);
  347. tr.Close ();
  348. AssertNotNull (inc.Schema);
  349. }
  350. [Test]
  351. // bug #78985 (contains two identical field path "@key" in
  352. // two different keys where one is in scope within another)
  353. public void DuplicateKeyFieldAttributePath ()
  354. {
  355. string schemaFileName = "Test/XmlFiles/xsd/78985.xsd";
  356. string xmlFileName = "Test/XmlFiles/xsd/78985.xml";
  357. XmlTextReader tr = new XmlTextReader (schemaFileName);
  358. XmlValidatingReader vr = new XmlValidatingReader (
  359. new XmlTextReader (xmlFileName));
  360. vr.Schemas.Add (XmlSchema.Read (tr, null));
  361. while (!vr.EOF)
  362. vr.Read ();
  363. }
  364. [Test]
  365. public void ThreeLevelNestedInclusion ()
  366. {
  367. XmlTextReader r = new XmlTextReader ("Test/XmlFiles/xsd/361818.xsd");
  368. try {
  369. XmlSchema xs = XmlSchema.Read (r, null);
  370. xs.Compile (null);
  371. } finally {
  372. r.Close ();
  373. }
  374. }
  375. #if NET_2_0
  376. internal class XmlTestResolver : XmlResolver
  377. {
  378. Uri receivedUri;
  379. public override ICredentials Credentials
  380. {
  381. set { throw new NotSupportedException (); }
  382. }
  383. public override Uri ResolveUri (Uri baseUri, string relativeUri)
  384. {
  385. return new Uri (relativeUri);
  386. }
  387. public Uri ReceivedUri
  388. {
  389. get { return receivedUri; }
  390. }
  391. public override object GetEntity (Uri absoluteUri, string role, Type ofObjectToReturn)
  392. {
  393. receivedUri = absoluteUri;
  394. return null;
  395. }
  396. }
  397. [Test]
  398. public void TestResolveUri ()
  399. {
  400. XmlSchemaSet schemaSet = new XmlSchemaSet ();
  401. FileStream stream = new FileStream ("Test/XmlFiles/xsd/resolveUriSchema.xsd", FileMode.Open);
  402. schemaSet.Add ("http://tempuri.org/resolveUriSchema.xsd", new XmlTextReader (stream));
  403. XmlTestResolver resolver = new XmlTestResolver ();
  404. XmlReaderSettings settings = new XmlReaderSettings ();
  405. settings.Schemas.XmlResolver = resolver;
  406. settings.Schemas.Add (schemaSet);
  407. settings.ValidationType = ValidationType.Schema;
  408. settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation;
  409. XmlReader reader = XmlReader.Create (stream, settings);
  410. try
  411. {
  412. reader.Read ();
  413. }
  414. catch (XmlException)
  415. {
  416. // do nothing - we are expecting this exception because the test xmlresolver returns null from its
  417. // GetEntity method.
  418. }
  419. AssertEquals ("assembly://MyAssembly.Name/MyProjectNameSpace/objects.xsd", resolver.ReceivedUri.OriginalString);
  420. }
  421. [Test]
  422. public void TestImportNoSchemaLocation()
  423. {
  424. XmlSchemaSet schemaSet = new XmlSchemaSet ();
  425. schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importNamespaceTest.xsd"));
  426. schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importedNamespace.xsd"));
  427. XmlReaderSettings settings = new XmlReaderSettings ();
  428. settings.Schemas.Add (schemaSet);
  429. settings.ValidationType = ValidationType.Schema;
  430. XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/xsdimporttest.xml", settings);
  431. // Parse the file.
  432. while (reader.Read()) {}
  433. }
  434. #endif
  435. }
  436. }