| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- //
- // System.Xml.XmlWriterSettingsTests.cs
- //
- // Authors:
- // Atsushi Enomoto <[email protected]>
- //
- // (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]
- [Ignore ("Write Test!")]
- public void ConformanceLevelTest ()
- {
- throw new NotImplementedException ();
- }
- [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
|