XmlNodeTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. try
  120. {
  121. document.InsertAfter(document.CreateElement("BAD_MAN"), docelem);
  122. Fail("#InsertAfter.BadPositionButNoError.1");
  123. }
  124. catch(XmlException) {}
  125. }
  126. public void TestPrependChild()
  127. {
  128. document = new XmlDocument();
  129. document.LoadXml("<root><sub1 /><sub2 /></root>");
  130. XmlElement docelem = document.DocumentElement;
  131. docelem.PrependChild(document.CreateElement("prepender"));
  132. AssertEquals("PrependChild", "prepender", docelem.FirstChild.Name);
  133. }
  134. public void saveTestRemoveAll ()
  135. {
  136. // TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  137. element.AppendChild(element2);
  138. removed = false;
  139. removing = false;
  140. element.RemoveAll ();
  141. Assert (removed);
  142. Assert (removing);
  143. }
  144. public void TestRemoveChild ()
  145. {
  146. element.AppendChild(element2);
  147. removed = false;
  148. removing = false;
  149. element.RemoveChild (element2);
  150. Assert (removed);
  151. Assert (removing);
  152. }
  153. }
  154. }