2
0

XmlAttributeCollectionTests.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Remove ()
  123. {
  124. XmlDocument xmlDoc = new XmlDocument ();
  125. 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' />");
  126. XmlElement el = xmlDoc.DocumentElement;
  127. XmlAttributeCollection col = el.Attributes;
  128. // Remove
  129. XmlAttribute attr = col.Remove(el.GetAttributeNode("a12"));
  130. AssertEquals("Remove", 11, col.Count);
  131. AssertEquals("Remove.Removed", "a12", attr.Name);
  132. // RemoveAt
  133. attr = col.RemoveAt(5);
  134. AssertEquals("RemoveAt", null, el.GetAttributeNode("a6"));
  135. AssertEquals("Remove.Removed", "pearl", attr.Value);
  136. }
  137. }
  138. }