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