2 // XmlNamespaceManagerTests.cs
5 // Jason Diamond (jason@injektilo.org)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
8 // (C) 2002 Jason Diamond http://injektilo.org/
9 // (C) 2003 Martin Willemoes Hansen
15 using NUnit.Framework;
17 namespace MonoTests.System.Xml
20 public class XmlNamespaceManagerTests
22 private XmlNameTable nameTable;
23 private XmlNamespaceManager namespaceManager;
26 public void GetReady ()
28 nameTable = new NameTable ();
29 namespaceManager = new XmlNamespaceManager (nameTable);
33 public void NewNamespaceManager ()
35 // make sure that you can call PopScope when there aren't any to pop.
36 Assertion.Assert (!namespaceManager.PopScope ());
38 // the following strings should have been added to the name table by the
40 string xmlnsPrefix = nameTable.Get ("xmlns");
41 string xmlPrefix = nameTable.Get ("xml");
42 string stringEmpty = nameTable.Get (String.Empty);
43 string xmlnsNamespace = "http://www.w3.org/2000/xmlns/";
44 string xmlNamespace = "http://www.w3.org/XML/1998/namespace";
46 // none of them should be null.
47 Assertion.AssertNotNull (xmlnsPrefix);
48 Assertion.AssertNotNull (xmlPrefix);
49 Assertion.AssertNotNull (stringEmpty);
50 Assertion.AssertNotNull (xmlnsNamespace);
51 Assertion.AssertNotNull (xmlNamespace);
53 // Microsoft's XmlNamespaceManager reports that these three
54 // namespaces aren't declared for some reason.
55 Assertion.Assert (!namespaceManager.HasNamespace ("xmlns"));
56 Assertion.Assert (!namespaceManager.HasNamespace ("xml"));
57 Assertion.Assert (!namespaceManager.HasNamespace (String.Empty));
59 // these three namespaces are declared by default.
60 Assertion.AssertEquals ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
61 Assertion.AssertEquals ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
62 Assertion.AssertEquals (String.Empty, namespaceManager.LookupNamespace (String.Empty));
64 // the namespaces should be the same references found in the name table.
65 Assertion.AssertSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
66 Assertion.AssertSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
67 Assertion.AssertSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));
69 // looking up undeclared namespaces should return null.
70 Assertion.AssertNull (namespaceManager.LookupNamespace ("foo"));
74 public void AddNamespace ()
76 // add a new namespace.
77 namespaceManager.AddNamespace ("foo", "http://foo/");
78 // make sure the new namespace is there.
79 Assertion.Assert (namespaceManager.HasNamespace ("foo"));
80 Assertion.AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
84 public void AddNamespaceWithNameTable ()
86 // add a known reference to the name table.
87 string fooNamespace = "http://foo/";
88 nameTable.Add(fooNamespace);
90 // create a new string with the same value but different address.
91 string fooNamespace2 = "http://";
92 fooNamespace2 += "foo/";
94 // the references must be different in order for this test to prove anything.
95 Assertion.Assert (!Object.ReferenceEquals (fooNamespace, fooNamespace2));
97 // add the namespace with the reference that's not in the name table.
98 namespaceManager.AddNamespace ("foo", fooNamespace2);
100 // the returned reference should be the same one that's in the name table.
101 Assertion.AssertSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
105 public void PushScope ()
107 // add a new namespace.
108 namespaceManager.AddNamespace ("foo", "http://foo/");
109 // make sure the new namespace is there.
110 Assertion.Assert (namespaceManager.HasNamespace ("foo"));
111 Assertion.AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
113 namespaceManager.PushScope ();
114 // add a new namespace.
115 namespaceManager.AddNamespace ("bar", "http://bar/");
116 // make sure the old namespace is not in this new scope.
117 Assertion.Assert (!namespaceManager.HasNamespace ("foo"));
118 // but we're still supposed to be able to lookup the old namespace.
119 Assertion.AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
120 // make sure the new namespace is there.
121 Assertion.Assert (namespaceManager.HasNamespace ("bar"));
122 Assertion.AssertEquals ("http://bar/", namespaceManager.LookupNamespace ("bar"));
126 public void PopScope ()
128 // add some namespaces and a scope.
131 Assertion.Assert (namespaceManager.PopScope ());
132 // make sure the first namespace is still there.
133 Assertion.Assert (namespaceManager.HasNamespace ("foo"));
134 Assertion.AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
135 // make sure the second namespace is no longer there.
136 Assertion.Assert (!namespaceManager.HasNamespace ("bar"));
137 Assertion.AssertNull (namespaceManager.LookupNamespace ("bar"));
138 // make sure there are no more scopes to pop.
139 Assertion.Assert (!namespaceManager.PopScope ());
140 // make sure that popping again doesn't cause an exception.
141 Assertion.Assert (!namespaceManager.PopScope ());
145 public void LookupPrefix ()
147 // This test should use an empty nametable.
148 XmlNamespaceManager nsmgr =
149 new XmlNamespaceManager (new NameTable ());
150 nsmgr.NameTable.Add ("urn:hoge");
151 nsmgr.NameTable.Add ("urn:fuga");
152 nsmgr.AddNamespace (string.Empty, "urn:hoge");
153 Assertion.AssertNull (nsmgr.LookupPrefix ("urn:fuga"));
154 Assertion.AssertEquals (String.Empty, nsmgr.LookupPrefix ("urn:hoge"));