* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.XML / Test / System.Xml / XmlNamespaceManagerTests.cs
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
10 //
11
12 using System;
13 using System.Xml;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.Xml
18 {
19         [TestFixture]
20         public class XmlNamespaceManagerTests : Assertion
21         {
22                 private XmlNameTable nameTable;
23                 private XmlNamespaceManager namespaceManager;
24
25                 [SetUp]
26                 public void GetReady ()
27                 {
28                         nameTable = new NameTable ();
29                         namespaceManager = new XmlNamespaceManager (nameTable);
30                 }
31
32                 [Test]
33                 public void NewNamespaceManager ()
34                 {
35                         // make sure that you can call PopScope when there aren't any to pop.
36                         Assert (!namespaceManager.PopScope ());
37
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";
45
46                         // none of them should be null.
47                         AssertNotNull (xmlnsPrefix);
48                         AssertNotNull (xmlPrefix);
49                         AssertNotNull (stringEmpty);
50                         AssertNotNull (xmlnsNamespace);
51                         AssertNotNull (xmlNamespace);
52
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));
58
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));
63
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));
68
69                         // looking up undeclared namespaces should return null.
70                         AssertNull (namespaceManager.LookupNamespace ("foo"));
71                 }
72
73                 [Test]
74                 public void AddNamespace ()
75                 {
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"));
85                 }
86
87                 [Test]
88                 public void AddNamespaceWithNameTable ()
89                 {
90                         // add a known reference to the name table.
91                         string fooNamespace = "http://foo/";
92                         nameTable.Add(fooNamespace);
93
94                         // create a new string with the same value but different address.
95                         string fooNamespace2 = "http://";
96                         fooNamespace2 += "foo/";
97
98                         // the references must be different in order for this test to prove anything.
99                         Assert (!Object.ReferenceEquals (fooNamespace, fooNamespace2));
100
101                         // add the namespace with the reference that's not in the name table.
102                         namespaceManager.AddNamespace ("foo", fooNamespace2);
103
104                         // the returned reference should be the same one that's in the name table.
105                         AssertSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
106                 }
107
108                 [Test]
109                 public void AddNamespace_XmlPrefix ()
110                 {
111                         namespaceManager.AddNamespace ("xml", "http://www.w3.org/XML/1998/namespace");
112                         namespaceManager.AddNamespace ("XmL", "http://foo/");
113                         namespaceManager.AddNamespace ("xmlsomething", "http://foo/");
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (ArgumentException))]
118                 public void AddNamespace_XmlPrefix_Invalid ()
119                 {
120                         namespaceManager.AddNamespace ("xml", "http://foo/");
121                 }
122
123                 [Test]
124                 public void PushScope ()
125                 {
126                         // add a new namespace.
127                         namespaceManager.AddNamespace ("foo", "http://foo/");
128                         // make sure the new namespace is there.
129                         Assert (namespaceManager.HasNamespace ("foo"));
130                         AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
131                         // push a new scope.
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 (!namespaceManager.HasNamespace ("foo"));
137                         // but we're still supposed to be able to lookup the old namespace.
138                         AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
139                         // make sure the new namespace is there.
140                         Assert (namespaceManager.HasNamespace ("bar"));
141                         AssertEquals ("http://bar/", namespaceManager.LookupNamespace ("bar"));
142                 }
143
144                 [Test]
145                 public void PopScope ()
146                 {
147                         // add some namespaces and a scope.
148                         PushScope ();
149                         // pop the scope.
150                         Assert (namespaceManager.PopScope ());
151                         // make sure the first namespace is still there.
152                         Assert (namespaceManager.HasNamespace ("foo"));
153                         AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
154                         // make sure the second namespace is no longer there.
155                         Assert (!namespaceManager.HasNamespace ("bar"));
156                         AssertNull (namespaceManager.LookupNamespace ("bar"));
157                         // make sure there are no more scopes to pop.
158                         Assert (!namespaceManager.PopScope ());
159                         // make sure that popping again doesn't cause an exception.
160                         Assert (!namespaceManager.PopScope ());
161                 }
162
163                 [Test]
164                 public void PopScopeMustKeepAddedInScope ()
165                 {
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                         AssertEquals ("urn:foo", namespaceManager.LookupNamespace ("foo"));
175                         AssertEquals ("urn:bar", namespaceManager.LookupNamespace ("bar"));
176                 }
177
178                 [Test]
179                 public void AddPushPopRemove ()
180                 {
181                         XmlNamespaceManager nsmgr =
182                                 new XmlNamespaceManager (new NameTable ());
183                         string ns = nsmgr.NameTable.Add ("urn:foo");
184                         nsmgr.AddNamespace ("foo", ns);
185                         AssertEquals ("foo", nsmgr.LookupPrefix (ns));
186                         nsmgr.PushScope ();
187                         AssertEquals ("foo", nsmgr.LookupPrefix (ns));
188                         nsmgr.PopScope ();
189                         AssertEquals ("foo", nsmgr.LookupPrefix (ns));
190                         nsmgr.RemoveNamespace ("foo", ns);
191                         AssertNull (nsmgr.LookupPrefix (ns));
192                 }
193
194                 [Test]
195                 public void LookupPrefix ()
196                 {
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                         AssertNull (nsmgr.LookupPrefix ("urn:fuga"));
204                         AssertEquals (String.Empty, nsmgr.LookupPrefix ("urn:hoge"));
205                 }
206
207                 string suffix = "oo";
208
209                 [Test]
210                 public void AtomizedLookup ()
211                 {
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                         AssertNotNull (nsmgr.LookupPrefix ("urn:foo"));
218 // FIXME: This returns registered URI inconsistently.
219 //                      AssertNull ("It is not atomized and thus should be failed", nsmgr.LookupPrefix ("urn:f" + suffix));
220                 }
221
222 #if NET_2_0
223                 XmlNamespaceScope l = XmlNamespaceScope.Local;
224                 XmlNamespaceScope x = XmlNamespaceScope.ExcludeXml;
225                 XmlNamespaceScope a = XmlNamespaceScope.All;
226
227                 [Test]
228                 [Category ("NotDotNet")] // MS bug
229                 public void GetNamespacesInScope ()
230                 {
231                         XmlNamespaceManager nsmgr =
232                                 new XmlNamespaceManager (new NameTable ());
233
234                         AssertEquals ("#1", 0, nsmgr.GetNamespacesInScope (l).Count);
235                         AssertEquals ("#2", 0, nsmgr.GetNamespacesInScope (x).Count);
236                         AssertEquals ("#3", 1, nsmgr.GetNamespacesInScope (a).Count);
237
238                         nsmgr.AddNamespace ("foo", "urn:foo");
239                         AssertEquals ("#4", 1, nsmgr.GetNamespacesInScope (l).Count);
240                         AssertEquals ("#5", 1, nsmgr.GetNamespacesInScope (x).Count);
241                         AssertEquals ("#6", 2, nsmgr.GetNamespacesInScope (a).Count);
242
243                         // default namespace
244                         nsmgr.AddNamespace ("", "urn:empty");
245                         AssertEquals ("#7", 2, nsmgr.GetNamespacesInScope (l).Count);
246                         AssertEquals ("#8", 2, nsmgr.GetNamespacesInScope (x).Count);
247                         AssertEquals ("#9", 3, nsmgr.GetNamespacesInScope (a).Count);
248
249                         // PushScope
250                         nsmgr.AddNamespace ("foo", "urn:foo");
251                         nsmgr.PushScope ();
252                         AssertEquals ("#10", 0, nsmgr.GetNamespacesInScope (l).Count);
253                         AssertEquals ("#11", 2, nsmgr.GetNamespacesInScope (x).Count);
254                         AssertEquals ("#12", 3, nsmgr.GetNamespacesInScope (a).Count);
255
256                         // PopScope
257                         nsmgr.PopScope ();
258                         AssertEquals ("#13", 2, nsmgr.GetNamespacesInScope (l).Count);
259                         AssertEquals ("#14", 2, nsmgr.GetNamespacesInScope (x).Count);
260                         AssertEquals ("#15", 3, nsmgr.GetNamespacesInScope (a).Count);
261
262                         nsmgr.AddNamespace ("", "");
263                         // MS bug - it should return 1 for .Local but it returns 2 instead.
264                         AssertEquals ("#16", 1, nsmgr.GetNamespacesInScope (l).Count);
265                         AssertEquals ("#17", 1, nsmgr.GetNamespacesInScope (x).Count);
266                         AssertEquals ("#18", 2, nsmgr.GetNamespacesInScope (a).Count);
267                 }
268 #endif
269         }
270 }