remove svn:executable from .cs files
[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 : Assertion
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                         AssertEquals (true, s.CheckCharacters);
35                         AssertEquals (false, s.CloseOutput);
36                         AssertEquals (ConformanceLevel.Document, s.ConformanceLevel);
37                         AssertEquals (Encoding.UTF8, s.Encoding);
38                         AssertEquals (false, s.Indent);
39                         AssertEquals ("  ", s.IndentChars);
40                         AssertEquals (Environment.NewLine, s.NewLineChars);
41                         AssertEquals (false, s.NewLineOnAttributes);
42                         AssertEquals (true, s.NormalizeNewLines);
43                         AssertEquals (false, s.OmitXmlDeclaration);
44                 }
45
46                 [Test]
47                 public void EncodingTest ()
48                 {
49                         // For Stream it makes sense
50                         XmlWriterSettings s = new XmlWriterSettings ();
51                         s.Encoding = Encoding.GetEncoding ("shift_jis");
52                         MemoryStream ms = new MemoryStream ();
53                         XmlWriter w = XmlWriter.Create (ms, s);
54                         w.WriteStartElement ("root");
55                         w.WriteEndElement ();
56                         w.Close ();
57                         byte [] data = ms.ToArray ();
58                         Assert (data.Length != 0);
59                         string str = s.Encoding.GetString (data);
60                         AssertEquals ("<?xml version=\"1.0\" encoding=\"shift_jis\"?><root />", str);
61
62                         // For TextWriter it does not make sense
63                         StringWriter sw = new StringWriter ();
64                         w = XmlWriter.Create (sw, s);
65                         w.WriteStartElement ("root");
66                         w.WriteEndElement ();
67                         w.Close ();
68                         AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?><root />", sw.ToString ());
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (ArgumentException))]
73                 public void CheckCharactersTest ()
74                 {
75                         XmlWriterSettings s = new XmlWriterSettings ();
76                         StringWriter sw = new StringWriter ();
77                         XmlWriter w = XmlWriter.Create (sw, s);
78                         w.WriteStartElement ("root");
79                         w.WriteString ("\0"); // invalid
80                         w.WriteEndElement ();
81                         w.Close ();
82                 }
83
84                 [Test]
85                 public void CheckCharactersFalseTest ()
86                 {
87                         XmlWriterSettings s = new XmlWriterSettings ();
88                         s.CheckCharacters = false;
89                         StringWriter sw = new StringWriter ();
90                         XmlWriter w = XmlWriter.Create (sw, s);
91                         w.WriteStartElement ("root");
92                         w.WriteString ("\0"); // invalid
93                         w.WriteEndElement ();
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (ObjectDisposedException))]
98                 public void CloseOutputTest ()
99                 {
100                         XmlWriterSettings s = new XmlWriterSettings ();
101                         s.CloseOutput = true;
102                         StringWriter sw = new StringWriter ();
103                         XmlWriter w = XmlWriter.Create (sw, s);
104                         w.WriteStartElement ("root");
105                         w.WriteEndElement ();
106                         w.Close ();
107                         sw.Write ("more"); // not allowed
108                 }
109
110                 [Test]
111                 [Ignore ("Write Test!")]
112                 public void ConformanceLevelTest ()
113                 {
114                         throw new NotImplementedException ();
115                 }
116
117                 [Test]
118                 public void IndentationAndFormatting ()
119                 {
120                         // Test for Indent, IndentChars, NewLineOnAttributes,
121                         // NewLineChars and OmitXmlDeclaration.
122                         string output = "<root\n    attr=\"value\"\n    attr2=\"value\">\n    <child>test</child>\n</root>";
123                         XmlWriterSettings s = new XmlWriterSettings ();
124                         s.OmitXmlDeclaration = true;
125                         s.Indent = true;
126                         s.IndentChars = "    ";
127                         s.NewLineChars = "\n";
128                         s.NewLineOnAttributes = true;
129                         StringWriter sw = new StringWriter ();
130                         XmlWriter w = XmlWriter.Create (sw, s);
131                         w.WriteStartElement ("root");
132                         w.WriteAttributeString ("attr", "value");
133                         w.WriteAttributeString ("attr2", "value");
134                         w.WriteStartElement ("child");
135                         w.WriteString ("test");
136                         w.WriteEndElement ();
137                         w.WriteEndElement ();
138                         w.Close ();
139                         AssertEquals (output, sw.ToString ());
140                 }
141
142                 [Test]
143                 [Ignore ("Write Test!")]
144                 public void NormalizeNewLinesTest ()
145                 {
146                         throw new NotImplementedException ();
147                 }
148         }
149 }
150 #endif