Remove fixed testcases from fixme.lst and knownFailures.lst; ignore one testcase...
[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 : Assertion
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                         AssertEquals ("not all attributes removed.", false, xmlElement.HasAttribute ("type"));
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                         AssertEquals ("attribute name not properly created.", true, xmlAttribute3.Name.Equals ("attr3"));
60                         AssertEquals ("attribute namespace not properly created.", true, xmlAttribute3.NamespaceURI.Equals ("namespace1"));
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                         AssertEquals("garnet", array[0].Value);
72                         AssertEquals("moonstone", array[8].Value);
73                         AssertEquals("turquoize", array[11].Value);
74                         col.CopyTo(array, 12);
75                         AssertEquals("garnet", array[12].Value);
76                         AssertEquals("moonstone", array[20].Value);
77                         AssertEquals("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                         AssertEquals("SetNamedItem.Normal", "bloodstone", el.GetAttribute("b3"));
92
93                         attr = xmlDoc.CreateAttribute("b3");
94                         attr.Value = "aquamaline";
95                         col.SetNamedItem(attr);
96                         AssertEquals("SetNamedItem.Override", "aquamaline", el.GetAttribute("b3"));
97                         AssertEquals("SetNamedItem.Override.Count.1", 1, el.Attributes.Count);
98                         AssertEquals("SetNamedItem.Override.Count.2", 1, col.Count);
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                         AssertEquals("InsertAfterNull", "garnet", el.GetAttributeNode("b1").Value);
112                         AssertEquals("InsertAfterNull.Pos", el.GetAttribute("b1"), col[0].Value);
113
114                         attr = xmlDoc.CreateAttribute("b3");
115                         attr.Value = "bloodstone";
116                         col.InsertAfter(attr, el.GetAttributeNode("b2"));
117                         AssertEquals("InsertAfterAttr", "bloodstone", el.GetAttributeNode("b3").Value);
118                         AssertEquals("InsertAfterAttr.Pos", el.GetAttribute("b3"), col[2].Value);
119
120                         attr = xmlDoc.CreateAttribute("b4");
121                         attr.Value = "diamond";
122                         col.InsertBefore(attr, null);
123                         AssertEquals("InsertBeforeNull", "diamond", el.GetAttributeNode("b4").Value);
124                         AssertEquals("InsertBeforeNull.Pos", el.GetAttribute("b4"), col[3].Value);
125
126                         attr = xmlDoc.CreateAttribute("warning");
127                         attr.Value = "mixed modern and traditional;-)";
128                         col.InsertBefore(attr, el.GetAttributeNode("b1"));
129                         AssertEquals("InsertBeforeAttr", "mixed modern and traditional;-)", el.GetAttributeNode("warning").Value);
130                         AssertEquals("InsertBeforeAttr.Pos", el.GetAttributeNode("warning").Value, col[0].Value);
131
132                         attr = xmlDoc.CreateAttribute("about");
133                         attr.Value = "lists of birthstone.";
134                         col.Prepend(attr);
135                         AssertEquals("Prepend", "lists of birthstone.", col[0].Value);
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 ();\r
152                         XmlElement testElement = testDoc.CreateElement ("TestElement" );\r
153                         testDoc.AppendChild (testElement);\r
154 \r
155                         XmlAttribute testAttr1 = testDoc.CreateAttribute ("TestAttribute1");\r
156                         testAttr1.Value = "First attribute";\r
157                         testElement.Attributes.Prepend (testAttr1);\r
158 \r
159                         XmlAttribute testAttr2 = testDoc.CreateAttribute ("TestAttribute2");\r
160                         testAttr2.Value = "Second attribute";\r
161                         testElement.Attributes.InsertAfter (testAttr2, testAttr1);\r
162 \r
163                         XmlAttribute testAttr3 = testDoc.CreateAttribute ("TestAttribute3");\r
164                         testAttr3.Value = "Third attribute";\r
165                         testElement.Attributes.InsertAfter (testAttr3, testAttr2);\r
166 \r
167                         XmlAttribute outOfOrder = testDoc.CreateAttribute ("TestAttribute2");\r
168                         outOfOrder.Value = "Should still be second attribute";\r
169                         testElement.Attributes.InsertAfter (outOfOrder, testElement.Attributes [0]);\r
170 \r
171                         AssertEquals ("First attribute", testElement.Attributes [0].Value);\r
172                         AssertEquals ("Should still be second attribute", testElement.Attributes [1].Value);\r
173                         AssertEquals ("Third attribute", testElement.Attributes [2].Value);\r
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                         AssertEquals("Remove", 11, col.Count);
187                         AssertEquals("Remove.Removed", "a12", attr.Name);
188
189                         // RemoveAt
190                         attr = col.RemoveAt(5);
191                         AssertEquals("RemoveAt", null, el.GetAttributeNode("a6"));
192                         AssertEquals("Remove.Removed", "pearl", attr.Value);
193                 }
194
195                 [Test]
196                 public void RemoveDefaultAttribute ()
197                 {
198                         string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root attr CDATA 'default'>]>";
199                         string xml = dtd + "<root/>";
200                         XmlDocument doc = new XmlDocument();\r
201                         doc.LoadXml (xml);\r
202 \r
203                         doc.DocumentElement.Attributes ["attr"].Value = "modified";\r
204                         doc.DocumentElement.RemoveAttribute("attr");\r
205 \r
206                         XmlAttribute defAttr = doc.DocumentElement.Attributes ["attr"];\r
207                         AssertNotNull (defAttr);\r
208                         AssertEquals ("default", defAttr.Value);\r
209
210                         defAttr.Value = "default"; // same value as default\r
211                         AssertEquals (true, defAttr.Specified);\r
212                 }
213         }
214 }