XmlElementTests.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 libraryList = document.GetElementsByTagName ("library");
  128. XmlNode xmlNode = libraryList.Item (0);
  129. XmlElement xmlElement = xmlNode as XmlElement;
  130. XmlNodeList bookList = xmlElement.GetElementsByTagName ("book");
  131. AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
  132. }
  133. public void TestGetElementsByTagNameUsingNameSpace ()
  134. {
  135. StringBuilder xml = new StringBuilder ();
  136. xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\" ");
  137. xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
  138. xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
  139. xml.Append ("<North:author>John Doe</North:author> " );
  140. xml.Append ("<North:price>34.95</North:price></North:book> " );
  141. xml.Append ("<South:book type=\"fiction\"> " );
  142. xml.Append ("<South:title>Bear and the Dragon</South:title> " );
  143. xml.Append ("<South:author>Tom Clancy</South:author> " );
  144. xml.Append ("<South:price>6.95</South:price></South:book> " );
  145. xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
  146. xml.Append ("<South:author>Robert Ludlum</South:author> " );
  147. xml.Append ("<South:price>9.95</South:price></South:book></library>");
  148. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  149. document = new XmlDocument ();
  150. document.Load (memoryStream);
  151. XmlNodeList libraryList = document.GetElementsByTagName ("library");
  152. XmlNode xmlNode = libraryList.Item (0);
  153. XmlElement xmlElement = xmlNode as XmlElement;
  154. XmlNodeList bookList = xmlElement.GetElementsByTagName ("book", "http://www.foo.com");
  155. AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 1, bookList.Count);
  156. }
  157. public void TestOuterXmlWithNamespace ()
  158. {
  159. XmlElement element = document.CreateElement ("foo", "bar", "#foo");
  160. AssertEquals ("<foo:bar xmlns:foo=\"#foo\" />", element.OuterXml);
  161. }
  162. public void TestRemoveAllAttributes ()
  163. {
  164. StringBuilder xml = new StringBuilder ();
  165. xml.Append ("<?xml version=\"1.0\" ?><library><book type=\"non-fiction\" price=\"34.95\"> ");
  166. xml.Append ("<title type=\"intro\">XML Fun</title> " );
  167. xml.Append ("<author>John Doe</author></book></library>");
  168. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  169. document = new XmlDocument ();
  170. document.Load (memoryStream);
  171. XmlNodeList bookList = document.GetElementsByTagName ("book");
  172. XmlNode xmlNode = bookList.Item (0);
  173. XmlElement xmlElement = xmlNode as XmlElement;
  174. xmlElement.RemoveAllAttributes ();
  175. AssertEquals ("attributes not properly removed.", false, xmlElement.HasAttribute ("type"));
  176. }
  177. public void TestSetAttributeNode()
  178. {
  179. XmlDocument xmlDoc = new XmlDocument ();
  180. XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
  181. XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
  182. XmlAttribute xmlAttribute2 = xmlEl.SetAttributeNode ("attr2", "namespace2");
  183. AssertEquals ("attribute name not properly created.", true, xmlAttribute.Name.Equals ("attr1"));
  184. AssertEquals ("attribute namespace not properly created.", true, xmlAttribute.NamespaceURI.Equals ("namespace1"));
  185. }
  186. }
  187. }