2 // PagedDataSourceTest.cs
5 // Duncan Mak (duncan@novell.com)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using NUnit.Framework;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Web.UI.WebControls;
36 using System.ComponentModel;
38 namespace MonoTests.System.Web.UI.WebControls {
41 public class PagedDataSourceTest {
48 ds = new PagedDataSource ();
51 public void SetUpTest ()
53 Assert.AreEqual (10, ds.PageSize);
54 Assert.IsFalse (ds.AllowPaging);
55 Assert.AreEqual (0, ds.CurrentPageIndex);
56 Assert.IsFalse (ds.AllowCustomPaging);
57 Assert.AreEqual (0, ds.VirtualCount);
64 ds.AllowPaging = false;
65 ds.CurrentPageIndex = 0;
66 ds.AllowCustomPaging = false;
70 void SetSource (IEnumerable source)
73 ds.DataSource = source;
77 public void GetItemProperties ()
79 PagedDataSource ds = new PagedDataSource ();
80 DataTable table = new DataTable ();
82 table.Columns.Add (new DataColumn ("one", typeof (string)));
83 table.Columns.Add (new DataColumn ("two", typeof (string)));
84 table.Columns.Add (new DataColumn ("three", typeof (string)));
86 ds.DataSource = new DataView (table);
87 PropertyDescriptorCollection props = ds.GetItemProperties (null);
89 Assert.AreEqual (props.Count, 3, "A1");
90 Assert.AreEqual (props [0].Name, "one", "A2");
91 Assert.AreEqual (props [1].Name, "two", "A3");
92 Assert.AreEqual (props [2].Name, "three", "A4");
94 ds.DataSource = new ArrayList ();
95 props = ds.GetItemProperties (null);
96 Assert.AreEqual (props, null, "A5");
100 public void GetEnumeratorTest ()
102 // Found out that there are 3 possibilities
103 // for GetEnumerator () from this test.
104 // One for ICollection, one for IList and otherwise, it uses the DataSource directly
106 // Hashtable implements ICollection
107 SetSource (new Hashtable ());
108 //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
110 // IList implementations
111 SetSource (new int [] { 1, 2, 3, 4, 5 });
112 //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
114 SetSource (new ArrayList ().ToArray ());
115 //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
118 SetSource (new MyEnumerable ());
121 public class MyEnumerable : IEnumerable, IEnumerator
123 IEnumerator IEnumerable.GetEnumerator () { return this; }
124 object IEnumerator.Current { get { return null; } }
125 bool IEnumerator.MoveNext () { return false; }
126 void IEnumerator.Reset () {}
130 public void FirstIndexInPageTest ()
133 Assert.AreEqual (0, ds.FirstIndexInPage);
135 SetSource (new int [] { 1, 2, 3, 4, 5 });
136 ds.AllowPaging = false;
137 Assert.AreEqual (0, ds.FirstIndexInPage);
139 ds.AllowCustomPaging = false;
140 Assert.AreEqual (0, ds.FirstIndexInPage);
142 ds.AllowPaging = true;
143 ds.CurrentPageIndex = 10;
145 Assert.AreEqual (ds.CurrentPageIndex * ds.PageSize, ds.FirstIndexInPage);
149 public void PageCountTest ()
152 Assert.AreEqual (0, ds.PageCount, "A1");
154 SetSource (new int [] {});
155 ds.AllowPaging = false;
156 Assert.AreEqual (1, ds.PageCount, "A2");
159 Assert.AreEqual (1, ds.PageCount, "A3");
161 SetSource (new int [] { 1, 2, 3, 4, 5 });
162 ds.AllowPaging = true;
164 Assert.AreEqual (1, ds.PageCount, "A4");
166 SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
167 ds.AllowPaging = true;
169 Assert.AreEqual (2, ds.PageCount, "A5");
171 SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
173 ds.AllowPaging = true;
174 Assert.AreEqual (3, ds.PageCount, "A6");
176 SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
177 ds.AllowPaging = true;
179 Assert.AreEqual (4, ds.PageCount, "A7");
183 public void CountTest ()
186 Assert.AreEqual (0, ds.Count);
188 SetSource (new int [] { 1, 2, 3, 4, 5 });
189 ds.AllowPaging = true;
191 ds.AllowCustomPaging = true;
192 Assert.AreEqual (ds.PageSize, ds.Count);
194 // ds.AllowCustomPaging = false;
195 // ds.CurrentPageIndex = ds.PageCount;
197 // Assert.AreEqual (ds.FirstIndexInPage - ds.DataSourceCount, ds.Count);
199 // ds.AllowPaging = false;
200 // Assert.AreEqual (ds.DataSourceCount, ds.Count);
204 public void IsFirstPageTest ()
206 ds.AllowPaging = false;
207 ds.CurrentPageIndex = 100;
208 Assert.IsTrue (ds.IsFirstPage);
210 ds.AllowPaging = true;
211 ds.CurrentPageIndex = 0;
212 Assert.IsTrue (ds.IsFirstPage);
214 ds.CurrentPageIndex = 10;
215 Assert.IsFalse (ds.IsFirstPage);
219 public void IsLastPageTest ()
222 ds.AllowPaging = false;
223 Assert.IsTrue (ds.IsLastPage);
225 // When PageCount is 0, IsLastPage is false
226 ds.AllowPaging = true;
227 Assert.AreEqual (0, ds.PageCount);
228 Assert.IsFalse (ds.IsLastPage);
230 SetSource (new int [] { 1, 2, 3 });
231 Assert.IsTrue (ds.IsLastPage);
232 Assert.IsTrue (ds.IsLastPage == (ds.CurrentPageIndex == ds.PageCount - 1));
233 ds.CurrentPageIndex = 3;
234 // Assert.IsTrue (ds.IsLastPage);
238 // Need mucho grande more here
239 public void EnumeratorTester (IEnumerator e, string name)
243 while (e.MoveNext ()) {
244 Assert.AreEqual (e.Current, index, name + "-A1-" + index);
248 Assert.AreEqual (30, index, name + "-A2");
251 public void EnumeratorTester_NoPaging (IEnumerator e, string name)
255 while (e.MoveNext ()) {
256 Assert.AreEqual (e.Current, index, name + "-A1-" + index);
260 Assert.AreEqual (50, index, name + "-A2");
264 public void TestEnumerators ()
266 PagedDataSource ds = new PagedDataSource ();
267 ds.AllowPaging = true;
269 ds.CurrentPageIndex = 2;
273 // Collection Enumerator
275 Queue q = new Queue ();
276 for (int i = 0; i < 50; i++)
279 EnumeratorTester (ds.GetEnumerator (), "collection");
284 ArrayList l = new ArrayList ();
285 for (int i = 0; i < 50; i++)
287 EnumeratorTester (ds.GetEnumerator (), "list");
291 public void TestEnumerators_NoPaging ()
293 PagedDataSource ds = new PagedDataSource ();
294 ds.AllowPaging = false;
297 // Collection Enumerator
299 Queue q = new Queue ();
300 for (int i = 0; i < 50; i++)
303 EnumeratorTester_NoPaging (ds.GetEnumerator (), "collection");
308 ArrayList l = new ArrayList ();
309 for (int i = 0; i < 50; i++)
311 EnumeratorTester_NoPaging (ds.GetEnumerator (), "list");
315 [ExpectedException (typeof (NullReferenceException))]
316 public void NullSource ()
318 PagedDataSource ds = new PagedDataSource ();
319 ds.DataSource = null;
320 IEnumerator data = ds.GetEnumerator ();
323 static void FillTable (DataTable table, int nelems)
325 table.Columns.Add (new DataColumn ("one", typeof (string)));
326 table.Columns.Add (new DataColumn ("two", typeof (string)));
328 for (int i = 0; i < nelems; i++) {
329 DataRow row = table.NewRow ();
332 table.Rows.Add (row);
337 public void Paging1 ()
339 PagedDataSource paged = new PagedDataSource ();
340 paged.AllowPaging = true;
342 DataTable table = new DataTable ();
343 FillTable (table, 100);
344 paged.DataSource = new DataView (table);
346 Assert.IsTrue (paged.IsFirstPage, "first-1");
347 Assert.IsFalse (paged.IsLastPage, "last-1");
349 paged.CurrentPageIndex = 100; // no problem setting this.
350 Assert.AreEqual (100, paged.CurrentPageIndex, "current-1");
351 Assert.IsFalse (paged.IsFirstPage, "first-2");
352 Assert.IsFalse (paged.IsLastPage, "last-2");
353 IEnumerator rator = paged.GetEnumerator ();
354 Assert.IsFalse (rator.MoveNext (), "beyondtheend-1");
358 [ExpectedException (typeof (IndexOutOfRangeException))]
359 public void Paging2 ()
361 PagedDataSource paged = new PagedDataSource ();
362 paged.AllowPaging = true;
364 DataTable table = new DataTable ();
365 FillTable (table, 100);
366 paged.DataSource = new DataView (table);
368 paged.CurrentPageIndex = -1;
369 Assert.AreEqual (-1, paged.CurrentPageIndex, "current");
370 Assert.IsFalse (paged.IsFirstPage, "first");
371 Assert.IsFalse (paged.IsLastPage, "last");
372 IEnumerator rator = paged.GetEnumerator ();
373 Assert.AreEqual (-1, paged.CurrentPageIndex, "current-2");
374 Assert.IsTrue (rator.MoveNext (), "beyondtheend");
375 DataRowView drv = (DataRowView) rator.Current; // Throws (out of range)
379 [ExpectedException (typeof (IndexOutOfRangeException))]
380 public void Paging3 ()
382 PagedDataSource paged = new PagedDataSource ();
383 paged.AllowPaging = true;
385 DataTable table = new DataTable ();
386 FillTable (table, 100);
387 paged.DataSource = new DataView (table);
389 paged.CurrentPageIndex = -7;
390 Assert.AreEqual (-7, paged.CurrentPageIndex, "current");
391 Assert.IsFalse (paged.IsFirstPage, "first");
392 Assert.IsFalse (paged.IsLastPage, "last");
393 IEnumerator rator = paged.GetEnumerator ();
394 Assert.AreEqual (-7, paged.CurrentPageIndex, "current-2");
395 Assert.IsTrue (rator.MoveNext (), "beyondtheend");
396 DataRowView drv = (DataRowView) rator.Current; // Throws (out of range)
400 public void Paging4 ()
402 PagedDataSource paged = new PagedDataSource ();
403 paged.AllowPaging = true;
405 DataTable table = new DataTable ();
406 FillTable (table, 100);
407 paged.DataSource = new DataView (table);
409 paged.CurrentPageIndex = 1;
410 IEnumerator rator = paged.GetEnumerator ();
411 Assert.IsTrue (rator.MoveNext (), "beginning-1");
412 DataRowView drv = (DataRowView) rator.Current;
413 int one = Int32.Parse ((string) drv ["one"]);
414 Assert.IsTrue (one == 0 || one == 1, "one-1");
415 int res = one + 2 * Int32.Parse ((string) drv ["two"]);
416 Assert.AreEqual (5, res, "five");
422 PagedDataSource paged = new PagedDataSource ();
423 DataTable table = new DataTable ();
424 FillTable (table, 100);
425 paged.DataSource = new DataView (table);
426 object [] data = new object [100];
427 paged.CopyTo (data, 0);
428 Type t = typeof (DataRowView);
429 Assert.AreEqual (t, data [0].GetType ());
433 [ExpectedException (typeof (NullReferenceException))]
436 PagedDataSource paged = new PagedDataSource ();
437 paged.DataSource = null;
438 object [] data = new object [100];
439 paged.CopyTo (data, 0);
445 PagedDataSource paged = new PagedDataSource ();
446 paged.DataSource = new object [] {"1", "2"};
447 object [] data = new object [100];
448 paged.CopyTo (data, 0);
452 public void VirtualPager1 ()
454 PagedDataSource paged = new PagedDataSource ();
455 paged.AllowPaging = true;
457 paged.VirtualCount = 100;
458 DataTable table = new DataTable ();
459 FillTable (table, 100);
460 paged.DataSource = new DataView (table);
463 IEnumerator rator = paged.GetEnumerator ();
464 while (rator.MoveNext ())
466 Assert.AreEqual (20, count, "count");
467 Assert.AreEqual (true, paged.IsFirstPage, "first");
468 Assert.AreEqual (false, paged.IsLastPage, "last");
472 public void VirtualPager2 ()
474 PagedDataSource paged = new PagedDataSource ();
475 paged.AllowPaging = true;
476 paged.PageSize = 100;
477 paged.VirtualCount = 50;
478 paged.AllowCustomPaging = true;
479 DataTable table = new DataTable ();
480 FillTable (table, 100);
481 paged.DataSource = new DataView (table);
484 IEnumerator rator = paged.GetEnumerator ();
485 while (rator.MoveNext ())
487 Assert.AreEqual (100, count, "count");
488 Assert.AreEqual (true, paged.IsFirstPage, "first");
489 Assert.AreEqual (true, paged.IsLastPage, "last");