1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
//
// System.Xml.XmlWriterSettingsTests.cs
//
// Authors:
// Atsushi Enomoto <atsushi@ximian.com>
//
// (C)2004 Novell Inc.
//
#if NET_2_0
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using NUnit.Framework;
namespace MonoTests.System.Xml
{
[TestFixture]
public class XmlWriterSettingsTests : Assertion
{
[Test]
public void DefaultValue ()
{
XmlWriterSettings s = new XmlWriterSettings ();
DefaultValue (s);
s.Reset ();
DefaultValue (s);
}
private void DefaultValue (XmlWriterSettings s)
{
AssertEquals (true, s.CheckCharacters);
AssertEquals (false, s.CloseOutput);
AssertEquals (ConformanceLevel.Document, s.ConformanceLevel);
AssertEquals (Encoding.UTF8, s.Encoding);
AssertEquals (false, s.Indent);
AssertEquals (" ", s.IndentChars);
AssertEquals (Environment.NewLine, s.NewLineChars);
AssertEquals (false, s.NewLineOnAttributes);
AssertEquals (false, s.OmitXmlDeclaration);
}
[Test]
public void EncodingTest ()
{
// For Stream it makes sense
XmlWriterSettings s = new XmlWriterSettings ();
s.Encoding = Encoding.GetEncoding ("shift_jis");
MemoryStream ms = new MemoryStream ();
XmlWriter w = XmlWriter.Create (ms, s);
w.WriteStartElement ("root");
w.WriteEndElement ();
w.Close ();
byte [] data = ms.ToArray ();
Assert (data.Length != 0);
string str = s.Encoding.GetString (data);
AssertEquals ("<?xml version=\"1.0\" encoding=\"shift_jis\"?><root />", str);
// For TextWriter it does not make sense
StringWriter sw = new StringWriter ();
w = XmlWriter.Create (sw, s);
w.WriteStartElement ("root");
w.WriteEndElement ();
w.Close ();
AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?><root />", sw.ToString ());
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void CheckCharactersTest ()
{
XmlWriterSettings s = new XmlWriterSettings ();
StringWriter sw = new StringWriter ();
XmlWriter w = XmlWriter.Create (sw, s);
w.WriteStartElement ("root");
w.WriteString ("\0"); // invalid
w.WriteEndElement ();
w.Close ();
}
[Test]
public void CheckCharactersFalseTest ()
{
XmlWriterSettings s = new XmlWriterSettings ();
s.CheckCharacters = false;
StringWriter sw = new StringWriter ();
XmlWriter w = XmlWriter.Create (sw, s);
w.WriteStartElement ("root");
w.WriteString ("\0"); // invalid
w.WriteEndElement ();
}
[Test]
[ExpectedException (typeof (ObjectDisposedException))]
public void CloseOutputTest ()
{
XmlWriterSettings s = new XmlWriterSettings ();
s.CloseOutput = true;
StringWriter sw = new StringWriter ();
XmlWriter w = XmlWriter.Create (sw, s);
w.WriteStartElement ("root");
w.WriteEndElement ();
w.Close ();
sw.Write ("more"); // not allowed
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConformanceLevelFragmentAndWriteStartDocument ()
{
XmlWriterSettings s = new XmlWriterSettings ();
s.ConformanceLevel = ConformanceLevel.Fragment;
s.OmitXmlDeclaration = true;
XmlWriter w = XmlWriter.Create (Console.Out, s);
w.WriteStartDocument ();
}
[Test]
public void ConformanceLevelAuto ()
{
XmlWriterSettings s = new XmlWriterSettings ();
s.ConformanceLevel = ConformanceLevel.Auto;
StringWriter sw = new StringWriter ();
XmlWriter w = XmlWriter.Create (sw, s);
w.WriteElementString ("foo", "");
w.Close ();
AssertEquals ("<foo />", sw.ToString ());
}
[Test]
public void ConformanceLevelAuto_WriteStartDocument ()
{
XmlWriterSettings s = new XmlWriterSettings ();
s.ConformanceLevel = ConformanceLevel.Auto;
StringWriter sw = new StringWriter ();
XmlWriter w = XmlWriter.Create (sw, s);
w.WriteStartDocument ();
w.WriteElementString ("foo", "");
w.Close ();
AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?><foo />", sw.ToString ());
}
[Test]
public void ConformanceLevelAuto_OmitXmlDecl_WriteStartDocument ()
{
XmlWriterSettings s = new XmlWriterSettings ();
s.ConformanceLevel = ConformanceLevel.Auto;
s.OmitXmlDeclaration = true;
StringWriter sw = new StringWriter ();
XmlWriter w = XmlWriter.Create (sw, s);
w.WriteStartDocument ();
w.WriteElementString ("foo", "");
w.Close ();
AssertEquals ("<foo />", sw.ToString ());
}
[Test]
public void ConformanceLevelDocument_OmitXmlDeclDeclaration ()
{
XmlWriterSettings s = new XmlWriterSettings ();
s.ConformanceLevel = ConformanceLevel.Document;
// LAMESPEC:
// On MSDN, XmlWriterSettings.OmitXmlDeclaration is documented as:
// "The XML declaration is always written if
// ConformanceLevel is set to Document, even
// if OmitXmlDeclaration is set to true. "
// but it is incorrect. It does consider
// OmitXmlDeclaration property.
s.OmitXmlDeclaration = true;
StringWriter sw = new StringWriter ();
XmlWriter w = XmlWriter.Create (sw, s);
w.WriteElementString ("foo", "");
w.Close ();
AssertEquals ("<foo />", sw.ToString ());
}
[Test]
public void IndentationAndFormatting ()
{
// Test for Indent, IndentChars, NewLineOnAttributes,
// NewLineChars and OmitXmlDeclaration.
string output = "<root\n attr=\"value\"\n attr2=\"value\">\n <child>test</child>\n</root>";
XmlWriterSettings s = new XmlWriterSettings ();
s.OmitXmlDeclaration = true;
s.Indent = true;
s.IndentChars = " ";
s.NewLineChars = "\n";
s.NewLineOnAttributes = true;
StringWriter sw = new StringWriter ();
XmlWriter w = XmlWriter.Create (sw, s);
w.WriteStartElement ("root");
w.WriteAttributeString ("attr", "value");
w.WriteAttributeString ("attr2", "value");
w.WriteStartElement ("child");
w.WriteString ("test");
w.WriteEndElement ();
w.WriteEndElement ();
w.Close ();
AssertEquals (output, sw.ToString ());
}
[Test]
public void SetEncodingNull ()
{
// null is allowed.
new XmlWriterSettings ().Encoding = null;
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void NewLineCharsNull ()
{
new XmlWriterSettings ().NewLineChars = null;
}
[Test]
public void CreateOmitXmlDeclaration ()
{
StringBuilder sb = new StringBuilder ();
// Even if XmlWriter is allowed to write fragment,
// DataContractSerializer never allows it to write
// content in contentOnly mode.
XmlWriterSettings settings = new XmlWriterSettings ();
settings.OmitXmlDeclaration = true;
//settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlWriter w = XmlWriter.Create (sb, settings);
w.WriteStartElement ("root");
w.WriteEndElement ();
w.Flush ();
AssertEquals ("<root />", sb.ToString ());
}
}
}
#endif
|