1 // XmlAttributeCollectionTests.cs : Tests for the XmlAttributeCollection class
3 // Author: Matt Hunter <xrkune@tconl.com>
4 // Author: Martin Willemoes Hansen <mwh@sysrq.dk>
6 // (C) 2002 Matt Hunter
7 // (C) 2003 Martin Willemoes Hansen
13 using System.Collections;
15 using NUnit.Framework;
17 namespace MonoTests.System.Xml
20 public class XmlAttributeCollectionTests
22 private XmlDocument document;
25 public void GetReady()
27 document = new XmlDocument ();
31 public void RemoveAll ()
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>");
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.");
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.");
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];
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);
81 public void SetNamedItem ()
83 XmlDocument xmlDoc = new XmlDocument ();
84 xmlDoc.LoadXml("<root />");
85 XmlElement el = xmlDoc.DocumentElement;
86 XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
88 XmlAttribute attr = xmlDoc.CreateAttribute("b3");
89 attr.Value = "bloodstone";
90 col.SetNamedItem(attr);
91 Assert.AreEqual ("bloodstone", el.GetAttribute("b3"), "SetNamedItem.Normal");
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");
102 public void InsertBeforeAfterPrepend ()
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");
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");
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");
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");
132 attr = xmlDoc.CreateAttribute("about");
133 attr.Value = "lists of birthstone.";
135 Assert.AreEqual ("lists of birthstone.", col[0].Value, "Prepend");
139 [ExpectedException (typeof (ArgumentException))]
140 public void InsertAfterError ()
142 this.document.LoadXml ("<root><elem a='1'/></root>");
143 XmlAttribute attr = document.CreateAttribute ("foo");
145 document.DocumentElement.Attributes.InsertAfter (attr, document.DocumentElement.FirstChild.Attributes [0]);
149 public void InsertAfterReplacesInCorrectOrder ()
151 XmlDocument testDoc = new XmlDocument ();
152 XmlElement testElement = testDoc.CreateElement ("TestElement" );
153 testDoc.AppendChild (testElement);
155 XmlAttribute testAttr1 = testDoc.CreateAttribute ("TestAttribute1");
156 testAttr1.Value = "First attribute";
157 testElement.Attributes.Prepend (testAttr1);
159 XmlAttribute testAttr2 = testDoc.CreateAttribute ("TestAttribute2");
160 testAttr2.Value = "Second attribute";
161 testElement.Attributes.InsertAfter (testAttr2, testAttr1);
163 XmlAttribute testAttr3 = testDoc.CreateAttribute ("TestAttribute3");
164 testAttr3.Value = "Third attribute";
165 testElement.Attributes.InsertAfter (testAttr3, testAttr2);
167 XmlAttribute outOfOrder = testDoc.CreateAttribute ("TestAttribute2");
168 outOfOrder.Value = "Should still be second attribute";
169 testElement.Attributes.InsertAfter (outOfOrder, testElement.Attributes [0]);
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);
177 public void Remove ()
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;
185 XmlAttribute attr = col.Remove(el.GetAttributeNode("a12"));
186 Assert.AreEqual (11, col.Count, "Remove");
187 Assert.AreEqual ("a12", attr.Name, "Remove.Removed");
190 attr = col.RemoveAt(5);
191 Assert.AreEqual (null, el.GetAttributeNode("a6"), "RemoveAt");
192 Assert.AreEqual ("pearl", attr.Value, "Remove.Removed");
197 [Category ("NotDotNet")] // enbug
199 public void RemoveDefaultAttribute ()
201 string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root attr CDATA 'default'>]>";
202 string xml = dtd + "<root/>";
203 XmlDocument doc = new XmlDocument();
206 doc.DocumentElement.Attributes ["attr"].Value = "modified";
207 doc.DocumentElement.RemoveAttribute("attr");
209 XmlAttribute defAttr = doc.DocumentElement.Attributes ["attr"];
210 Assert.IsNotNull (defAttr);
211 Assert.AreEqual ("default", defAttr.Value);
213 defAttr.Value = "default"; // same value as default
214 Assert.AreEqual (true, defAttr.Specified);
218 public void AddIdenticalAttrToTheSameNode ()
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);