XmlDeclarationTests.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // System.Xml.XmlDeclarationTests.cs
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. // Author: Martin Willemoes Hansen ([email protected])
  6. //
  7. // (C) Ximian, Inc.
  8. // (C) 2003 Martin Willemoes Hansen
  9. //
  10. using System;
  11. using System.IO;
  12. using System.Xml;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Xml
  15. {
  16. [TestFixture]
  17. public class XmlDeclarationTests
  18. {
  19. XmlDocument document;
  20. XmlDeclaration declaration;
  21. [SetUp]
  22. public void GetReady ()
  23. {
  24. document = new XmlDocument ();
  25. document.LoadXml ("<foo><bar></bar></foo>");
  26. declaration = document.CreateXmlDeclaration ("1.0", null, null);
  27. }
  28. [Test]
  29. public void InnerAndOuterXml ()
  30. {
  31. declaration = document.CreateXmlDeclaration ("1.0", null, null);
  32. Assert.AreEqual (String.Empty, declaration.InnerXml);
  33. Assert.AreEqual ("<?xml version=\"1.0\"?>", declaration.OuterXml);
  34. declaration = document.CreateXmlDeclaration ("1.0", "doesn't check", null);
  35. Assert.AreEqual (String.Empty, declaration.InnerXml);
  36. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"doesn't check\"?>", declaration.OuterXml);
  37. declaration = document.CreateXmlDeclaration ("1.0", null, "yes");
  38. Assert.AreEqual (String.Empty, declaration.InnerXml);
  39. Assert.AreEqual ("<?xml version=\"1.0\" standalone=\"yes\"?>", declaration.OuterXml);
  40. declaration = document.CreateXmlDeclaration ("1.0", "foo", "no");
  41. Assert.AreEqual (String.Empty, declaration.InnerXml);
  42. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"foo\" standalone=\"no\"?>", declaration.OuterXml);
  43. }
  44. internal void XmlNodeBaseProperties (XmlNode original, XmlNode cloned)
  45. {
  46. // assertequals (original.nodetype + " was incorrectly cloned.",
  47. // original.baseuri, cloned.baseuri);
  48. Assert.IsNull (cloned.ParentNode);
  49. Assert.AreEqual (original.Value, cloned.Value, "Value incorrectly cloned");
  50. Assert.IsTrue (!Object.ReferenceEquals (original, cloned), "Copies, not pointers");
  51. }
  52. [Test]
  53. public void Constructor ()
  54. {
  55. try {
  56. XmlDeclaration broken = document.CreateXmlDeclaration ("2.0", null, null);
  57. } catch (ArgumentException) {
  58. return;
  59. } catch (Exception e) {
  60. Assert.Fail ("first arg null, wrong exception: " + e.ToString());
  61. }
  62. }
  63. [Test]
  64. public void NodeType ()
  65. {
  66. Assert.AreEqual (XmlNodeType.XmlDeclaration, declaration.NodeType, "incorrect NodeType returned");
  67. }
  68. [Test]
  69. public void Names ()
  70. {
  71. Assert.AreEqual ("xml", declaration.Name, "Name is incorrect");
  72. Assert.AreEqual ("xml", declaration.LocalName, "LocalName is incorrect");
  73. }
  74. [Test]
  75. public void EncodingProperty ()
  76. {
  77. XmlDeclaration d1 = document.CreateXmlDeclaration ("1.0", "foo", null);
  78. Assert.AreEqual ("foo", d1.Encoding, "Encoding property");
  79. XmlDeclaration d2 = document.CreateXmlDeclaration ("1.0", null, null);
  80. Assert.AreEqual (String.Empty, d2.Encoding, "null Encoding property");
  81. }
  82. [Test]
  83. public void ValidInnerText ()
  84. {
  85. declaration.InnerText = "version='1.0'";
  86. declaration.InnerText = "version='1.0' encoding='euc-jp'";
  87. declaration.InnerText = "version='1.0' standalone='no'";
  88. declaration.InnerText = "version='1.0' encoding='iso-8859-1' standalone=\"yes\"";
  89. declaration.InnerText = @"version = '1.0' encoding =
  90. 'euc-jp' standalone = 'yes' ";
  91. declaration.InnerText = " version = '1.0'";
  92. }
  93. [Test]
  94. [ExpectedException (typeof (XmlException))]
  95. public void InvalidInnerText ()
  96. {
  97. declaration.InnerText = "version='1.0a'";
  98. }
  99. [Test]
  100. [ExpectedException (typeof (XmlException))]
  101. public void InvalidInnerText2 ()
  102. {
  103. declaration.InnerText = "version='1.0' encoding='euc-kr\"";
  104. }
  105. [Test]
  106. [ExpectedException (typeof (XmlException))]
  107. public void InvalidInnerText3 ()
  108. {
  109. declaration.InnerText = "version='2.0'";
  110. }
  111. [Test]
  112. [ExpectedException (typeof (XmlException))]
  113. public void InvalidInnerText4 ()
  114. {
  115. declaration.InnerText = "version='1.0' standalone='Yeah!!!!!'";
  116. }
  117. [Test]
  118. [ExpectedException (typeof (XmlException))]
  119. public void InvalidInnerText5 ()
  120. {
  121. declaration.InnerText = "version='1.0'standalone='yes'";
  122. }
  123. [Test]
  124. [ExpectedException (typeof (XmlException))]
  125. public void InvalidInnerText6 ()
  126. {
  127. declaration.InnerText = "version='1.0'standalone='yes' encoding='utf-8'";
  128. }
  129. [Test]
  130. public void StandaloneProperty ()
  131. {
  132. XmlDeclaration d1 = document.CreateXmlDeclaration ("1.0", null, "yes");
  133. Assert.AreEqual ("yes", d1.Standalone, "Yes standalone property");
  134. XmlDeclaration d2 = document.CreateXmlDeclaration ("1.0", null, "no");
  135. Assert.AreEqual ("no", d2.Standalone, "No standalone property");
  136. XmlDeclaration d3 = document.CreateXmlDeclaration ("1.0", null, null);
  137. Assert.AreEqual (String.Empty, d3.Standalone, "null Standalone property");
  138. }
  139. [Test]
  140. public void ValueProperty ()
  141. {
  142. string expected = "version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"" ;
  143. XmlDeclaration d = document.CreateXmlDeclaration ("1.0", "ISO-8859-1", "yes");
  144. Assert.AreEqual (expected, d.Value, "Value property");
  145. d.Value = expected;
  146. Assert.AreEqual (expected, d.Value, "Value round-trip");
  147. d.Value = " " + expected;
  148. Assert.AreEqual (expected, d.Value, "Value round-trip (padded)");
  149. d.Value = "version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"" ;
  150. Assert.AreEqual (expected, d.Value, "Value round-trip (padded 2)");
  151. d.Value = "version=\"1.0\"\tencoding=\"ISO-8859-1\" standalone=\"yes\"" ;
  152. Assert.AreEqual (expected, d.Value, "Value round-trip (\\t)");
  153. d.Value = "version=\"1.0\"\n encoding=\"ISO-8859-1\" standalone=\"yes\"" ;
  154. Assert.AreEqual (expected, d.Value, "Value round-trip (\\n)");
  155. d.Value = "version=\"1.0\" encoding = \"ISO-8859-1\" standalone = \"yes\"" ;
  156. Assert.AreEqual (expected, d.Value, "Value round-trip (spaces)");
  157. d.Value = "version='1.0' encoding='ISO-8859-1' standalone='yes'" ;
  158. Assert.AreEqual (expected, d.Value, "Value round-trip ('s)");
  159. }
  160. [Test]
  161. public void Bug79496 ()
  162. {
  163. StringWriter sw = new StringWriter ();
  164. XmlTextWriter xtw = new XmlTextWriter (sw);
  165. xtw.WriteStartDocument (true);
  166. xtw.WriteStartElement ("person");
  167. xtw.WriteEndElement ();
  168. xtw.Flush ();
  169. XmlDocument doc = new XmlDocument ();
  170. doc.LoadXml (sw.ToString ());
  171. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><person />", doc.OuterXml);
  172. }
  173. [Test]
  174. public void XmlCommentCloneNode ()
  175. {
  176. XmlNode original = declaration;
  177. XmlNode shallow = declaration.CloneNode (false); // shallow
  178. XmlNodeBaseProperties (original, shallow);
  179. XmlNode deep = declaration.CloneNode (true); // deep
  180. XmlNodeBaseProperties (original, deep);
  181. Assert.AreEqual (deep.OuterXml, shallow.OuterXml, "deep cloning differs from shallow cloning");
  182. }
  183. }
  184. }