XmlNamespaceManagerTests.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // XmlNamespaceManagerTests.cs
  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 NUnit.Framework;
  14. namespace MonoTests.System.Xml
  15. {
  16. [TestFixture]
  17. public class XmlNamespaceManagerTests
  18. {
  19. private XmlNameTable nameTable;
  20. private XmlNamespaceManager namespaceManager;
  21. [SetUp]
  22. public void GetReady ()
  23. {
  24. nameTable = new NameTable ();
  25. namespaceManager = new XmlNamespaceManager (nameTable);
  26. }
  27. [Test]
  28. public void NewNamespaceManager ()
  29. {
  30. // make sure that you can call PopScope when there aren't any to pop.
  31. Assert.IsTrue (!namespaceManager.PopScope ());
  32. // the following strings should have been added to the name table by the
  33. // namespace manager.
  34. string xmlnsPrefix = nameTable.Get ("xmlns");
  35. string xmlPrefix = nameTable.Get ("xml");
  36. string stringEmpty = nameTable.Get (String.Empty);
  37. string xmlnsNamespace = "http://www.w3.org/2000/xmlns/";
  38. string xmlNamespace = "http://www.w3.org/XML/1998/namespace";
  39. // none of them should be null.
  40. Assert.IsNotNull (xmlnsPrefix);
  41. Assert.IsNotNull (xmlPrefix);
  42. Assert.IsNotNull (stringEmpty);
  43. Assert.IsNotNull (xmlnsNamespace);
  44. Assert.IsNotNull (xmlNamespace);
  45. // Microsoft's XmlNamespaceManager reports that these three
  46. // namespaces aren't declared for some reason.
  47. Assert.IsTrue (!namespaceManager.HasNamespace ("xmlns"));
  48. Assert.IsTrue (!namespaceManager.HasNamespace ("xml"));
  49. Assert.IsTrue (!namespaceManager.HasNamespace (String.Empty));
  50. // these three namespaces are declared by default.
  51. Assert.AreEqual ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
  52. Assert.AreEqual ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
  53. Assert.AreEqual (String.Empty, namespaceManager.LookupNamespace (String.Empty));
  54. // the namespaces should be the same references found in the name table.
  55. Assert.AreSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
  56. Assert.AreSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
  57. Assert.AreSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));
  58. // looking up undeclared namespaces should return null.
  59. Assert.IsNull (namespaceManager.LookupNamespace ("foo"));
  60. }
  61. [Test]
  62. public void AddNamespace ()
  63. {
  64. // add a new namespace.
  65. namespaceManager.AddNamespace ("foo", "http://foo/");
  66. // make sure the new namespace is there.
  67. Assert.IsTrue (namespaceManager.HasNamespace ("foo"));
  68. Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  69. // adding a different namespace with the same prefix
  70. // is allowed.
  71. namespaceManager.AddNamespace ("foo", "http://foo1/");
  72. Assert.AreEqual ("http://foo1/", namespaceManager.LookupNamespace ("foo"));
  73. }
  74. [Test]
  75. public void AddNamespaceWithNameTable ()
  76. {
  77. // add a known reference to the name table.
  78. string fooNamespace = "http://foo/";
  79. nameTable.Add(fooNamespace);
  80. // create a new string with the same value but different address.
  81. string fooNamespace2 = "http://";
  82. fooNamespace2 += "foo/";
  83. // the references must be different in order for this test to prove anything.
  84. Assert.IsTrue (!Object.ReferenceEquals (fooNamespace, fooNamespace2));
  85. // add the namespace with the reference that's not in the name table.
  86. namespaceManager.AddNamespace ("foo", fooNamespace2);
  87. // the returned reference should be the same one that's in the name table.
  88. Assert.AreSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
  89. }
  90. [Test]
  91. public void AddNamespace_XmlPrefix ()
  92. {
  93. namespaceManager.AddNamespace ("xml", "http://www.w3.org/XML/1998/namespace");
  94. namespaceManager.AddNamespace ("XmL", "http://foo/");
  95. namespaceManager.AddNamespace ("xmlsomething", "http://foo/");
  96. }
  97. [Test]
  98. [ExpectedException (typeof (ArgumentException))]
  99. public void AddNamespace_XmlPrefix_Invalid ()
  100. {
  101. namespaceManager.AddNamespace ("xml", "http://foo/");
  102. }
  103. [Test]
  104. public void PushScope ()
  105. {
  106. // add a new namespace.
  107. namespaceManager.AddNamespace ("foo", "http://foo/");
  108. // make sure the new namespace is there.
  109. Assert.IsTrue (namespaceManager.HasNamespace ("foo"));
  110. Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  111. // push a new scope.
  112. namespaceManager.PushScope ();
  113. // add a new namespace.
  114. namespaceManager.AddNamespace ("bar", "http://bar/");
  115. // make sure the old namespace is not in this new scope.
  116. Assert.IsTrue (!namespaceManager.HasNamespace ("foo"));
  117. // but we're still supposed to be able to lookup the old namespace.
  118. Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  119. // make sure the new namespace is there.
  120. Assert.IsTrue (namespaceManager.HasNamespace ("bar"));
  121. Assert.AreEqual ("http://bar/", namespaceManager.LookupNamespace ("bar"));
  122. }
  123. [Test]
  124. public void PopScope ()
  125. {
  126. // add some namespaces and a scope.
  127. PushScope ();
  128. // pop the scope.
  129. Assert.IsTrue (namespaceManager.PopScope ());
  130. // make sure the first namespace is still there.
  131. Assert.IsTrue (namespaceManager.HasNamespace ("foo"));
  132. Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  133. // make sure the second namespace is no longer there.
  134. Assert.IsTrue (!namespaceManager.HasNamespace ("bar"));
  135. Assert.IsNull (namespaceManager.LookupNamespace ("bar"));
  136. // make sure there are no more scopes to pop.
  137. Assert.IsTrue (!namespaceManager.PopScope ());
  138. // make sure that popping again doesn't cause an exception.
  139. Assert.IsTrue (!namespaceManager.PopScope ());
  140. }
  141. [Test]
  142. public void PopScopeMustKeepAddedInScope ()
  143. {
  144. namespaceManager = new XmlNamespaceManager (new NameTable ()); // clear
  145. namespaceManager .AddNamespace ("foo", "urn:foo"); // 0
  146. namespaceManager .AddNamespace ("bar", "urn:bar"); // 0
  147. namespaceManager .PushScope (); // 1
  148. namespaceManager .PushScope (); // 2
  149. namespaceManager .PopScope (); // 2
  150. namespaceManager .PopScope (); // 1
  151. namespaceManager .PopScope (); // 0
  152. Assert.AreEqual ("urn:foo", namespaceManager.LookupNamespace ("foo"));
  153. Assert.AreEqual ("urn:bar", namespaceManager.LookupNamespace ("bar"));
  154. }
  155. [Test]
  156. public void AddPushPopRemove ()
  157. {
  158. XmlNamespaceManager nsmgr =
  159. new XmlNamespaceManager (new NameTable ());
  160. string ns = nsmgr.NameTable.Add ("urn:foo");
  161. nsmgr.AddNamespace ("foo", ns);
  162. Assert.AreEqual ("foo", nsmgr.LookupPrefix (ns));
  163. nsmgr.PushScope ();
  164. Assert.AreEqual ("foo", nsmgr.LookupPrefix (ns));
  165. nsmgr.PopScope ();
  166. Assert.AreEqual ("foo", nsmgr.LookupPrefix (ns));
  167. nsmgr.RemoveNamespace ("foo", ns);
  168. Assert.IsNull (nsmgr.LookupPrefix (ns));
  169. }
  170. [Test]
  171. public void LookupPrefix ()
  172. {
  173. // This test should use an empty nametable.
  174. XmlNamespaceManager nsmgr =
  175. new XmlNamespaceManager (new NameTable ());
  176. nsmgr.NameTable.Add ("urn:hoge");
  177. nsmgr.NameTable.Add ("urn:fuga");
  178. nsmgr.AddNamespace (string.Empty, "urn:hoge");
  179. Assert.IsNull (nsmgr.LookupPrefix ("urn:fuga"));
  180. Assert.AreEqual (String.Empty, nsmgr.LookupPrefix ("urn:hoge"));
  181. }
  182. string suffix = "oo";
  183. [Test]
  184. public void AtomizedLookup ()
  185. {
  186. if (DateTime.Now.Year == 0)
  187. suffix = String.Empty;
  188. XmlNamespaceManager nsmgr =
  189. new XmlNamespaceManager (new NameTable ());
  190. nsmgr.AddNamespace ("foo", "urn:foo");
  191. Assert.IsNotNull (nsmgr.LookupPrefix ("urn:foo"));
  192. // FIXME: This returns registered URI inconsistently.
  193. // Assert.IsNull (nsmgr.LookupPrefix ("urn:f" + suffix), "It is not atomized and thus should be failed");
  194. }
  195. [Test]
  196. public void TryToAddPrefixXml ()
  197. {
  198. NameTable nt = new NameTable ();
  199. XmlNamespaceManager nsmgr = new XmlNamespaceManager (nt);
  200. nsmgr.AddNamespace ("xml", "http://www.w3.org/XML/1998/namespace");
  201. }
  202. [Test]
  203. [ExpectedException (typeof (ArgumentException))]
  204. public void TryToAddPrefixXmlns ()
  205. {
  206. NameTable nt = new NameTable ();
  207. XmlNamespaceManager nsmgr = new XmlNamespaceManager (nt);
  208. nsmgr.AddNamespace ("xmlns", "http://www.w3.org/2000/xmlns/");
  209. }
  210. #if NET_2_0
  211. XmlNamespaceScope l = XmlNamespaceScope.Local;
  212. XmlNamespaceScope x = XmlNamespaceScope.ExcludeXml;
  213. XmlNamespaceScope a = XmlNamespaceScope.All;
  214. [Test]
  215. [Category ("NotDotNet")] // MS bug
  216. public void GetNamespacesInScope ()
  217. {
  218. XmlNamespaceManager nsmgr =
  219. new XmlNamespaceManager (new NameTable ());
  220. Assert.AreEqual (0, nsmgr.GetNamespacesInScope (l).Count, "#1");
  221. Assert.AreEqual (0, nsmgr.GetNamespacesInScope (x).Count, "#2");
  222. Assert.AreEqual (1, nsmgr.GetNamespacesInScope (a).Count, "#3");
  223. nsmgr.AddNamespace ("foo", "urn:foo");
  224. Assert.AreEqual (1, nsmgr.GetNamespacesInScope (l).Count, "#4");
  225. Assert.AreEqual (1, nsmgr.GetNamespacesInScope (x).Count, "#5");
  226. Assert.AreEqual (2, nsmgr.GetNamespacesInScope (a).Count, "#6");
  227. // default namespace
  228. nsmgr.AddNamespace ("", "urn:empty");
  229. Assert.AreEqual (2, nsmgr.GetNamespacesInScope (l).Count, "#7");
  230. Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#8");
  231. Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#9");
  232. // PushScope
  233. nsmgr.AddNamespace ("foo", "urn:foo");
  234. nsmgr.PushScope ();
  235. Assert.AreEqual (0, nsmgr.GetNamespacesInScope (l).Count, "#10");
  236. Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#11");
  237. Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#12");
  238. // PopScope
  239. nsmgr.PopScope ();
  240. Assert.AreEqual (2, nsmgr.GetNamespacesInScope (l).Count, "#13");
  241. Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#14");
  242. Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#15");
  243. nsmgr.AddNamespace ("", "");
  244. // MS bug - it should return 1 for .Local but it returns 2 instead.
  245. Assert.AreEqual (1, nsmgr.GetNamespacesInScope (l).Count, "#16");
  246. Assert.AreEqual (1, nsmgr.GetNamespacesInScope (x).Count, "#17");
  247. Assert.AreEqual (2, nsmgr.GetNamespacesInScope (a).Count, "#18");
  248. }
  249. #endif
  250. }
  251. }