2 // System.Xml.XmlNodeTests
5 // Kral Ferch <kral_ferch@hotmail.com>
15 using NUnit.Framework;
17 namespace MonoTests.System.Xml
19 public class XmlNodeTests : TestCase
21 public XmlNodeTests () : base ("MonoTests.System.Xml.XmlNodeTests testsuite") {}
22 public XmlNodeTests (string name) : base (name) {}
32 protected override void SetUp ()
34 document = new XmlDocument ();
35 document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
36 document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInserting);
37 document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
38 document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemoving);
39 element = document.CreateElement ("foo");
40 element2 = document.CreateElement ("bar");
43 private void EventNodeInserted(Object sender, XmlNodeChangedEventArgs e)
48 private void EventNodeInserting (Object sender, XmlNodeChangedEventArgs e)
53 private void EventNodeRemoved(Object sender, XmlNodeChangedEventArgs e)
58 private void EventNodeRemoving (Object sender, XmlNodeChangedEventArgs e)
63 public void TestAppendChild ()
69 element.AppendChild (element2);
73 // Can only append to elements, documents, and attributes
76 comment = document.CreateComment ("baz");
77 comment.AppendChild (element2);
78 Fail ("Expected an InvalidOperationException to be thrown.");
80 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);
87 element2 = document2.CreateElement ("qux");
88 element.AppendChild (element2);
89 Fail ("Expected an ArgumentException to be thrown.");
91 catch (ArgumentException) {}
92 AssertEquals (1, element.ChildNodes.Count);
94 // Can't append to a readonly node.
95 /* TODO put this in when I figure out how to create a read-only node.
98 XmlElement element3 = (XmlElement)element.CloneNode (false);
99 Assert (!element.IsReadOnly);
100 Assert (element3.IsReadOnly);
101 element2 = document.CreateElement ("quux");
102 element3.AppendChild (element2);
103 Fail ("Expected an ArgumentException to be thrown.");
105 catch (ArgumentException) {}
109 public void TestInsertBefore()
111 document = new XmlDocument();
112 document.LoadXml("<root><sub /></root>");
113 XmlElement docelem = document.DocumentElement;
114 docelem.InsertBefore(document.CreateElement("good_child"), docelem.FirstChild);
115 AssertEquals("InsertBefore.Normal", "good_child", docelem.FirstChild.Name);
117 document.InsertBefore(document.CreateElement("BAD_MAN"), docelem);
118 Fail("#InsertBefore.BadPositionButNoError.1");
120 catch(XmlException) {}
123 public void TestInsertAfter()
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 AssertEquals("InsertAfter.Normal", 3, docelem.ChildNodes.Count);
131 AssertEquals("InsertAfter.First", "sub1", docelem.FirstChild.Name);
132 AssertEquals("InsertAfter.Last", "sub2", docelem.LastChild.Name);
133 AssertEquals("InsertAfter.Prev", "good_child", docelem.FirstChild.NextSibling.Name);
134 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 AssertEquals("InsertAfter with bad location",
138 "<root><sub1 /><good_child /><sub2 /></root><BAD_MAN />",
142 public void TestPrependChild()
144 document = new XmlDocument();
145 document.LoadXml("<root><sub1 /><sub2 /></root>");
146 XmlElement docelem = document.DocumentElement;
147 docelem.PrependChild(document.CreateElement("prepender"));
148 AssertEquals("PrependChild", "prepender", docelem.FirstChild.Name);
151 public void saveTestRemoveAll ()
153 // TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
154 element.AppendChild(element2);
157 element.RemoveAll ();
162 public void TestRemoveChild ()
164 element.AppendChild(element2);
167 element.RemoveChild (element2);
172 public void TestGetPrefixOfNamespace ()
174 document.LoadXml ("<root><c1 xmlns='urn:foo'><c2 xmlns:foo='urn:foo' xmlns='urn:bar'><c3 xmlns=''/></c2></c1></root>");
175 AssertEquals ("root", String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"));
176 AssertEquals ("c1", String.Empty, document.DocumentElement.GetPrefixOfNamespace ("urn:foo"));
177 AssertEquals ("c2", String.Empty, document.DocumentElement.FirstChild.GetPrefixOfNamespace ("urn:foo"));
178 AssertEquals ("c3", "foo", document.DocumentElement.FirstChild.FirstChild.GetPrefixOfNamespace ("urn:foo"));