New test.
[mono.git] / mcs / class / System.XML / Test / System.Xml / XmlWriterSettingsTests.cs
1 //
2 // System.Xml.XmlWriterSettingsTests.cs
3 //
4 // Authors:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C)2004 Novell Inc.
8 //
9
10 #if NET_2_0
11 using System;
12 using System.IO;
13 using System.Text;
14 using System.Xml;
15 using System.Xml.Schema;
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Xml
19 {
20         [TestFixture]
21         public class XmlWriterSettingsTests : Assertion
22         {
23                 [Test]
24                 public void DefaultValue ()
25                 {
26                         XmlWriterSettings s = new XmlWriterSettings ();
27                         DefaultValue (s);
28                         s.Reset ();
29                         DefaultValue (s);
30                 }
31
32                 private void DefaultValue (XmlWriterSettings s)
33                 {
34                         AssertEquals (true, s.CheckCharacters);
35                         AssertEquals (false, s.CloseOutput);
36                         AssertEquals (ConformanceLevel.Document, s.ConformanceLevel);
37                         AssertEquals (Encoding.UTF8, s.Encoding);
38                         AssertEquals (false, s.Indent);
39                         AssertEquals ("  ", s.IndentChars);
40                         AssertEquals (Environment.NewLine, s.NewLineChars);
41                         AssertEquals (false, s.NewLineOnAttributes);
42                         AssertEquals (false, s.OmitXmlDeclaration);
43                 }
44
45                 [Test]
46                 public void EncodingTest ()
47                 {
48                         // For Stream it makes sense
49                         XmlWriterSettings s = new XmlWriterSettings ();
50                         s.Encoding = Encoding.GetEncoding ("shift_jis");
51                         MemoryStream ms = new MemoryStream ();
52                         XmlWriter w = XmlWriter.Create (ms, s);
53                         w.WriteStartElement ("root");
54                         w.WriteEndElement ();
55                         w.Close ();
56                         byte [] data = ms.ToArray ();
57                         Assert (data.Length != 0);
58                         string str = s.Encoding.GetString (data);
59                         AssertEquals ("<?xml version=\"1.0\" encoding=\"shift_jis\"?><root />", str);
60
61                         // For TextWriter it does not make sense
62                         StringWriter sw = new StringWriter ();
63                         w = XmlWriter.Create (sw, s);
64                         w.WriteStartElement ("root");
65                         w.WriteEndElement ();
66                         w.Close ();
67                         AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?><root />", sw.ToString ());
68                 }
69
70                 [Test]
71                 [ExpectedException (typeof (ArgumentException))]
72                 public void CheckCharactersTest ()
73                 {
74                         XmlWriterSettings s = new XmlWriterSettings ();
75                         StringWriter sw = new StringWriter ();
76                         XmlWriter w = XmlWriter.Create (sw, s);
77                         w.WriteStartElement ("root");
78                         w.WriteString ("\0"); // invalid
79                         w.WriteEndElement ();
80                         w.Close ();
81                 }
82
83                 [Test]
84                 public void CheckCharactersFalseTest ()
85                 {
86                         XmlWriterSettings s = new XmlWriterSettings ();
87                         s.CheckCharacters = false;
88                         StringWriter sw = new StringWriter ();
89                         XmlWriter w = XmlWriter.Create (sw, s);
90                         w.WriteStartElement ("root");
91                         w.WriteString ("\0"); // invalid
92                         w.WriteEndElement ();
93                 }
94
95                 [Test]
96                 [ExpectedException (typeof (ObjectDisposedException))]
97                 public void CloseOutputTest ()
98                 {
99                         XmlWriterSettings s = new XmlWriterSettings ();
100                         s.CloseOutput = true;
101                         StringWriter sw = new StringWriter ();
102                         XmlWriter w = XmlWriter.Create (sw, s);
103                         w.WriteStartElement ("root");
104                         w.WriteEndElement ();
105                         w.Close ();
106                         sw.Write ("more"); // not allowed
107                 }
108
109                 [Test]
110                 [Ignore ("Write Test!")]
111                 public void ConformanceLevelTest ()
112                 {
113                         throw new NotImplementedException ();
114                 }
115
116                 [Test]
117                 public void IndentationAndFormatting ()
118                 {
119                         // Test for Indent, IndentChars, NewLineOnAttributes,
120                         // NewLineChars and OmitXmlDeclaration.
121                         string output = "<root\n    attr=\"value\"\n    attr2=\"value\">\n    <child>test</child>\n</root>";
122                         XmlWriterSettings s = new XmlWriterSettings ();
123                         s.OmitXmlDeclaration = true;
124                         s.Indent = true;
125                         s.IndentChars = "    ";
126                         s.NewLineChars = "\n";
127                         s.NewLineOnAttributes = true;
128                         StringWriter sw = new StringWriter ();
129                         XmlWriter w = XmlWriter.Create (sw, s);
130                         w.WriteStartElement ("root");
131                         w.WriteAttributeString ("attr", "value");
132                         w.WriteAttributeString ("attr2", "value");
133                         w.WriteStartElement ("child");
134                         w.WriteString ("test");
135                         w.WriteEndElement ();
136                         w.WriteEndElement ();
137                         w.Close ();
138                         AssertEquals (output, sw.ToString ());
139                 }
140
141                 [Test]
142                 public void SetEncodingNull ()
143                 {
144                         // null is allowed.
145                         new XmlWriterSettings ().Encoding = null;
146                 }
147
148                 [Test]
149                 [ExpectedException (typeof (ArgumentNullException))]
150                 public void NewLineCharsNull ()
151                 {
152                         new XmlWriterSettings ().NewLineChars = null;
153                 }
154
155                 [Test]
156                 public void CreateOmitXmlDeclaration ()
157                 {
158                         StringBuilder sb = new StringBuilder ();
159                         // Even if XmlWriter is allowed to write fragment,
160                         // DataContractSerializer never allows it to write
161                         // content in contentOnly mode.
162                         XmlWriterSettings settings = new XmlWriterSettings ();
163                         settings.OmitXmlDeclaration = true;
164                         //settings.ConformanceLevel = ConformanceLevel.Fragment;
165                         XmlWriter w = XmlWriter.Create (sb, settings);
166                         w.WriteStartElement ("root");
167                         w.WriteEndElement ();
168                         w.Flush ();
169                         AssertEquals ("<root />", sb.ToString ());
170                 }
171         }
172 }
173 #endif