Can now scan most location paths (without predicates).
[mono.git] / mcs / class / System.XML / Test / XmlTextWriterTests.cs
1 //
2 // System.Xml.XmlTextWriterTests
3 //
4 // Author:
5 //   Kral Ferch <kral_ferch@hotmail.com>
6 //
7 // (C) 2002 Kral Ferch
8 //
9
10 using System;
11 using System.IO;
12 using System.Text;
13 using System.Xml;
14
15 using NUnit.Framework;
16
17 namespace Ximian.Mono.Tests
18 {
19         public class XmlTextWriterTests : TestCase
20         {
21                 public XmlTextWriterTests () : base ("Ximian.Mono.Tests.XmlTextWriterTests testsuite") {}
22                 public XmlTextWriterTests (string name) : base (name) {}
23
24                 StringWriter sw;
25                 XmlTextWriter xtw;
26
27                 protected override void SetUp ()
28                 {
29                         sw = new StringWriter ();
30                         xtw = new XmlTextWriter (sw);
31                 }
32
33                 public void TestCDataValid ()
34                 {
35                         xtw.WriteCData ("foo");
36                         AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", sw.GetStringBuilder().ToString());
37                 }
38
39                 public void TestCDataInvalid ()
40                 {
41                         try {
42                                 xtw.WriteCData("foo]]>bar");
43                                 Fail("Should have thrown an ArgumentException.");
44                         } 
45                         catch (ArgumentException) { }
46                 }
47
48                 public void TestCloseOpenElements ()
49                 {
50                         xtw.WriteStartElement("foo");
51                         xtw.WriteStartElement("bar");
52                         xtw.WriteStartElement("baz");
53                         xtw.Close();
54                         AssertEquals ("Close didn't write out end elements properly.", "<foo><bar><baz /></bar></foo>",
55                                 sw.GetStringBuilder().ToString());
56                 }
57
58                 public void TestCloseWriteAfter ()
59                 {
60                         xtw.WriteElementString ("foo", "bar");
61                         xtw.Close ();
62
63                         // WriteEndElement and WriteStartDocument aren't tested here because
64                         // they will always throw different exceptions besides 'The Writer is closed.'
65                         // and there are already tests for those exceptions.
66
67                         try {
68                                 xtw.WriteCData ("foo");
69                                 Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
70                         } 
71                         catch (InvalidOperationException e) {
72                                 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
73                         }
74
75                         try {
76                                 xtw.WriteComment ("foo");
77                                 Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
78                         } 
79                         catch (InvalidOperationException e) {
80                                 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
81                         }
82
83                         try {
84                                 xtw.WriteProcessingInstruction ("foo", "bar");
85                                 Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
86                         } 
87                         catch (InvalidOperationException e) {
88                                 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
89                         }
90
91                         try {
92                                 xtw.WriteStartElement ("foo", "bar", "baz");
93                                 Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
94                         } 
95                         catch (InvalidOperationException e) {
96                                 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
97                         }
98
99                         try {
100                                 xtw.WriteString ("foo");
101                                 Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
102                         } 
103                         catch (InvalidOperationException e) {
104                                 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
105                         }
106                 }
107
108                 public void TestCommentValid ()
109                 {
110                         xtw.WriteComment ("foo");
111                         AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", sw.GetStringBuilder().ToString());
112                 }
113
114                 public void TestCommentInvalid ()
115                 {
116                         try {
117                                 xtw.WriteComment("foo-");
118                                 Fail("Should have thrown an ArgumentException.");
119                         } 
120                         catch (ArgumentException) { }
121
122                         try {
123                                 xtw.WriteComment("foo-->bar");
124                                 Fail("Should have thrown an ArgumentException.");
125                         } 
126                         catch (ArgumentException) { }
127                 }
128
129                 public void TestConstructorsAndBaseStream ()
130                 {
131                         Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (null, this.xtw.BaseStream));
132
133                         MemoryStream ms;
134                         StreamReader sr;
135                         XmlTextWriter xtw;
136
137                         ms = new MemoryStream ();
138                         xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
139                         xtw.WriteStartDocument ();
140                         xtw.Flush ();
141                         ms.Seek (0, SeekOrigin.Begin);
142                         sr = new StreamReader (ms);
143                         AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?>", sr.ReadToEnd ());
144                         Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
145
146                         ms = new MemoryStream ();
147                         xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
148                         xtw.WriteStartDocument (true);
149                         xtw.Flush ();
150                         ms.Seek (0, SeekOrigin.Begin);
151                         sr = new StreamReader (ms);
152                         AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
153
154                         ms = new MemoryStream ();
155                         xtw = new XmlTextWriter (ms, new UTF8Encoding ());
156                         xtw.WriteStartDocument ();
157                         xtw.Flush ();
158                         ms.Seek (0, SeekOrigin.Begin);
159                         sr = new StreamReader (ms);
160                         AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
161
162                         ms = new MemoryStream ();
163                         xtw = new XmlTextWriter (ms, null);
164                         xtw.WriteStartDocument ();
165                         xtw.Flush ();
166                         ms.Seek (0, SeekOrigin.Begin);
167                         sr = new StreamReader (ms);
168                         AssertEquals ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
169
170                         ms = new MemoryStream ();
171                         xtw = new XmlTextWriter (ms, null);
172                         xtw.WriteStartDocument (true);
173                         xtw.Flush ();
174                         ms.Seek (0, SeekOrigin.Begin);
175                         sr = new StreamReader (ms);
176                         AssertEquals ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
177                         Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
178                 }
179
180                 public void TestDocumentStart ()
181                 {
182                         xtw.WriteStartDocument ();
183                         AssertEquals ("XmlDeclaration is incorrect.", "<?xml version=\"1.0\" encoding=\"utf-16\"?>",
184                                 sw.GetStringBuilder ().ToString ());
185
186                         try 
187                         {
188                                 xtw.WriteStartDocument ();
189                                 Fail("Should have thrown an InvalidOperationException.");
190                         } 
191                         catch (InvalidOperationException e) {
192                                 AssertEquals ("Exception message is incorrect.",
193                                         "WriteStartDocument should be the first call.", e.Message);
194                         }
195
196                         xtw = new XmlTextWriter (sw = new StringWriter ());
197                         xtw.WriteStartDocument (true);
198                         AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>",
199                                 sw.GetStringBuilder ().ToString ());
200
201                         xtw = new XmlTextWriter (sw = new StringWriter ());
202                         xtw.WriteStartDocument (false);
203                         AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"no\"?>",
204                                 sw.GetStringBuilder ().ToString ());
205                 }
206
207                 public void TestElementEmpty ()
208                 {
209                         xtw.WriteStartElement ("foo");
210                         xtw.WriteEndElement ();
211                         AssertEquals ("Incorrect output.", "<foo />", sw.GetStringBuilder().ToString());
212                 }
213
214                 public void TestElementWriteElementString ()
215                 {
216                         xtw.WriteElementString ("foo", "bar");
217                         AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", sw.GetStringBuilder().ToString());
218                 }
219
220                 public void TestFormatting ()
221                 {
222                         xtw.Formatting = Formatting.Indented;
223                         xtw.WriteStartDocument ();
224                         xtw.WriteStartElement ("foo");
225                         xtw.WriteElementString ("bar", "");
226                         xtw.Close ();
227                         AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<foo>\r\n  <bar />\r\n</foo>",
228                                 sw.GetStringBuilder ().ToString ());
229                 }
230
231                 public void TestFormattingInvalidXmlForFun ()
232                 {
233                         xtw.Formatting = Formatting.Indented;
234                         xtw.IndentChar = 'x';
235                         xtw.WriteStartDocument ();
236                         xtw.WriteStartElement ("foo");
237                         xtw.WriteStartElement ("bar");
238                         xtw.WriteElementString ("baz", "");
239                         xtw.Close ();
240                         AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<foo>\r\nxx<bar>\r\nxxxx<baz />\r\nxx</bar>\r\n</foo>",
241                                 sw.GetStringBuilder ().ToString ());
242                 }
243
244                 public void TestFormattingFromRemarks ()
245                 {
246                         // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
247                         xtw.Formatting = Formatting.Indented; 
248                         xtw.WriteStartElement ("ol"); 
249                         xtw.WriteStartElement ("li"); 
250                         xtw.WriteString ("The big "); // This means "li" now has a mixed content model. 
251                         xtw.WriteElementString ("b", "E"); 
252                         xtw.WriteElementString ("i", "lephant"); 
253                         xtw.WriteString (" walks slowly."); 
254                         xtw.WriteEndElement (); 
255                         xtw.WriteEndElement ();
256                         AssertEquals ("<ol>\r\n  <li>The big <b>E</b><i>lephant</i> walks slowly.</li>\r\n</ol>",
257                                 sw.GetStringBuilder ().ToString ());
258                 }
259
260
261                 // TODO: Need some tests for attributes with namespaces here...
262                 public void saveTestNamespacesAttributesPassingInNamespaces ()
263                 {
264                 }
265
266                 public void TestNamespacesElementsPassingInNamespaces ()
267                 {
268                         xtw.Namespaces = false;
269
270                         // These shouldn't throw any exceptions since they don't pass in
271                         // a namespace.
272                         xtw.WriteElementString ("foo", "bar");
273                         xtw.WriteStartElement ("baz");
274                         xtw.WriteStartElement ("quux", "");
275                         xtw.WriteStartElement ("quuux", null);
276                         xtw.WriteStartElement (null, "a", null);
277                         xtw.WriteStartElement (null, "b", "");
278                         xtw.WriteStartElement ("", "c", null);
279                         xtw.WriteStartElement ("", "d", "");
280
281                         AssertEquals ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", sw.GetStringBuilder ().ToString ());
282
283                         // These should throw ArgumentException because they pass in a
284                         // namespace when Namespaces = false.
285                         try {
286                                 xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
287                                 Fail ("Expected an ArgumentException.");
288                         } catch (ArgumentException) {}
289
290                         try {
291                                 xtw.WriteStartElement ("foo", "http://netsack.com/");
292                                 Fail ("Expected an ArgumentException.");
293                         } catch (ArgumentException) {}
294
295                         try {
296                                 xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
297                                 Fail ("Expected an ArgumentException.");
298                         } catch (ArgumentException) {}
299
300                         try {
301                                 xtw.WriteStartElement ("foo", "bar", null);
302                                 Fail ("Expected an ArgumentException.");
303                         } catch (ArgumentException) {}
304
305                         try {
306                                 xtw.WriteStartElement ("foo", "bar", "");
307                                 Fail ("Expected an ArgumentException.");
308                         } catch (ArgumentException) {}
309
310                         try {
311                                 xtw.WriteStartElement ("foo", "", "");
312                                 Fail ("Expected an ArgumentException.");
313                         } catch (ArgumentException) {}
314                 }
315
316                 public void TestNamespacesNoNamespaceClearsDefaultNamespace ()
317                 {
318                         xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
319                         xtw.WriteStartElement(String.Empty, "bar", String.Empty);
320                         xtw.WriteElementString("baz", String.Empty, String.Empty);
321                         xtw.WriteEndElement();
322                         xtw.WriteEndElement();
323                         AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
324                                 "<foo xmlns=\"http://netsack.com/\"><bar xmlns=\"\"><baz /></bar></foo>",
325                                 sw.GetStringBuilder().ToString());
326                 }
327
328                 public void TestNamespacesPrefix ()
329                 {
330                         xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
331                         xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
332                         xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
333                         xtw.WriteEndElement ();
334                         xtw.WriteEndElement ();
335                         AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
336                                 "<foo:bar xmlns:foo=\"http://netsack.com/\"><foo:baz><foo:qux /></foo:baz></foo:bar>",
337                                 sw.GetStringBuilder ().ToString ());
338                 }
339
340                 public void TestNamespacesPrefixWithEmptyAndNullNamespace ()
341                 {
342                         try {
343                                 xtw.WriteStartElement ("foo", "bar", "");
344                                 Fail ("Should have thrown an ArgumentException.");
345                         } catch (ArgumentException) {}
346
347                         try 
348                         {
349                                 xtw.WriteStartElement ("foo", "bar", null);
350                                 Fail ("Should have thrown an ArgumentException.");
351                         } 
352                         catch (ArgumentException) {}
353                 }
354
355                 public void TestNamespacesSettingWhenWriteStateNotStart ()
356                 {
357                         xtw.WriteStartElement ("foo");
358                         try 
359                         {
360                                 xtw.Namespaces = false;
361                                 Fail ("Expected an InvalidOperationException.");
362                         } 
363                         catch (InvalidOperationException) {}
364                         AssertEquals (true, xtw.Namespaces);
365                 }
366
367                 public void TestProcessingInstructionValid ()
368                 {
369                         xtw.WriteProcessingInstruction("foo", "bar");
370                         AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", sw.GetStringBuilder().ToString());
371                 }
372
373                 public void TestProcessingInstructionInvalid ()
374                 {
375                         try 
376                         {
377                                 xtw.WriteProcessingInstruction("fo?>o", "bar");
378                                 Fail("Should have thrown an ArgumentException.");
379                         } 
380                         catch (ArgumentException) { }
381
382                         try 
383                         {
384                                 xtw.WriteProcessingInstruction("foo", "ba?>r");
385                                 Fail("Should have thrown an ArgumentException.");
386                         } 
387                         catch (ArgumentException) { }
388
389                         try 
390                         {
391                                 xtw.WriteProcessingInstruction("", "bar");
392                                 Fail("Should have thrown an ArgumentException.");
393                         } 
394                         catch (ArgumentException) { }
395
396                         try 
397                         {
398                                 xtw.WriteProcessingInstruction(null, "bar");
399                                 Fail("Should have thrown an ArgumentException.");
400                         } 
401                         catch (ArgumentException) { }
402                 }
403
404                 public void TestQuoteCharInvalid ()
405                 {
406                         try {
407                                 xtw.QuoteChar = 'x';
408                                 Fail ("Should have thrown an ArgumentException.");
409                         } catch (ArgumentException) {}
410                 }
411
412                 public void TestWriteEndElement ()
413                 {
414                         try 
415                         {
416                                 xtw.WriteEndElement ();
417                                 Fail ("Should have thrown an InvalidOperationException.");
418                         }
419                         catch (InvalidOperationException e) {
420                                 AssertEquals ("Exception message is incorrect.",
421                                         "There was no XML start tag open.", e.Message);
422                         }
423                 }
424
425                 public void TestWriteState ()
426                 {
427                         AssertEquals (WriteState.Start, xtw.WriteState);
428                         xtw.WriteStartDocument ();
429                         AssertEquals (WriteState.Prolog, xtw.WriteState);
430                         xtw.WriteStartElement ("root");
431                         AssertEquals (WriteState.Element, xtw.WriteState);
432                         xtw.WriteElementString ("foo", "bar");
433                         AssertEquals (WriteState.Content, xtw.WriteState);
434                         xtw.Close ();
435                         AssertEquals (WriteState.Closed, xtw.WriteState);
436                 }
437         }
438 }