XsdParticleValidationTests.cs 5.9 KB

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