XmlSchemaValidatorTests.cs 4.4 KB

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