1 // XmlAttributeCollectionTests.cs : Tests for the XmlAttributeCollection class
3 // Author: Matt Hunter <xrkune@tconl.com>
5 // <c> 2002 Matt Hunter
11 using System.Collections;
13 using NUnit.Framework;
15 namespace MonoTests.System.Xml
17 public class XmlAttributeCollectionTests : TestCase
19 public XmlAttributeCollectionTests() : base("MonoTests.System.Xml.XmlAttributeCollectionTests testsuite") { }
20 public XmlAttributeCollectionTests(string name) : base(name) { }
22 private XmlDocument document;
24 protected override void SetUp()
26 document = new XmlDocument ();
28 public void TestRemoveAll ()
30 StringBuilder xml = new StringBuilder ();
31 xml.Append ("<?xml version=\"1.0\" ?><library><book type=\"non-fiction\" price=\"34.95\"> ");
32 xml.Append ("<title type=\"intro\">XML Fun</title> " );
33 xml.Append ("<author>John Doe</author></book></library>");
35 MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
36 document = new XmlDocument ();
37 document.Load (memoryStream);
38 XmlNodeList bookList = document.GetElementsByTagName ("book");
39 XmlNode xmlNode = bookList.Item (0);
40 XmlElement xmlElement = xmlNode as XmlElement;
41 XmlAttributeCollection attributes = xmlElement.Attributes;
42 attributes.RemoveAll ();
43 AssertEquals ("not all attributes removed.", false, xmlElement.HasAttribute ("type"));
46 public void TestAppend ()
48 XmlDocument xmlDoc = new XmlDocument ();
49 XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
50 XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
51 XmlNode xmlNode = xmlDoc.CreateNode (XmlNodeType.Attribute, "attr3", "namespace1");
52 XmlAttribute xmlAttribute3 = xmlNode as XmlAttribute;
53 XmlAttributeCollection attributeCol = xmlEl.Attributes;
54 xmlAttribute3 = attributeCol.Append (xmlAttribute3);
55 AssertEquals ("attribute name not properly created.", true, xmlAttribute3.Name.Equals ("attr3"));
56 AssertEquals ("attribute namespace not properly created.", true, xmlAttribute3.NamespaceURI.Equals ("namespace1"));
59 public void TestCopyTo ()
61 XmlDocument xmlDoc = new XmlDocument ();
62 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' />");
63 XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
64 XmlAttribute[] array = new XmlAttribute[24];
66 AssertEquals("garnet", array[0].Value);
67 AssertEquals("moonstone", array[8].Value);
68 AssertEquals("turquoize", array[11].Value);
69 col.CopyTo(array, 12);
70 AssertEquals("garnet", array[12].Value);
71 AssertEquals("moonstone", array[20].Value);
72 AssertEquals("turquoize", array[23].Value);
75 public void TestSetNamedItem ()
77 XmlDocument xmlDoc = new XmlDocument ();
78 xmlDoc.LoadXml("<root />");
79 XmlElement el = xmlDoc.DocumentElement;
80 XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
82 XmlAttribute attr = xmlDoc.CreateAttribute("b3");
83 attr.Value = "bloodstone";
84 col.SetNamedItem(attr);
85 AssertEquals("SetNamedItem.Normal", "bloodstone", el.GetAttribute("b3"));
87 attr = xmlDoc.CreateAttribute("b3");
88 attr.Value = "aquamaline";
89 col.SetNamedItem(attr);
90 AssertEquals("SetNamedItem.Override", "aquamaline", el.GetAttribute("b3"));
91 AssertEquals("SetNamedItem.Override.Count.1", 1, el.Attributes.Count);
92 AssertEquals("SetNamedItem.Override.Count.2", 1, col.Count);
95 public void TestInsertBeforeAfterPrepend ()
97 XmlDocument xmlDoc = new XmlDocument ();
98 xmlDoc.LoadXml("<root b2='amethyst' />");
99 XmlElement el = xmlDoc.DocumentElement;
100 XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
101 XmlAttribute attr = xmlDoc.CreateAttribute("b1");
102 attr.Value = "garnet";
103 col.InsertAfter(attr, null);
104 AssertEquals("InsertAfterNull", "garnet", el.GetAttributeNode("b1").Value);
105 AssertEquals("InsertAfterNull.Pos", el.GetAttribute("b1"), col[0].Value);
107 attr = xmlDoc.CreateAttribute("b3");
108 attr.Value = "bloodstone";
109 col.InsertAfter(attr, el.GetAttributeNode("b2"));
110 AssertEquals("InsertAfterAttr", "bloodstone", el.GetAttributeNode("b3").Value);
111 AssertEquals("InsertAfterAttr.Pos", el.GetAttribute("b3"), col[2].Value);
113 attr = xmlDoc.CreateAttribute("b4");
114 attr.Value = "diamond";
115 col.InsertBefore(attr, null);
116 AssertEquals("InsertBeforeNull", "diamond", el.GetAttributeNode("b4").Value);
117 AssertEquals("InsertBeforeNull.Pos", el.GetAttribute("b4"), col[3].Value);
119 attr = xmlDoc.CreateAttribute("warning");
120 attr.Value = "mixed modern and traditional;-)";
121 col.InsertBefore(attr, el.GetAttributeNode("b1"));
122 AssertEquals("InsertBeforeAttr", "mixed modern and traditional;-)", el.GetAttributeNode("warning").Value);
123 AssertEquals("InsertBeforeAttr.Pos", el.GetAttributeNode("warning").Value, col[0].Value);
125 attr = xmlDoc.CreateAttribute("about");
126 attr.Value = "lists of birthstone.";
128 AssertEquals("Prepend", "lists of birthstone.", col[0].Value);
131 public void TestRemove ()
133 XmlDocument xmlDoc = new XmlDocument ();
134 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' />");
135 XmlElement el = xmlDoc.DocumentElement;
136 XmlAttributeCollection col = el.Attributes;
139 XmlAttribute attr = col.Remove(el.GetAttributeNode("a12"));
140 AssertEquals("Remove", 11, col.Count);
141 AssertEquals("Remove.Removed", "a12", attr.Name);
144 attr = col.RemoveAt(5);
145 AssertEquals("RemoveAt", null, el.GetAttributeNode("a6"));
146 AssertEquals("Remove.Removed", "pearl", attr.Value);