2002-12-01 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / Test / XmlAttributeCollectionTests.cs
1 // XmlAttributeCollectionTests.cs : Tests for the XmlAttributeCollection class
2 //
3 // Author: Matt Hunter <xrkune@tconl.com>
4 //
5 // <c> 2002 Matt Hunter
6
7 using System;
8 using System.Xml;
9 using System.Text;
10 using System.IO;
11 using System.Collections;
12
13 using NUnit.Framework;
14
15 namespace MonoTests.System.Xml
16 {
17         public class XmlAttributeCollectionTests : TestCase
18         {
19                 public XmlAttributeCollectionTests() : base("MonoTests.System.Xml.XmlAttributeCollectionTests testsuite") { }
20                 public XmlAttributeCollectionTests(string name) : base(name) { }
21
22                 private XmlDocument document;
23
24                 protected override void SetUp()
25                 {
26                         document = new XmlDocument ();
27                 }
28                 public void TestRemoveAll ()
29                 {
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>");
34
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"));
44                 }
45
46                 public void TestAppend () 
47                 {
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"));
57                 }
58
59                 public void TestCopyTo () 
60                 {
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];
65                         col.CopyTo(array, 0);
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);
73                 }
74
75                 public void TestSetNamedItem ()
76                 {
77                         XmlDocument xmlDoc = new XmlDocument ();
78                         xmlDoc.LoadXml("<root />");
79                         XmlElement el = xmlDoc.DocumentElement;
80                         XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
81
82                         XmlAttribute attr = xmlDoc.CreateAttribute("b3");
83                         attr.Value = "bloodstone";
84                         col.SetNamedItem(attr);
85                         AssertEquals("SetNamedItem.Normal", "bloodstone", el.GetAttribute("b3"));
86
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);
93                 }
94
95                 public void TestInsertBeforeAfterPrepend () 
96                 {
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);
106
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);
112
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);
118
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);
124
125                         attr = xmlDoc.CreateAttribute("about");
126                         attr.Value = "lists of birthstone.";
127                         col.Prepend(attr);
128                         AssertEquals("Prepend", "lists of birthstone.", col[0].Value);
129                 }
130
131                 public void TestRemove ()
132                 {
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;
137
138                         // Remove
139                         XmlAttribute attr = col.Remove(el.GetAttributeNode("a12"));
140                         AssertEquals("Remove", 11, col.Count);
141                         AssertEquals("Remove.Removed", "a12", attr.Name);
142
143                         // RemoveAt
144                         attr = col.RemoveAt(5);
145                         AssertEquals("RemoveAt", null, el.GetAttributeNode("a6"));
146                         AssertEquals("Remove.Removed", "pearl", attr.Value);
147                 }
148         }
149 }