2 // System.Xml.XmlTextWriterTests
5 // Kral Ferch <kral_ferch@hotmail.com>
15 using NUnit.Framework;
17 namespace Ximian.Mono.Tests
19 public class XmlTextWriterTests : TestCase
21 public XmlTextWriterTests () : base ("Ximian.Mono.Tests.XmlTextWriterTests testsuite") {}
22 public XmlTextWriterTests (string name) : base (name) {}
27 protected override void SetUp ()
29 sw = new StringWriter ();
30 xtw = new XmlTextWriter (sw);
33 public void TestCDataValid ()
35 xtw.WriteCData ("foo");
36 AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", sw.GetStringBuilder().ToString());
39 public void TestCDataInvalid ()
42 xtw.WriteCData("foo]]>bar");
43 Fail("Should have thrown an ArgumentException.");
45 catch (ArgumentException) { }
48 public void TestCloseOpenElements ()
50 xtw.WriteStartElement("foo");
51 xtw.WriteStartElement("bar");
52 xtw.WriteStartElement("baz");
54 AssertEquals ("Close didn't write out end elements properly.", "<foo><bar><baz /></bar></foo>",
55 sw.GetStringBuilder().ToString());
58 public void TestCloseWriteAfter ()
60 xtw.WriteElementString ("foo", "bar");
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.
68 xtw.WriteCData ("foo");
69 Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
71 catch (InvalidOperationException e) {
72 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
76 xtw.WriteComment ("foo");
77 Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
79 catch (InvalidOperationException e) {
80 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
84 xtw.WriteProcessingInstruction ("foo", "bar");
85 Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
87 catch (InvalidOperationException e) {
88 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
92 xtw.WriteStartElement ("foo", "bar", "baz");
93 Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
95 catch (InvalidOperationException e) {
96 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
100 xtw.WriteString ("foo");
101 Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
103 catch (InvalidOperationException e) {
104 AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
108 public void TestCommentValid ()
110 xtw.WriteComment ("foo");
111 AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", sw.GetStringBuilder().ToString());
114 public void TestCommentInvalid ()
117 xtw.WriteComment("foo-");
118 Fail("Should have thrown an ArgumentException.");
120 catch (ArgumentException) { }
123 xtw.WriteComment("foo-->bar");
124 Fail("Should have thrown an ArgumentException.");
126 catch (ArgumentException) { }
129 public void TestConstructorsAndBaseStream ()
131 Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (null, this.xtw.BaseStream));
137 ms = new MemoryStream ();
138 xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
139 xtw.WriteStartDocument ();
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));
146 ms = new MemoryStream ();
147 xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
148 xtw.WriteStartDocument (true);
150 ms.Seek (0, SeekOrigin.Begin);
151 sr = new StreamReader (ms);
152 AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
154 ms = new MemoryStream ();
155 xtw = new XmlTextWriter (ms, new UTF8Encoding ());
156 xtw.WriteStartDocument ();
158 ms.Seek (0, SeekOrigin.Begin);
159 sr = new StreamReader (ms);
160 AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
162 ms = new MemoryStream ();
163 xtw = new XmlTextWriter (ms, null);
164 xtw.WriteStartDocument ();
166 ms.Seek (0, SeekOrigin.Begin);
167 sr = new StreamReader (ms);
168 AssertEquals ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
170 ms = new MemoryStream ();
171 xtw = new XmlTextWriter (ms, null);
172 xtw.WriteStartDocument (true);
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));
180 public void TestDocumentStart ()
182 xtw.WriteStartDocument ();
183 AssertEquals ("XmlDeclaration is incorrect.", "<?xml version=\"1.0\" encoding=\"utf-16\"?>",
184 sw.GetStringBuilder ().ToString ());
188 xtw.WriteStartDocument ();
189 Fail("Should have thrown an InvalidOperationException.");
191 catch (InvalidOperationException e) {
192 AssertEquals ("Exception message is incorrect.",
193 "WriteStartDocument should be the first call.", e.Message);
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 ());
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 ());
207 public void TestElementEmpty ()
209 xtw.WriteStartElement ("foo");
210 xtw.WriteEndElement ();
211 AssertEquals ("Incorrect output.", "<foo />", sw.GetStringBuilder().ToString());
214 public void TestElementWriteElementString ()
216 xtw.WriteElementString ("foo", "bar");
217 AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", sw.GetStringBuilder().ToString());
220 public void TestFormatting ()
222 xtw.Formatting = Formatting.Indented;
223 xtw.WriteStartDocument ();
224 xtw.WriteStartElement ("foo");
225 xtw.WriteElementString ("bar", "");
227 AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<foo>\r\n <bar />\r\n</foo>",
228 sw.GetStringBuilder ().ToString ());
231 public void TestFormattingInvalidXmlForFun ()
233 xtw.Formatting = Formatting.Indented;
234 xtw.IndentChar = 'x';
235 xtw.WriteStartDocument ();
236 xtw.WriteStartElement ("foo");
237 xtw.WriteStartElement ("bar");
238 xtw.WriteElementString ("baz", "");
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 ());
244 public void TestFormattingFromRemarks ()
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 ());
261 // TODO: Need some tests for attributes with namespaces here...
262 public void saveTestNamespacesAttributesPassingInNamespaces ()
266 public void TestNamespacesElementsPassingInNamespaces ()
268 xtw.Namespaces = false;
270 // These shouldn't throw any exceptions since they don't pass in
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", "");
281 AssertEquals ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", sw.GetStringBuilder ().ToString ());
283 // These should throw ArgumentException because they pass in a
284 // namespace when Namespaces = false.
286 xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
287 Fail ("Expected an ArgumentException.");
288 } catch (ArgumentException) {}
291 xtw.WriteStartElement ("foo", "http://netsack.com/");
292 Fail ("Expected an ArgumentException.");
293 } catch (ArgumentException) {}
296 xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
297 Fail ("Expected an ArgumentException.");
298 } catch (ArgumentException) {}
301 xtw.WriteStartElement ("foo", "bar", null);
302 Fail ("Expected an ArgumentException.");
303 } catch (ArgumentException) {}
306 xtw.WriteStartElement ("foo", "bar", "");
307 Fail ("Expected an ArgumentException.");
308 } catch (ArgumentException) {}
311 xtw.WriteStartElement ("foo", "", "");
312 Fail ("Expected an ArgumentException.");
313 } catch (ArgumentException) {}
316 public void TestNamespacesNoNamespaceClearsDefaultNamespace ()
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());
328 public void TestNamespacesPrefix ()
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 ());
340 public void TestNamespacesPrefixWithEmptyAndNullNamespace ()
343 xtw.WriteStartElement ("foo", "bar", "");
344 Fail ("Should have thrown an ArgumentException.");
345 } catch (ArgumentException) {}
349 xtw.WriteStartElement ("foo", "bar", null);
350 Fail ("Should have thrown an ArgumentException.");
352 catch (ArgumentException) {}
355 public void TestNamespacesSettingWhenWriteStateNotStart ()
357 xtw.WriteStartElement ("foo");
360 xtw.Namespaces = false;
361 Fail ("Expected an InvalidOperationException.");
363 catch (InvalidOperationException) {}
364 AssertEquals (true, xtw.Namespaces);
367 public void TestProcessingInstructionValid ()
369 xtw.WriteProcessingInstruction("foo", "bar");
370 AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", sw.GetStringBuilder().ToString());
373 public void TestProcessingInstructionInvalid ()
377 xtw.WriteProcessingInstruction("fo?>o", "bar");
378 Fail("Should have thrown an ArgumentException.");
380 catch (ArgumentException) { }
384 xtw.WriteProcessingInstruction("foo", "ba?>r");
385 Fail("Should have thrown an ArgumentException.");
387 catch (ArgumentException) { }
391 xtw.WriteProcessingInstruction("", "bar");
392 Fail("Should have thrown an ArgumentException.");
394 catch (ArgumentException) { }
398 xtw.WriteProcessingInstruction(null, "bar");
399 Fail("Should have thrown an ArgumentException.");
401 catch (ArgumentException) { }
404 public void TestQuoteCharInvalid ()
408 Fail ("Should have thrown an ArgumentException.");
409 } catch (ArgumentException) {}
412 public void TestWriteEndElement ()
416 xtw.WriteEndElement ();
417 Fail ("Should have thrown an InvalidOperationException.");
419 catch (InvalidOperationException e) {
420 AssertEquals ("Exception message is incorrect.",
421 "There was no XML start tag open.", e.Message);
425 public void TestWriteState ()
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);
435 AssertEquals (WriteState.Closed, xtw.WriteState);