XmlDocumentTests.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. // System.Xml.XmlDocumentTests
  2. //
  3. // Authors:
  4. // Jason Diamond <[email protected]>
  5. // Kral Ferch <[email protected]>
  6. // Martin Willemoes Hansen <[email protected]>
  7. //
  8. // (C) 2002 Jason Diamond, Kral Ferch
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Xml;
  14. using System.IO;
  15. using System.Text;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.Xml
  18. {
  19. [TestFixture]
  20. public class XmlDocumentTests
  21. {
  22. private XmlDocument document;
  23. private ArrayList eventStrings = new ArrayList();
  24. // These Event* methods support the TestEventNode* Tests in this file.
  25. // Most of them are event handlers for the XmlNodeChangedEventHandler
  26. // delegate.
  27. private void EventStringAdd(string eventName, XmlNodeChangedEventArgs e)
  28. {
  29. string oldParent = (e.OldParent != null) ? e.OldParent.Name : "<none>";
  30. string newParent = (e.NewParent != null) ? e.NewParent.Name : "<none>";
  31. eventStrings.Add (String.Format ("{0}, {1}, {2}, {3}, {4}", eventName, e.Action.ToString (), e.Node.OuterXml, oldParent, newParent));
  32. }
  33. private void EventNodeChanged(Object sender, XmlNodeChangedEventArgs e)
  34. {
  35. EventStringAdd ("NodeChanged", e);
  36. }
  37. private void EventNodeChanging (Object sender, XmlNodeChangedEventArgs e)
  38. {
  39. EventStringAdd ("NodeChanging", e);
  40. }
  41. private void EventNodeChangingException (Object sender, XmlNodeChangedEventArgs e)
  42. {
  43. throw new Exception ("don't change the value.");
  44. }
  45. private void EventNodeInserted(Object sender, XmlNodeChangedEventArgs e)
  46. {
  47. EventStringAdd ("NodeInserted", e);
  48. }
  49. private void EventNodeInserting(Object sender, XmlNodeChangedEventArgs e)
  50. {
  51. EventStringAdd ("NodeInserting", e);
  52. }
  53. private void EventNodeInsertingException(Object sender, XmlNodeChangedEventArgs e)
  54. {
  55. throw new Exception ("don't insert the element.");
  56. }
  57. private void EventNodeRemoved(Object sender, XmlNodeChangedEventArgs e)
  58. {
  59. EventStringAdd ("NodeRemoved", e);
  60. }
  61. private void EventNodeRemoving(Object sender, XmlNodeChangedEventArgs e)
  62. {
  63. EventStringAdd ("NodeRemoving", e);
  64. }
  65. private void EventNodeRemovingException(Object sender, XmlNodeChangedEventArgs e)
  66. {
  67. throw new Exception ("don't remove the element.");
  68. }
  69. [SetUp]
  70. public void GetReady ()
  71. {
  72. document = new XmlDocument ();
  73. document.PreserveWhitespace = true;
  74. }
  75. [Test]
  76. public void CreateNodeNodeTypeNameEmptyParams ()
  77. {
  78. XmlNode node;
  79. try {
  80. node = document.CreateNode (null, null, null);
  81. Assertion.Fail ("Expected an ArgumentException to be thrown.");
  82. } catch (ArgumentException) {}
  83. try {
  84. node = document.CreateNode ("attribute", null, null);
  85. Assertion.Fail ("Expected a NullReferenceException to be thrown.");
  86. } catch (NullReferenceException) {}
  87. try {
  88. node = document.CreateNode ("attribute", "", null);
  89. Assertion.Fail ("Expected an ArgumentException to be thrown.");
  90. } catch (ArgumentException) {}
  91. try {
  92. node = document.CreateNode ("element", null, null);
  93. Assertion.Fail ("Expected a NullReferenceException to be thrown.");
  94. } catch (NullReferenceException) {}
  95. try {
  96. node = document.CreateNode ("element", "", null);
  97. Assertion.Fail ("Expected an ArgumentException to be thrown.");
  98. } catch (ArgumentException) {}
  99. try {
  100. node = document.CreateNode ("entityreference", null, null);
  101. Assertion.Fail ("Expected a NullReferenceException to be thrown.");
  102. } catch (NullReferenceException) {}
  103. }
  104. [Test]
  105. public void CreateNodeInvalidXmlNodeType ()
  106. {
  107. XmlNode node;
  108. try {
  109. node = document.CreateNode (XmlNodeType.EndElement, null, null);
  110. Assertion.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  111. } catch (ArgumentOutOfRangeException) {}
  112. try {
  113. node = document.CreateNode (XmlNodeType.EndEntity, null, null);
  114. Assertion.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  115. } catch (ArgumentOutOfRangeException) {}
  116. try {
  117. node = document.CreateNode (XmlNodeType.Entity, null, null);
  118. Assertion.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  119. } catch (ArgumentOutOfRangeException) {}
  120. try {
  121. node = document.CreateNode (XmlNodeType.None, null, null);
  122. Assertion.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  123. } catch (ArgumentOutOfRangeException) {}
  124. try {
  125. node = document.CreateNode (XmlNodeType.Notation, null, null);
  126. Assertion.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  127. } catch (ArgumentOutOfRangeException) {}
  128. // TODO: undocumented allowable type.
  129. node = document.CreateNode (XmlNodeType.XmlDeclaration, null, null);
  130. Assertion.AssertEquals (XmlNodeType.XmlDeclaration, node.NodeType);
  131. }
  132. [Test]
  133. public void CreateNodeWhichParamIsUsed ()
  134. {
  135. XmlNode node;
  136. // No constructor params for Document, DocumentFragment.
  137. node = document.CreateNode (XmlNodeType.CDATA, "a", "b", "c");
  138. Assertion.AssertEquals (String.Empty, ((XmlCDataSection)node).Value);
  139. node = document.CreateNode (XmlNodeType.Comment, "a", "b", "c");
  140. Assertion.AssertEquals (String.Empty, ((XmlComment)node).Value);
  141. node = document.CreateNode (XmlNodeType.DocumentType, "a", "b", "c");
  142. Assertion.AssertNull (((XmlDocumentType)node).Value);
  143. // TODO: add this back in to test when it's implemented.
  144. // node = document.CreateNode (XmlNodeType.EntityReference, "a", "b", "c");
  145. // Assertion.AssertNull (((XmlEntityReference)node).Value);
  146. node = document.CreateNode (XmlNodeType.ProcessingInstruction, "a", "b", "c");
  147. Assertion.AssertEquals (String.Empty, ((XmlProcessingInstruction)node).Value);
  148. node = document.CreateNode (XmlNodeType.SignificantWhitespace, "a", "b", "c");
  149. Assertion.AssertEquals (String.Empty, ((XmlSignificantWhitespace)node).Value);
  150. node = document.CreateNode (XmlNodeType.Text, "a", "b", "c");
  151. Assertion.AssertEquals (String.Empty, ((XmlText)node).Value);
  152. node = document.CreateNode (XmlNodeType.Whitespace, "a", "b", "c");
  153. Assertion.AssertEquals (String.Empty, ((XmlWhitespace)node).Value);
  154. node = document.CreateNode (XmlNodeType.XmlDeclaration, "a", "b", "c");
  155. Assertion.AssertEquals ("version=\"1.0\"", ((XmlDeclaration)node).Value);
  156. }
  157. [Test]
  158. public void CreateNodeNodeTypeName ()
  159. {
  160. XmlNode node;
  161. try {
  162. node = document.CreateNode ("foo", null, null);
  163. Assertion.Fail ("Expected an ArgumentException to be thrown.");
  164. } catch (ArgumentException) {}
  165. node = document.CreateNode("attribute", "foo", null);
  166. Assertion.AssertEquals (XmlNodeType.Attribute, node.NodeType);
  167. node = document.CreateNode("cdatasection", null, null);
  168. Assertion.AssertEquals (XmlNodeType.CDATA, node.NodeType);
  169. node = document.CreateNode("comment", null, null);
  170. Assertion.AssertEquals (XmlNodeType.Comment, node.NodeType);
  171. node = document.CreateNode("document", null, null);
  172. Assertion.AssertEquals (XmlNodeType.Document, node.NodeType);
  173. // TODO: test which constructor this ended up calling,
  174. // i.e. reuse underlying NameTable or not?
  175. // TODO: add this back in to test when it's implemented.
  176. // node = document.CreateNode("documentfragment", null, null);
  177. // Assertion.AssertEquals (XmlNodeType.DocumentFragment, node.NodeType);
  178. node = document.CreateNode("documenttype", null, null);
  179. Assertion.AssertEquals (XmlNodeType.DocumentType, node.NodeType);
  180. node = document.CreateNode("element", "foo", null);
  181. Assertion.AssertEquals (XmlNodeType.Element, node.NodeType);
  182. // TODO: add this back in to test when it's implemented.
  183. // node = document.CreateNode("entityreference", "foo", null);
  184. // Assertion.AssertEquals (XmlNodeType.EntityReference, node.NodeType);
  185. node = document.CreateNode("processinginstruction", null, null);
  186. Assertion.AssertEquals (XmlNodeType.ProcessingInstruction, node.NodeType);
  187. node = document.CreateNode("significantwhitespace", null, null);
  188. Assertion.AssertEquals (XmlNodeType.SignificantWhitespace, node.NodeType);
  189. node = document.CreateNode("text", null, null);
  190. Assertion.AssertEquals (XmlNodeType.Text, node.NodeType);
  191. node = document.CreateNode("whitespace", null, null);
  192. Assertion.AssertEquals (XmlNodeType.Whitespace, node.NodeType);
  193. }
  194. [Test]
  195. public void DocumentElement ()
  196. {
  197. Assertion.AssertNull (document.DocumentElement);
  198. XmlElement element = document.CreateElement ("foo", "bar", "http://foo/");
  199. Assertion.AssertNotNull (element);
  200. Assertion.AssertEquals ("foo", element.Prefix);
  201. Assertion.AssertEquals ("bar", element.LocalName);
  202. Assertion.AssertEquals ("http://foo/", element.NamespaceURI);
  203. Assertion.AssertEquals ("foo:bar", element.Name);
  204. Assertion.AssertSame (element, document.AppendChild (element));
  205. Assertion.AssertSame (element, document.DocumentElement);
  206. }
  207. [Test]
  208. public void DocumentEmpty()
  209. {
  210. Assertion.AssertEquals ("Incorrect output for empty document.", "", document.OuterXml);
  211. }
  212. [Test]
  213. public void EventNodeChanged()
  214. {
  215. XmlElement element;
  216. XmlComment comment;
  217. document.NodeChanged += new XmlNodeChangedEventHandler (this.EventNodeChanged);
  218. // Node that is part of the document.
  219. document.AppendChild (document.CreateElement ("foo"));
  220. comment = document.CreateComment ("bar");
  221. document.DocumentElement.AppendChild (comment);
  222. Assertion.AssertEquals ("<!--bar-->", document.DocumentElement.InnerXml);
  223. comment.Value = "baz";
  224. Assertion.Assert (eventStrings.Contains ("NodeChanged, Change, <!--baz-->, foo, foo"));
  225. Assertion.AssertEquals ("<!--baz-->", document.DocumentElement.InnerXml);
  226. // Node that isn't part of the document but created by the document.
  227. element = document.CreateElement ("foo");
  228. comment = document.CreateComment ("bar");
  229. element.AppendChild (comment);
  230. Assertion.AssertEquals ("<!--bar-->", element.InnerXml);
  231. comment.Value = "baz";
  232. Assertion.Assert (eventStrings.Contains ("NodeChanged, Change, <!--baz-->, foo, foo"));
  233. Assertion.AssertEquals ("<!--baz-->", element.InnerXml);
  234. /*
  235. TODO: Insert this when XmlNode.InnerText() and XmlNode.InnerXml() have been implemented.
  236. // Node that is part of the document.
  237. element = document.CreateElement ("foo");
  238. element.InnerText = "bar";
  239. document.AppendChild(element);
  240. element.InnerText = "baz";
  241. Assertion.Assert(eventStrings.Contains("NodeChanged, Change, baz, foo, foo"));
  242. // Node that isn't part of the document but created by the document.
  243. element = document.CreateElement("qux");
  244. element.InnerText = "quux";
  245. element.InnerText = "quuux";
  246. Assertion.Assert(eventStrings.Contains("NodeChanged, Change, quuux, qux, qux"));
  247. */
  248. }
  249. [Test]
  250. public void EventNodeChanging()
  251. {
  252. XmlElement element;
  253. XmlComment comment;
  254. document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChanging);
  255. // Node that is part of the document.
  256. document.AppendChild (document.CreateElement ("foo"));
  257. comment = document.CreateComment ("bar");
  258. document.DocumentElement.AppendChild (comment);
  259. Assertion.AssertEquals ("<!--bar-->", document.DocumentElement.InnerXml);
  260. comment.Value = "baz";
  261. Assertion.Assert (eventStrings.Contains ("NodeChanging, Change, <!--bar-->, foo, foo"));
  262. Assertion.AssertEquals ("<!--baz-->", document.DocumentElement.InnerXml);
  263. // Node that isn't part of the document but created by the document.
  264. element = document.CreateElement ("foo");
  265. comment = document.CreateComment ("bar");
  266. element.AppendChild (comment);
  267. Assertion.AssertEquals ("<!--bar-->", element.InnerXml);
  268. comment.Value = "baz";
  269. Assertion.Assert (eventStrings.Contains ("NodeChanging, Change, <!--bar-->, foo, foo"));
  270. Assertion.AssertEquals ("<!--baz-->", element.InnerXml);
  271. // If an exception is thrown the Document returns to original state.
  272. document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChangingException);
  273. element = document.CreateElement("foo");
  274. comment = document.CreateComment ("bar");
  275. element.AppendChild (comment);
  276. Assertion.AssertEquals ("<!--bar-->", element.InnerXml);
  277. try
  278. {
  279. comment.Value = "baz";
  280. Assertion.Fail("Expected an exception to be thrown by the NodeChanging event handler method EventNodeChangingException().");
  281. } catch (Exception) {}
  282. Assertion.AssertEquals ("<!--bar-->", element.InnerXml);
  283. // Yes it's a bit anal but this tests whether the node changing event exception fires before the
  284. // ArgumentOutOfRangeException. Turns out it does so that means our implementation needs to raise
  285. // the node changing event before doing any work.
  286. try
  287. {
  288. comment.ReplaceData(-1, 0, "qux");
  289. Assertion.Fail("Expected an ArgumentOutOfRangeException to be thrown.");
  290. }
  291. catch (Exception) {}
  292. /*
  293. TODO: Insert this when XmlNode.InnerText() and XmlNode.InnerXml() have been implemented.
  294. // Node that is part of the document.
  295. element = document.CreateElement ("foo");
  296. element.InnerText = "bar";
  297. document.AppendChild(element);
  298. element.InnerText = "baz";
  299. Assertion.Assert(eventStrings.Contains("NodeChanging, Change, bar, foo, foo"));
  300. // Node that isn't part of the document but created by the document.
  301. element = document.CreateElement("foo");
  302. element.InnerText = "bar";
  303. element.InnerText = "baz";
  304. Assertion.Assert(eventStrings.Contains("NodeChanging, Change, bar, foo, foo"));
  305. // If an exception is thrown the Document returns to original state.
  306. document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChangingException);
  307. element = document.CreateElement("foo");
  308. element.InnerText = "bar";
  309. try {
  310. element.InnerText = "baz";
  311. Assertion.Fail("Expected an exception to be thrown by the NodeChanging event handler method EventNodeChangingException().");
  312. } catch (Exception) {}
  313. Assertion.AssertEquals("bar", element.InnerText);
  314. */
  315. }
  316. [Test]
  317. public void EventNodeInserted()
  318. {
  319. XmlElement element;
  320. document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
  321. // Inserted 'foo' element to the document.
  322. element = document.CreateElement ("foo");
  323. document.AppendChild (element);
  324. Assertion.Assert (eventStrings.Contains ("NodeInserted, Insert, <foo />, <none>, #document"));
  325. // Append child on node in document
  326. element = document.CreateElement ("foo");
  327. document.DocumentElement.AppendChild (element);
  328. Assertion.Assert (eventStrings.Contains ("NodeInserted, Insert, <foo />, <none>, foo"));
  329. // Append child on node not in document but created by document
  330. element = document.CreateElement ("bar");
  331. element.AppendChild(document.CreateElement ("bar"));
  332. Assertion.Assert(eventStrings.Contains("NodeInserted, Insert, <bar />, <none>, bar"));
  333. }
  334. [Test]
  335. public void EventNodeInserting()
  336. {
  337. XmlElement element;
  338. document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInserting);
  339. // Inserting 'foo' element to the document.
  340. element = document.CreateElement ("foo");
  341. document.AppendChild (element);
  342. Assertion.Assert (eventStrings.Contains ("NodeInserting, Insert, <foo />, <none>, #document"));
  343. // Append child on node in document
  344. element = document.CreateElement ("foo");
  345. document.DocumentElement.AppendChild (element);
  346. Assertion.Assert(eventStrings.Contains ("NodeInserting, Insert, <foo />, <none>, foo"));
  347. // Append child on node not in document but created by document
  348. element = document.CreateElement ("bar");
  349. Assertion.AssertEquals (0, element.ChildNodes.Count);
  350. element.AppendChild (document.CreateElement ("bar"));
  351. Assertion.Assert (eventStrings.Contains ("NodeInserting, Insert, <bar />, <none>, bar"));
  352. Assertion.AssertEquals (1, element.ChildNodes.Count);
  353. // If an exception is thrown the Document returns to original state.
  354. document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInsertingException);
  355. Assertion.AssertEquals (1, element.ChildNodes.Count);
  356. try
  357. {
  358. element.AppendChild (document.CreateElement("baz"));
  359. Assertion.Fail ("Expected an exception to be thrown by the NodeInserting event handler method EventNodeInsertingException().");
  360. }
  361. catch (Exception) {}
  362. Assertion.AssertEquals (1, element.ChildNodes.Count);
  363. }
  364. [Test]
  365. public void EventNodeRemoved()
  366. {
  367. XmlElement element;
  368. XmlElement element2;
  369. document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
  370. // Removed 'bar' element from 'foo' outside document.
  371. element = document.CreateElement ("foo");
  372. element2 = document.CreateElement ("bar");
  373. element.AppendChild (element2);
  374. Assertion.AssertEquals (1, element.ChildNodes.Count);
  375. element.RemoveChild (element2);
  376. Assertion.Assert (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
  377. Assertion.AssertEquals (0, element.ChildNodes.Count);
  378. /*
  379. * TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  380. // RemoveAll.
  381. element = document.CreateElement ("foo");
  382. element2 = document.CreateElement ("bar");
  383. element.AppendChild(element2);
  384. Assertion.AssertEquals(1, element.ChildNodes.Count);
  385. element.RemoveAll();
  386. Assertion.Assert (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
  387. Assertion.AssertEquals(0, element.ChildNodes.Count);
  388. */
  389. // Removed 'bar' element from 'foo' inside document.
  390. element = document.CreateElement ("foo");
  391. document.AppendChild (element);
  392. element = document.CreateElement ("bar");
  393. document.DocumentElement.AppendChild (element);
  394. Assertion.AssertEquals (1, document.DocumentElement.ChildNodes.Count);
  395. document.DocumentElement.RemoveChild (element);
  396. Assertion.Assert (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
  397. Assertion.AssertEquals (0, document.DocumentElement.ChildNodes.Count);
  398. }
  399. [Test]
  400. public void EventNodeRemoving()
  401. {
  402. XmlElement element;
  403. XmlElement element2;
  404. document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemoving);
  405. // Removing 'bar' element from 'foo' outside document.
  406. element = document.CreateElement ("foo");
  407. element2 = document.CreateElement ("bar");
  408. element.AppendChild (element2);
  409. Assertion.AssertEquals (1, element.ChildNodes.Count);
  410. element.RemoveChild (element2);
  411. Assertion.Assert (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
  412. Assertion.AssertEquals (0, element.ChildNodes.Count);
  413. /*
  414. * TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  415. // RemoveAll.
  416. element = document.CreateElement ("foo");
  417. element2 = document.CreateElement ("bar");
  418. element.AppendChild(element2);
  419. Assertion.AssertEquals(1, element.ChildNodes.Count);
  420. element.RemoveAll();
  421. Assertion.Assert (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
  422. Assertion.AssertEquals(0, element.ChildNodes.Count);
  423. */
  424. // Removing 'bar' element from 'foo' inside document.
  425. element = document.CreateElement ("foo");
  426. document.AppendChild (element);
  427. element = document.CreateElement ("bar");
  428. document.DocumentElement.AppendChild (element);
  429. Assertion.AssertEquals (1, document.DocumentElement.ChildNodes.Count);
  430. document.DocumentElement.RemoveChild (element);
  431. Assertion.Assert (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
  432. Assertion.AssertEquals (0, document.DocumentElement.ChildNodes.Count);
  433. // If an exception is thrown the Document returns to original state.
  434. document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemovingException);
  435. element.AppendChild (element2);
  436. Assertion.AssertEquals (1, element.ChildNodes.Count);
  437. try
  438. {
  439. element.RemoveChild(element2);
  440. Assertion.Fail ("Expected an exception to be thrown by the NodeRemoving event handler method EventNodeRemovingException().");
  441. }
  442. catch (Exception) {}
  443. Assertion.AssertEquals (1, element.ChildNodes.Count);
  444. }
  445. [Test]
  446. public void GetElementsByTagNameNoNameSpace ()
  447. {
  448. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  449. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  450. <author>Tom Clancy</author><price>6.95</price></book><book>
  451. <title>Bourne Identity</title><author>Robert Ludlum</author>
  452. <price>9.95</price></book><Fluffer><Nutter><book>
  453. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  454. <price>9.95</price></book></Nutter></Fluffer></library>";
  455. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  456. document = new XmlDocument ();
  457. document.Load (memoryStream);
  458. XmlNodeList bookList = document.GetElementsByTagName ("book");
  459. Assertion.AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
  460. }
  461. [Test]
  462. public void GetElementsByTagNameUsingNameSpace ()
  463. {
  464. StringBuilder xml = new StringBuilder ();
  465. xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\" ");
  466. xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
  467. xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
  468. xml.Append ("<North:author>John Doe</North:author> " );
  469. xml.Append ("<North:price>34.95</North:price></North:book> " );
  470. xml.Append ("<South:book type=\"fiction\"> " );
  471. xml.Append ("<South:title>Bear and the Dragon</South:title> " );
  472. xml.Append ("<South:author>Tom Clancy</South:author> " );
  473. xml.Append ("<South:price>6.95</South:price></South:book> " );
  474. xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
  475. xml.Append ("<South:author>Robert Ludlum</South:author> " );
  476. xml.Append ("<South:price>9.95</South:price></South:book></library>");
  477. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  478. document = new XmlDocument ();
  479. document.Load (memoryStream);
  480. XmlNodeList bookList = document.GetElementsByTagName ("book", "http://www.goo.com");
  481. Assertion.AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 2, bookList.Count);
  482. }
  483. [Test]
  484. public void InnerAndOuterXml ()
  485. {
  486. Assertion.AssertEquals (String.Empty, document.InnerXml);
  487. Assertion.AssertEquals (document.InnerXml, document.OuterXml);
  488. XmlDeclaration declaration = document.CreateXmlDeclaration ("1.0", null, null);
  489. document.AppendChild (declaration);
  490. Assertion.AssertEquals ("<?xml version=\"1.0\"?>", document.InnerXml);
  491. Assertion.AssertEquals (document.InnerXml, document.OuterXml);
  492. XmlElement element = document.CreateElement ("foo");
  493. document.AppendChild (element);
  494. Assertion.AssertEquals ("<?xml version=\"1.0\"?><foo />", document.InnerXml);
  495. Assertion.AssertEquals (document.InnerXml, document.OuterXml);
  496. XmlComment comment = document.CreateComment ("bar");
  497. document.DocumentElement.AppendChild (comment);
  498. Assertion.AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar--></foo>", document.InnerXml);
  499. Assertion.AssertEquals (document.InnerXml, document.OuterXml);
  500. XmlText text = document.CreateTextNode ("baz");
  501. document.DocumentElement.AppendChild (text);
  502. Assertion.AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar-->baz</foo>", document.InnerXml);
  503. Assertion.AssertEquals (document.InnerXml, document.OuterXml);
  504. element = document.CreateElement ("quux");
  505. element.SetAttribute ("quuux", "squonk");
  506. document.DocumentElement.AppendChild (element);
  507. Assertion.AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar-->baz<quux quuux=\"squonk\" /></foo>", document.InnerXml);
  508. Assertion.AssertEquals (document.InnerXml, document.OuterXml);
  509. }
  510. [Test]
  511. public void LoadWithSystemIOStream ()
  512. {
  513. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  514. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  515. <author>Tom Clancy</author><price>6.95</price></book><book>
  516. <title>Bourne Identity</title><author>Robert Ludlum</author>
  517. <price>9.95</price></book><Fluffer><Nutter><book>
  518. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  519. <price>9.95</price></book></Nutter></Fluffer></library>";
  520. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  521. document = new XmlDocument ();
  522. document.Load (memoryStream);
  523. Assertion.AssertEquals ("Not Loaded From IOStream", true, document.HasChildNodes);
  524. }
  525. [Test]
  526. public void LoadXmlCDATA ()
  527. {
  528. document.LoadXml ("<foo><![CDATA[bar]]></foo>");
  529. Assertion.Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.CDATA);
  530. Assertion.AssertEquals ("bar", document.DocumentElement.FirstChild.Value);
  531. }
  532. [Test]
  533. public void LoadXMLComment()
  534. {
  535. // XmlTextReader needs to throw this exception
  536. // try {
  537. // document.LoadXml("<!--foo-->");
  538. // Assertion.Fail("XmlException should have been thrown.");
  539. // }
  540. // catch (XmlException e) {
  541. // Assertion.AssertEquals("Exception message doesn't match.", "The root element is missing.", e.Message);
  542. // }
  543. document.LoadXml ("<foo><!--Comment--></foo>");
  544. Assertion.Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.Comment);
  545. Assertion.AssertEquals ("Comment", document.DocumentElement.FirstChild.Value);
  546. document.LoadXml (@"<foo><!--bar--></foo>");
  547. Assertion.AssertEquals ("Incorrect target.", "bar", ((XmlComment)document.FirstChild.FirstChild).Data);
  548. }
  549. [Test]
  550. public void LoadXmlElementSingle ()
  551. {
  552. Assertion.AssertNull (document.DocumentElement);
  553. document.LoadXml ("<foo/>");
  554. Assertion.AssertNotNull (document.DocumentElement);
  555. Assertion.AssertSame (document.FirstChild, document.DocumentElement);
  556. Assertion.AssertEquals (String.Empty, document.DocumentElement.Prefix);
  557. Assertion.AssertEquals ("foo", document.DocumentElement.LocalName);
  558. Assertion.AssertEquals (String.Empty, document.DocumentElement.NamespaceURI);
  559. Assertion.AssertEquals ("foo", document.DocumentElement.Name);
  560. }
  561. [Test]
  562. public void LoadXmlElementWithAttributes ()
  563. {
  564. Assertion.AssertNull (document.DocumentElement);
  565. document.LoadXml ("<foo bar='baz' quux='quuux' hoge='hello &amp; world' />");
  566. XmlElement documentElement = document.DocumentElement;
  567. Assertion.AssertEquals ("baz", documentElement.GetAttribute ("bar"));
  568. Assertion.AssertEquals ("quuux", documentElement.GetAttribute ("quux"));
  569. Assertion.AssertEquals ("hello & world", documentElement.GetAttribute ("hoge"));
  570. Assertion.AssertEquals ("hello & world", documentElement.Attributes ["hoge"].Value);
  571. Assertion.AssertEquals (1, documentElement.GetAttributeNode ("hoge").ChildNodes.Count);
  572. }
  573. [Test]
  574. public void LoadXmlElementWithChildElement ()
  575. {
  576. document.LoadXml ("<foo><bar/></foo>");
  577. Assertion.Assert (document.ChildNodes.Count == 1);
  578. Assertion.Assert (document.FirstChild.ChildNodes.Count == 1);
  579. Assertion.AssertEquals ("foo", document.DocumentElement.LocalName);
  580. Assertion.AssertEquals ("bar", document.DocumentElement.FirstChild.LocalName);
  581. }
  582. [Test]
  583. public void LoadXmlElementWithTextNode ()
  584. {
  585. document.LoadXml ("<foo>bar</foo>");
  586. Assertion.Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.Text);
  587. Assertion.AssertEquals ("bar", document.DocumentElement.FirstChild.Value);
  588. }
  589. [Test]
  590. public void LoadXmlExceptionClearsDocument ()
  591. {
  592. document.LoadXml ("<foo/>");
  593. Assertion.Assert (document.FirstChild != null);
  594. try {
  595. document.LoadXml ("<123/>");
  596. Assertion.Fail ("An XmlException should have been thrown.");
  597. } catch (XmlException) {}
  598. Assertion.Assert (document.FirstChild == null);
  599. }
  600. [Test]
  601. public void LoadXmlProcessingInstruction ()
  602. {
  603. document.LoadXml (@"<?foo bar='baaz' quux='quuux'?><quuuux></quuuux>");
  604. Assertion.AssertEquals ("Incorrect target.", "foo", ((XmlProcessingInstruction)document.FirstChild).Target);
  605. Assertion.AssertEquals ("Incorrect data.", "bar='baaz' quux='quuux'", ((XmlProcessingInstruction)document.FirstChild).Data);
  606. }
  607. [Test]
  608. public void OuterXml ()
  609. {
  610. string xml;
  611. xml = "<root><![CDATA[foo]]></root>";
  612. document.LoadXml (xml);
  613. Assertion.AssertEquals("XmlDocument with cdata OuterXml is incorrect.", xml, document.OuterXml);
  614. xml = "<root><!--foo--></root>";
  615. document.LoadXml (xml);
  616. Assertion.AssertEquals("XmlDocument with comment OuterXml is incorrect.", xml, document.OuterXml);
  617. xml = "<root><?foo bar?></root>";
  618. document.LoadXml (xml);
  619. Assertion.AssertEquals("XmlDocument with processing instruction OuterXml is incorrect.", xml, document.OuterXml);
  620. }
  621. [Test]
  622. public void ParentNodes ()
  623. {
  624. document.LoadXml ("<foo><bar><baz/></bar></foo>");
  625. XmlNode node = document.FirstChild.FirstChild.FirstChild;
  626. Assertion.AssertEquals ("Wrong child found.", "baz", node.LocalName);
  627. Assertion.AssertEquals ("Wrong parent.", "bar", node.ParentNode.LocalName);
  628. Assertion.AssertEquals ("Wrong parent.", "foo", node.ParentNode.ParentNode.LocalName);
  629. Assertion.AssertEquals ("Wrong parent.", "#document", node.ParentNode.ParentNode.ParentNode.LocalName);
  630. Assertion.AssertNull ("Expected parent to be null.", node.ParentNode.ParentNode.ParentNode.ParentNode);
  631. }
  632. [Test]
  633. public void RemovedElementNextSibling ()
  634. {
  635. XmlNode node;
  636. XmlNode nextSibling;
  637. document.LoadXml ("<foo><child1/><child2/></foo>");
  638. node = document.DocumentElement.FirstChild;
  639. document.DocumentElement.RemoveChild (node);
  640. nextSibling = node.NextSibling;
  641. Assertion.AssertNull ("Expected removed node's next sibling to be null.", nextSibling);
  642. }
  643. // ImportNode
  644. [Test]
  645. public void ImportNode ()
  646. {
  647. XmlNode n;
  648. string xlinkURI = "http://www.w3.org/1999/XLink";
  649. string xml1 = "<?xml version='1.0' encoding='utf-8' ?><foo xmlns:xlink='" + xlinkURI + "'><bar a1='v1' xlink:href='#foo'><baz><![CDATA[cdata section.\n\titem 1\n\titem 2\n]]>From here, simple text node.</baz></bar></foo>";
  650. document.LoadXml(xml1);
  651. XmlDocument newDoc = new XmlDocument();
  652. newDoc.LoadXml("<hoge><fuga /></hoge>");
  653. XmlElement bar = document.DocumentElement.FirstChild as XmlElement;
  654. // Attribute
  655. n = newDoc.ImportNode(bar.GetAttributeNode("href", xlinkURI), true);
  656. Assertion.AssertEquals("#ImportNode.Attr.NS.LocalName", "href", n.LocalName);
  657. Assertion.AssertEquals("#ImportNode.Attr.NS.NSURI", xlinkURI, n.NamespaceURI);
  658. Assertion.AssertEquals("#ImportNode.Attr.NS.Value", "#foo", n.Value);
  659. // CDATA
  660. n = newDoc.ImportNode(bar.FirstChild.FirstChild, true);
  661. Assertion.AssertEquals("#ImportNode.CDATA", "cdata section.\n\titem 1\n\titem 2\n", n.Value);
  662. // Element
  663. XmlElement e = newDoc.ImportNode(bar, true) as XmlElement;
  664. Assertion.AssertEquals("#ImportNode.Element.Name", "bar", e.Name);
  665. Assertion.AssertEquals("#ImportNode.Element.Attr", "#foo", e.GetAttribute("href", xlinkURI));
  666. Assertion.AssertEquals("#ImportNode.Element.deep", "baz", e.FirstChild.Name);
  667. // Entity Reference:
  668. // [2002/10/14] CreateEntityReference was not implemented.
  669. // document.LoadXml("<!DOCTYPE test PUBLIC 'dummy' [<!ENTITY FOOENT 'foo'>]><root>&FOOENT;</root>");
  670. // n = newDoc.ImportNode(document.DocumentElement.FirstChild);
  671. // Assertion.AssertEquals("#ImportNode.EntityReference", "FOOENT", n.Name);
  672. // Assertion.AssertEquals("#ImportNode.EntityReference", "foo_", n.Value);
  673. // Processing Instruction
  674. document.LoadXml("<foo><?xml-stylesheet href='foo.xsl' ?></foo>");
  675. XmlProcessingInstruction pi = (XmlProcessingInstruction)newDoc.ImportNode(document.DocumentElement.FirstChild, false);
  676. Assertion.AssertEquals("#ImportNode.ProcessingInstruction.Name", "xml-stylesheet", pi.Name);
  677. Assertion.AssertEquals("#ImportNode.ProcessingInstruction.Data", "href='foo.xsl'", pi.Data.Trim());
  678. // Text
  679. document.LoadXml(xml1);
  680. n = newDoc.ImportNode((XmlText)bar.FirstChild.ChildNodes[1], true);
  681. Assertion.AssertEquals("#ImportNode.Text", "From here, simple text node.", n.Value);
  682. // XmlDeclaration
  683. document.LoadXml(xml1);
  684. XmlDeclaration decl = (XmlDeclaration)newDoc.ImportNode(document.FirstChild, false);
  685. Assertion.AssertEquals("#ImportNode.XmlDeclaration.Type", XmlNodeType.XmlDeclaration, decl.NodeType);
  686. Assertion.AssertEquals("#ImportNode.XmlDeclaration.Encoding", "utf-8", decl.Encoding);
  687. }
  688. [Test]
  689. public void NameTable()
  690. {
  691. XmlDocument doc = new XmlDocument();
  692. Assertion.AssertNotNull(doc.NameTable);
  693. }
  694. [Test]
  695. public void SingleEmptyRootDocument()
  696. {
  697. XmlDocument doc = new XmlDocument();
  698. doc.LoadXml("<root />");
  699. Assertion.AssertNotNull(doc.DocumentElement);
  700. }
  701. [Test]
  702. public void DocumentWithDoctypeDecl ()
  703. {
  704. XmlDocument doc = new XmlDocument ();
  705. try {
  706. doc.LoadXml ("<!DOCTYPE test><root />");
  707. } catch (XmlException) {
  708. Assertion.Fail ("#DoctypeDecl.OnlyName");
  709. }
  710. #if NetworkEnabled
  711. try
  712. {
  713. doc.LoadXml ("<!DOCTYPE test SYSTEM 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><root />");
  714. } catch (XmlException) {
  715. Assertion.Fail("#DoctypeDecl.System");
  716. }
  717. try {
  718. doc.LoadXml ("<!DOCTYPE test PUBLIC '-//test' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><root />");
  719. } catch (XmlException) {
  720. Assertion.Fail ("#DoctypeDecl.Public");
  721. }
  722. #endif
  723. // Should this be commented out?
  724. try {
  725. doc.LoadXml ("<!DOCTYPE test [<!ELEMENT foo >]><root />");
  726. } catch (XmlException) {
  727. Assertion.Fail("#DoctypeDecl.ElementDecl");
  728. }
  729. }
  730. [Test]
  731. public void CloneNode ()
  732. {
  733. XmlDocument doc = new XmlDocument ();
  734. doc.LoadXml ("<foo><bar /><baz hoge='fuga'>TEST Text</baz></foo>");
  735. XmlDocument doc2 = (XmlDocument)doc.CloneNode (false);
  736. Assertion.AssertEquals ("ShallowCopy", 0, doc2.ChildNodes.Count);
  737. doc2 = (XmlDocument)doc.CloneNode (true);
  738. Assertion.AssertEquals ("DeepCopy", "foo", doc2.DocumentElement.Name);
  739. }
  740. [Test]
  741. public void OuterXmlWithDefaultXmlns ()
  742. {
  743. XmlDocument doc = new XmlDocument ();
  744. doc.LoadXml ("<iq type=\"get\" id=\"ATECLIENT_1\"><query xmlns=\"jabber:iq:auth\"><username></username></query></iq>");
  745. Assertion.AssertEquals ("<iq type=\"get\" id=\"ATECLIENT_1\"><query xmlns=\"jabber:iq:auth\"><username /></query></iq>", doc.OuterXml);
  746. }
  747. [Test]
  748. public void PreserveWhitespace ()
  749. {
  750. string input =
  751. "<?xml version=\"1.0\" encoding=\"utf-8\" ?><!-- --> <foo/>";
  752. XmlDocument dom = new XmlDocument ();
  753. XmlTextReader reader = new XmlTextReader (new StringReader (input));
  754. dom.Load (reader);
  755. Assertion.AssertEquals (XmlNodeType.Element, dom.FirstChild.NextSibling.NextSibling.NodeType);
  756. }
  757. // [Test] Comment out in the meantime.
  758. public void LoadExternalUri ()
  759. {
  760. // set any URL of well-formed XML.
  761. document.Load ("http://www.go-mono.com/index.rss");
  762. }
  763. [Test]
  764. public void LoadDocumentWithIgnoreSection ()
  765. {
  766. // set any URL of well-formed XML.
  767. document.Load ("xmlfiles/test.xml");
  768. }
  769. }
  770. }