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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
|
//
// System.Xml.XmlTextWriterTests
//
// Authors:
// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
// Martin Willemoes Hansen <mwh@sysrq.dk>
//
// (C) 2003 Atsushi Enomoto
// (C) 2003 Martin Willemoes Hansen
//
//
// This class mainly checks inheritance and behaviors of XmlWriter.
//
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using NUnit.Framework;
using AssertType = NUnit.Framework.Assert;
namespace MonoTests.System.Xml
{
[TestFixture]
public class XmlWriterTests
{
StringWriter writer;
XmlTextWriter xtw;
[SetUp]
public void SetUp ()
{
writer = new StringWriter ();
xtw = new XmlTextWriter (writer);
}
[Test]
public void WriteNodeFullDocument ()
{
string xml = "<?xml version='1.0'?><root />";
XmlTextReader xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
xtw.WriteNode (xtr, false);
Assert.AreEqual (xml, writer.ToString ());
writer.GetStringBuilder ().Length = 0;
// With encoding
xml = "<?xml version='1.0' encoding='iso-2022-jp'?><root />";
xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
xtw.WriteNode (xtr, false);
Assert.AreEqual (xml, writer.ToString ());
xtr.Close ();
}
[Test]
public void WriteNodeXmlDecl ()
{
string xml = "<?xml version='1.0'?><root />";
StringReader sr = new StringReader (xml);
XmlTextReader xtr = new XmlTextReader (sr);
xtr.Read ();
xtw.WriteNode (xtr, false);
Assert.AreEqual ("<?xml version='1.0'?>",
writer.ToString ());
xtr.Close ();
}
[Test]
public void WriteNodeEmptyElement ()
{
string xml = "<root attr='value' attr2='value' />";
StringReader sr = new StringReader (xml);
XmlTextReader xtr = new XmlTextReader (sr);
xtw.WriteNode (xtr, false);
Assert.AreEqual (xml.Replace ("'", "\""),
writer.ToString ());
xtr.Close ();
}
[Test]
public void WriteNodeNonEmptyElement ()
{
string xml = @"<foo><bar></bar></foo>";
xtw.WriteNode (new XmlTextReader (xml, XmlNodeType.Document, null), false);
Assert.AreEqual (xml, writer.ToString ());
}
[Test]
public void WriteNodeSingleContentElement ()
{
string xml = "<root attr='value' attr2='value'><foo /></root>";
StringReader sr = new StringReader (xml);
XmlTextReader xtr = new XmlTextReader (sr);
xtw.WriteNode (xtr, false);
Assert.AreEqual (xml.Replace ("'", "\""),
writer.ToString ());
xtr.Close ();
}
[Test]
public void WriteNodeNone ()
{
XmlTextReader xtr = new XmlTextReader ("", XmlNodeType.Element, null);
xtr.Read ();
xtw.WriteNode (xtr, false); // does not report any errors
xtr.Close ();
}
[Test]
#if NET_2_0
[Category ("NotDotNet")] // enbugged in 2.0
#endif
[ExpectedException (typeof (XmlException))]
public void WriteNodeError ()
{
XmlTextReader xtr = new XmlTextReader ("<root>", XmlNodeType.Document, null);
xtr.Read ();
try {
xtr.Read ();
} catch {
}
XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
xtw.WriteNode (xtr, false);
}
[Test]
public void WriteSurrogateCharEntity ()
{
xtw.WriteSurrogateCharEntity ('\udfff', '\udb00');
Assert.AreEqual ("󐏿", writer.ToString ());
try {
xtw.WriteSurrogateCharEntity ('\ud800', '\udc00');
Assert.Fail ();
} catch {
}
try {
xtw.WriteSurrogateCharEntity ('\udbff', '\ud800');
Assert.Fail ();
} catch {
}
try {
xtw.WriteSurrogateCharEntity ('\ue000', '\ud800');
Assert.Fail ();
} catch {
}
try {
xtw.WriteSurrogateCharEntity ('\udfff', '\udc00');
Assert.Fail ();
} catch {
}
}
// MS.NET's not-overriden XmlWriter.WriteStartElement(name)
// invokes WriteStartElement(null, name, null).
// WriteStartElement(name, ns) invokes (null, name, ns), too.
[Test]
public void StartElement ()
{
StartElementTestWriter xw = new StartElementTestWriter ();
xw.WriteStartDocument ();
xw.WriteStartElement ("test");
Assert.IsNull (xw.NS, "StartElementOverride.NS");
Assert.IsNull (xw.Prefix, "StartElementOverride.Prefix");
xw.NS = String.Empty;
xw.Prefix = String.Empty;
xw.WriteStartElement ("test", "urn:hoge");
Assert.AreEqual ("urn:hoge", xw.NS, "StartElementOverride.NS");
Assert.IsNull (null, xw.Prefix, "StartElementOverride.Prefix");
}
class StartElementTestWriter : DefaultXmlWriter
{
public StartElementTestWriter () : base () {}
public string NS = String.Empty;
public string Prefix = String.Empty;
public override void WriteStartElement (string prefix, string localName, string ns)
{
this.NS = ns;
this.Prefix = prefix;
}
}
[Test]
public void WriteAttributes ()
{
string xml = "<root><test a='b' c='d' /><b /></root>";
XmlTextReader xtr = new XmlTextReader (xml,
XmlNodeType.Document, null);
xtw.QuoteChar = '\'';
xtr.Read ();
xtw.WriteStartElement ("root"); // <root
xtr.Read ();
xtw.WriteStartElement ("test"); // ><test
xtw.WriteAttributes (xtr, false); // a='b' c='d'
Assert.AreEqual (XmlNodeType.Element, xtr.NodeType);
xtw.WriteEndElement (); // />
xtw.WriteStartElement ("b"); // <b
xtw.WriteEndElement (); // />
xtw.WriteEndElement (); // </root>
xtw.Close ();
Assert.AreEqual (xml, writer.ToString ());
}
#if NET_2_0
[Test]
public void Create_File ()
{
string file = Path.GetTempFileName ();
XmlWriter writer = XmlWriter.Create (file);
Assert.IsNotNull (writer.Settings, "#A1");
//Assert.IsTrue (writer.Settings.CloseOutput, "#A2");
writer.Close ();
File.Delete (file);
XmlWriterSettings settings = new XmlWriterSettings ();
settings.CloseOutput = true;
writer = XmlWriter.Create (file, settings);
//Assert.IsFalse (object.ReferenceEquals (settings, writer.Settings), "#B1");
Assert.IsTrue (settings.CloseOutput, "#B2");
writer.Close ();
File.Delete (file);
writer = XmlWriter.Create (file, (XmlWriterSettings) null);
Assert.IsNotNull (writer.Settings, "#C1");
//Assert.IsTrue (writer.Settings.CloseOutput, "#C2");
writer.Close ();
File.Delete (file);
settings = new XmlWriterSettings ();
writer = XmlWriter.Create (file, settings);
//Assert.IsFalse (object.ReferenceEquals (settings, writer.Settings), "#D1");
//Assert.IsTrue (writer.Settings.CloseOutput, "#D2");
writer.Close ();
File.Delete (file);
writer = XmlWriter.Create (file);
Assert.IsNotNull (writer.Settings, "#E1");
//Assert.IsTrue (writer.Settings.CloseOutput, "#E2");
writer.Close ();
File.Delete (file);
}
[Test]
public void Create_Stream ()
{
MemoryStream ms = new MemoryStream ();
XmlWriter writer = XmlWriter.Create (ms);
Assert.IsNotNull (writer.Settings, "#A1");
Assert.IsFalse (writer.Settings.CloseOutput, "#A2");
writer.Close ();
Assert.IsTrue (ms.CanWrite, "#A3");
XmlWriterSettings settings = new XmlWriterSettings ();
settings.CloseOutput = true;
writer = XmlWriter.Create (ms, settings);
//Assert.IsFalse (object.ReferenceEquals (settings, writer.Settings), "#B1");
Assert.IsTrue (settings.CloseOutput, "#B2");
writer.Close ();
Assert.IsFalse (ms.CanWrite, "#B3");
ms = new MemoryStream ();
settings = new XmlWriterSettings ();
writer = XmlWriter.Create (ms, settings);
//Assert.IsFalse (object.ReferenceEquals (settings, writer.Settings), "#C1");
Assert.IsFalse (writer.Settings.CloseOutput, "#C2");
writer.Close ();
Assert.IsTrue (ms.CanWrite, "#C3");
ms = new MemoryStream ();
writer = XmlWriter.Create (ms, (XmlWriterSettings) null);
Assert.IsNotNull (writer.Settings, "#D1");
Assert.IsFalse (writer.Settings.CloseOutput, "#D2");
writer.Close ();
Assert.IsTrue (ms.CanWrite, "#D3");
}
[Test]
public void Create_TextWriter ()
{
MemoryStream ms = new MemoryStream ();
XmlWriter writer = XmlWriter.Create (new StreamWriter (ms));
Assert.IsNotNull (writer.Settings, "#A1");
Assert.IsFalse (writer.Settings.CloseOutput, "#A2");
writer.Close ();
Assert.IsTrue (ms.CanWrite, "#A3");
XmlWriterSettings settings = new XmlWriterSettings ();
settings.CloseOutput = true;
writer = XmlWriter.Create (new StreamWriter (ms), settings);
//Assert.IsFalse (object.ReferenceEquals (settings, writer.Settings), "#B1");
Assert.IsTrue (settings.CloseOutput, "#B2");
writer.Close ();
Assert.IsFalse (ms.CanWrite, "#B3");
ms = new MemoryStream ();
settings = new XmlWriterSettings ();
writer = XmlWriter.Create (new StreamWriter (ms), settings);
//Assert.IsFalse (object.ReferenceEquals (settings, writer.Settings), "#C1");
Assert.IsFalse (writer.Settings.CloseOutput, "#C2");
writer.Close ();
Assert.IsTrue (ms.CanWrite, "#C3");
ms = new MemoryStream ();
writer = XmlWriter.Create (new StreamWriter (ms), (XmlWriterSettings) null);
Assert.IsNotNull (writer.Settings, "#D1");
Assert.IsFalse (writer.Settings.CloseOutput, "#D2");
writer.Close ();
Assert.IsTrue (ms.CanWrite, "#D3");
}
[Test]
public void Create_XmlWriter ()
{
MemoryStream ms = new MemoryStream ();
XmlTextWriter xtw = new XmlTextWriter (ms, Encoding.UTF8);
XmlWriter writer = XmlWriter.Create (xtw);
Assert.IsNotNull (writer.Settings, "#A1");
Assert.IsFalse (writer.Settings.CloseOutput, "#A2");
writer.Close ();
Assert.IsFalse (ms.CanWrite, "#A3");
ms = new MemoryStream ();
xtw = new XmlTextWriter (ms, Encoding.UTF8);
XmlWriterSettings settings = new XmlWriterSettings ();
settings.CloseOutput = true;
writer = XmlWriter.Create (xtw, settings);
//Assert.IsFalse (object.ReferenceEquals (settings, writer.Settings), "#B1");
//Assert.IsFalse (writer.Settings.CloseOutput, "#B2");
writer.Close ();
Assert.IsFalse (ms.CanWrite, "#B3");
ms = new MemoryStream ();
xtw = new XmlTextWriter (ms, Encoding.UTF8);
settings = new XmlWriterSettings ();
writer = XmlWriter.Create (xtw, settings);
//Assert.IsFalse (object.ReferenceEquals (settings, writer.Settings), "#C1");
Assert.IsFalse (writer.Settings.CloseOutput, "#C2");
writer.Close ();
Assert.IsFalse (ms.CanWrite, "#C3");
ms = new MemoryStream ();
xtw = new XmlTextWriter (ms, Encoding.UTF8);
writer = XmlWriter.Create (xtw, (XmlWriterSettings) null);
Assert.IsNotNull (writer.Settings, "#D1");
Assert.IsFalse (writer.Settings.CloseOutput, "#D2");
writer.Close ();
Assert.IsFalse (ms.CanWrite, "#D3");
}
[Test]
public void Create_XmlWriter2 ()
{
MemoryStream ms = new MemoryStream ();
XmlWriterSettings settings = new XmlWriterSettings ();
XmlWriter xw = XmlWriter.Create (ms, settings);
XmlWriter writer = XmlWriter.Create (xw, new XmlWriterSettings ());
Assert.IsNotNull (writer.Settings, "#A1");
Assert.IsFalse (writer.Settings.CloseOutput, "#A2");
writer.Close ();
Assert.IsTrue (ms.CanWrite, "#A3");
ms = new MemoryStream ();
settings = new XmlWriterSettings ();
settings.CloseOutput = true;
settings.OmitXmlDeclaration = true;
xw = XmlWriter.Create (ms, settings);
writer = XmlWriter.Create (xw, new XmlWriterSettings ());
Assert.IsNotNull (writer.Settings, "#B1");
Assert.IsTrue (writer.Settings.CloseOutput, "#B2");
Assert.IsTrue (writer.Settings.OmitXmlDeclaration, "#B3");
writer.Close ();
Assert.IsFalse (ms.CanWrite, "#B3");
}
[Test]
[ExpectedException (typeof (XmlException))]
public void CreateWriter_AttributeNamespacesXmlnsXmlns ()
{
// Unlike XmlTextWriter, null namespace is not ignored.
XmlWriter w = XmlWriter.Create (new StringWriter ());
w.WriteStartElement ("foo");
w.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
}
XmlWriter CreateWriter (TextWriter tw)
{
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
XmlWriter w = XmlWriter.Create (tw, s);
w.WriteStartElement ("root");
return w;
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void WriteValueNull ()
{
XmlWriter w = CreateWriter (TextWriter.Null);
w.WriteValue ((object) null);
}
[Test]
[ExpectedException (typeof (InvalidCastException))] // it throws somewhat funny exception
public void WriteValueNonExistentQName ()
{
XmlWriter w = CreateWriter (TextWriter.Null);
w.WriteValue (new XmlQualifiedName ("foo", "urn:foo"));
}
[Test]
public void WriteValueEmptyQName ()
{
StringWriter sw = new StringWriter ();
XmlWriter w = CreateWriter (sw);
w.WriteValue (XmlQualifiedName.Empty);
w.Close ();
}
[Test]
public void WriteValueQName ()
{
StringWriter sw = new StringWriter ();
XmlWriter w = CreateWriter (sw);
w.WriteAttributeString ("xmlns", "x", "http://www.w3.org/2000/xmlns/", "urn:foo");
w.WriteValue (new XmlQualifiedName ("foo", "urn:foo"));
w.Close ();
Assert.AreEqual ("<root xmlns:x=\"urn:foo\">x:foo</root>", sw.ToString ());
}
[Test]
public void WriteValueTimeSpan ()
{
StringWriter sw = new StringWriter ();
XmlWriter w = CreateWriter (sw);
w.WriteValue (TimeSpan.FromSeconds (5));
w.Close ();
Assert.AreEqual ("<root>PT5S</root>", sw.ToString ());
}
[Test]
public void WriteValueArray ()
{
StringWriter sw = new StringWriter ();
XmlWriter w = CreateWriter (sw);
w.WriteValue (new int [] {1, 2, 3});
w.WriteValue (new int [] {4, 5, 6});
w.Close ();
Assert.AreEqual ("<root>1 2 34 5 6</root>", sw.ToString ());
}
[Test]
[ExpectedException (typeof (InvalidCastException))] // it throws somewhat funny exception
public void WriteValueTextReader ()
{
// it is documented as supported, but actually isn't.
XmlWriter w = CreateWriter (TextWriter.Null);
w.WriteValue (new StringReader ("foobar"));
}
XPathNavigator GetNavigator (string xml)
{
return new XPathDocument (XmlReader.Create (
new StringReader (xml))).CreateNavigator ();
}
string WriteNavigator (XPathNavigator nav, bool defattr)
{
StringWriter sw = new StringWriter ();
XmlWriterSettings settings = new XmlWriterSettings ();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
using (XmlWriter w = XmlWriter.Create (sw, settings)) {
w.WriteNode (nav, defattr);
}
return sw.ToString ();
}
[Test]
public void WriteNodeNavigator1 ()
{
XPathNavigator nav = GetNavigator ("<root>test<!-- comment --></root>");
// at Root
AssertType.AreEqual ("<root>test<!-- comment --></root>", WriteNavigator (nav, false), "#1");
// at document element
nav.MoveToFirstChild ();
AssertType.AreEqual ("<root>test<!-- comment --></root>", WriteNavigator (nav, false), "#2");
// at text
nav.MoveToFirstChild ();
AssertType.AreEqual ("test", WriteNavigator (nav, false), "#3");
// at comment
nav.MoveToNext ();
AssertType.AreEqual ("<!-- comment -->", WriteNavigator (nav, false), "#4");
}
string WriteSubtree (XPathNavigator nav)
{
StringWriter sw = new StringWriter ();
XmlWriterSettings settings = new XmlWriterSettings ();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
using (XmlWriter w = XmlWriter.Create (sw, settings)) {
nav.WriteSubtree(w);
}
return sw.ToString ();
}
[Test]
public void NavigatorWriteSubtree1 ()
{
XPathNavigator nav = GetNavigator ("<root>test<!-- comment --></root>");
// at Root
AssertType.AreEqual ("<root>test<!-- comment --></root>", WriteSubtree (nav), "#1");
// at document element
nav.MoveToFirstChild ();
AssertType.AreEqual ("<root>test<!-- comment --></root>", WriteSubtree (nav), "#2");
// at text
nav.MoveToFirstChild ();
AssertType.AreEqual ("test", WriteSubtree (nav), "#3");
// at comment
nav.MoveToNext ();
AssertType.AreEqual ("<!-- comment -->", WriteSubtree (nav), "#4");
}
[Test]
public void WriteNodeXPathNavigator ()
{
string xml = "<A xmlns='urn:x'><B xmlns='urn:y' /></A>";
XPathNavigator nav = new XPathDocument (new StringReader(xml)).CreateNavigator ();
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
StringWriter sw = new StringWriter ();
XmlWriter w = XmlWriter.Create (sw, s);
w.WriteNode (nav, false);
w.Close ();
AssertType.AreEqual ("<A xmlns=\"urn:x\"><B xmlns=\"urn:y\" /></A>", sw.ToString ());
}
[Test]
public void WriteNodeXPathNavigatorAttribute ()
{
string xml = "<!DOCTYPE root [<!ELEMENT root EMPTY> <!ATTLIST root implicit NMTOKEN 'nam'>]><root attr='val' />";
XPathNavigator nav = new XPathDocument (new StringReader (xml)).CreateNavigator ();
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
StringWriter sw = new StringWriter ();
XmlWriter w = XmlWriter.Create (sw, s);
w.WriteStartElement ("hoge");
nav.MoveToFirstChild ();
nav.MoveToFirstAttribute ();
w.WriteNode (nav, false);
nav.MoveToNextAttribute ();
w.WriteNode (nav, false);
w.Close ();
AssertType.AreEqual ("<hoge />", sw.ToString ());
}
[Test]
public void WriteStringDifferentBehavior ()
{
// Messy implementation difference.
// from XmlTextWriter -> <foo />
// from XmlWriter.XCreate() -> <foo></foo>
var sw = new StringWriter ();
var xw = XmlWriter.Create (sw);
xw.WriteStartElement ("foo");
xw.WriteString ("");
xw.WriteEndElement ();
xw.Close ();
AssertType.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo></foo>".Replace ('\'', '"'), sw.ToString ());
}
// | | |returns the same reader
// source |wrapper|result| |checks state (and err)
// overrides|setting| | | |test name
// ---------+----------------------------
// - | -(Doc)| Doc | |x|0:CreateNOCL
// | Auto | Doc | |x|1:CreateNOCLSettingsCLAuto
// | Doc | Doc | |x|2:CreateNOCLSettingsCLDoc
// | Frag | Doc | |x|3:CreateNOCLSettingsCLFrag
// Auto | -(Doc)| Doc | | |4:CreateCLAuto
// | Auto | Auto |=|x|5:CreateCLAutoSettingsCLAuto
// | Doc | Doc | | |6:CreateCLAutoSettingsCLDoc
// | Frag | Frag | | |7:CreateCLAutoSettingsCLFrag
// Document | -(Doc)| Doc |=|x|8:CreateCLDoc
// | Auto | Doc |=|x|9:CreateCLDocSettingsCLAuto
// | Doc | Doc |=|x|A:CreateCLDocSettingsCLDoc
// | Frag | Frag | | |B:CreateCLDocSettingsCLFrag
// Fragment | -(Doc)| Doc | | |C:CreateCLFrag
// | Auto | Frag |=|x|D:CreateCLFragSettingsCLAuto
// | Doc | Doc | | |E:CreateCLFragSettingsCLDoc
// | Frag | Frag |=|x|F:CreateCLFragSettingsCLFrag
//
// What we can see from above:
// - default ConformanceLevel is Document. (0 4 8 C)
// - If wrapper is Auto, it delegates to source. (1 5 9 D)
// - Auto can happen only if both inputs are Auto (5)
// - If wrapper is Frag, all but default becomes Frag (7 B F)
// - with default it becomes Document (3)
// So a default value is likely stronger(!)
// - Basically, when there is no difference between the wrapper
// and the source settings, it does not create wrapper.
[Test]
public void CreateCLAuto ()
{
ConformanceLevelAuto cl = new ConformanceLevelAuto ();
XmlWriter xw = XmlWriter.Create (cl);
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreNotEqual (xw, cl, "#2");
WriteState state = xw.WriteState;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateCLDoc ()
{
ConformanceLevelDocument cl = new ConformanceLevelDocument ();
XmlWriter xw = XmlWriter.Create (cl);
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreEqual (xw, cl, "#2"); // equal
WriteState state = xw.WriteState;
}
[Test]
public void CreateCLFrag ()
{
ConformanceLevelFragment cl = new ConformanceLevelFragment ();
XmlWriter xw = XmlWriter.Create (cl);
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreNotEqual (xw, cl, "#2");
WriteState state = xw.WriteState;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateNOCL ()
{
InvalidWriteState cl = new InvalidWriteState ();
Assert.IsNull (cl.Settings, "#0");
XmlWriter xw = XmlWriter.Create (cl);
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreNotEqual (xw, cl, "#2");
WriteState state = xw.WriteState;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateNOCLSettingsCLAuto ()
{
InvalidWriteState cl = new InvalidWriteState ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Auto });
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreNotEqual (xw, cl, "#2");
WriteState state = xw.WriteState;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateNOCLSettingsCLDoc ()
{
InvalidWriteState cl = new InvalidWriteState ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Document });
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreNotEqual (xw, cl, "#2");
WriteState state = xw.WriteState;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateNOCLSettingsCLFrag ()
{
InvalidWriteState cl = new InvalidWriteState ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Fragment });
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreNotEqual (xw, cl, "#2");
WriteState state = xw.WriteState;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateCLAutoSettingsCLAuto ()
{
ConformanceLevelAuto cl = new ConformanceLevelAuto ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Auto });
Assert.AreEqual (ConformanceLevel.Auto, xw.Settings.ConformanceLevel, "#1");
Assert.AreEqual (xw, cl, "#2"); // equal
WriteState state = xw.WriteState;
}
[Test]
public void CreateCLAutoSettingsCLDoc ()
{
ConformanceLevelAuto cl = new ConformanceLevelAuto ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Document });
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreNotEqual (xw, cl, "#2");
WriteState state = xw.WriteState;
}
[Test]
public void CreateCLAutoSettingsCLFrag ()
{
ConformanceLevelAuto cl = new ConformanceLevelAuto ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Fragment });
Assert.AreEqual (ConformanceLevel.Fragment, xw.Settings.ConformanceLevel, "#1");
Assert.AreNotEqual (xw, cl, "#2");
WriteState state = xw.WriteState;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateCLDocSettingsCLAuto ()
{
ConformanceLevelDocument cl = new ConformanceLevelDocument ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Auto });
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreEqual (xw, cl, "#2"); // equal
WriteState state = xw.WriteState;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateCLDocSettingsCLDoc ()
{
ConformanceLevelDocument cl = new ConformanceLevelDocument ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Document });
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreEqual (xw, cl, "#2"); // equal
WriteState state = xw.WriteState;
}
[Test]
public void CreateCLDocSettingsCLFrag ()
{
ConformanceLevelDocument cl = new ConformanceLevelDocument ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Fragment });
Assert.AreEqual (ConformanceLevel.Fragment, xw.Settings.ConformanceLevel, "#1");
Assert.AreNotEqual (xw, cl, "#2");
WriteState state = xw.WriteState;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateCLFragSettingsCLAuto ()
{
ConformanceLevelFragment cl = new ConformanceLevelFragment ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Auto });
Assert.AreEqual (ConformanceLevel.Fragment, xw.Settings.ConformanceLevel, "#1");
Assert.AreEqual (xw, cl, "#2"); // equal
WriteState state = xw.WriteState;
}
[Test]
public void CreateCLFragSettingsCLDoc ()
{
ConformanceLevelFragment cl = new ConformanceLevelFragment ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Document });
Assert.AreEqual (ConformanceLevel.Document, xw.Settings.ConformanceLevel, "#1");
Assert.AreNotEqual (xw, cl, "#2");
WriteState state = xw.WriteState;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CreateCLFragSettingsCLFrag ()
{
ConformanceLevelFragment cl = new ConformanceLevelFragment ();
XmlWriter xw = XmlWriter.Create (cl, new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Fragment });
Assert.AreEqual (ConformanceLevel.Fragment, xw.Settings.ConformanceLevel, "#1");
Assert.AreEqual (xw, cl, "#2"); // equal
WriteState state = xw.WriteState;
}
#endif
}
internal class DefaultXmlWriter : XmlWriter
{
public DefaultXmlWriter () : base ()
{
}
public override void Close ()
{
}
public override void Flush ()
{
}
public override string LookupPrefix (string ns)
{
return null;
}
public override void WriteBase64 (byte [] buffer, int index, int count)
{
}
public override void WriteBinHex (byte [] buffer, int index, int count)
{
}
public override void WriteCData (string text)
{
}
public override void WriteCharEntity (char ch)
{
}
public override void WriteChars (char [] buffer, int index, int count)
{
}
public override void WriteComment (string text)
{
}
public override void WriteDocType (string name, string pubid, string sysid, string subset)
{
}
public override void WriteEndAttribute ()
{
}
public override void WriteEndDocument ()
{
}
public override void WriteEndElement ()
{
}
public override void WriteEntityRef (string name)
{
}
public override void WriteFullEndElement ()
{
}
public override void WriteName (string name)
{
}
public override void WriteNmToken (string name)
{
}
public override void WriteNode (XmlReader reader, bool defattr)
{
}
public override void WriteProcessingInstruction (string name, string text)
{
}
public override void WriteQualifiedName (string localName, string ns)
{
}
public override void WriteRaw (string data)
{
}
public override void WriteRaw (char [] buffer, int index, int count)
{
}
public override void WriteStartAttribute (string prefix, string localName, string ns)
{
}
public override void WriteStartDocument (bool standalone)
{
}
public override void WriteStartDocument ()
{
}
public override void WriteStartElement (string prefix, string localName, string ns)
{
}
public override void WriteString (string text)
{
}
public override void WriteSurrogateCharEntity (char lowChar, char highChar)
{
}
public override void WriteWhitespace (string ws)
{
}
public override WriteState WriteState {
get {
return WriteState.Start;
}
}
public override string XmlLang {
get {
return null;
}
}
public override XmlSpace XmlSpace {
get {
return XmlSpace.None;
}
}
}
class InvalidWriteState : XmlWriter {
public override void Close () { }
public override void Flush () { }
public override string LookupPrefix (string ns) { return null; }
public override void WriteBase64 (byte [] buffer, int index, int count) { }
public override void WriteCData (string text) { }
public override void WriteCharEntity (char ch) { }
public override void WriteChars (char [] buffer, int index, int count) { }
public override void WriteComment (string text) { }
public override void WriteDocType (string name, string pubid, string sysid, string subset) { }
public override void WriteEndAttribute () { }
public override void WriteEndDocument () { }
public override void WriteEndElement () { }
public override void WriteEntityRef (string name) { }
public override void WriteFullEndElement () { }
public override void WriteProcessingInstruction (string name, string text) {}
public override void WriteRaw (string data) {}
public override void WriteRaw (char [] buffer, int index, int count) {}
public override void WriteStartAttribute (string prefix, string localName, string ns) {}
public override void WriteStartDocument (bool standalone) {}
public override void WriteStartDocument () {}
public override void WriteStartElement (string prefix, string localName, string ns) {}
public override void WriteString (string text) {}
public override void WriteSurrogateCharEntity (char lowChar, char highChar) {}
public override void WriteWhitespace (string ws) {}
public override WriteState WriteState {
get { throw new InvalidOperationException (); }
}
}
class ConformanceLevelAuto : InvalidWriteState {
public override XmlWriterSettings Settings {
get {
return new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Auto };
}
}
}
class ConformanceLevelDocument : InvalidWriteState {
public override XmlWriterSettings Settings {
get {
return new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Document };
}
}
}
class ConformanceLevelFragment : InvalidWriteState {
public override XmlWriterSettings Settings {
get {
return new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Fragment };
}
}
}
}
|