XmlAttributeTests.cs 14 KB

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