2
0

XmlNamespaceManagerTests.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 : Assertion
  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 (!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. AssertNotNull (xmlnsPrefix);
  41. AssertNotNull (xmlPrefix);
  42. AssertNotNull (stringEmpty);
  43. AssertNotNull (xmlnsNamespace);
  44. AssertNotNull (xmlNamespace);
  45. // Microsoft's XmlNamespaceManager reports that these three
  46. // namespaces aren't declared for some reason.
  47. Assert (!namespaceManager.HasNamespace ("xmlns"));
  48. Assert (!namespaceManager.HasNamespace ("xml"));
  49. Assert (!namespaceManager.HasNamespace (String.Empty));
  50. // these three namespaces are declared by default.
  51. AssertEquals ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
  52. AssertEquals ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
  53. AssertEquals (String.Empty, namespaceManager.LookupNamespace (String.Empty));
  54. // the namespaces should be the same references found in the name table.
  55. AssertSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
  56. AssertSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
  57. AssertSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));
  58. // looking up undeclared namespaces should return null.
  59. AssertNull (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 (namespaceManager.HasNamespace ("foo"));
  68. AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  69. // adding a different namespace with the same prefix
  70. // is allowed.
  71. namespaceManager.AddNamespace ("foo", "http://foo1/");
  72. AssertEquals ("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 (!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. AssertSame (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 (namespaceManager.HasNamespace ("foo"));
  110. AssertEquals ("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 (!namespaceManager.HasNamespace ("foo"));
  117. // but we're still supposed to be able to lookup the old namespace.
  118. AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  119. // make sure the new namespace is there.
  120. Assert (namespaceManager.HasNamespace ("bar"));
  121. AssertEquals ("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 (namespaceManager.PopScope ());
  130. // make sure the first namespace is still there.
  131. Assert (namespaceManager.HasNamespace ("foo"));
  132. AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  133. // make sure the second namespace is no longer there.
  134. Assert (!namespaceManager.HasNamespace ("bar"));
  135. AssertNull (namespaceManager.LookupNamespace ("bar"));
  136. // make sure there are no more scopes to pop.
  137. Assert (!namespaceManager.PopScope ());
  138. // make sure that popping again doesn't cause an exception.
  139. Assert (!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. AssertEquals ("urn:foo", namespaceManager.LookupNamespace ("foo"));
  153. AssertEquals ("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. AssertEquals ("foo", nsmgr.LookupPrefix (ns));
  163. nsmgr.PushScope ();
  164. AssertEquals ("foo", nsmgr.LookupPrefix (ns));
  165. nsmgr.PopScope ();
  166. AssertEquals ("foo", nsmgr.LookupPrefix (ns));
  167. nsmgr.RemoveNamespace ("foo", ns);
  168. AssertNull (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. AssertNull (nsmgr.LookupPrefix ("urn:fuga"));
  180. AssertEquals (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. AssertNotNull (nsmgr.LookupPrefix ("urn:foo"));
  192. // FIXME: This returns registered URI inconsistently.
  193. // AssertNull ("It is not atomized and thus should be failed", nsmgr.LookupPrefix ("urn:f" + suffix));
  194. }
  195. #if NET_2_0
  196. XmlNamespaceScope l = XmlNamespaceScope.Local;
  197. XmlNamespaceScope x = XmlNamespaceScope.ExcludeXml;
  198. XmlNamespaceScope a = XmlNamespaceScope.All;
  199. [Test]
  200. [Category ("NotDotNet")] // MS bug
  201. public void GetNamespacesInScope ()
  202. {
  203. XmlNamespaceManager nsmgr =
  204. new XmlNamespaceManager (new NameTable ());
  205. AssertEquals ("#1", 0, nsmgr.GetNamespacesInScope (l).Count);
  206. AssertEquals ("#2", 0, nsmgr.GetNamespacesInScope (x).Count);
  207. AssertEquals ("#3", 1, nsmgr.GetNamespacesInScope (a).Count);
  208. nsmgr.AddNamespace ("foo", "urn:foo");
  209. AssertEquals ("#4", 1, nsmgr.GetNamespacesInScope (l).Count);
  210. AssertEquals ("#5", 1, nsmgr.GetNamespacesInScope (x).Count);
  211. AssertEquals ("#6", 2, nsmgr.GetNamespacesInScope (a).Count);
  212. // default namespace
  213. nsmgr.AddNamespace ("", "urn:empty");
  214. AssertEquals ("#7", 2, nsmgr.GetNamespacesInScope (l).Count);
  215. AssertEquals ("#8", 2, nsmgr.GetNamespacesInScope (x).Count);
  216. AssertEquals ("#9", 3, nsmgr.GetNamespacesInScope (a).Count);
  217. // PushScope
  218. nsmgr.AddNamespace ("foo", "urn:foo");
  219. nsmgr.PushScope ();
  220. AssertEquals ("#10", 0, nsmgr.GetNamespacesInScope (l).Count);
  221. AssertEquals ("#11", 2, nsmgr.GetNamespacesInScope (x).Count);
  222. AssertEquals ("#12", 3, nsmgr.GetNamespacesInScope (a).Count);
  223. // PopScope
  224. nsmgr.PopScope ();
  225. AssertEquals ("#13", 2, nsmgr.GetNamespacesInScope (l).Count);
  226. AssertEquals ("#14", 2, nsmgr.GetNamespacesInScope (x).Count);
  227. AssertEquals ("#15", 3, nsmgr.GetNamespacesInScope (a).Count);
  228. nsmgr.AddNamespace ("", "");
  229. // MS bug - it should return 1 for .Local but it returns 2 instead.
  230. AssertEquals ("#16", 1, nsmgr.GetNamespacesInScope (l).Count);
  231. AssertEquals ("#17", 1, nsmgr.GetNamespacesInScope (x).Count);
  232. AssertEquals ("#18", 2, nsmgr.GetNamespacesInScope (a).Count);
  233. }
  234. #endif
  235. }
  236. }