XPathEditableNavigatorTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. //
  2. // XPathEditableNavigatorTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. using System;
  10. using System.Xml;
  11. using System.Xml.XPath;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Xml.XPath
  14. {
  15. [TestFixture]
  16. public class XPathEditableNavigatorTests
  17. {
  18. private XPathNavigator GetInstance (string xml)
  19. {
  20. XmlDocument doc = new XmlDocument ();
  21. doc.LoadXml (xml);
  22. return doc.CreateNavigator ();
  23. }
  24. private static void AssertNavigator (string label, XPathNavigator nav, XPathNodeType type, string prefix, string localName, string ns, string name, string value, bool hasAttributes, bool hasChildren, bool isEmptyElement)
  25. {
  26. label += nav.GetType ();
  27. Assert.AreEqual (type, nav.NodeType, label + "NodeType");
  28. Assert.AreEqual (prefix, nav.Prefix, label + "Prefix");
  29. Assert.AreEqual (localName, nav.LocalName, label + "LocalName");
  30. Assert.AreEqual (ns, nav.NamespaceURI, label + "Namespace");
  31. Assert.AreEqual (name, nav.Name, label + "Name");
  32. Assert.AreEqual (value, nav.Value, label + "Value");
  33. Assert.AreEqual (hasAttributes, nav.HasAttributes, label + "HasAttributes");
  34. Assert.AreEqual (hasChildren, nav.HasChildren, label + "HasChildren");
  35. Assert.AreEqual (isEmptyElement, nav.IsEmptyElement, label + "IsEmptyElement");
  36. }
  37. [Test]
  38. [ExpectedException (typeof (XmlException))]
  39. public void AppendChildStartDocumentInvalid ()
  40. {
  41. XPathNavigator nav = GetInstance (String.Empty);
  42. XmlWriter w = nav.AppendChild ();
  43. w.WriteStartDocument ();
  44. w.Close ();
  45. }
  46. [Test]
  47. [ExpectedException (typeof (XmlException))]
  48. public void AppendChildStartAttributeInvalid ()
  49. {
  50. XPathNavigator nav = GetInstance (String.Empty);
  51. XmlWriter w = nav.AppendChild ();
  52. // Seems like it is just ignored.
  53. w.WriteStartAttribute ("test");
  54. w.WriteEndAttribute ();
  55. w.Close ();
  56. Assert.AreEqual (XPathNodeType.Root, nav.NodeType, "#1");
  57. Assert.IsFalse (nav.MoveToFirstChild (), "#2");
  58. }
  59. [Test]
  60. [ExpectedException (typeof (XmlException))]
  61. public void AppendChildElementIncomplete ()
  62. {
  63. XPathNavigator nav = GetInstance (String.Empty);
  64. XmlWriter w = nav.AppendChild ();
  65. w.WriteStartElement ("foo");
  66. w.Close ();
  67. }
  68. [Test]
  69. // empty content is allowed.
  70. public void AppendChildEmptyString ()
  71. {
  72. XPathNavigator nav = GetInstance ("<root/>");
  73. nav.MoveToFirstChild (); // root
  74. nav.AppendChild (String.Empty);
  75. }
  76. [Test]
  77. public void AppendChildElement ()
  78. {
  79. XPathNavigator nav = GetInstance ("<root/>");
  80. nav.MoveToFirstChild ();
  81. XmlWriter w = nav.AppendChild ();
  82. w.WriteStartElement ("foo");
  83. w.WriteEndElement ();
  84. w.Close ();
  85. Assert.IsTrue (nav.MoveToFirstChild ());
  86. AssertNavigator ("#1", nav,
  87. XPathNodeType.Element,
  88. String.Empty, // Prefix
  89. "foo", // LocalName
  90. String.Empty, // NamespaceURI
  91. "foo", // Name
  92. String.Empty, // Value
  93. false, // HasAttributes
  94. false, // HasChildren
  95. true); // IsEmptyElement
  96. }
  97. [Test]
  98. public void AppendChildStringFragment ()
  99. {
  100. // check that the input string inherits
  101. // namespace context.
  102. XPathNavigator nav = GetInstance ("<root xmlns='urn:foo'/>");
  103. nav.MoveToFirstChild ();
  104. nav.AppendChild ("<child/>fragment<child></child>");
  105. Assert.IsTrue (nav.MoveToFirstChild (), "#1-1");
  106. AssertNavigator ("#1-2", nav,
  107. XPathNodeType.Element,
  108. String.Empty, // Prefix
  109. "child", // LocalName
  110. "urn:foo", // NamespaceURI
  111. "child", // Name
  112. String.Empty, // Value
  113. false, // HasAttributes
  114. false, // HasChildren
  115. true); // IsEmptyElement
  116. Assert.IsFalse (nav.MoveToFirstChild (), "#2-1");
  117. Assert.IsTrue (nav.MoveToNext (), "#2-2");
  118. AssertNavigator ("#2-3", nav,
  119. XPathNodeType.Text,
  120. String.Empty, // Prefix
  121. String.Empty, // LocalName
  122. String.Empty, // NamespaceURI
  123. String.Empty, // Name
  124. "fragment", // Value
  125. false, // HasAttributes
  126. false, // HasChildren
  127. false); // IsEmptyElement
  128. Assert.IsTrue (nav.MoveToNext (), "#3-1");
  129. AssertNavigator ("#3-2", nav,
  130. XPathNodeType.Element,
  131. String.Empty, // Prefix
  132. "child", // LocalName
  133. "urn:foo", // NamespaceURI
  134. "child", // Name
  135. String.Empty, // Value
  136. false, // HasAttributes
  137. false, // HasChildren
  138. false); // IsEmptyElement
  139. }
  140. [Test]
  141. public void AppendChildStringInvalidFragment ()
  142. {
  143. XPathNavigator nav = GetInstance ("<root xmlns='urn:foo'/>");
  144. nav.MoveToFirstChild ();
  145. nav.AppendChild ("<?xml version='1.0'?><root/>");
  146. }
  147. [Test]
  148. [ExpectedException (typeof (InvalidOperationException))]
  149. public void AppendChildToTextNode ()
  150. {
  151. XPathNavigator nav = GetInstance ("<root>text</root>");
  152. nav.MoveToFirstChild ();
  153. nav.MoveToFirstChild ();
  154. XmlWriter w = nav.AppendChild ();
  155. }
  156. [Test]
  157. public void InsertAfter ()
  158. {
  159. XPathNavigator nav = GetInstance ("<root>test</root>");
  160. nav.MoveToFirstChild ();
  161. nav.MoveToFirstChild ();
  162. nav.InsertAfter ("<blah/><doh>sample</doh>");
  163. AssertNavigator ("#1", nav,
  164. XPathNodeType.Text,
  165. String.Empty, // Prefix
  166. String.Empty, // LocalName
  167. String.Empty, // NamespaceURI
  168. String.Empty, // Name
  169. "test", // Value
  170. false, // HasAttributes
  171. false, // HasChildren
  172. false); // IsEmptyElement
  173. Assert.IsTrue (nav.MoveToNext (), "#2");
  174. AssertNavigator ("#2-2", nav,
  175. XPathNodeType.Element,
  176. String.Empty, // Prefix
  177. "blah", // LocalName
  178. String.Empty, // NamespaceURI
  179. "blah", // Name
  180. String.Empty, // Value
  181. false, // HasAttributes
  182. false, // HasChildren
  183. true); // IsEmptyElement
  184. Assert.IsTrue (nav.MoveToNext (), "#3");
  185. AssertNavigator ("#3-2", nav,
  186. XPathNodeType.Element,
  187. String.Empty, // Prefix
  188. "doh", // LocalName
  189. String.Empty, // NamespaceURI
  190. "doh", // Name
  191. "sample", // Value
  192. false, // HasAttributes
  193. true, // HasChildren
  194. false); // IsEmptyElement
  195. }
  196. [Test]
  197. [ExpectedException (typeof (InvalidOperationException))]
  198. public void InsertAfterRoot ()
  199. {
  200. XPathNavigator nav = GetInstance ("<root/>");
  201. nav.InsertAfter ();
  202. }
  203. [Test]
  204. [ExpectedException (typeof (InvalidOperationException))]
  205. public void InsertAfterAttribute ()
  206. {
  207. XPathNavigator nav = GetInstance ("<root a='b'/>");
  208. nav.MoveToFirstChild ();
  209. nav.MoveToFirstAttribute ();
  210. nav.InsertAfter ();
  211. }
  212. [Test]
  213. [ExpectedException (typeof (InvalidOperationException))]
  214. public void InsertAfterNamespace ()
  215. {
  216. XPathNavigator nav = GetInstance ("<root xmlns='urn:foo'/>");
  217. nav.MoveToFirstChild ();
  218. nav.MoveToFirstNamespace ();
  219. nav.InsertAfter ();
  220. }
  221. [Test]
  222. [ExpectedException (typeof (InvalidOperationException))]
  223. // xmlns:xml='...', which is likely to have XmlElement or XmlDocument as its node.
  224. public void InsertAfterNamespace2 ()
  225. {
  226. XPathNavigator nav = GetInstance ("<root />");
  227. nav.MoveToFirstChild ();
  228. nav.MoveToFirstNamespace ();
  229. nav.InsertAfter ();
  230. }
  231. [Test]
  232. // empty content is allowed.
  233. public void InsertAfterEmptyString ()
  234. {
  235. XPathNavigator nav = GetInstance ("<root/>");
  236. nav.MoveToFirstChild (); // root
  237. nav.InsertAfter (String.Empty);
  238. }
  239. [Test]
  240. public void InsertBefore ()
  241. {
  242. XPathNavigator nav = GetInstance ("<root>test</root>");
  243. nav.MoveToFirstChild ();
  244. nav.MoveToFirstChild ();
  245. nav.InsertBefore ("<blah/><doh>sample</doh>");
  246. AssertNavigator ("#1", nav,
  247. XPathNodeType.Text,
  248. String.Empty, // Prefix
  249. String.Empty, // LocalName
  250. String.Empty, // NamespaceURI
  251. String.Empty, // Name
  252. "test", // Value
  253. false, // HasAttributes
  254. false, // HasChildren
  255. false); // IsEmptyElement
  256. Assert.IsTrue (nav.MoveToFirst (), "#2-1");
  257. AssertNavigator ("#2-2", nav,
  258. XPathNodeType.Element,
  259. String.Empty, // Prefix
  260. "blah", // LocalName
  261. String.Empty, // NamespaceURI
  262. "blah", // Name
  263. String.Empty, // Value
  264. false, // HasAttributes
  265. false, // HasChildren
  266. true); // IsEmptyElement
  267. Assert.IsTrue (nav.MoveToNext (), "#3");
  268. AssertNavigator ("#3-2", nav,
  269. XPathNodeType.Element,
  270. String.Empty, // Prefix
  271. "doh", // LocalName
  272. String.Empty, // NamespaceURI
  273. "doh", // Name
  274. "sample", // Value
  275. false, // HasAttributes
  276. true, // HasChildren
  277. false); // IsEmptyElement
  278. }
  279. [Test]
  280. [ExpectedException (typeof (InvalidOperationException))]
  281. public void InsertBeforeRoot ()
  282. {
  283. XPathNavigator nav = GetInstance ("<root/>");
  284. nav.InsertBefore ();
  285. }
  286. [Test]
  287. [ExpectedException (typeof (InvalidOperationException))]
  288. public void InsertBeforeAttribute ()
  289. {
  290. XPathNavigator nav = GetInstance ("<root a='b'/>");
  291. nav.MoveToFirstChild ();
  292. nav.MoveToFirstAttribute ();
  293. nav.InsertBefore ();
  294. }
  295. [Test]
  296. [ExpectedException (typeof (InvalidOperationException))]
  297. public void InsertBeforeNamespace ()
  298. {
  299. XPathNavigator nav = GetInstance ("<root xmlns='urn:foo'/>");
  300. nav.MoveToFirstChild ();
  301. nav.MoveToFirstNamespace ();
  302. nav.InsertBefore ();
  303. }
  304. [Test]
  305. [ExpectedException (typeof (InvalidOperationException))]
  306. // xmlns:xml='...', which is likely to have XmlElement or XmlDocument as its node.
  307. public void InsertBeforeNamespace2 ()
  308. {
  309. XPathNavigator nav = GetInstance ("<root />");
  310. nav.MoveToFirstChild ();
  311. nav.MoveToFirstNamespace ();
  312. nav.InsertBefore ();
  313. }
  314. [Test]
  315. // empty content is allowed.
  316. public void InsertBeforeEmptyString ()
  317. {
  318. XPathNavigator nav = GetInstance ("<root/>");
  319. nav.MoveToFirstChild (); // root
  320. nav.InsertBefore (String.Empty);
  321. }
  322. [Test]
  323. public void DeleteRange ()
  324. {
  325. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  326. nav.MoveToFirstChild ();
  327. nav.MoveToFirstChild (); // <foo>
  328. XPathNavigator end = nav.Clone ();
  329. end.MoveToNext (); // <next>
  330. end.MoveToNext (); // final
  331. nav.DeleteRange (end);
  332. AssertNavigator ("#1", nav,
  333. XPathNodeType.Element,
  334. String.Empty, // Prefix
  335. "root", // LocalName
  336. String.Empty, // NamespaceURI
  337. "root", // Name
  338. String.Empty, // Value
  339. false, // HasAttributes
  340. false, // HasChildren
  341. false); // IsEmptyElement
  342. }
  343. [Test]
  344. [ExpectedException (typeof (ArgumentNullException))]
  345. public void DeleteRangeNullArg ()
  346. {
  347. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  348. nav.MoveToFirstChild ();
  349. nav.MoveToFirstChild (); // <foo>
  350. nav.DeleteRange (null);
  351. }
  352. [Test]
  353. [ExpectedException (typeof (InvalidOperationException))]
  354. public void DeleteRangeInvalidArg ()
  355. {
  356. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  357. nav.MoveToFirstChild ();
  358. nav.MoveToFirstChild (); // <foo>
  359. XPathNavigator end = nav.Clone ();
  360. end.MoveToNext (); // <next>
  361. end.MoveToFirstChild (); // child
  362. nav.DeleteRange (end);
  363. }
  364. [Test]
  365. public void ReplaceRange ()
  366. {
  367. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  368. nav.MoveToFirstChild ();
  369. nav.MoveToFirstChild (); // <foo>
  370. XPathNavigator end = nav.Clone ();
  371. end.MoveToNext (); // <next>
  372. XmlWriter w = nav.ReplaceRange (end);
  373. AssertNavigator ("#1", nav,
  374. XPathNodeType.Element,
  375. String.Empty, // Prefix
  376. "foo", // LocalName
  377. String.Empty, // NamespaceURI
  378. "foo", // Name
  379. String.Empty, // Value
  380. false, // HasAttributes
  381. true, // HasChildren
  382. false); // IsEmptyElement
  383. Assert.IsTrue (nav.MoveToParent (), "#1-2");
  384. w.WriteStartElement ("whoa");
  385. w.WriteEndElement ();
  386. w.Close ();
  387. AssertNavigator ("#2", nav,
  388. XPathNodeType.Element,
  389. String.Empty, // Prefix
  390. "whoa", // LocalName
  391. String.Empty, // NamespaceURI
  392. "whoa", // Name
  393. String.Empty, // Value
  394. false, // HasAttributes
  395. false, // HasChildren
  396. true); // IsEmptyElement
  397. Assert.IsTrue (nav.MoveToNext (), "#2-1");
  398. AssertNavigator ("#3", nav,
  399. XPathNodeType.Text,
  400. String.Empty, // Prefix
  401. String.Empty, // LocalName
  402. String.Empty, // NamespaceURI
  403. String.Empty, // Name
  404. "final", // Value
  405. false, // HasAttributes
  406. false, // HasChildren
  407. false); // IsEmptyElement
  408. }
  409. [Test]
  410. [ExpectedException (typeof (ArgumentNullException))]
  411. public void ReplaceRangeNullArg ()
  412. {
  413. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  414. nav.MoveToFirstChild ();
  415. nav.MoveToFirstChild (); // <foo>
  416. nav.ReplaceRange (null);
  417. }
  418. [Test]
  419. [ExpectedException (typeof (InvalidOperationException))]
  420. public void ReplaceRangeInvalidArg ()
  421. {
  422. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  423. nav.MoveToFirstChild ();
  424. nav.MoveToFirstChild (); // <foo>
  425. XPathNavigator end = nav.Clone ();
  426. end.MoveToNext (); // <next>
  427. end.MoveToFirstChild (); // child
  428. nav.ReplaceRange (end);
  429. }
  430. [Test]
  431. public void PrependChildXmlReader ()
  432. {
  433. XPathNavigator nav = GetInstance ("<root><foo>existing_child</foo></root>");
  434. nav.MoveToFirstChild ();
  435. nav.MoveToFirstChild (); // foo
  436. XmlReader reader = new XmlTextReader (
  437. "<child>text</child><next_sibling/>",
  438. XmlNodeType.Element,
  439. null);
  440. nav.PrependChild (reader);
  441. XmlAssert.AssertNode ("#0",
  442. reader,
  443. XmlNodeType.None,
  444. 0, // Depth
  445. false, // IsEmptyElement
  446. String.Empty, // Name
  447. String.Empty, // Prefix
  448. String.Empty, // LocalName
  449. String.Empty, // NamespaceURI
  450. String.Empty, // Value
  451. false, // HasValue
  452. 0, // AttributeCount
  453. false); // HasAttributes
  454. AssertNavigator ("#1", nav,
  455. XPathNodeType.Element,
  456. String.Empty, // Prefix
  457. "foo", // LocalName
  458. String.Empty, // NamespaceURI
  459. "foo", // Name
  460. "textexisting_child", // Value
  461. false, // HasAttributes
  462. true, // HasChildren
  463. false); // IsEmptyElement
  464. Assert.IsTrue (nav.MoveToFirstChild (), "#1-2");
  465. AssertNavigator ("#2", nav,
  466. XPathNodeType.Element,
  467. String.Empty, // Prefix
  468. "child", // LocalName
  469. String.Empty, // NamespaceURI
  470. "child", // Name
  471. "text", // Value
  472. false, // HasAttributes
  473. true, // HasChildren
  474. false); // IsEmptyElement
  475. Assert.IsTrue (nav.MoveToNext (), "#2-2");
  476. AssertNavigator ("#3", nav,
  477. XPathNodeType.Element,
  478. String.Empty, // Prefix
  479. "next_sibling", // LocalName
  480. String.Empty, // NamespaceURI
  481. "next_sibling", // Name
  482. String.Empty, // Value
  483. false, // HasAttributes
  484. false, // HasChildren
  485. true); // IsEmptyElement
  486. Assert.IsTrue (nav.MoveToNext (), "#3-2");
  487. AssertNavigator ("#4", nav,
  488. XPathNodeType.Text,
  489. String.Empty, // Prefix
  490. String.Empty, // LocalName
  491. String.Empty, // NamespaceURI
  492. String.Empty, // Name
  493. "existing_child",// Value
  494. false, // HasAttributes
  495. false, // HasChildren
  496. false); // IsEmptyElement
  497. }
  498. [Test]
  499. [ExpectedException (typeof (InvalidOperationException))]
  500. public void PrependChildInvalid ()
  501. {
  502. XPathNavigator nav = GetInstance ("<root><foo>existing_child</foo></root>");
  503. nav.MoveToFirstChild ();
  504. nav.MoveToFirstChild (); // foo
  505. XmlWriter w = nav.PrependChild ();
  506. w.WriteStartAttribute ("whoa");
  507. w.WriteEndAttribute ();
  508. w.Close ();
  509. }
  510. [Test]
  511. // empty content is allowed.
  512. public void PrependChildEmptyString ()
  513. {
  514. XPathNavigator nav = GetInstance ("<root><foo/><bar/><baz/></root>");
  515. nav.MoveToFirstChild ();
  516. nav.MoveToFirstChild (); // foo
  517. nav.MoveToNext (); // bar
  518. nav.PrependChild (String.Empty);
  519. AssertNavigator ("#1", nav,
  520. XPathNodeType.Element,
  521. String.Empty, // Prefix
  522. "bar", // LocalName
  523. String.Empty, // NamespaceURI
  524. "bar", // Name
  525. String.Empty, // Value
  526. false, // HasAttributes
  527. false, // HasChildren
  528. true); // IsEmptyElement
  529. Assert.IsTrue (nav.MoveToFirst (), "#1-2");
  530. AssertNavigator ("#2", nav,
  531. XPathNodeType.Element,
  532. String.Empty, // Prefix
  533. "foo", // LocalName
  534. String.Empty, // NamespaceURI
  535. "foo", // Name
  536. String.Empty, // Value
  537. false, // HasAttributes
  538. false, // HasChildren
  539. true); // IsEmptyElement
  540. }
  541. [Test]
  542. public void ReplaceSelf ()
  543. {
  544. XPathNavigator nav = GetInstance ("<root><foo>existing_child</foo></root>");
  545. nav.MoveToFirstChild ();
  546. nav.MoveToFirstChild (); // foo
  547. nav.ReplaceSelf ("<hijacker>hah, hah</hijacker><next/>");
  548. AssertNavigator ("#1", nav,
  549. XPathNodeType.Element,
  550. String.Empty, // Prefix
  551. "hijacker", // LocalName
  552. String.Empty, // NamespaceURI
  553. "hijacker", // Name
  554. "hah, hah", // Value
  555. false, // HasAttributes
  556. true, // HasChildren
  557. false); // IsEmptyElement
  558. Assert.IsTrue (nav.MoveToNext (), "#1-2");
  559. AssertNavigator ("#2", nav,
  560. XPathNodeType.Element,
  561. String.Empty, // Prefix
  562. "next", // LocalName
  563. String.Empty, // NamespaceURI
  564. "next", // Name
  565. String.Empty, // Value
  566. false, // HasAttributes
  567. false, // HasChildren
  568. true); // IsEmptyElement
  569. }
  570. [Test]
  571. // possible internal behavior difference e.g. due to ReadNode()
  572. public void ReplaceSelfXmlReaderInteractive ()
  573. {
  574. XPathNavigator nav = GetInstance ("<root><foo>existing_child</foo></root>");
  575. nav.MoveToFirstChild ();
  576. nav.MoveToFirstChild (); // foo
  577. XmlReader xr = new XmlTextReader (
  578. "<hijacker>hah, hah</hijacker><next/>",
  579. XmlNodeType.Element,
  580. null);
  581. xr.MoveToContent ();
  582. nav.ReplaceSelf (xr);
  583. AssertNavigator ("#1", nav,
  584. XPathNodeType.Element,
  585. String.Empty, // Prefix
  586. "hijacker", // LocalName
  587. String.Empty, // NamespaceURI
  588. "hijacker", // Name
  589. "hah, hah", // Value
  590. false, // HasAttributes
  591. true, // HasChildren
  592. false); // IsEmptyElement
  593. Assert.IsFalse (nav.MoveToNext (), "#1-2");
  594. }
  595. [Test]
  596. [ExpectedException (typeof (InvalidOperationException))]
  597. // empty content is not allowed
  598. public void ReplaceSelfEmptyString ()
  599. {
  600. XPathNavigator nav = GetInstance ("<root><foo>existing_child</foo></root>");
  601. nav.MoveToFirstChild ();
  602. nav.MoveToFirstChild (); // foo
  603. nav.ReplaceSelf (String.Empty);
  604. }
  605. [Test]
  606. public void SetValueEmptyString ()
  607. {
  608. XPathNavigator nav = GetInstance ("<root><foo>existing_child</foo></root>");
  609. nav.MoveToFirstChild ();
  610. nav.MoveToFirstChild (); // foo
  611. nav.SetValue (String.Empty);
  612. AssertNavigator ("#1", nav,
  613. XPathNodeType.Element,
  614. String.Empty, // Prefix
  615. "foo", // LocalName
  616. String.Empty, // NamespaceURI
  617. "foo", // Name
  618. String.Empty, // Value
  619. false, // HasAttributes
  620. true, // HasChildren
  621. false); // IsEmptyElement
  622. }
  623. [Test]
  624. public void MoveToFollowing ()
  625. {
  626. XPathNavigator end;
  627. XPathNavigator nav = GetInstance ("<root><bar><foo attr='v1'><baz><foo attr='v2'/></baz></foo></bar><dummy/><foo attr='v3'></foo></root>");
  628. Assert.IsTrue (nav.MoveToFollowing ("foo", String.Empty), "#1");
  629. Assert.AreEqual ("v1", nav.GetAttribute ("attr", String.Empty), "#2");
  630. Assert.IsTrue (nav.MoveToFollowing ("foo", String.Empty), "#3");
  631. Assert.AreEqual ("v2", nav.GetAttribute ("attr", String.Empty), "#4");
  632. Assert.IsTrue (nav.MoveToFollowing ("foo", String.Empty), "#5");
  633. Assert.AreEqual ("v3", nav.GetAttribute ("attr", String.Empty), "#6");
  634. // round 2
  635. end = nav.Clone ();
  636. nav.MoveToRoot ();
  637. Assert.IsTrue (nav.MoveToFollowing ("foo", String.Empty, end), "#7");
  638. Assert.AreEqual ("v1", nav.GetAttribute ("attr", String.Empty), "#8");
  639. Assert.IsTrue (nav.MoveToFollowing ("foo", String.Empty, end), "#9");
  640. Assert.AreEqual ("v2", nav.GetAttribute ("attr", String.Empty), "#10");
  641. // end is exclusive
  642. Assert.IsFalse (nav.MoveToFollowing ("foo", String.Empty, end), "#11");
  643. // in this case it never moves to somewhere else.
  644. Assert.AreEqual ("v2", nav.GetAttribute ("attr", String.Empty), "#12");
  645. }
  646. [Test]
  647. public void MoveToFollowingFromAttribute ()
  648. {
  649. XPathNavigator nav = GetInstance ("<root a='b'><foo/></root>");
  650. nav.MoveToFirstChild ();
  651. nav.MoveToFirstAttribute ();
  652. // should first move to owner element and go on.
  653. Assert.IsTrue (nav.MoveToFollowing ("foo", String.Empty));
  654. }
  655. [Test]
  656. public void AppendChildInDocumentFragment ()
  657. {
  658. XmlDocumentFragment f = new XmlDocument ().CreateDocumentFragment ();
  659. XmlWriter w = f.CreateNavigator ().AppendChild ();
  660. w.WriteStartElement ("foo");
  661. w.WriteEndElement ();
  662. w.Close ();
  663. Assert.IsNotNull (f.FirstChild as XmlElement);
  664. }
  665. [Test]
  666. public void CanEdit ()
  667. {
  668. XmlDocument doc = new XmlDocument ();
  669. Assert.IsTrue (doc.CreateNavigator ().CanEdit);
  670. Assert.IsTrue (GetInstance ("<root/>").CanEdit);
  671. }
  672. [Test]
  673. public void DeleteSelfAttribute ()
  674. {
  675. // bug #376210.
  676. XmlDocument document = new XmlDocument ();
  677. document.LoadXml ("<test><node date='2000-12-23'>z</node></test>");
  678. XPathNavigator navigator = document.CreateNavigator ();
  679. XPathNavigator nodeElement = navigator.SelectSingleNode ("//node");
  680. nodeElement.MoveToAttribute ("date", String.Empty);
  681. nodeElement.DeleteSelf ();
  682. Assert.AreEqual ("<test><node>z</node></test>", document.OuterXml);
  683. }
  684. [Test]
  685. [Ignore ("Microsoft reference source causes InvalidOperationException")]
  686. public void WriteAttributeOnAppendedChild ()
  687. {
  688. XmlDocument x = new XmlDocument ();
  689. XmlElement y = x.CreateElement ("test");
  690. using (XmlWriter w = y.CreateNavigator ().AppendChild ())
  691. w.WriteAttributeString ("test", "test1");
  692. }
  693. [Test] // bug #1558
  694. public void CreateNavigatorReturnsEdidable ()
  695. {
  696. var document = new XmlDocument();
  697. document.LoadXml ("<div>hello world</div>");
  698. XPathNavigator navigator = document.CreateNavigator ().CreateNavigator ();
  699. navigator.SelectSingleNode ("//div").SetValue ("hello world 2");
  700. }
  701. }
  702. }