Merge pull request #1349 from martinjt/MachineKeyProtect
[mono.git] / mcs / class / System.Xml.Linq / Test / System.Xml.Linq / XNodeWriterTest.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.IO;
29 using System.Text;
30 using System.Xml;
31 using System.Xml.Linq;
32 using System.Xml.Serialization;
33
34 using NUnit.Framework;
35
36 using XPI = System.Xml.Linq.XProcessingInstruction;
37
38 namespace MonoTests.System.Xml.Linq
39 {
40         [TestFixture]
41         public class XNodeWriterTest
42         {
43                 [Test]
44                 public void WriteEmptyElements ()
45                 {
46                         var doc = new XDocument ();
47                         XmlWriter w = doc.CreateWriter ();
48                         w.WriteStartElement ("root");
49                         w.WriteStartElement ("foo");
50                         w.WriteEndElement ();
51                         w.WriteStartElement ("bar");
52                         w.WriteFullEndElement ();
53                         w.WriteEndElement ();
54                         w.Close ();
55                         Assert.IsTrue (((XElement) doc.Root.FirstNode).IsEmpty, "#1");
56                         Assert.IsFalse (((XElement) doc.Root.LastNode).IsEmpty, "#2");
57                 }
58
59                 [Test]
60                 public void CreateWriter1 ()
61                 {
62                         string xml = "<root><foo/><bar></bar><baz a='v' xmlns='urn:foo' xmlns:x='urn:x'><x:ext xmlns=''>test</x:ext><!-- comment -->  <?some-pi some-data?></baz></root>";
63                         XDocument doc = new XDocument ();
64                         XmlWriter xw = doc.CreateWriter ();
65                         XmlReader xr = XmlReader.Create (new StringReader (xml));
66                         while (!xr.EOF)
67                                 xw.WriteNode (xr, false);
68                         xw.Close ();
69
70                         Assert.AreEqual ("root", doc.Root.Name.LocalName, "#1");
71                         XElement el = doc.Root.FirstNode as XElement;
72                         Assert.AreEqual ("foo", el.Name.LocalName, "#2-1");
73                         Assert.IsTrue (el.IsEmpty, "#2-2");
74                         Assert.IsFalse (el.HasAttributes, "#2-3");
75                         el = el.NextNode as XElement;
76                         Assert.IsFalse (el.IsEmpty, "#3");
77                         el = el.NextNode as XElement;
78                         Assert.AreEqual ("a", el.FirstAttribute.Name.LocalName, "#4-1");
79                         Assert.AreEqual ("xmlns", el.FirstAttribute.NextAttribute.Name.LocalName, "#4-2");
80                         Assert.AreEqual ("x", el.LastAttribute.Name.LocalName, "#4-3");
81                         Assert.AreEqual (XNamespace.Xmlns, el.LastAttribute.Name.Namespace, "#4-4");
82                         el = el.FirstNode as XElement;
83                         // <x:ext
84                         Assert.AreEqual ("ext", el.Name.LocalName, "#5-1");
85                         Assert.AreEqual (XNamespace.Get ("urn:x"), el.Name.Namespace, "#5-2");
86                         // xmlns=''
87                         Assert.AreEqual ("xmlns", el.FirstAttribute.Name.LocalName, "#5-3");
88                         Assert.AreEqual (XNamespace.Get (String.Empty), el.FirstAttribute.Name.Namespace, "#5-4");
89                         XText t = el.FirstNode as XText;
90                         Assert.AreEqual ("test", t.Value, "#6");
91                         XComment c = el.NextNode as XComment;
92                         Assert.AreEqual (" comment ", c.Value, "#7");
93                         t = c.NextNode as XText;
94                         Assert.AreEqual ("  ", t.Value, "#8");
95                         XPI pi = t.NextNode as XPI;
96                         Assert.AreEqual ("some-pi", pi.Target, "#9-1");
97                         Assert.AreEqual ("some-data", pi.Data, "#9-2");
98                         Assert.IsNull (el.Parent.NextNode, "#10");
99                         Assert.IsNull (el.Parent.Parent.NextNode, "#11");
100                 }
101
102                 [XmlRoot ("MyRoot", Namespace = "urn:mynamespace")]
103                 public class MyData
104                 {
105                         [XmlElement]
106                         public MyElementBase[] Elements;
107                 }
108
109                 [XmlInclude (typeof(MyElement1))]
110                 public abstract class MyElementBase
111                 {
112                 }
113
114                 [XmlType ("MyElement1", Namespace = "urn:mynamespace")]
115                 public class MyElement1 : MyElementBase
116                 {
117                         [XmlAttribute]
118                         public int V;
119                 }
120
121                 [Test]
122                 public void Bug24300 ()
123                 {
124                         XmlSerializer serializer = new XmlSerializer (typeof (MyData));
125                         MyData data = new MyData () { Elements = new MyElementBase [] { new MyElement1 { V = 2 } } };
126
127                         XDocument doc = new XDocument ();
128                         using (var writer = doc.CreateWriter ())
129                         {
130                             serializer.Serialize (writer, data);
131                         }
132
133                         var sb = new StringBuilder ();
134                         sb.Append ("<MyRoot xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"urn:mynamespace\">\r\n");
135                         sb.Append ("  <Elements xsi:type=\"MyElement1\" V=\"2\" />\r\n");
136                         sb.Append ("</MyRoot>");
137
138                         Assert.AreEqual (sb.ToString (), doc.Root.ToString ());
139                 }
140         }
141 }