(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.XML / Test / System.Xml / XmlNamespaceManagerTests.cs
blob4ddfda7e8a6b09b9bfdbeb3a36f82f5900dd6732
1 //
2 // XmlNamespaceManagerTests.cs
3 //
4 // Authors:
5 // Jason Diamond (jason@injektilo.org)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 //
8 // (C) 2002 Jason Diamond http://injektilo.org/
9 // (C) 2003 Martin Willemoes Hansen
12 using System;
13 using System.Xml;
15 using NUnit.Framework;
17 namespace MonoTests.System.Xml
19 [TestFixture]
20 public class XmlNamespaceManagerTests : Assertion
22 private XmlNameTable nameTable;
23 private XmlNamespaceManager namespaceManager;
25 [SetUp]
26 public void GetReady ()
28 nameTable = new NameTable ();
29 namespaceManager = new XmlNamespaceManager (nameTable);
32 [Test]
33 public void NewNamespaceManager ()
35 // make sure that you can call PopScope when there aren't any to pop.
36 Assert (!namespaceManager.PopScope ());
38 // the following strings should have been added to the name table by the
39 // namespace manager.
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 AssertNotNull (xmlnsPrefix);
48 AssertNotNull (xmlPrefix);
49 AssertNotNull (stringEmpty);
50 AssertNotNull (xmlnsNamespace);
51 AssertNotNull (xmlNamespace);
53 // Microsoft's XmlNamespaceManager reports that these three
54 // namespaces aren't declared for some reason.
55 Assert (!namespaceManager.HasNamespace ("xmlns"));
56 Assert (!namespaceManager.HasNamespace ("xml"));
57 Assert (!namespaceManager.HasNamespace (String.Empty));
59 // these three namespaces are declared by default.
60 AssertEquals ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
61 AssertEquals ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
62 AssertEquals (String.Empty, namespaceManager.LookupNamespace (String.Empty));
64 // the namespaces should be the same references found in the name table.
65 AssertSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
66 AssertSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
67 AssertSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));
69 // looking up undeclared namespaces should return null.
70 AssertNull (namespaceManager.LookupNamespace ("foo"));
73 [Test]
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 (namespaceManager.HasNamespace ("foo"));
80 AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
81 // adding a different namespace with the same prefix
82 // is allowed.
83 namespaceManager.AddNamespace ("foo", "http://foo1/");
84 AssertEquals ("http://foo1/", namespaceManager.LookupNamespace ("foo"));
87 [Test]
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 (!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 AssertSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
108 [Test]
109 public void PushScope ()
111 // add a new namespace.
112 namespaceManager.AddNamespace ("foo", "http://foo/");
113 // make sure the new namespace is there.
114 Assert (namespaceManager.HasNamespace ("foo"));
115 AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
116 // push a new scope.
117 namespaceManager.PushScope ();
118 // add a new namespace.
119 namespaceManager.AddNamespace ("bar", "http://bar/");
120 // make sure the old namespace is not in this new scope.
121 Assert (!namespaceManager.HasNamespace ("foo"));
122 // but we're still supposed to be able to lookup the old namespace.
123 AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
124 // make sure the new namespace is there.
125 Assert (namespaceManager.HasNamespace ("bar"));
126 AssertEquals ("http://bar/", namespaceManager.LookupNamespace ("bar"));
129 [Test]
130 public void PopScope ()
132 // add some namespaces and a scope.
133 PushScope ();
134 // pop the scope.
135 Assert (namespaceManager.PopScope ());
136 // make sure the first namespace is still there.
137 Assert (namespaceManager.HasNamespace ("foo"));
138 AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
139 // make sure the second namespace is no longer there.
140 Assert (!namespaceManager.HasNamespace ("bar"));
141 AssertNull (namespaceManager.LookupNamespace ("bar"));
142 // make sure there are no more scopes to pop.
143 Assert (!namespaceManager.PopScope ());
144 // make sure that popping again doesn't cause an exception.
145 Assert (!namespaceManager.PopScope ());
148 [Test]
149 public void PopScopeMustKeepAddedInScope ()
151 namespaceManager = new XmlNamespaceManager (new NameTable ()); // clear
152 namespaceManager .AddNamespace ("foo", "urn:foo"); // 0
153 namespaceManager .AddNamespace ("bar", "urn:bar"); // 0
154 namespaceManager .PushScope (); // 1
155 namespaceManager .PushScope (); // 2
156 namespaceManager .PopScope (); // 2
157 namespaceManager .PopScope (); // 1
158 namespaceManager .PopScope (); // 0
159 AssertEquals ("urn:foo", namespaceManager.LookupNamespace ("foo"));
160 AssertEquals ("urn:bar", namespaceManager.LookupNamespace ("bar"));
163 [Test]
164 public void AddPushPopRemove ()
166 XmlNamespaceManager nsmgr =
167 new XmlNamespaceManager (new NameTable ());
168 string ns = nsmgr.NameTable.Add ("urn:foo");
169 nsmgr.AddNamespace ("foo", ns);
170 AssertEquals ("foo", nsmgr.LookupPrefix (ns));
171 nsmgr.PushScope ();
172 AssertEquals ("foo", nsmgr.LookupPrefix (ns));
173 nsmgr.PopScope ();
174 AssertEquals ("foo", nsmgr.LookupPrefix (ns));
175 nsmgr.RemoveNamespace ("foo", ns);
176 AssertNull (nsmgr.LookupPrefix (ns));
179 [Test]
180 public void LookupPrefix ()
182 // This test should use an empty nametable.
183 XmlNamespaceManager nsmgr =
184 new XmlNamespaceManager (new NameTable ());
185 nsmgr.NameTable.Add ("urn:hoge");
186 nsmgr.NameTable.Add ("urn:fuga");
187 nsmgr.AddNamespace (string.Empty, "urn:hoge");
188 AssertNull (nsmgr.LookupPrefix ("urn:fuga"));
189 AssertEquals (String.Empty, nsmgr.LookupPrefix ("urn:hoge"));
192 string suffix = "oo";
194 [Test]
195 public void AtomizedLookup ()
197 if (DateTime.Now.Year == 0)
198 suffix = String.Empty;
199 XmlNamespaceManager nsmgr =
200 new XmlNamespaceManager (new NameTable ());
201 nsmgr.AddNamespace ("foo", "urn:foo");
202 AssertNotNull (nsmgr.LookupPrefix ("urn:foo"));
203 // FIXME: This returns registered URI inconsistently.
204 // AssertNull ("It is not atomized and thus should be failed", nsmgr.LookupPrefix ("urn:f" + suffix));
205 #if NET_2_0
206 AssertNotNull ("Atomization should not matter.", nsmgr.LookupPrefix ("urn:f" + suffix, false));
207 #endif
210 #if NET_2_0
211 XmlNamespaceScope l = XmlNamespaceScope.Local;
212 XmlNamespaceScope x = XmlNamespaceScope.ExcludeXml;
213 XmlNamespaceScope a = XmlNamespaceScope.All;
215 [Test]
216 public void GetNamespacesInScope ()
218 XmlNamespaceManager nsmgr =
219 new XmlNamespaceManager (new NameTable ());
221 AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
222 AssertEquals (0, nsmgr.GetNamespacesInScope (x).Count);
223 AssertEquals (1, nsmgr.GetNamespacesInScope (a).Count);
225 nsmgr.AddNamespace ("foo", "urn:foo");
226 AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
227 AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
228 AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
230 nsmgr.RemoveNamespace ("foo", "urn:foo", false);
231 AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
232 AssertEquals (0, nsmgr.GetNamespacesInScope (x).Count);
233 AssertEquals (1, nsmgr.GetNamespacesInScope (a).Count);
235 // default namespace
236 nsmgr.AddNamespace ("", "urn:empty");
237 AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
238 AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
239 AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
241 nsmgr.RemoveNamespace ("", "urn:empty", false);
242 AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
243 AssertEquals (0, nsmgr.GetNamespacesInScope (x).Count);
244 AssertEquals (1, nsmgr.GetNamespacesInScope (a).Count);
246 // PushScope
247 nsmgr.AddNamespace ("foo", "urn:foo");
248 nsmgr.PushScope ();
249 AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
250 AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
251 AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
253 // PopScope
254 nsmgr.PopScope ();
255 AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
256 AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
257 AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
259 nsmgr.AddNamespace ("", "");
260 AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
261 AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
262 AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
264 #endif