2005-12-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Test / System.Xml.XPath / XPathEditableNavigatorTests.cs
1 //
2 // XPathEditableNavigatorTests.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9
10 #if NET_2_0
11
12 using System;
13 using System.Xml;
14 using System.Xml.XPath;
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Xml.XPath
18 {
19         [TestFixture]
20         public class XPathEditableNavigatorTests
21         {
22                 private XPathNavigator GetInstance (string xml)
23                 {
24                         XmlDocument doc = new XmlDocument ();
25                         doc.LoadXml (xml);
26                         return doc.CreateNavigator ();
27                 }
28
29                 private static void AssertNavigator (string label, XPathNavigator nav, XPathNodeType type, string prefix, string localName, string ns, string name, string value, bool hasAttributes, bool hasChildren, bool isEmptyElement)
30                 {
31                         label += nav.GetType ();
32                         Assert.AreEqual (type, nav.NodeType, label + "NodeType");
33                         Assert.AreEqual (prefix, nav.Prefix, label + "Prefix");
34                         Assert.AreEqual (localName, nav.LocalName, label + "LocalName");
35                         Assert.AreEqual (ns, nav.NamespaceURI, label + "Namespace");
36                         Assert.AreEqual (name, nav.Name, label + "Name");
37                         Assert.AreEqual (value, nav.Value, label + "Value");
38                         Assert.AreEqual (hasAttributes, nav.HasAttributes, label + "HasAttributes");
39                         Assert.AreEqual (hasChildren, nav.HasChildren, label + "HasChildren");
40                         Assert.AreEqual (isEmptyElement, nav.IsEmptyElement, label + "IsEmptyElement");
41                 }
42
43                 [Test]
44                 [ExpectedException (typeof (XmlException))]
45                 public void AppendChildStartDocumentInvalid ()
46                 {
47                         XPathNavigator nav = GetInstance (String.Empty);
48                         XmlWriter w = nav.AppendChild ();
49                         w.WriteStartDocument ();
50                         w.Close ();
51                 }
52
53                 [Test]
54                 [ExpectedException (typeof (XmlException))]
55                 public void AppendChildStartAttributeInvalid ()
56                 {
57                         XPathNavigator nav = GetInstance (String.Empty);
58                         XmlWriter w = nav.AppendChild ();
59                         // Seems like it is just ignored.
60                         w.WriteStartAttribute ("test");
61                         w.WriteEndAttribute ();
62                         w.Close ();
63                         Assert.AreEqual (XPathNodeType.Root, nav.NodeType, "#1");
64                         Assert.IsFalse (nav.MoveToFirstChild (), "#2");
65                 }
66
67                 [Test]
68                 [ExpectedException (typeof (XmlException))]
69                 public void AppendChildElementIncomplete ()
70                 {
71                         XPathNavigator nav = GetInstance (String.Empty);
72                         XmlWriter w = nav.AppendChild ();
73                         w.WriteStartElement ("foo");
74                         w.Close ();
75                 }
76
77                 [Test]
78                 public void AppendChildElement ()
79                 {
80                         XPathNavigator nav = GetInstance ("<root/>");
81                         nav.MoveToFirstChild ();
82                         XmlWriter w = nav.AppendChild ();
83                         w.WriteStartElement ("foo");
84                         w.WriteEndElement ();
85                         w.Close ();
86                         Assert.IsTrue (nav.MoveToFirstChild ());
87                         AssertNavigator ("#1", nav,
88                                 XPathNodeType.Element,
89                                 String.Empty,   // Prefix
90                                 "foo",          // LocalName
91                                 String.Empty,   // NamespaceURI
92                                 "foo",          // Name
93                                 String.Empty,   // Value
94                                 false,          // HasAttributes
95                                 false,          // HasChildren
96                                 true);          // IsEmptyElement
97                 }
98
99                 [Test]
100                 public void AppendChildStringFragment ()
101                 {
102                         // check that the input string inherits
103                         // namespace context.
104                         XPathNavigator nav = GetInstance ("<root xmlns='urn:foo'/>");
105                         nav.MoveToFirstChild ();
106                         nav.AppendChild ("<child/>fragment<child></child>");
107
108                         Assert.IsTrue (nav.MoveToFirstChild (), "#1-1");
109                         AssertNavigator ("#1-2", nav,
110                                 XPathNodeType.Element,
111                                 String.Empty,   // Prefix
112                                 "child",        // LocalName
113                                 "urn:foo",      // NamespaceURI
114                                 "child",        // Name
115                                 String.Empty,   // Value
116                                 false,          // HasAttributes
117                                 false,          // HasChildren
118                                 true);          // IsEmptyElement
119
120                         Assert.IsFalse (nav.MoveToFirstChild (), "#2-1");
121                         Assert.IsTrue (nav.MoveToNext (), "#2-2");
122                         AssertNavigator ("#2-3", nav,
123                                 XPathNodeType.Text,
124                                 String.Empty,   // Prefix
125                                 String.Empty,   // LocalName
126                                 String.Empty,   // NamespaceURI
127                                 String.Empty,   // Name
128                                 "fragment",     // Value
129                                 false,          // HasAttributes
130                                 false,          // HasChildren
131                                 false);         // IsEmptyElement
132
133                         Assert.IsTrue (nav.MoveToNext (), "#3-1");
134                         AssertNavigator ("#3-2", nav,
135                                 XPathNodeType.Element,
136                                 String.Empty,   // Prefix
137                                 "child",        // LocalName
138                                 "urn:foo",      // NamespaceURI
139                                 "child",        // Name
140                                 String.Empty,   // Value
141                                 false,          // HasAttributes
142                                 false,          // HasChildren
143                                 false);         // IsEmptyElement
144                 }
145
146                 [Test]
147                 public void AppendChildStringInvalidFragment ()
148                 {
149                         XPathNavigator nav = GetInstance ("<root xmlns='urn:foo'/>");
150                         nav.MoveToFirstChild ();
151                         nav.AppendChild ("<?xml version='1.0'?><root/>");
152                 }
153
154                 [Test]
155                 [ExpectedException (typeof (InvalidOperationException))]
156                 public void AppendChildToTextNode ()
157                 {
158                         XPathNavigator nav = GetInstance ("<root>text</root>");
159                         nav.MoveToFirstChild ();
160                         nav.MoveToFirstChild ();
161                         XmlWriter w = nav.AppendChild ();
162                 }
163
164                 [Test]
165                 public void InsertAfter ()
166                 {
167                         XPathNavigator nav = GetInstance ("<root>test</root>");
168                         nav.MoveToFirstChild ();
169                         nav.MoveToFirstChild ();
170                         nav.InsertAfter ("<blah/><doh>sample</doh>");
171
172                         AssertNavigator ("#1", nav,
173                                 XPathNodeType.Text,
174                                 String.Empty,   // Prefix
175                                 String.Empty,   // LocalName
176                                 String.Empty,   // NamespaceURI
177                                 String.Empty,   // Name
178                                 "test",         // Value
179                                 false,          // HasAttributes
180                                 false,          // HasChildren
181                                 false);         // IsEmptyElement
182
183                         Assert.IsTrue (nav.MoveToNext (), "#2");
184                         AssertNavigator ("#2-2", nav,
185                                 XPathNodeType.Element,
186                                 String.Empty,   // Prefix
187                                 "blah",         // LocalName
188                                 String.Empty,   // NamespaceURI
189                                 "blah",         // Name
190                                 String.Empty,   // Value
191                                 false,          // HasAttributes
192                                 false,          // HasChildren
193                                 true);          // IsEmptyElement
194
195                         Assert.IsTrue (nav.MoveToNext (), "#3");
196                         AssertNavigator ("#3-2", nav,
197                                 XPathNodeType.Element,
198                                 String.Empty,   // Prefix
199                                 "doh",          // LocalName
200                                 String.Empty,   // NamespaceURI
201                                 "doh",          // Name
202                                 "sample",       // Value
203                                 false,          // HasAttributes
204                                 true,           // HasChildren
205                                 false);         // IsEmptyElement
206                 }
207
208                 [Test]
209                 [ExpectedException (typeof (InvalidOperationException))]
210                 public void InsertAfterRoot ()
211                 {
212                         XPathNavigator nav = GetInstance ("<root/>");
213                         nav.InsertAfter ();
214                 }
215
216                 [Test]
217                 public void InsertBefore ()
218                 {
219                         XPathNavigator nav = GetInstance ("<root>test</root>");
220                         nav.MoveToFirstChild ();
221                         nav.MoveToFirstChild ();
222                         nav.InsertBefore ("<blah/><doh>sample</doh>");
223
224                         AssertNavigator ("#1", nav,
225                                 XPathNodeType.Text,
226                                 String.Empty,   // Prefix
227                                 String.Empty,   // LocalName
228                                 String.Empty,   // NamespaceURI
229                                 String.Empty,   // Name
230                                 "test",         // Value
231                                 false,          // HasAttributes
232                                 false,          // HasChildren
233                                 false);         // IsEmptyElement
234
235                         Assert.IsTrue (nav.MoveToFirst (), "#2-1");
236                         AssertNavigator ("#2-2", nav,
237                                 XPathNodeType.Element,
238                                 String.Empty,   // Prefix
239                                 "blah",         // LocalName
240                                 String.Empty,   // NamespaceURI
241                                 "blah",         // Name
242                                 String.Empty,   // Value
243                                 false,          // HasAttributes
244                                 false,          // HasChildren
245                                 true);          // IsEmptyElement
246
247                         Assert.IsTrue (nav.MoveToNext (), "#3");
248                         AssertNavigator ("#3-2", nav,
249                                 XPathNodeType.Element,
250                                 String.Empty,   // Prefix
251                                 "doh",          // LocalName
252                                 String.Empty,   // NamespaceURI
253                                 "doh",          // Name
254                                 "sample",       // Value
255                                 false,          // HasAttributes
256                                 true,           // HasChildren
257                                 false);         // IsEmptyElement
258                 }
259
260                 [Test]
261                 [ExpectedException (typeof (InvalidOperationException))]
262                 public void InsertBeforeRoot ()
263                 {
264                         XPathNavigator nav = GetInstance ("<root/>");
265                         nav.InsertBefore ();
266                 }
267         }
268 }
269
270 #endif