2 // Tests for System.Web.UI.WebControls.XmlDataSource.cs
5 // Chris Toshok (toshok@novell.com)
6 // Yoni Klain (yonik@mainsoft.com)
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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")]
36 using System.Collections;
37 using System.Collections.Specialized;
39 using System.Globalization;
42 using System.Web.UI.WebControls;
45 using NUnit.Framework;
46 using MonoTests.SystemWeb.Framework;
47 using MonoTests.stand_alone.WebHarness;
48 using System.Threading;
50 namespace MonoTests.System.Web.UI.WebControls
52 class DSPoker : XmlDataSource
58 public object SaveState () {
59 return SaveViewState ();
61 public void LoadState (object o) {
65 public HierarchicalDataSourceView DoGetHierarchicalView(string viewPath)
67 return base.GetHierarchicalView(viewPath);
70 public void DoOnTransforming (EventArgs e)
72 base.OnTransforming (e);
75 public void DoOnDataSourceChanged ()
77 base.OnDataSourceChanged (new EventArgs ());
82 public class XmlDataSourceTest
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>";
87 public void CopyTestResources ()
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");
98 public void Defaults ()
100 DSPoker p = new DSPoker ();
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");
109 Assert.AreEqual (null, p.TransformArgumentList, "A17");
113 public void Defaults_NotWorking ()
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");
123 public void Attributes ()
125 DSPoker p = new DSPoker ();
128 Assert.AreEqual (data, p.Data, "A1");
130 p.Transform = "transform";
131 Assert.AreEqual ("transform", p.Transform, "A2");
134 Assert.AreEqual ("xpath", p.XPath, "A3");
138 public void ViewState ()
140 // XXX weird... something odd going on with
141 // ViewState? or are none of these stored?
142 DSPoker p = new DSPoker ();
145 p.Transform = "transform";
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");
156 p.DataFile = "DataFile";
157 p.TransformFile = "TransformFile";
159 state = p.SaveState ();
160 copy = new DSPoker ();
162 copy.LoadState (state);
163 Assert.AreEqual ("", copy.DataFile, "A1");
164 Assert.AreEqual ("", copy.TransformFile, "A2");
170 private static int _testcounter;
171 private static bool _eventChecker;
172 private eventAssert ()
177 public static bool eventChecker
181 throw new NotImplementedException ();
185 _eventChecker = value;
189 static private void testAdded ()
192 _eventChecker = false;
195 public static void IsTrue (string msg)
197 Assert.IsTrue (_eventChecker, msg + "#" + _testcounter);
202 public static void IsFalse (string msg)
204 Assert.IsFalse (_eventChecker, msg + "#" + _testcounter);
211 public void XmlDataSource_DataSourceViewChanged ()
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
219 eventAssert.IsTrue ("XmlDataSource");
220 p.Transform = "transform";
221 eventAssert.IsTrue ("XmlDataSource");
223 eventAssert.IsTrue ("XmlDataSource");
224 p.DataFile = "DataFile";
225 eventAssert.IsTrue ("XmlDataSource");
226 p.TransformFile = "TransformFile";
227 eventAssert.IsTrue ("XmlDataSource");
230 void XmlDataSourceTest_DataSourceChanged (object sender, EventArgs e)
232 eventAssert.eventChecker = true;
237 [Category ("NunitWeb")]
238 public void DataFile ()
240 new WebTest (PageInvoker.CreateOnLoad (datafile)).Run ();
243 public static void datafile (Page p)
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>";
248 XmlDataSource ds = new XmlDataSource ();
249 p.Form.Controls.Add (ds);
250 ds.DataFile = "~/XMLDataSourceTest.xml";
252 string derivedxml = ((XmlDocument) ds.GetXmlDocument ()).InnerXml;
253 HtmlDiff.AssertAreEqual (originalxml, derivedxml, "Loading xml");
257 public void GetXmlDocument ()
259 DSPoker p = new DSPoker ();
261 XmlDocument doc = p.GetXmlDocument ();
262 HtmlDiff.AssertAreEqual (data, doc.InnerXml, "GetXmlDocument");
266 [Category ("NunitWeb")]
269 Page page = new Page ();
270 XmlDataSource ds = new XmlDataSource ();
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>
277 <first-name>Jane</first-name>
278 <last-name>Austen</last-name>
280 <price>24.95</price>""
282 <book genre=""novel"" publicationdate=""1985"" bk:ISBN=""0771008139"">
283 <title>The Handmaid's Tale</title>
285 <first-name>Margaret</first-name>
286 <last-name>Atwood</last-name>
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";
300 Assert.AreEqual (2, list0.Items.Count, "Before XPath elements");
302 ds.XPath = "/bookstore/book [title='Pride and Prejudice']";
303 list1.DataSourceID = "ds";
305 Assert.AreEqual (1, list1.Items.Count, "After XPath elements");
307 ds.XPath = "bookstore/book [@genre='novel']";
308 list2.DataSourceID = "ds";
310 Assert.AreEqual (2, list2.Items.Count, "After XPath property");
314 public void GetHierarchicalView ()
316 Page page = new Page ();
317 DSPoker ds = new DSPoker ();
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>
324 <first-name>Jane</first-name>
325 <last-name>Austen</last-name>
329 <book genre=""novel"" publicationdate=""1985"" bk:ISBN=""0771008139"">
330 <title>The Handmaid's Tale</title>
332 <first-name>Margaret</first-name>
333 <last-name>Atwood</last-name>
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");
350 [Category ("NunitWeb")]
351 public void Transform ()
353 string origin = @"<div>
358 <td><font color=""blue"">12345</font></td>
364 string result = new WebTest ("XMLDataSourceTest1.aspx").Run();
365 HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml(result) , "TransformFail");
369 [Category ("NunitWeb")]
370 public void TransformFile ()
372 string origin = @"<div><h2>Order</h2>
377 <td><font color=""blue"">12345</font></td>
384 string result = new WebTest ("XMLDataSourceTest2.aspx").Run ();
385 HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformFileFail");
389 [Category ("NunitWeb")]
390 public void TransformArgumentList ()
392 string origin = @"<div>
398 <td><font color=""blue"">12345purchased by: Mainsoft developers</font></td>
405 string result = new WebTest ("XMLDataSourceTest3.aspx").Run ();
406 HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformArgumentListFail");
410 [Category ("NunitWeb")]
413 string origin = @"<div>
414 <h2>BookStore</h2><hr>
418 <td><font color=""blue"">ThisIsATest</font></td>
424 <h2>BookStore</h2><hr>
428 <td><font color=""blue"">The Handmaid's Tale</font></td>
434 string result = new WebTest ("XMLDataSourceTest4.aspx").Run ();
435 HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformArgumentListFail");
442 public void Events ()
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");
451 void p_Transforming (object sender, EventArgs e)
456 //TODO: This is implementation specific test - remove it.
458 [Category ("NotWorking")]
459 [ExpectedException (typeof (NullReferenceException))]
460 public void GetXmlDocumentException ()
462 DSPoker p = new DSPoker ();
466 public void CacheKeyContext ()
468 var xds = new XmlDataSource ();
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");
476 [TestFixtureTearDown]
477 public void TearDown ()