1cd2aa83bfb20e2f4cd241baa6276cfb8a12eb51
[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.Xml;
13
14 using NUnit.Framework;
15
16 namespace Ximian.Mono.Tests
17 {
18         public class XmlTextWriterTests : TestCase
19         {
20                 public XmlTextWriterTests () : base ("Ximian.Mono.Tests.XmlTextWriterTests testsuite") {}
21                 public XmlTextWriterTests (string name) : base (name) {}
22
23                 StringWriter sw;
24                 XmlTextWriter xtw;
25
26                 protected override void SetUp ()
27                 {
28                         sw = new StringWriter ();
29                         xtw = new XmlTextWriter (sw);
30                 }
31
32                 public void TestCDataValid ()
33                 {
34                         xtw.WriteCData ("foo");
35                         AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", sw.GetStringBuilder().ToString());
36                 }
37
38                 public void TestCDataInvalid ()
39                 {
40                         try {
41                                 xtw.WriteCData("foo]]>bar");
42                                 Fail("Should have thrown an ArgumentException.");
43                         } 
44                         catch (ArgumentException) { }
45                 }
46
47                 public void TestCloseOpenElements ()
48                 {
49                         xtw.WriteStartElement("foo");
50                         xtw.WriteStartElement("bar");
51                         xtw.WriteStartElement("baz");
52                         xtw.Close();
53                         AssertEquals ("Close didn't write out end elements properly.", "<foo><bar><baz /></bar></foo>",
54                                 sw.GetStringBuilder().ToString());
55                 }
56
57                 public void TestCloseWriteAfter ()
58                 {
59                         xtw.WriteElementString ("foo", "bar");
60                         xtw.Close ();
61
62                         // WriteEndElement and WriteStartDocument aren't tested here because
63                         // they will always throw different exceptions besides 'The Writer is closed.'
64                         // and there are already tests for those exceptions.
65
66                         try {
67                                 xtw.WriteCData ("foo");
68                                 Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
69                         } 
70                         catch (InvalidOperationException e) {
71                                 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
72                         }
73
74                         try {
75                                 xtw.WriteComment ("foo");
76                                 Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
77                         } 
78                         catch (InvalidOperationException e) {
79                                 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
80                         }
81
82                         try {
83                                 xtw.WriteProcessingInstruction ("foo", "bar");
84                                 Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
85                         } 
86                         catch (InvalidOperationException e) {
87                                 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
88                         }
89
90                         try {
91                                 xtw.WriteStartElement ("foo", "bar", "baz");
92                                 Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
93                         } 
94                         catch (InvalidOperationException e) {
95                                 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
96                         }
97
98                         try {
99                                 xtw.WriteString ("foo");
100                                 Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
101                         } 
102                         catch (InvalidOperationException e) {
103                                 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
104                         }
105                 }
106
107                 public void TestCommentValid ()
108                 {
109                         xtw.WriteComment ("foo");
110                         AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", sw.GetStringBuilder().ToString());
111                 }
112
113                 public void TestCommentInvalid ()
114                 {
115                         try {
116                                 xtw.WriteComment("foo-");
117                                 Fail("Should have thrown an ArgumentException.");
118                         } 
119                         catch (ArgumentException) { }
120
121                         try {
122                                 xtw.WriteComment("foo-->bar");
123                                 Fail("Should have thrown an ArgumentException.");
124                         } 
125                         catch (ArgumentException) { }
126                 }
127
128                 public void TestDocumentStart ()
129                 {
130                         xtw.WriteStartDocument ();
131                         AssertEquals ("XmlDeclaration is incorrect.", "<?xml version=\"1.0\" encoding=\"utf-16\"?>",
132                                 sw.GetStringBuilder ().ToString ());
133
134                         try 
135                         {
136                                 xtw.WriteStartDocument ();
137                                 Fail("Should have thrown an InvalidOperationException.");
138                         } 
139                         catch (InvalidOperationException e) {
140                                 AssertEquals ("Exception message is incorrect.",
141                                         "WriteStartDocument should be the first call.", e.Message);
142                         }
143                 }
144
145                 public void TestElementEmpty ()
146                 {
147                         xtw.WriteStartElement ("foo");
148                         xtw.WriteEndElement ();
149                         AssertEquals ("Incorrect output.", "<foo />", sw.GetStringBuilder().ToString());
150                 }
151
152                 public void TestElementWriteElementString ()
153                 {
154                         xtw.WriteElementString ("foo", "bar");
155                         AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", sw.GetStringBuilder().ToString());
156                 }
157
158                 public void TestProcessingInstructionValid ()
159                 {
160                         xtw.WriteProcessingInstruction("foo", "bar");
161                         AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", sw.GetStringBuilder().ToString());
162                 }
163
164                 public void TestProcessingInstructionInvalid ()
165                 {
166                         try {
167                                 xtw.WriteProcessingInstruction("fo?>o", "bar");
168                                 Fail("Should have thrown an ArgumentException.");
169                         } catch (ArgumentException) { }
170
171                         try {
172                                 xtw.WriteProcessingInstruction("foo", "ba?>r");
173                                 Fail("Should have thrown an ArgumentException.");
174                         } catch (ArgumentException) { }
175
176                         try {
177                                 xtw.WriteProcessingInstruction("", "bar");
178                                 Fail("Should have thrown an ArgumentException.");
179                         } catch (ArgumentException) { }
180
181                         try {
182                                 xtw.WriteProcessingInstruction(null, "bar");
183                                 Fail("Should have thrown an ArgumentException.");
184                         } catch (ArgumentException) { }
185                 }
186
187                 public void TestNamespacesNoNamespaceClearsDefaultNamespace ()
188                 {
189                         xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
190                         xtw.WriteStartElement(String.Empty, "bar", String.Empty);
191                         xtw.WriteElementString("baz", String.Empty, String.Empty);
192                         xtw.WriteEndElement();
193                         xtw.WriteEndElement();
194                         AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
195                                 "<foo xmlns=\"http://netsack.com/\"><bar xmlns=\"\"><baz /></bar></foo>",
196                                 sw.GetStringBuilder().ToString());
197                 }
198
199                 public void TestNamespacesPrefix ()
200                 {
201                         xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
202                         xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
203                         xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
204                         xtw.WriteEndElement ();
205                         xtw.WriteEndElement ();
206                         AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
207                                 "<foo:bar xmlns:foo=\"http://netsack.com/\"><foo:baz><foo:qux /></foo:baz></foo:bar>",
208                                 sw.GetStringBuilder ().ToString ());
209                 }
210
211                 public void TestNamespacesPrefixWithEmptyNamespace ()
212                 {
213                         try {
214                                 xtw.WriteStartElement ("foo", "bar", "");
215                                 Fail ("Should have thrown an ArgumentException.");
216                         }
217                         catch (ArgumentException e) {
218                                 AssertEquals ("Exception message is incorrect.",
219                                         "Cannot use a prefix with an empty namespace.", e.Message);
220                         }
221                 }
222
223                 public void TestWriteEndElement ()
224                 {
225                         try 
226                         {
227                                 xtw.WriteEndElement ();
228                                 Fail ("Should have thrown an InvalidOperationException.");
229                         }
230                         catch (InvalidOperationException e) {
231                                 AssertEquals ("Exception message is incorrect.",
232                                         "There was no XML start tag open.", e.Message);
233                         }
234                 }
235         }
236 }