XmlNodeTests.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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
  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. Assertion.Assert (inserted);
  73. Assertion.Assert (inserting);
  74. // Can only append to elements, documents, and attributes
  75. try
  76. {
  77. comment = document.CreateComment ("baz");
  78. comment.AppendChild (element2);
  79. Assertion.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. Assertion.AssertEquals (1, element.ChildNodes.Count);
  85. try
  86. {
  87. element2 = document2.CreateElement ("qux");
  88. element.AppendChild (element2);
  89. Assertion.Fail ("Expected an ArgumentException to be thrown.");
  90. }
  91. catch (ArgumentException) {}
  92. Assertion.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. Assertion.Assert (!element.IsReadOnly);
  99. Assertion.Assert (element3.IsReadOnly);
  100. element2 = document.CreateElement ("quux");
  101. element3.AppendChild (element2);
  102. Assertion.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. Assertion.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. // Assertion.Fail ("#InsertBefore.BadPositionButNoError.1");
  119. // }
  120. // catch (XmlException) {}
  121. }
  122. [Test]
  123. public void InsertAfter()
  124. {
  125. document = new XmlDocument();
  126. document.LoadXml("<root><sub1 /><sub2 /></root>");
  127. XmlElement docelem = document.DocumentElement;
  128. XmlElement newelem = document.CreateElement("good_child");
  129. docelem.InsertAfter(newelem, docelem.FirstChild);
  130. Assertion.AssertEquals("InsertAfter.Normal", 3, docelem.ChildNodes.Count);
  131. Assertion.AssertEquals("InsertAfter.First", "sub1", docelem.FirstChild.Name);
  132. Assertion.AssertEquals("InsertAfter.Last", "sub2", docelem.LastChild.Name);
  133. Assertion.AssertEquals("InsertAfter.Prev", "good_child", docelem.FirstChild.NextSibling.Name);
  134. Assertion.AssertEquals("InsertAfter.Next", "good_child", docelem.LastChild.PreviousSibling.Name);
  135. // this doesn't throw an exception
  136. document.InsertAfter(document.CreateElement("BAD_MAN"), docelem);
  137. Assertion.AssertEquals("InsertAfter with bad location",
  138. "<root><sub1 /><good_child /><sub2 /></root><BAD_MAN />",
  139. document.InnerXml);
  140. }
  141. [Test]
  142. public void PrependChild()
  143. {
  144. document = new XmlDocument();
  145. document.LoadXml("<root><sub1 /><sub2 /></root>");
  146. XmlElement docelem = document.DocumentElement;
  147. docelem.PrependChild(document.CreateElement("prepender"));
  148. Assertion.AssertEquals("PrependChild", "prepender", docelem.FirstChild.Name);
  149. }
  150. public void saveTestRemoveAll ()
  151. {
  152. // TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  153. element.AppendChild(element2);
  154. removed = false;
  155. removing = false;
  156. element.RemoveAll ();
  157. Assertion.Assert (removed);
  158. Assertion.Assert (removing);
  159. }
  160. [Test]
  161. public void RemoveChild ()
  162. {
  163. element.AppendChild(element2);
  164. removed = false;
  165. removing = false;
  166. element.RemoveChild (element2);
  167. Assertion.Assert (removed);
  168. Assertion.Assert (removing);
  169. }
  170. [Test]
  171. public void RemoveLastChild ()
  172. {
  173. element.InnerXml = "<foo/><bar/><baz/>";
  174. element.RemoveChild (element.LastChild);
  175. Assertion.AssertNotNull (element.FirstChild);
  176. }
  177. [Test]
  178. public void GetPrefixOfNamespace ()
  179. {
  180. document.LoadXml ("<root><c1 xmlns='urn:foo'><c2 xmlns:foo='urn:foo' xmlns='urn:bar'><c3 xmlns=''/></c2></c1></root>");
  181. Assertion.AssertEquals ("root", String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"));
  182. Assertion.AssertEquals ("c1", String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"));
  183. Assertion.AssertEquals ("c2", String.Empty, document.DocumentElement.FirstChild.GetPrefixOfNamespace ("urn:foo"));
  184. Assertion.AssertEquals ("c3", "foo", document.DocumentElement.FirstChild.FirstChild.GetPrefixOfNamespace ("urn:foo"));
  185. }
  186. [Test]
  187. public void ReplaceChild ()
  188. {
  189. document.LoadXml ("<root/>");
  190. document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
  191. document.NodeChanged += new XmlNodeChangedEventHandler (this.EventNodeChanged);
  192. document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
  193. inserted = changed = removed = false;
  194. XmlElement el = document.CreateElement("root2");
  195. document.ReplaceChild (el, document.DocumentElement);
  196. Assertion.AssertEquals ("root2", document.DocumentElement.Name);
  197. Assertion.AssertEquals (1, document.ChildNodes.Count);
  198. Assertion.Assert (inserted && removed && !changed);
  199. }
  200. }
  201. }