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