| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- //
- // MonoTests.System.Xml.XsdParticleValidationTests.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // (C)2003 Atsushi Enomoto
- //
- using System;
- using System.Xml;
- using System.Xml.Schema;
- using NUnit.Framework;
- #if NET_2_0
- using ValidationException = System.Xml.Schema.XmlSchemaValidationException;
- #else
- using ValidationException = System.Xml.Schema.XmlSchemaException;
- #endif
- namespace MonoTests.System.Xml
- {
- // using XmlValidatingReader = XmlTextReader;
- [TestFixture]
- public class XsdParticleValidationTests : Assertion
- {
- XmlSchema schema;
- XmlReader xr;
- XmlValidatingReader xvr;
- private void PrepareReader1 (string xsdUrl, string xml)
- {
- schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/XsdValidation/" + xsdUrl), null);
- xr = new XmlTextReader (xml, XmlNodeType.Document, null);
- xvr = new XmlValidatingReader (xr);
- xvr.Schemas.Add (schema);
- // xvr = xr as XmlValidatingReader;
- }
- [Test]
- public void ValidateRootElementOnlyValid ()
- {
- PrepareReader1 ("1.xsd", "<root xmlns='urn:foo' />");
- xvr.Read ();
- PrepareReader1 ("1.xsd", "<root xmlns='urn:foo'></root>");
- xvr.Read ();
- xvr.Read ();
- }
- [Test]
- #if NET_2_0
- [Category ("NotDotNet")]
- // MS.NET throws XmlSchemaException, not -ValidationException.
- #endif
- [ExpectedException (typeof (ValidationException))]
- public void ValidateRootElementOnlyInvalid ()
- {
- PrepareReader1 ("1.xsd", "<invalid xmlns='urn:foo' />");
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateRootElementOnlyInvalid2 ()
- {
- PrepareReader1 ("1.xsd", "<root xmlns='urn:foo'><invalid_child/></root>");
- xvr.Read ();
- xvr.Read ();
- }
- [Test]
- public void ValidateElementContainsElementValid1 ()
- {
- PrepareReader1 ("2.xsd", "<root xmlns='urn:foo'><child/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- public void ValidateElementContainsElementValid2 ()
- {
- PrepareReader1 ("2.xsd", "<root xmlns='urn:foo'><child/><child/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateElementContainsElementInvalid1 ()
- {
- PrepareReader1 ("2.xsd", "<root xmlns='urn:foo'></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateElementContainsElementInvalid2 ()
- {
- PrepareReader1 ("2.xsd", "<root xmlns='urn:foo'><child/><child/><child/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- public void ValidateSequenceValid ()
- {
- PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/><child1/><child2/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateSequenceInvalid1 ()
- {
- PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateSequenceInvalid2 ()
- {
- PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateSequenceInvalid3 ()
- {
- PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/><child1/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateSequenceInvalid4 ()
- {
- PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/><child1/><child2/><child1/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateSequenceInvalid5 ()
- {
- PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/><child1/><child2/><child1/><child2/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- public void ValidateChoiceValid ()
- {
- PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child1/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child2/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child1/><child2/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child2/><child2/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child2/><child2/><child2/><child2/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child2/><child2/><child1/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateChoiceInvalid1 ()
- {
- PrepareReader1 ("4.xsd", "<root xmlns='urn:foo'><child1/><child1/><child1/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateChoiceInvalid2 ()
- {
- PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child2/><child2/><child2/><child2/><child2/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateChoiceInvalid3 ()
- {
- PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child2/><child2/><child2/><child1/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- [Test]
- [ExpectedException (typeof (ValidationException))]
- public void ValidateChoiceInvalid4 ()
- {
- PrepareReader1 ("3.xsd", "<root xmlns='urn:foo'><child1/><child2/><child2/><child2/></root>");
- while (!xvr.EOF)
- xvr.Read ();
- }
- }
- }
|