2
0

XmlNodeTests.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 InsertBefore()
  109. {
  110. document = new XmlDocument();
  111. document.LoadXml("<root><sub /></root>");
  112. XmlElement docelem = document.DocumentElement;
  113. docelem.InsertBefore(document.CreateElement("good_child"), docelem.FirstChild);
  114. AssertEquals("InsertBefore.Normal", "good_child", docelem.FirstChild.Name);
  115. // These are required for .NET 1.0 but not for .NET 1.1.
  116. try {
  117. document.InsertBefore (document.CreateElement ("BAD_MAN"), docelem);
  118. #if !USE_VERSION_1_1
  119. Fail ("#InsertBefore.BadPositionButNoError.1");
  120. #endif
  121. }
  122. #if USE_VERSION_1_1
  123. catch (XmlException ex) {
  124. throw ex;
  125. }
  126. #else
  127. catch (Exception) {}
  128. #endif
  129. }
  130. [Test]
  131. public void InsertAfter()
  132. {
  133. document = new XmlDocument();
  134. document.LoadXml("<root><sub1 /><sub2 /></root>");
  135. XmlElement docelem = document.DocumentElement;
  136. XmlElement newelem = document.CreateElement("good_child");
  137. docelem.InsertAfter(newelem, docelem.FirstChild);
  138. AssertEquals("InsertAfter.Normal", 3, docelem.ChildNodes.Count);
  139. AssertEquals("InsertAfter.First", "sub1", docelem.FirstChild.Name);
  140. AssertEquals("InsertAfter.Last", "sub2", docelem.LastChild.Name);
  141. AssertEquals("InsertAfter.Prev", "good_child", docelem.FirstChild.NextSibling.Name);
  142. AssertEquals("InsertAfter.Next", "good_child", docelem.LastChild.PreviousSibling.Name);
  143. // this doesn't throw any exception *only on .NET 1.1*
  144. // .NET 1.0 throws an exception.
  145. try {
  146. document.InsertAfter(document.CreateElement("BAD_MAN"), docelem);
  147. #if USE_VERSION_1_1
  148. AssertEquals("InsertAfter with bad location",
  149. "<root><sub1 /><good_child /><sub2 /></root><BAD_MAN />",
  150. document.InnerXml);
  151. } catch (XmlException ex) {
  152. throw ex;
  153. }
  154. #else
  155. } catch (Exception) {}
  156. #endif
  157. }
  158. [Test]
  159. public void PrependChild()
  160. {
  161. document = new XmlDocument();
  162. document.LoadXml("<root><sub1 /><sub2 /></root>");
  163. XmlElement docelem = document.DocumentElement;
  164. docelem.PrependChild(document.CreateElement("prepender"));
  165. AssertEquals("PrependChild", "prepender", docelem.FirstChild.Name);
  166. }
  167. public void saveTestRemoveAll ()
  168. {
  169. // TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  170. element.AppendChild(element2);
  171. removed = false;
  172. removing = false;
  173. element.RemoveAll ();
  174. Assert (removed);
  175. Assert (removing);
  176. }
  177. [Test]
  178. public void RemoveChild ()
  179. {
  180. element.AppendChild(element2);
  181. removed = false;
  182. removing = false;
  183. element.RemoveChild (element2);
  184. Assert (removed);
  185. Assert (removing);
  186. }
  187. [Test]
  188. public void RemoveLastChild ()
  189. {
  190. element.InnerXml = "<foo/><bar/><baz/>";
  191. element.RemoveChild (element.LastChild);
  192. AssertNotNull (element.FirstChild);
  193. }
  194. [Test]
  195. public void GetPrefixOfNamespace ()
  196. {
  197. document.LoadXml ("<root><c1 xmlns='urn:foo'><c2 xmlns:foo='urn:foo' xmlns='urn:bar'><c3 xmlns=''/></c2></c1></root>");
  198. AssertEquals ("root", String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"));
  199. AssertEquals ("c1", String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"));
  200. AssertEquals ("c2", String.Empty, document.DocumentElement.FirstChild.GetPrefixOfNamespace ("urn:foo"));
  201. AssertEquals ("c3", "foo", document.DocumentElement.FirstChild.FirstChild.GetPrefixOfNamespace ("urn:foo"));
  202. }
  203. [Test]
  204. public void ReplaceChild ()
  205. {
  206. document.LoadXml ("<root/>");
  207. document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
  208. document.NodeChanged += new XmlNodeChangedEventHandler (this.EventNodeChanged);
  209. document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
  210. inserted = changed = removed = false;
  211. XmlElement el = document.CreateElement("root2");
  212. document.ReplaceChild (el, document.DocumentElement);
  213. AssertEquals ("root2", document.DocumentElement.Name);
  214. AssertEquals (1, document.ChildNodes.Count);
  215. Assert (inserted && removed && !changed);
  216. }
  217. [Test]
  218. public void InnerText ()
  219. {
  220. document.LoadXml ("<root>This is <b>mixed</b> content. Also includes <![CDATA[CDATA section]]>.<!-- Should be ignored --></root>");
  221. string total = "This is mixed content. Also includes CDATA section.";
  222. XmlNode elemB = document.DocumentElement.ChildNodes [1];
  223. AssertEquals ("mixed", elemB.FirstChild.InnerText); // text node
  224. AssertEquals ("mixed", elemB.InnerText); // element b
  225. AssertEquals (total, document.DocumentElement.InnerText); // element root
  226. AssertEquals (total, document.InnerText); // whole document
  227. }
  228. [Test]
  229. public void InnerXmlWithXmlns ()
  230. {
  231. XmlDocument document = new XmlDocument ();
  232. XmlElement xel = document.CreateElement ("KeyValue", "http://www.w3.org/2000/09/xmldsig#");
  233. xel.SetAttribute ("xmlns", "http://www.w3.org/2000/09/xmldsig#");
  234. xel.InnerXml = "<DSAKeyValue>blablabla</DSAKeyValue>";
  235. string expected = "<KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue>blablabla</DSAKeyValue></KeyValue>";
  236. AssertEquals (expected, xel.OuterXml);
  237. }
  238. }
  239. }