XPathNavigatorCommonTests.cs 14 KB

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