XmlNodeTests.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // System.Xml.XmlNodeTests
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. using System.Xml;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Xml
  15. {
  16. public class XmlNodeTests : TestCase
  17. {
  18. public XmlNodeTests () : base ("MonoTests.System.Xml.XmlNodeTests testsuite") {}
  19. public XmlNodeTests (string name) : base (name) {}
  20. XmlDocument document;
  21. XmlElement element;
  22. XmlElement element2;
  23. bool inserted;
  24. bool inserting;
  25. bool removed;
  26. bool removing;
  27. protected override void SetUp ()
  28. {
  29. document = new XmlDocument ();
  30. document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
  31. document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInserting);
  32. document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
  33. document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemoving);
  34. element = document.CreateElement ("foo");
  35. element2 = document.CreateElement ("bar");
  36. }
  37. private void EventNodeInserted(Object sender, XmlNodeChangedEventArgs e)
  38. {
  39. inserted = true;
  40. }
  41. private void EventNodeInserting (Object sender, XmlNodeChangedEventArgs e)
  42. {
  43. inserting = true;
  44. }
  45. private void EventNodeRemoved(Object sender, XmlNodeChangedEventArgs e)
  46. {
  47. removed = true;
  48. }
  49. private void EventNodeRemoving (Object sender, XmlNodeChangedEventArgs e)
  50. {
  51. removing = true;
  52. }
  53. public void TestAppendChild ()
  54. {
  55. XmlComment comment;
  56. inserted = false;
  57. inserting = false;
  58. element.AppendChild (element2);
  59. Assert (inserted);
  60. Assert (inserting);
  61. // Can only append to elements, documents, and attributes
  62. try
  63. {
  64. comment = document.CreateComment ("baz");
  65. comment.AppendChild (element2);
  66. Fail ("Expected an InvalidOperationException to be thrown.");
  67. }
  68. catch (InvalidOperationException) {}
  69. // Can't append a node from one document into another document.
  70. XmlDocument document2 = new XmlDocument();
  71. AssertEquals (1, element.ChildNodes.Count);
  72. try
  73. {
  74. element2 = document2.CreateElement ("qux");
  75. element.AppendChild (element2);
  76. Fail ("Expected an ArgumentException to be thrown.");
  77. }
  78. catch (ArgumentException) {}
  79. AssertEquals (1, element.ChildNodes.Count);
  80. // Can't append to a readonly node.
  81. /* TODO put this in when I figure out how to create a read-only node.
  82. try
  83. {
  84. XmlElement element3 = (XmlElement)element.CloneNode (false);
  85. Assert (!element.IsReadOnly);
  86. Assert (element3.IsReadOnly);
  87. element2 = document.CreateElement ("quux");
  88. element3.AppendChild (element2);
  89. Fail ("Expected an ArgumentException to be thrown.");
  90. }
  91. catch (ArgumentException) {}
  92. */
  93. }
  94. public void TestInsertBefore()
  95. {
  96. document = new XmlDocument();
  97. document.LoadXml("<root><sub /></root>");
  98. XmlElement docelem = document.DocumentElement;
  99. docelem.InsertBefore(document.CreateElement("good_child"), docelem.FirstChild);
  100. AssertEquals("InsertBefore.Normal", "good_child", docelem.FirstChild.Name);
  101. try {
  102. document.InsertBefore(document.CreateElement("BAD_MAN"), docelem);
  103. Fail("#InsertBefore.BadPositionButNoError.1");
  104. }
  105. catch(XmlException) {}
  106. }
  107. public void TestInsertAfter()
  108. {
  109. document = new XmlDocument();
  110. document.LoadXml("<root><sub1 /><sub2 /></root>");
  111. XmlElement docelem = document.DocumentElement;
  112. XmlElement newelem = document.CreateElement("good_child");
  113. docelem.InsertAfter(newelem, docelem.FirstChild);
  114. AssertEquals("InsertAfter.Normal", 3, docelem.ChildNodes.Count);
  115. AssertEquals("InsertAfter.First", "sub1", docelem.FirstChild.Name);
  116. AssertEquals("InsertAfter.Last", "sub2", docelem.LastChild.Name);
  117. AssertEquals("InsertAfter.Prev", "good_child", docelem.FirstChild.NextSibling.Name);
  118. AssertEquals("InsertAfter.Next", "good_child", docelem.LastChild.PreviousSibling.Name);
  119. // this doesn't throw an exception
  120. document.InsertAfter(document.CreateElement("BAD_MAN"), docelem);
  121. AssertEquals("InsertAfter with bad location",
  122. "<root><sub1 /><good_child /><sub2 /></root><BAD_MAN />",
  123. document.InnerXml);
  124. }
  125. public void TestPrependChild()
  126. {
  127. document = new XmlDocument();
  128. document.LoadXml("<root><sub1 /><sub2 /></root>");
  129. XmlElement docelem = document.DocumentElement;
  130. docelem.PrependChild(document.CreateElement("prepender"));
  131. AssertEquals("PrependChild", "prepender", docelem.FirstChild.Name);
  132. }
  133. public void saveTestRemoveAll ()
  134. {
  135. // TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  136. element.AppendChild(element2);
  137. removed = false;
  138. removing = false;
  139. element.RemoveAll ();
  140. Assert (removed);
  141. Assert (removing);
  142. }
  143. public void TestRemoveChild ()
  144. {
  145. element.AppendChild(element2);
  146. removed = false;
  147. removing = false;
  148. element.RemoveChild (element2);
  149. Assert (removed);
  150. Assert (removing);
  151. }
  152. public void TestGetPrefixOfNamespace ()
  153. {
  154. document.LoadXml ("<root><c1 xmlns='urn:foo'><c2 xmlns:foo='urn:foo' xmlns='urn:bar'><c3 xmlns=''/></c2></c1></root>");
  155. AssertEquals ("root", String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"));
  156. AssertEquals ("c1", String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"));
  157. AssertEquals ("c2", String.Empty, document.DocumentElement.FirstChild.GetPrefixOfNamespace ("urn:foo"));
  158. AssertEquals ("c3", "foo", document.DocumentElement.FirstChild.FirstChild.GetPrefixOfNamespace ("urn:foo"));
  159. }
  160. }
  161. }