XsdParticleValidationTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // MonoTests.System.Xml.XsdParticleValidationTests.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. // using XmlValidatingReader = XmlTextReader;
  16. [TestFixture]
  17. public class XsdValidatingReaderTests : Assertion
  18. {
  19. XmlSchema schema;
  20. XmlReader xr;
  21. XmlValidatingReader xvr;
  22. private void PrepareReader1 (string xsdUrl, string xml)
  23. {
  24. schema = XmlSchema.Read (new XmlTextReader ("XmlFiles/XsdValidation/" + xsdUrl), null);
  25. xr = new XmlTextReader (xml, XmlNodeType.Document, null);
  26. xvr = new XmlValidatingReader (xr);
  27. xvr.Schemas.Add (schema);
  28. // xvr = xr as XmlValidatingReader;
  29. }
  30. [Test]
  31. public void ValidateRootElementOnlyValid ()
  32. {
  33. PrepareReader1 ("1.xsd", "<root xmlns='urn:foo' />");
  34. xvr.Read ();
  35. }
  36. [Test]
  37. [ExpectedException (typeof (XmlSchemaException))]
  38. public void ValidateRootElementOnlyInvalid ()
  39. {
  40. PrepareReader1 ("1.xsd", "<invalid xmlns='urn:foo' />");
  41. xvr.Read ();
  42. }
  43. [Test]
  44. [ExpectedException (typeof (XmlSchemaException))]
  45. public void ValidateRootElementOnlyInvalid2 ()
  46. {
  47. PrepareReader1 ("1.xsd", "<root xmlns='urn:foo'><invalid_child/></root>");
  48. xvr.Read ();
  49. xvr.Read ();
  50. }
  51. [Test]
  52. public void ValidateElementContainsElementValid1 ()
  53. {
  54. PrepareReader1 ("2.xsd", "<root xmlns='urn:foo'><child/></root>");
  55. while (!xvr.EOF)
  56. xvr.Read ();
  57. }
  58. [Test]
  59. public void ValidateElementContainsElementValid2 ()
  60. {
  61. PrepareReader1 ("2.xsd", "<root xmlns='urn:foo'><child/><child/></root>");
  62. while (!xvr.EOF)
  63. xvr.Read ();
  64. }
  65. [Test]
  66. [ExpectedException (typeof (XmlSchemaException))]
  67. public void ValidateElementContainsElementInvalid1 ()
  68. {
  69. PrepareReader1 ("2.xsd", "<root xmlns='urn:foo'></root>");
  70. while (!xvr.EOF)
  71. xvr.Read ();
  72. }
  73. [Test]
  74. [ExpectedException (typeof (XmlSchemaException))]
  75. public void ValidateElementContainsElementInvalid2 ()
  76. {
  77. PrepareReader1 ("2.xsd", "<root xmlns='urn:foo'><child/><child/><child/></root>");
  78. while (!xvr.EOF)
  79. xvr.Read ();
  80. }
  81. [Test]
  82. public void ValidateSequenceValid ()
  83. {
  84. PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/></root>");
  85. while (!xvr.EOF)
  86. xvr.Read ();
  87. PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/><child1/><child2/></root>");
  88. while (!xvr.EOF)
  89. xvr.Read ();
  90. }
  91. [Test]
  92. [ExpectedException (typeof (XmlSchemaException))]
  93. public void ValidateSequenceInvalid1 ()
  94. {
  95. PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'></root>");
  96. while (!xvr.EOF)
  97. xvr.Read ();
  98. }
  99. [Test]
  100. [ExpectedException (typeof (XmlSchemaException))]
  101. public void ValidateSequenceInvalid2 ()
  102. {
  103. PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/></root>");
  104. while (!xvr.EOF)
  105. xvr.Read ();
  106. }
  107. [Test]
  108. [ExpectedException (typeof (XmlSchemaException))]
  109. public void ValidateSequenceInvalid3 ()
  110. {
  111. PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/><child1/></root>");
  112. while (!xvr.EOF)
  113. xvr.Read ();
  114. }
  115. [Test]
  116. [ExpectedException (typeof (XmlSchemaException))]
  117. public void ValidateSequenceInvalid4 ()
  118. {
  119. PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/><child1/><child2/><child1/></root>");
  120. while (!xvr.EOF)
  121. xvr.Read ();
  122. }
  123. [Test]
  124. [ExpectedException (typeof (XmlSchemaException))]
  125. public void ValidateSequenceInvalid5 ()
  126. {
  127. PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/><child1/><child2/><child1/><child2/></root>");
  128. while (!xvr.EOF)
  129. xvr.Read ();
  130. }
  131. [Test]
  132. public void ValidateChoiceValid ()
  133. {
  134. PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child1/></root>");
  135. while (!xvr.EOF)
  136. xvr.Read ();
  137. PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child2/></root>");
  138. while (!xvr.EOF)
  139. xvr.Read ();
  140. PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child1/><child2/></root>");
  141. while (!xvr.EOF)
  142. xvr.Read ();
  143. PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child2/><child2/></root>");
  144. while (!xvr.EOF)
  145. xvr.Read ();
  146. PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child2/><child2/><child2/><child2/></root>");
  147. while (!xvr.EOF)
  148. xvr.Read ();
  149. PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child2/><child2/><child1/></root>");
  150. while (!xvr.EOF)
  151. xvr.Read ();
  152. PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'></root>");
  153. while (!xvr.EOF)
  154. xvr.Read ();
  155. }
  156. [Test]
  157. [ExpectedException (typeof (XmlSchemaException))]
  158. public void ValidateChoiceInvalid1 ()
  159. {
  160. PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child1/><child1/><child1/></root>");
  161. while (!xvr.EOF)
  162. xvr.Read ();
  163. }
  164. [Test]
  165. [ExpectedException (typeof (XmlSchemaException))]
  166. public void ValidateChoiceInvalid2 ()
  167. {
  168. PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child2/><child2/><child2/><child2/><child2/></root>");
  169. while (!xvr.EOF)
  170. xvr.Read ();
  171. }
  172. [Test]
  173. [ExpectedException (typeof (XmlSchemaException))]
  174. public void ValidateChoiceInvalid3 ()
  175. {
  176. PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child2/><child2/><child2/><child1/></root>");
  177. while (!xvr.EOF)
  178. xvr.Read ();
  179. }
  180. [Test]
  181. [ExpectedException (typeof (XmlSchemaException))]
  182. public void ValidateChoiceInvalid4 ()
  183. {
  184. PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/><child2/><child2/></root>");
  185. while (!xvr.EOF)
  186. xvr.Read ();
  187. }
  188. }
  189. }