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")]
37 using System.Collections;
38 using System.Collections.Specialized;
40 using System.Globalization;
43 using System.Web.UI.WebControls;
46 using NUnit.Framework;
47 using MonoTests.SystemWeb.Framework;
48 using MonoTests.stand_alone.WebHarness;
49 using System.Threading;
51 namespace MonoTests.System.Web.UI.WebControls
53 class DSPoker : XmlDataSource
59 public object SaveState () {
60 return SaveViewState ();
62 public void LoadState (object o) {
66 public HierarchicalDataSourceView DoGetHierarchicalView(string viewPath)
68 return base.GetHierarchicalView(viewPath);
71 public void DoOnTransforming (EventArgs e)
73 base.OnTransforming (e);
76 public void DoOnDataSourceChanged ()
78 base.OnDataSourceChanged (new EventArgs ());
83 public class XmlDataSourceTest
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>";
88 public void CopyTestResources ()
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");
99 public void Defaults ()
101 DSPoker p = new DSPoker ();
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");
110 Assert.AreEqual (null, p.TransformArgumentList, "A17");
114 public void Defaults_NotWorking ()
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");
124 public void Attributes ()
126 DSPoker p = new DSPoker ();
129 Assert.AreEqual (data, p.Data, "A1");
131 p.Transform = "transform";
132 Assert.AreEqual ("transform", p.Transform, "A2");
135 Assert.AreEqual ("xpath", p.XPath, "A3");
139 public void ViewState ()
141 // XXX weird... something odd going on with
142 // ViewState? or are none of these stored?
143 DSPoker p = new DSPoker ();
146 p.Transform = "transform";
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");
157 p.DataFile = "DataFile";
158 p.TransformFile = "TransformFile";
160 state = p.SaveState ();
161 copy = new DSPoker ();
163 copy.LoadState (state);
164 Assert.AreEqual ("", copy.DataFile, "A1");
165 Assert.AreEqual ("", copy.TransformFile, "A2");
171 private static int _testcounter;
172 private static bool _eventChecker;
173 private eventAssert ()
178 public static bool eventChecker
182 throw new NotImplementedException ();
186 _eventChecker = value;
190 static private void testAdded ()
193 _eventChecker = false;
196 public static void IsTrue (string msg)
198 Assert.IsTrue (_eventChecker, msg + "#" + _testcounter);
203 public static void IsFalse (string msg)
205 Assert.IsFalse (_eventChecker, msg + "#" + _testcounter);
212 public void XmlDataSource_DataSourceViewChanged ()
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
220 eventAssert.IsTrue ("XmlDataSource");
221 p.Transform = "transform";
222 eventAssert.IsTrue ("XmlDataSource");
224 eventAssert.IsTrue ("XmlDataSource");
225 p.DataFile = "DataFile";
226 eventAssert.IsTrue ("XmlDataSource");
227 p.TransformFile = "TransformFile";
228 eventAssert.IsTrue ("XmlDataSource");
231 void XmlDataSourceTest_DataSourceChanged (object sender, EventArgs e)
233 eventAssert.eventChecker = true;
238 [Category ("NunitWeb")]
239 public void DataFile ()
241 new WebTest (PageInvoker.CreateOnLoad (datafile)).Run ();
244 public static void datafile (Page p)
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>";
249 XmlDataSource ds = new XmlDataSource ();
250 p.Form.Controls.Add (ds);
251 ds.DataFile = "~/XMLDataSourceTest.xml";
253 string derivedxml = ((XmlDocument) ds.GetXmlDocument ()).InnerXml;
254 HtmlDiff.AssertAreEqual (originalxml, derivedxml, "Loading xml");
258 public void GetXmlDocument ()
260 DSPoker p = new DSPoker ();
262 XmlDocument doc = p.GetXmlDocument ();
263 HtmlDiff.AssertAreEqual (data, doc.InnerXml, "GetXmlDocument");
267 [Category ("NunitWeb")]
270 Page page = new Page ();
271 XmlDataSource ds = new XmlDataSource ();
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>
278 <first-name>Jane</first-name>
279 <last-name>Austen</last-name>
281 <price>24.95</price>""
283 <book genre=""novel"" publicationdate=""1985"" bk:ISBN=""0771008139"">
284 <title>The Handmaid's Tale</title>
286 <first-name>Margaret</first-name>
287 <last-name>Atwood</last-name>
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";
301 Assert.AreEqual (2, list0.Items.Count, "Before XPath elements");
303 ds.XPath = "/bookstore/book [title='Pride and Prejudice']";
304 list1.DataSourceID = "ds";
306 Assert.AreEqual (1, list1.Items.Count, "After XPath elements");
308 ds.XPath = "bookstore/book [@genre='novel']";
309 list2.DataSourceID = "ds";
311 Assert.AreEqual (2, list2.Items.Count, "After XPath property");
315 public void GetHierarchicalView ()
317 Page page = new Page ();
318 DSPoker ds = new DSPoker ();
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>
325 <first-name>Jane</first-name>
326 <last-name>Austen</last-name>
330 <book genre=""novel"" publicationdate=""1985"" bk:ISBN=""0771008139"">
331 <title>The Handmaid's Tale</title>
333 <first-name>Margaret</first-name>
334 <last-name>Atwood</last-name>
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");
351 [Category ("NunitWeb")]
352 public void Transform ()
354 string origin = @"<div>
359 <td><font color=""blue"">12345</font></td>
365 string result = new WebTest ("XMLDataSourceTest1.aspx").Run();
366 HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml(result) , "TransformFail");
370 [Category ("NunitWeb")]
371 public void TransformFile ()
373 string origin = @"<div><h2>Order</h2>
378 <td><font color=""blue"">12345</font></td>
385 string result = new WebTest ("XMLDataSourceTest2.aspx").Run ();
386 HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformFileFail");
390 [Category ("NunitWeb")]
391 public void TransformArgumentList ()
393 string origin = @"<div>
399 <td><font color=""blue"">12345purchased by: Mainsoft developers</font></td>
406 string result = new WebTest ("XMLDataSourceTest3.aspx").Run ();
407 HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformArgumentListFail");
411 [Category ("NunitWeb")]
414 string origin = @"<div>
415 <h2>BookStore</h2><hr>
419 <td><font color=""blue"">ThisIsATest</font></td>
425 <h2>BookStore</h2><hr>
429 <td><font color=""blue"">The Handmaid's Tale</font></td>
435 string result = new WebTest ("XMLDataSourceTest4.aspx").Run ();
436 HtmlDiff.AssertAreEqual (origin, HtmlDiff.GetControlFromPageHtml (result), "TransformArgumentListFail");
443 public void Events ()
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");
452 void p_Transforming (object sender, EventArgs e)
457 //TODO: This is implementation specific test - remove it.
459 [Category ("NotWorking")]
460 [ExpectedException (typeof (NullReferenceException))]
461 public void GetXmlDocumentException ()
463 DSPoker p = new DSPoker ();
468 public void CacheKeyContext ()
470 var xds = new XmlDataSource ();
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");
479 [TestFixtureTearDown]
480 public void TearDown ()