[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / System.Xml.Linq / Test / System.Xml.Linq / XDocumentTest.cs
1 //
2 // Authors:
3 //   Atsushi Enomoto
4 //
5 // Copyright 2007 Novell (http://www.novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26
27 using System;
28 using System.Collections.Generic;
29 using System.IO;
30 using System.Text;
31 using System.Xml;
32 using System.Xml.Linq;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Xml.Linq
37 {
38         [TestFixture]
39         public class XDocumentTest
40         {
41                 [Test]
42                 public void Load1 ()
43                 {
44                         string xml = "<?xml version='1.0'?><root />";
45
46                         XDocument doc = XDocument.Load (new StringReader (xml));
47                         Assert.IsTrue (doc.FirstNode is XElement, "#1");
48                         Assert.IsTrue (doc.LastNode is XElement, "#2");
49                         Assert.IsNull (doc.NextNode, "#3");
50                         Assert.IsNull (doc.PreviousNode, "#4");
51                         Assert.AreEqual (1, new List<XNode> (doc.Nodes ()).Count, "#5");
52                         Assert.IsNull (doc.FirstNode.Parent, "#6");
53                         Assert.AreEqual (doc.FirstNode, doc.LastNode, "#7");
54                         Assert.AreEqual (XmlNodeType.Document, doc.NodeType, "#8");
55                         Assert.AreEqual (doc.FirstNode, doc.Root, "#7");
56                 }
57
58                 [Test]
59                 public void Load2 ()
60                 {
61                         // https://bugzilla.novell.com/show_bug.cgi?id=496285
62                         byte [] bytes = Encoding.UTF8.GetBytes ("<root/>");
63                         var reader = new XmlTextReader (new MemoryStream (bytes));
64                         var doc = XDocument.Load (reader);
65                 }
66
67                 [Test]
68                 [ExpectedException (typeof (ArgumentException))]
69                 public void LoadInvalid ()
70                 {
71                         string xml = "text";
72                         XmlReaderSettings s = new XmlReaderSettings ();
73                         s.ConformanceLevel = ConformanceLevel.Fragment;
74
75                         XDocument.Load (XmlReader.Create (new StringReader (xml), s));
76                 }
77
78                 [Test]
79                 [ExpectedException (typeof (InvalidOperationException))]
80                 public void LoadWhitespaces ()
81                 {
82                         string xml = "   ";
83                         XmlReaderSettings s = new XmlReaderSettings ();
84                         s.ConformanceLevel = ConformanceLevel.Fragment;
85
86                         XDocument.Load (XmlReader.Create (new StringReader (xml), s));
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentException))]
91                 public void AddTextToDocument ()
92                 {
93                         XDocument doc = new XDocument ();
94                         doc.Add ("test");
95                 }
96
97                 [Test]
98                 [Category ("NotDotNet")]
99                 //[ExpectedException (typeof (ArgumentException))]
100                 public void AddXDeclarationToDocument ()
101                 {
102                         XDocument doc = new XDocument ();
103                         // LAMESPEC: XDeclaration is treated as a general object
104                         // and hence converted to a string -> error
105                         doc.Add (new XDeclaration ("1.0", null, null));
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (ArgumentException))]
110                 public void AddXAttributeToDocument ()
111                 {
112                         var doc = new XDocument ();
113                         doc.Add (new XAttribute ("foo", " "));
114                 }
115                 
116                 [Test] // bug #4850
117                 public void AddXmlDeclarationEvenForDecllessDoc ()
118                 {
119                         var doc = new XDocument (
120                                 new XElement ("resources",
121                                         new XElement ("string",
122                                                 new XAttribute ("name", "whatever"),
123                                                 "This is sparta")));
124                         var sw = new StringWriter ();
125                         using (var writer = new XmlTextWriter (sw))
126                                 doc.WriteTo (writer);
127                         Assert.IsTrue (sw.ToString ().StartsWith ("<?xml"), "#1");
128                 }
129         }
130 }