2002-12-01 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[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                         try 
136                         {
137                                 document.InsertAfter(document.CreateElement("BAD_MAN"), docelem);
138                                 Fail("#InsertAfter.BadPositionButNoError.1");
139                         }
140                         catch(XmlException) {}
141                 }
142
143                 public void TestPrependChild()
144                 {
145                         document = new XmlDocument();
146                         document.LoadXml("<root><sub1 /><sub2 /></root>");
147                         XmlElement docelem = document.DocumentElement;
148                         docelem.PrependChild(document.CreateElement("prepender"));
149                         AssertEquals("PrependChild", "prepender", docelem.FirstChild.Name);
150                 }
151
152                 public void saveTestRemoveAll ()
153                 {
154                         // TODO:  put this test back in when AttributeCollection.RemoveAll() is implemented.
155                         element.AppendChild(element2);
156                         removed = false;
157                         removing = false;
158                         element.RemoveAll ();
159                         Assert (removed);
160                         Assert (removing);
161                 }
162
163                 public void TestRemoveChild ()
164                 {
165                         element.AppendChild(element2);
166                         removed = false;
167                         removing = false;
168                         element.RemoveChild (element2);
169                         Assert (removed);
170                         Assert (removing);
171                 }
172         }
173 }