XsdValidatingReaderTests.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 ();
  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. 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. public void TestNamespacesFalse ()
  115. {
  116. // This tests if Namespaces=false is specified, then
  117. // the reader's NamespaceURI should be always string.Empty and
  118. // validation should be done against such schema that has target ns as "".
  119. string xml = "<x:root xmlns:x='urn:foo' />";
  120. xvr = PrepareXmlReader (xml);
  121. xvr.Namespaces = false;
  122. AssertEquals (ValidationType.Auto, xvr.ValidationType);
  123. XmlSchema schema = new XmlSchema ();
  124. schema.TargetNamespace = "urn:foo";
  125. XmlSchemaElement elem = new XmlSchemaElement ();
  126. elem.Name = "root";
  127. schema.Items.Add (elem);
  128. xvr.Schemas.Add (schema);
  129. xvr.Read (); // root
  130. Assert (!xvr.Namespaces);
  131. AssertEquals ("x:root", xvr.Name);
  132. // LocalName may contain colons.
  133. AssertEquals ("x:root", xvr.LocalName);
  134. // NamespaceURI is not supplied.
  135. AssertEquals ("", xvr.NamespaceURI);
  136. }
  137. [Test]
  138. public void TestReadTypedAttributeValue ()
  139. {
  140. string xml = "<root attr='12'></root>";
  141. XmlSchema schema = new XmlSchema ();
  142. XmlSchemaElement elem = new XmlSchemaElement ();
  143. elem.Name = "root";
  144. XmlSchemaComplexType ct = new XmlSchemaComplexType ();
  145. XmlSchemaAttribute attr = new XmlSchemaAttribute ();
  146. attr.Name = "attr";
  147. attr.SchemaTypeName = new XmlQualifiedName ("int", XmlSchema.Namespace);
  148. ct.Attributes.Add (attr);
  149. elem.SchemaType = ct;
  150. schema.Items.Add (elem);
  151. xvr = PrepareXmlReader (xml);
  152. xvr.Schemas.Add (schema);
  153. xvr.Read ();
  154. AssertEquals ("root", xvr.Name);
  155. Assert (xvr.MoveToNextAttribute ()); // attr
  156. AssertEquals ("attr", xvr.Name);
  157. XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
  158. AssertNotNull (dt);
  159. AssertEquals (typeof (int), dt.ValueType);
  160. AssertEquals (XmlTokenizedType.None, dt.TokenizedType);
  161. object o = xvr.ReadTypedValue ();
  162. AssertEquals (XmlNodeType.Attribute, xvr.NodeType);
  163. AssertEquals (typeof (int), o.GetType ());
  164. int n = (int) o;
  165. AssertEquals (12, n);
  166. Assert (xvr.ReadAttributeValue ()); // can read = seems not proceed.
  167. }
  168. }
  169. }