File: XmlNodeReaderTests.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-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 (205 lines) | stat: -rw-r--r-- 6,518 bytes parent folder | download | duplicates (2)
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
//
// System.Xml.XmlNodeReaderTests
//
// Authors:
//   Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
//   Martin Willemoes Hansen <mwh@sysrq.dk>
//
// (C) 2003 Atsushi Enomoto
// (C) 2003 Martin Willemoes Hansen
//
//

using System;
using System.IO;
using System.Text;
using System.Xml;

using NUnit.Framework;

namespace MonoTests.System.Xml
{
	[TestFixture]
	public class XmlNodeReaderTests
	{
		[SetUp]
		public void GetReady ()
		{
			document.LoadXml ("<root attr1='value1'><child /></root>");
		}

		XmlDocument document = new XmlDocument ();

		[Test]
		public void InvalidConstruction ()
		{
			XmlNodeReader nrdr;
			try {
				nrdr = new XmlNodeReader (null);
				Assert.Fail ("ArgumentNullException is expected.");
			} catch (ArgumentNullException) {
			}
			nrdr = new XmlNodeReader (new XmlDocument ());
			nrdr.Read ();
			Assert.AreEqual (ReadState.Error, nrdr.ReadState, "newDoc.ReadState");
			Assert.AreEqual (true, nrdr.EOF, "newDoc.EOF");
			Assert.AreEqual (XmlNodeType.None, nrdr.NodeType, "newDoc.NodeType");
			nrdr = new XmlNodeReader (document.CreateDocumentFragment ());
			nrdr.Read ();
			Assert.AreEqual (ReadState.Error, nrdr.ReadState, "Fragment.ReadState");
			Assert.AreEqual (true, nrdr.EOF, "Fragment.EOF");
			Assert.AreEqual (XmlNodeType.None, nrdr.NodeType, "Fragment.NodeType");
		}

		[Test]
		public void ReadFromElement ()
		{
			XmlNodeReader nrdr = new XmlNodeReader (document.DocumentElement);
			nrdr.Read ();
			Assert.AreEqual (XmlNodeType.Element, nrdr.NodeType, "<root>.NodeType");
			Assert.AreEqual ("root", nrdr.Name, "<root>.Name");
			Assert.AreEqual (ReadState.Interactive, nrdr.ReadState, "<root>.ReadState");
			Assert.AreEqual (0, nrdr.Depth, "<root>.Depth");
		}


		[Test]
		public void ReadInnerXmlWrongInit ()
		{
			document.LoadXml ("<root>test of <b>mixed</b> string.</root>");
			XmlNodeReader nrdr = new XmlNodeReader (document);
			nrdr.ReadInnerXml ();
			Assert.AreEqual (ReadState.Initial, nrdr.ReadState, "initial.ReadState");
			Assert.AreEqual (false, nrdr.EOF, "initial.EOF");
			Assert.AreEqual (XmlNodeType.None, nrdr.NodeType, "initial.NodeType");
		}

		[Test]
		public void ResolveEntity ()
		{
			string ent1 = "<!ENTITY ent 'entity string'>";
			string ent2 = "<!ENTITY ent2 '<foo/><foo/>'>]>";
			string dtd = "<!DOCTYPE root[<!ELEMENT root (#PCDATA|foo)*>" + ent1 + ent2;
			string xml = dtd + "<root>&ent;&ent2;</root>";
			document.LoadXml (xml);
			Assert.AreEqual (xml, document.OuterXml);
			XmlNodeReader nr = new XmlNodeReader (document);
			nr.Read ();	// DTD
			nr.Read ();	// root
			nr.Read ();	// &ent;
			Assert.AreEqual (XmlNodeType.EntityReference, nr.NodeType);
			Assert.AreEqual (1, nr.Depth, "depth#1");
			nr.ResolveEntity ();
			// It is still entity reference.
			Assert.AreEqual (XmlNodeType.EntityReference, nr.NodeType);
			nr.Read ();
			Assert.AreEqual (XmlNodeType.Text, nr.NodeType);
			Assert.AreEqual (2, nr.Depth, "depth#2");
			Assert.AreEqual ("entity string", nr.Value);
			nr.Read ();
			Assert.AreEqual (XmlNodeType.EndEntity, nr.NodeType);
			Assert.AreEqual (1, nr.Depth, "depth#3");
			Assert.AreEqual ("", nr.Value);

			nr.Read ();	// &ent2;
			Assert.AreEqual (XmlNodeType.EntityReference, nr.NodeType);
			Assert.AreEqual (1, nr.Depth, "depth#4");
			nr.ResolveEntity ();
			Assert.AreEqual (xml, document.OuterXml);
			// It is still entity reference.
			Assert.AreEqual (XmlNodeType.EntityReference, nr.NodeType);
			// It now became element node.
			nr.Read ();
			Assert.AreEqual (XmlNodeType.Element, nr.NodeType);
			Assert.AreEqual (2, nr.Depth, "depth#5");

			Assert.AreEqual (xml, document.OuterXml);
		}

		[Test]
		[Ignore (".NET 2.0 XmlNodeReader does not allow undeclared entities at all.")]
		public void ResolveEntity2 ()
		{
			document.RemoveAll ();
			string ent1 = "<!ENTITY ent 'entity string'>";
			string ent2 = "<!ENTITY ent2 '<foo/><foo/>'>]>";
			string dtd = "<!DOCTYPE root[<!ELEMENT root (#PCDATA|foo)*>" + ent1 + ent2;
			string xml = dtd + "<root>&ent3;&ent2;</root>";
			XmlTextReader xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
			xtr.Read ();
			document.AppendChild (document.ReadNode (xtr));
			document.AppendChild (document.ReadNode (xtr));
			xtr.Close ();
			Assert.AreEqual (xml, document.OuterXml);
			XmlNodeReader nr = new XmlNodeReader (document);
			nr.Read ();	// DTD
			nr.Read ();	// root
			nr.Read ();	// &ent3;
			Assert.AreEqual (XmlNodeType.EntityReference, nr.NodeType);
			// ent3 does not exist in this dtd.
			nr.ResolveEntity ();
			Assert.AreEqual (XmlNodeType.EntityReference, nr.NodeType);
			nr.Read ();
#if false
			// Hmm... MS.NET returned as it is a Text node.
			Assert.AreEqual (XmlNodeType.Text, nr.NodeType);
			Assert.AreEqual (String.Empty, nr.Value);
			nr.Read ();
			// Really!?
			Assert.AreEqual (XmlNodeType.EndEntity, nr.NodeType);
			Assert.AreEqual (String.Empty, nr.Value);
#endif
		}

		[Test]
		[Ignore (".NET 2.0 XmlNodeReader does not allow undeclared entities at all.")]
		public void ResolveEntityWithoutDTD ()
		{
			document.RemoveAll ();
			string xml = "<root>&ent;&ent2;</root>";
			XmlTextReader xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
			xtr.Read ();
			document.AppendChild (document.ReadNode (xtr));
			xtr.Close ();
			Assert.AreEqual (xml, document.OuterXml);
			XmlNodeReader nr = new XmlNodeReader (document);
			nr.Read ();	// root
			nr.Read ();	// &ent;
			Assert.AreEqual (XmlNodeType.EntityReference, nr.NodeType);
			// ent does not exist in this dtd.
			nr.ResolveEntity ();
		}

		[Test] // bug #76260
		public void FromEmptyNonDocumentElement ()
		{
			document.LoadXml ("<root><child/></root>");
			XmlNodeReader nr = new XmlNodeReader (
				document.DocumentElement.FirstChild);
			nr.Read ();
			Assert.AreEqual (true, nr.IsEmptyElement, "#0");
			Assert.IsTrue (!nr.Read (), "#1");

			document.LoadXml ("<root><child></child></root>");
			nr = new XmlNodeReader (
				document.DocumentElement.FirstChild);
			nr.Read ();
			Assert.IsTrue (nr.Read (), "#2");
			Assert.AreEqual (false, nr.IsEmptyElement, "#2.2");
			Assert.IsTrue (!nr.Read (), "#3");
		}
		
		[Test] // bug #550379
		public void MoveToNextAttributeFromValue ()
		{
			document.LoadXml ("<ul test='xxx'></ul>");
			XmlNodeReader nr = new XmlNodeReader (document);
			nr.Read ();
			//nr.Read (); // why twice?
			Assert.IsTrue (nr.MoveToFirstAttribute (), "#1");
			Assert.IsTrue (nr.ReadAttributeValue (), "#2");
			Assert.IsFalse (nr.MoveToNextAttribute (), "#3");
		}
	}

}