sorry, forgot to add it.
[mono.git] / mcs / class / System.XML / Test / XmlNodeTests.cs
1 //
2 // System.Xml.XmlNodeTests
3 //
4 // Author:
5 //   Kral Ferch <kral_ferch@hotmail.com>
6 //
7 // (C) 2002 Kral Ferch
8 //
9
10 using System;
11 using System.IO;
12 using System.Text;
13 using System.Xml;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Xml
18 {
19         public class XmlNodeTests : TestCase
20         {
21                 public XmlNodeTests () : base ("MonoTests.System.Xml.XmlNodeTests testsuite") {}
22                 public XmlNodeTests (string name) : base (name) {}
23
24                 XmlDocument document;
25                 XmlElement element;
26                 XmlElement element2;
27                 bool inserted;
28                 bool inserting;
29                 bool removed;
30                 bool removing;
31
32                 protected override void SetUp ()
33                 {
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");
41                 }
42
43                 private void EventNodeInserted(Object sender, XmlNodeChangedEventArgs e)
44                 {
45                         inserted = true;
46                 }
47
48                 private void EventNodeInserting (Object sender, XmlNodeChangedEventArgs e)
49                 {
50                         inserting = true;
51                 }
52
53                 private void EventNodeRemoved(Object sender, XmlNodeChangedEventArgs e)
54                 {
55                         removed = true;
56                 }
57
58                 private void EventNodeRemoving (Object sender, XmlNodeChangedEventArgs e)
59                 {
60                         removing = true;
61                 }
62
63                 public void TestAppendChild ()
64                 {
65                         XmlComment comment;
66
67                         inserted = false;
68                         inserting = false;
69                         element.AppendChild (element2);
70                         Assert (inserted);
71                         Assert (inserting);
72
73                         // Can only append to elements, documents, and attributes
74                         try 
75                         {
76                                 comment = document.CreateComment ("baz");
77                                 comment.AppendChild (element2);
78                                 Fail ("Expected an InvalidOperationException to be thrown.");
79                         } 
80                         catch (InvalidOperationException) {}
81
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
94                         // Can't append to a readonly node.
95 /* TODO put this in when I figure out how to create a read-only node.
96                         try 
97                         {
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.");
104                         } 
105                         catch (ArgumentException) {}
106 */
107                 }
108
109                 public void TestInsertBefore()
110                 {
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);
116                         try {
117                                 document.InsertBefore(document.CreateElement("BAD_MAN"), docelem);
118                                 Fail("#InsertBefore.BadPositionButNoError.1");
119                         }
120                         catch(XmlException) {}
121                 }
122
123                 public void TestInsertAfter()
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                         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 />",
139                                 document.InnerXml);
140 }
141
142                 public void TestPrependChild()
143                 {
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);
149                 }
150
151                 public void saveTestRemoveAll ()
152                 {
153                         // TODO:  put this test back in when AttributeCollection.RemoveAll() is implemented.
154                         element.AppendChild(element2);
155                         removed = false;
156                         removing = false;
157                         element.RemoveAll ();
158                         Assert (removed);
159                         Assert (removing);
160                 }
161
162                 public void TestRemoveChild ()
163                 {
164                         element.AppendChild(element2);
165                         removed = false;
166                         removing = false;
167                         element.RemoveChild (element2);
168                         Assert (removed);
169                         Assert (removing);
170                 }
171                 
172                 public void TestGetPrefixOfNamespace ()
173                 {
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"));
179
180                 }
181         }
182 }