XsdValidatingReaderTests.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 XsdValidationTests : Assertion
  17. {
  18. public XsdValidationTests ()
  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 ();
  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 ();
  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. o = xvr.ReadTypedValue (); // read "12"
  89. AssertEquals (XmlNodeType.EndElement, xvr.NodeType);
  90. AssertNotNull (o);
  91. AssertEquals (typeof (decimal), o.GetType ());
  92. decimal n = (decimal) o;
  93. AssertEquals (12, n);
  94. Assert (!xvr.EOF);
  95. AssertEquals ("root", xvr.Name);
  96. AssertNull (xvr.SchemaType); // EndElement's type
  97. // Lap 2:
  98. xvr = PrepareXmlReader (xml);
  99. xvr.Schemas.Add (schema);
  100. xvr.Read (); // root
  101. XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
  102. AssertNotNull (dt);
  103. AssertEquals (typeof (decimal), dt.ValueType);
  104. AssertEquals (XmlTokenizedType.None, dt.TokenizedType);
  105. xvr.Read (); // text "12"
  106. AssertNull (xvr.SchemaType);
  107. o = xvr.ReadTypedValue ();
  108. // ReadTypedValue is different from ReadString().
  109. AssertNull (o);
  110. }
  111. [Test]
  112. public void TestNamespacesFalse ()
  113. {
  114. // This tests if Namespaces=false is specified, then
  115. // the reader's NamespaceURI should be always string.Empty and
  116. // validation should be done against such schema that has target ns as "".
  117. string xml = "<x:root xmlns:x='urn:foo' />";
  118. xvr = PrepareXmlReader (xml);
  119. xvr.Namespaces = false;
  120. AssertEquals (ValidationType.Auto, xvr.ValidationType);
  121. XmlSchema schema = new XmlSchema ();
  122. schema.TargetNamespace = "urn:foo";
  123. XmlSchemaElement elem = new XmlSchemaElement ();
  124. elem.Name = "root";
  125. schema.Items.Add (elem);
  126. xvr.Schemas.Add (schema);
  127. xvr.Read (); // root
  128. Assert (!xvr.Namespaces);
  129. AssertEquals ("x:root", xvr.Name);
  130. // LocalName may contain colons.
  131. AssertEquals ("x:root", xvr.LocalName);
  132. // NamespaceURI is not supplied.
  133. AssertEquals ("", xvr.NamespaceURI);
  134. }
  135. [Test]
  136. public void TestReadTypedAttributeValue ()
  137. {
  138. string xml = "<root attr='12'></root>";
  139. XmlSchema schema = new XmlSchema ();
  140. XmlSchemaElement elem = new XmlSchemaElement ();
  141. elem.Name = "root";
  142. XmlSchemaComplexType ct = new XmlSchemaComplexType ();
  143. XmlSchemaAttribute attr = new XmlSchemaAttribute ();
  144. attr.Name = "attr";
  145. attr.SchemaTypeName = new XmlQualifiedName ("int", XmlSchema.Namespace);
  146. ct.Attributes.Add (attr);
  147. elem.SchemaType = ct;
  148. schema.Items.Add (elem);
  149. xvr = PrepareXmlReader (xml);
  150. xvr.Schemas.Add (schema);
  151. xvr.Read ();
  152. AssertEquals ("root", xvr.Name);
  153. Assert (xvr.MoveToNextAttribute ()); // attr
  154. AssertEquals ("attr", xvr.Name);
  155. XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
  156. AssertNotNull (dt);
  157. AssertEquals (typeof (int), dt.ValueType);
  158. AssertEquals (XmlTokenizedType.None, dt.TokenizedType);
  159. object o = xvr.ReadTypedValue ();
  160. AssertEquals (XmlNodeType.Attribute, xvr.NodeType);
  161. AssertEquals (typeof (int), o.GetType ());
  162. int n = (int) o;
  163. AssertEquals (12, n);
  164. Assert (xvr.ReadAttributeValue ()); // can read = seems not proceed.
  165. }
  166. }
  167. }