XmlElementTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. // for NodeInserted Event
  36. private bool Inserted = false;
  37. private void OnNodeInserted (object o, XmlNodeChangedEventArgs e)
  38. {
  39. Inserted = true;
  40. }
  41. // for NodeChanged Event
  42. private bool Changed = false;
  43. private void OnNodeChanged (object o, XmlNodeChangedEventArgs e)
  44. {
  45. Changed = true;
  46. }
  47. // for NodeRemoved Event
  48. private bool Removed = false;
  49. private void OnNodeRemoved (object o, XmlNodeChangedEventArgs e)
  50. {
  51. Removed = true;
  52. }
  53. public void TestCloneNode ()
  54. {
  55. XmlElement element = document.CreateElement ("foo");
  56. XmlElement child = document.CreateElement ("bar");
  57. XmlElement grandson = document.CreateElement ("baz");
  58. element.SetAttribute ("attr1", "val1");
  59. element.SetAttribute ("attr2", "val2");
  60. element.AppendChild (child);
  61. child.SetAttribute ("attr3", "val3");
  62. child.AppendChild (grandson);
  63. document.AppendChild (element);
  64. XmlNode deep = element.CloneNode (true);
  65. // AssertEquals ("These should be the same", deep.OuterXml, element.OuterXml);
  66. AssertNull ("This is not null", deep.ParentNode);
  67. Assert ("Copies, not pointers", !Object.ReferenceEquals (element,deep));
  68. XmlNode shallow = element.CloneNode (false);
  69. AssertNull ("This is not null", shallow.ParentNode);
  70. Assert ("Copies, not pointers", !Object.ReferenceEquals (element,shallow));
  71. AssertEquals ("Shallow clones shalt have no children!", false, shallow.HasChildNodes);
  72. }
  73. public void TestCreateElement1 ()
  74. {
  75. XmlElement element = document.CreateElement ("name");
  76. AssertElement (element, String.Empty, "name", String.Empty, 0);
  77. }
  78. public void TestCreateElement1WithPrefix ()
  79. {
  80. XmlElement element = document.CreateElement ("prefix:localName");
  81. AssertElement (element, "prefix", "localName", String.Empty, 0);
  82. }
  83. public void TestCreateElement2 ()
  84. {
  85. XmlElement element = document.CreateElement ("qualifiedName", "namespaceURI");
  86. AssertElement (element, String.Empty, "qualifiedName",
  87. "namespaceURI", 0);
  88. }
  89. public void TestCreateElement2WithPrefix ()
  90. {
  91. XmlElement element = document.CreateElement ("prefix:localName", "namespaceURI");
  92. AssertElement (element, "prefix", "localName", "namespaceURI", 0);
  93. }
  94. public void TestCreateElement3 ()
  95. {
  96. XmlElement element = document.CreateElement ("prefix", "localName", "namespaceURI");
  97. AssertElement (element, "prefix", "localName", "namespaceURI", 0);
  98. }
  99. public void TestCreateElement3WithNullNamespace ()
  100. {
  101. // bug #26855, NamespaceURI should NEVER be null.
  102. XmlElement element = document.CreateElement (null, "localName", null);
  103. AssertElement (element, String.Empty, "localName", String.Empty, 0);
  104. }
  105. public void TestInnerAndOuterXml ()
  106. {
  107. XmlElement element;
  108. XmlText text;
  109. XmlComment comment;
  110. element = document.CreateElement ("foo");
  111. AssertEquals (String.Empty, element.InnerXml);
  112. AssertEquals ("<foo />", element.OuterXml);
  113. text = document.CreateTextNode ("bar");
  114. element.AppendChild (text);
  115. AssertEquals ("bar", element.InnerXml);
  116. AssertEquals ("<foo>bar</foo>", element.OuterXml);
  117. element.SetAttribute ("baz", "quux");
  118. AssertEquals ("bar", element.InnerXml);
  119. AssertEquals ("<foo baz=\"quux\">bar</foo>", element.OuterXml);
  120. comment = document.CreateComment ("squonk");
  121. element.AppendChild (comment);
  122. AssertEquals ("bar<!--squonk-->", element.InnerXml);
  123. AssertEquals ("<foo baz=\"quux\">bar<!--squonk--></foo>", element.OuterXml);
  124. element.RemoveAll();
  125. element.AppendChild(document.CreateElement("hoge"));
  126. AssertEquals ("<hoge />", element.InnerXml);
  127. }
  128. public void TestSetGetAttribute ()
  129. {
  130. XmlElement element = document.CreateElement ("foo");
  131. element.SetAttribute ("attr1", "val1");
  132. element.SetAttribute ("attr2", "val2");
  133. AssertEquals ("val1", element.GetAttribute ("attr1"));
  134. AssertEquals ("val2", element.GetAttribute ("attr2"));
  135. }
  136. public void TestGetElementsByTagNameNoNameSpace ()
  137. {
  138. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  139. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  140. <author>Tom Clancy</author><price>6.95</price></book><book>
  141. <title>Bourne Identity</title><author>Robert Ludlum</author>
  142. <price>9.95</price></book><Fluffer><Nutter><book>
  143. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  144. <price>9.95</price></book></Nutter></Fluffer></library>";
  145. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  146. document = new XmlDocument ();
  147. document.Load (memoryStream);
  148. XmlNodeList libraryList = document.GetElementsByTagName ("library");
  149. XmlNode xmlNode = libraryList.Item (0);
  150. XmlElement xmlElement = xmlNode as XmlElement;
  151. XmlNodeList bookList = xmlElement.GetElementsByTagName ("book");
  152. AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
  153. }
  154. public void TestGetElementsByTagNameUsingNameSpace ()
  155. {
  156. StringBuilder xml = new StringBuilder ();
  157. xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\" ");
  158. xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
  159. xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
  160. xml.Append ("<North:author>John Doe</North:author> " );
  161. xml.Append ("<North:price>34.95</North:price></North:book> " );
  162. xml.Append ("<South:book type=\"fiction\"> " );
  163. xml.Append ("<South:title>Bear and the Dragon</South:title> " );
  164. xml.Append ("<South:author>Tom Clancy</South:author> " );
  165. xml.Append ("<South:price>6.95</South:price></South:book> " );
  166. xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
  167. xml.Append ("<South:author>Robert Ludlum</South:author> " );
  168. xml.Append ("<South:price>9.95</South:price></South:book></library>");
  169. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  170. document = new XmlDocument ();
  171. document.Load (memoryStream);
  172. XmlNodeList libraryList = document.GetElementsByTagName ("library");
  173. XmlNode xmlNode = libraryList.Item (0);
  174. XmlElement xmlElement = xmlNode as XmlElement;
  175. XmlNodeList bookList = xmlElement.GetElementsByTagName ("book", "http://www.foo.com");
  176. AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 1, bookList.Count);
  177. }
  178. public void TestOuterXmlWithNamespace ()
  179. {
  180. XmlElement element = document.CreateElement ("foo", "bar", "#foo");
  181. AssertEquals ("<foo:bar xmlns:foo=\"#foo\" />", element.OuterXml);
  182. }
  183. public void TestRemoveAllAttributes ()
  184. {
  185. StringBuilder xml = new StringBuilder ();
  186. xml.Append ("<?xml version=\"1.0\" ?><library><book type=\"non-fiction\" price=\"34.95\"> ");
  187. xml.Append ("<title type=\"intro\">XML Fun</title> " );
  188. xml.Append ("<author>John Doe</author></book></library>");
  189. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  190. document = new XmlDocument ();
  191. document.Load (memoryStream);
  192. XmlNodeList bookList = document.GetElementsByTagName ("book");
  193. XmlNode xmlNode = bookList.Item (0);
  194. XmlElement xmlElement = xmlNode as XmlElement;
  195. xmlElement.RemoveAllAttributes ();
  196. AssertEquals ("attributes not properly removed.", false, xmlElement.HasAttribute ("type"));
  197. }
  198. public void TestSetAttributeNode ()
  199. {
  200. XmlDocument xmlDoc = new XmlDocument ();
  201. XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
  202. XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
  203. XmlAttribute xmlAttribute2 = xmlEl.SetAttributeNode ("attr2", "namespace2");
  204. AssertEquals ("attribute name not properly created.", true, xmlAttribute.Name.Equals ("attr1"));
  205. AssertEquals ("attribute namespace not properly created.", true, xmlAttribute.NamespaceURI.Equals ("namespace1"));
  206. }
  207. public void TestInnerXmlSetter ()
  208. {
  209. XmlDocument doc = new XmlDocument ();
  210. doc.LoadXml ("<root/>");
  211. XmlElement el = doc.DocumentElement;
  212. AssertNull ("#Simple", el.FirstChild);
  213. el.InnerXml = "<foo><bar att='baz'/></foo>";
  214. XmlElement child = el.FirstChild as XmlElement;
  215. AssertNotNull ("#Simple.Child", child);
  216. AssertEquals ("#Simple.Child.Name", "foo", child.LocalName);
  217. XmlElement grandchild = child.FirstChild as XmlElement;
  218. AssertNotNull ("#Simple.GrandChild", grandchild);
  219. AssertEquals ("#Simple.GrandChild.Name", "bar", grandchild.LocalName);
  220. AssertEquals ("#Simple.GrandChild.Attr", "baz", grandchild.GetAttribute ("att"));
  221. doc.LoadXml ("<root xmlns='NS0' xmlns:ns1='NS1'><foo/><ns1:bar/><ns2:bar xmlns:ns2='NS2' /></root>");
  222. el = doc.DocumentElement.FirstChild.NextSibling as XmlElement; // ns1:bar
  223. AssertNull ("#Namespaced.Prepare", el.FirstChild);
  224. el.InnerXml = "<ns1:baz />";
  225. AssertNotNull ("#Namespaced.Child", el.FirstChild);
  226. AssertEquals ("#Namespaced.Child.Name", "baz", el.FirstChild.LocalName);
  227. AssertEquals ("#Namespaced.Child.NSURI", "NS1", el.FirstChild.NamespaceURI); // important!
  228. el.InnerXml = "<hoge />";
  229. AssertEquals ("#Namespaced.VerifyPreviousCleared", "hoge", el.FirstChild.Name);
  230. }
  231. public void TestRemoveAttribute ()
  232. {
  233. string xlinkURI = "http://www.w3.org/1999/XLink";
  234. XmlDocument doc = new XmlDocument ();
  235. doc.LoadXml ("<root a1='1' a2='2' xlink:href='urn:foo' xmlns:xlink='" + xlinkURI + "' />");
  236. XmlElement el = doc.DocumentElement;
  237. el.RemoveAttribute ("a1");
  238. AssertNull ("RemoveAttribute", el.GetAttributeNode ("a1"));
  239. el.RemoveAttribute ("xlink:href");
  240. AssertNull ("RemoveAttribute", el.GetAttributeNode ("href", xlinkURI));
  241. el.RemoveAllAttributes ();
  242. AssertNull ("RemoveAllAttributes", el.GetAttributeNode ("a2"));
  243. }
  244. public void TestWriteToWithDefaultNamespace ()
  245. {
  246. XmlDocument doc = new XmlDocument ();
  247. doc.LoadXml ("<RetrievalElement URI=\"\"xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />");
  248. StringWriter sw = new StringWriter ();
  249. XmlTextWriter xtw = new XmlTextWriter (sw);
  250. doc.DocumentElement.WriteTo (xtw);
  251. AssertEquals ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", sw.ToString());
  252. }
  253. public void TestWriteToWithDeletedNamespacePrefix ()
  254. {
  255. XmlDocument doc = new XmlDocument ();
  256. doc.LoadXml ("<root xmlns:foo='urn:dummy'><foo foo:bar='baz' /></root>");
  257. doc.DocumentElement.RemoveAllAttributes ();
  258. Assert (doc.DocumentElement.FirstChild.OuterXml.IndexOf("xmlns:foo") > 0);
  259. }
  260. public void TestWriteToWithDifferentNamespaceAttributes ()
  261. {
  262. XmlDocument doc = new XmlDocument ();
  263. doc.LoadXml ("<root xmlns:foo='urn:dummy' xmlns:html='http://www.w3.org/1999/xhtml' html:style='font-size: 1em'></root>");
  264. Assert (doc.OuterXml.IndexOf ("xmlns:html=\"http://www.w3.org/1999/xhtml\"") > 0);
  265. }
  266. public void TestInnerTextAndEvent ()
  267. {
  268. XmlDocument doc = new XmlDocument ();
  269. doc.LoadXml ("<root><child>text</child><child2><![CDATA[cdata]]></child2></root>");
  270. doc.NodeInserted += new XmlNodeChangedEventHandler (
  271. OnNodeInserted);
  272. doc.NodeRemoved += new XmlNodeChangedEventHandler (
  273. OnNodeRemoved);
  274. // If only one child of the element is Text node,
  275. // then no events are fired.
  276. doc.DocumentElement.FirstChild.InnerText = "no events fired.";
  277. AssertEquals ("NoInsertEventFired", false, Inserted);
  278. AssertEquals ("NoRemoveEventFired", false, Removed);
  279. AssertEquals ("SetInnerTextToSingleText", "no events fired.", doc.DocumentElement.FirstChild.InnerText);
  280. Inserted = false;
  281. Removed = false;
  282. // if only one child of the element is CDataSection,
  283. // then events are fired.
  284. doc.DocumentElement.LastChild.InnerText = "events are fired.";
  285. AssertEquals ("InsertedEventFired", true, Inserted);
  286. AssertEquals ("RemovedEventFired", true, Removed);
  287. AssertEquals ("SetInnerTextToCDataSection", "events are fired.", doc.DocumentElement.LastChild.InnerText);
  288. }
  289. }
  290. }