XmlAttributeTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // XmlAttributeTests.cs : Tests for the XmlAttribute class
  2. //
  3. // Author: Mike Kestner <[email protected]>
  4. // Author: Martin Willemoes Hansen <[email protected]>
  5. //
  6. // (C) 2002 Mike Kestner
  7. // (C) 2003 Martin Willemoes Hansen
  8. using System;
  9. using System.IO;
  10. using System.Xml;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Xml
  13. {
  14. [TestFixture]
  15. public class XmlAttributeTests : Assertion
  16. {
  17. XmlDocument doc;
  18. XmlAttribute attr;
  19. bool inserted;
  20. bool changed;
  21. bool removed;
  22. [SetUp]
  23. public void GetReady()
  24. {
  25. doc = new XmlDocument ();
  26. attr = doc.CreateAttribute ("attr1");
  27. attr.Value = "val1";
  28. }
  29. private void EventNodeInserted(Object sender, XmlNodeChangedEventArgs e)
  30. {
  31. inserted = true;
  32. }
  33. private void EventNodeChanged(Object sender, XmlNodeChangedEventArgs e)
  34. {
  35. changed = true;
  36. }
  37. private void EventNodeRemoved(Object sender, XmlNodeChangedEventArgs e)
  38. {
  39. removed = true;
  40. }
  41. [Test]
  42. public void Attributes ()
  43. {
  44. AssertNull (attr.Attributes);
  45. }
  46. [Test]
  47. public void AttributeInnerAndOuterXml ()
  48. {
  49. attr = doc.CreateAttribute ("foo", "bar", "http://abc.def");
  50. attr.Value = "baz";
  51. AssertEquals ("baz", attr.InnerXml);
  52. AssertEquals ("foo:bar=\"baz\"", attr.OuterXml);
  53. }
  54. [Test]
  55. public void AttributeWithNoValue ()
  56. {
  57. XmlAttribute attribute = doc.CreateAttribute ("name");
  58. AssertEquals (String.Empty, attribute.Value);
  59. Assert (!attribute.HasChildNodes);
  60. AssertNull (attribute.FirstChild);
  61. AssertNull (attribute.LastChild);
  62. AssertEquals (0, attribute.ChildNodes.Count);
  63. }
  64. [Test]
  65. public void AttributeWithValue ()
  66. {
  67. XmlAttribute attribute = doc.CreateAttribute ("name");
  68. attribute.Value = "value";
  69. AssertEquals ("value", attribute.Value);
  70. Assert (attribute.HasChildNodes);
  71. AssertNotNull (attribute.FirstChild);
  72. AssertNotNull (attribute.LastChild);
  73. AssertEquals (1, attribute.ChildNodes.Count);
  74. AssertEquals (XmlNodeType.Text, attribute.ChildNodes [0].NodeType);
  75. AssertEquals ("value", attribute.ChildNodes [0].Value);
  76. }
  77. [Test]
  78. [ExpectedException (typeof (ArgumentException))]
  79. public void CheckPrefixWithNamespace ()
  80. {
  81. XmlDocument doc = new XmlDocument ();
  82. doc.LoadXml ("<root xmlns:foo='urn:foo' foo='attfoo' foo:foo='attfoofoo' />");
  83. // hogehoge does not match to any namespace.
  84. AssertEquals ("xmlns:foo", doc.DocumentElement.Attributes [0].Name);
  85. doc.DocumentElement.Attributes [0].Prefix="hogehoge";
  86. doc.Save (Console.Out);
  87. }
  88. [Test]
  89. public void NamespaceAttributes ()
  90. {
  91. try {
  92. doc.CreateAttribute ("", "xmlns", "urn:foo");
  93. Assertion.Fail ("Creating xmlns attribute with invalid nsuri should be error.");
  94. } catch (Exception) {
  95. }
  96. doc.LoadXml ("<root/>");
  97. try {
  98. doc.DocumentElement.SetAttribute ("xmlns", "urn:foo", "urn:bar");
  99. Assertion.Fail ("SetAttribute for xmlns with invalid nsuri should be error.");
  100. } catch (ArgumentException) {
  101. }
  102. }
  103. [Test]
  104. public void HasChildNodes ()
  105. {
  106. Assert (attr.HasChildNodes);
  107. }
  108. [Test]
  109. public void Name ()
  110. {
  111. AssertEquals ("attr1", attr.Name);
  112. }
  113. [Test]
  114. public void NodeType ()
  115. {
  116. AssertEquals (XmlNodeType.Attribute, attr.NodeType);
  117. }
  118. [Test]
  119. public void OwnerDocument ()
  120. {
  121. AssertSame (doc, attr.OwnerDocument);
  122. }
  123. [Test]
  124. public void ParentNode ()
  125. {
  126. AssertNull ("Attr parents not allowed", attr.ParentNode);
  127. }
  128. [Test]
  129. public void Value ()
  130. {
  131. AssertEquals ("val1", attr.Value);
  132. }
  133. [Test]
  134. #if NET_2_0
  135. [Category ("NotDotNet")] // enbug in 2.0
  136. #endif
  137. public void SetInnerTextAndXml ()
  138. {
  139. string original = doc.OuterXml;
  140. doc.LoadXml ("<root name='value' />");
  141. XmlAttribute attr = doc.DocumentElement.Attributes ["name"];
  142. attr.InnerText = "a&b";
  143. AssertEquals ("setInnerText", "a&b", attr.Value);
  144. attr.InnerXml = "a&amp;b";
  145. AssertEquals ("setInnerXml", "a&b", attr.Value);
  146. attr.InnerXml = "'a&amp;b'";
  147. AssertEquals ("setInnerXml.InnerXml", "'a&amp;b'", attr.InnerXml);
  148. AssertEquals ("setInnerXml.Value", "'a&b'", attr.Value);
  149. attr.InnerXml = "\"a&amp;b\"";
  150. AssertEquals ("Double_Quote", "\"a&amp;b\"", attr.InnerXml);
  151. attr.InnerXml = "\"a&amp;b'";
  152. AssertEquals ("DoubleQuoteStart_SingleQuoteEnd",
  153. "\"a&amp;b'", attr.InnerXml);
  154. attr.Value = "";
  155. XmlNodeChangedEventHandler evInserted = new XmlNodeChangedEventHandler (EventNodeInserted);
  156. XmlNodeChangedEventHandler evChanged = new XmlNodeChangedEventHandler (EventNodeChanged);
  157. XmlNodeChangedEventHandler evRemoved = new XmlNodeChangedEventHandler (EventNodeRemoved);
  158. doc.NodeInserted += evInserted;
  159. doc.NodeChanged += evChanged;
  160. doc.NodeRemoved += evRemoved;
  161. try {
  162. // set_InnerText event
  163. attr.InnerText = "fire";
  164. AssertEquals ("setInnerText.NodeInserted", false, inserted);
  165. AssertEquals ("setInnerText.NodeChanged", true, changed);
  166. AssertEquals ("setInnerText.NodeRemoved", false, removed);
  167. inserted = changed = removed = false;
  168. // set_InnerXml event
  169. attr.InnerXml = "fire";
  170. AssertEquals ("setInnserXml.NodeInserted", true, inserted);
  171. AssertEquals ("setInnserXml.NodeChanged", false, changed);
  172. AssertEquals ("setInnserXml.NodeRemoved", true, removed);
  173. inserted = changed = removed = false;
  174. } finally {
  175. doc.NodeInserted -= evInserted;
  176. doc.NodeChanged -= evChanged;
  177. doc.NodeRemoved -= evRemoved;
  178. }
  179. }
  180. private void OnSetInnerText (object o, XmlNodeChangedEventArgs e)
  181. {
  182. if(e.NewParent.Value == "fire")
  183. doc.DocumentElement.SetAttribute ("appended", "event was fired");
  184. }
  185. [Test]
  186. public void WriteTo ()
  187. {
  188. doc.AppendChild (doc.CreateElement ("root"));
  189. doc.DocumentElement.SetAttribute ("attr","");
  190. doc.DocumentElement.Attributes ["attr"].InnerXml = "&ent;";
  191. StringWriter sw = new StringWriter ();
  192. XmlTextWriter xtw = new XmlTextWriter (sw);
  193. xtw.WriteStartElement ("result");
  194. XmlAttribute attr = doc.DocumentElement.Attributes ["attr"];
  195. attr.WriteTo (xtw);
  196. xtw.Close ();
  197. Assertion.AssertEquals ("<result attr=\"&ent;\" />", sw.ToString ());
  198. }
  199. [Test]
  200. public void IdentityConstraints ()
  201. {
  202. string dtd = "<!DOCTYPE root [<!ELEMENT root (c)+><!ELEMENT c EMPTY><!ATTLIST c foo ID #IMPLIED bar CDATA #IMPLIED>]>";
  203. string xml = dtd + "<root><c foo='id1' bar='1' /><c foo='id2' bar='2'/></root>";
  204. XmlValidatingReader vr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  205. doc.Load (vr);
  206. AssertNotNull (doc.GetElementById ("id1"));
  207. AssertNotNull (doc.GetElementById ("id2"));
  208. // MS.NET BUG: Later I try to append it to another element, but
  209. // it should raise InvalidOperationException.
  210. // (and if MS.NET conform to DOM 1.0, it should be XmlException.)
  211. // XmlAttribute attr = doc.DocumentElement.FirstChild.Attributes [0];
  212. XmlAttribute attr = doc.DocumentElement.FirstChild.Attributes.RemoveAt (0);
  213. AssertEquals ("id1", attr.Value);
  214. doc.DocumentElement.LastChild.Attributes.SetNamedItem (attr);
  215. AssertNotNull (doc.GetElementById ("id1"));
  216. XmlElement elem2 = doc.GetElementById ("id2");
  217. // MS.NET BUG: it doesn't remove replaced attribute with SetNamedItem!
  218. // AssertNull (elem2);
  219. // AssertEquals ("2", elem2.GetAttribute ("bar"));
  220. // elem2.RemoveAttribute ("foo");
  221. // AssertEquals ("", elem2.GetAttribute ("foo"));
  222. // MS.NET BUG: elem should be the element which has the attribute bar='1'!
  223. XmlElement elem = doc.GetElementById ("id1");
  224. // AssertEquals ("2", elem.GetAttribute ("bar"));
  225. // Here, required attribute foo is no more required,
  226. XmlElement elemNew = doc.CreateElement ("c");
  227. doc.DocumentElement.AppendChild (elemNew);
  228. // but once attribute is set, document recognizes this ID.
  229. elemNew.SetAttribute ("foo", "id3");
  230. AssertNotNull (doc.GetElementById ("id3"));
  231. elemNew.RemoveAttribute ("foo");
  232. AssertNull (doc.GetElementById ("id3"));
  233. // MS.NET BUG: multiple IDs are allowed.
  234. // In such case GetElementById fails.
  235. elemNew.SetAttribute ("foo", "id2");
  236. // While XmlValidatingReader validated ID cannot be removed.
  237. // It is too curious for me.
  238. elem.RemoveAttribute ("foo");
  239. // Finally...
  240. doc.RemoveAll ();
  241. AssertNull (doc.GetElementById ("id1"));
  242. AssertNull (doc.GetElementById ("id2"));
  243. AssertNull (doc.GetElementById ("id3"));
  244. }
  245. int removeAllStep;
  246. [Test]
  247. public void DefaultAttributeRemoval ()
  248. {
  249. XmlDocument doc = new XmlDocument ();
  250. doc.LoadXml ("<!DOCTYPE root [<!ELEMENT root (#PCDATA)><!ATTLIST root foo CDATA 'foo-def'>]><root></root>");
  251. doc.NodeInserted += new XmlNodeChangedEventHandler (OnInsert);
  252. doc.NodeChanged += new XmlNodeChangedEventHandler (OnChange);
  253. doc.NodeRemoved += new XmlNodeChangedEventHandler (OnRemove);
  254. doc.DocumentElement.RemoveAll ();
  255. }
  256. private void OnInsert (object o, XmlNodeChangedEventArgs e)
  257. {
  258. if (removeAllStep == 1)
  259. AssertEquals (XmlNodeType.Text, e.Node.NodeType);
  260. else if (removeAllStep == 2) {
  261. AssertEquals ("foo", e.Node.Name);
  262. Assert (! ((XmlAttribute) e.Node).Specified);
  263. }
  264. else
  265. Fail ();
  266. removeAllStep++;
  267. }
  268. private void OnChange (object o, XmlNodeChangedEventArgs e)
  269. {
  270. Fail ("Should not be called.");
  271. }
  272. private void OnRemove (object o, XmlNodeChangedEventArgs e)
  273. {
  274. AssertEquals (0, removeAllStep);
  275. AssertEquals ("foo", e.Node.Name);
  276. removeAllStep++;
  277. }
  278. [Test]
  279. public void EmptyStringHasTextNode ()
  280. {
  281. doc.LoadXml ("<root attr=''/>");
  282. XmlAttribute attr = doc.DocumentElement.GetAttributeNode ("attr");
  283. AssertNotNull (attr);
  284. AssertEquals (1, attr.ChildNodes.Count);
  285. AssertEquals (XmlNodeType.Text, attr.ChildNodes [0].NodeType);
  286. AssertEquals (String.Empty, attr.ChildNodes [0].Value);
  287. }
  288. [Test]
  289. public void CrazyPrefix ()
  290. {
  291. XmlDocument doc = new XmlDocument ();
  292. doc.AppendChild (doc.CreateElement ("foo"));
  293. doc.DocumentElement.SetAttribute ("a", "urn:a", "attr");
  294. XmlAttribute a = doc.DocumentElement.Attributes [0];
  295. a.Prefix ="hoge:hoge:hoge";
  296. // This test is nothing more than ****.
  297. AssertEquals ("hoge:hoge:hoge", a.Prefix);
  298. // The resulting string is not XML (so broken), so
  299. // it should not be tested.
  300. // doc.Save (TextWriter.Null);
  301. }
  302. [Test]
  303. public void SetValueAndEntityRefChild ()
  304. {
  305. string dtd = @"<!DOCTYPE root [
  306. <!ELEMENT root EMPTY>
  307. <!ATTLIST root foo CDATA #IMPLIED>
  308. <!ENTITY ent 'entityyyy'>
  309. ]>";
  310. string xml = dtd + "<root foo='&ent;' />";
  311. XmlDocument doc = new XmlDocument ();
  312. doc.LoadXml (xml);
  313. doc.DocumentElement.Attributes [0].Value = "replaced";
  314. }
  315. [Test] // bug #76311
  316. public void UpdateIDAttrValueAfterAppend ()
  317. {
  318. XmlDocument doc = new XmlDocument ();
  319. doc.LoadXml ("<!DOCTYPE USS[<!ELEMENT USS EMPTY><!ATTLIST USS Id ID #REQUIRED>]><USS Id='foo'/>");
  320. AssertNotNull ("#1", doc.SelectSingleNode ("id ('foo')"));
  321. doc.DocumentElement.Attributes [0].Value = "bar";
  322. AssertNull ("#2", doc.SelectSingleNode ("id ('foo')"));
  323. AssertNotNull ("#3", doc.SelectSingleNode ("id ('bar')"));
  324. doc.DocumentElement.Attributes [0].ChildNodes [0].Value = "baz";
  325. // Tests below don't work fine under MS.NET
  326. // AssertNull ("#4", doc.SelectSingleNode ("id ('bar')"));
  327. // AssertNotNull ("#5", doc.SelectSingleNode ("id ('baz')"));
  328. doc.DocumentElement.Attributes [0].AppendChild (doc.CreateTextNode ("baz"));
  329. AssertNull ("#6", doc.SelectSingleNode ("id ('baz')"));
  330. // AssertNull ("#6-2", doc.SelectSingleNode ("id ('bar')"));
  331. // AssertNotNull ("#7", doc.SelectSingleNode ("id ('bazbaz')"));
  332. }
  333. [Test] // http://lists.ximian.com/pipermail/mono-list/2006-May/031557.html
  334. public void NonEmptyPrefixWithEmptyNS ()
  335. {
  336. XmlDocument xmlDoc = new XmlDocument ();
  337. xmlDoc.AppendChild (xmlDoc.CreateNode (XmlNodeType.XmlDeclaration, "", ""));
  338. XmlElement docElement = xmlDoc.CreateElement ("doc");
  339. docElement.SetAttribute ("xmlns", "http://whatever.org/XMLSchema/foo");
  340. docElement.SetAttribute ("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
  341. docElement.SetAttribute ("xsi:schemaLocation", "http://whatever.org/XMLSchema/foo.xsd");
  342. xmlDoc.AppendChild (docElement);
  343. XmlElement fooElement = xmlDoc.CreateElement ("foo");
  344. docElement.AppendChild (fooElement);
  345. xmlDoc.Save (TextWriter.Null);
  346. }
  347. [Test]
  348. public void NullPrefix ()
  349. {
  350. new MyXmlAttribute ("foo", "urn:foo", new XmlDocument ());
  351. }
  352. class MyXmlAttribute : XmlAttribute
  353. {
  354. public MyXmlAttribute (string localName, string ns, XmlDocument doc)
  355. : base (null, localName, ns, doc)
  356. {
  357. }
  358. }
  359. }
  360. }