XsdValidatingReaderTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // MonoTests.System.Xml.XsdValidatingReaderTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C)2003 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.Xml;
  11. using System.Xml.Schema;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Xml
  14. {
  15. [TestFixture]
  16. public class XsdValidatingReaderTests : Assertion
  17. {
  18. public XsdValidatingReaderTests ()
  19. {
  20. }
  21. XmlReader xtr;
  22. XmlValidatingReader xvr;
  23. private XmlValidatingReader PrepareXmlReader (string xml)
  24. {
  25. XmlReader reader = new XmlTextReader (xml, XmlNodeType.Document, null);
  26. // XmlDocument doc = new XmlDocument ();
  27. // doc.LoadXml (xml);
  28. // XmlReader reader = new XmlNodeReader (doc);
  29. return new XmlValidatingReader (reader);
  30. }
  31. [Test]
  32. public void TestEmptySchema ()
  33. {
  34. string xml = "<root/>";
  35. xvr = PrepareXmlReader (xml);
  36. xvr.ValidationType = ValidationType.Schema;
  37. xvr.Read (); // Is is missing schema component.
  38. }
  39. [Test]
  40. public void TestSimpleValidation ()
  41. {
  42. string xml = "<root/>";
  43. xvr = PrepareXmlReader (xml);
  44. AssertEquals (ValidationType.Auto, xvr.ValidationType);
  45. XmlSchema schema = new XmlSchema ();
  46. XmlSchemaElement elem = new XmlSchemaElement ();
  47. elem.Name = "root";
  48. schema.Items.Add (elem);
  49. xvr.Schemas.Add (schema);
  50. xvr.Read (); // root
  51. AssertEquals (ValidationType.Auto, xvr.ValidationType);
  52. xvr.Read (); // EOF
  53. xml = "<hoge/>";
  54. xvr = PrepareXmlReader (xml);
  55. xvr.Schemas.Add (schema);
  56. try {
  57. xvr.Read ();
  58. Fail ("element mismatch is incorrectly allowed");
  59. } catch (XmlSchemaException) {
  60. }
  61. xml = "<hoge xmlns='urn:foo' />";
  62. xvr = PrepareXmlReader (xml);
  63. xvr.Schemas.Add (schema);
  64. try {
  65. xvr.Read ();
  66. Fail ("Element in different namespace is incorrectly allowed.");
  67. } catch (XmlSchemaException) {
  68. }
  69. }
  70. [Test]
  71. public void TestReadTypedValueSimple ()
  72. {
  73. string xml = "<root>12</root>";
  74. XmlSchema schema = new XmlSchema ();
  75. XmlSchemaElement elem = new XmlSchemaElement ();
  76. elem.Name = "root";
  77. elem.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
  78. schema.Items.Add (elem);
  79. // Lap 1:
  80. xvr = PrepareXmlReader (xml);
  81. xvr.Schemas.Add (schema);
  82. // Read directly from root.
  83. object o = xvr.ReadTypedValue ();
  84. AssertEquals (ReadState.Initial, xvr.ReadState);
  85. AssertNull (o);
  86. xvr.Read (); // element root
  87. AssertEquals (XmlNodeType.Element, xvr.NodeType);
  88. AssertNotNull (xvr.SchemaType);
  89. Assert (xvr.SchemaType is XmlSchemaDatatype);
  90. o = xvr.ReadTypedValue (); // read "12"
  91. AssertEquals (XmlNodeType.EndElement, xvr.NodeType);
  92. AssertNotNull (o);
  93. AssertEquals (typeof (decimal), o.GetType ());
  94. decimal n = (decimal) o;
  95. AssertEquals (12, n);
  96. Assert (!xvr.EOF);
  97. AssertEquals ("root", xvr.Name);
  98. AssertNull (xvr.SchemaType); // EndElement's type
  99. // Lap 2:
  100. xvr = PrepareXmlReader (xml);
  101. xvr.Schemas.Add (schema);
  102. xvr.Read (); // root
  103. XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
  104. AssertNotNull (dt);
  105. AssertEquals (typeof (decimal), dt.ValueType);
  106. AssertEquals (XmlTokenizedType.None, dt.TokenizedType);
  107. xvr.Read (); // text "12"
  108. AssertNull (xvr.SchemaType);
  109. o = xvr.ReadTypedValue ();
  110. // ReadTypedValue is different from ReadString().
  111. AssertNull (o);
  112. }
  113. [Test]
  114. [Ignore ("XML Schema validator should not be available for validating non namespace-aware XmlReader that handled colon as a name character")]
  115. public void TestNamespacesFalse ()
  116. {
  117. // This tests if Namespaces=false is specified, then
  118. // the reader's NamespaceURI should be always string.Empty and
  119. // validation should be done against such schema that has target ns as "".
  120. string xml = "<x:root xmlns:x='urn:foo' />";
  121. xvr = PrepareXmlReader (xml);
  122. xvr.Namespaces = false;
  123. AssertEquals (ValidationType.Auto, xvr.ValidationType);
  124. XmlSchema schema = new XmlSchema ();
  125. schema.TargetNamespace = "urn:foo";
  126. XmlSchemaElement elem = new XmlSchemaElement ();
  127. elem.Name = "root";
  128. schema.Items.Add (elem);
  129. xvr.Schemas.Add (schema);
  130. xvr.Read (); // root
  131. Assert (!xvr.Namespaces);
  132. AssertEquals ("x:root", xvr.Name);
  133. // LocalName may contain colons.
  134. AssertEquals ("x:root", xvr.LocalName);
  135. // NamespaceURI is not supplied.
  136. AssertEquals ("", xvr.NamespaceURI);
  137. }
  138. [Test]
  139. public void TestReadTypedAttributeValue ()
  140. {
  141. string xml = "<root attr='12'></root>";
  142. XmlSchema schema = new XmlSchema ();
  143. XmlSchemaElement elem = new XmlSchemaElement ();
  144. elem.Name = "root";
  145. XmlSchemaComplexType ct = new XmlSchemaComplexType ();
  146. XmlSchemaAttribute attr = new XmlSchemaAttribute ();
  147. attr.Name = "attr";
  148. attr.SchemaTypeName = new XmlQualifiedName ("int", XmlSchema.Namespace);
  149. ct.Attributes.Add (attr);
  150. elem.SchemaType = ct;
  151. schema.Items.Add (elem);
  152. xvr = PrepareXmlReader (xml);
  153. xvr.Schemas.Add (schema);
  154. xvr.Read ();
  155. AssertEquals ("root", xvr.Name);
  156. Assert (xvr.MoveToNextAttribute ()); // attr
  157. AssertEquals ("attr", xvr.Name);
  158. XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
  159. AssertNotNull (dt);
  160. AssertEquals (typeof (int), dt.ValueType);
  161. AssertEquals (XmlTokenizedType.None, dt.TokenizedType);
  162. object o = xvr.ReadTypedValue ();
  163. AssertEquals (XmlNodeType.Attribute, xvr.NodeType);
  164. AssertEquals (typeof (int), o.GetType ());
  165. int n = (int) o;
  166. AssertEquals (12, n);
  167. Assert (xvr.ReadAttributeValue ()); // can read = seems not proceed.
  168. }
  169. }
  170. }