XmlSchemaSetTests.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. [Ignore ("This behavior might be changed, since Add(XmlSchema) does not throw any exceptions, while this does.")]
  52. [ExpectedException (typeof (ArgumentException))]
  53. public void AddTwice ()
  54. {
  55. XmlSchemaSet ss = new XmlSchemaSet ();
  56. XmlDocument doc = new XmlDocument ();
  57. doc.LoadXml ("<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' />");
  58. ss.Add ("ab", new XmlNodeReader (doc));
  59. ss.Add ("ab", new XmlNodeReader (doc));
  60. }
  61. [Test]
  62. public void CompilationSettings ()
  63. {
  64. Assert.IsNotNull (new XmlSchemaSet ().CompilationSettings);
  65. new XmlSchemaSet ().CompilationSettings = null;
  66. }
  67. [Test]
  68. public void DisableUpaCheck ()
  69. {
  70. string schema = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  71. <xs:complexType name='Foo'>
  72. <xs:sequence>
  73. <xs:choice minOccurs='0'>
  74. <xs:element name='el'/>
  75. </xs:choice>
  76. <xs:element name='el' />
  77. </xs:sequence>
  78. </xs:complexType>
  79. </xs:schema>";
  80. XmlSchema xs = XmlSchema.Read (new XmlTextReader (
  81. schema, XmlNodeType.Document, null), null);
  82. XmlSchemaSet xss = new XmlSchemaSet ();
  83. xss.Add (xs);
  84. xss.CompilationSettings.EnableUpaCheck = false;
  85. xss.Compile ();
  86. }
  87. [Test]
  88. public void AddRollbackIsCompiled ()
  89. {
  90. XmlSchemaSet ss = new XmlSchemaSet ();
  91. ss.Add (new XmlSchema ());
  92. ss.Compile ();
  93. Assert.IsTrue (ss.IsCompiled, "#1");
  94. XmlSchema sc = new XmlSchema (); // compiled one
  95. sc.Compile (null);
  96. ss.Add (sc);
  97. Assert.IsFalse (ss.IsCompiled, "#2");
  98. ss.Add (new XmlSchema ()); // not-compiled one
  99. Assert.IsFalse (ss.IsCompiled, "#3");
  100. XmlSchema s;
  101. s = new XmlSchema ();
  102. s.TargetNamespace = "urn:foo";
  103. XmlSchemaElement el;
  104. el = new XmlSchemaElement ();
  105. el.Name = "root";
  106. s.Items.Add (el);
  107. ss.Add (s);
  108. s = new XmlSchema ();
  109. s.TargetNamespace = "urn:foo";
  110. el = new XmlSchemaElement ();
  111. el.Name = "foo";
  112. s.Items.Add (el);
  113. ss.Add (s);
  114. ss.Compile ();
  115. Assert.IsTrue (ss.IsCompiled, "#4");
  116. ss.RemoveRecursive (s);
  117. Assert.IsTrue (ss.IsCompiled, "#5");
  118. }
  119. }
  120. }
  121. #endif