diff options
Diffstat (limited to 'mcs/class/System.XML/Test/System.Xml')
8 files changed, 667 insertions, 651 deletions
diff --git a/mcs/class/System.XML/Test/System.Xml/XmlNodeTests.cs b/mcs/class/System.XML/Test/System.Xml/XmlNodeTests.cs index 89e659542d1..895475450a2 100644 --- a/mcs/class/System.XML/Test/System.Xml/XmlNodeTests.cs +++ b/mcs/class/System.XML/Test/System.Xml/XmlNodeTests.cs @@ -16,6 +16,8 @@ using System.Xml; using NUnit.Framework; +using MonoTests.Helpers; + namespace MonoTests.System.Xml { [TestFixture] @@ -448,7 +450,7 @@ namespace MonoTests.System.Xml { // See bug #64120. XmlDocument doc = new XmlDocument (); - doc.Load ("Test/XmlFiles/simple.xml"); + doc.Load (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/simple.xml")); XmlElement el = doc.CreateElement ("foo"); Assert.AreEqual (String.Empty, el.BaseURI); doc.DocumentElement.AppendChild (el); diff --git a/mcs/class/System.XML/Test/System.Xml/XmlReaderCommonTests.cs b/mcs/class/System.XML/Test/System.Xml/XmlReaderCommonTests.cs index 764ce368cfc..6cd87631870 100644 --- a/mcs/class/System.XML/Test/System.Xml/XmlReaderCommonTests.cs +++ b/mcs/class/System.XML/Test/System.Xml/XmlReaderCommonTests.cs @@ -20,6 +20,8 @@ using System.Threading.Tasks; using NUnit.Framework;
+using MonoTests.Helpers;
+
namespace MonoTests.System.Xml
{
[TestFixture]
@@ -1515,7 +1517,7 @@ namespace MonoTests.System.Xml {
XmlReaderSettings s = new XmlReaderSettings ();
s.ProhibitDtd = false;
- XmlReader xr = XmlReader.Create ("Test/XmlFiles/nested-dtd-test.xml", s);
+ XmlReader xr = XmlReader.Create (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/nested-dtd-test.xml"), s);
xr.Read ();
Assert.AreEqual (XmlNodeType.DocumentType, xr.NodeType, "#1");
xr.Read ();
@@ -1528,7 +1530,7 @@ namespace MonoTests.System.Xml [ExpectedException (typeof (XmlException))]
public void CreateSimpleProhibitDtd ()
{
- XmlReader xr = XmlReader.Create ("Test/XmlFiles/nested-dtd-test.xml");
+ XmlReader xr = XmlReader.Create (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/nested-dtd-test.xml"));
xr.Read ();
}
diff --git a/mcs/class/System.XML/Test/System.Xml/XmlReaderSettingsTests.cs b/mcs/class/System.XML/Test/System.Xml/XmlReaderSettingsTests.cs index 2b20f06e971..c596fa2338d 100644 --- a/mcs/class/System.XML/Test/System.Xml/XmlReaderSettingsTests.cs +++ b/mcs/class/System.XML/Test/System.Xml/XmlReaderSettingsTests.cs @@ -18,6 +18,8 @@ using System.Reflection; using ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags; using AssertType = NUnit.Framework.Assert; +using MonoTests.Helpers; + namespace MonoTests.System.Xml { [TestFixture] @@ -150,7 +152,7 @@ namespace MonoTests.System.Xml public void CreateAndSettings () { Assert.IsNotNull (XmlReader.Create (CreateStream ("<xml/>")).Settings); - Assert.IsNotNull (XmlReader.Create ("Test/XmlFiles/simple.xml").Settings); + Assert.IsNotNull (XmlReader.Create (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/simple.xml")).Settings); } [Test] @@ -297,7 +299,7 @@ namespace MonoTests.System.Xml public void CreateValidatorFromNonIXmlNamespaceResolver () { XmlReaderSettings settings = new XmlReaderSettings (); - settings.Schemas.Add (null, "Test/XmlFiles/xsd/xml.xsd"); + settings.Schemas.Add (null, TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/xml.xsd")); settings.ValidationType = ValidationType.Schema; XmlReader xr = XmlReader.Create (new StringReader ("<root/>")); XmlReader dr = new Commons.Xml.XmlDefaultReader (xr); @@ -311,7 +313,7 @@ namespace MonoTests.System.Xml { XmlReaderSettings settings = new XmlReaderSettings (); settings.XmlResolver = null; - using (XmlReader xr = XmlReader.Create ("Test/XmlFiles/simple.xml", settings)) { + using (XmlReader xr = XmlReader.Create (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/simple.xml"), settings)) { while (!xr.EOF) xr.Read (); } @@ -335,7 +337,7 @@ namespace MonoTests.System.Xml { XmlReaderSettings settings = new XmlReaderSettings (); settings.XmlResolver = new ThrowExceptionResolver (); - using (XmlReader xr = XmlReader.Create ("Test/XmlFiles/simple.xml", settings)) { + using (XmlReader xr = XmlReader.Create (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/simple.xml"), settings)) { while (!xr.EOF) xr.Read (); } @@ -367,7 +369,7 @@ namespace MonoTests.System.Xml XmlSchema xs = new XmlSchema (); settings.Schemas.Add (xs); settings.ValidationType = ValidationType.Schema; - using (XmlReader r = XmlReader.Create ("Test/XmlFiles/simple.xml", settings)) { + using (XmlReader r = XmlReader.Create (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/simple.xml"), settings)) { r.Read (); } } diff --git a/mcs/class/System.XML/Test/System.Xml/XmlTextReaderTests.cs b/mcs/class/System.XML/Test/System.Xml/XmlTextReaderTests.cs index 7205ea7d730..0fe11ec16ed 100644 --- a/mcs/class/System.XML/Test/System.Xml/XmlTextReaderTests.cs +++ b/mcs/class/System.XML/Test/System.Xml/XmlTextReaderTests.cs @@ -15,6 +15,8 @@ using System.Text; using NUnit.Framework; +using MonoTests.Helpers; + namespace MonoTests.System.Xml { [TestFixture] @@ -651,7 +653,7 @@ namespace MonoTests.System.Xml public void ExternalDocument () { XmlDocument doc = new XmlDocument (); - doc.Load ("Test/XmlFiles/nested-dtd-test.xml"); + doc.Load (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/nested-dtd-test.xml")); } [Test] @@ -868,7 +870,7 @@ namespace MonoTests.System.Xml [Test] public void CloseIsNotAlwaysEOF2 () { - XmlTextReader xtr = new XmlTextReader ("Test/XmlFiles/simple.xml"); + XmlTextReader xtr = new XmlTextReader (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/simple.xml")); xtr.Close (); Assert.IsTrue (!xtr.EOF); // Close() != EOF } @@ -1119,7 +1121,7 @@ namespace MonoTests.System.Xml { XmlTextReader xtr = null; try { - xtr = new XmlTextReader (File.OpenText ("Test/XmlFiles/76102.xml")); + xtr = new XmlTextReader (File.OpenText (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/76102.xml"))); while (!xtr.EOF) xtr.Read (); } finally { diff --git a/mcs/class/System.XML/Test/System.Xml/XmlUrlResolverTests.cs b/mcs/class/System.XML/Test/System.Xml/XmlUrlResolverTests.cs index 6926379c96a..9d085ec5036 100644 --- a/mcs/class/System.XML/Test/System.Xml/XmlUrlResolverTests.cs +++ b/mcs/class/System.XML/Test/System.Xml/XmlUrlResolverTests.cs @@ -12,6 +12,8 @@ using System.Xml; using NUnit.Framework;
using System.Reflection;
+using MonoTests.Helpers;
+
namespace MonoTests.System.Xml
{
[TestFixture]
@@ -28,7 +30,7 @@ namespace MonoTests.System.Xml [Test]
public void FileUri ()
{
- Uri resolved = resolver.ResolveUri (null, "Test/XmlFiles/xsd/xml.xsd");
+ Uri resolved = resolver.ResolveUri (null, TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/xml.xsd"));
Assert.AreEqual ("file", resolved.Scheme);
Stream s = resolver.GetEntity (resolved, null, typeof (Stream)) as Stream;
}
diff --git a/mcs/class/System.XML/Test/System.Xml/XsdParticleValidationTests.cs b/mcs/class/System.XML/Test/System.Xml/XsdParticleValidationTests.cs index 69c12c7c525..079ea1d8c83 100644 --- a/mcs/class/System.XML/Test/System.Xml/XsdParticleValidationTests.cs +++ b/mcs/class/System.XML/Test/System.Xml/XsdParticleValidationTests.cs @@ -13,6 +13,8 @@ using NUnit.Framework; using ValidationException = System.Xml.Schema.XmlSchemaValidationException;
+using MonoTests.Helpers;
+
namespace MonoTests.System.Xml
{
// using XmlValidatingReader = XmlTextReader;
@@ -26,7 +28,7 @@ namespace MonoTests.System.Xml private void PrepareReader1 (string xsdUrl, string xml)
{
- schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/XsdValidation/" + xsdUrl), null);
+ schema = XmlSchema.Read (new XmlTextReader (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/XsdValidation/" + xsdUrl)), null);
xr = new XmlTextReader (xml, XmlNodeType.Document, null);
xvr = new XmlValidatingReader (xr);
xvr.Schemas.Add (schema);
diff --git a/mcs/class/System.XML/Test/System.Xml/XsdValidatingReaderTests.cs b/mcs/class/System.XML/Test/System.Xml/XsdValidatingReaderTests.cs index 8bbcf11706a..ac5ced83bb6 100644 --- a/mcs/class/System.XML/Test/System.Xml/XsdValidatingReaderTests.cs +++ b/mcs/class/System.XML/Test/System.Xml/XsdValidatingReaderTests.cs @@ -1,633 +1,635 @@ -//
-// MonoTests.System.Xml.XsdValidatingReaderTests.cs
-//
-// Author:
-// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
-//
-// (C)2003 Atsushi Enomoto
-// (C)2005-2007 Novell, Inc.
-//
-using System;
-using System.IO;
-using System.Net;
-using System.Xml;
-using System.Xml.Schema;
-using NUnit.Framework;
-
-namespace MonoTests.System.Xml
-{
- [TestFixture]
- public class XsdValidatingReaderTests
- {
- public XsdValidatingReaderTests ()
- {
- }
-
- XmlReader xtr;
- XmlValidatingReader xvr;
-
- private XmlValidatingReader PrepareXmlReader (string xml)
- {
- XmlReader reader = new XmlTextReader (xml, XmlNodeType.Document, null);
-// XmlDocument doc = new XmlDocument ();
-// doc.LoadXml (xml);
-// XmlReader reader = new XmlNodeReader (doc);
-
- return new XmlValidatingReader (reader);
- }
-
- [Test]
- public void TestEmptySchema ()
- {
- string xml = "<root/>";
- xvr = PrepareXmlReader (xml);
- xvr.ValidationType = ValidationType.Schema;
- xvr.Read (); // Is is missing schema component.
- }
-
- [Test]
- public void TestSimpleValidation ()
- {
- string xml = "<root/>";
- xvr = PrepareXmlReader (xml);
- Assert.AreEqual (ValidationType.Auto, xvr.ValidationType);
- XmlSchema schema = new XmlSchema ();
- XmlSchemaElement elem = new XmlSchemaElement ();
- elem.Name = "root";
- schema.Items.Add (elem);
- xvr.Schemas.Add (schema);
- xvr.Read (); // root
- Assert.AreEqual (ValidationType.Auto, xvr.ValidationType);
- xvr.Read (); // EOF
-
- xml = "<hoge/>";
- xvr = PrepareXmlReader (xml);
- xvr.Schemas.Add (schema);
- try {
- xvr.Read ();
- Assert.Fail ("element mismatch is incorrectly allowed");
- } catch (XmlSchemaException) {
- }
-
- xml = "<hoge xmlns='urn:foo' />";
- xvr = PrepareXmlReader (xml);
- xvr.Schemas.Add (schema);
- try {
- xvr.Read ();
- Assert.Fail ("Element in different namespace is incorrectly allowed.");
- } catch (XmlSchemaException) {
- }
- }
-
- [Test]
- public void TestReadTypedValueSimple ()
- {
- string xml = "<root>12</root>";
- XmlSchema schema = new XmlSchema ();
- XmlSchemaElement elem = new XmlSchemaElement ();
- elem.Name = "root";
- elem.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
- schema.Items.Add (elem);
-
- // Lap 1:
-
- xvr = PrepareXmlReader (xml);
- xvr.Schemas.Add (schema);
- // Read directly from root.
- object o = xvr.ReadTypedValue ();
- Assert.AreEqual (ReadState.Initial, xvr.ReadState);
- Assert.IsNull (o);
-
- xvr.Read (); // element root
- Assert.AreEqual (XmlNodeType.Element, xvr.NodeType);
- Assert.IsNotNull (xvr.SchemaType);
- Assert.IsTrue (xvr.SchemaType is XmlSchemaDatatype);
- o = xvr.ReadTypedValue (); // read "12"
- Assert.AreEqual (XmlNodeType.EndElement, xvr.NodeType);
- Assert.IsNotNull (o);
- Assert.AreEqual (typeof (decimal), o.GetType ());
- decimal n = (decimal) o;
- Assert.AreEqual (12, n);
- Assert.IsTrue (!xvr.EOF);
- Assert.AreEqual ("root", xvr.Name);
- Assert.IsNull (xvr.SchemaType); // EndElement's type
-
- // Lap 2:
-
- xvr = PrepareXmlReader (xml);
- xvr.Schemas.Add (schema);
- xvr.Read (); // root
- XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
- Assert.IsNotNull (dt);
- Assert.AreEqual (typeof (decimal), dt.ValueType);
- Assert.AreEqual (XmlTokenizedType.None, dt.TokenizedType);
- xvr.Read (); // text "12"
- Assert.IsNull (xvr.SchemaType);
- o = xvr.ReadTypedValue ();
- // ReadTypedValue is different from ReadString().
- Assert.IsNull (o);
- }
-
- [Test]
- [Ignore ("XML Schema validator should not be available for validating non namespace-aware XmlReader that handled colon as a name character")]
- public void TestNamespacesFalse ()
- {
- // This tests if Namespaces=false is specified, then
- // the reader's NamespaceURI should be always string.Empty and
- // validation should be done against such schema that has target ns as "".
- string xml = "<x:root xmlns:x='urn:foo' />";
- xvr = PrepareXmlReader (xml);
- xvr.Namespaces = false;
- Assert.AreEqual (ValidationType.Auto, xvr.ValidationType);
- XmlSchema schema = new XmlSchema ();
- schema.TargetNamespace = "urn:foo";
- XmlSchemaElement elem = new XmlSchemaElement ();
- elem.Name = "root";
- schema.Items.Add (elem);
- xvr.Schemas.Add (schema);
- xvr.Read (); // root
- Assert.IsTrue (!xvr.Namespaces);
- Assert.AreEqual ("x:root", xvr.Name);
- // LocalName may contain colons.
- Assert.AreEqual ("x:root", xvr.LocalName);
- // NamespaceURI is not supplied.
- Assert.AreEqual ("", xvr.NamespaceURI);
- }
-
- [Test]
- public void TestReadTypedAttributeValue ()
- {
- string xml = "<root attr='12'></root>";
- XmlSchema schema = new XmlSchema ();
- XmlSchemaElement elem = new XmlSchemaElement ();
- elem.Name = "root";
- XmlSchemaComplexType ct = new XmlSchemaComplexType ();
- XmlSchemaAttribute attr = new XmlSchemaAttribute ();
- attr.Name = "attr";
- attr.SchemaTypeName = new XmlQualifiedName ("int", XmlSchema.Namespace);
- ct.Attributes.Add (attr);
- elem.SchemaType = ct;
- schema.Items.Add (elem);
-
- xvr = PrepareXmlReader (xml);
- xvr.Schemas.Add (schema);
- xvr.Read ();
- Assert.AreEqual ("root", xvr.Name);
- Assert.IsTrue (xvr.MoveToNextAttribute ()); // attr
- Assert.AreEqual ("attr", xvr.Name);
- XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
- Assert.IsNotNull (dt);
- Assert.AreEqual (typeof (int), dt.ValueType);
- Assert.AreEqual (XmlTokenizedType.None, dt.TokenizedType);
- object o = xvr.ReadTypedValue ();
- Assert.AreEqual (XmlNodeType.Attribute, xvr.NodeType);
- Assert.AreEqual (typeof (int), o.GetType ());
- int n = (int) o;
- Assert.AreEqual (12, n);
- Assert.IsTrue (xvr.ReadAttributeValue ()); // can read = seems not proceed.
- }
-
- [Test]
- public void DuplicateSchemaAssignment ()
- {
- string xml = @"<data
- xmlns='http://www.test.com/schemas/'
- xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
- xsi:schemaLocation='http://www.test.com/schemas/ /home/user/schema.xsd' />";
- string xsd = @"<xs:schema
- targetNamespace='http://www.test.com/schemas/'
- xmlns:xs='http://www.w3.org/2001/XMLSchema'
- xmlns='http://www.test.com/schemas/' >
- <xs:element name='data' /></xs:schema>";
-
- string xmlns = "http://www.test.com/schemas/";
-
- XmlValidatingReader xvr = new XmlValidatingReader (
- xml, XmlNodeType.Document, null);
- XmlSchemaCollection schemas = new XmlSchemaCollection ();
- schemas.Add (XmlSchema.Read (new XmlTextReader (
- xsd, XmlNodeType.Document, null), null));
- xvr.Schemas.Add (schemas);
- while (!xvr.EOF)
- xvr.Read ();
- }
-
- [Test] // bug #76234
- public void DTDValidatorNamespaceHandling ()
- {
- string xml = "<xml xmlns='urn:a'> <foo> <a:bar xmlns='urn:b' xmlns:a='urn:a' /> <bug /> </foo> </xml>";
- XmlValidatingReader vr = new XmlValidatingReader (
- xml, XmlNodeType.Document, null);
- vr.Read ();
- vr.Read (); // whitespace
- Assert.AreEqual (String.Empty, vr.NamespaceURI, "#1");
- vr.Read (); // foo
- Assert.AreEqual ("urn:a", vr.NamespaceURI, "#2");
- vr.Read (); // whitespace
- vr.Read (); // a:bar
- Assert.AreEqual ("urn:a", vr.NamespaceURI, "#3");
- vr.Read (); // whitespace
- vr.Read (); // bug
- Assert.AreEqual ("urn:a", vr.NamespaceURI, "#4");
- }
-
- [Test]
- public void MultipleSchemaInSchemaLocation ()
- {
- XmlTextReader xtr = new XmlTextReader ("Test/XmlFiles/xsd/multi-schemaLocation.xml");
- XmlValidatingReader vr = new XmlValidatingReader (xtr);
- while (!vr.EOF)
- vr.Read ();
- }
-
- [Test]
- public void ReadTypedValueSimpleTypeRestriction ()
- {
- string xml = "<root>xx</root><!-- after -->";
- string xsd = @"
-<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
- <xs:element name='root'>
- <xs:simpleType>
- <xs:restriction base='xs:string'>
- <xs:minLength value='2' />
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-</xs:schema>";
- XmlTextReader xir =
- new XmlTextReader (xml, XmlNodeType.Document, null);
- XmlTextReader xsr =
- new XmlTextReader (xsd, XmlNodeType.Document, null);
- XmlValidatingReader vr = new XmlValidatingReader (xir);
- vr.Schemas.Add (XmlSchema.Read (xsr, null));
- vr.Read (); // root
- Assert.AreEqual ("xx", vr.ReadTypedValue ());
- Assert.AreEqual (XmlNodeType.EndElement, vr.NodeType);
- }
-
- // If we normalize string before validating with facets,
- // this test will fail. It will also fail if ReadTypedValue()
- // ignores whitespace nodes.
- [Test]
- public void ReadTypedValueWhitespaces ()
- {
- string xml = "<root> </root><!-- after -->";
- string xsd = @"
-<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
- <xs:element name='root'>
- <xs:simpleType>
- <xs:restriction base='xs:string'>
- <xs:minLength value='2' />
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-</xs:schema>";
- XmlTextReader xir =
- new XmlTextReader (xml, XmlNodeType.Document, null);
- XmlTextReader xsr =
- new XmlTextReader (xsd, XmlNodeType.Document, null);
- XmlValidatingReader vr = new XmlValidatingReader (xir);
- vr.Schemas.Add (XmlSchema.Read (xsr, null));
- vr.Read (); // root
- Assert.AreEqual (" ", vr.ReadTypedValue ());
- Assert.AreEqual (XmlNodeType.EndElement, vr.NodeType);
- }
-
- [Test] // bug #77241
- public void EmptyContentAllowWhitespace ()
- {
- string doc = @"
-<root>
- <!-- some comment -->
- <child/>
-</root>
-";
- string schema = @"
-<xsd:schema xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
- <xsd:element name=""root"">
- <xsd:complexType>
- <xsd:sequence>
- <xsd:element name=""child"" type=""xsd:string"" />
- </xsd:sequence>
- </xsd:complexType>
- </xsd:element>
-</xsd:schema>
-";
- XmlValidatingReader reader = new XmlValidatingReader (
- new XmlTextReader (new StringReader (doc)));
- reader.Schemas.Add (null,
- new XmlTextReader (new StringReader (schema)));
- while (reader.Read ())
- ;
- }
-
- [Test] // bug #79650
+// +// MonoTests.System.Xml.XsdValidatingReaderTests.cs +// +// Author: +// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp> +// +// (C)2003 Atsushi Enomoto +// (C)2005-2007 Novell, Inc. +// +using System; +using System.IO; +using System.Net; +using System.Xml; +using System.Xml.Schema; +using NUnit.Framework; + +using MonoTests.Helpers; + +namespace MonoTests.System.Xml +{ + [TestFixture] + public class XsdValidatingReaderTests + { + public XsdValidatingReaderTests () + { + } + + XmlReader xtr; + XmlValidatingReader xvr; + + private XmlValidatingReader PrepareXmlReader (string xml) + { + XmlReader reader = new XmlTextReader (xml, XmlNodeType.Document, null); +// XmlDocument doc = new XmlDocument (); +// doc.LoadXml (xml); +// XmlReader reader = new XmlNodeReader (doc); + + return new XmlValidatingReader (reader); + } + + [Test] + public void TestEmptySchema () + { + string xml = "<root/>"; + xvr = PrepareXmlReader (xml); + xvr.ValidationType = ValidationType.Schema; + xvr.Read (); // Is is missing schema component. + } + + [Test] + public void TestSimpleValidation () + { + string xml = "<root/>"; + xvr = PrepareXmlReader (xml); + Assert.AreEqual (ValidationType.Auto, xvr.ValidationType); + XmlSchema schema = new XmlSchema (); + XmlSchemaElement elem = new XmlSchemaElement (); + elem.Name = "root"; + schema.Items.Add (elem); + xvr.Schemas.Add (schema); + xvr.Read (); // root + Assert.AreEqual (ValidationType.Auto, xvr.ValidationType); + xvr.Read (); // EOF + + xml = "<hoge/>"; + xvr = PrepareXmlReader (xml); + xvr.Schemas.Add (schema); + try { + xvr.Read (); + Assert.Fail ("element mismatch is incorrectly allowed"); + } catch (XmlSchemaException) { + } + + xml = "<hoge xmlns='urn:foo' />"; + xvr = PrepareXmlReader (xml); + xvr.Schemas.Add (schema); + try { + xvr.Read (); + Assert.Fail ("Element in different namespace is incorrectly allowed."); + } catch (XmlSchemaException) { + } + } + + [Test] + public void TestReadTypedValueSimple () + { + string xml = "<root>12</root>"; + XmlSchema schema = new XmlSchema (); + XmlSchemaElement elem = new XmlSchemaElement (); + elem.Name = "root"; + elem.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace); + schema.Items.Add (elem); + + // Lap 1: + + xvr = PrepareXmlReader (xml); + xvr.Schemas.Add (schema); + // Read directly from root. + object o = xvr.ReadTypedValue (); + Assert.AreEqual (ReadState.Initial, xvr.ReadState); + Assert.IsNull (o); + + xvr.Read (); // element root + Assert.AreEqual (XmlNodeType.Element, xvr.NodeType); + Assert.IsNotNull (xvr.SchemaType); + Assert.IsTrue (xvr.SchemaType is XmlSchemaDatatype); + o = xvr.ReadTypedValue (); // read "12" + Assert.AreEqual (XmlNodeType.EndElement, xvr.NodeType); + Assert.IsNotNull (o); + Assert.AreEqual (typeof (decimal), o.GetType ()); + decimal n = (decimal) o; + Assert.AreEqual (12, n); + Assert.IsTrue (!xvr.EOF); + Assert.AreEqual ("root", xvr.Name); + Assert.IsNull (xvr.SchemaType); // EndElement's type + + // Lap 2: + + xvr = PrepareXmlReader (xml); + xvr.Schemas.Add (schema); + xvr.Read (); // root + XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype; + Assert.IsNotNull (dt); + Assert.AreEqual (typeof (decimal), dt.ValueType); + Assert.AreEqual (XmlTokenizedType.None, dt.TokenizedType); + xvr.Read (); // text "12" + Assert.IsNull (xvr.SchemaType); + o = xvr.ReadTypedValue (); + // ReadTypedValue is different from ReadString(). + Assert.IsNull (o); + } + + [Test] + [Ignore ("XML Schema validator should not be available for validating non namespace-aware XmlReader that handled colon as a name character")] + public void TestNamespacesFalse () + { + // This tests if Namespaces=false is specified, then + // the reader's NamespaceURI should be always string.Empty and + // validation should be done against such schema that has target ns as "". + string xml = "<x:root xmlns:x='urn:foo' />"; + xvr = PrepareXmlReader (xml); + xvr.Namespaces = false; + Assert.AreEqual (ValidationType.Auto, xvr.ValidationType); + XmlSchema schema = new XmlSchema (); + schema.TargetNamespace = "urn:foo"; + XmlSchemaElement elem = new XmlSchemaElement (); + elem.Name = "root"; + schema.Items.Add (elem); + xvr.Schemas.Add (schema); + xvr.Read (); // root + Assert.IsTrue (!xvr.Namespaces); + Assert.AreEqual ("x:root", xvr.Name); + // LocalName may contain colons. + Assert.AreEqual ("x:root", xvr.LocalName); + // NamespaceURI is not supplied. + Assert.AreEqual ("", xvr.NamespaceURI); + } + + [Test] + public void TestReadTypedAttributeValue () + { + string xml = "<root attr='12'></root>"; + XmlSchema schema = new XmlSchema (); + XmlSchemaElement elem = new XmlSchemaElement (); + elem.Name = "root"; + XmlSchemaComplexType ct = new XmlSchemaComplexType (); + XmlSchemaAttribute attr = new XmlSchemaAttribute (); + attr.Name = "attr"; + attr.SchemaTypeName = new XmlQualifiedName ("int", XmlSchema.Namespace); + ct.Attributes.Add (attr); + elem.SchemaType = ct; + schema.Items.Add (elem); + + xvr = PrepareXmlReader (xml); + xvr.Schemas.Add (schema); + xvr.Read (); + Assert.AreEqual ("root", xvr.Name); + Assert.IsTrue (xvr.MoveToNextAttribute ()); // attr + Assert.AreEqual ("attr", xvr.Name); + XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype; + Assert.IsNotNull (dt); + Assert.AreEqual (typeof (int), dt.ValueType); + Assert.AreEqual (XmlTokenizedType.None, dt.TokenizedType); + object o = xvr.ReadTypedValue (); + Assert.AreEqual (XmlNodeType.Attribute, xvr.NodeType); + Assert.AreEqual (typeof (int), o.GetType ()); + int n = (int) o; + Assert.AreEqual (12, n); + Assert.IsTrue (xvr.ReadAttributeValue ()); // can read = seems not proceed. + } + + [Test] + public void DuplicateSchemaAssignment () + { + string xml = @"<data + xmlns='http://www.test.com/schemas/' + xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' + xsi:schemaLocation='http://www.test.com/schemas/ /home/user/schema.xsd' />"; + string xsd = @"<xs:schema + targetNamespace='http://www.test.com/schemas/' + xmlns:xs='http://www.w3.org/2001/XMLSchema' + xmlns='http://www.test.com/schemas/' > + <xs:element name='data' /></xs:schema>"; + + string xmlns = "http://www.test.com/schemas/"; + + XmlValidatingReader xvr = new XmlValidatingReader ( + xml, XmlNodeType.Document, null); + XmlSchemaCollection schemas = new XmlSchemaCollection (); + schemas.Add (XmlSchema.Read (new XmlTextReader ( + xsd, XmlNodeType.Document, null), null)); + xvr.Schemas.Add (schemas); + while (!xvr.EOF) + xvr.Read (); + } + + [Test] // bug #76234 + public void DTDValidatorNamespaceHandling () + { + string xml = "<xml xmlns='urn:a'> <foo> <a:bar xmlns='urn:b' xmlns:a='urn:a' /> <bug /> </foo> </xml>"; + XmlValidatingReader vr = new XmlValidatingReader ( + xml, XmlNodeType.Document, null); + vr.Read (); + vr.Read (); // whitespace + Assert.AreEqual (String.Empty, vr.NamespaceURI, "#1"); + vr.Read (); // foo + Assert.AreEqual ("urn:a", vr.NamespaceURI, "#2"); + vr.Read (); // whitespace + vr.Read (); // a:bar + Assert.AreEqual ("urn:a", vr.NamespaceURI, "#3"); + vr.Read (); // whitespace + vr.Read (); // bug + Assert.AreEqual ("urn:a", vr.NamespaceURI, "#4"); + } + + [Test] + public void MultipleSchemaInSchemaLocation () + { + XmlTextReader xtr = new XmlTextReader (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/multi-schemaLocation.xml")); + XmlValidatingReader vr = new XmlValidatingReader (xtr); + while (!vr.EOF) + vr.Read (); + } + + [Test] + public void ReadTypedValueSimpleTypeRestriction () + { + string xml = "<root>xx</root><!-- after -->"; + string xsd = @" +<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'> + <xs:element name='root'> + <xs:simpleType> + <xs:restriction base='xs:string'> + <xs:minLength value='2' /> + </xs:restriction> + </xs:simpleType> + </xs:element> +</xs:schema>"; + XmlTextReader xir = + new XmlTextReader (xml, XmlNodeType.Document, null); + XmlTextReader xsr = + new XmlTextReader (xsd, XmlNodeType.Document, null); + XmlValidatingReader vr = new XmlValidatingReader (xir); + vr.Schemas.Add (XmlSchema.Read (xsr, null)); + vr.Read (); // root + Assert.AreEqual ("xx", vr.ReadTypedValue ()); + Assert.AreEqual (XmlNodeType.EndElement, vr.NodeType); + } + + // If we normalize string before validating with facets, + // this test will fail. It will also fail if ReadTypedValue() + // ignores whitespace nodes. + [Test] + public void ReadTypedValueWhitespaces () + { + string xml = "<root> </root><!-- after -->"; + string xsd = @" +<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'> + <xs:element name='root'> + <xs:simpleType> + <xs:restriction base='xs:string'> + <xs:minLength value='2' /> + </xs:restriction> + </xs:simpleType> + </xs:element> +</xs:schema>"; + XmlTextReader xir = + new XmlTextReader (xml, XmlNodeType.Document, null); + XmlTextReader xsr = + new XmlTextReader (xsd, XmlNodeType.Document, null); + XmlValidatingReader vr = new XmlValidatingReader (xir); + vr.Schemas.Add (XmlSchema.Read (xsr, null)); + vr.Read (); // root + Assert.AreEqual (" ", vr.ReadTypedValue ()); + Assert.AreEqual (XmlNodeType.EndElement, vr.NodeType); + } + + [Test] // bug #77241 + public void EmptyContentAllowWhitespace () + { + string doc = @" +<root> + <!-- some comment --> + <child/> +</root> +"; + string schema = @" +<xsd:schema xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> + <xsd:element name=""root""> + <xsd:complexType> + <xsd:sequence> + <xsd:element name=""child"" type=""xsd:string"" /> + </xsd:sequence> + </xsd:complexType> + </xsd:element> +</xsd:schema> +"; + XmlValidatingReader reader = new XmlValidatingReader ( + new XmlTextReader (new StringReader (doc))); + reader.Schemas.Add (null, + new XmlTextReader (new StringReader (schema))); + while (reader.Read ()) + ; + } + + [Test] // bug #79650 // LAMESPEC: .NET does not throw XmlSchemaValidationException - [ExpectedException (typeof (XmlSchemaException))]
- public void EnumerationFacetOnAttribute ()
- {
- string xml = "<test mode='NOT AN ENUMERATION VALUE' />";
- XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/79650.xsd"), null);
- XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
- xvr.ValidationType = ValidationType.Schema;
- xvr.Schemas.Add (schema);
- while (!xvr.EOF)
- xvr.Read ();
- }
-
- class XmlErrorResolver : XmlResolver
- {
- public override ICredentials Credentials {
- set { }
- }
-
- public override object GetEntity (Uri uri, string role, Type type)
- {
- throw new Exception ();
- }
- }
-
- [Test] // bug #79924
- public void ValidationTypeNoneIgnoreSchemaLocations ()
- {
- string xml = "<project xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='nosuchschema.xsd'/>";
- XmlValidatingReader vr = new XmlValidatingReader (
- new XmlTextReader (new StringReader (xml)));
- vr.ValidationType = ValidationType.None;
- vr.XmlResolver = new XmlErrorResolver ();
- while (!vr.EOF)
- vr.Read ();
- }
-
- [Test] // bug #336625
- public void ValidationTypeNoneIgnoreLocatedSchemaErrors ()
- {
- string xml = "<test xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='Test/XmlFiles/xsd/336625.xsd'/>";
- XmlValidatingReader vr = new XmlValidatingReader (
- new XmlTextReader (new StringReader (xml)));
- vr.ValidationType = ValidationType.None;
- while (!vr.EOF)
- vr.Read ();
- }
-
- [Test]
- public void Bug81360 ()
- {
- string schemaFile = "Test/XmlFiles/xsd/81360.xsd";
- XmlTextReader treader = new XmlTextReader (schemaFile);
- XmlSchema sc = XmlSchema.Read (treader, null);
- sc.Compile (null);
- string xml = @"<body xmlns='" + sc.TargetNamespace + "'><div></div></body>";
- XmlTextReader reader = new XmlTextReader (new StringReader (xml));
- XmlValidatingReader validator = new XmlValidatingReader (reader);
- validator.Schemas.Add (sc);
- validator.ValidationType = ValidationType.Schema;
- while (!validator.EOF)
- validator.Read ();
- }
-
- [Test]
- public void Bug81460 ()
- {
- string xsd = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='foo'><xs:complexType><xs:attribute name='a' default='x' /></xs:complexType></xs:element></xs:schema>";
- string xml = "<foo/>";
- XmlReaderSettings s = new XmlReaderSettings ();
- s.ValidationType = ValidationType.Schema;
- s.Schemas.Add (XmlSchema.Read (new StringReader (xsd), null));
- XmlReader r = XmlReader.Create (new StringReader (xml), s);
- r.Read ();
- r.MoveToFirstAttribute (); // default attribute
- Assert.AreEqual (String.Empty, r.Prefix);
- }
-
- [Test]
- // annoyance
- [ExpectedException (typeof (XmlSchemaValidationException))]
- public void Bug82099 ()
- {
- string xsd = @"
-<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
- <xsd:element name='Customer' type='CustomerType' />
- <xsd:complexType name='CustomerType'>
- <xsd:attribute name='name' type='xsd:string' />
- </xsd:complexType>
-</xsd:schema>";
- XmlSchema schema = XmlSchema.Read (new StringReader (xsd), null);
-
- string xml = "<Customer name='Bob'> </Customer>";
-
- XmlReaderSettings settings = new XmlReaderSettings ();
- settings.Schemas.Add (schema);
- settings.ValidationType = ValidationType.Schema;
-
- XmlReader reader = XmlReader.Create (new StringReader (xml), settings);
-
- reader.Read ();
- reader.Read ();
- reader.Read ();
- }
-
- [Test]
- public void Bug82010 ()
- {
- string xmlfile = "Test/XmlFiles/xsd/82010.xml";
- string xsdfile = "Test/XmlFiles/xsd/82010.xsd";
- XmlTextReader xr = null, xr2 = null;
- try {
- xr = new XmlTextReader (xsdfile);
- xr2 = new XmlTextReader (xmlfile);
- XmlValidatingReader xvr = new XmlValidatingReader (xr2);
- xvr.Schemas.Add (XmlSchema.Read (xr, null));
- while (!xvr.EOF)
- xvr.Read ();
- } finally {
- if (xr2 != null)
- xr2.Close ();
- if (xr != null)
- xr.Close ();
- }
- }
-
- [Test]
- public void Bug376395 ()
- {
- string xmlfile = "Test/XmlFiles/xsd/376395.xml";
- string xsdfile = "Test/XmlFiles/xsd/376395.xsd";
- XmlTextReader xr = null, xr2 = null;
- try {
- xr = new XmlTextReader (xsdfile);
- xr2 = new XmlTextReader (xmlfile);
- XmlValidatingReader xvr = new XmlValidatingReader (xr2);
- xvr.Schemas.Add (XmlSchema.Read (xr, null));
- while (!xvr.EOF)
- xvr.Read ();
- } finally {
- if (xr2 != null)
- xr2.Close ();
- if (xr != null)
- xr.Close ();
- }
- }
-
- [Test]
- public void ValidateMixedInsideXsdAny ()
- {
- string xml = @"<root xmlns='urn:foo'>
- <X><Z>text</Z></X>
- <Y><X><Z>text</Z></X></Y>
-</root>";
- string xsd = @"
-<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
- targetNamespace='urn:foo' xmlns='urn:foo'>
- <xs:complexType name='root-type'>
- <xs:sequence><xs:element ref='X' /><xs:element ref='Y' /></xs:sequence>
- </xs:complexType>
- <xs:complexType name='X-type'>
- <xs:choice minOccurs='1' maxOccurs='unbounded'>
- <xs:any processContents='skip'/>
- </xs:choice>
- </xs:complexType>
- <xs:complexType name='Y-type'>
- <xs:sequence><xs:element ref='X' /></xs:sequence>
- </xs:complexType>
- <xs:element name='root' type='root-type' />
- <xs:element name='X' type='X-type' />
- <xs:element name='Y' type='Y-type' />
-</xs:schema>";
- XmlTextReader xtr = new XmlTextReader (new StringReader (xml));
- XmlValidatingReader xvr = new XmlValidatingReader (xtr);
- XmlReader xsr = new XmlTextReader (new StringReader (xsd));
- xvr.Schemas.Add (XmlSchema.Read (xsr, null));
- while (!xvr.EOF)
- xvr.Read ();
- xtr = new XmlTextReader (new StringReader (xml));
- xsr = new XmlTextReader (new StringReader (xsd));
- var s = new XmlReaderSettings ();
- s.Schemas.Add (XmlSchema.Read (xsr, null));
- s.ValidationType = ValidationType.Schema;
- XmlReader xvr2 = XmlReader.Create (xtr, s);
- while (!xvr2.EOF)
- xvr2.Read ();
- }
-
- [Test]
- public void WhitespaceAndElementOnly ()
- {
- string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
- <xs:element name='element_list'>
- <xs:complexType>
- <xs:sequence>
- <xs:element name='element' maxOccurs='unbounded' />
- </xs:sequence>
- </xs:complexType>
- </xs:element>
-</xs:schema>";
- string xml = @"<element_list>
- <!-- blah blah blah -->
- <element />
-
- <!-- blah blah -->
- <element />
-</element_list>";
- RunValidation (xml, xsd);
- }
-
- [Test]
- [ExpectedException (typeof (XmlSchemaValidationException))]
- public void EnumerationFacet ()
- {
- // bug #339934
- string xsd = @"<xs:schema id='schema' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
- <xs:simpleType name='ModeType'>
- <xs:restriction base='xs:string'>
- <xs:enumeration value='on' />
- <xs:enumeration value='off' />
- </xs:restriction>
- </xs:simpleType>
- <xs:element name='test'>
- <xs:complexType>
- <xs:sequence/>
- <xs:attribute name='mode' type='ModeType' use='required' />
- </xs:complexType>
- </xs:element>
-</xs:schema>";
- string xml = @"<test mode='out of scope'></test>";
-
- RunValidation (xml, xsd);
- }
-
- [Test]
- public void Bug501763 ()
- {
- string xsd1 = @"
- <xs:schema id='foo1'
- targetNamespace='foo1'
- elementFormDefault='qualified'
- xmlns='foo1'
- xmlns:xs='http://www.w3.org/2001/XMLSchema'
- xmlns:f2='foo2'>
-
- <xs:import namespace='foo2' />
- <xs:element name='Foo1Element' type='f2:Foo2ExtendedType'/>
- </xs:schema>";
-
- string xsd2 = @"
- <xs:schema id='foo2'
- targetNamespace='foo2'
- elementFormDefault='qualified'
- xmlns='foo2'
- xmlns:xs='http://www.w3.org/2001/XMLSchema' >
-
- <xs:element name='Foo2Element' type='Foo2Type' />
-
- <xs:complexType name='Foo2Type'>
- <xs:attribute name='foo2Attr' type='xs:string' use='required'/>
- </xs:complexType>
-
- <xs:complexType name='Foo2ExtendedType'>
- <xs:complexContent>
- <xs:extension base='Foo2Type'>
- <xs:attribute name='foo2ExtAttr' type='xs:string' use='required'/>
- </xs:extension>
- </xs:complexContent>
- </xs:complexType>
- </xs:schema>";
-
-
- XmlDocument doc = new XmlDocument ();
-
- XmlSchema schema1 = XmlSchema.Read (XmlReader.Create (new StringReader (xsd1)), null);
- XmlSchema schema2 = XmlSchema.Read (XmlReader.Create (new StringReader (xsd2)), null);
-
- doc.LoadXml (@"
- <Foo2Element
- foo2Attr='dummyvalue1'
- xmlns='foo2'
- />");
- doc.Schemas.Add (schema2);
- doc.Validate (null);
-
- doc = new XmlDocument();
- doc.LoadXml(@"
- <Foo1Element
- foo2Attr='dummyvalue1'
- foo2ExtAttr='dummyvalue2'
- xmlns='foo1'
- />");
- doc.Schemas.Add (schema2);
- doc.Schemas.Add (schema1);
- doc.Validate (null);
- }
-
- void RunValidation (string xml, string xsd)
- {
- XmlReaderSettings s = new XmlReaderSettings ();
- s.ValidationType = ValidationType.Schema;
- s.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
-
- XmlReader r = XmlReader.Create (new StringReader (xml), s);
- while (!r.EOF)
- r.Read ();
- }
- }
-}
+ [ExpectedException (typeof (XmlSchemaException))] + public void EnumerationFacetOnAttribute () + { + string xml = "<test mode='NOT AN ENUMERATION VALUE' />"; + XmlSchema schema = XmlSchema.Read (new XmlTextReader (TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/79650.xsd")), null); + XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null); + xvr.ValidationType = ValidationType.Schema; + xvr.Schemas.Add (schema); + while (!xvr.EOF) + xvr.Read (); + } + + class XmlErrorResolver : XmlResolver + { + public override ICredentials Credentials { + set { } + } + + public override object GetEntity (Uri uri, string role, Type type) + { + throw new Exception (); + } + } + + [Test] // bug #79924 + public void ValidationTypeNoneIgnoreSchemaLocations () + { + string xml = "<project xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='nosuchschema.xsd'/>"; + XmlValidatingReader vr = new XmlValidatingReader ( + new XmlTextReader (new StringReader (xml))); + vr.ValidationType = ValidationType.None; + vr.XmlResolver = new XmlErrorResolver (); + while (!vr.EOF) + vr.Read (); + } + + [Test] // bug #336625 + public void ValidationTypeNoneIgnoreLocatedSchemaErrors () + { + string xml = "<test xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='Test/XmlFiles/xsd/336625.xsd'/>"; + XmlValidatingReader vr = new XmlValidatingReader ( + new XmlTextReader (new StringReader (xml))); + vr.ValidationType = ValidationType.None; + while (!vr.EOF) + vr.Read (); + } + + [Test] + public void Bug81360 () + { + string schemaResource = "Test/XmlFiles/xsd/81360.xsd"; + XmlTextReader treader = new XmlTextReader (TestResourceHelper.GetFullPathOfResource (schemaResource)); + XmlSchema sc = XmlSchema.Read (treader, null); + sc.Compile (null); + string xml = @"<body xmlns='" + sc.TargetNamespace + "'><div></div></body>"; + XmlTextReader reader = new XmlTextReader (new StringReader (xml)); + XmlValidatingReader validator = new XmlValidatingReader (reader); + validator.Schemas.Add (sc); + validator.ValidationType = ValidationType.Schema; + while (!validator.EOF) + validator.Read (); + } + + [Test] + public void Bug81460 () + { + string xsd = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element name='foo'><xs:complexType><xs:attribute name='a' default='x' /></xs:complexType></xs:element></xs:schema>"; + string xml = "<foo/>"; + XmlReaderSettings s = new XmlReaderSettings (); + s.ValidationType = ValidationType.Schema; + s.Schemas.Add (XmlSchema.Read (new StringReader (xsd), null)); + XmlReader r = XmlReader.Create (new StringReader (xml), s); + r.Read (); + r.MoveToFirstAttribute (); // default attribute + Assert.AreEqual (String.Empty, r.Prefix); + } + + [Test] + // annoyance + [ExpectedException (typeof (XmlSchemaValidationException))] + public void Bug82099 () + { + string xsd = @" +<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> + <xsd:element name='Customer' type='CustomerType' /> + <xsd:complexType name='CustomerType'> + <xsd:attribute name='name' type='xsd:string' /> + </xsd:complexType> +</xsd:schema>"; + XmlSchema schema = XmlSchema.Read (new StringReader (xsd), null); + + string xml = "<Customer name='Bob'> </Customer>"; + + XmlReaderSettings settings = new XmlReaderSettings (); + settings.Schemas.Add (schema); + settings.ValidationType = ValidationType.Schema; + + XmlReader reader = XmlReader.Create (new StringReader (xml), settings); + + reader.Read (); + reader.Read (); + reader.Read (); + } + + [Test] + public void Bug82010 () + { + string xmlresource = "Test/XmlFiles/xsd/82010.xml"; + string xsdresource = "Test/XmlFiles/xsd/82010.xsd"; + XmlTextReader xr = null, xr2 = null; + try { + xr = new XmlTextReader (TestResourceHelper.GetFullPathOfResource (xsdresource)); + xr2 = new XmlTextReader (TestResourceHelper.GetFullPathOfResource (xmlresource)); + XmlValidatingReader xvr = new XmlValidatingReader (xr2); + xvr.Schemas.Add (XmlSchema.Read (xr, null)); + while (!xvr.EOF) + xvr.Read (); + } finally { + if (xr2 != null) + xr2.Close (); + if (xr != null) + xr.Close (); + } + } + + [Test] + public void Bug376395 () + { + string xmlresource = "Test/XmlFiles/xsd/376395.xml"; + string xsdresource = "Test/XmlFiles/xsd/376395.xsd"; + XmlTextReader xr = null, xr2 = null; + try { + xr = new XmlTextReader (TestResourceHelper.GetFullPathOfResource (xsdresource)); + xr2 = new XmlTextReader (TestResourceHelper.GetFullPathOfResource (xmlresource)); + XmlValidatingReader xvr = new XmlValidatingReader (xr2); + xvr.Schemas.Add (XmlSchema.Read (xr, null)); + while (!xvr.EOF) + xvr.Read (); + } finally { + if (xr2 != null) + xr2.Close (); + if (xr != null) + xr.Close (); + } + } + + [Test] + public void ValidateMixedInsideXsdAny () + { + string xml = @"<root xmlns='urn:foo'> + <X><Z>text</Z></X> + <Y><X><Z>text</Z></X></Y> +</root>"; + string xsd = @" +<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' + targetNamespace='urn:foo' xmlns='urn:foo'> + <xs:complexType name='root-type'> + <xs:sequence><xs:element ref='X' /><xs:element ref='Y' /></xs:sequence> + </xs:complexType> + <xs:complexType name='X-type'> + <xs:choice minOccurs='1' maxOccurs='unbounded'> + <xs:any processContents='skip'/> + </xs:choice> + </xs:complexType> + <xs:complexType name='Y-type'> + <xs:sequence><xs:element ref='X' /></xs:sequence> + </xs:complexType> + <xs:element name='root' type='root-type' /> + <xs:element name='X' type='X-type' /> + <xs:element name='Y' type='Y-type' /> +</xs:schema>"; + XmlTextReader xtr = new XmlTextReader (new StringReader (xml)); + XmlValidatingReader xvr = new XmlValidatingReader (xtr); + XmlReader xsr = new XmlTextReader (new StringReader (xsd)); + xvr.Schemas.Add (XmlSchema.Read (xsr, null)); + while (!xvr.EOF) + xvr.Read (); + xtr = new XmlTextReader (new StringReader (xml)); + xsr = new XmlTextReader (new StringReader (xsd)); + var s = new XmlReaderSettings (); + s.Schemas.Add (XmlSchema.Read (xsr, null)); + s.ValidationType = ValidationType.Schema; + XmlReader xvr2 = XmlReader.Create (xtr, s); + while (!xvr2.EOF) + xvr2.Read (); + } + + [Test] + public void WhitespaceAndElementOnly () + { + string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'> + <xs:element name='element_list'> + <xs:complexType> + <xs:sequence> + <xs:element name='element' maxOccurs='unbounded' /> + </xs:sequence> + </xs:complexType> + </xs:element> +</xs:schema>"; + string xml = @"<element_list> + <!-- blah blah blah --> + <element /> + + <!-- blah blah --> + <element /> +</element_list>"; + RunValidation (xml, xsd); + } + + [Test] + [ExpectedException (typeof (XmlSchemaValidationException))] + public void EnumerationFacet () + { + // bug #339934 + string xsd = @"<xs:schema id='schema' xmlns:xs='http://www.w3.org/2001/XMLSchema'> + <xs:simpleType name='ModeType'> + <xs:restriction base='xs:string'> + <xs:enumeration value='on' /> + <xs:enumeration value='off' /> + </xs:restriction> + </xs:simpleType> + <xs:element name='test'> + <xs:complexType> + <xs:sequence/> + <xs:attribute name='mode' type='ModeType' use='required' /> + </xs:complexType> + </xs:element> +</xs:schema>"; + string xml = @"<test mode='out of scope'></test>"; + + RunValidation (xml, xsd); + } + + [Test] + public void Bug501763 () + { + string xsd1 = @" + <xs:schema id='foo1' + targetNamespace='foo1' + elementFormDefault='qualified' + xmlns='foo1' + xmlns:xs='http://www.w3.org/2001/XMLSchema' + xmlns:f2='foo2'> + + <xs:import namespace='foo2' /> + <xs:element name='Foo1Element' type='f2:Foo2ExtendedType'/> + </xs:schema>"; + + string xsd2 = @" + <xs:schema id='foo2' + targetNamespace='foo2' + elementFormDefault='qualified' + xmlns='foo2' + xmlns:xs='http://www.w3.org/2001/XMLSchema' > + + <xs:element name='Foo2Element' type='Foo2Type' /> + + <xs:complexType name='Foo2Type'> + <xs:attribute name='foo2Attr' type='xs:string' use='required'/> + </xs:complexType> + + <xs:complexType name='Foo2ExtendedType'> + <xs:complexContent> + <xs:extension base='Foo2Type'> + <xs:attribute name='foo2ExtAttr' type='xs:string' use='required'/> + </xs:extension> + </xs:complexContent> + </xs:complexType> + </xs:schema>"; + + + XmlDocument doc = new XmlDocument (); + + XmlSchema schema1 = XmlSchema.Read (XmlReader.Create (new StringReader (xsd1)), null); + XmlSchema schema2 = XmlSchema.Read (XmlReader.Create (new StringReader (xsd2)), null); + + doc.LoadXml (@" + <Foo2Element + foo2Attr='dummyvalue1' + xmlns='foo2' + />"); + doc.Schemas.Add (schema2); + doc.Validate (null); + + doc = new XmlDocument(); + doc.LoadXml(@" + <Foo1Element + foo2Attr='dummyvalue1' + foo2ExtAttr='dummyvalue2' + xmlns='foo1' + />"); + doc.Schemas.Add (schema2); + doc.Schemas.Add (schema1); + doc.Validate (null); + } + + void RunValidation (string xml, string xsd) + { + XmlReaderSettings s = new XmlReaderSettings (); + s.ValidationType = ValidationType.Schema; + s.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null)); + + XmlReader r = XmlReader.Create (new StringReader (xml), s); + while (!r.EOF) + r.Read (); + } + } +} diff --git a/mcs/class/System.XML/Test/System.Xml/nist_dom/util.cs b/mcs/class/System.XML/Test/System.Xml/nist_dom/util.cs index ec03b3b79b8..f06293b8ab5 100644 --- a/mcs/class/System.XML/Test/System.Xml/nist_dom/util.cs +++ b/mcs/class/System.XML/Test/System.Xml/nist_dom/util.cs @@ -14,6 +14,8 @@ using System; using System.Xml; +using MonoTests.Helpers; + namespace nist_dom { public class XmlNodeArrayList : XmlNodeList @@ -117,12 +119,12 @@ namespace nist_dom try { //System.Console.WriteLine(System.IO.Directory.GetCurrentDirectory()); - masterDoc.Load("Test/System.Xml/nist_dom/files/staff.xml"); - originalDoc.Load("Test/System.Xml/nist_dom/files/staff.xml"); - masterXML.Load("Test/System.Xml/nist_dom/files/staff.html"); - otherDoc.Load("Test/System.Xml/nist_dom/files/otherDoc.xml"); - HTMLDoc.Load("Test/System.Xml/nist_dom/files/staff.html"); - noDTDXMLObject.Load("Test/System.Xml/nist_dom/files/noDTDXMLfile.xml"); + masterDoc.Load(TestResourceHelper.GetFullPathOfResource ("Test/System.Xml/nist_dom/files/staff.xml")); + originalDoc.Load(TestResourceHelper.GetFullPathOfResource ("Test/System.Xml/nist_dom/files/staff.xml")); + masterXML.Load(TestResourceHelper.GetFullPathOfResource ("Test/System.Xml/nist_dom/files/staff.html")); + otherDoc.Load(TestResourceHelper.GetFullPathOfResource ("Test/System.Xml/nist_dom/files/otherDoc.xml")); + HTMLDoc.Load(TestResourceHelper.GetFullPathOfResource ("Test/System.Xml/nist_dom/files/staff.html")); + noDTDXMLObject.Load(TestResourceHelper.GetFullPathOfResource ("Test/System.Xml/nist_dom/files/noDTDXMLfile.xml")); } catch (System.Exception ex) { |