XmlAttributeCollectionTests.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // XmlAttributeCollectionTests.cs : Tests for the XmlAttributeCollection class
  2. //
  3. // Author: Matt Hunter <[email protected]>
  4. // Author: Martin Willemoes Hansen <[email protected]>
  5. //
  6. // (C) 2002 Matt Hunter
  7. // (C) 2003 Martin Willemoes Hansen
  8. using System;
  9. using System.Xml;
  10. using System.Text;
  11. using System.IO;
  12. using System.Collections;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Xml
  15. {
  16. [TestFixture]
  17. public class XmlAttributeCollectionTests : Assertion
  18. {
  19. private XmlDocument document;
  20. [SetUp]
  21. public void GetReady()
  22. {
  23. document = new XmlDocument ();
  24. }
  25. [Test]
  26. public void RemoveAll ()
  27. {
  28. StringBuilder xml = new StringBuilder ();
  29. xml.Append ("<?xml version=\"1.0\" ?><library><book type=\"non-fiction\" price=\"34.95\"> ");
  30. xml.Append ("<title type=\"intro\">XML Fun</title> " );
  31. xml.Append ("<author>John Doe</author></book></library>");
  32. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  33. document = new XmlDocument ();
  34. document.Load (memoryStream);
  35. XmlNodeList bookList = document.GetElementsByTagName ("book");
  36. XmlNode xmlNode = bookList.Item (0);
  37. XmlElement xmlElement = xmlNode as XmlElement;
  38. XmlAttributeCollection attributes = xmlElement.Attributes;
  39. attributes.RemoveAll ();
  40. AssertEquals ("not all attributes removed.", false, xmlElement.HasAttribute ("type"));
  41. }
  42. [Test]
  43. public void Append ()
  44. {
  45. XmlDocument xmlDoc = new XmlDocument ();
  46. XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
  47. XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
  48. XmlNode xmlNode = xmlDoc.CreateNode (XmlNodeType.Attribute, "attr3", "namespace1");
  49. XmlAttribute xmlAttribute3 = xmlNode as XmlAttribute;
  50. XmlAttributeCollection attributeCol = xmlEl.Attributes;
  51. xmlAttribute3 = attributeCol.Append (xmlAttribute3);
  52. AssertEquals ("attribute name not properly created.", true, xmlAttribute3.Name.Equals ("attr3"));
  53. AssertEquals ("attribute namespace not properly created.", true, xmlAttribute3.NamespaceURI.Equals ("namespace1"));
  54. }
  55. [Test]
  56. public void CopyTo ()
  57. {
  58. XmlDocument xmlDoc = new XmlDocument ();
  59. 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' />");
  60. XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
  61. XmlAttribute[] array = new XmlAttribute[24];
  62. col.CopyTo(array, 0);
  63. AssertEquals("garnet", array[0].Value);
  64. AssertEquals("moonstone", array[8].Value);
  65. AssertEquals("turquoize", array[11].Value);
  66. col.CopyTo(array, 12);
  67. AssertEquals("garnet", array[12].Value);
  68. AssertEquals("moonstone", array[20].Value);
  69. AssertEquals("turquoize", array[23].Value);
  70. }
  71. [Test]
  72. public void SetNamedItem ()
  73. {
  74. XmlDocument xmlDoc = new XmlDocument ();
  75. xmlDoc.LoadXml("<root />");
  76. XmlElement el = xmlDoc.DocumentElement;
  77. XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
  78. XmlAttribute attr = xmlDoc.CreateAttribute("b3");
  79. attr.Value = "bloodstone";
  80. col.SetNamedItem(attr);
  81. AssertEquals("SetNamedItem.Normal", "bloodstone", el.GetAttribute("b3"));
  82. attr = xmlDoc.CreateAttribute("b3");
  83. attr.Value = "aquamaline";
  84. col.SetNamedItem(attr);
  85. AssertEquals("SetNamedItem.Override", "aquamaline", el.GetAttribute("b3"));
  86. AssertEquals("SetNamedItem.Override.Count.1", 1, el.Attributes.Count);
  87. AssertEquals("SetNamedItem.Override.Count.2", 1, col.Count);
  88. }
  89. [Test]
  90. public void InsertBeforeAfterPrepend ()
  91. {
  92. XmlDocument xmlDoc = new XmlDocument ();
  93. xmlDoc.LoadXml("<root b2='amethyst' />");
  94. XmlElement el = xmlDoc.DocumentElement;
  95. XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes;
  96. XmlAttribute attr = xmlDoc.CreateAttribute("b1");
  97. attr.Value = "garnet";
  98. col.InsertAfter(attr, null);
  99. AssertEquals("InsertAfterNull", "garnet", el.GetAttributeNode("b1").Value);
  100. AssertEquals("InsertAfterNull.Pos", el.GetAttribute("b1"), col[0].Value);
  101. attr = xmlDoc.CreateAttribute("b3");
  102. attr.Value = "bloodstone";
  103. col.InsertAfter(attr, el.GetAttributeNode("b2"));
  104. AssertEquals("InsertAfterAttr", "bloodstone", el.GetAttributeNode("b3").Value);
  105. AssertEquals("InsertAfterAttr.Pos", el.GetAttribute("b3"), col[2].Value);
  106. attr = xmlDoc.CreateAttribute("b4");
  107. attr.Value = "diamond";
  108. col.InsertBefore(attr, null);
  109. AssertEquals("InsertBeforeNull", "diamond", el.GetAttributeNode("b4").Value);
  110. AssertEquals("InsertBeforeNull.Pos", el.GetAttribute("b4"), col[3].Value);
  111. attr = xmlDoc.CreateAttribute("warning");
  112. attr.Value = "mixed modern and traditional;-)";
  113. col.InsertBefore(attr, el.GetAttributeNode("b1"));
  114. AssertEquals("InsertBeforeAttr", "mixed modern and traditional;-)", el.GetAttributeNode("warning").Value);
  115. AssertEquals("InsertBeforeAttr.Pos", el.GetAttributeNode("warning").Value, col[0].Value);
  116. attr = xmlDoc.CreateAttribute("about");
  117. attr.Value = "lists of birthstone.";
  118. col.Prepend(attr);
  119. AssertEquals("Prepend", "lists of birthstone.", col[0].Value);
  120. }
  121. [Test]
  122. [ExpectedException (typeof (ArgumentException))]
  123. public void InsertAfterError ()
  124. {
  125. this.document.LoadXml ("<root><elem a='1'/></root>");
  126. XmlAttribute attr = document.CreateAttribute ("foo");
  127. attr.Value = "test";
  128. document.DocumentElement.Attributes.InsertAfter (attr, document.DocumentElement.FirstChild.Attributes [0]);
  129. }
  130. [Test]
  131. public void InsertAfterReplacesInCorrectOrder ()
  132. {
  133. XmlDocument testDoc = new XmlDocument ();
  134. XmlElement testElement = testDoc.CreateElement ("TestElement" );
  135. testDoc.AppendChild (testElement);
  136. XmlAttribute testAttr1 = testDoc.CreateAttribute ("TestAttribute1");
  137. testAttr1.Value = "First attribute";
  138. testElement.Attributes.Prepend (testAttr1);
  139. XmlAttribute testAttr2 = testDoc.CreateAttribute ("TestAttribute2");
  140. testAttr2.Value = "Second attribute";
  141. testElement.Attributes.InsertAfter (testAttr2, testAttr1);
  142. XmlAttribute testAttr3 = testDoc.CreateAttribute ("TestAttribute3");
  143. testAttr3.Value = "Third attribute";
  144. testElement.Attributes.InsertAfter (testAttr3, testAttr2);
  145. XmlAttribute outOfOrder = testDoc.CreateAttribute ("TestAttribute2");
  146. outOfOrder.Value = "Should still be second attribute";
  147. testElement.Attributes.InsertAfter (outOfOrder, testElement.Attributes [0]);
  148. AssertEquals ("First attribute", testElement.Attributes [0].Value);
  149. AssertEquals ("Should still be second attribute", testElement.Attributes [1].Value);
  150. AssertEquals ("Third attribute", testElement.Attributes [2].Value);
  151. }
  152. [Test]
  153. public void Remove ()
  154. {
  155. XmlDocument xmlDoc = new XmlDocument ();
  156. 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' />");
  157. XmlElement el = xmlDoc.DocumentElement;
  158. XmlAttributeCollection col = el.Attributes;
  159. // Remove
  160. XmlAttribute attr = col.Remove(el.GetAttributeNode("a12"));
  161. AssertEquals("Remove", 11, col.Count);
  162. AssertEquals("Remove.Removed", "a12", attr.Name);
  163. // RemoveAt
  164. attr = col.RemoveAt(5);
  165. AssertEquals("RemoveAt", null, el.GetAttributeNode("a6"));
  166. AssertEquals("Remove.Removed", "pearl", attr.Value);
  167. }
  168. [Test]
  169. #if NET_2_0
  170. [Category ("NotDotNet")] // enbug
  171. #endif
  172. public void RemoveDefaultAttribute ()
  173. {
  174. string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root attr CDATA 'default'>]>";
  175. string xml = dtd + "<root/>";
  176. XmlDocument doc = new XmlDocument();
  177. doc.LoadXml (xml);
  178. doc.DocumentElement.Attributes ["attr"].Value = "modified";
  179. doc.DocumentElement.RemoveAttribute("attr");
  180. XmlAttribute defAttr = doc.DocumentElement.Attributes ["attr"];
  181. AssertNotNull (defAttr);
  182. AssertEquals ("default", defAttr.Value);
  183. defAttr.Value = "default"; // same value as default
  184. AssertEquals (true, defAttr.Specified);
  185. }
  186. }
  187. }