Merge pull request #1222 from LogosBible/uri-trycreate
[mono.git] / mcs / class / System.XML / Test / System.Xml / XmlReaderCommonTests.cs
index 1f6ee9bb39a8fc5c061b77b491600d4c0cd7290b..16c46b496f4ce7a996bdbf568db76d706bd7e52a 100644 (file)
@@ -15,6 +15,10 @@ using System.Text;
 using System.Xml;\r
 using System.Xml.Schema;\r
 using System.Xml.XPath;\r
+#if NET_4_5\r
+using System.Threading;\r
+using System.Threading.Tasks;\r
+#endif\r
 \r
 using NUnit.Framework;\r
 \r
@@ -197,14 +201,12 @@ namespace MonoTests.System.Xml
                        document.LoadXml (xml);\r
                        xnr = new XmlNodeReader (document);\r
                        method (xnr);\r
-#if NET_2_0\r
 /*\r
                        // XPathNavigatorReader tests\r
                        System.Xml.XPath.XPathDocument doc = new System.Xml.XPath.XPathDocument (new StringReader (xml));\r
                        XmlReader xpr = doc.CreateNavigator ().ReadSubtree ();\r
                        method (xpr);\r
 */\r
-#endif\r
                }\r
 \r
 \r
@@ -1344,6 +1346,7 @@ namespace MonoTests.System.Xml
 \r
                [Test]\r
                [Category ("NotDotNet")]\r
+               [Ignore ("Bug in Microsoft referencesource")]\r
                public void IndexerAndAttributes ()\r
                {\r
                        string xml = @"<?xml version='1.0' standalone='no'?><foo _1='1' _2='2' _3='3' />";\r
@@ -1509,7 +1512,6 @@ namespace MonoTests.System.Xml
                        reader.Read (); // silently returns false\r
                }\r
 \r
-#if NET_2_0\r
                [Test]\r
                public void CreateSimple ()\r
                {\r
@@ -1536,7 +1538,7 @@ namespace MonoTests.System.Xml
                // a bit revised version of bug #78706\r
                public void CreateFromUrlClose ()\r
                {\r
-                       string file = "Test/XmlFiles/78706.xml";\r
+                       string file = Path.Combine (Path.GetTempPath (), "78706.xml");\r
                        try {\r
                                if (!File.Exists (file))\r
                                        File.Create (file).Close ();\r
@@ -1556,7 +1558,7 @@ namespace MonoTests.System.Xml
                // a bit revised version of bug #385638\r
                public void CreateFromUrlClose2 ()\r
                {\r
-                       string file = "Test/XmlFiles/385638.xml";\r
+                       string file = Path.Combine (Path.GetTempPath (), "385638.xml");\r
                        try {\r
                                if (File.Exists (file))\r
                                        File.Delete (file);\r
@@ -1723,6 +1725,48 @@ namespace MonoTests.System.Xml
                                reader.ReadToNextSibling ("book"); // should not result in an infinite loop\r
                }\r
 \r
+               // bug #676020\r
+               [Test]\r
+               public void ReadToNextSibling4 ()\r
+               {\r
+                       string xml = @"<SerializableStringDictionary>\r
+<SerializableStringDictionary>\r
+<DictionaryEntry Key=""Key1"" Value=""Value1""/>\r
+<DictionaryEntry Key=""Key2"" Value=""Value2""/>\r
+<DictionaryEntry Key=""Key3"" Value=""Value3""/>\r
+</SerializableStringDictionary>\r
+</SerializableStringDictionary>";\r
+\r
+                       var reader = XmlReader.Create (new StringReader (xml));\r
+\r
+                       Assert.IsTrue (reader.ReadToDescendant ("SerializableStringDictionary"), "#1");\r
+                       Assert.IsTrue (reader.ReadToDescendant ("DictionaryEntry"), "#2");\r
+\r
+                       int count = 0;\r
+                       do {\r
+                               reader.MoveToAttribute ("Key");\r
+                               var key = reader.ReadContentAsString ();\r
+                               reader.MoveToAttribute ("Value");\r
+                               var value = reader.ReadContentAsString ();\r
+                               count++;\r
+                       }\r
+                       while (reader.ReadToNextSibling ("DictionaryEntry"));\r
+                       Assert.AreEqual (3, count, "#3");\r
+               }\r
+\r
+               [Test, Category("NotWorking")]\r
+               public void ReadToNextSiblingInInitialReadState ()\r
+               {\r
+                       var xml = "<Text name=\"hello\"><Something></Something></Text>";\r
+                       var ms = new MemoryStream(Encoding.Default.GetBytes(xml));\r
+                       var xtr = XmlReader.Create(ms);\r
+\r
+                       Assert.AreEqual(xtr.ReadState, ReadState.Initial);\r
+                       xtr.ReadToNextSibling("Text");\r
+\r
+                       Assert.AreEqual("hello", xtr.GetAttribute("name"));\r
+               }\r
+\r
                [Test]\r
                public void ReadSubtree ()\r
                {\r
@@ -2201,7 +2245,7 @@ namespace MonoTests.System.Xml
                        Assert.AreEqual (XmlNodeType.Text, reader.NodeType, "#2");\r
                        bytesRead = reader.ReadElementContentAsBase64 (fixedSizeBuffer, 0, fixedSizeBuffer.Length);\r
                        Assert.AreEqual (0, bytesRead, "#3");\r
-                       Assert.AreEqual (XmlNodeType.EndElement, reader.NodeType, "#4");\r
+                       Assert.AreEqual (XmlNodeType.None, reader.NodeType, "#4");\r
                }\r
 \r
                [Test]\r
@@ -2218,6 +2262,91 @@ namespace MonoTests.System.Xml
                        var q = (XmlQualifiedName) xr.ReadElementContentAs (typeof (XmlQualifiedName), xr as IXmlNamespaceResolver);\r
                        Assert.AreEqual ("urn:foo", q.Namespace, "#1");\r
                }\r
+\r
+               [Test]\r
+               public void ReadElementContentAsArray ()\r
+               {\r
+                       var sw = new StringWriter ();\r
+                       var xw = XmlWriter.Create (sw);\r
+                       xw.WriteStartElement ("root");\r
+                       xw.WriteAttributeString ("xmlns", "b", "http://www.w3.org/2000/xmlns/", "urn:bar");\r
+                       var arr = new XmlQualifiedName [] { new XmlQualifiedName ("foo"), new XmlQualifiedName ("bar", "urn:bar") };\r
+                       xw.WriteValue (arr);\r
+                       xw.Close ();\r
+                       var xr = XmlReader.Create (new StringReader (sw.ToString ()));\r
+                       xr.MoveToContent ();\r
+                       var ret = xr.ReadElementContentAs (typeof (XmlQualifiedName []), null) as XmlQualifiedName [];\r
+                       Assert.IsNotNull (ret, "#1");\r
+                       Assert.AreEqual (arr [0], ret [0], "#2");\r
+                       Assert.AreEqual (arr [1], ret [1], "#3");\r
+               }\r
+\r
+               [Test]\r
+               public void ReadContentAs ()\r
+               {\r
+                       var xr = XmlReader.Create (new StringReader ("<doc a=' 1 '/>"));\r
+                       xr.Read ();\r
+                       xr.MoveToAttribute ("a");\r
+\r
+                       Assert.AreEqual ((Byte) 1, xr.ReadContentAs (typeof (Byte), null), "#1");\r
+                       Assert.AreEqual ((SByte) 1, xr.ReadContentAs (typeof (SByte), null), "#2");\r
+                       Assert.AreEqual ((Int16) 1, xr.ReadContentAs (typeof (Int16), null), "#3");\r
+                       Assert.AreEqual ((UInt16) 1, xr.ReadContentAs (typeof (UInt16), null), "#4");\r
+                       Assert.AreEqual ((Int32) 1, xr.ReadContentAs (typeof (Int32), null), "#5");\r
+                       Assert.AreEqual ((UInt32) 1, xr.ReadContentAs (typeof (UInt32), null), "#6");\r
+                       Assert.AreEqual ((Int64) 1, xr.ReadContentAs (typeof (Int64), null), "#7");\r
+                       Assert.AreEqual ((UInt64) 1, xr.ReadContentAs (typeof (UInt64), null), "#8");\r
+               }\r
+\r
+#if NET_4_5\r
+               [Test]\r
+               [ExpectedException(typeof(InvalidOperationException))]\r
+               public void MustSetAsyncFlag ()\r
+               {\r
+                       var r = XmlReader.Create (new StringReader ("<root/>"));\r
+                       r.ReadAsync ();\r
+               }\r
+\r
+               Exception RunAsync (Action action)\r
+               {\r
+                       var task = Task<Exception>.Run (async () => {\r
+                               try {\r
+                                       action ();\r
+                                       return null;\r
+                               } catch (Exception ex) {\r
+                                       return ex;\r
+                               }\r
+                       });\r
+                       task.Wait ();\r
+                       Assert.That (task.IsCompleted);\r
+                       return task.Result;\r
+               }\r
+\r
+               [Test]\r
+               public void SimpleAsync ()\r
+               {\r
+                       var xml = "<root test=\"monkey\"/>";\r
+                       var task = Task<Exception>.Run (async () => {\r
+                               try {\r
+                                       var s = new XmlReaderSettings ();\r
+                                       s.Async = true;\r
+                                       var r = XmlReader.Create (new StringReader (xml), s);\r
+\r
+                                       Assert.That (await r.ReadAsync ());\r
+                                       Assert.That (r.MoveToFirstAttribute ());\r
+\r
+                                       Assert.AreEqual (await r.GetValueAsync (), "monkey");\r
+                                       r.Close ();\r
+                                       return null;\r
+                               } catch (Exception ex) {\r
+                                       return ex;\r
+                               }\r
+                       });\r
+                       task.Wait ();\r
+                       Assert.That (task.IsCompleted);\r
+                       if (task.Result != null)\r
+                               throw task.Result;\r
+               }\r
 #endif\r
        }\r
 }\r