XmlSchemaCollectionTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // System.Xml.XmlSchemaCollectionTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2002 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using NUnit.Framework;
  15. using MonoTests.Helpers;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlSchemaCollectionTests
  20. {
  21. private XmlSchema GetSchema (string resourceName)
  22. {
  23. return XmlSchema.Read (new XmlTextReader (TestResourceHelper.GetFullPathOfResource (resourceName)), null);
  24. }
  25. private XmlQualifiedName QName (string name, string ns)
  26. {
  27. return new XmlQualifiedName (name, ns);
  28. }
  29. [Test]
  30. public void TestAdd ()
  31. {
  32. XmlSchemaCollection col = new XmlSchemaCollection ();
  33. XmlSchema schema = new XmlSchema ();
  34. XmlSchemaElement elem = new XmlSchemaElement ();
  35. elem.Name = "foo";
  36. schema.Items.Add (elem);
  37. schema.TargetNamespace = "urn:foo";
  38. col.Add (schema);
  39. col.Add (schema); // No problem !?
  40. XmlSchema schema2 = new XmlSchema ();
  41. schema2.Items.Add (elem);
  42. schema2.TargetNamespace = "urn:foo";
  43. col.Add (schema2); // No problem !!
  44. schema.Compile (null);
  45. col.Add (schema);
  46. col.Add (schema); // Still no problem !!!
  47. schema2.Compile (null);
  48. col.Add (schema2);
  49. schema = GetSchema ("Test/XmlFiles/xsd/3.xsd");
  50. schema.Compile (null);
  51. col.Add (schema);
  52. schema2 = GetSchema ("Test/XmlFiles/xsd/3.xsd");
  53. schema2.Compile (null);
  54. col.Add (schema2);
  55. }
  56. [Test]
  57. public void TestAddDoesCompilation ()
  58. {
  59. XmlSchema schema = new XmlSchema ();
  60. Assert.IsFalse (schema.IsCompiled);
  61. XmlSchemaCollection col = new XmlSchemaCollection ();
  62. col.Add (schema);
  63. Assert.IsTrue (schema.IsCompiled);
  64. }
  65. [Test] // bug #75126
  66. public void TestGetEnumerator ()
  67. {
  68. new XmlSchemaCollection().GetEnumerator();
  69. }
  70. [Test] // bug #78220
  71. public void TestCompile ()
  72. {
  73. string schemaFragment1 = string.Format (CultureInfo.InvariantCulture,
  74. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  75. "<xs:schema xmlns:tns=\"NSDate\" elementFormDefault=\"qualified\" targetNamespace=\"NSDate\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  76. " <xs:import namespace=\"NSStatus\" />{0}" +
  77. " <xs:element name=\"trans\" type=\"tns:TranslationStatus\" />{0}" +
  78. " <xs:complexType name=\"TranslationStatus\">{0}" +
  79. " <xs:simpleContent>{0}" +
  80. " <xs:extension xmlns:q1=\"NSStatus\" base=\"q1:StatusType\">{0}" +
  81. " <xs:attribute name=\"Language\" type=\"xs:int\" use=\"required\" />{0}" +
  82. " </xs:extension>{0}" +
  83. " </xs:simpleContent>{0}" +
  84. " </xs:complexType>{0}" +
  85. "</xs:schema>", Environment.NewLine);
  86. string schemaFragment2 = string.Format (CultureInfo.InvariantCulture,
  87. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  88. "<xs:schema xmlns:tns=\"NSStatus\" elementFormDefault=\"qualified\" targetNamespace=\"NSStatus\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  89. " <xs:simpleType name=\"StatusType\">{0}" +
  90. " <xs:restriction base=\"xs:string\">{0}" +
  91. " <xs:enumeration value=\"Untouched\" />{0}" +
  92. " <xs:enumeration value=\"Touched\" />{0}" +
  93. " <xs:enumeration value=\"Complete\" />{0}" +
  94. " <xs:enumeration value=\"None\" />{0}" +
  95. " </xs:restriction>{0}" +
  96. " </xs:simpleType>{0}" +
  97. "</xs:schema>", Environment.NewLine);
  98. XmlSchema schema1 = XmlSchema.Read (new StringReader (schemaFragment1), null);
  99. XmlSchema schema2 = XmlSchema.Read (new StringReader (schemaFragment2), null);
  100. XmlSchemaCollection schemas = new XmlSchemaCollection ();
  101. schemas.Add (schema2);
  102. schemas.Add (schema1);
  103. Assert.IsTrue (schema1.IsCompiled, "#1");
  104. Assert.IsTrue (schema2.IsCompiled, "#2");
  105. }
  106. }
  107. }