XmlWriterSettingsTests.cs 3.9 KB

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