2 // MonoTests.System.Xml.XPathNavigatorCommonTests
5 // Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
7 // (C) 2003 Atsushi Enomoto
12 using System.Xml.XPath;
13 using NUnit.Framework;
15 namespace MonoTests.System.Xml
18 public class XPathNavigatorCommonTests : Assertion
22 XPathDocument xpathDocument;
25 public void GetReady ()
27 document = new XmlDocument ();
30 private XPathNavigator GetXmlDocumentNavigator (string xml)
32 document.LoadXml (xml);
33 return document.CreateNavigator ();
36 private XPathNavigator GetXPathDocumentNavigator (XmlNode node)
38 XmlNodeReader xr = new XmlNodeReader (node);
39 xpathDocument = new XPathDocument (xr);
40 return xpathDocument.CreateNavigator ();
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)
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);
58 public void DocumentWithXmlDeclaration ()
60 string xml = "<?xml version=\"1.0\" standalone=\"yes\"?><foo>bar</foo>";
62 nav = GetXmlDocumentNavigator (xml);
63 DocumentWithXmlDeclaration (nav);
64 nav = GetXPathDocumentNavigator (document);
65 DocumentWithXmlDeclaration (nav);
68 public void DocumentWithXmlDeclaration (XPathNavigator nav)
70 nav.MoveToFirstChild ();
71 AssertNavigator ("#1", nav, XPathNodeType.Element, "", "foo", "", "foo", "bar", false, true, false);
75 public void DocumentWithProcessingInstruction ()
77 string xml = "<?xml-stylesheet href='foo.xsl' type='text/xsl' ?><foo />";
79 nav = GetXmlDocumentNavigator (xml);
80 DocumentWithProcessingInstruction (nav);
81 nav = GetXPathDocumentNavigator (document);
82 DocumentWithProcessingInstruction (nav);
85 public void DocumentWithProcessingInstruction (XPathNavigator nav)
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 ());
93 public void XmlRootElementOnly ()
95 string xml = "<foo />";
97 nav = GetXmlDocumentNavigator (xml);
98 XmlRootElementOnly (nav);
99 nav = GetXPathDocumentNavigator (document);
100 XmlRootElementOnly (nav);
103 private void XmlRootElementOnly (XPathNavigator nav)
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 ());
112 AssertNavigator ("#3", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
113 Assert (!nav.MoveToNext ());
117 public void XmlSimpleTextContent ()
119 string xml = "<foo>Test.</foo>";
121 nav = GetXmlDocumentNavigator (xml);
122 XmlSimpleTextContent (nav);
123 nav = GetXPathDocumentNavigator (document);
124 XmlSimpleTextContent (nav);
127 private void XmlSimpleTextContent (XPathNavigator nav)
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);
137 Assert (nav.MoveToParent ());
138 AssertNavigator ("#4", nav, XPathNodeType.Element, "", "foo", "", "foo", "Test.", false, true, false);
140 Assert (nav.MoveToParent ());
141 AssertNavigator ("#5", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
143 nav.MoveToFirstChild ();
144 nav.MoveToFirstChild ();
146 AssertNavigator ("#6", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
147 Assert (!nav.MoveToNext ());
151 public void XmlSimpleElementContent ()
153 string xml = "<foo><bar /></foo>";
155 nav = GetXmlDocumentNavigator (xml);
156 XmlSimpleElementContent (nav);
157 nav = GetXPathDocumentNavigator (document);
158 XmlSimpleElementContent (nav);
161 private void XmlSimpleElementContent (XPathNavigator nav)
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 ());
169 Assert (nav.MoveToFirstChild ());
170 AssertNavigator ("#3", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
172 Assert (nav.MoveToParent ());
173 AssertNavigator ("#4", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
176 AssertNavigator ("#5", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
177 Assert (!nav.MoveToNext ());
181 public void XmlTwoElementsContent ()
183 string xml = "<foo><bar /><baz /></foo>";
185 nav = GetXmlDocumentNavigator (xml);
186 XmlTwoElementsContent (nav);
187 nav = GetXPathDocumentNavigator (document);
188 XmlTwoElementsContent (nav);
191 private void XmlTwoElementsContent (XPathNavigator nav)
193 AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
195 Assert (nav.MoveToFirstChild ());
196 AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
197 Assert (!nav.MoveToNext ());
198 Assert (!nav.MoveToPrevious ());
200 Assert (nav.MoveToFirstChild ());
201 AssertNavigator ("#3", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
202 Assert (!nav.MoveToFirstChild ());
204 Assert (nav.MoveToNext ());
205 AssertNavigator ("#4", nav, XPathNodeType.Element, "", "baz", "", "baz", "", false, false, true);
206 Assert (!nav.MoveToFirstChild ());
208 Assert (nav.MoveToPrevious ());
209 AssertNavigator ("#5", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
212 AssertNavigator ("#6", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
213 Assert (!nav.MoveToNext ());
217 public void XmlElementWithAttributes ()
219 string xml = "<img src='foo.png' alt='image Fooooooo!' />";
221 nav = GetXmlDocumentNavigator (xml);
222 XmlElementWithAttributes (nav);
223 nav = GetXPathDocumentNavigator (document);
224 XmlElementWithAttributes (nav);
227 private void XmlElementWithAttributes (XPathNavigator nav)
229 nav.MoveToFirstChild ();
230 AssertNavigator ("#1", nav, XPathNodeType.Element, "", "img", "", "img", "", true, false, true);
231 Assert (!nav.MoveToNext ());
232 Assert (!nav.MoveToPrevious ());
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.
238 Assert (nav.MoveToNextAttribute ());
239 AssertNavigator ("#3", nav, XPathNodeType.Attribute, "", "alt", "", "alt", "image Fooooooo!", false, false, false);
240 Assert (!nav.MoveToNextAttribute ());
242 Assert (nav.MoveToParent ());
243 AssertNavigator ("#4", nav, XPathNodeType.Element, "", "img", "", "img", "", true, false, true);
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);
253 AssertNavigator ("#7", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
257 // seems like MS does not want to fix their long-time-known
258 // XPathNavigator bug, so just set it as NotDotNet.
260 [Category ("NotDotNet")]
261 public void XmlNamespaceNode ()
263 string xml = "<html xmlns='http://www.w3.org/1999/xhtml'><body>test.</body></html>";
265 nav = GetXmlDocumentNavigator (xml);
266 XmlNamespaceNode (nav);
267 nav = GetXPathDocumentNavigator (document);
268 XmlNamespaceNode (nav);
271 private void XmlNamespaceNode (XPathNavigator nav)
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);
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
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));
293 // Test to check if MoveToRoot() resets Namespace node status.
295 AssertNavigator ("#4", nav, XPathNodeType.Root, "", "", "", "", "test.", false, true, false);
296 nav.MoveToFirstChild ();
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);
304 // Test MoveToParent()
305 Assert (nav.MoveToParent ());
306 AssertNavigator ("#6", nav, XPathNodeType.Element,
307 "", "html", xhtml, "html", "test.", false, true, false);
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);
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);
324 AssertNavigator ("#10", nav, XPathNodeType.Root, "", "", "", "", "test.", false, true, false);
328 public void MoveToNamespaces ()
330 string xml = "<a xmlns:x='urn:x'><b xmlns:y='urn:y'/><c/><d><e attr='a'/></d></a>";
\r
332 nav = GetXmlDocumentNavigator (xml);
333 MoveToNamespaces (nav);
334 nav = GetXPathDocumentNavigator (document);
335 MoveToNamespaces (nav);
338 private void MoveToNamespaces (XPathNavigator nav)
340 XPathNodeIterator iter = nav.Select ("//e");
\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
351 public void IsDescendant ()
353 string xml = "<a><b/><c/><d><e attr='a'/></d></a>";
\r
355 nav = GetXmlDocumentNavigator (xml);
357 nav = GetXPathDocumentNavigator (document);
361 private void IsDescendant (XPathNavigator nav)
363 XPathNavigator tmp = nav.Clone ();
\r
364 XPathNodeIterator iter = nav.Select ("//e");
\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
383 public void LiterallySplittedText ()
385 string xml = "<root><![CDATA[test]]> string</root>";
\r
387 nav = GetXmlDocumentNavigator (xml);
388 LiterallySplittedText (nav);
389 nav = GetXPathDocumentNavigator (document);
390 LiterallySplittedText (nav);
393 private void LiterallySplittedText (XPathNavigator nav)
395 nav.MoveToFirstChild ();
\r
396 nav.MoveToFirstChild ();
\r
397 AssertEquals (XPathNodeType.Text, nav.NodeType);
\r
398 AssertEquals ("test string", nav.Value);
\r
403 public void SelectChildren ()
405 string xml = "<root><foo xmlns='urn:foo' /><ns:foo xmlns:ns='urn:foo' /></root>";
407 nav = GetXmlDocumentNavigator (xml);
408 SelectChildrenNS (nav);
409 nav = GetXPathDocumentNavigator (document);
410 SelectChildrenNS (nav);
413 private void SelectChildrenNS (XPathNavigator nav)
415 nav.MoveToFirstChild (); // root
416 XPathNodeIterator iter = nav.SelectChildren ("foo", "urn:foo");
417 AssertEquals (2, iter.Count);