merge -r 53370:58178
[mono.git] / mcs / class / System.XML / Test / System.Xml.XPath / XPathNavigatorCommonTests.cs
1 //
2 // MonoTests.System.Xml.XPathNavigatorCommonTests
3 //
4 // Author:
5 //      Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
6 //
7 // (C) 2003 Atsushi Enomoto
8 //
9
10 using System;
11 using System.Xml;
12 using System.Xml.XPath;
13 using NUnit.Framework;
14
15 namespace MonoTests.System.Xml
16 {
17         [TestFixture]
18         public class XPathNavigatorCommonTests : Assertion
19         {
20                 XmlDocument document;
21                 XPathNavigator nav;
22                 XPathDocument xpathDocument;
23
24                 [SetUp]
25                 public void GetReady ()
26                 {
27                         document = new XmlDocument ();
28                 }
29
30                 private XPathNavigator GetXmlDocumentNavigator (string xml)
31                 {
32                         document.LoadXml (xml);
33                         return document.CreateNavigator ();
34                 }
35                 
36                 private XPathNavigator GetXPathDocumentNavigator (XmlNode node)
37                 {
38                         XmlNodeReader xr = new XmlNodeReader (node);
39                         xpathDocument = new XPathDocument (xr);
40                         return xpathDocument.CreateNavigator ();
41                 }
42
43                 private void AssertNavigator (string label, XPathNavigator nav, XPathNodeType type, string prefix, string localName, string ns, string name, string value, bool hasAttributes, bool hasChildren, bool isEmptyElement)
44                 {
45                         label += nav.GetType ();
46                         AssertEquals (label + "NodeType", type, nav.NodeType);
47                         AssertEquals (label + "Prefix", prefix, nav.Prefix);
48                         AssertEquals (label + "LocalName", localName, nav.LocalName);
49                         AssertEquals (label + "Namespace", ns, nav.NamespaceURI);
50                         AssertEquals (label + "Name", name, nav.Name);
51                         AssertEquals (label + "Value", value, nav.Value);
52                         AssertEquals (label + "HasAttributes", hasAttributes, nav.HasAttributes);
53                         AssertEquals (label + "HasChildren", hasChildren, nav.HasChildren);
54                         AssertEquals (label + "IsEmptyElement", isEmptyElement, nav.IsEmptyElement);
55                 }
56
57                 [Test]
58                 public void DocumentWithXmlDeclaration ()
59                 {
60                         string xml = "<?xml version=\"1.0\" standalone=\"yes\"?><foo>bar</foo>";
61
62                         nav = GetXmlDocumentNavigator (xml);
63                         DocumentWithXmlDeclaration (nav);
64                         nav = GetXPathDocumentNavigator (document);
65                         DocumentWithXmlDeclaration (nav);
66                 }
67
68                 public void DocumentWithXmlDeclaration (XPathNavigator nav)
69                 {
70                         nav.MoveToFirstChild ();
71                         AssertNavigator ("#1", nav, XPathNodeType.Element, "", "foo", "", "foo", "bar", false, true, false);
72                 }
73
74                 [Test]
75                 public void DocumentWithProcessingInstruction ()
76                 {
77                         string xml = "<?xml-stylesheet href='foo.xsl' type='text/xsl' ?><foo />";
78
79                         nav = GetXmlDocumentNavigator (xml);
80                         DocumentWithProcessingInstruction (nav);
81                         nav = GetXPathDocumentNavigator (document);
82                         DocumentWithProcessingInstruction (nav);
83                 }
84
85                 public void DocumentWithProcessingInstruction (XPathNavigator nav)
86                 {
87                         Assert (nav.MoveToFirstChild ());
88                         AssertNavigator ("#1", nav, XPathNodeType.ProcessingInstruction, "", "xml-stylesheet", "", "xml-stylesheet", "href='foo.xsl' type='text/xsl' ", false, false, false);
89                         Assert (!nav.MoveToFirstChild ());
90                 }
91
92                 [Test]
93                 public void XmlRootElementOnly ()
94                 {
95                         string xml = "<foo />";
96
97                         nav = GetXmlDocumentNavigator (xml);
98                         XmlRootElementOnly (nav);
99                         nav = GetXPathDocumentNavigator (document);
100                         XmlRootElementOnly (nav);
101                 }
102
103                 private void XmlRootElementOnly (XPathNavigator nav)
104                 {
105                         AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
106                         Assert (nav.MoveToFirstChild ());
107                         AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, false, true);
108                         Assert (!nav.MoveToFirstChild ());
109                         Assert (!nav.MoveToNext ());
110                         Assert (!nav.MoveToPrevious ());
111                         nav.MoveToRoot ();
112                         AssertNavigator ("#3", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
113                         Assert (!nav.MoveToNext ());
114                 }
115
116                 [Test]
117                 public void XmlSimpleTextContent ()
118                 {
119                         string xml = "<foo>Test.</foo>";
120
121                         nav = GetXmlDocumentNavigator (xml);
122                         XmlSimpleTextContent (nav);
123                         nav = GetXPathDocumentNavigator (document);
124                         XmlSimpleTextContent (nav);
125                 }
126
127                 private void XmlSimpleTextContent (XPathNavigator nav)
128                 {
129                         AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
130                         Assert (nav.MoveToFirstChild ());
131                         AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "Test.", false, true, false);
132                         Assert (!nav.MoveToNext ());
133                         Assert (!nav.MoveToPrevious ());
134                         Assert (nav.MoveToFirstChild ());
135                         AssertNavigator ("#3", nav, XPathNodeType.Text, "", "", "", "", "Test.", false, false, false);
136
137                         Assert (nav.MoveToParent ());
138                         AssertNavigator ("#4", nav, XPathNodeType.Element, "", "foo", "", "foo", "Test.", false, true, false);
139
140                         Assert (nav.MoveToParent ());
141                         AssertNavigator ("#5", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
142
143                         nav.MoveToFirstChild ();
144                         nav.MoveToFirstChild ();
145                         nav.MoveToRoot ();
146                         AssertNavigator ("#6", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
147                         Assert (!nav.MoveToNext ());
148                 }
149
150                 [Test]
151                 public void XmlSimpleElementContent ()
152                 {
153                         string xml = "<foo><bar /></foo>";
154
155                         nav = GetXmlDocumentNavigator (xml);
156                         XmlSimpleElementContent (nav);
157                         nav = GetXPathDocumentNavigator (document);
158                         XmlSimpleElementContent (nav);
159                 }
160
161                 private void XmlSimpleElementContent (XPathNavigator nav)
162                 {
163                         AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
164                         Assert (nav.MoveToFirstChild ());
165                         AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
166                         Assert (!nav.MoveToNext ());
167                         Assert (!nav.MoveToPrevious ());
168
169                         Assert (nav.MoveToFirstChild ());
170                         AssertNavigator ("#3", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
171
172                         Assert (nav.MoveToParent ());
173                         AssertNavigator ("#4", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
174
175                         nav.MoveToRoot ();
176                         AssertNavigator ("#5", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
177                         Assert (!nav.MoveToNext ());
178                 }
179
180                 [Test]
181                 public void XmlTwoElementsContent ()
182                 {
183                         string xml = "<foo><bar /><baz /></foo>";
184
185                         nav = GetXmlDocumentNavigator (xml);
186                         XmlTwoElementsContent (nav);
187                         nav = GetXPathDocumentNavigator (document);
188                         XmlTwoElementsContent (nav);
189                 }
190
191                 private void XmlTwoElementsContent (XPathNavigator nav)
192                 {
193                         AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
194
195                         Assert (nav.MoveToFirstChild ());
196                         AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
197                         Assert (!nav.MoveToNext ());
198                         Assert (!nav.MoveToPrevious ());
199
200                         Assert (nav.MoveToFirstChild ());
201                         AssertNavigator ("#3", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
202                         Assert (!nav.MoveToFirstChild ());
203
204                         Assert (nav.MoveToNext ());
205                         AssertNavigator ("#4", nav, XPathNodeType.Element, "", "baz", "", "baz", "", false, false, true);
206                         Assert (!nav.MoveToFirstChild ());
207
208                         Assert (nav.MoveToPrevious ());
209                         AssertNavigator ("#5", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
210
211                         nav.MoveToRoot ();
212                         AssertNavigator ("#6", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
213                         Assert (!nav.MoveToNext ());
214                 }
215
216                 [Test]
217                 public void XmlElementWithAttributes ()
218                 {
219                         string xml = "<img src='foo.png' alt='image Fooooooo!' />";
220
221                         nav = GetXmlDocumentNavigator (xml);
222                         XmlElementWithAttributes (nav);
223                         nav = GetXPathDocumentNavigator (document);
224                         XmlElementWithAttributes (nav);
225                 }
226
227                 private void XmlElementWithAttributes (XPathNavigator nav)
228                 {
229                         nav.MoveToFirstChild ();
230                         AssertNavigator ("#1", nav, XPathNodeType.Element, "", "img", "", "img", "", true, false, true);
231                         Assert (!nav.MoveToNext ());
232                         Assert (!nav.MoveToPrevious ());
233
234                         Assert (nav.MoveToFirstAttribute ());
235                         AssertNavigator ("#2", nav, XPathNodeType.Attribute, "", "src", "", "src", "foo.png", false, false, false);
236                         Assert (!nav.MoveToFirstAttribute ());  // On attributes, it fails.
237
238                         Assert (nav.MoveToNextAttribute ());
239                         AssertNavigator ("#3", nav, XPathNodeType.Attribute, "", "alt", "", "alt", "image Fooooooo!", false, false, false);
240                         Assert (!nav.MoveToNextAttribute ());
241
242                         Assert (nav.MoveToParent ());
243                         AssertNavigator ("#4", nav, XPathNodeType.Element, "", "img", "", "img", "", true, false, true);
244
245                         Assert (nav.MoveToAttribute ("alt", ""));
246                         AssertNavigator ("#5", nav, XPathNodeType.Attribute, "", "alt", "", "alt", "image Fooooooo!", false, false, false);
247                         Assert (!nav.MoveToAttribute ("src", ""));      // On attributes, it fails.
248                         Assert (nav.MoveToParent ());
249                         Assert (nav.MoveToAttribute ("src", ""));
250                         AssertNavigator ("#6", nav, XPathNodeType.Attribute, "", "src", "", "src", "foo.png", false, false, false);
251
252                         nav.MoveToRoot ();
253                         AssertNavigator ("#7", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
254                 }
255
256                 [Test]
257                 // seems like MS does not want to fix their long-time-known
258                 // XPathNavigator bug, so just set it as NotDotNet.
259                 // We are better.
260                 [Category ("NotDotNet")]
261                 public void XmlNamespaceNode ()
262                 {
263                         string xml = "<html xmlns='http://www.w3.org/1999/xhtml'><body>test.</body></html>";
264
265                         nav = GetXmlDocumentNavigator (xml);
266                         XmlNamespaceNode (nav);
267                         nav = GetXPathDocumentNavigator (document);
268                         XmlNamespaceNode (nav);
269                 }
270
271                 private void XmlNamespaceNode (XPathNavigator nav)
272                 {
273                         string xhtml = "http://www.w3.org/1999/xhtml";
274                         string xmlNS = "http://www.w3.org/XML/1998/namespace";
275                         nav.MoveToFirstChild ();
276                         AssertNavigator ("#1", nav, XPathNodeType.Element,
277                                 "", "html", xhtml, "html", "test.", false, true, false);
278                         Assert (nav.MoveToFirstNamespace (XPathNamespaceScope.Local));
279                         AssertNavigator ("#2", nav, XPathNodeType.Namespace,
280                                 "", "", "", "", xhtml, false, false, false);
281
282                         // Test difference between Local, ExcludeXml and All.
283                         Assert (!nav.MoveToNextNamespace (XPathNamespaceScope.Local));
284                         Assert (!nav.MoveToNextNamespace (XPathNamespaceScope.ExcludeXml));
285                         // LAMESPEC: MS.NET 1.0 XmlDocument seems to have some bugs around here.
286                         // see http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q316808
287 #if true
288                         Assert (nav.MoveToNextNamespace (XPathNamespaceScope.All));
289                         AssertNavigator ("#3", nav, XPathNodeType.Namespace,
290                                 "", "xml", "", "xml", xmlNS, false, false, false);
291                         Assert (!nav.MoveToNextNamespace (XPathNamespaceScope.All));
292 #endif
293                         // Test to check if MoveToRoot() resets Namespace node status.
294                         nav.MoveToRoot ();
295                         AssertNavigator ("#4", nav, XPathNodeType.Root, "", "", "", "", "test.", false, true, false);
296                         nav.MoveToFirstChild ();
297
298                         // Test without XPathNamespaceScope argument.
299                         Assert (nav.MoveToFirstNamespace ());
300                         Assert (nav.MoveToNextNamespace ());
301                         AssertNavigator ("#5", nav, XPathNodeType.Namespace,
302                                 "", "xml", "", "xml", xmlNS, false, false, false);
303
304                         // Test MoveToParent()
305                         Assert (nav.MoveToParent ());
306                         AssertNavigator ("#6", nav, XPathNodeType.Element,
307                                 "", "html", xhtml, "html", "test.", false, true, false);
308
309                         nav.MoveToFirstChild ();        // body
310                         // Test difference between Local and ExcludeXml
311                         Assert (!nav.MoveToFirstNamespace (XPathNamespaceScope.Local));
312                         Assert (nav.MoveToFirstNamespace (XPathNamespaceScope.ExcludeXml));
313                         AssertNavigator ("#7", nav, XPathNodeType.Namespace,
314                                 "", "", "", "", xhtml, false, false, false);
315
316                         Assert (nav.MoveToNextNamespace (XPathNamespaceScope.All));
317                         AssertNavigator ("#8", nav, XPathNodeType.Namespace,
318                                 "", "xml", "", "xml", xmlNS, false, false, false);
319                         Assert (nav.MoveToParent ());
320                         AssertNavigator ("#9", nav, XPathNodeType.Element,
321                                 "", "body", xhtml, "body", "test.", false, true, false);
322
323                         nav.MoveToRoot ();
324                         AssertNavigator ("#10", nav, XPathNodeType.Root, "", "", "", "", "test.", false, true, false);
325                 }
326
327                 [Test]
328                 public void MoveToNamespaces ()
329                 {
330                         string xml = "<a xmlns:x='urn:x'><b xmlns:y='urn:y'/><c/><d><e attr='a'/></d></a>";\r
331
332                         nav = GetXmlDocumentNavigator (xml);
333                         MoveToNamespaces (nav);
334                         nav = GetXPathDocumentNavigator (document);
335                         MoveToNamespaces (nav);
336                 }
337
338                 private void MoveToNamespaces (XPathNavigator nav)
339                 {
340                         XPathNodeIterator iter = nav.Select ("//e");\r
341                         iter.MoveNext ();\r
342                         nav.MoveTo (iter.Current);\r
343                         AssertEquals ("e", nav.Name);\r
344                         nav.MoveToFirstNamespace ();\r
345                         AssertEquals ("x", nav.Name);\r
346                         nav.MoveToNextNamespace ();\r
347                         AssertEquals ("xml", nav.Name);\r
348                 }
349
350                 [Test]
351                 public void IsDescendant ()
352                 {
353                         string xml = "<a><b/><c/><d><e attr='a'/></d></a>";\r
354
355                         nav = GetXmlDocumentNavigator (xml);
356                         IsDescendant (nav);
357                         nav = GetXPathDocumentNavigator (document);
358                         IsDescendant (nav);
359                 }
360
361                 private void IsDescendant (XPathNavigator nav)
362                 {
363                         XPathNavigator tmp = nav.Clone ();\r
364                         XPathNodeIterator iter = nav.Select ("//e");\r
365                         iter.MoveNext ();\r
366                         Assert (nav.MoveTo (iter.Current));\r
367                         Assert (nav.MoveToFirstAttribute ());\r
368                         AssertEquals ("attr", nav.Name);\r
369                         AssertEquals ("", tmp.Name);\r
370                         Assert (tmp.IsDescendant (nav));\r
371                         Assert (!nav.IsDescendant (tmp));\r
372                         tmp.MoveToFirstChild ();\r
373                         AssertEquals ("a", tmp.Name);\r
374                         Assert (tmp.IsDescendant (nav));\r
375                         Assert (!nav.IsDescendant (tmp));\r
376                         tmp.MoveTo (iter.Current);\r
377                         AssertEquals ("e", tmp.Name);\r
378                         Assert (tmp.IsDescendant (nav));\r
379                         Assert (!nav.IsDescendant (tmp));\r
380                 }
381
382                 [Test]
383                 public void LiterallySplittedText ()
384                 {
385                         string xml = "<root><![CDATA[test]]> string</root>";\r
386
387                         nav = GetXmlDocumentNavigator (xml);
388                         LiterallySplittedText (nav);
389                         nav = GetXPathDocumentNavigator (document);
390                         LiterallySplittedText (nav);
391                 }
392
393                 private void LiterallySplittedText (XPathNavigator nav)
394                 {
395                         nav.MoveToFirstChild ();\r
396                         nav.MoveToFirstChild ();\r
397                         AssertEquals (XPathNodeType.Text, nav.NodeType);\r
398                         AssertEquals ("test string", nav.Value);\r
399                 }
400
401                 // bug #75609
402                 [Test]
403                 public void SelectChildren ()
404                 {
405                         string xml = "<root><foo xmlns='urn:foo' /><ns:foo xmlns:ns='urn:foo' /></root>";
406
407                         nav = GetXmlDocumentNavigator (xml);
408                         SelectChildrenNS (nav);
409                         nav = GetXPathDocumentNavigator (document);
410                         SelectChildrenNS (nav);
411                 }
412
413                 private void SelectChildrenNS (XPathNavigator nav)
414                 {
415                         nav.MoveToFirstChild (); // root
416                         XPathNodeIterator iter = nav.SelectChildren ("foo", "urn:foo");
417                         AssertEquals (2, iter.Count);
418                 }
419         }
420 }