File: XmlNamespaceManagerTests.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (286 lines) | stat: -rw-r--r-- 10,139 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// XmlNamespaceManagerTests.cs
//
// Authors:
//   Jason Diamond (jason@injektilo.org)
//   Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (C) 2002 Jason Diamond  http://injektilo.org/
// (C) 2003 Martin Willemoes Hansen
//

using System;
using System.Xml;

using NUnit.Framework;

namespace MonoTests.System.Xml
{
	[TestFixture]
	public class XmlNamespaceManagerTests
	{
		private XmlNameTable nameTable;
		private XmlNamespaceManager namespaceManager;

		[SetUp]
		public void GetReady ()
		{
			nameTable = new NameTable ();
			namespaceManager = new XmlNamespaceManager (nameTable);
		}

		[Test]
		public void NewNamespaceManager ()
		{
			// make sure that you can call PopScope when there aren't any to pop.
			Assert.IsTrue (!namespaceManager.PopScope ());

			// the following strings should have been added to the name table by the
			// namespace manager.
			string xmlnsPrefix = nameTable.Get ("xmlns");
			string xmlPrefix = nameTable.Get ("xml");
			string stringEmpty = nameTable.Get (String.Empty);
			string xmlnsNamespace = "http://www.w3.org/2000/xmlns/";
			string xmlNamespace = "http://www.w3.org/XML/1998/namespace";

			// none of them should be null.
			Assert.IsNotNull (xmlnsPrefix);
			Assert.IsNotNull (xmlPrefix);
			Assert.IsNotNull (stringEmpty);
			Assert.IsNotNull (xmlnsNamespace);
			Assert.IsNotNull (xmlNamespace);

			// Microsoft's XmlNamespaceManager reports that these three
			// namespaces aren't declared for some reason.
			Assert.IsTrue (!namespaceManager.HasNamespace ("xmlns"));
			Assert.IsTrue (!namespaceManager.HasNamespace ("xml"));
			Assert.IsTrue (!namespaceManager.HasNamespace (String.Empty));

			// these three namespaces are declared by default.
			Assert.AreEqual ("http://www.w3.org/2000/xmlns/", namespaceManager.LookupNamespace ("xmlns"));
			Assert.AreEqual ("http://www.w3.org/XML/1998/namespace", namespaceManager.LookupNamespace ("xml"));
			Assert.AreEqual (String.Empty, namespaceManager.LookupNamespace (String.Empty));

			// the namespaces should be the same references found in the name table.
			Assert.AreSame (xmlnsNamespace, namespaceManager.LookupNamespace ("xmlns"));
			Assert.AreSame (xmlNamespace, namespaceManager.LookupNamespace ("xml"));
			Assert.AreSame (stringEmpty, namespaceManager.LookupNamespace (String.Empty));

			// looking up undeclared namespaces should return null.
			Assert.IsNull (namespaceManager.LookupNamespace ("foo"));
		}

		[Test]
		public void AddNamespace ()
		{
			// add a new namespace.
			namespaceManager.AddNamespace ("foo", "http://foo/");
			// make sure the new namespace is there.
			Assert.IsTrue (namespaceManager.HasNamespace ("foo"));
			Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
			// adding a different namespace with the same prefix
			// is allowed.
			namespaceManager.AddNamespace ("foo", "http://foo1/");
			Assert.AreEqual ("http://foo1/", namespaceManager.LookupNamespace ("foo"));
		}

		[Test]
		public void AddNamespaceWithNameTable ()
		{
			// add a known reference to the name table.
			string fooNamespace = "http://foo/";
			nameTable.Add(fooNamespace);

			// create a new string with the same value but different address.
			string fooNamespace2 = "http://";
			fooNamespace2 += "foo/";

			// the references must be different in order for this test to prove anything.
			Assert.IsTrue (!Object.ReferenceEquals (fooNamespace, fooNamespace2));

			// add the namespace with the reference that's not in the name table.
			namespaceManager.AddNamespace ("foo", fooNamespace2);

			// the returned reference should be the same one that's in the name table.
			Assert.AreSame (fooNamespace, namespaceManager.LookupNamespace ("foo"));
		}

		[Test]
		public void AddNamespace_XmlPrefix ()
		{
			namespaceManager.AddNamespace ("xml", "http://www.w3.org/XML/1998/namespace");
			namespaceManager.AddNamespace ("XmL", "http://foo/");
			namespaceManager.AddNamespace ("xmlsomething", "http://foo/");
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void AddNamespace_XmlPrefix_Invalid ()
		{
			namespaceManager.AddNamespace ("xml", "http://foo/");
		}

		[Test]
		public void PushScope ()
		{
			// add a new namespace.
			namespaceManager.AddNamespace ("foo", "http://foo/");
			// make sure the new namespace is there.
			Assert.IsTrue (namespaceManager.HasNamespace ("foo"));
			Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
			// push a new scope.
			namespaceManager.PushScope ();
			// add a new namespace.
			namespaceManager.AddNamespace ("bar", "http://bar/");
			// make sure the old namespace is not in this new scope.
			Assert.IsTrue (!namespaceManager.HasNamespace ("foo"));
			// but we're still supposed to be able to lookup the old namespace.
			Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
			// make sure the new namespace is there.
			Assert.IsTrue (namespaceManager.HasNamespace ("bar"));
			Assert.AreEqual ("http://bar/", namespaceManager.LookupNamespace ("bar"));
		}

		[Test]
		public void PopScope ()
		{
			// add some namespaces and a scope.
			PushScope ();
			// pop the scope.
			Assert.IsTrue (namespaceManager.PopScope ());
			// make sure the first namespace is still there.
			Assert.IsTrue (namespaceManager.HasNamespace ("foo"));
			Assert.AreEqual ("http://foo/", namespaceManager.LookupNamespace ("foo"));
			// make sure the second namespace is no longer there.
			Assert.IsTrue (!namespaceManager.HasNamespace ("bar"));
			Assert.IsNull (namespaceManager.LookupNamespace ("bar"));
			// make sure there are no more scopes to pop.
			Assert.IsTrue (!namespaceManager.PopScope ());
			// make sure that popping again doesn't cause an exception.
			Assert.IsTrue (!namespaceManager.PopScope ());
		}

		[Test]
		public void PopScopeMustKeepAddedInScope ()
		{
			namespaceManager = new XmlNamespaceManager (new NameTable ()); // clear
			namespaceManager .AddNamespace ("foo", "urn:foo");	// 0
			namespaceManager .AddNamespace ("bar", "urn:bar");	// 0
			namespaceManager .PushScope ();	// 1
			namespaceManager .PushScope ();	// 2
			namespaceManager .PopScope ();	// 2
			namespaceManager .PopScope ();	// 1
			namespaceManager .PopScope ();	// 0
			Assert.AreEqual ("urn:foo", namespaceManager.LookupNamespace ("foo"));
			Assert.AreEqual ("urn:bar", namespaceManager.LookupNamespace ("bar"));
		}

		[Test]
		public void AddPushPopRemove ()
		{
			XmlNamespaceManager nsmgr =
				new XmlNamespaceManager (new NameTable ());
			string ns = nsmgr.NameTable.Add ("urn:foo");
			nsmgr.AddNamespace ("foo", ns);
			Assert.AreEqual ("foo", nsmgr.LookupPrefix (ns));
			nsmgr.PushScope ();
			Assert.AreEqual ("foo", nsmgr.LookupPrefix (ns));
			nsmgr.PopScope ();
			Assert.AreEqual ("foo", nsmgr.LookupPrefix (ns));
			nsmgr.RemoveNamespace ("foo", ns);
			Assert.IsNull (nsmgr.LookupPrefix (ns));
		}

		[Test]
		public void LookupPrefix ()
		{
			// This test should use an empty nametable.
			XmlNamespaceManager nsmgr =
				new XmlNamespaceManager (new NameTable ());
			nsmgr.NameTable.Add ("urn:hoge");
			nsmgr.NameTable.Add ("urn:fuga");
			nsmgr.AddNamespace (string.Empty, "urn:hoge");
			Assert.IsNull (nsmgr.LookupPrefix ("urn:fuga"));
			Assert.AreEqual (String.Empty, nsmgr.LookupPrefix ("urn:hoge"));
		}

		string suffix = "oo";

		[Test]
		public void AtomizedLookup ()
		{
			if (DateTime.Now.Year == 0)
				suffix = String.Empty;
			XmlNamespaceManager nsmgr =
				new XmlNamespaceManager (new NameTable ());
			nsmgr.AddNamespace ("foo", "urn:foo");
			Assert.IsNotNull (nsmgr.LookupPrefix ("urn:foo"));
// FIXME: This returns registered URI inconsistently.
//			Assert.IsNull (nsmgr.LookupPrefix ("urn:f" + suffix), "It is not atomized and thus should be failed");
		}

		[Test]
		public void TryToAddPrefixXml ()
		{
			NameTable nt = new NameTable ();
			XmlNamespaceManager nsmgr = new XmlNamespaceManager (nt);

			nsmgr.AddNamespace ("xml", "http://www.w3.org/XML/1998/namespace");
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void TryToAddPrefixXmlns ()
		{
			NameTable nt = new NameTable ();
			XmlNamespaceManager nsmgr = new XmlNamespaceManager (nt);

			nsmgr.AddNamespace ("xmlns", "http://www.w3.org/2000/xmlns/");
		}

		XmlNamespaceScope l = XmlNamespaceScope.Local;
		XmlNamespaceScope x = XmlNamespaceScope.ExcludeXml;
		XmlNamespaceScope a = XmlNamespaceScope.All;

		[Test]
		public void GetNamespacesInScope ()
		{
			XmlNamespaceManager nsmgr =
				new XmlNamespaceManager (new NameTable ());

			Assert.AreEqual (0, nsmgr.GetNamespacesInScope (l).Count, "#1");
			Assert.AreEqual (0, nsmgr.GetNamespacesInScope (x).Count, "#2");
			Assert.AreEqual (1, nsmgr.GetNamespacesInScope (a).Count, "#3");

			nsmgr.AddNamespace ("foo", "urn:foo");
			Assert.AreEqual (1, nsmgr.GetNamespacesInScope (l).Count, "#4");
			Assert.AreEqual (1, nsmgr.GetNamespacesInScope (x).Count, "#5");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (a).Count, "#6");

			// default namespace
			nsmgr.AddNamespace ("", "urn:empty");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (l).Count, "#7");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#8");
			Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#9");

			// PushScope
			nsmgr.AddNamespace ("foo", "urn:foo");
			nsmgr.PushScope ();
			Assert.AreEqual (0, nsmgr.GetNamespacesInScope (l).Count, "#10");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#11");
			Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#12");

			// PopScope
			nsmgr.PopScope ();
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (l).Count, "#13");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (x).Count, "#14");
			Assert.AreEqual (3, nsmgr.GetNamespacesInScope (a).Count, "#15");

			nsmgr.AddNamespace ("", "");
			// MS bug - it should return 1 for .Local but it returns 2 instead.
			//Assert.AreEqual (1, nsmgr.GetNamespacesInScope (l).Count, "#16");
			Assert.AreEqual (1, nsmgr.GetNamespacesInScope (x).Count, "#17");
			Assert.AreEqual (2, nsmgr.GetNamespacesInScope (a).Count, "#18");
		}
	}
}