[bcl] Remove NET_4_0 defines from class libs
[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
34
35 using System;
36 using System.Collections;
37 using System.Collections.Specialized;
38 using System.IO;
39 using System.Globalization;
40 using System.Web;
41 using System.Web.UI;
42 using System.Web.UI.WebControls;
43 using System.Xml;
44 using System.Xml.Xsl;
45 using NUnit.Framework;
46 using MonoTests.SystemWeb.Framework;
47 using MonoTests.stand_alone.WebHarness;
48 using System.Threading;
49
50 namespace MonoTests.System.Web.UI.WebControls
51 {
52         class DSPoker : XmlDataSource
53         {
54                 public DSPoker () {
55                         TrackViewState ();
56                 }
57
58                 public object SaveState () {
59                         return SaveViewState ();
60                 }
61                 public void LoadState (object o) {
62                         LoadViewState (o);
63                 }
64
65                 public HierarchicalDataSourceView  DoGetHierarchicalView(string viewPath)
66                 {
67                          return base.GetHierarchicalView(viewPath);
68                 }
69
70                 public void DoOnTransforming (EventArgs e)
71                 {
72                         base.OnTransforming (e);
73                 }
74
75                 public void DoOnDataSourceChanged ()
76                 {
77                         base.OnDataSourceChanged (new EventArgs ());
78                 }
79         }
80
81         [TestFixture]
82         public class XmlDataSourceTest
83         {
84                 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>";
85
86                 [TestFixtureSetUp]
87                 public void CopyTestResources ()
88                 {
89                         WebTest.CopyResource (GetType (), "XMLDataSourceTest.xml", "XMLDataSourceTest.xml");
90                         WebTest.CopyResource (GetType (), "XMLDataSourceTest.xsl", "XMLDataSourceTest.xsl");
91                         WebTest.CopyResource (GetType (), "XMLDataSourceTest1.aspx", "XMLDataSourceTest1.aspx");
92                         WebTest.CopyResource (GetType (), "XMLDataSourceTest2.aspx", "XMLDataSourceTest2.aspx");
93                         WebTest.CopyResource (GetType (), "XMLDataSourceTest3.aspx", "XMLDataSourceTest3.aspx");
94                         WebTest.CopyResource (GetType (), "XMLDataSourceTest4.aspx", "XMLDataSourceTest4.aspx");
95                 }
96
97                 [Test]
98                 public void Defaults ()
99                 {
100                         DSPoker p = new DSPoker ();
101
102                         Assert.AreEqual ("", p.Data, "A4");
103                         Assert.AreEqual ("", p.DataFile, "A5");
104                         Assert.AreEqual ("", p.Transform, "A9");
105                         Assert.AreEqual ("", p.TransformFile, "A10");
106                         Assert.AreEqual ("", p.XPath, "A11");
107                         
108                         // Added
109                         Assert.AreEqual (null, p.TransformArgumentList, "A17");
110                 }
111
112                 [Test]
113                 public void Defaults_NotWorking ()
114                 {
115                         DSPoker p = new DSPoker ();
116                         Assert.AreEqual (0, p.CacheDuration, "A12");
117                         Assert.AreEqual (DataSourceCacheExpiry.Absolute, p.CacheExpirationPolicy, "A13");
118                         Assert.AreEqual ("", p.CacheKeyDependency, "A14");
119                         Assert.AreEqual (true, p.EnableCaching, "A15");
120                 }
121
122                 [Test]
123                 public void Attributes ()
124                 {
125                         DSPoker p = new DSPoker ();
126
127                         p.Data = data;
128                         Assert.AreEqual (data, p.Data, "A1");
129
130                         p.Transform = "transform";
131                         Assert.AreEqual ("transform", p.Transform, "A2");
132
133                         p.XPath = "xpath";
134                         Assert.AreEqual ("xpath", p.XPath, "A3");
135                 }
136
137                 [Test]
138                 public void ViewState ()
139                 {
140                         // XXX weird... something odd going on with
141                         // ViewState?  or are none of these stored?
142                         DSPoker p = new DSPoker ();
143
144                         p.Data = data;
145                         p.Transform = "transform";
146                         p.XPath = "xpath";
147
148                         object state = p.SaveState ();
149                         DSPoker copy = new DSPoker ();
150                         copy.LoadState (state);
151                         Assert.AreEqual ("", copy.Data, "A1");
152                         Assert.AreEqual ("", copy.Transform, "A2");
153                         Assert.AreEqual ("", copy.XPath, "A3");
154
155                         p = new DSPoker ();
156                         p.DataFile = "DataFile";
157                         p.TransformFile = "TransformFile";
158
159                         state = p.SaveState ();
160                         copy = new DSPoker ();
161
162                         copy.LoadState (state);
163                         Assert.AreEqual ("", copy.DataFile, "A1");
164                         Assert.AreEqual ("", copy.TransformFile, "A2");
165                 }
166
167                 #region help_results
168                 class eventAssert
169                 {
170                         private static int _testcounter;
171                         private static bool _eventChecker;
172                         private eventAssert ()
173                         {
174                                 _testcounter = 0;
175                         }
176
177                         public static bool eventChecker
178                         {
179                                 get
180                                 {
181                                         throw new NotImplementedException ();
182                                 }
183                                 set
184                                 {
185                                         _eventChecker = value;
186                                 }
187                         }
188
189                         static private void testAdded ()
190                         {
191                                 _testcounter++;
192                                 _eventChecker = false;
193                         }
194
195                         public static void IsTrue (string msg)
196                         {
197                                 Assert.IsTrue (_eventChecker, msg + "#" + _testcounter);
198                                 testAdded ();
199
200                         }
201
202                         public static void IsFalse (string msg)
203                         {
204                                 Assert.IsFalse (_eventChecker, msg + "#" + _testcounter);
205                                 testAdded ();
206                         }
207                 }
208                 #endregion
209
210                 [Test]
211                 public void XmlDataSource_DataSourceViewChanged ()
212                 {
213                         DSPoker p = new DSPoker ();
214                         ((IDataSource) p).DataSourceChanged += new EventHandler (XmlDataSourceTest_DataSourceChanged);
215                         p.DoOnDataSourceChanged ();
216                         eventAssert.IsTrue ("XmlDataSource"); // Assert include counter the first is zero
217
218                         p.Data = data;
219                         eventAssert.IsTrue ("XmlDataSource");
220                         p.Transform = "transform";
221                         eventAssert.IsTrue ("XmlDataSource");
222                         p.XPath = "xpath";
223                         eventAssert.IsTrue ("XmlDataSource");
224                         p.DataFile = "DataFile";
225                         eventAssert.IsTrue ("XmlDataSource");
226                         p.TransformFile = "TransformFile";
227                         eventAssert.IsTrue ("XmlDataSource");
228                 }
229
230                 void XmlDataSourceTest_DataSourceChanged (object sender, EventArgs e)
231                 {
232                         eventAssert.eventChecker = true;
233                         
234                 }
235
236                 [Test]
237                 [Category ("NunitWeb")]
238                 public void DataFile ()
239                 {
240                         new WebTest (PageInvoker.CreateOnLoad (datafile)).Run ();
241                 }
242
243                 public static void datafile (Page p)
244                 {
245                         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>""
246                         </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>";
247
248                         XmlDataSource ds = new XmlDataSource ();
249                         p.Form.Controls.Add (ds);
250                         ds.DataFile = "~/XMLDataSourceTest.xml";
251                         ds.DataBind ();
252                         string derivedxml = ((XmlDocument) ds.GetXmlDocument ()).InnerXml;
253                         HtmlDiff.AssertAreEqual (originalxml, derivedxml, "Loading xml");
254                 }
255
256                 [Test]
257                 public void GetXmlDocument ()
258                 {
259                         DSPoker p = new DSPoker ();
260                         p.Data = data;
261                         XmlDocument doc = p.GetXmlDocument ();
262                         HtmlDiff.AssertAreEqual (data, doc.InnerXml, "GetXmlDocument");
263                 }
264
265                 [Test]
266                 [Category ("NunitWeb")]
267                 public void XPath ()
268                 {
269                         Page page = new Page ();
270                         XmlDataSource ds = new XmlDataSource ();
271                         ds.ID = "ds";
272                         ds.Data = @"<?xml version=""1.0"" encoding=""utf-8""?>
273                                         <bookstore xmlns:bk=""urn:samples"">
274                                           <book genre=""novel"" publicationdate=""1999"" bk:ISBN=""0192100262"">
275                                             <title>Pride and Prejudice</title>
276                                             <author>
277                                               <first-name>Jane</first-name>
278                                               <last-name>Austen</last-name>
279                                             </author>
280                                             <price>24.95</price>""
281                                           </book>
282                                           <book genre=""novel"" publicationdate=""1985"" bk:ISBN=""0771008139"">
283                                             <title>The Handmaid's Tale</title>
284                                             <author>
285                                               <first-name>Margaret</first-name>
286                                               <last-name>Atwood</last-name>
287                                             </author>
288                                             <price>29.95</price>
289                                           </book>
290                                         </bookstore>";
291                         DataList list0 = new DataList ();
292                         DataList list1 = new DataList ();
293                         DataList list2 = new DataList ();
294                         page.Controls.Add (list0);
295                         page.Controls.Add (list1);
296                         page.Controls.Add (list2);
297                         page.Controls.Add (ds);
298                         list0.DataSourceID = "ds";
299                         list0.DataBind ();
300                         Assert.AreEqual (2, list0.Items.Count, "Before XPath elements");
301
302                         ds.XPath = "/bookstore/book [title='Pride and Prejudice']";
303                         list1.DataSourceID = "ds";
304                         list1.DataBind ();
305                         Assert.AreEqual (1, list1.Items.Count, "After XPath elements");
306
307                         ds.XPath = "bookstore/book [@genre='novel']";
308                         list2.DataSourceID = "ds";
309                         list2.DataBind ();
310                         Assert.AreEqual (2, list2.Items.Count, "After XPath property");
311                 }
312
313                 [Test]
314                 public void GetHierarchicalView ()
315                 {
316                         Page page = new Page ();
317                         DSPoker ds = new DSPoker ();
318                         ds.ID = "ds";
319                         ds.Data = @"<?xml version=""1.0"" encoding=""utf-8""?>
320                                         <bookstore xmlns:bk=""urn:samples"">
321                                           <book genre=""novel"" publicationdate=""1999"" bk:ISBN=""0192100262"">
322                                             <title>Pride and Prejudice</title>
323                                             <author>
324                                               <first-name>Jane</first-name>
325                                               <last-name>Austen</last-name>
326                                             </author>
327                                             <price>24.95</price>
328                                           </book>
329                                           <book genre=""novel"" publicationdate=""1985"" bk:ISBN=""0771008139"">
330                                             <title>The Handmaid's Tale</title>
331                                             <author>
332                                               <first-name>Margaret</first-name>
333                                               <last-name>Atwood</last-name>
334                                             </author>
335                                             <price>29.95</price>
336                                           </book>
337                                         </bookstore>";
338                         HierarchicalDataSourceView view = ds.DoGetHierarchicalView ("");
339                         IHierarchicalEnumerable num = view.Select ();
340                         foreach (object obj in num) {
341                                 IHierarchyData hdata = num.GetHierarchyData (obj);
342                                 XmlElement element = (XmlElement) hdata.Item;
343                                 Assert.AreEqual ("bookstore", element.Name, "RootElementName");
344                                 Assert.AreEqual ("Pride and PrejudiceJaneAusten24.95The Handmaid's TaleMargaretAtwood29.95", element.InnerText, "InnerText");
345                                 Assert.AreEqual (2, element.ChildNodes.Count, "ChildElementNodes");
346                         }
347                 }
348
349                 [Test]
350                 [Category ("NunitWeb")]
351                 public void Transform ()
352                 {
353                         string origin = @"<div>
354                                                 <h2>Order</h2><hr>
355                                                 <table>
356                                                   <tr>
357                                                     <td>Customer</td>
358                                                     <td><font color=""blue"">12345</font></td>
359                                                     <td>Todd</td>
360                                                     <td>Rowe</td>
361                                                   </tr>
362                                                 </table>
363                                                 <hr></div>";
364                         string result = new WebTest ("XMLDataSourceTest1.aspx").Run();
365                         HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml(result) , "TransformFail");
366                 }
367
368                 [Test]
369                 [Category ("NunitWeb")]
370                 public void TransformFile ()
371                 {
372                         string origin = @"<div><h2>Order</h2>
373                                                 <hr>
374                                                 <table>
375                                                   <tr>
376                                                     <td>Customer</td>
377                                                     <td><font color=""blue"">12345</font></td>
378                                                     <td>Todd</td>
379                                                     <td>Rowe</td>
380                                                   </tr>
381                                                 </table>
382                                                 <hr>
383                                           </div>";
384                         string result = new WebTest ("XMLDataSourceTest2.aspx").Run ();
385                         HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformFileFail");
386                 }
387
388                 [Test]
389                 [Category ("NunitWeb")]
390                 public void TransformArgumentList ()
391                 {
392                         string origin = @"<div>
393                                               <h2>Order</h2>
394                                                 <hr>
395                                                 <table>
396                                                   <tr>
397                                                     <td>Customer</td>
398                                                     <td><font color=""blue"">12345purchased by: Mainsoft developers</font></td>
399                                                     <td>Todd</td>
400                                                     <td>Rowe</td>
401                                                   </tr>
402                                                 </table>
403                                                 <hr>
404                                         </div>";
405                         string result = new WebTest ("XMLDataSourceTest3.aspx").Run ();
406                         HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformArgumentListFail");
407                 }
408
409                 [Test]
410                 [Category ("NunitWeb")]
411                 public void Save ()
412                 {
413                         string origin = @"<div>
414                                                 <h2>BookStore</h2><hr>
415                                                 <table>
416                                                   <tr>
417                                                     <td>Book</td>
418                                                     <td><font color=""blue"">ThisIsATest</font></td>
419                                                     <td></td>
420                                                     <td></td>
421                                                     <td>24.95</td>
422                                                   </tr>
423                                                 </table><hr>
424                                                 <h2>BookStore</h2><hr>
425                                                 <table>
426                                                   <tr>
427                                                     <td>Book</td>
428                                                     <td><font color=""blue"">The Handmaid's Tale</font></td>
429                                                     <td></td>
430                                                     <td></td>
431                                                     <td>29.95</td>
432                                                   </tr>
433                                                 </table><hr></div>";
434                         string result = new WebTest ("XMLDataSourceTest4.aspx").Run ();
435                         HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformArgumentListFail");
436                 }
437
438                 //events test
439                 bool checker;
440
441                 [Test]
442                 public void Events ()
443                 {
444                         DSPoker p = new DSPoker ();
445                         p.Transforming += new EventHandler (p_Transforming);
446                         Assert.AreEqual (false, checker, "BeforeTransformingEvent");
447                         p.DoOnTransforming (new EventArgs ());
448                         Assert.AreEqual (true, checker, "AfterTransformingEvent");
449                 }
450
451                 void p_Transforming (object sender, EventArgs e)
452                 {
453                         checker = true;
454                 }
455
456                 //TODO: This is implementation specific test - remove it.
457                 [Test]
458                 [Category ("NotWorking")]
459                 [ExpectedException (typeof (NullReferenceException))]
460                 public void GetXmlDocumentException ()
461                 {
462                         DSPoker p = new DSPoker ();
463                         p.GetXmlDocument ();
464                 }
465                 [Test]
466                 public void CacheKeyContext ()
467                 {
468                         var xds = new XmlDataSource ();
469
470                         Assert.AreEqual (String.Empty, xds.CacheKeyContext, "#A1");
471                         xds.CacheKeyContext = null;
472                         Assert.AreEqual (String.Empty, xds.CacheKeyContext, "#A2");
473                         xds.CacheKeyContext = "MyKey";
474                         Assert.AreEqual ("MyKey", xds.CacheKeyContext, "#A1");
475                 }
476                 [TestFixtureTearDown]
477                 public void TearDown ()
478                 {
479                         WebTest.Unload ();
480                 }
481         }       
482 }
483