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
: Assertion
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 AssertEquals ("not all attributes removed.", false, xmlElement
.HasAttribute ("type"));
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"));
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 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
);
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 AssertEquals("SetNamedItem.Normal", "bloodstone", el
.GetAttribute("b3"));
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
);
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 AssertEquals("InsertAfterNull", "garnet", el
.GetAttributeNode("b1").Value
);
112 AssertEquals("InsertAfterNull.Pos", el
.GetAttribute("b1"), col
[0].Value
);
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
);
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
);
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
);
132 attr
= xmlDoc
.CreateAttribute("about");
133 attr
.Value
= "lists of birthstone.";
135 AssertEquals("Prepend", "lists of birthstone.", col
[0].Value
);
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 AssertEquals ("First attribute", testElement
.Attributes
[0].Value
);
172 AssertEquals ("Should still be second attribute", testElement
.Attributes
[1].Value
);
173 AssertEquals ("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 AssertEquals("Remove", 11, col
.Count
);
187 AssertEquals("Remove.Removed", "a12", attr
.Name
);
190 attr
= col
.RemoveAt(5);
191 AssertEquals("RemoveAt", null, el
.GetAttributeNode("a6"));
192 AssertEquals("Remove.Removed", "pearl", attr
.Value
);
196 public void RemoveDefaultAttribute ()
198 string dtd
= "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root attr CDATA 'default'>]>";
199 string xml
= dtd
+ "<root/>";
200 XmlDocument doc
= new XmlDocument();
203 doc
.DocumentElement
.Attributes
["attr"].Value
= "modified";
204 doc
.DocumentElement
.RemoveAttribute("attr");
206 XmlAttribute defAttr
= doc
.DocumentElement
.Attributes
["attr"];
207 AssertNotNull (defAttr
);
208 AssertEquals ("default", defAttr
.Value
);
210 defAttr
.Value
= "default"; // same value as default
211 AssertEquals (true, defAttr
.Specified
);