2005-05-05 Atsushi Enomoto <atsushi@ximian.com>
[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 PushScope ()
110                 {
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"));
127                 }
128
129                 [Test]
130                 public void PopScope ()
131                 {
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 ());
146                 }
147
148                 [Test]
149                 public void PopScopeMustKeepAddedInScope ()
150                 {
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"));
161                 }
162
163                 [Test]
164                 public void AddPushPopRemove ()
165                 {
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));
177                 }
178
179                 [Test]
180                 public void LookupPrefix ()
181                 {
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"));
190                 }
191
192                 string suffix = "oo";
193
194                 [Test]
195                 public void AtomizedLookup ()
196                 {
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                 }
206
207 #if NET_2_0
208                 XmlNamespaceScope l = XmlNamespaceScope.Local;
209                 XmlNamespaceScope x = XmlNamespaceScope.ExcludeXml;
210                 XmlNamespaceScope a = XmlNamespaceScope.All;
211
212                 [Test]
213                 public void GetNamespacesInScope ()
214                 {
215                         XmlNamespaceManager nsmgr =
216                                 new XmlNamespaceManager (new NameTable ());
217
218                         AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
219                         AssertEquals (0, nsmgr.GetNamespacesInScope (x).Count);
220                         AssertEquals (1, nsmgr.GetNamespacesInScope (a).Count);
221
222                         nsmgr.AddNamespace ("foo", "urn:foo");
223                         AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
224                         AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
225                         AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
226
227                         // default namespace
228                         nsmgr.AddNamespace ("", "urn:empty");
229                         AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
230                         AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
231                         AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
232
233                         // PushScope
234                         nsmgr.AddNamespace ("foo", "urn:foo");
235                         nsmgr.PushScope ();
236                         AssertEquals (0, nsmgr.GetNamespacesInScope (l).Count);
237                         AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
238                         AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
239
240                         // PopScope
241                         nsmgr.PopScope ();
242                         AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
243                         AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
244                         AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
245
246                         nsmgr.AddNamespace ("", "");
247                         AssertEquals (1, nsmgr.GetNamespacesInScope (l).Count);
248                         AssertEquals (1, nsmgr.GetNamespacesInScope (x).Count);
249                         AssertEquals (2, nsmgr.GetNamespacesInScope (a).Count);
250                 }
251 #endif
252         }
253 }