XmlSchemaSetTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. public void AddSchemaThenReader ()
  52. {
  53. XmlSchemaSet ss = new XmlSchemaSet ();
  54. XmlDocument doc = new XmlDocument ();
  55. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
  56. XmlSchema xs = new XmlSchema ();
  57. xs.TargetNamespace = "ab";
  58. ss.Add (xs);
  59. ss.Add ("ab", new XmlNodeReader (doc));
  60. }
  61. [Test]
  62. [Category ("NotWorking")] // How can we differentiate this
  63. // case and the testcase above?
  64. [ExpectedException (typeof (ArgumentException))]
  65. public void AddReaderTwice ()
  66. {
  67. XmlSchemaSet ss = new XmlSchemaSet ();
  68. XmlDocument doc = new XmlDocument ();
  69. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
  70. ss.Add ("ab", new XmlNodeReader (doc));
  71. ss.Add ("ab", new XmlNodeReader (doc));
  72. }
  73. [Test]
  74. public void AddSchemaTwice ()
  75. {
  76. XmlSchemaSet ss = new XmlSchemaSet ();
  77. XmlDocument doc = new XmlDocument ();
  78. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:ab' />");
  79. ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
  80. ss.Add (XmlSchema.Read (new XmlNodeReader (doc), null));
  81. }
  82. [Test]
  83. public void CompilationSettings ()
  84. {
  85. Assert.IsNotNull (new XmlSchemaSet ().CompilationSettings);
  86. new XmlSchemaSet ().CompilationSettings = null;
  87. }
  88. [Test]
  89. public void DisableUpaCheck ()
  90. {
  91. string schema = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  92. <xs:complexType name='Foo'>
  93. <xs:sequence>
  94. <xs:choice minOccurs='0'>
  95. <xs:element name='el'/>
  96. </xs:choice>
  97. <xs:element name='el' />
  98. </xs:sequence>
  99. </xs:complexType>
  100. </xs:schema>";
  101. XmlSchema xs = XmlSchema.Read (new XmlTextReader (
  102. schema, XmlNodeType.Document, null), null);
  103. XmlSchemaSet xss = new XmlSchemaSet ();
  104. xss.Add (xs);
  105. xss.CompilationSettings.EnableUpaCheck = false;
  106. xss.Compile ();
  107. }
  108. [Test]
  109. public void AddRollbackIsCompiled ()
  110. {
  111. XmlSchemaSet ss = new XmlSchemaSet ();
  112. ss.Add (new XmlSchema ());
  113. ss.Compile ();
  114. Assert.IsTrue (ss.IsCompiled, "#1");
  115. XmlSchema sc = new XmlSchema (); // compiled one
  116. sc.Compile (null);
  117. ss.Add (sc);
  118. Assert.IsFalse (ss.IsCompiled, "#2");
  119. ss.Add (new XmlSchema ()); // not-compiled one
  120. Assert.IsFalse (ss.IsCompiled, "#3");
  121. XmlSchema s;
  122. s = new XmlSchema ();
  123. s.TargetNamespace = "urn:foo";
  124. XmlSchemaElement el;
  125. el = new XmlSchemaElement ();
  126. el.Name = "root";
  127. s.Items.Add (el);
  128. ss.Add (s);
  129. s = new XmlSchema ();
  130. s.TargetNamespace = "urn:foo";
  131. el = new XmlSchemaElement ();
  132. el.Name = "foo";
  133. s.Items.Add (el);
  134. ss.Add (s);
  135. ss.Compile ();
  136. Assert.IsTrue (ss.IsCompiled, "#4");
  137. ss.RemoveRecursive (s);
  138. Assert.IsTrue (ss.IsCompiled, "#5");
  139. }
  140. }
  141. }
  142. #endif