BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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
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                         Assert.AreEqual (true, s.CheckCharacters);
35                         Assert.AreEqual (false, s.CloseOutput);
36                         Assert.AreEqual (ConformanceLevel.Document, s.ConformanceLevel);
37                         Assert.AreEqual (Encoding.UTF8, s.Encoding);
38                         Assert.AreEqual (false, s.Indent);
39                         Assert.AreEqual ("  ", s.IndentChars);
40                         Assert.AreEqual (Environment.NewLine, s.NewLineChars);
41                         Assert.AreEqual (false, s.NewLineOnAttributes);
42                         Assert.AreEqual (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.IsTrue (data.Length != 0);
58                         string str = s.Encoding.GetString (data);
59                         Assert.AreEqual ("<?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                         Assert.AreEqual ("<?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                 [ExpectedException (typeof (InvalidOperationException))]
111                 public void ConformanceLevelFragmentAndWriteStartDocument ()
112                 {
113                         XmlWriterSettings s = new XmlWriterSettings ();
114                         s.ConformanceLevel = ConformanceLevel.Fragment;
115                         s.OmitXmlDeclaration = true;
116                         XmlWriter w = XmlWriter.Create (Console.Out, s);
117                         w.WriteStartDocument ();
118                 }
119
120                 [Test]
121                 public void ConformanceLevelAuto ()
122                 {
123                         XmlWriterSettings s = new XmlWriterSettings ();
124                         s.ConformanceLevel = ConformanceLevel.Auto;
125                         StringWriter sw = new StringWriter ();
126                         XmlWriter w = XmlWriter.Create (sw, s);
127                         w.WriteElementString ("foo", "");
128                         w.Close ();
129                         Assert.AreEqual ("<foo />", sw.ToString ());
130                 }
131
132                 [Test]
133                 public void ConformanceLevelAuto_WriteStartDocument ()
134                 {
135                         XmlWriterSettings s = new XmlWriterSettings ();
136                         s.ConformanceLevel = ConformanceLevel.Auto;
137                         StringWriter sw = new StringWriter ();
138                         XmlWriter w = XmlWriter.Create (sw, s);
139                         w.WriteStartDocument ();
140                         w.WriteElementString ("foo", "");
141                         w.Close ();
142                         Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?><foo />", sw.ToString ());
143                 }
144
145                 [Test]
146                 public void ConformanceLevelAuto_OmitXmlDecl_WriteStartDocument ()
147                 {
148                         XmlWriterSettings s = new XmlWriterSettings ();
149                         s.ConformanceLevel = ConformanceLevel.Auto;
150                         s.OmitXmlDeclaration = true;
151                         StringWriter sw = new StringWriter ();
152                         XmlWriter w = XmlWriter.Create (sw, s);
153                         w.WriteStartDocument ();
154                         w.WriteElementString ("foo", "");
155                         w.Close ();
156                         Assert.AreEqual ("<foo />", sw.ToString ());
157                 }
158
159                 [Test]
160                 public void ConformanceLevelDocument_OmitXmlDeclDeclaration ()
161                 {
162                         XmlWriterSettings s = new XmlWriterSettings ();
163                         s.ConformanceLevel = ConformanceLevel.Document;
164                         // LAMESPEC:
165                         // On MSDN, XmlWriterSettings.OmitXmlDeclaration is documented as:
166                         // "The XML declaration is always written if
167                         //  ConformanceLevel is set to Document, even 
168                         //  if OmitXmlDeclaration is set to true. "
169                         // but it is incorrect. It does consider 
170                         // OmitXmlDeclaration property.
171                         s.OmitXmlDeclaration = true;
172                         StringWriter sw = new StringWriter ();
173                         XmlWriter w = XmlWriter.Create (sw, s);
174                         w.WriteElementString ("foo", "");
175                         w.Close ();
176                         Assert.AreEqual ("<foo />", sw.ToString ());
177                 }
178
179                 [Test]
180                 public void IndentationAndFormatting ()
181                 {
182                         // Test for Indent, IndentChars, NewLineOnAttributes,
183                         // NewLineChars and OmitXmlDeclaration.
184                         string output = "<root\n    attr=\"value\"\n    attr2=\"value\">\n    <child>test</child>\n</root>";
185                         XmlWriterSettings s = new XmlWriterSettings ();
186                         s.OmitXmlDeclaration = true;
187                         s.Indent = true;
188                         s.IndentChars = "    ";
189                         s.NewLineChars = "\n";
190                         s.NewLineOnAttributes = true;
191                         StringWriter sw = new StringWriter ();
192                         XmlWriter w = XmlWriter.Create (sw, s);
193                         w.WriteStartElement ("root");
194                         w.WriteAttributeString ("attr", "value");
195                         w.WriteAttributeString ("attr2", "value");
196                         w.WriteStartElement ("child");
197                         w.WriteString ("test");
198                         w.WriteEndElement ();
199                         w.WriteEndElement ();
200                         w.Close ();
201                         Assert.AreEqual (output, sw.ToString ());
202                 }
203
204                 [Test]
205                 public void SetEncodingNull ()
206                 {
207                         // null is allowed.
208                         new XmlWriterSettings ().Encoding = null;
209                 }
210
211                 [Test]
212                 [ExpectedException (typeof (ArgumentNullException))]
213                 public void NewLineCharsNull ()
214                 {
215                         new XmlWriterSettings ().NewLineChars = null;
216                 }
217
218                 [Test]
219                 public void CreateOmitXmlDeclaration ()
220                 {
221                         StringBuilder sb = new StringBuilder ();
222                         // Even if XmlWriter is allowed to write fragment,
223                         // DataContractSerializer never allows it to write
224                         // content in contentOnly mode.
225                         XmlWriterSettings settings = new XmlWriterSettings ();
226                         settings.OmitXmlDeclaration = true;
227                         //settings.ConformanceLevel = ConformanceLevel.Fragment;
228                         XmlWriter w = XmlWriter.Create (sb, settings);
229                         w.WriteStartElement ("root");
230                         w.WriteEndElement ();
231                         w.Flush ();
232                         Assert.AreEqual ("<root />", sb.ToString ());
233                 }
234
235                 [Test]
236                 public void NewLineOnAttributesMixedContent ()
237                 {
238                         StringWriter sw = new StringWriter ();
239                         XmlWriterSettings s = new XmlWriterSettings ();
240                         s.NewLineOnAttributes = true;
241                         s.OmitXmlDeclaration = true;
242                         XmlWriter w = XmlWriter.Create (sw, s);
243                         w.WriteStartElement ("root");
244                         w.WriteAttributeString ("a", "v");
245                         w.WriteAttributeString ("b", "x");
246                         w.WriteString ("some string");
247                         w.WriteEndElement ();
248                         w.Close ();
249                         Assert.AreEqual ("<root a=\"v\" b=\"x\">some string</root>", sw.ToString (), "#1");
250
251                         // mixed content: bug #81770
252                         string expected = "<root>some string<mixed\n    a=\"v\"\n    b=\"x\">some string</mixed></root>";
253                         s.Indent = true;
254                         sw = new StringWriter ();
255                         w = XmlWriter.Create (sw, s);
256                         w.WriteStartElement ("root");
257                         w.WriteString ("some string");
258                         w.WriteStartElement ("mixed");
259                         w.WriteAttributeString ("a", "v");
260                         w.WriteAttributeString ("b", "x");
261                         w.WriteString ("some string");
262                         w.WriteEndElement ();
263                         w.WriteEndElement ();
264                         w.Close ();
265                         Assert.AreEqual (expected, sw.ToString ().Replace ("\r\n", "\n"), "#2");
266                 }
267
268                 [Test]
269                 public void OmitXmlDeclarationAndNewLine ()
270                 {
271                         XmlDocument doc = new XmlDocument ();
272                         doc.LoadXml ("<root/>");
273                         XmlWriterSettings settings = new XmlWriterSettings ();
274                         settings.OmitXmlDeclaration = true;
275                         settings.NewLineChars = "\r\n";
276                         settings.NewLineHandling = NewLineHandling.Replace;
277                         settings.Encoding = Encoding.UTF8;
278                         settings.Indent = true;
279
280                         StringWriter sw = new StringWriter ();
281                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
282                                 doc.Save (xw);
283                         }
284                         // no heading newline.
285                         Assert.AreEqual ("<root />", sw.ToString ());
286                 }
287                 
288                 [Test] // surprisingly niche behavior yet caused bug #3231.
289                 public void IndentAndTopLevelWhitespaces ()
290                 {
291                         var sw = new StringWriter ();
292                         var xw = XmlWriter.Create (sw, new XmlWriterSettings () { Indent = true });
293                         xw.WriteProcessingInstruction ("xml", "version='1.0'");
294                         xw.WriteWhitespace ("\n");
295                         xw.WriteComment ("AAA");
296                         xw.WriteWhitespace ("\n");
297                         xw.WriteWhitespace ("\n");
298                         xw.WriteStartElement ("root");
299                         xw.Close ();
300                         string xml = @"<?xml version='1.0'?>
301 <!--AAA-->
302
303 <root />";
304                         Assert.AreEqual (xml, sw.ToString ().Replace ("\r\n", "\n"), "#1");
305                 }
306         }
307 }
308 #endif