XmlWriterSettingsTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // System.Xml.XmlWriterSettingsTests.cs
  3. //
  4. // Authors:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C)2004 Novell Inc.
  8. //
  9. #if NET_2_0
  10. using System;
  11. using System.IO;
  12. using System.Text;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlWriterSettingsTests : Assertion
  20. {
  21. [Test]
  22. public void DefaultValue ()
  23. {
  24. XmlWriterSettings s = new XmlWriterSettings ();
  25. DefaultValue (s);
  26. s.Reset ();
  27. DefaultValue (s);
  28. }
  29. private void DefaultValue (XmlWriterSettings s)
  30. {
  31. AssertEquals (true, s.CheckCharacters);
  32. AssertEquals (false, s.CloseOutput);
  33. AssertEquals (ConformanceLevel.Document, s.ConformanceLevel);
  34. AssertEquals (Encoding.UTF8, s.Encoding);
  35. AssertEquals (false, s.Indent);
  36. AssertEquals (" ", s.IndentChars);
  37. AssertEquals (Environment.NewLine, s.NewLineChars);
  38. AssertEquals (false, s.NewLineOnAttributes);
  39. AssertEquals (false, s.OmitXmlDeclaration);
  40. }
  41. [Test]
  42. public void EncodingTest ()
  43. {
  44. // For Stream it makes sense
  45. XmlWriterSettings s = new XmlWriterSettings ();
  46. s.Encoding = Encoding.GetEncoding ("shift_jis");
  47. MemoryStream ms = new MemoryStream ();
  48. XmlWriter w = XmlWriter.Create (ms, s);
  49. w.WriteStartElement ("root");
  50. w.WriteEndElement ();
  51. w.Close ();
  52. byte [] data = ms.ToArray ();
  53. Assert (data.Length != 0);
  54. string str = s.Encoding.GetString (data);
  55. AssertEquals ("<?xml version=\"1.0\" encoding=\"shift_jis\"?><root />", str);
  56. // For TextWriter it does not make sense
  57. StringWriter sw = new StringWriter ();
  58. w = XmlWriter.Create (sw, s);
  59. w.WriteStartElement ("root");
  60. w.WriteEndElement ();
  61. w.Close ();
  62. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?><root />", sw.ToString ());
  63. }
  64. [Test]
  65. [ExpectedException (typeof (ArgumentException))]
  66. public void CheckCharactersTest ()
  67. {
  68. XmlWriterSettings s = new XmlWriterSettings ();
  69. StringWriter sw = new StringWriter ();
  70. XmlWriter w = XmlWriter.Create (sw, s);
  71. w.WriteStartElement ("root");
  72. w.WriteString ("\0"); // invalid
  73. w.WriteEndElement ();
  74. w.Close ();
  75. }
  76. [Test]
  77. public void CheckCharactersFalseTest ()
  78. {
  79. XmlWriterSettings s = new XmlWriterSettings ();
  80. s.CheckCharacters = false;
  81. StringWriter sw = new StringWriter ();
  82. XmlWriter w = XmlWriter.Create (sw, s);
  83. w.WriteStartElement ("root");
  84. w.WriteString ("\0"); // invalid
  85. w.WriteEndElement ();
  86. }
  87. [Test]
  88. [ExpectedException (typeof (ObjectDisposedException))]
  89. public void CloseOutputTest ()
  90. {
  91. XmlWriterSettings s = new XmlWriterSettings ();
  92. s.CloseOutput = true;
  93. StringWriter sw = new StringWriter ();
  94. XmlWriter w = XmlWriter.Create (sw, s);
  95. w.WriteStartElement ("root");
  96. w.WriteEndElement ();
  97. w.Close ();
  98. sw.Write ("more"); // not allowed
  99. }
  100. [Test]
  101. [Ignore ("Write Test!")]
  102. public void ConformanceLevelTest ()
  103. {
  104. throw new NotImplementedException ();
  105. }
  106. [Test]
  107. public void IndentationAndFormatting ()
  108. {
  109. // Test for Indent, IndentChars, NewLineOnAttributes,
  110. // NewLineChars and OmitXmlDeclaration.
  111. string output = "<root\n attr=\"value\"\n attr2=\"value\">\n <child>test</child>\n</root>";
  112. XmlWriterSettings s = new XmlWriterSettings ();
  113. s.OmitXmlDeclaration = true;
  114. s.Indent = true;
  115. s.IndentChars = " ";
  116. s.NewLineChars = "\n";
  117. s.NewLineOnAttributes = true;
  118. StringWriter sw = new StringWriter ();
  119. XmlWriter w = XmlWriter.Create (sw, s);
  120. w.WriteStartElement ("root");
  121. w.WriteAttributeString ("attr", "value");
  122. w.WriteAttributeString ("attr2", "value");
  123. w.WriteStartElement ("child");
  124. w.WriteString ("test");
  125. w.WriteEndElement ();
  126. w.WriteEndElement ();
  127. w.Close ();
  128. AssertEquals (output, sw.ToString ());
  129. }
  130. [Test]
  131. public void SetEncodingNull ()
  132. {
  133. // null is allowed.
  134. new XmlWriterSettings ().Encoding = null;
  135. }
  136. [Test]
  137. [ExpectedException (typeof (ArgumentNullException))]
  138. public void NewLineCharsNull ()
  139. {
  140. new XmlWriterSettings ().NewLineChars = null;
  141. }
  142. [Test]
  143. public void CreateOmitXmlDeclaration ()
  144. {
  145. StringBuilder sb = new StringBuilder ();
  146. // Even if XmlWriter is allowed to write fragment,
  147. // DataContractSerializer never allows it to write
  148. // content in contentOnly mode.
  149. XmlWriterSettings settings = new XmlWriterSettings ();
  150. settings.OmitXmlDeclaration = true;
  151. //settings.ConformanceLevel = ConformanceLevel.Fragment;
  152. XmlWriter w = XmlWriter.Create (sb, settings);
  153. w.WriteStartElement ("root");
  154. w.WriteEndElement ();
  155. w.Flush ();
  156. AssertEquals ("<root />", sb.ToString ());
  157. }
  158. }
  159. }
  160. #endif