* Removed AllTests.cs and TheTests.cs
[mono.git] / mcs / class / System.XML / Test / 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
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                         Assertion.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                         Assertion.AssertNotNull (xmlnsPrefix);
48                         Assertion.AssertNotNull (xmlPrefix);
49                         Assertion.AssertNotNull (stringEmpty);
50                         Assertion.AssertNotNull (xmlnsNamespace);
51                         Assertion.AssertNotNull (xmlNamespace);
52
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));
58
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));
63
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));
68
69                         // looking up undeclared namespaces should return null.
70                         Assertion.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                         Assertion.Assert (namespaceManager.HasNamespace ("foo"));
80                         Assertion.AssertEquals ("http://foo/", namespaceManager.LookupNamespace ("foo"));
81                 }
82
83                 [Test]
84                 public void AddNamespaceWithNameTable ()
85                 {
86                         // add a known reference to the name table.
87                         string fooNamespace = "http://foo/";
88                         nameTable.Add(fooNamespace);
89
90                         // create a new string with the same value but different address.
91                         string fooNamespace2 = "http://";
92                         fooNamespace2 += "foo/";
93
94                         // the references must be different in order for this test to prove anything.
95                         Assertion.Assert (!Object.ReferenceEquals (fooNamespace, fooNamespace2));
96
97                         // add the namespace with the reference that's not in the name table.
98                         namespaceManager.AddNamespace ("foo", fooNamespace2);
99
100                         // the returned reference should be the same one that's in the name table.
101                         Assertion.AssertSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
102                 }
103
104                 [Test]
105                 public void PushScope ()
106                 {
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"));
112                         // push a new scope.
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"));
123                 }
124
125                 [Test]
126                 public void PopScope ()
127                 {
128                         // add some namespaces and a scope.
129                         PushScope ();
130                         // pop the 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 ());
142                 }
143
144                 [Test]
145                 public void LookupPrefix ()
146                 {
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"));
155                 }
156         }
157 }