XPathEditableNavigatorTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. public void AppendChildElement ()
  71. {
  72. XPathNavigator nav = GetInstance ("<root/>");
  73. nav.MoveToFirstChild ();
  74. XmlWriter w = nav.AppendChild ();
  75. w.WriteStartElement ("foo");
  76. w.WriteEndElement ();
  77. w.Close ();
  78. Assert.IsTrue (nav.MoveToFirstChild ());
  79. AssertNavigator ("#1", nav,
  80. XPathNodeType.Element,
  81. String.Empty, // Prefix
  82. "foo", // LocalName
  83. String.Empty, // NamespaceURI
  84. "foo", // Name
  85. String.Empty, // Value
  86. false, // HasAttributes
  87. false, // HasChildren
  88. true); // IsEmptyElement
  89. }
  90. [Test]
  91. public void AppendChildStringFragment ()
  92. {
  93. // check that the input string inherits
  94. // namespace context.
  95. XPathNavigator nav = GetInstance ("<root xmlns='urn:foo'/>");
  96. nav.MoveToFirstChild ();
  97. nav.AppendChild ("<child/>fragment<child></child>");
  98. Assert.IsTrue (nav.MoveToFirstChild (), "#1-1");
  99. AssertNavigator ("#1-2", nav,
  100. XPathNodeType.Element,
  101. String.Empty, // Prefix
  102. "child", // LocalName
  103. "urn:foo", // NamespaceURI
  104. "child", // Name
  105. String.Empty, // Value
  106. false, // HasAttributes
  107. false, // HasChildren
  108. true); // IsEmptyElement
  109. Assert.IsFalse (nav.MoveToFirstChild (), "#2-1");
  110. Assert.IsTrue (nav.MoveToNext (), "#2-2");
  111. AssertNavigator ("#2-3", nav,
  112. XPathNodeType.Text,
  113. String.Empty, // Prefix
  114. String.Empty, // LocalName
  115. String.Empty, // NamespaceURI
  116. String.Empty, // Name
  117. "fragment", // Value
  118. false, // HasAttributes
  119. false, // HasChildren
  120. false); // IsEmptyElement
  121. Assert.IsTrue (nav.MoveToNext (), "#3-1");
  122. AssertNavigator ("#3-2", nav,
  123. XPathNodeType.Element,
  124. String.Empty, // Prefix
  125. "child", // LocalName
  126. "urn:foo", // NamespaceURI
  127. "child", // Name
  128. String.Empty, // Value
  129. false, // HasAttributes
  130. false, // HasChildren
  131. false); // IsEmptyElement
  132. }
  133. [Test]
  134. public void AppendChildStringInvalidFragment ()
  135. {
  136. XPathNavigator nav = GetInstance ("<root xmlns='urn:foo'/>");
  137. nav.MoveToFirstChild ();
  138. nav.AppendChild ("<?xml version='1.0'?><root/>");
  139. }
  140. [Test]
  141. [ExpectedException (typeof (InvalidOperationException))]
  142. public void AppendChildToTextNode ()
  143. {
  144. XPathNavigator nav = GetInstance ("<root>text</root>");
  145. nav.MoveToFirstChild ();
  146. nav.MoveToFirstChild ();
  147. XmlWriter w = nav.AppendChild ();
  148. }
  149. [Test]
  150. public void InsertAfter ()
  151. {
  152. XPathNavigator nav = GetInstance ("<root>test</root>");
  153. nav.MoveToFirstChild ();
  154. nav.MoveToFirstChild ();
  155. nav.InsertAfter ("<blah/><doh>sample</doh>");
  156. AssertNavigator ("#1", nav,
  157. XPathNodeType.Text,
  158. String.Empty, // Prefix
  159. String.Empty, // LocalName
  160. String.Empty, // NamespaceURI
  161. String.Empty, // Name
  162. "test", // Value
  163. false, // HasAttributes
  164. false, // HasChildren
  165. false); // IsEmptyElement
  166. Assert.IsTrue (nav.MoveToNext (), "#2");
  167. AssertNavigator ("#2-2", nav,
  168. XPathNodeType.Element,
  169. String.Empty, // Prefix
  170. "blah", // LocalName
  171. String.Empty, // NamespaceURI
  172. "blah", // Name
  173. String.Empty, // Value
  174. false, // HasAttributes
  175. false, // HasChildren
  176. true); // IsEmptyElement
  177. Assert.IsTrue (nav.MoveToNext (), "#3");
  178. AssertNavigator ("#3-2", nav,
  179. XPathNodeType.Element,
  180. String.Empty, // Prefix
  181. "doh", // LocalName
  182. String.Empty, // NamespaceURI
  183. "doh", // Name
  184. "sample", // Value
  185. false, // HasAttributes
  186. true, // HasChildren
  187. false); // IsEmptyElement
  188. }
  189. [Test]
  190. [ExpectedException (typeof (InvalidOperationException))]
  191. public void InsertAfterRoot ()
  192. {
  193. XPathNavigator nav = GetInstance ("<root/>");
  194. nav.InsertAfter ();
  195. }
  196. [Test]
  197. [ExpectedException (typeof (InvalidOperationException))]
  198. public void InsertAfterAttribute ()
  199. {
  200. XPathNavigator nav = GetInstance ("<root a='b'/>");
  201. nav.MoveToFirstChild ();
  202. nav.MoveToFirstAttribute ();
  203. nav.InsertAfter ();
  204. }
  205. [Test]
  206. [ExpectedException (typeof (InvalidOperationException))]
  207. public void InsertAfterNamespace ()
  208. {
  209. XPathNavigator nav = GetInstance ("<root xmlns='urn:foo'/>");
  210. nav.MoveToFirstChild ();
  211. nav.MoveToFirstNamespace ();
  212. nav.InsertAfter ();
  213. }
  214. [Test]
  215. [ExpectedException (typeof (InvalidOperationException))]
  216. // xmlns:xml='...', which is likely to have XmlElement or XmlDocument as its node.
  217. public void InsertAfterNamespace2 ()
  218. {
  219. XPathNavigator nav = GetInstance ("<root />");
  220. nav.MoveToFirstChild ();
  221. nav.MoveToFirstNamespace ();
  222. nav.InsertAfter ();
  223. }
  224. [Test]
  225. public void InsertBefore ()
  226. {
  227. XPathNavigator nav = GetInstance ("<root>test</root>");
  228. nav.MoveToFirstChild ();
  229. nav.MoveToFirstChild ();
  230. nav.InsertBefore ("<blah/><doh>sample</doh>");
  231. AssertNavigator ("#1", nav,
  232. XPathNodeType.Text,
  233. String.Empty, // Prefix
  234. String.Empty, // LocalName
  235. String.Empty, // NamespaceURI
  236. String.Empty, // Name
  237. "test", // Value
  238. false, // HasAttributes
  239. false, // HasChildren
  240. false); // IsEmptyElement
  241. Assert.IsTrue (nav.MoveToFirst (), "#2-1");
  242. AssertNavigator ("#2-2", nav,
  243. XPathNodeType.Element,
  244. String.Empty, // Prefix
  245. "blah", // LocalName
  246. String.Empty, // NamespaceURI
  247. "blah", // Name
  248. String.Empty, // Value
  249. false, // HasAttributes
  250. false, // HasChildren
  251. true); // IsEmptyElement
  252. Assert.IsTrue (nav.MoveToNext (), "#3");
  253. AssertNavigator ("#3-2", nav,
  254. XPathNodeType.Element,
  255. String.Empty, // Prefix
  256. "doh", // LocalName
  257. String.Empty, // NamespaceURI
  258. "doh", // Name
  259. "sample", // Value
  260. false, // HasAttributes
  261. true, // HasChildren
  262. false); // IsEmptyElement
  263. }
  264. [Test]
  265. [ExpectedException (typeof (InvalidOperationException))]
  266. public void InsertBeforeRoot ()
  267. {
  268. XPathNavigator nav = GetInstance ("<root/>");
  269. nav.InsertBefore ();
  270. }
  271. [Test]
  272. [ExpectedException (typeof (InvalidOperationException))]
  273. public void InsertBeforeAttribute ()
  274. {
  275. XPathNavigator nav = GetInstance ("<root a='b'/>");
  276. nav.MoveToFirstChild ();
  277. nav.MoveToFirstAttribute ();
  278. nav.InsertBefore ();
  279. }
  280. [Test]
  281. [ExpectedException (typeof (InvalidOperationException))]
  282. public void InsertBeforeNamespace ()
  283. {
  284. XPathNavigator nav = GetInstance ("<root xmlns='urn:foo'/>");
  285. nav.MoveToFirstChild ();
  286. nav.MoveToFirstNamespace ();
  287. nav.InsertBefore ();
  288. }
  289. [Test]
  290. [ExpectedException (typeof (InvalidOperationException))]
  291. // xmlns:xml='...', which is likely to have XmlElement or XmlDocument as its node.
  292. public void InsertBeforeNamespace2 ()
  293. {
  294. XPathNavigator nav = GetInstance ("<root />");
  295. nav.MoveToFirstChild ();
  296. nav.MoveToFirstNamespace ();
  297. nav.InsertBefore ();
  298. }
  299. [Test]
  300. public void DeleteRange ()
  301. {
  302. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  303. nav.MoveToFirstChild ();
  304. nav.MoveToFirstChild (); // <foo>
  305. XPathNavigator end = nav.Clone ();
  306. end.MoveToNext (); // <next>
  307. end.MoveToNext (); // final
  308. nav.DeleteRange (end);
  309. AssertNavigator ("#1", nav,
  310. XPathNodeType.Element,
  311. String.Empty, // Prefix
  312. "root", // LocalName
  313. String.Empty, // NamespaceURI
  314. "root", // Name
  315. String.Empty, // Value
  316. false, // HasAttributes
  317. false, // HasChildren
  318. false); // IsEmptyElement
  319. }
  320. [Test]
  321. [ExpectedException (typeof (ArgumentNullException))]
  322. public void DeleteRangeNullArg ()
  323. {
  324. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  325. nav.MoveToFirstChild ();
  326. nav.MoveToFirstChild (); // <foo>
  327. nav.DeleteRange (null);
  328. }
  329. [Test]
  330. [ExpectedException (typeof (InvalidOperationException))]
  331. public void DeleteRangeInvalidArg ()
  332. {
  333. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  334. nav.MoveToFirstChild ();
  335. nav.MoveToFirstChild (); // <foo>
  336. XPathNavigator end = nav.Clone ();
  337. end.MoveToNext (); // <next>
  338. end.MoveToFirstChild (); // child
  339. nav.DeleteRange (end);
  340. }
  341. [Test]
  342. public void ReplaceRange ()
  343. {
  344. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  345. nav.MoveToFirstChild ();
  346. nav.MoveToFirstChild (); // <foo>
  347. XPathNavigator end = nav.Clone ();
  348. end.MoveToNext (); // <next>
  349. XmlWriter w = nav.ReplaceRange (end);
  350. AssertNavigator ("#1", nav,
  351. XPathNodeType.Element,
  352. String.Empty, // Prefix
  353. "foo", // LocalName
  354. String.Empty, // NamespaceURI
  355. "foo", // Name
  356. String.Empty, // Value
  357. false, // HasAttributes
  358. true, // HasChildren
  359. false); // IsEmptyElement
  360. Assert.IsTrue (nav.MoveToParent (), "#1-2");
  361. w.WriteStartElement ("whoa");
  362. w.WriteEndElement ();
  363. w.Close ();
  364. AssertNavigator ("#2", nav,
  365. XPathNodeType.Element,
  366. String.Empty, // Prefix
  367. "whoa", // LocalName
  368. String.Empty, // NamespaceURI
  369. "whoa", // Name
  370. String.Empty, // Value
  371. false, // HasAttributes
  372. false, // HasChildren
  373. true); // IsEmptyElement
  374. Assert.IsTrue (nav.MoveToNext (), "#2-1");
  375. AssertNavigator ("#3", nav,
  376. XPathNodeType.Text,
  377. String.Empty, // Prefix
  378. String.Empty, // LocalName
  379. String.Empty, // NamespaceURI
  380. String.Empty, // Name
  381. "final", // Value
  382. false, // HasAttributes
  383. false, // HasChildren
  384. false); // IsEmptyElement
  385. }
  386. [Test]
  387. [ExpectedException (typeof (ArgumentNullException))]
  388. public void ReplaceRangeNullArg ()
  389. {
  390. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  391. nav.MoveToFirstChild ();
  392. nav.MoveToFirstChild (); // <foo>
  393. nav.ReplaceRange (null);
  394. }
  395. [Test]
  396. [ExpectedException (typeof (InvalidOperationException))]
  397. public void ReplaceRangeInvalidArg ()
  398. {
  399. XPathNavigator nav = GetInstance ("<root><foo><bar/><baz/></foo><next>child<tmp/></next>final</root>");
  400. nav.MoveToFirstChild ();
  401. nav.MoveToFirstChild (); // <foo>
  402. XPathNavigator end = nav.Clone ();
  403. end.MoveToNext (); // <next>
  404. end.MoveToFirstChild (); // child
  405. nav.ReplaceRange (end);
  406. }
  407. [Test]
  408. public void PrependChildXmlReader ()
  409. {
  410. XPathNavigator nav = GetInstance ("<root><foo>existing_child</foo></root>");
  411. nav.MoveToFirstChild ();
  412. nav.MoveToFirstChild (); // foo
  413. XmlReader reader = new XmlTextReader (
  414. "<child>text</child><next_sibling/>",
  415. XmlNodeType.Element,
  416. null);
  417. nav.PrependChild (reader);
  418. XmlAssert.AssertNode ("#0",
  419. reader,
  420. XmlNodeType.None,
  421. 0, // Depth
  422. false, // IsEmptyElement
  423. String.Empty, // Name
  424. String.Empty, // Prefix
  425. String.Empty, // LocalName
  426. String.Empty, // NamespaceURI
  427. String.Empty, // Value
  428. false, // HasValue
  429. 0, // AttributeCount
  430. false); // HasAttributes
  431. AssertNavigator ("#1", nav,
  432. XPathNodeType.Element,
  433. String.Empty, // Prefix
  434. "foo", // LocalName
  435. String.Empty, // NamespaceURI
  436. "foo", // Name
  437. "textexisting_child", // Value
  438. false, // HasAttributes
  439. true, // HasChildren
  440. false); // IsEmptyElement
  441. Assert.IsTrue (nav.MoveToFirstChild (), "#1-2");
  442. AssertNavigator ("#2", nav,
  443. XPathNodeType.Element,
  444. String.Empty, // Prefix
  445. "child", // LocalName
  446. String.Empty, // NamespaceURI
  447. "child", // Name
  448. "text", // Value
  449. false, // HasAttributes
  450. true, // HasChildren
  451. false); // IsEmptyElement
  452. Assert.IsTrue (nav.MoveToNext (), "#2-2");
  453. AssertNavigator ("#3", nav,
  454. XPathNodeType.Element,
  455. String.Empty, // Prefix
  456. "next_sibling", // LocalName
  457. String.Empty, // NamespaceURI
  458. "next_sibling", // Name
  459. String.Empty, // Value
  460. false, // HasAttributes
  461. false, // HasChildren
  462. true); // IsEmptyElement
  463. Assert.IsTrue (nav.MoveToNext (), "#3-2");
  464. AssertNavigator ("#4", nav,
  465. XPathNodeType.Text,
  466. String.Empty, // Prefix
  467. String.Empty, // LocalName
  468. String.Empty, // NamespaceURI
  469. String.Empty, // Name
  470. "existing_child",// Value
  471. false, // HasAttributes
  472. false, // HasChildren
  473. false); // IsEmptyElement
  474. }
  475. [Test]
  476. [ExpectedException (typeof (InvalidOperationException))]
  477. public void PrependChildInvalid ()
  478. {
  479. XPathNavigator nav = GetInstance ("<root><foo>existing_child</foo></root>");
  480. nav.MoveToFirstChild ();
  481. nav.MoveToFirstChild (); // foo
  482. XmlWriter w = nav.PrependChild ();
  483. w.WriteStartAttribute ("whoa");
  484. w.WriteEndAttribute ();
  485. w.Close ();
  486. }
  487. }
  488. }
  489. #endif