XmlDataSourceTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //
  2. // Tests for System.Web.UI.WebControls.XmlDataSource.cs
  3. //
  4. // Author:
  5. // Chris Toshok ([email protected])
  6. // Yoni Klain ([email protected])
  7. //
  8. //
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. // NOTE: INCLUDE 2 CATEGORIES [Category ("NunitWeb")];[Category ("NotWorking")]
  30. #if NET_2_0
  31. using System;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.IO;
  35. using System.Globalization;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. using System.Xml;
  40. using System.Xml.Xsl;
  41. using NUnit.Framework;
  42. using MonoTests.SystemWeb.Framework;
  43. using MonoTests.stand_alone.WebHarness;
  44. using System.Threading;
  45. namespace MonoTests.System.Web.UI.WebControls
  46. {
  47. class DSPoker : XmlDataSource
  48. {
  49. public DSPoker () {
  50. TrackViewState ();
  51. }
  52. public object SaveState () {
  53. return SaveViewState ();
  54. }
  55. public void LoadState (object o) {
  56. LoadViewState (o);
  57. }
  58. public HierarchicalDataSourceView DoGetHierarchicalView(string viewPath)
  59. {
  60. return base.GetHierarchicalView(viewPath);
  61. }
  62. public void DoOnTransforming (EventArgs e)
  63. {
  64. base.OnTransforming (e);
  65. }
  66. }
  67. [TestFixture]
  68. public class XmlDataSourceTest
  69. {
  70. string data = @"<?xml version=""1.0"" encoding=""utf-8""?><IranHistoricalPlaces name=""places""><Place name=""Taghe Bostan""><City>Kermanshah</City><Antiquity>2000</Antiquity></Place><Place name=""Persepolis""><City>Shiraz</City><Antiquity>2500</Antiquity></Place></IranHistoricalPlaces>";
  71. [TestFixtureSetUp]
  72. public void CopyTestResources ()
  73. {
  74. #if DOT_NET
  75. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.XMLDataSourceTest.xml", "XMLDataSourceTest.xml");
  76. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.XMLDataSourceTest.xsl", "XMLDataSourceTest.xsl");
  77. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.XMLDataSourceTest1.aspx", "XMLDataSourceTest1.aspx");
  78. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.XMLDataSourceTest2.aspx", "XMLDataSourceTest2.aspx");
  79. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.XMLDataSourceTest3.aspx", "XMLDataSourceTest3.aspx");
  80. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.XMLDataSourceTest4.aspx", "XMLDataSourceTest4.aspx");
  81. #else
  82. WebTest.CopyResource (GetType (), "XMLDataSourceTest.xml", "XMLDataSourceTest.xml");
  83. WebTest.CopyResource (GetType (), "XMLDataSourceTest.xsl", "XMLDataSourceTest.xsl");
  84. WebTest.CopyResource (GetType (), "XMLDataSourceTest1.aspx", "XMLDataSourceTest1.aspx");
  85. WebTest.CopyResource (GetType (), "XMLDataSourceTest2.aspx", "XMLDataSourceTest2.aspx");
  86. WebTest.CopyResource (GetType (), "XMLDataSourceTest3.aspx", "XMLDataSourceTest3.aspx");
  87. WebTest.CopyResource (GetType (), "XMLDataSourceTest4.aspx", "XMLDataSourceTest4.aspx");
  88. #endif
  89. }
  90. [Test]
  91. public void Defaults ()
  92. {
  93. DSPoker p = new DSPoker ();
  94. Assert.AreEqual ("", p.Data, "A4");
  95. Assert.AreEqual ("", p.DataFile, "A5");
  96. Assert.AreEqual ("", p.Transform, "A9");
  97. Assert.AreEqual ("", p.TransformFile, "A10");
  98. Assert.AreEqual ("", p.XPath, "A11");
  99. // Added
  100. Assert.AreEqual (null, p.TransformArgumentList, "A17");
  101. }
  102. [Test]
  103. public void Defaults_NotWorking ()
  104. {
  105. DSPoker p = new DSPoker ();
  106. Assert.AreEqual (0, p.CacheDuration, "A12");
  107. Assert.AreEqual (DataSourceCacheExpiry.Absolute, p.CacheExpirationPolicy, "A13");
  108. Assert.AreEqual ("", p.CacheKeyDependency, "A14");
  109. Assert.AreEqual (true, p.EnableCaching, "A15");
  110. }
  111. [Test]
  112. public void Attributes ()
  113. {
  114. DSPoker p = new DSPoker ();
  115. p.Data = data;
  116. Assert.AreEqual (data, p.Data, "A1");
  117. p.Transform = "transform";
  118. Assert.AreEqual ("transform", p.Transform, "A2");
  119. p.XPath = "xpath";
  120. Assert.AreEqual ("xpath", p.XPath, "A3");
  121. }
  122. [Test]
  123. public void ViewState ()
  124. {
  125. // XXX weird... something odd going on with
  126. // ViewState? or are none of these stored?
  127. DSPoker p = new DSPoker ();
  128. p.Data = data;
  129. p.Transform = "transform";
  130. p.XPath = "xpath";
  131. object state = p.SaveState ();
  132. DSPoker copy = new DSPoker ();
  133. copy.LoadState (state);
  134. Assert.AreEqual ("", copy.Data, "A1");
  135. Assert.AreEqual ("", copy.Transform, "A2");
  136. Assert.AreEqual ("", copy.XPath, "A3");
  137. p = new DSPoker ();
  138. p.DataFile = "DataFile";
  139. p.TransformFile = "TransformFile";
  140. state = p.SaveState ();
  141. copy = new DSPoker ();
  142. copy.LoadState (state);
  143. Assert.AreEqual ("", copy.DataFile, "A1");
  144. Assert.AreEqual ("", copy.TransformFile, "A2");
  145. }
  146. [Test]
  147. [Category ("NunitWeb")]
  148. public void DataFile ()
  149. {
  150. new WebTest (PageInvoker.CreateOnLoad (datafile)).Run ();
  151. }
  152. public static void datafile (Page p)
  153. {
  154. string originalxml = @"<?xml version=""1.0"" encoding=""utf-8""?><bookstore xmlns:bk=""urn:samples""><book genre=""novel"" publicationdate=""1999"" bk:ISBN=""0192100262""><title>Pride and Prejudice</title><author><first-name>Jane</first-name><last-name>Austen</last-name></author><price>24.95</price>""
  155. </book><book genre=""novel"" publicationdate=""1985"" bk:ISBN=""0771008139""><title>The Handmaid's Tale</title><author><first-name>Margaret</first-name><last-name>Atwood</last-name></author><price>29.95</price></book></bookstore>";
  156. XmlDataSource ds = new XmlDataSource ();
  157. p.Form.Controls.Add (ds);
  158. ds.DataFile = "~/XMLDataSourceTest.xml";
  159. ds.DataBind ();
  160. string derivedxml = ((XmlDocument) ds.GetXmlDocument ()).InnerXml;
  161. HtmlDiff.AssertAreEqual (originalxml, derivedxml, "Loading xml");
  162. }
  163. [Test]
  164. public void GetXmlDocument ()
  165. {
  166. DSPoker p = new DSPoker ();
  167. p.Data = data;
  168. XmlDocument doc = p.GetXmlDocument ();
  169. HtmlDiff.AssertAreEqual (data, doc.InnerXml, "GetXmlDocument");
  170. }
  171. [Test]
  172. [Category ("NunitWeb")]
  173. public void XPath ()
  174. {
  175. Page page = new Page ();
  176. XmlDataSource ds = new XmlDataSource ();
  177. ds.ID = "ds";
  178. ds.Data = @"<?xml version=""1.0"" encoding=""utf-8""?>
  179. <bookstore xmlns:bk=""urn:samples"">
  180. <book genre=""novel"" publicationdate=""1999"" bk:ISBN=""0192100262"">
  181. <title>Pride and Prejudice</title>
  182. <author>
  183. <first-name>Jane</first-name>
  184. <last-name>Austen</last-name>
  185. </author>
  186. <price>24.95</price>""
  187. </book>
  188. <book genre=""novel"" publicationdate=""1985"" bk:ISBN=""0771008139"">
  189. <title>The Handmaid's Tale</title>
  190. <author>
  191. <first-name>Margaret</first-name>
  192. <last-name>Atwood</last-name>
  193. </author>
  194. <price>29.95</price>
  195. </book>
  196. </bookstore>";
  197. DataList list0 = new DataList ();
  198. DataList list1 = new DataList ();
  199. DataList list2 = new DataList ();
  200. page.Controls.Add (list0);
  201. page.Controls.Add (list1);
  202. page.Controls.Add (list2);
  203. page.Controls.Add (ds);
  204. list0.DataSourceID = "ds";
  205. list0.DataBind ();
  206. Assert.AreEqual (2, list0.Items.Count, "Before XPath elements");
  207. ds.XPath = "/bookstore/book [title='Pride and Prejudice']";
  208. list1.DataSourceID = "ds";
  209. list1.DataBind ();
  210. Assert.AreEqual (1, list1.Items.Count, "After XPath elements");
  211. ds.XPath = "bookstore/book [@genre='novel']";
  212. list2.DataSourceID = "ds";
  213. list2.DataBind ();
  214. Assert.AreEqual (2, list2.Items.Count, "After XPath property");
  215. }
  216. [Test]
  217. public void GetHierarchicalView ()
  218. {
  219. Page page = new Page ();
  220. DSPoker ds = new DSPoker ();
  221. ds.ID = "ds";
  222. ds.Data = @"<?xml version=""1.0"" encoding=""utf-8""?>
  223. <bookstore xmlns:bk=""urn:samples"">
  224. <book genre=""novel"" publicationdate=""1999"" bk:ISBN=""0192100262"">
  225. <title>Pride and Prejudice</title>
  226. <author>
  227. <first-name>Jane</first-name>
  228. <last-name>Austen</last-name>
  229. </author>
  230. <price>24.95</price>
  231. </book>
  232. <book genre=""novel"" publicationdate=""1985"" bk:ISBN=""0771008139"">
  233. <title>The Handmaid's Tale</title>
  234. <author>
  235. <first-name>Margaret</first-name>
  236. <last-name>Atwood</last-name>
  237. </author>
  238. <price>29.95</price>
  239. </book>
  240. </bookstore>";
  241. HierarchicalDataSourceView view = ds.DoGetHierarchicalView ("");
  242. IHierarchicalEnumerable num = view.Select ();
  243. foreach (object obj in num) {
  244. IHierarchyData hdata = num.GetHierarchyData (obj);
  245. XmlElement element = (XmlElement) hdata.Item;
  246. Assert.AreEqual ("bookstore", element.Name, "RootElementName");
  247. Assert.AreEqual ("Pride and PrejudiceJaneAusten24.95The Handmaid's TaleMargaretAtwood29.95", element.InnerText, "InnerText");
  248. Assert.AreEqual (2, element.ChildNodes.Count, "ChildElementNodes");
  249. }
  250. }
  251. [Test]
  252. [Category ("NunitWeb")]
  253. public void Transform ()
  254. {
  255. string origin = @"<div>
  256. <h2>Order</h2><hr>
  257. <table>
  258. <tr>
  259. <td>Customer</td>
  260. <td><font color=""blue"">12345</font></td>
  261. <td>Todd</td>
  262. <td>Rowe</td>
  263. </tr>
  264. </table>
  265. <hr></div>";
  266. string result = new WebTest ("XMLDataSourceTest1.aspx").Run();
  267. HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml(result) , "TransformFail");
  268. }
  269. [Test]
  270. [Category ("NunitWeb")]
  271. public void TransformFile ()
  272. {
  273. string origin = @"<div><h2>Order</h2>
  274. <hr>
  275. <table>
  276. <tr>
  277. <td>Customer</td>
  278. <td><font color=""blue"">12345</font></td>
  279. <td>Todd</td>
  280. <td>Rowe</td>
  281. </tr>
  282. </table>
  283. <hr>
  284. </div>";
  285. string result = new WebTest ("XMLDataSourceTest2.aspx").Run ();
  286. HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformFileFail");
  287. }
  288. [Test]
  289. [Category ("NunitWeb")]
  290. public void TransformArgumentList ()
  291. {
  292. string origin = @"<div>
  293. <h2>Order</h2>
  294. <hr>
  295. <table>
  296. <tr>
  297. <td>Customer</td>
  298. <td><font color=""blue"">12345purchased by: Mainsoft developers</font></td>
  299. <td>Todd</td>
  300. <td>Rowe</td>
  301. </tr>
  302. </table>
  303. <hr>
  304. </div>";
  305. string result = new WebTest ("XMLDataSourceTest3.aspx").Run ();
  306. HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformArgumentListFail");
  307. }
  308. [Test]
  309. [Category ("NunitWeb")]
  310. #if TARGET_JVM
  311. [Category ("NotWorking")] // can not write into file located inside jar
  312. #endif
  313. public void Save ()
  314. {
  315. string origin = @"<div>
  316. <h2>BookStore</h2><hr>
  317. <table>
  318. <tr>
  319. <td>Book</td>
  320. <td><font color=""blue"">ThisIsATest</font></td>
  321. <td></td>
  322. <td></td>
  323. <td>24.95</td>
  324. </tr>
  325. </table><hr>
  326. <h2>BookStore</h2><hr>
  327. <table>
  328. <tr>
  329. <td>Book</td>
  330. <td><font color=""blue"">The Handmaid's Tale</font></td>
  331. <td></td>
  332. <td></td>
  333. <td>29.95</td>
  334. </tr>
  335. </table><hr></div>";
  336. string result = new WebTest ("XMLDataSourceTest4.aspx").Run ();
  337. HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformArgumentListFail");
  338. }
  339. //events test
  340. bool checker;
  341. [Test]
  342. public void Events ()
  343. {
  344. DSPoker p = new DSPoker ();
  345. p.Transforming += new EventHandler (p_Transforming);
  346. Assert.AreEqual (false, checker, "BeforeTransformingEvent");
  347. p.DoOnTransforming (new EventArgs ());
  348. Assert.AreEqual (true, checker, "AfterTransformingEvent");
  349. }
  350. void p_Transforming (object sender, EventArgs e)
  351. {
  352. checker = true;
  353. }
  354. //TODO: This is implementation specific test - remove it.
  355. [Test]
  356. [Category ("NotWorking")]
  357. [ExpectedException (typeof (NullReferenceException))]
  358. public void GetXmlDocumentException ()
  359. {
  360. DSPoker p = new DSPoker ();
  361. p.GetXmlDocument ();
  362. }
  363. [TestFixtureTearDown]
  364. public void TearDown ()
  365. {
  366. WebTest.Unload ();
  367. }
  368. }
  369. }
  370. #endif