XsdParticleValidationTests.cs 5.7 KB

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