XmlElementTests.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // XmlElementTests
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. //
  7. // (C) 2002 Jason Diamond http://injektilo.org/
  8. //
  9. using System;
  10. using System.Xml;
  11. using System.IO;
  12. using System.Text;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Xml
  15. {
  16. public class XmlElementTests : TestCase
  17. {
  18. public XmlElementTests () : base ("MonoTests.System.Xml.XmlElementTests testsuite") { }
  19. public XmlElementTests (string name) : base (name) { }
  20. private XmlDocument document;
  21. protected override void SetUp()
  22. {
  23. document = new XmlDocument ();
  24. }
  25. private void AssertElement (XmlElement element, string prefix,
  26. string localName, string namespaceURI,
  27. int attributesCount)
  28. {
  29. AssertEquals (prefix != String.Empty ? prefix + ":" + localName : localName, element.Name);
  30. AssertEquals (prefix, element.Prefix);
  31. AssertEquals (localName, element.LocalName);
  32. AssertEquals (namespaceURI, element.NamespaceURI);
  33. //AssertEquals (attributesCount, element.Attributes.Count);
  34. }
  35. public void TestCloneNode ()
  36. {
  37. XmlElement element = document.CreateElement ("foo");
  38. XmlElement child = document.CreateElement ("bar");
  39. XmlElement grandson = document.CreateElement ("baz");
  40. element.SetAttribute ("attr1", "val1");
  41. element.SetAttribute ("attr2", "val2");
  42. element.AppendChild (child);
  43. child.SetAttribute ("attr3", "val3");
  44. child.AppendChild (grandson);
  45. document.AppendChild (element);
  46. XmlNode deep = element.CloneNode (true);
  47. // AssertEquals ("These should be the same", deep.OuterXml, element.OuterXml);
  48. AssertNull ("This is not null", deep.ParentNode);
  49. Assert ("Copies, not pointers", !Object.ReferenceEquals (element,deep));
  50. XmlNode shallow = element.CloneNode (false);
  51. AssertNull ("This is not null", shallow.ParentNode);
  52. Assert ("Copies, not pointers", !Object.ReferenceEquals (element,shallow));
  53. AssertEquals ("Shallow clones shalt have no children!", false, shallow.HasChildNodes);
  54. }
  55. public void TestCreateElement1 ()
  56. {
  57. XmlElement element = document.CreateElement ("name");
  58. AssertElement (element, String.Empty, "name", String.Empty, 0);
  59. }
  60. public void TestCreateElement1WithPrefix ()
  61. {
  62. XmlElement element = document.CreateElement ("prefix:localName");
  63. AssertElement (element, "prefix", "localName", String.Empty, 0);
  64. }
  65. public void TestCreateElement2 ()
  66. {
  67. XmlElement element = document.CreateElement ("qualifiedName", "namespaceURI");
  68. AssertElement (element, String.Empty, "qualifiedName",
  69. "namespaceURI", 0);
  70. }
  71. public void TestCreateElement2WithPrefix ()
  72. {
  73. XmlElement element = document.CreateElement ("prefix:localName", "namespaceURI");
  74. AssertElement (element, "prefix", "localName", "namespaceURI", 0);
  75. }
  76. public void TestCreateElement3 ()
  77. {
  78. XmlElement element = document.CreateElement ("prefix", "localName", "namespaceURI");
  79. AssertElement (element, "prefix", "localName", "namespaceURI", 0);
  80. }
  81. public void TestCreateElement3WithNullNamespace ()
  82. {
  83. // bug #26855, NamespaceURI should NEVER be null.
  84. XmlElement element = document.CreateElement (null, "localName", null);
  85. AssertElement (element, String.Empty, "localName", String.Empty, 0);
  86. }
  87. public void TestInnerAndOuterXml ()
  88. {
  89. XmlElement element;
  90. XmlText text;
  91. XmlComment comment;
  92. element = document.CreateElement ("foo");
  93. AssertEquals (String.Empty, element.InnerXml);
  94. AssertEquals ("<foo />", element.OuterXml);
  95. text = document.CreateTextNode ("bar");
  96. element.AppendChild (text);
  97. AssertEquals ("bar", element.InnerXml);
  98. AssertEquals ("<foo>bar</foo>", element.OuterXml);
  99. element.SetAttribute ("baz", "quux");
  100. AssertEquals ("bar", element.InnerXml);
  101. AssertEquals ("<foo baz=\"quux\">bar</foo>", element.OuterXml);
  102. comment = document.CreateComment ("squonk");
  103. element.AppendChild (comment);
  104. AssertEquals ("bar<!--squonk-->", element.InnerXml);
  105. AssertEquals ("<foo baz=\"quux\">bar<!--squonk--></foo>", element.OuterXml);
  106. }
  107. public void TestSetGetAttribute ()
  108. {
  109. XmlElement element = document.CreateElement ("foo");
  110. element.SetAttribute ("attr1", "val1");
  111. element.SetAttribute ("attr2", "val2");
  112. AssertEquals ("val1", element.GetAttribute ("attr1"));
  113. AssertEquals ("val2", element.GetAttribute ("attr2"));
  114. }
  115. public void TestGetElementsByTagNameNoNameSpace ()
  116. {
  117. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  118. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  119. <author>Tom Clancy</author><price>6.95</price></book><book>
  120. <title>Bourne Identity</title><author>Robert Ludlum</author>
  121. <price>9.95</price></book><Fluffer><Nutter><book>
  122. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  123. <price>9.95</price></book></Nutter></Fluffer></library>";
  124. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  125. document = new XmlDocument ();
  126. document.Load (memoryStream);
  127. XmlNodeList bookList = document.GetElementsByTagName ("book");
  128. AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
  129. }
  130. public void TestOuterXmlWithNamespace ()
  131. {
  132. XmlElement element = document.CreateElement ("foo", "bar", "#foo");
  133. AssertEquals ("<foo:bar xmlns:foo=\"#foo\" />", element.OuterXml);
  134. }
  135. }
  136. }