XmlAttributeCollectionTests.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 NUnit.Framework;
  11. namespace MonoTests.System.Xml
  12. {
  13. public class XmlAttributeCollectionTests : TestCase
  14. {
  15. public XmlAttributeCollectionTests() : base("MonoTests.System.Xml.XmlAttributeCollectionTests testsuite") { }
  16. public XmlAttributeCollectionTests(string name) : base(name) { }
  17. private XmlDocument document;
  18. protected override void SetUp()
  19. {
  20. document = new XmlDocument ();
  21. }
  22. public void TestRemoveAll ()
  23. {
  24. StringBuilder xml = new StringBuilder ();
  25. xml.Append ("<?xml version=\"1.0\" ?><library><book type=\"non-fiction\" price=\"34.95\"> ");
  26. xml.Append ("<title type=\"intro\">XML Fun</title> " );
  27. xml.Append ("<author>John Doe</author></book></library>");
  28. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  29. document = new XmlDocument ();
  30. document.Load (memoryStream);
  31. XmlNodeList bookList = document.GetElementsByTagName ("book");
  32. XmlNode xmlNode = bookList.Item (0);
  33. XmlElement xmlElement = xmlNode as XmlElement;
  34. XmlAttributeCollection attributes = xmlElement.Attributes;
  35. attributes.RemoveAll ();
  36. AssertEquals ("not all attributes removed.", false, xmlElement.HasAttribute ("type"));
  37. }
  38. public void TestAppend ()
  39. {
  40. XmlDocument xmlDoc = new XmlDocument ();
  41. XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
  42. XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
  43. XmlNode xmlNode = xmlDoc.CreateNode (XmlNodeType.Attribute, "attr3", "namespace1");
  44. XmlAttribute xmlAttribute3 = xmlNode as XmlAttribute;
  45. XmlAttributeCollection attributeCol = xmlEl.Attributes;
  46. xmlAttribute3 = attributeCol.Append (xmlAttribute3);
  47. AssertEquals ("attribute name not properly created.", true, xmlAttribute3.Name.Equals ("attr3"));
  48. AssertEquals ("attribute namespace not properly created.", true, xmlAttribute3.NamespaceURI.Equals ("namespace1"));
  49. }
  50. }
  51. }