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 Assert.IsTrue (!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 Assert.IsNotNull (xmlnsPrefix);
48 Assert.IsNotNull (xmlPrefix);
49 Assert.IsNotNull (stringEmpty);
50 Assert.IsNotNull (xmlnsNamespace);
51 Assert.IsNotNull (xmlNamespace);
53 // Microsoft's XmlNamespaceManager reports that these three
54 // namespaces aren't declared for some reason.
55 Assert.IsTrue (!namespaceManager.HasNamespace ("xmlns"));
56 Assert.IsTrue (!namespaceManager.HasNamespace ("xml"));
57 Assert.IsTrue (!namespaceManager.HasNamespace (String.Empty));
59 // these three namespaces are declared by default.
60 Assert.AreEqual ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
61 Assert.AreEqual ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
62 Assert.AreEqual (String.Empty, namespaceManager.LookupNamespace (String.Empty));
64 // the namespaces should be the same references found in the name table.
65 Assert.AreSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
66 Assert.AreSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
67 Assert.AreSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));
69 // looking up undeclared namespaces should return null.
70 Assert.IsNull (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 Assert.IsTrue (namespaceManager.HasNamespace ("foo"));
80 Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
81 // adding a different namespace with the same prefix
83 namespaceManager.AddNamespace ("foo", "http://foo1/");
84 Assert.AreEqual ("http://foo1/", namespaceManager.LookupNamespace ("foo"));
88 public void AddNamespaceWithNameTable ()
90 // add a known reference to the name table.
91 string fooNamespace = "http://foo/";
92 nameTable.Add(fooNamespace);
94 // create a new string with the same value but different address.
95 string fooNamespace2 = "http://";
96 fooNamespace2 += "foo/";
98 // the references must be different in order for this test to prove anything.
99 Assert.IsTrue (!Object.ReferenceEquals (fooNamespace, fooNamespace2));
101 // add the namespace with the reference that's not in the name table.
102 namespaceManager.AddNamespace ("foo", fooNamespace2);
104 // the returned reference should be the same one that's in the name table.
105 Assert.AreSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
109 public void AddNamespace_XmlPrefix ()
111 namespaceManager.AddNamespace ("xml", "http://www.w3.org/XML/1998/namespace");
112 namespaceManager.AddNamespace ("XmL", "http://foo/");
113 namespaceManager.AddNamespace ("xmlsomething", "http://foo/");
117 [ExpectedException (typeof (ArgumentException))]
118 public void AddNamespace_XmlPrefix_Invalid ()
120 namespaceManager.AddNamespace ("xml", "http://foo/");
124 public void PushScope ()
126 // add a new namespace.
127 namespaceManager.AddNamespace ("foo", "http://foo/");
128 // make sure the new namespace is there.
129 Assert.IsTrue (namespaceManager.HasNamespace ("foo"));
130 Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
132 namespaceManager.PushScope ();
133 // add a new namespace.
134 namespaceManager.AddNamespace ("bar", "http://bar/");
135 // make sure the old namespace is not in this new scope.
136 Assert.IsTrue (!namespaceManager.HasNamespace ("foo"));
137 // but we're still supposed to be able to lookup the old namespace.
138 Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
139 // make sure the new namespace is there.
140 Assert.IsTrue (namespaceManager.HasNamespace ("bar"));
141 Assert.AreEqual ("http://bar/", namespaceManager.LookupNamespace ("bar"));
145 public void PopScope ()
147 // add some namespaces and a scope.
150 Assert.IsTrue (namespaceManager.PopScope ());
151 // make sure the first namespace is still there.
152 Assert.IsTrue (namespaceManager.HasNamespace ("foo"));
153 Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
154 // make sure the second namespace is no longer there.
155 Assert.IsTrue (!namespaceManager.HasNamespace ("bar"));
156 Assert.IsNull (namespaceManager.LookupNamespace ("bar"));
157 // make sure there are no more scopes to pop.
158 Assert.IsTrue (!namespaceManager.PopScope ());
159 // make sure that popping again doesn't cause an exception.
160 Assert.IsTrue (!namespaceManager.PopScope ());
164 public void PopScopeMustKeepAddedInScope ()
166 namespaceManager = new XmlNamespaceManager (new NameTable ()); // clear
167 namespaceManager .AddNamespace ("foo", "urn:foo"); // 0
168 namespaceManager .AddNamespace ("bar", "urn:bar"); // 0
169 namespaceManager .PushScope (); // 1
170 namespaceManager .PushScope (); // 2
171 namespaceManager .PopScope (); // 2
172 namespaceManager .PopScope (); // 1
173 namespaceManager .PopScope (); // 0
174 Assert.AreEqual ("urn:foo", namespaceManager.LookupNamespace ("foo"));
175 Assert.AreEqual ("urn:bar", namespaceManager.LookupNamespace ("bar"));
179 public void AddPushPopRemove ()
181 XmlNamespaceManager nsmgr =
182 new XmlNamespaceManager (new NameTable ());
183 string ns = nsmgr.NameTable.Add ("urn:foo");
184 nsmgr.AddNamespace ("foo", ns);
185 Assert.AreEqual ("foo", nsmgr.LookupPrefix (ns));
187 Assert.AreEqual ("foo", nsmgr.LookupPrefix (ns));
189 Assert.AreEqual ("foo", nsmgr.LookupPrefix (ns));
190 nsmgr.RemoveNamespace ("foo", ns);
191 Assert.IsNull (nsmgr.LookupPrefix (ns));
195 public void LookupPrefix ()
197 // This test should use an empty nametable.
198 XmlNamespaceManager nsmgr =
199 new XmlNamespaceManager (new NameTable ());
200 nsmgr.NameTable.Add ("urn:hoge");
201 nsmgr.NameTable.Add ("urn:fuga");
202 nsmgr.AddNamespace (string.Empty, "urn:hoge");
203 Assert.IsNull (nsmgr.LookupPrefix ("urn:fuga"));
204 Assert.AreEqual (String.Empty, nsmgr.LookupPrefix ("urn:hoge"));
207 string suffix = "oo";
210 public void AtomizedLookup ()
212 if (DateTime.Now.Year == 0)
213 suffix = String.Empty;
214 XmlNamespaceManager nsmgr =
215 new XmlNamespaceManager (new NameTable ());
216 nsmgr.AddNamespace ("foo", "urn:foo");
217 Assert.IsNotNull (nsmgr.LookupPrefix ("urn:foo"));
218 // FIXME: This returns registered URI inconsistently.
219 // Assert.IsNull (nsmgr.LookupPrefix ("urn:f" + suffix), "It is not atomized and thus should be failed");
223 XmlNamespaceScope l = XmlNamespaceScope.Local;
224 XmlNamespaceScope x = XmlNamespaceScope.ExcludeXml;
225 XmlNamespaceScope a = XmlNamespaceScope.All;
228 [Category ("NotDotNet")] // MS bug
229 public void GetNamespacesInScope ()
231 XmlNamespaceManager nsmgr =
232 new XmlNamespaceManager (new NameTable ());
234 Assert.AreEqual (0, nsmgr.GetNamespacesInScope (l).Count, "#1");
235 Assert.AreEqual (0, nsmgr.GetNamespacesInScope (x).Count, "#2");
236 Assert.AreEqual (1, nsmgr.GetNamespacesInScope (a).Count, "#3");
238 nsmgr.AddNamespace ("foo", "urn:foo");
239 Assert.AreEqual (1, nsmgr.GetNamespacesInScope (l).Count, "#4");
240 Assert.AreEqual (1, nsmgr.GetNamespacesInScope (x).Count, "#5");
241 Assert.AreEqual (2, nsmgr.GetNamespacesInScope (a).Count, "#6");
244 nsmgr.AddNamespace ("", "urn:empty");
245 Assert.AreEqual (2, nsmgr.GetNamespacesInScope (l).Count, "#7");
246 Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#8");
247 Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#9");
250 nsmgr.AddNamespace ("foo", "urn:foo");
252 Assert.AreEqual (0, nsmgr.GetNamespacesInScope (l).Count, "#10");
253 Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#11");
254 Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#12");
258 Assert.AreEqual (2, nsmgr.GetNamespacesInScope (l).Count, "#13");
259 Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#14");
260 Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#15");
262 nsmgr.AddNamespace ("", "");
263 // MS bug - it should return 1 for .Local but it returns 2 instead.
264 Assert.AreEqual (1, nsmgr.GetNamespacesInScope (l).Count, "#16");
265 Assert.AreEqual (1, nsmgr.GetNamespacesInScope (x).Count, "#17");
266 Assert.AreEqual (2, nsmgr.GetNamespacesInScope (a).Count, "#18");