2 // MonoTests.System.Xml.XPathNavigatorCommonTests
5 // Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
7 // (C) 2003 Atsushi Enomoto
13 using System.Xml.XPath;
14 using NUnit.Framework;
16 namespace MonoTests.System.Xml
19 public class XPathNavigatorCommonTests : Assertion
23 XPathDocument xpathDocument;
26 public void GetReady ()
28 document = new XmlDocument ();
31 private XPathNavigator GetXmlDocumentNavigator (string xml)
33 document.LoadXml (xml);
34 return document.CreateNavigator ();
37 private XPathNavigator GetXPathDocumentNavigator (XmlNode node)
39 XmlNodeReader xr = new XmlNodeReader (node);
40 xpathDocument = new XPathDocument (xr);
41 return xpathDocument.CreateNavigator ();
44 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)
46 label += nav.GetType ();
47 AssertEquals (label + "NodeType", type, nav.NodeType);
48 AssertEquals (label + "Prefix", prefix, nav.Prefix);
49 AssertEquals (label + "LocalName", localName, nav.LocalName);
50 AssertEquals (label + "Namespace", ns, nav.NamespaceURI);
51 AssertEquals (label + "Name", name, nav.Name);
52 AssertEquals (label + "Value", value, nav.Value);
53 AssertEquals (label + "HasAttributes", hasAttributes, nav.HasAttributes);
54 AssertEquals (label + "HasChildren", hasChildren, nav.HasChildren);
55 AssertEquals (label + "IsEmptyElement", isEmptyElement, nav.IsEmptyElement);
59 public void DocumentWithXmlDeclaration ()
61 string xml = "<?xml version=\"1.0\" standalone=\"yes\"?><foo>bar</foo>";
63 nav = GetXmlDocumentNavigator (xml);
64 DocumentWithXmlDeclaration (nav);
65 nav = GetXPathDocumentNavigator (document);
66 DocumentWithXmlDeclaration (nav);
69 public void DocumentWithXmlDeclaration (XPathNavigator nav)
71 nav.MoveToFirstChild ();
72 AssertNavigator ("#1", nav, XPathNodeType.Element, "", "foo", "", "foo", "bar", false, true, false);
76 public void DocumentWithProcessingInstruction ()
78 string xml = "<?xml-stylesheet href='foo.xsl' type='text/xsl' ?><foo />";
80 nav = GetXmlDocumentNavigator (xml);
81 DocumentWithProcessingInstruction (nav);
82 nav = GetXPathDocumentNavigator (document);
83 DocumentWithProcessingInstruction (nav);
86 public void DocumentWithProcessingInstruction (XPathNavigator nav)
88 Assert (nav.MoveToFirstChild ());
89 AssertNavigator ("#1", nav, XPathNodeType.ProcessingInstruction, "", "xml-stylesheet", "", "xml-stylesheet", "href='foo.xsl' type='text/xsl' ", false, false, false);
90 Assert (!nav.MoveToFirstChild ());
94 public void XmlRootElementOnly ()
96 string xml = "<foo />";
98 nav = GetXmlDocumentNavigator (xml);
99 XmlRootElementOnly (nav);
100 nav = GetXPathDocumentNavigator (document);
101 XmlRootElementOnly (nav);
104 private void XmlRootElementOnly (XPathNavigator nav)
106 AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
107 Assert (nav.MoveToFirstChild ());
108 AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, false, true);
109 Assert (!nav.MoveToFirstChild ());
110 Assert (!nav.MoveToNext ());
111 Assert (!nav.MoveToPrevious ());
113 AssertNavigator ("#3", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
114 Assert (!nav.MoveToNext ());
118 public void XmlSimpleTextContent ()
120 string xml = "<foo>Test.</foo>";
122 nav = GetXmlDocumentNavigator (xml);
123 XmlSimpleTextContent (nav);
124 nav = GetXPathDocumentNavigator (document);
125 XmlSimpleTextContent (nav);
128 private void XmlSimpleTextContent (XPathNavigator nav)
130 AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
131 Assert (nav.MoveToFirstChild ());
132 AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "Test.", false, true, false);
133 Assert (!nav.MoveToNext ());
134 Assert (!nav.MoveToPrevious ());
135 Assert (nav.MoveToFirstChild ());
136 AssertNavigator ("#3", nav, XPathNodeType.Text, "", "", "", "", "Test.", false, false, false);
138 Assert (nav.MoveToParent ());
139 AssertNavigator ("#4", nav, XPathNodeType.Element, "", "foo", "", "foo", "Test.", false, true, false);
141 Assert (nav.MoveToParent ());
142 AssertNavigator ("#5", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
144 nav.MoveToFirstChild ();
145 nav.MoveToFirstChild ();
147 AssertNavigator ("#6", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
148 Assert (!nav.MoveToNext ());
152 public void XmlSimpleElementContent ()
154 string xml = "<foo><bar /></foo>";
156 nav = GetXmlDocumentNavigator (xml);
157 XmlSimpleElementContent (nav);
158 nav = GetXPathDocumentNavigator (document);
159 XmlSimpleElementContent (nav);
162 private void XmlSimpleElementContent (XPathNavigator nav)
164 AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
165 Assert (nav.MoveToFirstChild ());
166 AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
167 Assert (!nav.MoveToNext ());
168 Assert (!nav.MoveToPrevious ());
170 Assert (nav.MoveToFirstChild ());
171 AssertNavigator ("#3", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
173 Assert (nav.MoveToParent ());
174 AssertNavigator ("#4", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
177 AssertNavigator ("#5", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
178 Assert (!nav.MoveToNext ());
182 public void XmlTwoElementsContent ()
184 string xml = "<foo><bar /><baz /></foo>";
186 nav = GetXmlDocumentNavigator (xml);
187 XmlTwoElementsContent (nav);
188 nav = GetXPathDocumentNavigator (document);
189 XmlTwoElementsContent (nav);
192 private void XmlTwoElementsContent (XPathNavigator nav)
194 AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
196 Assert (nav.MoveToFirstChild ());
197 AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
198 Assert (!nav.MoveToNext ());
199 Assert (!nav.MoveToPrevious ());
201 Assert (nav.MoveToFirstChild ());
202 AssertNavigator ("#3", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
203 Assert (!nav.MoveToFirstChild ());
205 Assert (nav.MoveToNext ());
206 AssertNavigator ("#4", nav, XPathNodeType.Element, "", "baz", "", "baz", "", false, false, true);
207 Assert (!nav.MoveToFirstChild ());
209 Assert (nav.MoveToPrevious ());
210 AssertNavigator ("#5", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
213 AssertNavigator ("#6", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
214 Assert (!nav.MoveToNext ());
218 public void XmlElementWithAttributes ()
220 string xml = "<img src='foo.png' alt='image Fooooooo!' />";
222 nav = GetXmlDocumentNavigator (xml);
223 XmlElementWithAttributes (nav);
224 nav = GetXPathDocumentNavigator (document);
225 XmlElementWithAttributes (nav);
228 private void XmlElementWithAttributes (XPathNavigator nav)
230 nav.MoveToFirstChild ();
231 AssertNavigator ("#1", nav, XPathNodeType.Element, "", "img", "", "img", "", true, false, true);
232 Assert (!nav.MoveToNext ());
233 Assert (!nav.MoveToPrevious ());
235 Assert (nav.MoveToFirstAttribute ());
236 AssertNavigator ("#2", nav, XPathNodeType.Attribute, "", "src", "", "src", "foo.png", false, false, false);
237 Assert (!nav.MoveToFirstAttribute ()); // On attributes, it fails.
239 Assert (nav.MoveToNextAttribute ());
240 AssertNavigator ("#3", nav, XPathNodeType.Attribute, "", "alt", "", "alt", "image Fooooooo!", false, false, false);
241 Assert (!nav.MoveToNextAttribute ());
243 Assert (nav.MoveToParent ());
244 AssertNavigator ("#4", nav, XPathNodeType.Element, "", "img", "", "img", "", true, false, true);
246 Assert (nav.MoveToAttribute ("alt", ""));
247 AssertNavigator ("#5", nav, XPathNodeType.Attribute, "", "alt", "", "alt", "image Fooooooo!", false, false, false);
248 Assert (!nav.MoveToAttribute ("src", "")); // On attributes, it fails.
249 Assert (nav.MoveToParent ());
250 Assert (nav.MoveToAttribute ("src", ""));
251 AssertNavigator ("#6", nav, XPathNodeType.Attribute, "", "src", "", "src", "foo.png", false, false, false);
254 AssertNavigator ("#7", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
258 // seems like MS does not want to fix their long-time-known
259 // XPathNavigator bug, so just set it as NotDotNet.
261 [Category ("NotDotNet")]
262 public void XmlNamespaceNode ()
264 string xml = "<html xmlns='http://www.w3.org/1999/xhtml'><body>test.</body></html>";
266 nav = GetXmlDocumentNavigator (xml);
267 XmlNamespaceNode (nav);
268 nav = GetXPathDocumentNavigator (document);
269 XmlNamespaceNode (nav);
272 private void XmlNamespaceNode (XPathNavigator nav)
274 string xhtml = "http://www.w3.org/1999/xhtml";
275 string xmlNS = "http://www.w3.org/XML/1998/namespace";
276 nav.MoveToFirstChild ();
277 AssertNavigator ("#1", nav, XPathNodeType.Element,
278 "", "html", xhtml, "html", "test.", false, true, false);
279 Assert (nav.MoveToFirstNamespace (XPathNamespaceScope.Local));
280 AssertNavigator ("#2", nav, XPathNodeType.Namespace,
281 "", "", "", "", xhtml, false, false, false);
283 // Test difference between Local, ExcludeXml and All.
284 Assert (!nav.MoveToNextNamespace (XPathNamespaceScope.Local));
285 Assert (!nav.MoveToNextNamespace (XPathNamespaceScope.ExcludeXml));
286 // LAMESPEC: MS.NET 1.0 XmlDocument seems to have some bugs around here.
287 // see http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q316808
289 Assert (nav.MoveToNextNamespace (XPathNamespaceScope.All));
290 AssertNavigator ("#3", nav, XPathNodeType.Namespace,
291 "", "xml", "", "xml", xmlNS, false, false, false);
292 Assert (!nav.MoveToNextNamespace (XPathNamespaceScope.All));
294 // Test to check if MoveToRoot() resets Namespace node status.
296 AssertNavigator ("#4", nav, XPathNodeType.Root, "", "", "", "", "test.", false, true, false);
297 nav.MoveToFirstChild ();
299 // Test without XPathNamespaceScope argument.
300 Assert (nav.MoveToFirstNamespace ());
301 Assert (nav.MoveToNextNamespace ());
302 AssertNavigator ("#5", nav, XPathNodeType.Namespace,
303 "", "xml", "", "xml", xmlNS, false, false, false);
305 // Test MoveToParent()
306 Assert (nav.MoveToParent ());
307 AssertNavigator ("#6", nav, XPathNodeType.Element,
308 "", "html", xhtml, "html", "test.", false, true, false);
310 nav.MoveToFirstChild (); // body
311 // Test difference between Local and ExcludeXml
312 Assert ("Local should fail",
313 !nav.MoveToFirstNamespace (XPathNamespaceScope.Local));
314 Assert ("ExcludeXml should succeed",
315 nav.MoveToFirstNamespace (XPathNamespaceScope.ExcludeXml));
316 AssertNavigator ("#7", nav, XPathNodeType.Namespace,
317 "", "", "", "", xhtml, false, false, false);
319 Assert (nav.MoveToNextNamespace (XPathNamespaceScope.All));
320 AssertNavigator ("#8", nav, XPathNodeType.Namespace,
321 "", "xml", "", "xml", xmlNS, false, false, false);
322 Assert (nav.MoveToParent ());
323 AssertNavigator ("#9", nav, XPathNodeType.Element,
324 "", "body", xhtml, "body", "test.", false, true, false);
327 AssertNavigator ("#10", nav, XPathNodeType.Root, "", "", "", "", "test.", false, true, false);
331 public void MoveToNamespaces ()
333 string xml = "<a xmlns:x='urn:x'><b xmlns:y='urn:y'/><c/><d><e attr='a'/></d></a>";
335 nav = GetXmlDocumentNavigator (xml);
336 MoveToNamespaces (nav);
337 nav = GetXPathDocumentNavigator (document);
338 MoveToNamespaces (nav);
341 private void MoveToNamespaces (XPathNavigator nav)
343 XPathNodeIterator iter = nav.Select ("//e");
345 nav.MoveTo (iter.Current);
346 AssertEquals ("e", nav.Name);
347 nav.MoveToFirstNamespace ();
348 AssertEquals ("x", nav.Name);
349 nav.MoveToNextNamespace ();
350 AssertEquals ("xml", nav.Name);
354 public void IsDescendant ()
356 string xml = "<a><b/><c/><d><e attr='a'/></d></a>";
358 nav = GetXmlDocumentNavigator (xml);
360 nav = GetXPathDocumentNavigator (document);
364 private void IsDescendant (XPathNavigator nav)
366 XPathNavigator tmp = nav.Clone ();
367 XPathNodeIterator iter = nav.Select ("//e");
369 Assert (nav.MoveTo (iter.Current));
370 Assert (nav.MoveToFirstAttribute ());
371 AssertEquals ("attr", nav.Name);
372 AssertEquals ("", tmp.Name);
373 Assert (tmp.IsDescendant (nav));
374 Assert (!nav.IsDescendant (tmp));
375 tmp.MoveToFirstChild ();
376 AssertEquals ("a", tmp.Name);
377 Assert (tmp.IsDescendant (nav));
378 Assert (!nav.IsDescendant (tmp));
379 tmp.MoveTo (iter.Current);
380 AssertEquals ("e", tmp.Name);
381 Assert (tmp.IsDescendant (nav));
382 Assert (!nav.IsDescendant (tmp));
386 public void LiterallySplittedText ()
388 string xml = "<root><![CDATA[test]]> string</root>";
390 nav = GetXmlDocumentNavigator (xml);
391 LiterallySplittedText (nav);
392 nav = GetXPathDocumentNavigator (document);
393 LiterallySplittedText (nav);
396 private void LiterallySplittedText (XPathNavigator nav)
398 nav.MoveToFirstChild ();
399 nav.MoveToFirstChild ();
400 AssertEquals (XPathNodeType.Text, nav.NodeType);
401 AssertEquals ("test string", nav.Value);
406 public void SelectChildren ()
408 string xml = "<root><foo xmlns='urn:foo' /><ns:foo xmlns:ns='urn:foo' /></root>";
410 nav = GetXmlDocumentNavigator (xml);
411 SelectChildrenNS (nav);
412 nav = GetXPathDocumentNavigator (document);
413 SelectChildrenNS (nav);
416 private void SelectChildrenNS (XPathNavigator nav)
418 nav.MoveToFirstChild (); // root
419 XPathNodeIterator iter = nav.SelectChildren ("foo", "urn:foo");
420 AssertEquals (2, iter.Count);
427 public void OuterXml ()
429 string xml = @"<?xml version=""1.0""?>
431 <two>Some data.</two>
434 nav = GetXmlDocumentNavigator (xml);
436 nav = GetXPathDocumentNavigator (document);
440 private void OuterXml (XPathNavigator nav)
443 <two>Some data.</two>
445 AssertEquals (ret, nav.OuterXml.Replace ("\r\n", "\n"));