XPathNavigatorCommonTests.cs 16 KB

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