XmlAttributeCollectionTests.cs 5.7 KB

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