XmlNamespaceManagerTests.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 PushScope ()
  92. {
  93. // add a new namespace.
  94. namespaceManager.AddNamespace ("foo", "http://foo/");
  95. // make sure the new namespace is there.
  96. Assert (namespaceManager.HasNamespace ("foo"));
  97. AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  98. // push a new scope.
  99. namespaceManager.PushScope ();
  100. // add a new namespace.
  101. namespaceManager.AddNamespace ("bar", "http://bar/");
  102. // make sure the old namespace is not in this new scope.
  103. Assert (!namespaceManager.HasNamespace ("foo"));
  104. // but we're still supposed to be able to lookup the old namespace.
  105. AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  106. // make sure the new namespace is there.
  107. Assert (namespaceManager.HasNamespace ("bar"));
  108. AssertEquals ("http://bar/", namespaceManager.LookupNamespace ("bar"));
  109. }
  110. [Test]
  111. public void PopScope ()
  112. {
  113. // add some namespaces and a scope.
  114. PushScope ();
  115. // pop the scope.
  116. Assert (namespaceManager.PopScope ());
  117. // make sure the first namespace is still there.
  118. Assert (namespaceManager.HasNamespace ("foo"));
  119. AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  120. // make sure the second namespace is no longer there.
  121. Assert (!namespaceManager.HasNamespace ("bar"));
  122. AssertNull (namespaceManager.LookupNamespace ("bar"));
  123. // make sure there are no more scopes to pop.
  124. Assert (!namespaceManager.PopScope ());
  125. // make sure that popping again doesn't cause an exception.
  126. Assert (!namespaceManager.PopScope ());
  127. }
  128. [Test]
  129. public void PopScopeMustKeepAddedInScope ()
  130. {
  131. namespaceManager = new XmlNamespaceManager (new NameTable ()); // clear
  132. namespaceManager .AddNamespace ("foo", "urn:foo"); // 0
  133. namespaceManager .AddNamespace ("bar", "urn:bar"); // 0
  134. namespaceManager .PushScope (); // 1
  135. namespaceManager .PushScope (); // 2
  136. namespaceManager .PopScope (); // 2
  137. namespaceManager .PopScope (); // 1
  138. namespaceManager .PopScope (); // 0
  139. AssertEquals ("urn:foo", namespaceManager.LookupNamespace ("foo"));
  140. AssertEquals ("urn:bar", namespaceManager.LookupNamespace ("bar"));
  141. }
  142. [Test]
  143. public void AddPushPopRemove ()
  144. {
  145. XmlNamespaceManager nsmgr =
  146. new XmlNamespaceManager (new NameTable ());
  147. string ns = nsmgr.NameTable.Add ("urn:foo");
  148. nsmgr.AddNamespace ("foo", ns);
  149. AssertEquals ("foo", nsmgr.LookupPrefix (ns));
  150. nsmgr.PushScope ();
  151. AssertEquals ("foo", nsmgr.LookupPrefix (ns));
  152. nsmgr.PopScope ();
  153. AssertEquals ("foo", nsmgr.LookupPrefix (ns));
  154. nsmgr.RemoveNamespace ("foo", ns);
  155. AssertNull (nsmgr.LookupPrefix (ns));
  156. }
  157. [Test]
  158. public void LookupPrefix ()
  159. {
  160. // This test should use an empty nametable.
  161. XmlNamespaceManager nsmgr =
  162. new XmlNamespaceManager (new NameTable ());
  163. nsmgr.NameTable.Add ("urn:hoge");
  164. nsmgr.NameTable.Add ("urn:fuga");
  165. nsmgr.AddNamespace (string.Empty, "urn:hoge");
  166. AssertNull (nsmgr.LookupPrefix ("urn:fuga"));
  167. AssertEquals (String.Empty, nsmgr.LookupPrefix ("urn:hoge"));
  168. }
  169. string suffix = "oo";
  170. [Test]
  171. public void AtomizedLookup ()
  172. {
  173. if (DateTime.Now.Year == 0)
  174. suffix = String.Empty;
  175. XmlNamespaceManager nsmgr =
  176. new XmlNamespaceManager (new NameTable ());
  177. nsmgr.AddNamespace ("foo", "urn:foo");
  178. AssertNotNull (nsmgr.LookupPrefix ("urn:foo"));
  179. // FIXME: This returns registered URI inconsistently.
  180. // AssertNull ("It is not atomized and thus should be failed", nsmgr.LookupPrefix ("urn:f" + suffix));
  181. #if NET_2_0
  182. AssertNotNull ("Atomization should not matter.", nsmgr.LookupPrefix ("urn:f" + suffix, false));
  183. #endif
  184. }
  185. #if NET_2_0
  186. XmlNamespaceScope l = XmlNamespaceScope.Local;
  187. XmlNamespaceScope x = XmlNamespaceScope.ExcludeXml;
  188. XmlNamespaceScope a = XmlNamespaceScope.All;
  189. [Test]
  190. public void GetNamespacesInScope ()
  191. {
  192. XmlNamespaceManager nsmgr =
  193. new XmlNamespaceManager (new NameTable ());
  194. AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
  195. AssertEquals (0, nsmgr.GetNamespacesInScope (x).Count);
  196. AssertEquals (1, nsmgr.GetNamespacesInScope (a).Count);
  197. nsmgr.AddNamespace ("foo", "urn:foo");
  198. AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
  199. AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
  200. AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
  201. nsmgr.RemoveNamespace ("foo", "urn:foo", false);
  202. AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
  203. AssertEquals (0, nsmgr.GetNamespacesInScope (x).Count);
  204. AssertEquals (1, nsmgr.GetNamespacesInScope (a).Count);
  205. // default namespace
  206. nsmgr.AddNamespace ("", "urn:empty");
  207. AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
  208. AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
  209. AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
  210. nsmgr.RemoveNamespace ("", "urn:empty", false);
  211. AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
  212. AssertEquals (0, nsmgr.GetNamespacesInScope (x).Count);
  213. AssertEquals (1, nsmgr.GetNamespacesInScope (a).Count);
  214. // PushScope
  215. nsmgr.AddNamespace ("foo", "urn:foo");
  216. nsmgr.PushScope ();
  217. AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
  218. AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
  219. AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
  220. // PopScope
  221. nsmgr.PopScope ();
  222. AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
  223. AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
  224. AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
  225. nsmgr.AddNamespace ("", "");
  226. AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
  227. AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
  228. AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
  229. }
  230. #endif
  231. }
  232. }