XmlAttributeCollectionTests.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. public void InsertAfterReplacesInCorrectOrder ()
  123. {
  124. XmlDocument testDoc = new XmlDocument ();
  125. XmlElement testElement = testDoc.CreateElement ("TestElement" );
  126. testDoc.AppendChild (testElement);
  127. XmlAttribute testAttr1 = testDoc.CreateAttribute ("TestAttribute1");
  128. testAttr1.Value = "First attribute";
  129. testElement.Attributes.Prepend (testAttr1);
  130. XmlAttribute testAttr2 = testDoc.CreateAttribute ("TestAttribute2");
  131. testAttr2.Value = "Second attribute";
  132. testElement.Attributes.InsertAfter (testAttr2, testAttr1);
  133. XmlAttribute testAttr3 = testDoc.CreateAttribute ("TestAttribute3");
  134. testAttr3.Value = "Third attribute";
  135. testElement.Attributes.InsertAfter (testAttr3, testAttr2);
  136. XmlAttribute outOfOrder = testDoc.CreateAttribute ("TestAttribute2");
  137. outOfOrder.Value = "Should still be second attribute";
  138. testElement.Attributes.InsertAfter (outOfOrder, testElement.Attributes [0]);
  139. AssertEquals ("First attribute", testElement.Attributes [0].Value);
  140. AssertEquals ("Should still be second attribute", testElement.Attributes [1].Value);
  141. AssertEquals ("Third attribute", testElement.Attributes [2].Value);
  142. }
  143. [Test]
  144. public void Remove ()
  145. {
  146. XmlDocument xmlDoc = new XmlDocument ();
  147. 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' />");
  148. XmlElement el = xmlDoc.DocumentElement;
  149. XmlAttributeCollection col = el.Attributes;
  150. // Remove
  151. XmlAttribute attr = col.Remove(el.GetAttributeNode("a12"));
  152. AssertEquals("Remove", 11, col.Count);
  153. AssertEquals("Remove.Removed", "a12", attr.Name);
  154. // RemoveAt
  155. attr = col.RemoveAt(5);
  156. AssertEquals("RemoveAt", null, el.GetAttributeNode("a6"));
  157. AssertEquals("Remove.Removed", "pearl", attr.Value);
  158. }
  159. [Test]
  160. public void RemoveDefaultAttribute ()
  161. {
  162. string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root attr CDATA 'default'>]>";
  163. string xml = dtd + "<root/>";
  164. XmlDocument doc = new XmlDocument();
  165. doc.LoadXml (xml);
  166. doc.DocumentElement.Attributes ["attr"].Value = "modified";
  167. doc.DocumentElement.RemoveAttribute("attr");
  168. XmlAttribute defAttr = doc.DocumentElement.Attributes ["attr"];
  169. AssertNotNull (defAttr);
  170. AssertEquals ("default", defAttr.Value);
  171. defAttr.Value = "default"; // same value as default
  172. AssertEquals (true, defAttr.Specified);
  173. }
  174. }
  175. }