2009-06-26 Robert Jordan <robertj@gmx.net>
[mono.git] / mcs / class / System.XML / Test / System.Xml / XmlReaderSettingsTests.cs
index 5c7381d4190538b07fcde50316dc6fd56a392a89..33115994bca9945394b8678661c7a8c37d65c1cb 100644 (file)
@@ -10,6 +10,7 @@
 #if NET_2_0
 using System;
 using System.IO;
+using System.Net;
 using System.Text;
 using System.Xml;
 using System.Xml.Schema;
@@ -21,7 +22,7 @@ using AssertType = NUnit.Framework.Assert;
 namespace MonoTests.System.Xml
 {
        [TestFixture]
-       public class XmlReaderSettingsTests : Assertion
+       public class XmlReaderSettingsTests
        {
                public Stream CreateStream (string xml)
                {
@@ -32,29 +33,28 @@ namespace MonoTests.System.Xml
                public void DefaultValue ()
                {
                        XmlReaderSettings s = new XmlReaderSettings ();
-                       AssertEquals ("CheckCharacters", true, s.CheckCharacters);
-                       AssertEquals ("ConformanceLevel", ConformanceLevel.Document,
-                               s.ConformanceLevel);
-                       AssertEquals ("ValidationType", ValidationType.None, s.ValidationType);
-                       AssertEquals ("IgnoreComments", false, s.IgnoreComments);
-                       Assert ("ProcessInlineSchema", 0 == (s.ValidationFlags &
-                               ValidationFlags.ProcessInlineSchema));
-                       AssertEquals ("IgnorePI", false, s.IgnoreProcessingInstructions);
-                       Assert ("ProcessSchemaLocation", 0 == (s.ValidationFlags &
-                               ValidationFlags.ProcessSchemaLocation));
-                       Assert ("ReportValidationWarnings", 0 == (s.ValidationFlags &
-                               ValidationFlags.ReportValidationWarnings));
-                       Assert ("ProcessIdentityConstraints", 0 != (s.ValidationFlags &
-                               ValidationFlags.ProcessIdentityConstraints));
+                       Assert.AreEqual (true, s.CheckCharacters, "CheckCharacters");
+                       Assert.AreEqual (ConformanceLevel.Document, s.ConformanceLevel, "ConformanceLevel");
+                       Assert.AreEqual (ValidationType.None, s.ValidationType, "ValidationType");
+                       Assert.AreEqual (false, s.IgnoreComments, "IgnoreComments");
+                       Assert.IsTrue (0 == (s.ValidationFlags &
+                               ValidationFlags.ProcessInlineSchema), "ProcessInlineSchema");
+                       Assert.AreEqual (false, s.IgnoreProcessingInstructions, "IgnorePI");
+                       Assert.IsTrue (0 == (s.ValidationFlags &
+                               ValidationFlags.ProcessSchemaLocation), "ProcessSchemaLocation");
+                       Assert.IsTrue (0 == (s.ValidationFlags &
+                               ValidationFlags.ReportValidationWarnings), "ReportValidationWarnings");
+                       Assert.IsTrue (0 != (s.ValidationFlags &
+                               ValidationFlags.ProcessIdentityConstraints), "ProcessIdentityConstraints");
                        // No one should use this flag BTW if someone wants
                        // code to be conformant to W3C XML Schema standard.
-                       Assert ("AllowXmlAttributes", 0 != (s.ValidationFlags &
-                               ValidationFlags.AllowXmlAttributes));
-                       AssertEquals ("IgnoreWhitespace", false, s.IgnoreWhitespace);
-                       AssertEquals ("LineNumberOffset", 0, s.LineNumberOffset);
-                       AssertEquals ("LinePositionOffset", 0, s.LinePositionOffset);
-                       AssertNull ("NameTable", s.NameTable);
-                       AssertEquals ("Schemas.Count", 0, s.Schemas.Count);
+                       Assert.IsTrue (0 != (s.ValidationFlags &
+                               ValidationFlags.AllowXmlAttributes), "AllowXmlAttributes");
+                       Assert.AreEqual (false, s.IgnoreWhitespace, "IgnoreWhitespace");
+                       Assert.AreEqual (0, s.LineNumberOffset, "LineNumberOffset");
+                       Assert.AreEqual (0, s.LinePositionOffset, "LinePositionOffset");
+                       Assert.IsNull (s.NameTable, "NameTable");
+                       Assert.AreEqual (0, s.Schemas.Count, "Schemas.Count");
                }
 
                [Test]
@@ -93,10 +93,10 @@ namespace MonoTests.System.Xml
                                sr, settings);
                        xtr.Read ();
                        xtr.MoveToFirstAttribute ();
-                       AssertEquals ("   value   ", xtr.Value);
+                       Assert.AreEqual ("   value   ", xtr.Value);
                        xtr.Read ();
                        // Text string is normalized
-                       AssertEquals ("test\nstring", xtr.Value);
+                       Assert.AreEqual ("test\nstring", xtr.Value);
                }
 
                [Test]
@@ -115,9 +115,9 @@ namespace MonoTests.System.Xml
                        settings.CheckCharacters = false;
                        xtr.Read ();
                        xtr.MoveToFirstAttribute ();
-                       AssertEquals ("\0", xtr.Value);
+                       Assert.AreEqual ("\0", xtr.Value);
                        xtr.Read ();
-                       AssertEquals ("\0", xtr.Value);
+                       Assert.AreEqual ("\0", xtr.Value);
                }
 
                // Hmm, does it really make sense? :-/
@@ -142,16 +142,16 @@ namespace MonoTests.System.Xml
                        // But it won't work against XmlNodeReader.
                        xr.Read ();
                        xr.MoveToFirstAttribute ();
-                       AssertEquals ("\0", xr.Value);
+                       Assert.AreEqual ("\0", xr.Value);
                        xr.Read ();
-                       AssertEquals ("\0", xr.Value);
+                       Assert.AreEqual ("\0", xr.Value);
                }
 
                [Test]
                public void CreateAndSettings ()
                {
-                       AssertNotNull (XmlReader.Create (CreateStream ("<xml/>")).Settings);
-                       AssertNotNull (XmlReader.Create ("Test/XmlFiles/simple.xml").Settings);
+                       Assert.IsNotNull (XmlReader.Create (CreateStream ("<xml/>")).Settings);
+                       Assert.IsNotNull (XmlReader.Create ("Test/XmlFiles/simple.xml").Settings);
                }
 
                [Test]
@@ -306,6 +306,119 @@ namespace MonoTests.System.Xml
                        // but don't reject because of that fact.
                        XmlReader r = XmlReader.Create (dr, settings);
                }
+
+               [Test]
+               public void NullResolver ()
+               {
+                       XmlReaderSettings settings = new XmlReaderSettings ();
+                       settings.XmlResolver = null;
+                       using (XmlReader xr = XmlReader.Create ("Test/XmlFiles/simple.xml", settings)) {
+                               while (!xr.EOF)
+                                       xr.Read ();
+                       }
+               }
+
+               class ThrowExceptionResolver : XmlResolver
+               {
+                       public override ICredentials Credentials {
+                               set { }
+                       }
+
+                       public override object GetEntity (Uri uri, string type, Type expected)
+                       {
+                               throw new ApplicationException ("error");
+                       }
+               }
+
+               [Test]
+               [ExpectedException (typeof (ApplicationException))]
+               public void CustomResolverUsedForXmlStream ()
+               {
+                       XmlReaderSettings settings = new XmlReaderSettings ();
+                       settings.XmlResolver = new ThrowExceptionResolver ();
+                       using (XmlReader xr = XmlReader.Create ("Test/XmlFiles/simple.xml", settings)) {
+                               while (!xr.EOF)
+                                       xr.Read ();
+                       }
+               }
+
+               [Test]
+               [ExpectedException (typeof (ApplicationException))]
+               public void ValidationEventHandler ()
+               {
+                       XmlReaderSettings settings = new XmlReaderSettings ();
+                       settings.Schemas.Add (new XmlSchema ());
+                       settings.ValidationType = ValidationType.Schema;
+                       settings.ValidationEventHandler += delegate (object o, ValidationEventArgs e) {
+                               throw new ApplicationException ();
+                       };
+                       XmlReader r = XmlReader.Create (
+                               new StringReader ("<root/>"), settings);
+                       while (!r.EOF)
+                               r.Read ();
+               }
+
+               [Test]
+               [ExpectedException (typeof (XmlSchemaValidationException))]
+               // make sure that Create(string,XmlReaderSettings) returns
+               // validating XmlReader.
+               public void CreateFromUrlWithValidation ()
+               {
+                       XmlReaderSettings settings = new XmlReaderSettings();
+                       XmlSchema xs = new XmlSchema ();
+                       settings.Schemas.Add (xs);
+                       settings.ValidationType = ValidationType.Schema;
+                       using (XmlReader r = XmlReader.Create ("Test/XmlFiles/simple.xml", settings)) {
+                               r.Read ();
+                       }
+               }
+
+               [Test]
+               public void ResolveEntities () // bug #81000
+               {
+                       XmlReaderSettings s = new XmlReaderSettings ();
+                       s.ProhibitDtd = false;
+                       s.XmlResolver = new XmlResolver81000 ();
+
+                       string xml = "<!DOCTYPE root SYSTEM \"foo.dtd\"><root>&alpha;</root>";
+                       XmlReader r = XmlReader.Create (new StringReader (xml), s);
+                       r.Read ();
+                       r.Read ();
+                       r.Read ();
+                       // not EntityReference but Text
+                       Assert.AreEqual (XmlNodeType.Text, r.NodeType, "#1");
+                       r.Read ();
+                       Assert.AreEqual (XmlNodeType.EndElement, r.NodeType, "#2");
+               }
+
+               public class XmlResolver81000 : XmlResolver
+               {
+                       public override ICredentials Credentials { set {} }
+
+                       public override object GetEntity (Uri uri, string role, Type type)
+                       {
+                               return new MemoryStream (Encoding.UTF8.GetBytes ("<!ENTITY alpha \"bravo\">"));
+                       }
+               }
+
+               [Test]
+               public void IgnoreComments () // Bug #82062.
+               {
+                       string xml = "<root><!-- ignore --></root>";
+                       XmlReaderSettings s = new XmlReaderSettings ();
+                       s.IgnoreComments = true;
+                       XmlReader r = XmlReader.Create (new StringReader (xml), s);
+                       r.Read ();
+                       r.Read ();
+                       Assert.AreEqual (String.Empty, r.Value); // should not be at the comment node.
+               }
+
+               [Test]
+               public void CreateSetsBaseUri () // bug #392385
+               {
+                       XmlReader r = XmlReader.Create (new StringReader ("<x/>"), new XmlReaderSettings (), "urn:foo");
+                       Assert.AreEqual ("urn:foo", r.BaseURI);
+               }
        }
 }
 #endif