XPathEditableNavigatorTests.cs 20 KB

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