XmlNamespaceManagerTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // XmlNamespaceManagerTests.cs
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. //
  7. // (C) 2002 Jason Diamond http://injektilo.org/
  8. //
  9. using System;
  10. using System.Xml;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Xml
  13. {
  14. public class XmlNamespaceManagerTests : TestCase
  15. {
  16. public XmlNamespaceManagerTests () : base ("MonoTests.System.Xml.XmlNameSpaceManagerTests testsuite") { }
  17. public XmlNamespaceManagerTests (string name) : base (name) { }
  18. private XmlNameTable nameTable;
  19. private XmlNamespaceManager namespaceManager;
  20. protected override void SetUp ()
  21. {
  22. nameTable = new NameTable ();
  23. namespaceManager = new XmlNamespaceManager (nameTable);
  24. }
  25. public void TestNewNamespaceManager ()
  26. {
  27. // make sure that you can call PopScope when there aren't any to pop.
  28. Assert (!namespaceManager.PopScope ());
  29. // the following strings should have been added to the name table by the
  30. // namespace manager.
  31. string xmlnsPrefix = nameTable.Get ("xmlns");
  32. string xmlPrefix = nameTable.Get ("xml");
  33. string stringEmpty = nameTable.Get (String.Empty);
  34. string xmlnsNamespace = "http://www.w3.org/2000/xmlns/";
  35. string xmlNamespace = "http://www.w3.org/XML/1998/namespace";
  36. // none of them should be null.
  37. AssertNotNull (xmlnsPrefix);
  38. AssertNotNull (xmlPrefix);
  39. AssertNotNull (stringEmpty);
  40. AssertNotNull (xmlnsNamespace);
  41. AssertNotNull (xmlNamespace);
  42. // Microsoft's XmlNamespaceManager reports that these three
  43. // namespaces aren't declared for some reason.
  44. Assert (!namespaceManager.HasNamespace ("xmlns"));
  45. Assert (!namespaceManager.HasNamespace ("xml"));
  46. Assert (!namespaceManager.HasNamespace (String.Empty));
  47. // these three namespaces are declared by default.
  48. AssertEquals ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
  49. AssertEquals ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
  50. AssertEquals (String.Empty, namespaceManager.LookupNamespace (String.Empty));
  51. // the namespaces should be the same references found in the name table.
  52. AssertSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
  53. AssertSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
  54. AssertSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));
  55. // looking up undeclared namespaces should return null.
  56. AssertNull (namespaceManager.LookupNamespace ("foo"));
  57. }
  58. public void TestAddNamespace ()
  59. {
  60. // add a new namespace.
  61. namespaceManager.AddNamespace ("foo", "http://foo/");
  62. // make sure the new namespace is there.
  63. Assert (namespaceManager.HasNamespace ("foo"));
  64. AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  65. }
  66. public void TestAddNamespaceWithNameTable ()
  67. {
  68. // add a known reference to the name table.
  69. string fooNamespace = "http://foo/";
  70. nameTable.Add(fooNamespace);
  71. // create a new string with the same value but different address.
  72. string fooNamespace2 = "http://";
  73. fooNamespace2 += "foo/";
  74. // the references must be different in order for this test to prove anything.
  75. Assert (!Object.ReferenceEquals (fooNamespace, fooNamespace2));
  76. // add the namespace with the reference that's not in the name table.
  77. namespaceManager.AddNamespace ("foo", fooNamespace2);
  78. // the returned reference should be the same one that's in the name table.
  79. AssertSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
  80. }
  81. public void TestPushScope ()
  82. {
  83. // add a new namespace.
  84. namespaceManager.AddNamespace ("foo", "http://foo/");
  85. // make sure the new namespace is there.
  86. Assert (namespaceManager.HasNamespace ("foo"));
  87. AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  88. // push a new scope.
  89. namespaceManager.PushScope ();
  90. // add a new namespace.
  91. namespaceManager.AddNamespace ("bar", "http://bar/");
  92. // make sure the old namespace is not in this new scope.
  93. Assert (!namespaceManager.HasNamespace ("foo"));
  94. // but we're still supposed to be able to lookup the old namespace.
  95. AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  96. // make sure the new namespace is there.
  97. Assert (namespaceManager.HasNamespace ("bar"));
  98. AssertEquals ("http://bar/", namespaceManager.LookupNamespace ("bar"));
  99. }
  100. public void TestPopScope ()
  101. {
  102. // add some namespaces and a scope.
  103. TestPushScope ();
  104. // pop the scope.
  105. Assert (namespaceManager.PopScope ());
  106. // make sure the first namespace is still there.
  107. Assert (namespaceManager.HasNamespace ("foo"));
  108. AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
  109. // make sure the second namespace is no longer there.
  110. Assert (!namespaceManager.HasNamespace ("bar"));
  111. AssertNull (namespaceManager.LookupNamespace ("bar"));
  112. // make sure there are no more scopes to pop.
  113. Assert (!namespaceManager.PopScope ());
  114. // make sure that popping again doesn't cause an exception.
  115. Assert (!namespaceManager.PopScope ());
  116. }
  117. }
  118. }