XPathNavigatorCommonTests.cs 14 KB

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