namespace and prefix support in XmlTextWriter
[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                         try {
63                                 xtw.WriteCData ("foo");
64                                 Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
65                         } 
66                         catch (InvalidOperationException e) {
67                                 AssertEquals ("InvalidOperationException message incorrect.", "The Writer is closed.", e.Message);
68                         }
69
70                         try {
71                                 xtw.WriteComment ("foo");
72                                 Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
73                         } 
74                         catch (InvalidOperationException e) {
75                                 AssertEquals ("InvalidOperationException message incorrect.", "The Writer is closed.", e.Message);
76                         }
77
78                         try {
79                                 xtw.WriteProcessingInstruction ("foo", "bar");
80                                 Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
81                         } 
82                         catch (InvalidOperationException e) {
83                                 AssertEquals ("InvalidOperationException message incorrect.", "The Writer is closed.", e.Message);
84                         }
85                 }
86
87                 public void TestCommentValid ()
88                 {
89                         xtw.WriteComment ("foo");
90                         AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", sw.GetStringBuilder().ToString());
91                 }
92
93                 public void TestCommentInvalid ()
94                 {
95                         try {
96                                 xtw.WriteComment("foo-");
97                                 Fail("Should have thrown an ArgumentException.");
98                         } 
99                         catch (ArgumentException) { }
100
101                         try {
102                                 xtw.WriteComment("foo-->bar");
103                                 Fail("Should have thrown an ArgumentException.");
104                         } 
105                         catch (ArgumentException) { }
106                 }
107
108                 public void TestElementEmpty ()
109                 {
110                         xtw.WriteStartElement ("foo");
111                         xtw.WriteEndElement ();
112                         AssertEquals ("Incorrect output.", "<foo />", sw.GetStringBuilder().ToString());
113                 }
114
115                 public void TestElementWriteElementString ()
116                 {
117                         xtw.WriteElementString ("foo", "bar");
118                         AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", sw.GetStringBuilder().ToString());
119                 }
120
121                 public void TestProcessingInstructionValid ()
122                 {
123                         xtw.WriteProcessingInstruction("foo", "bar");
124                         AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", sw.GetStringBuilder().ToString());
125                 }
126
127                 public void TestProcessingInstructionInvalid ()
128                 {
129                         try {
130                                 xtw.WriteProcessingInstruction("fo?>o", "bar");
131                                 Fail("Should have thrown an ArgumentException.");
132                         } catch (ArgumentException) { }
133
134                         try {
135                                 xtw.WriteProcessingInstruction("foo", "ba?>r");
136                                 Fail("Should have thrown an ArgumentException.");
137                         } catch (ArgumentException) { }
138
139                         try {
140                                 xtw.WriteProcessingInstruction("", "bar");
141                                 Fail("Should have thrown an ArgumentException.");
142                         } catch (ArgumentException) { }
143
144                         try {
145                                 xtw.WriteProcessingInstruction(null, "bar");
146                                 Fail("Should have thrown an ArgumentException.");
147                         } catch (ArgumentException) { }
148                 }
149
150                 public void TestNamespacesNoNamespaceClearsDefaultNamespace ()
151                 {
152                         xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
153                         xtw.WriteStartElement(String.Empty, "bar", String.Empty);
154                         xtw.WriteElementString("baz", String.Empty, String.Empty);
155                         xtw.WriteEndElement();
156                         xtw.WriteEndElement();
157                         AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
158                                 "<foo xmlns=\"http://netsack.com/\"><bar xmlns=\"\"><baz /></bar></foo>",
159                                 sw.GetStringBuilder().ToString());
160                 }
161
162                 public void TestNamespacesPrefix ()
163                 {
164                         xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
165                         xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
166                         xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
167                         xtw.WriteEndElement ();
168                         xtw.WriteEndElement ();
169                         AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
170                                 "<foo:bar xmlns:foo=\"http://netsack.com/\"><foo:baz><foo:qux /></foo:baz></foo:bar>",
171                                 sw.GetStringBuilder ().ToString ());
172                 }
173
174                 public void TestNamespacesPrefixWithEmptyNamespace ()
175                 {
176                         try {
177                                 xtw.WriteStartElement ("foo", "bar", "");
178                                 Fail ("Should have thrown an ArgumentException.");
179                         }
180                         catch (ArgumentException e) {
181                                 AssertEquals ("ArgumentException text is incorrect.",
182                                         "Cannot use a prefix with an empty namespace.", e.Message);
183                         }
184                 }
185         }
186 }