2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.XML / Test / System.Xml / XmlAttributeCollectionTests.cs
1 // XmlAttributeCollectionTests.cs : Tests for the XmlAttributeCollection class
2 //
3 // Author: Matt Hunter <xrkune@tconl.com>
4 // Author: Martin Willemoes Hansen <mwh@sysrq.dk>
5 //
6 // (C) 2002 Matt Hunter
7 // (C) 2003 Martin Willemoes Hansen
8
9 using System;
10 using System.Xml;
11 using System.Text;
12 using System.IO;
13 using System.Collections;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Xml
18 {
19         [TestFixture]
20         public class XmlAttributeCollectionTests
21         {
22                 private XmlDocument document;
23
24                 [SetUp]
25                 public void GetReady()
26                 {
27                         document = new XmlDocument ();
28                 }
29
30                 [Test]
31                 public void RemoveAll ()
32                 {
33                         StringBuilder xml = new StringBuilder ();
34                         xml.Append ("<?xml version=\"1.0\" ?><library><book type=\"non-fiction\" price=\"34.95\"> ");
35                         xml.Append ("<title type=\"intro\">XML Fun</title> " );
36                         xml.Append ("<author>John Doe</author></book></library>");
37
38                         MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
39                         document = new XmlDocument ();
40                         document.Load (memoryStream);
41                         XmlNodeList bookList = document.GetElementsByTagName ("book");
42                         XmlNode xmlNode = bookList.Item (0);
43                         XmlElement xmlElement = xmlNode as XmlElement;
44                         XmlAttributeCollection attributes = xmlElement.Attributes;
45                         attributes.RemoveAll ();
46                         Assert.AreEqual (false, xmlElement.HasAttribute ("type"), "not all attributes removed.");
47                 }
48
49                 [Test]
50                 public void Append () 
51                 {
52                         XmlDocument xmlDoc = new XmlDocument ();
53                         XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
54                         XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
55                         XmlNode xmlNode = xmlDoc.CreateNode (XmlNodeType.Attribute, "attr3", "namespace1");
56                         XmlAttribute xmlAttribute3 = xmlNode as XmlAttribute;
57                         XmlAttributeCollection attributeCol = xmlEl.Attributes;
58                         xmlAttribute3 = attributeCol.Append (xmlAttribute3);
59                         Assert.AreEqual (true, xmlAttribute3.Name.Equals ("attr3"), "attribute name not properly created.");
60                         Assert.AreEqual (true, xmlAttribute3.NamespaceURI.Equals ("namespace1"), "attribute namespace not properly created.");
61                 }
62
63                 [Test]
64                 public void CopyTo () 
65                 {
66                         XmlDocument xmlDoc = new XmlDocument ();
67                         xmlDoc.LoadXml("<root a1='garnet' a2='amethyst' a3='Bloodstone' a4='diamond' a5='emerald' a6='pearl' a7='ruby' a8='sapphire' a9='moonstone' a10='opal' a11='topaz' a12='turquoize' />");
68                         XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
69                         XmlAttribute[] array = new XmlAttribute[24];
70                         col.CopyTo(array, 0);
71                         Assert.AreEqual ("garnet", array[0].Value);
72                         Assert.AreEqual ("moonstone", array[8].Value);
73                         Assert.AreEqual ("turquoize", array[11].Value);
74                         col.CopyTo(array, 12);
75                         Assert.AreEqual ("garnet", array[12].Value);
76                         Assert.AreEqual ("moonstone", array[20].Value);
77                         Assert.AreEqual ("turquoize", array[23].Value);
78                 }
79
80                 [Test]
81                 public void SetNamedItem ()
82                 {
83                         XmlDocument xmlDoc = new XmlDocument ();
84                         xmlDoc.LoadXml("<root />");
85                         XmlElement el = xmlDoc.DocumentElement;
86                         XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
87
88                         XmlAttribute attr = xmlDoc.CreateAttribute("b3");
89                         attr.Value = "bloodstone";
90                         col.SetNamedItem(attr);
91                         Assert.AreEqual ("bloodstone", el.GetAttribute("b3"), "SetNamedItem.Normal");
92
93                         attr = xmlDoc.CreateAttribute("b3");
94                         attr.Value = "aquamaline";
95                         col.SetNamedItem(attr);
96                         Assert.AreEqual ("aquamaline", el.GetAttribute("b3"), "SetNamedItem.Override");
97                         Assert.AreEqual (1, el.Attributes.Count, "SetNamedItem.Override.Count.1");
98                         Assert.AreEqual (1, col.Count, "SetNamedItem.Override.Count.2");
99                 }
100
101                 [Test]
102                 public void InsertBeforeAfterPrepend () 
103                 {
104                         XmlDocument xmlDoc = new XmlDocument ();
105                         xmlDoc.LoadXml("<root b2='amethyst' />");
106                         XmlElement el = xmlDoc.DocumentElement;
107                         XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
108                         XmlAttribute attr = xmlDoc.CreateAttribute("b1");
109                         attr.Value = "garnet";
110                         col.InsertAfter(attr, null);
111                         Assert.AreEqual ("garnet", el.GetAttributeNode("b1").Value, "InsertAfterNull");
112                         Assert.AreEqual (el.GetAttribute("b1"), col[0].Value, "InsertAfterNull.Pos");
113
114                         attr = xmlDoc.CreateAttribute("b3");
115                         attr.Value = "bloodstone";
116                         col.InsertAfter(attr, el.GetAttributeNode("b2"));
117                         Assert.AreEqual ("bloodstone", el.GetAttributeNode("b3").Value, "InsertAfterAttr");
118                         Assert.AreEqual (el.GetAttribute("b3"), col[2].Value, "InsertAfterAttr.Pos");
119
120                         attr = xmlDoc.CreateAttribute("b4");
121                         attr.Value = "diamond";
122                         col.InsertBefore(attr, null);
123                         Assert.AreEqual ("diamond", el.GetAttributeNode("b4").Value, "InsertBeforeNull");
124                         Assert.AreEqual (el.GetAttribute("b4"), col[3].Value, "InsertBeforeNull.Pos");
125
126                         attr = xmlDoc.CreateAttribute("warning");
127                         attr.Value = "mixed modern and traditional;-)";
128                         col.InsertBefore(attr, el.GetAttributeNode("b1"));
129                         Assert.AreEqual ("mixed modern and traditional;-)", el.GetAttributeNode("warning").Value, "InsertBeforeAttr");
130                         Assert.AreEqual (el.GetAttributeNode("warning").Value, col[0].Value, "InsertBeforeAttr.Pos");
131
132                         attr = xmlDoc.CreateAttribute("about");
133                         attr.Value = "lists of birthstone.";
134                         col.Prepend(attr);
135                         Assert.AreEqual ("lists of birthstone.", col[0].Value, "Prepend");
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (ArgumentException))]
140                 public void InsertAfterError ()
141                 {
142                         this.document.LoadXml ("<root><elem a='1'/></root>");
143                         XmlAttribute attr = document.CreateAttribute ("foo");
144                         attr.Value = "test";
145                         document.DocumentElement.Attributes.InsertAfter (attr, document.DocumentElement.FirstChild.Attributes [0]);
146                 }
147
148                 [Test]
149                 public void InsertAfterReplacesInCorrectOrder ()
150                 {
151                         XmlDocument testDoc = new XmlDocument ();
152                         XmlElement testElement = testDoc.CreateElement ("TestElement" );
153                         testDoc.AppendChild (testElement);
154
155                         XmlAttribute testAttr1 = testDoc.CreateAttribute ("TestAttribute1");
156                         testAttr1.Value = "First attribute";
157                         testElement.Attributes.Prepend (testAttr1);
158
159                         XmlAttribute testAttr2 = testDoc.CreateAttribute ("TestAttribute2");
160                         testAttr2.Value = "Second attribute";
161                         testElement.Attributes.InsertAfter (testAttr2, testAttr1);
162
163                         XmlAttribute testAttr3 = testDoc.CreateAttribute ("TestAttribute3");
164                         testAttr3.Value = "Third attribute";
165                         testElement.Attributes.InsertAfter (testAttr3, testAttr2);
166
167                         XmlAttribute outOfOrder = testDoc.CreateAttribute ("TestAttribute2");
168                         outOfOrder.Value = "Should still be second attribute";
169                         testElement.Attributes.InsertAfter (outOfOrder, testElement.Attributes [0]);
170
171                         Assert.AreEqual ("First attribute", testElement.Attributes [0].Value);
172                         Assert.AreEqual ("Should still be second attribute", testElement.Attributes [1].Value);
173                         Assert.AreEqual ("Third attribute", testElement.Attributes [2].Value);
174                 }
175
176                 [Test]
177                 public void Remove ()
178                 {
179                         XmlDocument xmlDoc = new XmlDocument ();
180                         xmlDoc.LoadXml("<root a1='garnet' a2='amethyst' a3='bloodstone' a4='diamond' a5='emerald' a6='pearl' a7='ruby' a8='sapphire' a9='moonstone' a10='opal' a11='topaz' a12='turquoize' />");
181                         XmlElement el = xmlDoc.DocumentElement;
182                         XmlAttributeCollection col = el.Attributes;
183
184                         // Remove
185                         XmlAttribute attr = col.Remove(el.GetAttributeNode("a12"));
186                         Assert.AreEqual (11, col.Count, "Remove");
187                         Assert.AreEqual ("a12", attr.Name, "Remove.Removed");
188
189                         // RemoveAt
190                         attr = col.RemoveAt(5);
191                         Assert.AreEqual (null, el.GetAttributeNode("a6"), "RemoveAt");
192                         Assert.AreEqual ("pearl", attr.Value, "Remove.Removed");
193                 }
194
195                 [Test]
196 #if NET_2_0
197                 [Category ("NotDotNet")] // enbug
198 #endif
199                 public void RemoveDefaultAttribute ()
200                 {
201                         string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root attr CDATA 'default'>]>";
202                         string xml = dtd + "<root/>";
203                         XmlDocument doc = new XmlDocument();
204                         doc.LoadXml (xml);
205
206                         doc.DocumentElement.Attributes ["attr"].Value = "modified";
207                         doc.DocumentElement.RemoveAttribute("attr");
208
209                         XmlAttribute defAttr = doc.DocumentElement.Attributes ["attr"];
210                         Assert.IsNotNull (defAttr);
211                         Assert.AreEqual ("default", defAttr.Value);
212
213                         defAttr.Value = "default"; // same value as default
214                         Assert.AreEqual (true, defAttr.Specified);
215                 }
216
217                 [Test]
218                 public void AddIdenticalAttrToTheSameNode ()
219                 {
220                         XmlDocument doc = new XmlDocument ();
221                         doc.LoadXml (@"<blah><br><mynode txt='blah/drinks1'/></br><br><mynode txt='hello world'/></br></blah>");
222                         XmlAttribute a = doc.CreateAttribute ("MyAttribute");
223                         doc.SelectNodes ("//mynode") [0].Attributes.Append (a);
224                         doc.SelectNodes ("//mynode") [0].Attributes.Append (a);
225                 }
226         }
227 }