XPathNavigatorCommonTests.cs 13 KB

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