XmlNamespaceManagerTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. Assertion.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. Assertion.AssertNotNull (xmlnsPrefix);
  41. Assertion.AssertNotNull (xmlPrefix);
  42. Assertion.AssertNotNull (stringEmpty);
  43. Assertion.AssertNotNull (xmlnsNamespace);
  44. Assertion.AssertNotNull (xmlNamespace);
  45. // Microsoft's XmlNamespaceManager reports that these three
  46. // namespaces aren't declared for some reason.
  47. Assertion.Assert (!namespaceManager.HasNamespace ("xmlns"));
  48. Assertion.Assert (!namespaceManager.HasNamespace ("xml"));
  49. Assertion.Assert (!namespaceManager.HasNamespace (String.Empty));
  50. // these three namespaces are declared by default.
  51. Assertion.AssertEquals ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
  52. Assertion.AssertEquals ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
  53. Assertion.AssertEquals (String.Empty, namespaceManager.LookupNamespace (String.Empty));
  54. // the namespaces should be the same references found in the name table.
  55. Assertion.AssertSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
  56. Assertion.AssertSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
  57. Assertion.AssertSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));
  58. // looking up undeclared namespaces should return null.
  59. Assertion.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. Assertion.Assert (namespaceManager.HasNamespace ("foo"));
  68. Assertion.AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  69. }
  70. [Test]
  71. public void AddNamespaceWithNameTable ()
  72. {
  73. // add a known reference to the name table.
  74. string fooNamespace = "http://foo/";
  75. nameTable.Add(fooNamespace);
  76. // create a new string with the same value but different address.
  77. string fooNamespace2 = "http://";
  78. fooNamespace2 += "foo/";
  79. // the references must be different in order for this test to prove anything.
  80. Assertion.Assert (!Object.ReferenceEquals (fooNamespace, fooNamespace2));
  81. // add the namespace with the reference that's not in the name table.
  82. namespaceManager.AddNamespace ("foo", fooNamespace2);
  83. // the returned reference should be the same one that's in the name table.
  84. Assertion.AssertSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
  85. }
  86. [Test]
  87. public void PushScope ()
  88. {
  89. // add a new namespace.
  90. namespaceManager.AddNamespace ("foo", "http://foo/");
  91. // make sure the new namespace is there.
  92. Assertion.Assert (namespaceManager.HasNamespace ("foo"));
  93. Assertion.AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  94. // push a new scope.
  95. namespaceManager.PushScope ();
  96. // add a new namespace.
  97. namespaceManager.AddNamespace ("bar", "http://bar/");
  98. // make sure the old namespace is not in this new scope.
  99. Assertion.Assert (!namespaceManager.HasNamespace ("foo"));
  100. // but we're still supposed to be able to lookup the old namespace.
  101. Assertion.AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  102. // make sure the new namespace is there.
  103. Assertion.Assert (namespaceManager.HasNamespace ("bar"));
  104. Assertion.AssertEquals ("http://bar/", namespaceManager.LookupNamespace ("bar"));
  105. }
  106. [Test]
  107. public void PopScope ()
  108. {
  109. // add some namespaces and a scope.
  110. PushScope ();
  111. // pop the scope.
  112. Assertion.Assert (namespaceManager.PopScope ());
  113. // make sure the first namespace is still there.
  114. Assertion.Assert (namespaceManager.HasNamespace ("foo"));
  115. Assertion.AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  116. // make sure the second namespace is no longer there.
  117. Assertion.Assert (!namespaceManager.HasNamespace ("bar"));
  118. Assertion.AssertNull (namespaceManager.LookupNamespace ("bar"));
  119. // make sure there are no more scopes to pop.
  120. Assertion.Assert (!namespaceManager.PopScope ());
  121. // make sure that popping again doesn't cause an exception.
  122. Assertion.Assert (!namespaceManager.PopScope ());
  123. }
  124. [Test]
  125. public void LookupPrefix ()
  126. {
  127. // This test should use an empty nametable.
  128. XmlNamespaceManager nsmgr =
  129. new XmlNamespaceManager (new NameTable ());
  130. nsmgr.NameTable.Add ("urn:hoge");
  131. nsmgr.NameTable.Add ("urn:fuga");
  132. nsmgr.AddNamespace (string.Empty, "urn:hoge");
  133. Assertion.AssertNull (nsmgr.LookupPrefix ("urn:fuga"));
  134. Assertion.AssertEquals (String.Empty, nsmgr.LookupPrefix ("urn:hoge"));
  135. }
  136. }
  137. }