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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
//
// System.Xml.XmlReaderSettingsTests.cs
//
// Authors:
// Atsushi Enomoto <atsushi@ximian.com>
//
// (C)2004 Novell Inc.
//
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using NUnit.Framework;
using ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags;
using AssertType = NUnit.Framework.Assert;
namespace MonoTests.System.Xml
{
[TestFixture]
public class XmlReaderSettingsTests
{
public Stream CreateStream (string xml)
{
return new MemoryStream (Encoding.UTF8.GetBytes (xml));
}
[Test]
public void DefaultValue ()
{
XmlReaderSettings s = new XmlReaderSettings ();
Assert.AreEqual (true, s.CheckCharacters, "CheckCharacters");
Assert.AreEqual (ConformanceLevel.Document, s.ConformanceLevel, "ConformanceLevel");
Assert.AreEqual (ValidationType.None, s.ValidationType, "ValidationType");
Assert.AreEqual (false, s.IgnoreComments, "IgnoreComments");
Assert.IsTrue (0 == (s.ValidationFlags &
ValidationFlags.ProcessInlineSchema), "ProcessInlineSchema");
Assert.AreEqual (false, s.IgnoreProcessingInstructions, "IgnorePI");
Assert.IsTrue (0 == (s.ValidationFlags &
ValidationFlags.ProcessSchemaLocation), "ProcessSchemaLocation");
Assert.IsTrue (0 == (s.ValidationFlags &
ValidationFlags.ReportValidationWarnings), "ReportValidationWarnings");
Assert.IsTrue (0 != (s.ValidationFlags &
ValidationFlags.ProcessIdentityConstraints), "ProcessIdentityConstraints");
// No one should use this flag BTW if someone wants
// code to be conformant to W3C XML Schema standard.
Assert.IsTrue (0 != (s.ValidationFlags &
ValidationFlags.AllowXmlAttributes), "AllowXmlAttributes");
Assert.AreEqual (false, s.IgnoreWhitespace, "IgnoreWhitespace");
Assert.AreEqual (0, s.LineNumberOffset, "LineNumberOffset");
Assert.AreEqual (0, s.LinePositionOffset, "LinePositionOffset");
Assert.IsNull (s.NameTable, "NameTable");
Assert.AreEqual (0, s.Schemas.Count, "Schemas.Count");
}
[Test]
public void SetSchemas ()
{
XmlReaderSettings s = new XmlReaderSettings ();
s.Schemas = new XmlSchemaSet ();
}
[Test]
public void SetSchemasNull ()
{
XmlReaderSettings s = new XmlReaderSettings ();
s.Schemas = null;
}
[Test]
public void CloseInput ()
{
StringReader sr = new StringReader ("<root/><root/>");
XmlReader xtr = XmlReader.Create (sr); // default false
xtr.Read ();
xtr.Close ();
// It should without error, unlike usual XmlTextReader.
sr.ReadLine ();
}
[Test]
public void CreateAndNormalization ()
{
StringReader sr = new StringReader (
"<root attr=' value '>test\rstring</root>");
XmlReaderSettings settings = new XmlReaderSettings ();
settings.CheckCharacters = false;
XmlReader xtr = XmlReader.Create (
sr, settings);
xtr.Read ();
xtr.MoveToFirstAttribute ();
Assert.AreEqual (" value ", xtr.Value);
xtr.Read ();
// Text string is normalized
Assert.AreEqual ("test\nstring", xtr.Value);
}
[Test]
public void CheckCharactersAndNormalization ()
{
// It should *not* raise an error (even Normalization
// is set by default).
StringReader sr = new StringReader (
"<root attr='�'>�</root>");
XmlReaderSettings settings = new XmlReaderSettings ();
settings.CheckCharacters = false;
XmlReader xtr = XmlReader.Create (
sr, settings);
// After creation, changes on source XmlReaderSettings
// does not matter.
settings.CheckCharacters = false;
xtr.Read ();
xtr.MoveToFirstAttribute ();
Assert.AreEqual ("\0", xtr.Value);
xtr.Read ();
Assert.AreEqual ("\0", xtr.Value);
}
// Hmm, does it really make sense? :-/
[Test]
public void CheckCharactersForNonTextReader ()
{
// It should *not* raise an error (even Normalization
// is set by default).
StringReader sr = new StringReader (
"<root attr='�'>�</root>");
XmlReaderSettings settings = new XmlReaderSettings ();
settings.CheckCharacters = false;
XmlReader xr = XmlReader.Create (
sr, settings);
// Enable character checking for XmlNodeReader.
settings.CheckCharacters = true;
XmlDocument doc = new XmlDocument ();
doc.Load (xr);
xr = XmlReader.Create (new XmlNodeReader (doc), settings);
// But it won't work against XmlNodeReader.
xr.Read ();
xr.MoveToFirstAttribute ();
Assert.AreEqual ("\0", xr.Value);
xr.Read ();
Assert.AreEqual ("\0", xr.Value);
}
[Test]
public void CreateAndSettings ()
{
Assert.IsNotNull (XmlReader.Create (CreateStream ("<xml/>")).Settings);
Assert.IsNotNull (XmlReader.Create ("Test/XmlFiles/simple.xml").Settings);
}
[Test]
public void CreateAndNameTable ()
{
// By default NameTable is null, but some of
// XmlReader.Create() should not result in null
// reference exceptions.
XmlReaderSettings s = new XmlReaderSettings ();
XmlReader.Create (new StringReader ("<root/>"), s, String.Empty)
.Read ();
XmlReader.Create (new StringReader ("<root/>"), s, (XmlParserContext) null)
.Read ();
XmlReader.Create (CreateStream ("<root/>"), s, String.Empty)
.Read ();
XmlReader.Create (CreateStream ("<root/>"), s, (XmlParserContext) null)
.Read ();
}
#region ConformanceLevel
[Test]
public void InferConformanceLevel ()
{
XmlReader xr = XmlReader.Create (new StringReader ("<foo/><bar/>"));
AssertType.AreEqual (ConformanceLevel.Document, xr.Settings.ConformanceLevel);
}
[Test]
public void InferWrappedReaderConformance ()
{
// Actually this test is weird, since XmlTextReader
// instance here does not have XmlReaderSettings.
XmlReaderSettings settings = new XmlReaderSettings ();
settings.ConformanceLevel = ConformanceLevel.Auto;
XmlReader xr = XmlReader.Create (
XmlReader.Create (new StringReader ("<foo/><bar/>")),
settings);
AssertType.AreEqual (ConformanceLevel.Document, xr.Settings.ConformanceLevel);
}
[Test]
[ExpectedException (typeof (XmlException))]
public void CreateConformanceDocument ()
{
XmlReaderSettings s = new XmlReaderSettings ();
s.ConformanceLevel = ConformanceLevel.Document;
XmlReader xr = XmlReader.Create (new StringReader (
"<foo/><bar/>"), s);
while (!xr.EOF)
xr.Read ();
}
[Test]
public void CreateConformanceFragment ()
{
XmlReaderSettings settings = new XmlReaderSettings ();
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader xr = XmlReader.Create (new StringReader (
"<foo/><bar/>"), settings);
while (!xr.EOF)
xr.Read ();
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateConformanceChangeToDocument ()
{
// Actually this test is weird, since XmlTextReader
// instance here does not have XmlReaderSettings.
XmlReaderSettings settings = new XmlReaderSettings ();
settings.ConformanceLevel = ConformanceLevel.Document;
XmlReader xr = XmlReader.Create (
new XmlTextReader ("<foo/><bar/>", XmlNodeType.Element, null),
settings);
while (!xr.EOF)
xr.Read ();
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateConformanceChangeToFragment ()
{
// Actually this test is weird, since XmlTextReader
// instance here does not have XmlReaderSettings.
XmlReaderSettings settings = new XmlReaderSettings ();
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader xr = XmlReader.Create (
new XmlTextReader ("<foo/>", XmlNodeType.Document, null),
settings);
while (!xr.EOF)
xr.Read ();
}
[Test]
public void CreateConformanceLevelExplicitAuto ()
{
// Even if we specify ConformanceLevel.Auto explicitly,
// XmlTextReader's ConformanceLevel becomes .Document.
XmlReaderSettings settings = new XmlReaderSettings ();
settings.ConformanceLevel = ConformanceLevel.Auto;
XmlReader xr = XmlReader.Create (
new XmlTextReader ("<foo/>", XmlNodeType.Document, null),
settings);
AssertType.AreEqual (ConformanceLevel.Document, xr.Settings.ConformanceLevel);
}
[Test]
public void CreateKeepConformance ()
{
XmlReaderSettings settings;
XmlReader xr;
// Fragment -> Fragment
settings = new XmlReaderSettings ();
settings.ConformanceLevel = ConformanceLevel.Fragment;
xr = XmlReader.Create (
XmlReader.Create (new StringReader ("<foo/>"), settings),
settings);
while (!xr.EOF)
xr.Read ();
// Document -> Document
settings.ConformanceLevel = ConformanceLevel.Document;
xr = XmlReader.Create (
XmlReader.Create (new StringReader ("<foo/>"), settings),
settings);
while (!xr.EOF)
xr.Read ();
}
#endregion
[Test]
public void CreateClonesSettings ()
{
XmlReaderSettings settings = new XmlReaderSettings ();
XmlReader xr = XmlReader.Create (new StringReader ("<doc/>"), settings);
AssertType.IsFalse (Object.ReferenceEquals (settings, xr.Settings));
}
[Test]
public void CreateValidatorFromNonIXmlNamespaceResolver ()
{
XmlReaderSettings settings = new XmlReaderSettings ();
settings.Schemas.Add (null, "Test/XmlFiles/xsd/xml.xsd");
settings.ValidationType = ValidationType.Schema;
XmlReader xr = XmlReader.Create (new StringReader ("<root/>"));
XmlReader dr = new Commons.Xml.XmlDefaultReader (xr);
// XmlDefaultReader does not implement IXmlNamespaceResolver
// but don't reject because of that fact.
XmlReader r = XmlReader.Create (dr, settings);
}
[Test]
public void NullResolver ()
{
XmlReaderSettings settings = new XmlReaderSettings ();
settings.XmlResolver = null;
using (XmlReader xr = XmlReader.Create ("Test/XmlFiles/simple.xml", settings)) {
while (!xr.EOF)
xr.Read ();
}
}
class ThrowExceptionResolver : XmlResolver
{
public override ICredentials Credentials {
set { }
}
public override object GetEntity (Uri uri, string type, Type expected)
{
throw new ApplicationException ("error");
}
}
[Test]
[ExpectedException (typeof (ApplicationException))]
public void CustomResolverUsedForXmlStream ()
{
XmlReaderSettings settings = new XmlReaderSettings ();
settings.XmlResolver = new ThrowExceptionResolver ();
using (XmlReader xr = XmlReader.Create ("Test/XmlFiles/simple.xml", settings)) {
while (!xr.EOF)
xr.Read ();
}
}
[Test]
[ExpectedException (typeof (ApplicationException))]
public void ValidationEventHandler ()
{
XmlReaderSettings settings = new XmlReaderSettings ();
settings.Schemas.Add (new XmlSchema ());
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += delegate (object o, ValidationEventArgs e) {
throw new ApplicationException ();
};
XmlReader r = XmlReader.Create (
new StringReader ("<root/>"), settings);
while (!r.EOF)
r.Read ();
}
[Test]
[ExpectedException (typeof (XmlSchemaValidationException))]
// make sure that Create(string,XmlReaderSettings) returns
// validating XmlReader.
public void CreateFromUrlWithValidation ()
{
XmlReaderSettings settings = new XmlReaderSettings();
XmlSchema xs = new XmlSchema ();
settings.Schemas.Add (xs);
settings.ValidationType = ValidationType.Schema;
using (XmlReader r = XmlReader.Create ("Test/XmlFiles/simple.xml", settings)) {
r.Read ();
}
}
[Test]
public void ResolveEntities () // bug #81000
{
XmlReaderSettings s = new XmlReaderSettings ();
s.ProhibitDtd = false;
s.XmlResolver = new XmlResolver81000 ();
string xml = "<!DOCTYPE root SYSTEM \"foo.dtd\"><root>α</root>";
XmlReader r = XmlReader.Create (new StringReader (xml), s);
r.Read ();
r.Read ();
r.Read ();
// not EntityReference but Text
Assert.AreEqual (XmlNodeType.Text, r.NodeType, "#1");
r.Read ();
Assert.AreEqual (XmlNodeType.EndElement, r.NodeType, "#2");
}
public class XmlResolver81000 : XmlResolver
{
public override ICredentials Credentials { set {} }
public override object GetEntity (Uri uri, string role, Type type)
{
return new MemoryStream (Encoding.UTF8.GetBytes ("<!ENTITY alpha \"bravo\">"));
}
}
[Test]
public void IgnoreComments () // Bug #82062.
{
string xml = "<root><!-- ignore --></root>";
XmlReaderSettings s = new XmlReaderSettings ();
s.IgnoreComments = true;
XmlReader r = XmlReader.Create (new StringReader (xml), s);
r.Read ();
r.Read ();
Assert.AreEqual (String.Empty, r.Value); // should not be at the comment node.
}
[Test]
public void CreateSetsBaseUri () // bug #392385
{
XmlReader r = XmlReader.Create (new StringReader ("<x/>"), new XmlReaderSettings (), "urn:foo");
Assert.AreEqual ("urn:foo", r.BaseURI);
}
[Test]
[ExpectedException (typeof (XmlException))]
public void ReadonlyAsync ()
{
var s = new XmlReaderSettings ();
var r = XmlReader.Create (new StringReader ("<root/>"), s);
r.Settings.Async = true;
}
[Test]
public void AsyncPropagation ()
{
var s = new XmlReaderSettings ();
s.Async = true;
var r = XmlReader.Create (new StringReader ("<root/>"), s);
var c = s.Clone ();
Assert.IsTrue (c.Async);
c.Reset ();
Assert.IsFalse (c.Async);
var r2 = XmlReader.Create (r, c);
Assert.IsTrue (r2.Settings.Async);
}
}
}
|