XmlDocumentTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using System;
  2. using System.Xml;
  3. using NUnit.Framework;
  4. namespace MonoTests.System.Xml
  5. {
  6. public class XmlDocumentTests : TestCase
  7. {
  8. public XmlDocumentTests () : base ("MonoTests.System.Xml.XmlDocumentTests testsuite") {}
  9. public XmlDocumentTests (string name) : base (name) {}
  10. private XmlDocument document;
  11. protected override void SetUp ()
  12. {
  13. document = new XmlDocument ();
  14. }
  15. public void TestCreateNodeNodeTypeNameEmptyParams ()
  16. {
  17. XmlNode node;
  18. try {
  19. node = document.CreateNode (null, null, null);
  20. Fail ("Expected an ArgumentException to be thrown.");
  21. } catch (ArgumentException) {}
  22. try {
  23. node = document.CreateNode ("attribute", null, null);
  24. Fail ("Expected a NullReferenceException to be thrown.");
  25. } catch (NullReferenceException) {}
  26. try {
  27. node = document.CreateNode ("attribute", "", null);
  28. Fail ("Expected an ArgumentException to be thrown.");
  29. } catch (ArgumentException) {}
  30. try {
  31. node = document.CreateNode ("element", null, null);
  32. Fail ("Expected a NullReferenceException to be thrown.");
  33. } catch (NullReferenceException) {}
  34. try {
  35. node = document.CreateNode ("element", "", null);
  36. Fail ("Expected an ArgumentException to be thrown.");
  37. } catch (ArgumentException) {}
  38. try {
  39. node = document.CreateNode ("entityreference", null, null);
  40. Fail ("Expected a NullReferenceException to be thrown.");
  41. } catch (NullReferenceException) {}
  42. }
  43. public void TestCreateNodeInvalidXmlNodeType ()
  44. {
  45. XmlNode node;
  46. try {
  47. node = document.CreateNode (XmlNodeType.EndElement, null, null);
  48. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  49. } catch (ArgumentOutOfRangeException) {}
  50. try {
  51. node = document.CreateNode (XmlNodeType.EndEntity, null, null);
  52. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  53. } catch (ArgumentOutOfRangeException) {}
  54. try {
  55. node = document.CreateNode (XmlNodeType.Entity, null, null);
  56. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  57. } catch (ArgumentOutOfRangeException) {}
  58. try {
  59. node = document.CreateNode (XmlNodeType.None, null, null);
  60. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  61. } catch (ArgumentOutOfRangeException) {}
  62. try {
  63. node = document.CreateNode (XmlNodeType.Notation, null, null);
  64. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  65. } catch (ArgumentOutOfRangeException) {}
  66. // TODO: undocumented allowable type.
  67. node = document.CreateNode (XmlNodeType.XmlDeclaration, null, null);
  68. AssertEquals (XmlNodeType.XmlDeclaration, node.NodeType);
  69. }
  70. public void TestCreateNodeWhichParamIsUsed ()
  71. {
  72. XmlNode node;
  73. // No constructor params for Document, DocumentFragment.
  74. node = document.CreateNode (XmlNodeType.CDATA, "a", "b", "c");
  75. AssertEquals (String.Empty, ((XmlCDataSection)node).Value);
  76. node = document.CreateNode (XmlNodeType.Comment, "a", "b", "c");
  77. AssertEquals (String.Empty, ((XmlComment)node).Value);
  78. node = document.CreateNode (XmlNodeType.DocumentType, "a", "b", "c");
  79. AssertNull (((XmlDocumentType)node).Value);
  80. // TODO: add this back in to test when it's implemented.
  81. // node = document.CreateNode (XmlNodeType.EntityReference, "a", "b", "c");
  82. // AssertNull (((XmlEntityReference)node).Value);
  83. node = document.CreateNode (XmlNodeType.ProcessingInstruction, "a", "b", "c");
  84. AssertEquals (String.Empty, ((XmlProcessingInstruction)node).Value);
  85. node = document.CreateNode (XmlNodeType.SignificantWhitespace, "a", "b", "c");
  86. AssertEquals (String.Empty, ((XmlSignificantWhitespace)node).Value);
  87. node = document.CreateNode (XmlNodeType.Text, "a", "b", "c");
  88. AssertEquals (String.Empty, ((XmlText)node).Value);
  89. node = document.CreateNode (XmlNodeType.Whitespace, "a", "b", "c");
  90. AssertEquals (String.Empty, ((XmlWhitespace)node).Value);
  91. node = document.CreateNode (XmlNodeType.XmlDeclaration, "a", "b", "c");
  92. AssertEquals ("version=\"1.0\"", ((XmlDeclaration)node).Value);
  93. }
  94. public void TestCreateNodeNodeTypeName ()
  95. {
  96. XmlNode node;
  97. try {
  98. node = document.CreateNode ("foo", null, null);
  99. Fail ("Expected an ArgumentException to be thrown.");
  100. } catch (ArgumentException) {}
  101. node = document.CreateNode("attribute", "foo", null);
  102. AssertEquals (XmlNodeType.Attribute, node.NodeType);
  103. node = document.CreateNode("cdatasection", null, null);
  104. AssertEquals (XmlNodeType.CDATA, node.NodeType);
  105. node = document.CreateNode("comment", null, null);
  106. AssertEquals (XmlNodeType.Comment, node.NodeType);
  107. node = document.CreateNode("document", null, null);
  108. AssertEquals (XmlNodeType.Document, node.NodeType);
  109. // TODO: test which constructor this ended up calling,
  110. // i.e. reuse underlying NameTable or not?
  111. // TODO: add this back in to test when it's implemented.
  112. // node = document.CreateNode("documentfragment", null, null);
  113. // AssertEquals (XmlNodeType.DocumentFragment, node.NodeType);
  114. node = document.CreateNode("documenttype", null, null);
  115. AssertEquals (XmlNodeType.DocumentType, node.NodeType);
  116. node = document.CreateNode("element", "foo", null);
  117. AssertEquals (XmlNodeType.Element, node.NodeType);
  118. // TODO: add this back in to test when it's implemented.
  119. // node = document.CreateNode("entityreference", "foo", null);
  120. // AssertEquals (XmlNodeType.EntityReference, node.NodeType);
  121. node = document.CreateNode("processinginstruction", null, null);
  122. AssertEquals (XmlNodeType.ProcessingInstruction, node.NodeType);
  123. node = document.CreateNode("significantwhitespace", null, null);
  124. AssertEquals (XmlNodeType.SignificantWhitespace, node.NodeType);
  125. node = document.CreateNode("text", null, null);
  126. AssertEquals (XmlNodeType.Text, node.NodeType);
  127. node = document.CreateNode("whitespace", null, null);
  128. AssertEquals (XmlNodeType.Whitespace, node.NodeType);
  129. }
  130. public void TestDocumentElement ()
  131. {
  132. AssertNull (document.DocumentElement);
  133. XmlElement element = document.CreateElement ("foo", "bar", "http://foo/");
  134. AssertNotNull (element);
  135. AssertEquals ("foo", element.Prefix);
  136. AssertEquals ("bar", element.LocalName);
  137. AssertEquals ("http://foo/", element.NamespaceURI);
  138. AssertEquals ("foo:bar", element.Name);
  139. AssertSame (element, document.AppendChild (element));
  140. AssertSame (element, document.DocumentElement);
  141. }
  142. public void TestDocumentEmpty()
  143. {
  144. AssertEquals ("Incorrect output for empty document.", "", document.OuterXml);
  145. }
  146. public void TestInnerAndOuterXml ()
  147. {
  148. AssertEquals (String.Empty, document.InnerXml);
  149. AssertEquals (document.InnerXml, document.OuterXml);
  150. XmlDeclaration declaration = document.CreateXmlDeclaration ("1.0", null, null);
  151. document.AppendChild (declaration);
  152. AssertEquals ("<?xml version=\"1.0\"?>", document.InnerXml);
  153. AssertEquals (document.InnerXml, document.OuterXml);
  154. XmlElement element = document.CreateElement ("foo");
  155. document.AppendChild (element);
  156. AssertEquals ("<?xml version=\"1.0\"?><foo />", document.InnerXml);
  157. AssertEquals (document.InnerXml, document.OuterXml);
  158. XmlComment comment = document.CreateComment ("bar");
  159. document.DocumentElement.AppendChild (comment);
  160. AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar--></foo>", document.InnerXml);
  161. AssertEquals (document.InnerXml, document.OuterXml);
  162. XmlText text = document.CreateTextNode ("baz");
  163. document.DocumentElement.AppendChild (text);
  164. AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar-->baz</foo>", document.InnerXml);
  165. AssertEquals (document.InnerXml, document.OuterXml);
  166. element = document.CreateElement ("quux");
  167. element.SetAttribute ("quuux", "squonk");
  168. document.DocumentElement.AppendChild (element);
  169. AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar-->baz<quux quuux=\"squonk\" /></foo>", document.InnerXml);
  170. AssertEquals (document.InnerXml, document.OuterXml);
  171. }
  172. public void TestLoadXmlCDATA ()
  173. {
  174. document.LoadXml ("<foo><![CDATA[bar]]></foo>");
  175. Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.CDATA);
  176. AssertEquals ("bar", document.DocumentElement.FirstChild.Value);
  177. }
  178. public void TestLoadXMLComment()
  179. {
  180. // XmlTextReader needs to throw this exception
  181. // try {
  182. // document.LoadXml("<!--foo-->");
  183. // Fail("XmlException should have been thrown.");
  184. // }
  185. // catch (XmlException e) {
  186. // AssertEquals("Exception message doesn't match.", "The root element is missing.", e.Message);
  187. // }
  188. document.LoadXml ("<foo><!--Comment--></foo>");
  189. Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.Comment);
  190. AssertEquals ("Comment", document.DocumentElement.FirstChild.Value);
  191. document.LoadXml (@"<foo><!--bar--></foo>");
  192. AssertEquals ("Incorrect target.", "bar", ((XmlComment)document.FirstChild.FirstChild).Data);
  193. }
  194. public void TestLoadXmlElementSingle ()
  195. {
  196. AssertNull (document.DocumentElement);
  197. document.LoadXml ("<foo/>");
  198. AssertNotNull (document.DocumentElement);
  199. AssertSame (document.FirstChild, document.DocumentElement);
  200. AssertEquals (String.Empty, document.DocumentElement.Prefix);
  201. AssertEquals ("foo", document.DocumentElement.LocalName);
  202. AssertEquals (String.Empty, document.DocumentElement.NamespaceURI);
  203. AssertEquals ("foo", document.DocumentElement.Name);
  204. }
  205. public void TestLoadXmlElementWithAttributes ()
  206. {
  207. AssertNull (document.DocumentElement);
  208. document.LoadXml ("<foo bar='baz' quux='quuux'/>");
  209. XmlElement documentElement = document.DocumentElement;
  210. AssertEquals ("baz", documentElement.GetAttribute ("bar"));
  211. AssertEquals ("quuux", documentElement.GetAttribute ("quux"));
  212. }
  213. public void TestLoadXmlElementWithChildElement ()
  214. {
  215. document.LoadXml ("<foo><bar/></foo>");
  216. Assert (document.ChildNodes.Count == 1);
  217. Assert (document.FirstChild.ChildNodes.Count == 1);
  218. AssertEquals ("foo", document.DocumentElement.LocalName);
  219. AssertEquals ("bar", document.DocumentElement.FirstChild.LocalName);
  220. }
  221. public void TestLoadXmlElementWithTextNode ()
  222. {
  223. document.LoadXml ("<foo>bar</foo>");
  224. Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.Text);
  225. AssertEquals ("bar", document.DocumentElement.FirstChild.Value);
  226. }
  227. public void TestLoadXmlExceptionClearsDocument ()
  228. {
  229. document.LoadXml ("<foo/>");
  230. Assert (document.FirstChild != null);
  231. try {
  232. document.LoadXml ("<123/>");
  233. Fail ("An XmlException should have been thrown.");
  234. } catch (XmlException) {}
  235. Assert (document.FirstChild == null);
  236. }
  237. public void TestLoadXmlProcessingInstruction ()
  238. {
  239. document.LoadXml (@"<?foo bar='baaz' quux='quuux'?><quuuux></quuuux>");
  240. AssertEquals ("Incorrect target.", "foo", ((XmlProcessingInstruction)document.FirstChild).Target);
  241. AssertEquals ("Incorrect data.", "bar='baaz' quux='quuux'", ((XmlProcessingInstruction)document.FirstChild).Data);
  242. }
  243. public void TestOuterXml ()
  244. {
  245. string xml;
  246. xml = "<root><![CDATA[foo]]></root>";
  247. document.LoadXml (xml);
  248. AssertEquals("XmlDocument with cdata OuterXml is incorrect.", xml, document.OuterXml);
  249. xml = "<root><!--foo--></root>";
  250. document.LoadXml (xml);
  251. AssertEquals("XmlDocument with comment OuterXml is incorrect.", xml, document.OuterXml);
  252. xml = "<root><?foo bar?></root>";
  253. document.LoadXml (xml);
  254. AssertEquals("XmlDocument with processing instruction OuterXml is incorrect.", xml, document.OuterXml);
  255. }
  256. public void TestParentNodes ()
  257. {
  258. document.LoadXml ("<foo><bar><baz/></bar></foo>");
  259. XmlNode node = document.FirstChild.FirstChild.FirstChild;
  260. AssertEquals ("Wrong child found.", "baz", node.LocalName);
  261. AssertEquals ("Wrong parent.", "bar", node.ParentNode.LocalName);
  262. AssertEquals ("Wrong parent.", "foo", node.ParentNode.ParentNode.LocalName);
  263. AssertEquals ("Wrong parent.", "#document", node.ParentNode.ParentNode.ParentNode.LocalName);
  264. AssertNull ("Expected parent to be null.", node.ParentNode.ParentNode.ParentNode.ParentNode);
  265. }
  266. public void TestRemovedElementNextSibling ()
  267. {
  268. XmlNode node;
  269. XmlNode nextSibling;
  270. document.LoadXml ("<foo><child1/><child2/></foo>");
  271. node = document.DocumentElement.FirstChild;
  272. document.DocumentElement.RemoveChild (node);
  273. nextSibling = node.NextSibling;
  274. AssertNull ("Expected removed node's next sibling to be null.", nextSibling);
  275. }
  276. }
  277. }