2 // XmlNamespaceManagerTests.cs
5 // Jason Diamond (jason@injektilo.org)
7 // (C) 2002 Jason Diamond http://injektilo.org/
13 using NUnit.Framework;
15 namespace MonoTests.System.Xml
17 public class XmlNamespaceManagerTests : TestCase
19 public XmlNamespaceManagerTests () : base ("MonoTests.System.Xml.XmlNameSpaceManagerTests testsuite") { }
20 public XmlNamespaceManagerTests (string name) : base (name) { }
22 private XmlNameTable nameTable;
23 private XmlNamespaceManager namespaceManager;
25 protected override void SetUp ()
27 nameTable = new NameTable ();
28 namespaceManager = new XmlNamespaceManager (nameTable);
31 public void TestNewNamespaceManager ()
33 // make sure that you can call PopScope when there aren't any to pop.
34 Assert (!namespaceManager.PopScope ());
36 // the following strings should have been added to the name table by the
38 string xmlnsPrefix = nameTable.Get ("xmlns");
39 string xmlPrefix = nameTable.Get ("xml");
40 string stringEmpty = nameTable.Get (String.Empty);
41 string xmlnsNamespace = "http://www.w3.org/2000/xmlns/";
42 string xmlNamespace = "http://www.w3.org/XML/1998/namespace";
44 // none of them should be null.
45 AssertNotNull (xmlnsPrefix);
46 AssertNotNull (xmlPrefix);
47 AssertNotNull (stringEmpty);
48 AssertNotNull (xmlnsNamespace);
49 AssertNotNull (xmlNamespace);
51 // Microsoft's XmlNamespaceManager reports that these three
52 // namespaces aren't declared for some reason.
53 Assert (!namespaceManager.HasNamespace ("xmlns"));
54 Assert (!namespaceManager.HasNamespace ("xml"));
55 Assert (!namespaceManager.HasNamespace (String.Empty));
57 // these three namespaces are declared by default.
58 AssertEquals ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
59 AssertEquals ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
60 AssertEquals (String.Empty, namespaceManager.LookupNamespace (String.Empty));
62 // the namespaces should be the same references found in the name table.
63 AssertSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
64 AssertSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
65 AssertSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));
67 // looking up undeclared namespaces should return null.
68 AssertNull (namespaceManager.LookupNamespace ("foo"));
71 public void TestAddNamespace ()
73 // add a new namespace.
74 namespaceManager.AddNamespace ("foo", "http://foo/");
75 // make sure the new namespace is there.
76 Assert (namespaceManager.HasNamespace ("foo"));
77 AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
80 public void TestAddNamespaceWithNameTable ()
82 // add a known reference to the name table.
83 string fooNamespace = "http://foo/";
84 nameTable.Add(fooNamespace);
86 // create a new string with the same value but different address.
87 string fooNamespace2 = "http://";
88 fooNamespace2 += "foo/";
90 // the references must be different in order for this test to prove anything.
91 Assert (!Object.ReferenceEquals (fooNamespace, fooNamespace2));
93 // add the namespace with the reference that's not in the name table.
94 namespaceManager.AddNamespace ("foo", fooNamespace2);
96 // the returned reference should be the same one that's in the name table.
97 AssertSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
100 public void TestPushScope ()
102 // add a new namespace.
103 namespaceManager.AddNamespace ("foo", "http://foo/");
104 // make sure the new namespace is there.
105 Assert (namespaceManager.HasNamespace ("foo"));
106 AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
108 namespaceManager.PushScope ();
109 // add a new namespace.
110 namespaceManager.AddNamespace ("bar", "http://bar/");
111 // make sure the old namespace is not in this new scope.
112 Assert (!namespaceManager.HasNamespace ("foo"));
113 // but we're still supposed to be able to lookup the old namespace.
114 AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
115 // make sure the new namespace is there.
116 Assert (namespaceManager.HasNamespace ("bar"));
117 AssertEquals ("http://bar/", namespaceManager.LookupNamespace ("bar"));
120 public void TestPopScope ()
122 // add some namespaces and a scope.
125 Assert (namespaceManager.PopScope ());
126 // make sure the first namespace is still there.
127 Assert (namespaceManager.HasNamespace ("foo"));
128 AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
129 // make sure the second namespace is no longer there.
130 Assert (!namespaceManager.HasNamespace ("bar"));
131 AssertNull (namespaceManager.LookupNamespace ("bar"));
132 // make sure there are no more scopes to pop.
133 Assert (!namespaceManager.PopScope ());
134 // make sure that popping again doesn't cause an exception.
135 Assert (!namespaceManager.PopScope ());
138 public void TestLookupPrefix ()
140 // This test should use an empty nametable.
141 XmlNamespaceManager nsmgr =
142 new XmlNamespaceManager (new NameTable ());
143 nsmgr.NameTable.Add ("urn:hoge");
144 nsmgr.NameTable.Add ("urn:fuga");
145 nsmgr.AddNamespace (string.Empty, "urn:hoge");
146 AssertNull (nsmgr.LookupPrefix ("urn:fuga"));
147 AssertEquals (String.Empty, nsmgr.LookupPrefix ("urn:hoge"));