XmlElementTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. element.RemoveAll();
  107. element.AppendChild(document.CreateElement("hoge"));
  108. AssertEquals ("<hoge />", element.InnerXml);
  109. }
  110. public void TestSetGetAttribute ()
  111. {
  112. XmlElement element = document.CreateElement ("foo");
  113. element.SetAttribute ("attr1", "val1");
  114. element.SetAttribute ("attr2", "val2");
  115. AssertEquals ("val1", element.GetAttribute ("attr1"));
  116. AssertEquals ("val2", element.GetAttribute ("attr2"));
  117. }
  118. public void TestGetElementsByTagNameNoNameSpace ()
  119. {
  120. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  121. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  122. <author>Tom Clancy</author><price>6.95</price></book><book>
  123. <title>Bourne Identity</title><author>Robert Ludlum</author>
  124. <price>9.95</price></book><Fluffer><Nutter><book>
  125. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  126. <price>9.95</price></book></Nutter></Fluffer></library>";
  127. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  128. document = new XmlDocument ();
  129. document.Load (memoryStream);
  130. XmlNodeList libraryList = document.GetElementsByTagName ("library");
  131. XmlNode xmlNode = libraryList.Item (0);
  132. XmlElement xmlElement = xmlNode as XmlElement;
  133. XmlNodeList bookList = xmlElement.GetElementsByTagName ("book");
  134. AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
  135. }
  136. public void TestGetElementsByTagNameUsingNameSpace ()
  137. {
  138. StringBuilder xml = new StringBuilder ();
  139. xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\" ");
  140. xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
  141. xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
  142. xml.Append ("<North:author>John Doe</North:author> " );
  143. xml.Append ("<North:price>34.95</North:price></North:book> " );
  144. xml.Append ("<South:book type=\"fiction\"> " );
  145. xml.Append ("<South:title>Bear and the Dragon</South:title> " );
  146. xml.Append ("<South:author>Tom Clancy</South:author> " );
  147. xml.Append ("<South:price>6.95</South:price></South:book> " );
  148. xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
  149. xml.Append ("<South:author>Robert Ludlum</South:author> " );
  150. xml.Append ("<South:price>9.95</South:price></South:book></library>");
  151. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  152. document = new XmlDocument ();
  153. document.Load (memoryStream);
  154. XmlNodeList libraryList = document.GetElementsByTagName ("library");
  155. XmlNode xmlNode = libraryList.Item (0);
  156. XmlElement xmlElement = xmlNode as XmlElement;
  157. XmlNodeList bookList = xmlElement.GetElementsByTagName ("book", "http://www.foo.com");
  158. AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 1, bookList.Count);
  159. }
  160. public void TestOuterXmlWithNamespace ()
  161. {
  162. XmlElement element = document.CreateElement ("foo", "bar", "#foo");
  163. AssertEquals ("<foo:bar xmlns:foo=\"#foo\" />", element.OuterXml);
  164. }
  165. public void TestRemoveAllAttributes ()
  166. {
  167. StringBuilder xml = new StringBuilder ();
  168. xml.Append ("<?xml version=\"1.0\" ?><library><book type=\"non-fiction\" price=\"34.95\"> ");
  169. xml.Append ("<title type=\"intro\">XML Fun</title> " );
  170. xml.Append ("<author>John Doe</author></book></library>");
  171. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  172. document = new XmlDocument ();
  173. document.Load (memoryStream);
  174. XmlNodeList bookList = document.GetElementsByTagName ("book");
  175. XmlNode xmlNode = bookList.Item (0);
  176. XmlElement xmlElement = xmlNode as XmlElement;
  177. xmlElement.RemoveAllAttributes ();
  178. AssertEquals ("attributes not properly removed.", false, xmlElement.HasAttribute ("type"));
  179. }
  180. public void TestSetAttributeNode()
  181. {
  182. XmlDocument xmlDoc = new XmlDocument ();
  183. XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
  184. XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
  185. XmlAttribute xmlAttribute2 = xmlEl.SetAttributeNode ("attr2", "namespace2");
  186. AssertEquals ("attribute name not properly created.", true, xmlAttribute.Name.Equals ("attr1"));
  187. AssertEquals ("attribute namespace not properly created.", true, xmlAttribute.NamespaceURI.Equals ("namespace1"));
  188. }
  189. public void TestInnerXmlSetter()
  190. {
  191. XmlDocument doc = new XmlDocument();
  192. doc.LoadXml("<root/>");
  193. XmlElement el = doc.DocumentElement;
  194. AssertNull("#Simple", el.FirstChild);
  195. el.InnerXml = "<foo><bar att='baz'/></foo>";
  196. XmlElement child = el.FirstChild as XmlElement;
  197. AssertNotNull("#Simple.Child", child);
  198. AssertEquals("#Simple.Child.Name", "foo", child.LocalName);
  199. XmlElement grandchild = child.FirstChild as XmlElement;
  200. AssertNotNull("#Simple.GrandChild", grandchild);
  201. AssertEquals("#Simple.GrandChild.Name", "bar", grandchild.LocalName);
  202. AssertEquals("#Simple.GrandChild.Attr", "baz", grandchild.GetAttribute("att"));
  203. doc.LoadXml("<root xmlns='NS0' xmlns:ns1='NS1'><foo/><ns1:bar/><ns2:bar xmlns:ns2='NS2' /></root>");
  204. el = doc.DocumentElement.FirstChild.NextSibling as XmlElement; // ns1:bar
  205. AssertNull("#Namespaced.Prepare", el.FirstChild);
  206. el.InnerXml = "<ns1:baz />";
  207. AssertNotNull("#Namespaced.Child", el.FirstChild);
  208. AssertEquals("#Namespaced.Child.Name", "baz", el.FirstChild.LocalName);
  209. AssertEquals("#Namespaced.Child.NSURI", "NS1", el.FirstChild.NamespaceURI); // important!
  210. el.InnerXml = "<hoge />";
  211. AssertEquals("#Namespaced.VerifyPreviousCleared", "hoge", el.FirstChild.Name);
  212. }
  213. public void TestRemoveAttribute()
  214. {
  215. string xlinkURI = "http://www.w3.org/1999/XLink";
  216. XmlDocument doc = new XmlDocument();
  217. doc.LoadXml("<root a1='1' a2='2' xlink:href='urn:foo' xmlns:xlink='" + xlinkURI + "' />");
  218. XmlElement el = doc.DocumentElement;
  219. el.RemoveAttribute("a1");
  220. AssertNull("RemoveAttribute", el.GetAttributeNode("a1"));
  221. el.RemoveAttribute("xlink:href");
  222. AssertNull("RemoveAttribute", el.GetAttributeNode("href", xlinkURI));
  223. el.RemoveAllAttributes();
  224. AssertNull("RemoveAllAttributes", el.GetAttributeNode("a2"));
  225. }
  226. public void TestWriteToWithDeletedNamespacePrefix()
  227. {
  228. XmlDocument doc = new XmlDocument();
  229. doc.LoadXml("<root xmlns:foo='urn:dummy'><foo foo:bar='baz' /></root>");
  230. doc.DocumentElement.RemoveAllAttributes();
  231. Assert(doc.DocumentElement.FirstChild.OuterXml.IndexOf("xmlns:foo") > 0);
  232. }
  233. public void TestWriteToWithDifferentNamespaceAttributes()
  234. {
  235. XmlDocument doc = new XmlDocument();
  236. doc.LoadXml("<root xmlns:foo='urn:dummy' xmlns:html='http://www.w3.org/1999/xhtml' html:style='font-size: 1em'></root>");
  237. Assert(doc.OuterXml.IndexOf("xmlns:html=\"http://www.w3.org/1999/xhtml\"") > 0);
  238. }
  239. }
  240. }