XPathNavigatorCommonTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 : Assertion
  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. 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);
  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 (nav.MoveToFirstChild ());
  83. AssertNavigator ("#1", nav, XPathNodeType.ProcessingInstruction, "", "xml-stylesheet", "", "xml-stylesheet", "href='foo.xsl' type='text/xsl' ", false, false, false);
  84. Assert (!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 (nav.MoveToFirstChild ());
  99. AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, false, true);
  100. Assert (!nav.MoveToFirstChild ());
  101. Assert (!nav.MoveToNext ());
  102. Assert (!nav.MoveToPrevious ());
  103. nav.MoveToRoot ();
  104. AssertNavigator ("#3", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
  105. Assert (!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 (nav.MoveToFirstChild ());
  120. AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "Test.", false, true, false);
  121. Assert (!nav.MoveToNext ());
  122. Assert (!nav.MoveToPrevious ());
  123. Assert (nav.MoveToFirstChild ());
  124. AssertNavigator ("#3", nav, XPathNodeType.Text, "", "", "", "", "Test.", false, false, false);
  125. Assert (nav.MoveToParent ());
  126. AssertNavigator ("#4", nav, XPathNodeType.Element, "", "foo", "", "foo", "Test.", false, true, false);
  127. Assert (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 (!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 (nav.MoveToFirstChild ());
  148. AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
  149. Assert (!nav.MoveToNext ());
  150. Assert (!nav.MoveToPrevious ());
  151. Assert (nav.MoveToFirstChild ());
  152. AssertNavigator ("#3", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
  153. Assert (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 (!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 (nav.MoveToFirstChild ());
  172. AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
  173. Assert (!nav.MoveToNext ());
  174. Assert (!nav.MoveToPrevious ());
  175. Assert (nav.MoveToFirstChild ());
  176. AssertNavigator ("#3", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
  177. Assert (!nav.MoveToFirstChild ());
  178. Assert (nav.MoveToNext ());
  179. AssertNavigator ("#4", nav, XPathNodeType.Element, "", "baz", "", "baz", "", false, false, true);
  180. Assert (!nav.MoveToFirstChild ());
  181. Assert (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 (!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 (!nav.MoveToNext ());
  201. Assert (!nav.MoveToPrevious ());
  202. Assert (nav.MoveToFirstAttribute ());
  203. AssertNavigator ("#2", nav, XPathNodeType.Attribute, "", "src", "", "src", "foo.png", false, false, false);
  204. Assert (!nav.MoveToFirstAttribute ()); // On attributes, it fails.
  205. Assert (nav.MoveToNextAttribute ());
  206. AssertNavigator ("#3", nav, XPathNodeType.Attribute, "", "alt", "", "alt", "image Fooooooo!", false, false, false);
  207. Assert (!nav.MoveToNextAttribute ());
  208. Assert (nav.MoveToParent ());
  209. AssertNavigator ("#4", nav, XPathNodeType.Element, "", "img", "", "img", "", true, false, true);
  210. Assert (nav.MoveToAttribute ("alt", ""));
  211. AssertNavigator ("#5", nav, XPathNodeType.Attribute, "", "alt", "", "alt", "image Fooooooo!", false, false, false);
  212. Assert (!nav.MoveToAttribute ("src", "")); // On attributes, it fails.
  213. Assert (nav.MoveToParent ());
  214. Assert (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 (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 (!nav.MoveToNextNamespace (XPathNamespaceScope.Local));
  244. Assert (!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 (nav.MoveToNextNamespace (XPathNamespaceScope.All));
  249. AssertNavigator ("#3", nav, XPathNodeType.Namespace,
  250. "", "xml", "", "xml", xmlNS, false, false, false);
  251. Assert (!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 (nav.MoveToFirstNamespace ());
  259. Assert (nav.MoveToNextNamespace ());
  260. AssertNavigator ("#5", nav, XPathNodeType.Namespace,
  261. "", "xml", "", "xml", xmlNS, false, false, false);
  262. // Test MoveToParent()
  263. Assert (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 ("Local should fail",
  269. !nav.MoveToFirstNamespace (XPathNamespaceScope.Local));
  270. Assert ("ExcludeXml should succeed",
  271. nav.MoveToFirstNamespace (XPathNamespaceScope.ExcludeXml));
  272. AssertNavigator ("#7", nav, XPathNodeType.Namespace,
  273. "", "", "", "", xhtml, false, false, false);
  274. Assert (nav.MoveToNextNamespace (XPathNamespaceScope.All));
  275. AssertNavigator ("#8", nav, XPathNodeType.Namespace,
  276. "", "xml", "", "xml", xmlNS, false, false, false);
  277. Assert (nav.MoveToParent ());
  278. AssertNavigator ("#9", nav, XPathNodeType.Element,
  279. "", "body", xhtml, "body", "test.", false, true, false);
  280. nav.MoveToRoot ();
  281. AssertNavigator ("#10", nav, XPathNodeType.Root, "", "", "", "", "test.", false, true, false);
  282. }
  283. [Test]
  284. public void MoveToNamespaces ()
  285. {
  286. string xml = "<a xmlns:x='urn:x'><b xmlns:y='urn:y'/><c/><d><e attr='a'/></d></a>";
  287. nav = GetXmlDocumentNavigator (xml);
  288. MoveToNamespaces (nav);
  289. nav = GetXPathDocumentNavigator (document);
  290. MoveToNamespaces (nav);
  291. }
  292. private void MoveToNamespaces (XPathNavigator nav)
  293. {
  294. XPathNodeIterator iter = nav.Select ("//e");
  295. iter.MoveNext ();
  296. nav.MoveTo (iter.Current);
  297. AssertEquals ("e", nav.Name);
  298. nav.MoveToFirstNamespace ();
  299. AssertEquals ("x", nav.Name);
  300. nav.MoveToNextNamespace ();
  301. AssertEquals ("xml", nav.Name);
  302. }
  303. [Test]
  304. public void IsDescendant ()
  305. {
  306. string xml = "<a><b/><c/><d><e attr='a'/></d></a>";
  307. nav = GetXmlDocumentNavigator (xml);
  308. IsDescendant (nav);
  309. nav = GetXPathDocumentNavigator (document);
  310. IsDescendant (nav);
  311. }
  312. private void IsDescendant (XPathNavigator nav)
  313. {
  314. XPathNavigator tmp = nav.Clone ();
  315. XPathNodeIterator iter = nav.Select ("//e");
  316. iter.MoveNext ();
  317. Assert (nav.MoveTo (iter.Current));
  318. Assert (nav.MoveToFirstAttribute ());
  319. AssertEquals ("attr", nav.Name);
  320. AssertEquals ("", tmp.Name);
  321. Assert (tmp.IsDescendant (nav));
  322. Assert (!nav.IsDescendant (tmp));
  323. tmp.MoveToFirstChild ();
  324. AssertEquals ("a", tmp.Name);
  325. Assert (tmp.IsDescendant (nav));
  326. Assert (!nav.IsDescendant (tmp));
  327. tmp.MoveTo (iter.Current);
  328. AssertEquals ("e", tmp.Name);
  329. Assert (tmp.IsDescendant (nav));
  330. Assert (!nav.IsDescendant (tmp));
  331. }
  332. [Test]
  333. public void LiterallySplittedText ()
  334. {
  335. string xml = "<root><![CDATA[test]]> string</root>";
  336. nav = GetXmlDocumentNavigator (xml);
  337. LiterallySplittedText (nav);
  338. nav = GetXPathDocumentNavigator (document);
  339. LiterallySplittedText (nav);
  340. }
  341. private void LiterallySplittedText (XPathNavigator nav)
  342. {
  343. nav.MoveToFirstChild ();
  344. nav.MoveToFirstChild ();
  345. AssertEquals (XPathNodeType.Text, nav.NodeType);
  346. AssertEquals ("test string", nav.Value);
  347. }
  348. // bug #75609
  349. [Test]
  350. public void SelectChildren ()
  351. {
  352. string xml = "<root><foo xmlns='urn:foo' /><ns:foo xmlns:ns='urn:foo' /></root>";
  353. nav = GetXmlDocumentNavigator (xml);
  354. SelectChildrenNS (nav);
  355. nav = GetXPathDocumentNavigator (document);
  356. SelectChildrenNS (nav);
  357. }
  358. private void SelectChildrenNS (XPathNavigator nav)
  359. {
  360. nav.MoveToFirstChild (); // root
  361. XPathNodeIterator iter = nav.SelectChildren ("foo", "urn:foo");
  362. AssertEquals (2, iter.Count);
  363. }
  364. #if NET_2_0
  365. [Test]
  366. // bug #78067
  367. public void OuterXml ()
  368. {
  369. string xml = @"<?xml version=""1.0""?>
  370. <one>
  371. <two>Some data.</two>
  372. </one>";
  373. nav = GetXmlDocumentNavigator (xml);
  374. OuterXml (nav);
  375. nav = GetXPathDocumentNavigator (document);
  376. OuterXml (nav);
  377. }
  378. private void OuterXml (XPathNavigator nav)
  379. {
  380. string ret = @"<one>
  381. <two>Some data.</two>
  382. </one>";
  383. AssertEquals (ret, nav.OuterXml.Replace ("\r\n", "\n"));
  384. }
  385. #endif
  386. [Test]
  387. public void GetNamespaceConsistentTree ()
  388. {
  389. document.PreserveWhitespace = true;
  390. 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>";
  391. nav = GetXmlDocumentNavigator (xml);
  392. GetNamespaceConsistentTree (nav);
  393. nav = GetXPathDocumentNavigator (document, XmlSpace.Preserve);
  394. GetNamespaceConsistentTree (nav);
  395. }
  396. private void GetNamespaceConsistentTree (XPathNavigator nav)
  397. {
  398. nav.MoveToFirstChild ();
  399. nav.MoveToFirstChild ();
  400. nav.MoveToNext ();
  401. AssertEquals ("#1." + nav.GetType (), "ns1", nav.GetNamespace (""));
  402. nav.MoveToNext ();
  403. nav.MoveToNext ();
  404. AssertEquals ("#2." + nav.GetType (), "", nav.GetNamespace (""));
  405. }
  406. }
  407. }