XmlNodeTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. //
  2. // System.Xml.XmlNodeTests
  3. //
  4. // Authors:
  5. // Kral Ferch <[email protected]>
  6. // Martin Willemoes Hansen
  7. //
  8. // (C) 2002 Kral Ferch
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.IO;
  13. using System.Text;
  14. using System.Xml;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlNodeTests : Assertion
  20. {
  21. XmlDocument document;
  22. XmlElement element;
  23. XmlElement element2;
  24. bool inserted;
  25. bool inserting;
  26. bool changed;
  27. bool changing;
  28. bool removed;
  29. bool removing;
  30. [SetUp]
  31. public void GetReady ()
  32. {
  33. document = new XmlDocument ();
  34. document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
  35. document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInserting);
  36. document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
  37. document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemoving);
  38. element = document.CreateElement ("foo");
  39. element2 = document.CreateElement ("bar");
  40. }
  41. private void EventNodeInserted(Object sender, XmlNodeChangedEventArgs e)
  42. {
  43. inserted = true;
  44. }
  45. private void EventNodeInserting (Object sender, XmlNodeChangedEventArgs e)
  46. {
  47. inserting = true;
  48. }
  49. private void EventNodeChanged(Object sender, XmlNodeChangedEventArgs e)
  50. {
  51. changed = true;
  52. }
  53. private void EventNodeChanging (Object sender, XmlNodeChangedEventArgs e)
  54. {
  55. changing = true;
  56. }
  57. private void EventNodeRemoved(Object sender, XmlNodeChangedEventArgs e)
  58. {
  59. removed = true;
  60. }
  61. private void EventNodeRemoving (Object sender, XmlNodeChangedEventArgs e)
  62. {
  63. removing = true;
  64. }
  65. [Test]
  66. public void AppendChild ()
  67. {
  68. XmlComment comment;
  69. inserted = false;
  70. inserting = false;
  71. element.AppendChild (element2);
  72. Assert (inserted);
  73. Assert (inserting);
  74. // Can only append to elements, documents, and attributes
  75. try
  76. {
  77. comment = document.CreateComment ("baz");
  78. comment.AppendChild (element2);
  79. Fail ("Expected an InvalidOperationException to be thrown.");
  80. }
  81. catch (InvalidOperationException) {}
  82. // Can't append a node from one document into another document.
  83. XmlDocument document2 = new XmlDocument();
  84. AssertEquals (1, element.ChildNodes.Count);
  85. try
  86. {
  87. element2 = document2.CreateElement ("qux");
  88. element.AppendChild (element2);
  89. Fail ("Expected an ArgumentException to be thrown.");
  90. }
  91. catch (ArgumentException) {}
  92. AssertEquals (1, element.ChildNodes.Count);
  93. // Can't append to a readonly node.
  94. /* TODO put this in when I figure out how to create a read-only node.
  95. try
  96. {
  97. XmlElement element3 = (XmlElement)element.CloneNode (false);
  98. Assert (!element.IsReadOnly);
  99. Assert (element3.IsReadOnly);
  100. element2 = document.CreateElement ("quux");
  101. element3.AppendChild (element2);
  102. Fail ("Expected an ArgumentException to be thrown.");
  103. }
  104. catch (ArgumentException) {}
  105. */
  106. }
  107. [Test]
  108. public void GetNamespaceOfPrefix ()
  109. {
  110. document.LoadXml ("<root xmlns='urn:default' attr='value' "
  111. + "xml:lang='en' xmlns:foo='urn:foo' foo:att='fooatt'>text node</root>");
  112. XmlNode n = document.DocumentElement;
  113. AssertEquals ("urn:default", n.GetNamespaceOfPrefix (String.Empty));
  114. AssertEquals ("urn:foo", n.GetNamespaceOfPrefix ("foo"));
  115. AssertEquals (String.Empty, n.GetNamespaceOfPrefix ("bar"));
  116. AssertEquals (String.Empty, n.GetNamespaceOfPrefix ("xml"));
  117. AssertEquals (String.Empty, n.GetNamespaceOfPrefix ("xmlns"));
  118. n = document.DocumentElement.FirstChild;
  119. AssertEquals ("urn:default", n.GetNamespaceOfPrefix (String.Empty));
  120. AssertEquals ("urn:foo", n.GetNamespaceOfPrefix ("foo"));
  121. AssertEquals (String.Empty, n.GetNamespaceOfPrefix ("bar"));
  122. AssertEquals (String.Empty, n.GetNamespaceOfPrefix ("xml"));
  123. AssertEquals (String.Empty, n.GetNamespaceOfPrefix ("xmlns"));
  124. }
  125. [Test]
  126. [ExpectedException (typeof (ArgumentNullException))]
  127. public void GetNamespaceOfPrefixNullArg ()
  128. {
  129. new XmlDocument ().GetNamespaceOfPrefix (null);
  130. }
  131. [Test]
  132. public void InsertBefore()
  133. {
  134. document = new XmlDocument();
  135. document.LoadXml("<root><sub /></root>");
  136. XmlElement docelem = document.DocumentElement;
  137. docelem.InsertBefore(document.CreateElement("good_child"), docelem.FirstChild);
  138. AssertEquals("InsertBefore.Normal", "good_child", docelem.FirstChild.Name);
  139. // These are required for .NET 1.0 but not for .NET 1.1.
  140. try {
  141. document.InsertBefore (document.CreateElement ("BAD_MAN"), docelem);
  142. Fail ("#InsertBefore.BadPositionButNoError.1");
  143. }
  144. catch (Exception) {}
  145. }
  146. [Test]
  147. public void InsertAfter()
  148. {
  149. document = new XmlDocument();
  150. document.LoadXml("<root><sub1 /><sub2 /></root>");
  151. XmlElement docelem = document.DocumentElement;
  152. XmlElement newelem = document.CreateElement("good_child");
  153. docelem.InsertAfter(newelem, docelem.FirstChild);
  154. AssertEquals("InsertAfter.Normal", 3, docelem.ChildNodes.Count);
  155. AssertEquals("InsertAfter.First", "sub1", docelem.FirstChild.Name);
  156. AssertEquals("InsertAfter.Last", "sub2", docelem.LastChild.Name);
  157. AssertEquals("InsertAfter.Prev", "good_child", docelem.FirstChild.NextSibling.Name);
  158. AssertEquals("InsertAfter.Next", "good_child", docelem.LastChild.PreviousSibling.Name);
  159. // this doesn't throw any exception *only on .NET 1.1*
  160. // .NET 1.0 throws an exception.
  161. try {
  162. document.InsertAfter(document.CreateElement("BAD_MAN"), docelem);
  163. #if USE_VERSION_1_1
  164. AssertEquals("InsertAfter with bad location",
  165. "<root><sub1 /><good_child /><sub2 /></root><BAD_MAN />",
  166. document.InnerXml);
  167. } catch (XmlException ex) {
  168. throw ex;
  169. }
  170. #else
  171. } catch (Exception) {}
  172. #endif
  173. }
  174. [Test]
  175. public void Normalize ()
  176. {
  177. XmlDocument doc = new XmlDocument ();
  178. doc.LoadXml ("<root>This is the <b>hardest</b> one.</root>");
  179. doc.NodeInserted += new XmlNodeChangedEventHandler (EventNodeInserted);
  180. doc.NodeChanged += new XmlNodeChangedEventHandler (EventNodeChanged);
  181. doc.NodeRemoved += new XmlNodeChangedEventHandler (EventNodeRemoved);
  182. AssertEquals (3, doc.DocumentElement.ChildNodes.Count);
  183. doc.DocumentElement.Normalize ();
  184. AssertEquals (3, doc.DocumentElement.ChildNodes.Count);
  185. Assert (changed);
  186. inserted = changed = removed = false;
  187. doc.DocumentElement.AppendChild (doc.CreateTextNode ("Addendum."));
  188. AssertEquals (4, doc.DocumentElement.ChildNodes.Count);
  189. inserted = changed = removed = false;
  190. doc.DocumentElement.Normalize ();
  191. AssertEquals (3, doc.DocumentElement.ChildNodes.Count);
  192. Assert (changed);
  193. Assert (removed);
  194. inserted = changed = removed = false;
  195. doc.DocumentElement.SetAttribute ("attr", "");
  196. XmlAttribute attr = doc.DocumentElement.Attributes [0] as XmlAttribute;
  197. AssertEquals (1, attr.ChildNodes.Count);
  198. inserted = changed = removed = false;
  199. attr.Normalize ();
  200. // Such behavior violates DOM Level 2 Node#normalize(),
  201. // but MS DOM is designed as such.
  202. AssertEquals (1, attr.ChildNodes.Count);
  203. }
  204. [Test]
  205. public void Normalize2 ()
  206. {
  207. XmlDocument doc = new XmlDocument ();
  208. doc.PreserveWhitespace = true;
  209. doc.LoadXml ("<root> </root>");
  210. XmlElement root = doc.DocumentElement;
  211. root.AppendChild (doc.CreateTextNode ("foo"));
  212. root.AppendChild (doc.CreateTextNode ("bar"));
  213. root.AppendChild (doc.CreateWhitespace (" "));
  214. root.AppendChild (doc.CreateTextNode ("baz"));
  215. doc.NodeInserted += new XmlNodeChangedEventHandler (OnChange);
  216. doc.NodeChanged += new XmlNodeChangedEventHandler (OnChange);
  217. doc.NodeRemoved += new XmlNodeChangedEventHandler (OnChange);
  218. AssertEquals ("Before Normalize()", 5, root.ChildNodes.Count);
  219. root.Normalize ();
  220. AssertEquals ("<root> foobar baz</root>", root.OuterXml);
  221. AssertEquals ("After Normalize()", 1, root.ChildNodes.Count);
  222. }
  223. int normalize2Count;
  224. private void OnChange (object o, XmlNodeChangedEventArgs e)
  225. {
  226. switch (normalize2Count) {
  227. case 0:
  228. AssertEquals ("Action0", XmlNodeChangedAction.Remove, e.Action);
  229. AssertEquals ("Value0", " ", e.Node.Value);
  230. break;
  231. case 1:
  232. AssertEquals ("Action1", XmlNodeChangedAction.Remove, e.Action);
  233. AssertEquals ("Value1", "bar", e.Node.Value);
  234. break;
  235. case 2:
  236. AssertEquals ("Action2", XmlNodeChangedAction.Remove, e.Action);
  237. AssertEquals ("Value2", " ", e.Node.Value);
  238. break;
  239. case 3:
  240. AssertEquals ("Action3", XmlNodeChangedAction.Remove, e.Action);
  241. AssertEquals ("Value3", "baz", e.Node.Value);
  242. break;
  243. case 4:
  244. AssertEquals ("Action4", XmlNodeChangedAction.Change, e.Action);
  245. AssertEquals ("Value4", " foobar baz", e.Node.Value);
  246. break;
  247. default:
  248. Fail (String.Format ("Unexpected event. Action = {0}, node type = {1}, node name = {2}, node value = {3}", e.Action, e.Node.NodeType, e.Node.Name, e.Node.Value));
  249. break;
  250. }
  251. normalize2Count++;
  252. }
  253. [Test]
  254. public void PrependChild()
  255. {
  256. document = new XmlDocument();
  257. document.LoadXml("<root><sub1 /><sub2 /></root>");
  258. XmlElement docelem = document.DocumentElement;
  259. docelem.PrependChild(document.CreateElement("prepender"));
  260. AssertEquals("PrependChild", "prepender", docelem.FirstChild.Name);
  261. }
  262. public void saveTestRemoveAll ()
  263. {
  264. // TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  265. element.AppendChild(element2);
  266. removed = false;
  267. removing = false;
  268. element.RemoveAll ();
  269. Assert (removed);
  270. Assert (removing);
  271. }
  272. [Test]
  273. public void RemoveChild ()
  274. {
  275. element.AppendChild(element2);
  276. removed = false;
  277. removing = false;
  278. element.RemoveChild (element2);
  279. Assert (removed);
  280. Assert (removing);
  281. }
  282. [Test]
  283. public void RemoveLastChild ()
  284. {
  285. element.InnerXml = "<foo/><bar/><baz/>";
  286. element.RemoveChild (element.LastChild);
  287. AssertNotNull (element.FirstChild);
  288. }
  289. [Test]
  290. public void GetPrefixOfNamespace ()
  291. {
  292. document.LoadXml ("<root><c1 xmlns='urn:foo'><c2 xmlns:foo='urn:foo' xmlns='urn:bar'><c3 xmlns=''/></c2></c1></root>");
  293. AssertEquals ("root", String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"));
  294. AssertEquals ("c1", String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"));
  295. AssertEquals ("c2", String.Empty, document.DocumentElement.FirstChild.GetPrefixOfNamespace ("urn:foo"));
  296. AssertEquals ("c3", "foo", document.DocumentElement.FirstChild.FirstChild.GetPrefixOfNamespace ("urn:foo"));
  297. // disconnected nodes.
  298. XmlNode n = document.CreateElement ("foo");
  299. AssertEquals (String.Empty, n.GetPrefixOfNamespace ("foo"));
  300. n = document.CreateTextNode ("text"); // does not have Attributes
  301. AssertEquals (String.Empty, n.GetPrefixOfNamespace ("foo"));
  302. n = document.CreateXmlDeclaration ("1.0", null, null); // does not have Attributes
  303. AssertEquals (String.Empty, n.GetPrefixOfNamespace ("foo"));
  304. }
  305. [Test]
  306. public void ReplaceChild ()
  307. {
  308. document.LoadXml ("<root/>");
  309. document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
  310. document.NodeChanged += new XmlNodeChangedEventHandler (this.EventNodeChanged);
  311. document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
  312. inserted = changed = removed = false;
  313. XmlElement el = document.CreateElement("root2");
  314. document.ReplaceChild (el, document.DocumentElement);
  315. AssertEquals ("root2", document.DocumentElement.Name);
  316. AssertEquals (1, document.ChildNodes.Count);
  317. Assert (inserted && removed && !changed);
  318. }
  319. [Test]
  320. public void InnerText ()
  321. {
  322. document.LoadXml ("<root>This is <b>mixed</b> content. Also includes <![CDATA[CDATA section]]>.<!-- Should be ignored --></root>");
  323. string total = "This is mixed content. Also includes CDATA section.";
  324. XmlNode elemB = document.DocumentElement.ChildNodes [1];
  325. AssertEquals ("mixed", elemB.FirstChild.InnerText); // text node
  326. AssertEquals ("mixed", elemB.InnerText); // element b
  327. AssertEquals (total, document.DocumentElement.InnerText); // element root
  328. AssertEquals (total, document.InnerText); // whole document
  329. }
  330. [Test]
  331. public void InnerXmlWithXmlns ()
  332. {
  333. XmlDocument document = new XmlDocument ();
  334. XmlElement xel = document.CreateElement ("KeyValue", "http://www.w3.org/2000/09/xmldsig#");
  335. xel.SetAttribute ("xmlns", "http://www.w3.org/2000/09/xmldsig#");
  336. xel.InnerXml = "<DSAKeyValue>blablabla</DSAKeyValue>";
  337. string expected = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue>blablabla</DSAKeyValue></KeyValue>";
  338. AssertEquals (expected, xel.OuterXml);
  339. }
  340. [Test]
  341. public void SelectNodes ()
  342. {
  343. // This test is done in this class since it tests only XmlDocumentNavigator.
  344. string xpath = "//@*|//namespace::*";
  345. XmlDocument doc = new XmlDocument ();
  346. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  347. XmlNodeList nl = doc.SelectNodes (xpath);
  348. AssertEquals (6, nl.Count);
  349. // BTW, as for namespace nodes, Node does not exist
  350. // in the tree, so the return value should be
  351. // implementation dependent.
  352. AssertEquals ("#1", XmlNodeType.Attribute, nl [0].NodeType);
  353. AssertEquals ("#2", XmlNodeType.Attribute, nl [1].NodeType);
  354. AssertEquals ("#3", XmlNodeType.Attribute, nl [2].NodeType);
  355. AssertEquals ("#4", XmlNodeType.Attribute, nl [3].NodeType);
  356. AssertEquals ("#5", XmlNodeType.Attribute, nl [4].NodeType);
  357. AssertEquals ("#6", XmlNodeType.Attribute, nl [5].NodeType);
  358. AssertEquals ("xmlns", nl [0].LocalName);
  359. AssertEquals ("xml", nl [1].LocalName);
  360. AssertEquals ("xmlns", nl [2].LocalName);
  361. AssertEquals ("xml", nl [3].LocalName);
  362. AssertEquals ("xmlns", nl [4].LocalName);
  363. AssertEquals ("xml", nl [5].LocalName);
  364. }
  365. [Test]
  366. [Ignore ("MS.NET has a bug; it fails to return nodes in document order.")]
  367. public void SelectNodes2 ()
  368. {
  369. // This test is done in this class since it tests only XmlDocumentNavigator.
  370. string xpath = "//*|//@*|//namespace::*";
  371. XmlDocument doc = new XmlDocument ();
  372. doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
  373. XmlNodeList nl = doc.SelectNodes (xpath);
  374. AssertEquals (9, nl.Count);
  375. AssertEquals (XmlNodeType.Element, nl [0].NodeType);
  376. AssertEquals (XmlNodeType.Attribute, nl [1].NodeType);
  377. AssertEquals (XmlNodeType.Attribute, nl [2].NodeType);
  378. AssertEquals (XmlNodeType.Element, nl [3].NodeType);
  379. AssertEquals (XmlNodeType.Attribute, nl [4].NodeType);
  380. AssertEquals (XmlNodeType.Attribute, nl [5].NodeType);
  381. AssertEquals (XmlNodeType.Element, nl [6].NodeType);
  382. AssertEquals (XmlNodeType.Attribute, nl [7].NodeType);
  383. AssertEquals (XmlNodeType.Attribute, nl [8].NodeType);
  384. AssertEquals ("element", nl [0].LocalName);
  385. AssertEquals ("xmlns", nl [1].LocalName);
  386. AssertEquals ("xml", nl [2].LocalName);
  387. AssertEquals ("foo", nl [3].LocalName);
  388. AssertEquals ("xmlns", nl [4].LocalName);
  389. AssertEquals ("xml", nl [5].LocalName);
  390. AssertEquals ("bar", nl [6].LocalName);
  391. AssertEquals ("xmlns", nl [7].LocalName);
  392. AssertEquals ("xml", nl [8].LocalName);
  393. }
  394. [Test]
  395. public void BaseURI ()
  396. {
  397. // See bug #64120.
  398. XmlDocument doc = new XmlDocument ();
  399. doc.Load ("Test/XmlFiles/simple.xml");
  400. XmlElement el = doc.CreateElement ("foo");
  401. AssertEquals (String.Empty, el.BaseURI);
  402. doc.DocumentElement.AppendChild (el);
  403. Assert (String.Empty != el.BaseURI);
  404. XmlAttribute attr = doc.CreateAttribute ("attr");
  405. AssertEquals (String.Empty, attr.BaseURI);
  406. }
  407. [Test]
  408. public void CloneReadonlyNode ()
  409. {
  410. // Clone() should return such node that is not readonly
  411. string dtd = "<!DOCTYPE root ["
  412. + "<!ELEMENT root (#PCDATA|foo)*>"
  413. + "<!ELEMENT foo EMPTY>"
  414. + "<!ENTITY ent1 '<foo /><![CDATA[cdata]]>'>]>";
  415. string xml = dtd + "<root>&ent1;</root>";
  416. XmlDocument doc = new XmlDocument ();
  417. doc.LoadXml (xml);
  418. XmlNode n = doc.DocumentElement.FirstChild.FirstChild;
  419. Assert ("#1", n.IsReadOnly);
  420. Assert ("#2", !n.CloneNode (true).IsReadOnly);
  421. }
  422. }
  423. }