2
0

XmlTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // Tests for System.Web.UI.WebControls.Xml.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Globalization;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Web.UI.WebControls;
  36. using System.Xml;
  37. using System.Xml.Xsl;
  38. namespace MonoTests.System.Web.UI.WebControls
  39. {
  40. class XmlPoker : Xml {
  41. public XmlPoker ()
  42. {
  43. }
  44. public object SaveState ()
  45. {
  46. return SaveViewState ();
  47. }
  48. public void LoadState (object o)
  49. {
  50. LoadViewState (o);
  51. }
  52. public void DoRender (HtmlTextWriter output)
  53. {
  54. Render (output);
  55. }
  56. public void DoAdd (object o)
  57. {
  58. AddParsedSubObject (o);
  59. }
  60. }
  61. [TestFixture]
  62. public class XmlTest {
  63. #if false
  64. public void Label_ViewState ()
  65. {
  66. XmlPoker p = new XmlPoker ();
  67. Assert.AreEqual (p.Text, "", "A1");
  68. p.Text = "Hello";
  69. Assert.AreEqual (p.Text, "Hello", "A2");
  70. object state = p.SaveState ();
  71. Poker copy = new Poker ();
  72. copy.LoadState (state);
  73. Assert.AreEqual (copy.Text, "Hello", "A3");
  74. }
  75. [Test]
  76. public void Label_Render ()
  77. {
  78. StringWriter sw = new StringWriter ();
  79. HtmlTextWriter tw = new HtmlTextWriter (sw);
  80. Label l = new Label ();
  81. l.Text = "Hello";
  82. l.RenderControl (tw);
  83. Assert.AreEqual ("<span>Hello</span>", sw.ToString (), "R1");
  84. }
  85. #endif
  86. [SetUp] public void Xml_Setup ()
  87. {
  88. if (File.Exists ("test.xml")) File.Delete ("test.xml");
  89. using (FileStream f = File.OpenWrite ("test.xml")){
  90. StreamWriter sw = new StreamWriter (f);
  91. sw.WriteLine ("<?xml version=\"1.0\"?><testfile></testfile>");
  92. }
  93. if (File.Exists ("test.xsl")) File.Delete ("test.xsl");
  94. using (FileStream f = File.OpenWrite ("test.xsl")){
  95. StreamWriter sw = new StreamWriter (f);
  96. sw.WriteLine ("<xsl:stylesheet version='1.0' " +
  97. "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" +
  98. "<xsl:template match=\"*\">" +
  99. "<xsl:copy-of select=\".\"/>" +
  100. "</xsl:template>" +
  101. "</xsl:stylesheet>");
  102. }
  103. }
  104. [TearDown] public void Xml_TearDown ()
  105. {
  106. if (File.Exists ("test.xml")) File.Delete ("test.xml");
  107. if (File.Exists ("test.xsl")) File.Delete ("test.xsl");
  108. }
  109. [Test] public void Xml_Values ()
  110. {
  111. Xml xml = new Xml ();
  112. Assert.AreEqual ("", xml.DocumentContent, "V1");
  113. Assert.AreEqual (null, xml.Document, "V2");
  114. Assert.AreEqual ("", xml.DocumentSource, "V3");
  115. Assert.AreEqual (null, xml.Transform, "V4");
  116. Assert.AreEqual (null, xml.TransformArgumentList, "V5");
  117. Assert.AreEqual ("", xml.TransformSource, "V6");
  118. // Check that assignments to null, are mapped back into ""
  119. xml.DocumentContent = null;
  120. Assert.AreEqual ("", xml.DocumentContent, "V7");
  121. xml.TransformSource = null;
  122. Assert.AreEqual ("", xml.TransformSource, "V8");
  123. Assert.AreEqual (null, xml.Transform, "V9");
  124. }
  125. // Tests that invalid documents can be set before rendering.
  126. [Test] public void Xml_InvalidDocument ()
  127. {
  128. Xml xml = new Xml ();
  129. xml.DocumentContent = "Hey";
  130. #if NET_2_0
  131. Assert.AreEqual ("Hey", xml.DocumentContent);
  132. #else
  133. Assert.AreEqual ("", xml.DocumentContent);
  134. #endif
  135. xml.DocumentContent = "<hey></hey>";
  136. #if NET_2_0
  137. Assert.AreEqual ("<hey></hey>", xml.DocumentContent);
  138. #else
  139. Assert.AreEqual ("", xml.DocumentContent);
  140. #endif
  141. xml.TransformSource = "test.xsl";
  142. Assert.AreEqual (null, xml.Transform, "ID");
  143. }
  144. [Test] public void Xml_RenderEmpty ()
  145. {
  146. XmlPoker xml = new XmlPoker ();
  147. StringWriter sw = new StringWriter ();
  148. HtmlTextWriter hw = new HtmlTextWriter (sw);
  149. xml.DoRender (hw);
  150. Assert.AreEqual ("", sw.ToString (), "RE1");
  151. }
  152. [Test] public void Xml_SourcePrecedence ()
  153. {
  154. XmlPoker xml = new XmlPoker ();
  155. xml.DocumentContent = "<content></content>";
  156. XmlDocument xml_doc = new XmlDocument ();
  157. xml_doc.LoadXml ("<document></document>");
  158. xml.Document = xml_doc;
  159. //
  160. // Can not check the precendece for the file backend
  161. // because it requires a complete setup to check for the
  162. // file access permission to it (Control.MapPathSecure)
  163. //
  164. // xml.DocumentSource = "test.xml";
  165. //
  166. StringWriter sw = new StringWriter ();
  167. HtmlTextWriter hw = new HtmlTextWriter (sw);
  168. xml.DoRender (hw);
  169. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?><document></document>",
  170. sw.ToString (), "SP1");
  171. // Now compare the inline XML
  172. xml.DocumentContent = "<content></content>";
  173. sw = new StringWriter ();
  174. hw = new HtmlTextWriter (sw);
  175. xml.DoRender (hw);
  176. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?><content></content>",
  177. sw.ToString (), "SP1");
  178. }
  179. [Test] public void Xml_DefaultTrasnform ()
  180. {
  181. XmlPoker xml = new XmlPoker ();
  182. // For the actual transform, I was lazy, but
  183. // xsp's web_xml.aspx works.
  184. }
  185. [ExpectedException(typeof (HttpException))]
  186. [Test] public void Xml_InsertInvalid ()
  187. {
  188. XmlPoker xml = new XmlPoker ();
  189. xml.DoAdd ("hello");
  190. }
  191. [ExpectedException(typeof (NullReferenceException))]
  192. [Test] public void Xml_InsertInvalid2 ()
  193. {
  194. XmlPoker xml = new XmlPoker ();
  195. xml.DoAdd (null);
  196. }
  197. [Test] public void Xml_InsertValid ()
  198. {
  199. XmlPoker xml = new XmlPoker ();
  200. xml.DoAdd (new LiteralControl ("<test></test>"));
  201. }
  202. }
  203. }