2 // System.Xml.XmlWriterSettingsTests.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
15 using System.Xml.Schema;
16 using NUnit.Framework;
18 namespace MonoTests.System.Xml
21 public class XmlWriterSettingsTests : Assertion
24 public void DefaultValue ()
26 XmlWriterSettings s = new XmlWriterSettings ();
32 private void DefaultValue (XmlWriterSettings s)
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 (false, s.OmitXmlDeclaration);
46 public void EncodingTest ()
48 // For Stream it makes sense
49 XmlWriterSettings s = new XmlWriterSettings ();
50 s.Encoding = Encoding.GetEncoding ("shift_jis");
51 MemoryStream ms = new MemoryStream ();
52 XmlWriter w = XmlWriter.Create (ms, s);
53 w.WriteStartElement ("root");
56 byte [] data = ms.ToArray ();
57 Assert (data.Length != 0);
58 string str = s.Encoding.GetString (data);
59 AssertEquals ("<?xml version=\"1.0\" encoding=\"shift_jis\"?><root />", str);
61 // For TextWriter it does not make sense
62 StringWriter sw = new StringWriter ();
63 w = XmlWriter.Create (sw, s);
64 w.WriteStartElement ("root");
67 AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?><root />", sw.ToString ());
71 [ExpectedException (typeof (ArgumentException))]
72 public void CheckCharactersTest ()
74 XmlWriterSettings s = new XmlWriterSettings ();
75 StringWriter sw = new StringWriter ();
76 XmlWriter w = XmlWriter.Create (sw, s);
77 w.WriteStartElement ("root");
78 w.WriteString ("\0"); // invalid
84 public void CheckCharactersFalseTest ()
86 XmlWriterSettings s = new XmlWriterSettings ();
87 s.CheckCharacters = false;
88 StringWriter sw = new StringWriter ();
89 XmlWriter w = XmlWriter.Create (sw, s);
90 w.WriteStartElement ("root");
91 w.WriteString ("\0"); // invalid
96 [ExpectedException (typeof (ObjectDisposedException))]
97 public void CloseOutputTest ()
99 XmlWriterSettings s = new XmlWriterSettings ();
100 s.CloseOutput = true;
101 StringWriter sw = new StringWriter ();
102 XmlWriter w = XmlWriter.Create (sw, s);
103 w.WriteStartElement ("root");
104 w.WriteEndElement ();
106 sw.Write ("more"); // not allowed
110 [Ignore ("Write Test!")]
111 public void ConformanceLevelTest ()
113 throw new NotImplementedException ();
117 public void IndentationAndFormatting ()
119 // Test for Indent, IndentChars, NewLineOnAttributes,
120 // NewLineChars and OmitXmlDeclaration.
121 string output = "<root\n attr=\"value\"\n attr2=\"value\">\n <child>test</child>\n</root>";
122 XmlWriterSettings s = new XmlWriterSettings ();
123 s.OmitXmlDeclaration = true;
126 s.NewLineChars = "\n";
127 s.NewLineOnAttributes = true;
128 StringWriter sw = new StringWriter ();
129 XmlWriter w = XmlWriter.Create (sw, s);
130 w.WriteStartElement ("root");
131 w.WriteAttributeString ("attr", "value");
132 w.WriteAttributeString ("attr2", "value");
133 w.WriteStartElement ("child");
134 w.WriteString ("test");
135 w.WriteEndElement ();
136 w.WriteEndElement ();
138 AssertEquals (output, sw.ToString ());
142 public void SetEncodingNull ()
145 new XmlWriterSettings ().Encoding = null;
149 [ExpectedException (typeof (ArgumentNullException))]
150 public void NewLineCharsNull ()
152 new XmlWriterSettings ().NewLineChars = null;
156 public void CreateOmitXmlDeclaration ()
158 StringBuilder sb = new StringBuilder ();
159 // Even if XmlWriter is allowed to write fragment,
160 // DataContractSerializer never allows it to write
161 // content in contentOnly mode.
162 XmlWriterSettings settings = new XmlWriterSettings ();
163 settings.OmitXmlDeclaration = true;
164 //settings.ConformanceLevel = ConformanceLevel.Fragment;
165 XmlWriter w = XmlWriter.Create (sb, settings);
166 w.WriteStartElement ("root");
167 w.WriteEndElement ();
169 AssertEquals ("<root />", sb.ToString ());