XmlElementTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // XmlElementTests
  3. //
  4. // Authors:
  5. // Jason Diamond ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. //
  8. // (C) 2002 Jason Diamond http://injektilo.org/
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.Xml;
  13. using System.IO;
  14. using System.Text;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlElementTests : Assertion
  20. {
  21. private XmlDocument document;
  22. [SetUp]
  23. public void GetReady ()
  24. {
  25. document = new XmlDocument ();
  26. }
  27. private void AssertElement (XmlElement element, string prefix,
  28. string localName, string namespaceURI,
  29. int attributesCount)
  30. {
  31. AssertEquals (prefix != String.Empty ? prefix + ":" + localName : localName, element.Name);
  32. AssertEquals (prefix, element.Prefix);
  33. AssertEquals (localName, element.LocalName);
  34. AssertEquals (namespaceURI, element.NamespaceURI);
  35. //AssertEquals (attributesCount, element.Attributes.Count);
  36. }
  37. // for NodeInserted Event
  38. private bool Inserted = false;
  39. private void OnNodeInserted (object o, XmlNodeChangedEventArgs e)
  40. {
  41. Inserted = true;
  42. }
  43. // for NodeChanged Event
  44. private bool Changed = false;
  45. private void OnNodeChanged (object o, XmlNodeChangedEventArgs e)
  46. {
  47. Changed = true;
  48. }
  49. // for NodeRemoved Event
  50. private bool Removed = false;
  51. private void OnNodeRemoved (object o, XmlNodeChangedEventArgs e)
  52. {
  53. Removed = true;
  54. }
  55. [Test]
  56. public void CloneNode ()
  57. {
  58. XmlElement element = document.CreateElement ("foo");
  59. XmlElement child = document.CreateElement ("bar");
  60. XmlElement grandson = document.CreateElement ("baz");
  61. element.SetAttribute ("attr1", "val1");
  62. element.SetAttribute ("attr2", "val2");
  63. element.AppendChild (child);
  64. child.SetAttribute ("attr3", "val3");
  65. child.AppendChild (grandson);
  66. document.AppendChild (element);
  67. XmlNode deep = element.CloneNode (true);
  68. // AssertEquals ("These should be the same", deep.OuterXml, element.OuterXml);
  69. AssertNull ("This is not null", deep.ParentNode);
  70. Assert ("Copies, not pointers", !Object.ReferenceEquals (element,deep));
  71. XmlNode shallow = element.CloneNode (false);
  72. AssertNull ("This is not null", shallow.ParentNode);
  73. Assert ("Copies, not pointers", !Object.ReferenceEquals (element,shallow));
  74. AssertEquals ("Shallow clones shalt have no children!", false, shallow.HasChildNodes);
  75. }
  76. [Test]
  77. public void CreateElement1 ()
  78. {
  79. XmlElement element = document.CreateElement ("name");
  80. AssertElement (element, String.Empty, "name", String.Empty, 0);
  81. }
  82. [Test]
  83. public void CreateElement1WithPrefix ()
  84. {
  85. XmlElement element = document.CreateElement ("prefix:localName");
  86. AssertElement (element, "prefix", "localName", String.Empty, 0);
  87. }
  88. [Test]
  89. public void CreateElement2 ()
  90. {
  91. XmlElement element = document.CreateElement ("qualifiedName", "namespaceURI");
  92. AssertElement (element, String.Empty, "qualifiedName",
  93. "namespaceURI", 0);
  94. }
  95. [Test]
  96. public void CreateElement2WithPrefix ()
  97. {
  98. XmlElement element = document.CreateElement ("prefix:localName", "namespaceURI");
  99. AssertElement (element, "prefix", "localName", "namespaceURI", 0);
  100. }
  101. [Test]
  102. public void CreateElement3 ()
  103. {
  104. XmlElement element = document.CreateElement ("prefix", "localName", "namespaceURI");
  105. AssertElement (element, "prefix", "localName", "namespaceURI", 0);
  106. }
  107. [Test]
  108. public void CreateElement3WithNullNamespace ()
  109. {
  110. // bug #26855, NamespaceURI should NEVER be null.
  111. XmlElement element = document.CreateElement (null, "localName", null);
  112. AssertElement (element, String.Empty, "localName", String.Empty, 0);
  113. }
  114. [Test]
  115. public void InnerAndOuterXml ()
  116. {
  117. XmlElement element;
  118. XmlText text;
  119. XmlComment comment;
  120. element = document.CreateElement ("foo");
  121. AssertEquals (String.Empty, element.InnerXml);
  122. AssertEquals ("<foo />", element.OuterXml);
  123. text = document.CreateTextNode ("bar");
  124. element.AppendChild (text);
  125. AssertEquals ("bar", element.InnerXml);
  126. AssertEquals ("<foo>bar</foo>", element.OuterXml);
  127. element.SetAttribute ("baz", "quux");
  128. AssertEquals ("bar", element.InnerXml);
  129. AssertEquals ("<foo baz=\"quux\">bar</foo>", element.OuterXml);
  130. comment = document.CreateComment ("squonk");
  131. element.AppendChild (comment);
  132. AssertEquals ("bar<!--squonk-->", element.InnerXml);
  133. AssertEquals ("<foo baz=\"quux\">bar<!--squonk--></foo>", element.OuterXml);
  134. element.RemoveAll();
  135. element.AppendChild(document.CreateElement("hoge"));
  136. AssertEquals ("<hoge />", element.InnerXml);
  137. }
  138. [Test]
  139. public void SetGetAttribute ()
  140. {
  141. XmlElement element = document.CreateElement ("foo");
  142. element.SetAttribute ("attr1", "val1");
  143. element.SetAttribute ("attr2", "val2");
  144. AssertEquals ("val1", element.GetAttribute ("attr1"));
  145. AssertEquals ("val2", element.GetAttribute ("attr2"));
  146. }
  147. [Test]
  148. public void GetElementsByTagNameNoNameSpace ()
  149. {
  150. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  151. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  152. <author>Tom Clancy</author><price>6.95</price></book><book>
  153. <title>Bourne Identity</title><author>Robert Ludlum</author>
  154. <price>9.95</price></book><Fluffer><Nutter><book>
  155. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  156. <price>9.95</price></book></Nutter></Fluffer></library>";
  157. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  158. document = new XmlDocument ();
  159. document.Load (memoryStream);
  160. XmlNodeList libraryList = document.GetElementsByTagName ("library");
  161. XmlNode xmlNode = libraryList.Item (0);
  162. XmlElement xmlElement = xmlNode as XmlElement;
  163. XmlNodeList bookList = xmlElement.GetElementsByTagName ("book");
  164. AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
  165. }
  166. [Test]
  167. public void GetElementsByTagNameUsingNameSpace ()
  168. {
  169. StringBuilder xml = new StringBuilder ();
  170. xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\" ");
  171. xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
  172. xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
  173. xml.Append ("<North:author>John Doe</North:author> " );
  174. xml.Append ("<North:price>34.95</North:price></North:book> " );
  175. xml.Append ("<South:book type=\"fiction\"> " );
  176. xml.Append ("<South:title>Bear and the Dragon</South:title> " );
  177. xml.Append ("<South:author>Tom Clancy</South:author> " );
  178. xml.Append ("<South:price>6.95</South:price></South:book> " );
  179. xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
  180. xml.Append ("<South:author>Robert Ludlum</South:author> " );
  181. xml.Append ("<South:price>9.95</South:price></South:book></library>");
  182. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  183. document = new XmlDocument ();
  184. document.Load (memoryStream);
  185. XmlNodeList libraryList = document.GetElementsByTagName ("library");
  186. XmlNode xmlNode = libraryList.Item (0);
  187. XmlElement xmlElement = xmlNode as XmlElement;
  188. XmlNodeList bookList = xmlElement.GetElementsByTagName ("book", "http://www.foo.com");
  189. AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 1, bookList.Count);
  190. }
  191. [Test]
  192. public void OuterXmlWithNamespace ()
  193. {
  194. XmlElement element = document.CreateElement ("foo", "bar", "#foo");
  195. AssertEquals ("<foo:bar xmlns:foo=\"#foo\" />", element.OuterXml);
  196. }
  197. [Test]
  198. public void RemoveAllAttributes ()
  199. {
  200. StringBuilder xml = new StringBuilder ();
  201. xml.Append ("<?xml version=\"1.0\" ?><library><book type=\"non-fiction\" price=\"34.95\"> ");
  202. xml.Append ("<title type=\"intro\">XML Fun</title> " );
  203. xml.Append ("<author>John Doe</author></book></library>");
  204. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  205. document = new XmlDocument ();
  206. document.Load (memoryStream);
  207. XmlNodeList bookList = document.GetElementsByTagName ("book");
  208. XmlNode xmlNode = bookList.Item (0);
  209. XmlElement xmlElement = xmlNode as XmlElement;
  210. xmlElement.RemoveAllAttributes ();
  211. AssertEquals ("attributes not properly removed.", false, xmlElement.HasAttribute ("type"));
  212. }
  213. [Test]
  214. public void SetAttributeNode ()
  215. {
  216. XmlDocument xmlDoc = new XmlDocument ();
  217. XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
  218. XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
  219. XmlAttribute xmlAttribute2 = xmlEl.SetAttributeNode ("attr2", "namespace2");
  220. AssertEquals ("attribute name not properly created.", true, xmlAttribute.Name.Equals ("attr1"));
  221. AssertEquals ("attribute namespace not properly created.", true, xmlAttribute.NamespaceURI.Equals ("namespace1"));
  222. }
  223. [Test]
  224. public void InnerTextAndEvent ()
  225. {
  226. XmlDocument doc = new XmlDocument ();
  227. doc.LoadXml ("<root><child>text</child><child2><![CDATA[cdata]]></child2></root>");
  228. doc.NodeInserted += new XmlNodeChangedEventHandler (
  229. OnNodeInserted);
  230. doc.NodeRemoved += new XmlNodeChangedEventHandler (
  231. OnNodeRemoved);
  232. // If only one child of the element is Text node,
  233. // then no events are fired.
  234. doc.DocumentElement.FirstChild.InnerText = "no events fired.";
  235. AssertEquals ("NoInsertEventFired", false, Inserted);
  236. AssertEquals ("NoRemoveEventFired", false, Removed);
  237. AssertEquals ("SetInnerTextToSingleText", "no events fired.", doc.DocumentElement.FirstChild.InnerText);
  238. Inserted = false;
  239. Removed = false;
  240. // if only one child of the element is CDataSection,
  241. // then events are fired.
  242. doc.DocumentElement.LastChild.InnerText = "events are fired.";
  243. AssertEquals ("InsertedEventFired", true, Inserted);
  244. AssertEquals ("RemovedEventFired", true, Removed);
  245. AssertEquals ("SetInnerTextToCDataSection", "events are fired.", doc.DocumentElement.LastChild.InnerText);
  246. }
  247. [Test]
  248. public void InnerXmlSetter ()
  249. {
  250. XmlDocument doc = new XmlDocument ();
  251. doc.LoadXml ("<root/>");
  252. XmlElement el = doc.DocumentElement;
  253. AssertNull ("#Simple", el.FirstChild);
  254. el.InnerXml = "<foo><bar att='baz'/></foo>";
  255. XmlElement child = el.FirstChild as XmlElement;
  256. AssertNotNull ("#Simple.Child", child);
  257. AssertEquals ("#Simple.Child.Name", "foo", child.LocalName);
  258. XmlElement grandchild = child.FirstChild as XmlElement;
  259. AssertNotNull ("#Simple.GrandChild", grandchild);
  260. AssertEquals ("#Simple.GrandChild.Name", "bar", grandchild.LocalName);
  261. AssertEquals ("#Simple.GrandChild.Attr", "baz", grandchild.GetAttribute ("att"));
  262. doc.LoadXml ("<root xmlns='NS0' xmlns:ns1='NS1'><foo/><ns1:bar/><ns2:bar xmlns:ns2='NS2' /></root>");
  263. el = doc.DocumentElement.FirstChild.NextSibling as XmlElement; // ns1:bar
  264. AssertNull ("#Namespaced.Prepare", el.FirstChild);
  265. el.InnerXml = "<ns1:baz />";
  266. AssertNotNull ("#Namespaced.Child", el.FirstChild);
  267. AssertEquals ("#Namespaced.Child.Name", "baz", el.FirstChild.LocalName);
  268. AssertEquals ("#Namespaced.Child.NSURI", "NS1", el.FirstChild.NamespaceURI); // important!
  269. el.InnerXml = "<hoge />";
  270. AssertEquals ("#Namespaced.VerifyPreviousCleared", "hoge", el.FirstChild.Name);
  271. }
  272. [Test]
  273. public void IsEmpty ()
  274. {
  275. document.LoadXml ("<root><foo/><bar></bar></root>");
  276. Assertion.AssertEquals ("Empty", true, ((XmlElement) document.DocumentElement.FirstChild).IsEmpty);
  277. Assertion.AssertEquals ("Empty", false, ((XmlElement) document.DocumentElement.LastChild).IsEmpty);
  278. }
  279. [Test]
  280. public void RemoveAttribute ()
  281. {
  282. string xlinkURI = "http://www.w3.org/1999/XLink";
  283. XmlDocument doc = new XmlDocument ();
  284. doc.LoadXml ("<root a1='1' a2='2' xlink:href='urn:foo' xmlns:xlink='" + xlinkURI + "' />");
  285. XmlElement el = doc.DocumentElement;
  286. el.RemoveAttribute ("a1");
  287. AssertNull ("RemoveAttribute", el.GetAttributeNode ("a1"));
  288. el.RemoveAttribute ("xlink:href");
  289. AssertNull ("RemoveAttribute", el.GetAttributeNode ("href", xlinkURI));
  290. el.RemoveAllAttributes ();
  291. AssertNull ("RemoveAllAttributes", el.GetAttributeNode ("a2"));
  292. }
  293. [Test]
  294. public void WriteToWithDefaultNamespace ()
  295. {
  296. XmlDocument doc = new XmlDocument ();
  297. doc.LoadXml ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />");
  298. StringWriter sw = new StringWriter ();
  299. XmlTextWriter xtw = new XmlTextWriter (sw);
  300. doc.DocumentElement.WriteTo (xtw);
  301. AssertEquals ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", sw.ToString());
  302. }
  303. [Test]
  304. public void WriteToWithDeletedNamespacePrefix ()
  305. {
  306. XmlDocument doc = new XmlDocument ();
  307. doc.LoadXml ("<root xmlns:foo='urn:dummy'><foo foo:bar='baz' /></root>");
  308. doc.DocumentElement.RemoveAllAttributes ();
  309. Assert (doc.DocumentElement.FirstChild.OuterXml.IndexOf("xmlns:foo") > 0);
  310. }
  311. [Test]
  312. public void WriteToWithDifferentNamespaceAttributes ()
  313. {
  314. XmlDocument doc = new XmlDocument ();
  315. doc.LoadXml ("<root xmlns:foo='urn:dummy' xmlns:html='http://www.w3.org/1999/xhtml' html:style='font-size: 1em'></root>");
  316. Assert (doc.OuterXml.IndexOf ("xmlns:html=\"http://www.w3.org/1999/xhtml\"") > 0);
  317. }
  318. [Test]
  319. public void WriteToDefaultAttribute ()
  320. {
  321. // default attributes should be ignored.
  322. string dtd = "<!DOCTYPE root[<!ATTLIST root hoge CDATA 'hoge-def'><!ENTITY foo 'ent-foo'>]>";
  323. string xml = dtd + "<root>&foo;</root>";
  324. XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document,null);
  325. xvr.EntityHandling = EntityHandling.ExpandCharEntities;
  326. xvr.ValidationType = ValidationType.None;
  327. document.Load (xvr);
  328. StringWriter sw = new StringWriter ();
  329. XmlTextWriter xtw = new XmlTextWriter (sw);
  330. document.DocumentElement.WriteTo (xtw);
  331. AssertEquals ("<root>&foo;</root>", sw.ToString ());
  332. }
  333. }
  334. }