XmlSchemaValidatorTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // XmlSchemaValidatorTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2008 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 XmlSchemaValidatorTests
  20. {
  21. [Test]
  22. public void XsdAnyToSkipAttributeValidation ()
  23. {
  24. // bug #358408
  25. XmlSchemaSet schemas = new XmlSchemaSet ();
  26. schemas.Add (null, "Test/XmlFiles/xsd/358408.xsd");
  27. XmlSchemaValidator v = new XmlSchemaValidator (
  28. new NameTable (),
  29. schemas,
  30. new XmlNamespaceManager (new NameTable ()),
  31. XmlSchemaValidationFlags.ProcessIdentityConstraints);
  32. v.Initialize ();
  33. v.ValidateWhitespace (" ");
  34. XmlSchemaInfo info = new XmlSchemaInfo ();
  35. ArrayList list = new ArrayList ();
  36. v.ValidateElement ("configuration", "", info, null, null, null, null);
  37. v.GetUnspecifiedDefaultAttributes (list);
  38. v.ValidateEndOfAttributes (info);
  39. v.ValidateWhitespace (" ");
  40. v.ValidateElement ("host", "", info, null, null, null, null);
  41. v.ValidateAttribute ("auto-start", "", "true", info);
  42. list.Clear ();
  43. v.GetUnspecifiedDefaultAttributes (list);
  44. v.ValidateEndOfAttributes (info);
  45. v.ValidateEndElement (null);//info);
  46. v.ValidateWhitespace (" ");
  47. v.ValidateElement ("service-managers", "", info, null, null, null, null);
  48. list.Clear ();
  49. v.GetUnspecifiedDefaultAttributes (list);
  50. v.ValidateEndOfAttributes (info);
  51. v.ValidateWhitespace (" ");
  52. v.ValidateElement ("service-manager", "", info, null, null, null, null);
  53. list.Clear ();
  54. v.GetUnspecifiedDefaultAttributes (list);
  55. v.ValidateEndOfAttributes (info);
  56. v.ValidateWhitespace (" ");
  57. v.ValidateElement ("foo", "", info, null, null, null, null);
  58. v.ValidateAttribute ("bar", "", "", info);
  59. }
  60. [Test]
  61. public void SkipInvolved () // bug #422581
  62. {
  63. XmlReader schemaReader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xsd");
  64. XmlSchema schema = XmlSchema.Read (schemaReader, null);
  65. XmlReaderSettings settings = new XmlReaderSettings ();
  66. settings.ValidationType = ValidationType.Schema;
  67. settings.Schemas.Add (schema);
  68. XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/422581.xml", settings);
  69. while (reader.Read ());
  70. }
  71. [Test]
  72. public void Bug433774 ()
  73. {
  74. string xsd = @"<xs:schema targetNamespace='urn:foo' xmlns='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  75. <xs:element name='Root'>
  76. <xs:complexType>
  77. <xs:sequence></xs:sequence>
  78. <xs:attribute name='version' type='xs:string' fixed='3' />
  79. </xs:complexType>
  80. </xs:element>
  81. </xs:schema>";
  82. XmlDocument doc = new XmlDocument ();
  83. doc.LoadXml ("<Root version='3' xmlns='urn:foo'/>");
  84. XmlSchemaSet schemaSet = new XmlSchemaSet();
  85. schemaSet.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
  86. doc.Schemas = schemaSet;
  87. XmlNode root = doc.DocumentElement;
  88. doc.Validate (null, root);
  89. }
  90. [Test]
  91. [ExpectedException (typeof (XmlSchemaValidationException))]
  92. public void Bug435206 ()
  93. {
  94. string xsd = @"<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  95. <xs:element name='myDoc'>
  96. <xs:complexType>
  97. <xs:attribute name='foo' type='xs:unsignedLong' use='required' />
  98. <xs:attribute name='bar' type='xs:dateTime' use='required' />
  99. </xs:complexType>
  100. </xs:element>
  101. </xs:schema>";
  102. XmlSchema schema = XmlSchema.Read (new StringReader (xsd), null);
  103. XmlReaderSettings settings = new XmlReaderSettings ();
  104. settings.ValidationType = ValidationType.Schema;
  105. settings.Schemas.Add (schema);
  106. XmlReader reader = XmlReader.Create (new StringReader (@"<myDoc foo='12' bar='January 1st 1900'/>"), settings);
  107. while (reader.Read ())
  108. ;
  109. }
  110. }
  111. }
  112. #endif