ExtensionsTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. //
  2. // Author:
  3. // Atsushi Enomoto <[email protected]>
  4. //
  5. // Copyright (C) 2010 Novell, Inc.
  6. // (C) 2003 Atsushi Enomoto <[email protected]>
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. //
  28. // (based on XPathNavigatorCommonTests)
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.IO;
  33. using System.Linq;
  34. using System.Xml;
  35. using System.Xml.Linq;
  36. using System.Xml.XPath;
  37. using NUnit.Framework;
  38. namespace MonoTests.System.Xml
  39. {
  40. [TestFixture]
  41. public class ExtensionsTest
  42. {
  43. XPathNavigator nav;
  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 = XDocument.Parse (xml).CreateNavigator ();
  62. DocumentWithXmlDeclaration (nav);
  63. }
  64. public void DocumentWithXmlDeclaration (XPathNavigator nav)
  65. {
  66. nav.MoveToFirstChild ();
  67. AssertNavigator ("#1", nav, XPathNodeType.Element, "", "foo", "", "foo", "bar", false, true, false);
  68. }
  69. [Test]
  70. public void DocumentWithProcessingInstruction ()
  71. {
  72. string xml = "<?xml-stylesheet href='foo.xsl' type='text/xsl' ?><foo />";
  73. nav = XDocument.Parse (xml).CreateNavigator ();
  74. DocumentWithProcessingInstruction (nav);
  75. }
  76. public void DocumentWithProcessingInstruction (XPathNavigator nav)
  77. {
  78. Assert.IsTrue (nav.MoveToFirstChild ());
  79. AssertNavigator ("#1", nav, XPathNodeType.ProcessingInstruction, "", "xml-stylesheet", "", "xml-stylesheet", "href='foo.xsl' type='text/xsl' ", false, false, false);
  80. Assert.IsTrue (!nav.MoveToFirstChild ());
  81. }
  82. [Test]
  83. public void XmlRootElementOnly ()
  84. {
  85. string xml = "<foo />";
  86. nav = XDocument.Parse (xml).CreateNavigator ();
  87. XmlRootElementOnly (nav);
  88. }
  89. private void XmlRootElementOnly (XPathNavigator nav)
  90. {
  91. AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
  92. Assert.IsTrue (nav.MoveToFirstChild ());
  93. AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, false, true);
  94. Assert.IsTrue (!nav.MoveToFirstChild ());
  95. Assert.IsTrue (!nav.MoveToNext ());
  96. Assert.IsTrue (!nav.MoveToPrevious ());
  97. nav.MoveToRoot ();
  98. AssertNavigator ("#3", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
  99. Assert.IsTrue (!nav.MoveToNext ());
  100. }
  101. [Test]
  102. public void XmlSimpleTextContent ()
  103. {
  104. string xml = "<foo>Test.</foo>";
  105. nav = XDocument.Parse (xml).CreateNavigator ();
  106. XmlSimpleTextContent (nav);
  107. }
  108. private void XmlSimpleTextContent (XPathNavigator nav)
  109. {
  110. AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
  111. Assert.IsTrue (nav.MoveToFirstChild (), "#x1");
  112. AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "Test.", false, true, false);
  113. Assert.IsTrue (!nav.MoveToNext (), "#x2");
  114. Assert.IsTrue (!nav.MoveToPrevious (), "#x3");
  115. Assert.IsTrue (nav.MoveToFirstChild (), "#x4");
  116. AssertNavigator ("#3", nav, XPathNodeType.Text, "", "", "", "", "Test.", false, false, false);
  117. Assert.IsTrue (nav.MoveToParent (), "#x5");
  118. AssertNavigator ("#4", nav, XPathNodeType.Element, "", "foo", "", "foo", "Test.", false, true, false);
  119. Assert.IsTrue (nav.MoveToParent (), "#x6");
  120. AssertNavigator ("#5", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
  121. nav.MoveToFirstChild ();
  122. nav.MoveToFirstChild ();
  123. nav.MoveToRoot ();
  124. AssertNavigator ("#6", nav, XPathNodeType.Root, "", "", "", "", "Test.", false, true, false);
  125. Assert.IsTrue (!nav.MoveToNext (), "#x7");
  126. }
  127. [Test]
  128. public void XmlSimpleElementContent ()
  129. {
  130. string xml = "<foo><bar /></foo>";
  131. nav = XDocument.Parse (xml).CreateNavigator ();
  132. XmlSimpleElementContent (nav);
  133. }
  134. private void XmlSimpleElementContent (XPathNavigator nav)
  135. {
  136. AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
  137. Assert.IsTrue (nav.MoveToFirstChild ());
  138. AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
  139. Assert.IsTrue (!nav.MoveToNext ());
  140. Assert.IsTrue (!nav.MoveToPrevious ());
  141. Assert.IsTrue (nav.MoveToFirstChild ());
  142. AssertNavigator ("#3", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
  143. Assert.IsTrue (nav.MoveToParent ());
  144. AssertNavigator ("#4", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
  145. nav.MoveToRoot ();
  146. AssertNavigator ("#5", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
  147. Assert.IsTrue (!nav.MoveToNext ());
  148. }
  149. [Test]
  150. public void XmlTwoElementsContent ()
  151. {
  152. string xml = "<foo><bar /><baz /></foo>";
  153. nav = XDocument.Parse (xml).CreateNavigator ();
  154. XmlTwoElementsContent (nav);
  155. }
  156. private void XmlTwoElementsContent (XPathNavigator nav)
  157. {
  158. AssertNavigator ("#1", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
  159. Assert.IsTrue (nav.MoveToFirstChild ());
  160. AssertNavigator ("#2", nav, XPathNodeType.Element, "", "foo", "", "foo", "", false, true, false);
  161. Assert.IsTrue (!nav.MoveToNext ());
  162. Assert.IsTrue (!nav.MoveToPrevious ());
  163. Assert.IsTrue (nav.MoveToFirstChild ());
  164. AssertNavigator ("#3", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
  165. Assert.IsTrue (!nav.MoveToFirstChild ());
  166. Assert.IsTrue (nav.MoveToNext ());
  167. AssertNavigator ("#4", nav, XPathNodeType.Element, "", "baz", "", "baz", "", false, false, true);
  168. Assert.IsTrue (!nav.MoveToFirstChild ());
  169. Assert.IsTrue (nav.MoveToPrevious ());
  170. AssertNavigator ("#5", nav, XPathNodeType.Element, "", "bar", "", "bar", "", false, false, true);
  171. nav.MoveToRoot ();
  172. AssertNavigator ("#6", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
  173. Assert.IsTrue (!nav.MoveToNext ());
  174. }
  175. [Test]
  176. public void XmlElementWithAttributes ()
  177. {
  178. string xml = "<img src='foo.png' alt='image Fooooooo!' />";
  179. nav = XDocument.Parse (xml).CreateNavigator ();
  180. XmlElementWithAttributes (nav);
  181. }
  182. private void XmlElementWithAttributes (XPathNavigator nav)
  183. {
  184. nav.MoveToFirstChild ();
  185. AssertNavigator ("#1", nav, XPathNodeType.Element, "", "img", "", "img", "", true, false, true);
  186. Assert.IsTrue (!nav.MoveToNext (), "#x1");
  187. Assert.IsTrue (!nav.MoveToPrevious (), "#x2");
  188. Assert.IsTrue (nav.MoveToFirstAttribute (), "#x3");
  189. AssertNavigator ("#2", nav, XPathNodeType.Attribute, "", "src", "", "src", "foo.png", false, false, false);
  190. Assert.IsTrue (!nav.MoveToFirstAttribute (), "#x4"); // On attributes, it fails.
  191. Assert.IsTrue (nav.MoveToNextAttribute (), "#x5");
  192. AssertNavigator ("#3", nav, XPathNodeType.Attribute, "", "alt", "", "alt", "image Fooooooo!", false, false, false);
  193. Assert.IsTrue (!nav.MoveToNextAttribute (), "#x6");
  194. Assert.IsTrue (nav.MoveToParent (), "#x7");
  195. AssertNavigator ("#4", nav, XPathNodeType.Element, "", "img", "", "img", "", true, false, true);
  196. Assert.IsTrue (nav.MoveToAttribute ("alt", ""), "#x8");
  197. AssertNavigator ("#5", nav, XPathNodeType.Attribute, "", "alt", "", "alt", "image Fooooooo!", false, false, false);
  198. Assert.IsTrue (!nav.MoveToAttribute ("src", ""), "#x9"); // On attributes, it fails.
  199. Assert.IsTrue (nav.MoveToParent (), "#x10");
  200. Assert.IsTrue (nav.MoveToAttribute ("src", ""), "#x11");
  201. AssertNavigator ("#6", nav, XPathNodeType.Attribute, "", "src", "", "src", "foo.png", false, false, false);
  202. nav.MoveToRoot ();
  203. AssertNavigator ("#7", nav, XPathNodeType.Root, "", "", "", "", "", false, true, false);
  204. }
  205. [Test]
  206. // seems like MS does not want to fix their long-time-known
  207. // XPathNavigator bug, so just set it as NotDotNet.
  208. // We are better.
  209. [Category ("NotDotNet")]
  210. public void XmlNamespaceNode ()
  211. {
  212. string xml = "<html xmlns='http://www.w3.org/1999/xhtml'><body>test.</body></html>";
  213. nav = XDocument.Parse (xml).CreateNavigator ();
  214. XmlNamespaceNode (nav);
  215. }
  216. private void XmlNamespaceNode (XPathNavigator nav)
  217. {
  218. string xhtml = "http://www.w3.org/1999/xhtml";
  219. string xmlNS = "http://www.w3.org/XML/1998/namespace";
  220. nav.MoveToFirstChild ();
  221. AssertNavigator ("#1", nav, XPathNodeType.Element,
  222. "", "html", xhtml, "html", "test.", false, true, false);
  223. Assert.IsTrue (nav.MoveToFirstNamespace (XPathNamespaceScope.Local));
  224. AssertNavigator ("#2", nav, XPathNodeType.Namespace,
  225. "", "", "", "", xhtml, false, false, false);
  226. // Test difference between Local, ExcludeXml and All.
  227. Assert.IsTrue (!nav.MoveToNextNamespace (XPathNamespaceScope.Local));
  228. Assert.IsTrue (!nav.MoveToNextNamespace (XPathNamespaceScope.ExcludeXml));
  229. // LAMESPEC: MS.NET 1.0 XmlDocument seems to have some bugs around here.
  230. // see http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q316808
  231. #if true
  232. Assert.IsTrue (nav.MoveToNextNamespace (XPathNamespaceScope.All));
  233. AssertNavigator ("#3", nav, XPathNodeType.Namespace,
  234. "", "xml", "", "xml", xmlNS, false, false, false);
  235. Assert.IsTrue (!nav.MoveToNextNamespace (XPathNamespaceScope.All));
  236. #endif
  237. // Test to check if MoveToRoot() resets Namespace node status.
  238. nav.MoveToRoot ();
  239. AssertNavigator ("#4", nav, XPathNodeType.Root, "", "", "", "", "test.", false, true, false);
  240. nav.MoveToFirstChild ();
  241. // Test without XPathNamespaceScope argument.
  242. Assert.IsTrue (nav.MoveToFirstNamespace ());
  243. Assert.IsTrue (nav.MoveToNextNamespace ());
  244. AssertNavigator ("#5", nav, XPathNodeType.Namespace,
  245. "", "xml", "", "xml", xmlNS, false, false, false);
  246. // Test MoveToParent()
  247. Assert.IsTrue (nav.MoveToParent ());
  248. AssertNavigator ("#6", nav, XPathNodeType.Element,
  249. "", "html", xhtml, "html", "test.", false, true, false);
  250. nav.MoveToFirstChild (); // body
  251. // Test difference between Local and ExcludeXml
  252. Assert.IsTrue (!nav.MoveToFirstNamespace (XPathNamespaceScope.Local), "Local should fail");
  253. Assert.IsTrue (nav.MoveToFirstNamespace (XPathNamespaceScope.ExcludeXml), "ExcludeXml should succeed");
  254. AssertNavigator ("#7", nav, XPathNodeType.Namespace,
  255. "", "", "", "", xhtml, false, false, false);
  256. Assert.IsTrue (nav.MoveToNextNamespace (XPathNamespaceScope.All));
  257. AssertNavigator ("#8", nav, XPathNodeType.Namespace,
  258. "", "xml", "", "xml", xmlNS, false, false, false);
  259. Assert.IsTrue (nav.MoveToParent ());
  260. AssertNavigator ("#9", nav, XPathNodeType.Element,
  261. "", "body", xhtml, "body", "test.", false, true, false);
  262. nav.MoveToRoot ();
  263. AssertNavigator ("#10", nav, XPathNodeType.Root, "", "", "", "", "test.", false, true, false);
  264. }
  265. [Test]
  266. public void MoveToNamespaces ()
  267. {
  268. string xml = "<a xmlns:x='urn:x'><b xmlns:y='urn:y'/><c/><d><e attr='a'/></d></a>";
  269. nav = XDocument.Parse (xml).CreateNavigator ();
  270. MoveToNamespaces (nav);
  271. }
  272. private void MoveToNamespaces (XPathNavigator nav)
  273. {
  274. XPathNodeIterator iter = nav.Select ("//e");
  275. iter.MoveNext ();
  276. nav.MoveTo (iter.Current);
  277. Assert.AreEqual ("e", nav.Name, "#1");
  278. nav.MoveToFirstNamespace ();
  279. Assert.AreEqual ("x", nav.Name, "#2");
  280. nav.MoveToNextNamespace ();
  281. Assert.AreEqual ("xml", nav.Name, "#3");
  282. }
  283. [Test]
  284. public void IsDescendant ()
  285. {
  286. string xml = "<a><b/><c/><d><e attr='a'/></d></a>";
  287. nav = XDocument.Parse (xml).CreateNavigator ();
  288. IsDescendant (nav);
  289. }
  290. private void IsDescendant (XPathNavigator nav)
  291. {
  292. XPathNavigator tmp = nav.Clone ();
  293. XPathNodeIterator iter = nav.Select ("//e");
  294. iter.MoveNext ();
  295. Assert.IsTrue (nav.MoveTo (iter.Current), "#1");
  296. Assert.IsTrue (nav.MoveToFirstAttribute (), "#2");
  297. Assert.AreEqual ("attr", nav.Name, "#3");
  298. Assert.AreEqual ("", tmp.Name, "#4");
  299. Assert.IsTrue (tmp.IsDescendant (nav), "#5");
  300. Assert.IsTrue (!nav.IsDescendant (tmp), "#6");
  301. tmp.MoveToFirstChild ();
  302. Assert.AreEqual ("a", tmp.Name, "#7");
  303. Assert.IsTrue (tmp.IsDescendant (nav), "#8");
  304. Assert.IsTrue (!nav.IsDescendant (tmp), "#9");
  305. tmp.MoveTo (iter.Current);
  306. Assert.AreEqual ("e", tmp.Name, "#10");
  307. Assert.IsTrue (tmp.IsDescendant (nav), "#11");
  308. Assert.IsTrue (!nav.IsDescendant (tmp), "#12");
  309. }
  310. [Test]
  311. public void LiterallySplitText ()
  312. {
  313. string xml = "<root><![CDATA[test]]> string</root>";
  314. nav = XDocument.Parse (xml).CreateNavigator ();
  315. LiterallySplitText (nav);
  316. }
  317. private void LiterallySplitText (XPathNavigator nav)
  318. {
  319. nav.MoveToFirstChild ();
  320. nav.MoveToFirstChild ();
  321. Assert.AreEqual (XPathNodeType.Text, nav.NodeType, "#1");
  322. Assert.AreEqual ("test string", nav.Value, "#2");
  323. }
  324. // bug #75609
  325. [Test]
  326. public void SelectChildren ()
  327. {
  328. string xml = "<root><foo xmlns='urn:foo' /><ns:foo xmlns:ns='urn:foo' /></root>";
  329. nav = XDocument.Parse (xml).CreateNavigator ();
  330. SelectChildrenNS (nav);
  331. }
  332. private void SelectChildrenNS (XPathNavigator nav)
  333. {
  334. nav.MoveToFirstChild (); // root
  335. XPathNodeIterator iter = nav.SelectChildren ("foo", "urn:foo");
  336. Assert.AreEqual (2, iter.Count, "#1");
  337. }
  338. #if NET_2_0
  339. [Test]
  340. // bug #78067
  341. public void OuterXml ()
  342. {
  343. string xml = @"<?xml version=""1.0""?>
  344. <one>
  345. <two>Some data.</two>
  346. </one>";
  347. nav = XDocument.Parse (xml).CreateNavigator ();
  348. OuterXml (nav);
  349. }
  350. private void OuterXml (XPathNavigator nav)
  351. {
  352. string ret = @"<one>
  353. <two>Some data.</two>
  354. </one>";
  355. Assert.AreEqual (ret, nav.OuterXml.Replace ("\r\n", "\n"), "#1");
  356. }
  357. [Test]
  358. public void ReadSubtreeLookupNamespace ()
  359. {
  360. string xml = "<x:foo xmlns:x='urn:x'><bar>x:val</bar></x:foo>";
  361. var doc = new XmlDocument ();
  362. doc.LoadXml (xml);
  363. XPathNavigator nav = doc.LastChild.LastChild.CreateNavigator ();
  364. var xr = nav.ReadSubtree ();
  365. xr.MoveToContent ();
  366. xr.Read (); // should be at x:val
  367. Assert.AreEqual ("urn:x", xr.LookupNamespace ("x"), "#1");
  368. }
  369. #endif
  370. [Test]
  371. public void GetNamespaceConsistentTree ()
  372. {
  373. 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>";
  374. nav = XDocument.Parse (xml).CreateNavigator ();
  375. GetNamespaceConsistentTree (nav);
  376. }
  377. private void GetNamespaceConsistentTree (XPathNavigator nav)
  378. {
  379. nav.MoveToFirstChild ();
  380. nav.MoveToFirstChild ();
  381. nav.MoveToNext ();
  382. Assert.AreEqual (String.Empty, nav.GetNamespace (""), "#1." + nav.GetType ());
  383. nav.MoveToNext ();
  384. nav.MoveToNext ();
  385. Assert.AreEqual ("", nav.GetNamespace (""), "#2." + nav.GetType ());
  386. }
  387. [Test] // bug #2383
  388. public void EvaluateNodeSetAsEnumerable ()
  389. {
  390. String xml = "<root a='value'/>";
  391. XDocument d = XDocument.Parse (xml);
  392. IEnumerable att = (IEnumerable) d.XPathEvaluate ("/root/@a");
  393. att.Cast<XAttribute> ().FirstOrDefault ();
  394. }
  395. [Test] // bug #2146
  396. public void RemoveDoesSnapshotCopy ()
  397. {
  398. var xml = XElement.Parse ("<p><n>one</n><n>two</n><n>three</n></p>");
  399. xml.Elements ().Last ().NodesBeforeSelf ().Remove ();
  400. }
  401. [Test] // bug #2146
  402. public void RemoveDoesSnapshotCopy2 ()
  403. {
  404. var xml = XElement.Parse ("<p><n>one</n><n>two</n><n>three</n></p>");
  405. xml.Elements ().First ().NodesAfterSelf ().Remove ();
  406. Assert.IsTrue (!xml.ToString ().Contains ("three"), "#1");
  407. }
  408. }
  409. }