2007-02-04 yonik <yonik@mainsoft.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / PagedDataSourceTest.cs
1 //
2 // PagedDataSourceTest.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@novell.com)
6 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Data;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Web.UI.WebControls;
36 using System.ComponentModel;
37
38 namespace MonoTests.System.Web.UI.WebControls {
39
40         [TestFixture]
41         public class PagedDataSourceTest {
42
43                 PagedDataSource ds;
44
45                 [SetUp]
46                 public void SetUp ()
47                 {
48                         ds = new PagedDataSource ();
49                 }
50
51                 public void SetUpTest ()
52                 {
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);
58                 }
59
60                 public void Reset ()
61                 {
62                         ds.DataSource = null;
63                         ds.PageSize = 10;
64                         ds.AllowPaging = false;
65                         ds.CurrentPageIndex = 0;
66                         ds.AllowCustomPaging = false;
67                         ds.VirtualCount = 0;
68                 }
69
70                 void SetSource (IEnumerable source)
71                 {
72                         Reset ();
73                         ds.DataSource = source;
74                 }
75
76                 [Test]
77                 public void GetItemProperties ()
78                 {
79                         PagedDataSource ds = new PagedDataSource ();
80                         DataTable table = new DataTable ();
81
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)));
85
86                         ds.DataSource = new DataView (table);
87                         PropertyDescriptorCollection props = ds.GetItemProperties (null);
88
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");
93
94                         ds.DataSource = new ArrayList ();
95                         props = ds.GetItemProperties (null);
96                         Assert.AreEqual (props, null, "A5");
97                 }
98
99                 [Test]
100                 public void GetEnumeratorTest ()
101                 {
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
105
106                         // Hashtable implements ICollection
107                         SetSource (new Hashtable ());
108                         //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
109
110                         // IList implementations
111                         SetSource (new int [] { 1, 2, 3, 4, 5 });
112                         //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
113
114                         SetSource (new ArrayList ().ToArray ());
115                         //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
116
117                         // Default case
118                         SetSource (new MyEnumerable ());
119                 }
120
121                 public class MyEnumerable : IEnumerable, IEnumerator
122                 {
123                         IEnumerator IEnumerable.GetEnumerator () { return this; }
124                         object IEnumerator.Current { get { return null; } }
125                         bool IEnumerator.MoveNext () { return false; }
126                         void IEnumerator.Reset () {}
127                 }
128
129                 [Test]
130                 public void FirstIndexInPageTest ()
131                 {
132                         SetSource (null);
133                         Assert.AreEqual (0, ds.FirstIndexInPage);
134                         
135                         SetSource (new int [] { 1, 2, 3, 4, 5 });
136                         ds.AllowPaging = false;
137                         Assert.AreEqual (0, ds.FirstIndexInPage);
138                         
139                         ds.AllowCustomPaging = false;
140                         Assert.AreEqual (0, ds.FirstIndexInPage);
141                         
142                         ds.AllowPaging = true;
143                         ds.CurrentPageIndex = 10;
144                         ds.PageSize = 5;
145                         Assert.AreEqual (ds.CurrentPageIndex * ds.PageSize, ds.FirstIndexInPage);
146                 }
147
148                 [Test]
149                 public void PageCountTest ()
150                 {
151                         SetSource (null);
152                         Assert.AreEqual (0, ds.PageCount, "A1");
153
154                         SetSource (new int [] {});
155                         ds.AllowPaging = false;
156                         Assert.AreEqual (1, ds.PageCount, "A2");
157
158                         ds.PageSize = 0;
159                         Assert.AreEqual (1, ds.PageCount, "A3");
160
161                         SetSource (new int [] { 1, 2, 3, 4, 5 });
162                         ds.AllowPaging = true;
163                         ds.PageSize = 10;
164                         Assert.AreEqual (1, ds.PageCount, "A4");
165
166                         SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
167                         ds.AllowPaging = true;
168                         ds.PageSize = 10;
169                         Assert.AreEqual (2, ds.PageCount, "A5");
170
171                         SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
172                         ds.PageSize = 5;
173                         ds.AllowPaging = true;
174                         Assert.AreEqual (3, ds.PageCount, "A6");
175
176                         SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
177                         ds.AllowPaging = true;
178                         ds.PageSize = 3;
179                         Assert.AreEqual (4, ds.PageCount, "A7");
180                 }
181
182                 [Test]
183                 public void CountTest ()
184                 {
185                         SetSource (null);
186                         Assert.AreEqual (0, ds.Count);
187
188                         SetSource (new int [] { 1, 2, 3, 4, 5 });
189                         ds.AllowPaging = true;
190
191                         ds.AllowCustomPaging = true;
192                         Assert.AreEqual (ds.PageSize, ds.Count);
193
194                         // ds.AllowCustomPaging = false;
195                         // ds.CurrentPageIndex = ds.PageCount;
196
197                         // Assert.AreEqual (ds.FirstIndexInPage - ds.DataSourceCount, ds.Count);
198
199                         // ds.AllowPaging = false;
200                         // Assert.AreEqual (ds.DataSourceCount, ds.Count);
201                 }
202
203                 [Test]
204                 public void IsFirstPageTest ()
205                 {
206                         ds.AllowPaging = false;
207                         ds.CurrentPageIndex = 100;
208                         Assert.IsTrue (ds.IsFirstPage);
209                         
210                         ds.AllowPaging = true;
211                         ds.CurrentPageIndex = 0;
212                         Assert.IsTrue (ds.IsFirstPage);
213
214                         ds.CurrentPageIndex = 10;
215                         Assert.IsFalse (ds.IsFirstPage);
216                 }
217                                 
218                 [Test]
219                 public void IsLastPageTest ()
220                 {
221                         Reset ();
222                         ds.AllowPaging = false;
223                         Assert.IsTrue (ds.IsLastPage);
224
225                         // When PageCount is 0, IsLastPage is false
226                         ds.AllowPaging = true;
227                         Assert.AreEqual (0, ds.PageCount);
228                         Assert.IsFalse (ds.IsLastPage);
229
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);
235                         
236                 }
237
238                 // Need mucho grande more here
239                 public void EnumeratorTester (IEnumerator e, string name)
240                 {                       
241                         int index = 20;
242
243                         while (e.MoveNext ()) {
244                                 Assert.AreEqual (e.Current, index, name + "-A1-" + index);
245                                 index++;
246                         }
247                         
248                         Assert.AreEqual (30, index, name + "-A2");
249                 }
250
251                 public void EnumeratorTester_NoPaging (IEnumerator e, string name)
252                 {                       
253                         int index = 0;
254
255                         while (e.MoveNext ()) {
256                                 Assert.AreEqual (e.Current, index, name + "-A1-" + index);
257                                 index++;
258                         }
259                         
260                         Assert.AreEqual (50, index, name + "-A2");
261                 }
262
263                 [Test]
264                 public void TestEnumerators ()
265                 {
266                         PagedDataSource ds = new PagedDataSource ();
267                         ds.AllowPaging = true;
268                         ds.PageSize = 10;
269                         ds.CurrentPageIndex = 2;
270
271
272                         //
273                         // Collection Enumerator
274                         //
275                         Queue q = new Queue ();
276                         for (int i = 0; i < 50; i++)
277                                 q.Enqueue (i);
278                         ds.DataSource = q;
279                         EnumeratorTester (ds.GetEnumerator (), "collection");
280                         
281                         //
282                         // List Enumerator
283                         //
284                         ArrayList l = new ArrayList ();
285                         for (int i = 0; i < 50; i++)
286                                 l.Add (i);
287                         EnumeratorTester (ds.GetEnumerator (), "list");
288                 }
289
290                 [Test]
291                 public void TestEnumerators_NoPaging ()
292                 {
293                         PagedDataSource ds = new PagedDataSource ();
294                         ds.AllowPaging = false;
295
296                         //
297                         // Collection Enumerator
298                         //
299                         Queue q = new Queue ();
300                         for (int i = 0; i < 50; i++)
301                                 q.Enqueue (i);
302                         ds.DataSource = q;
303                         EnumeratorTester_NoPaging (ds.GetEnumerator (), "collection");
304                         
305                         //
306                         // List Enumerator
307                         //
308                         ArrayList l = new ArrayList ();
309                         for (int i = 0; i < 50; i++)
310                                 l.Add (i);
311                         EnumeratorTester_NoPaging (ds.GetEnumerator (), "list");
312                 }
313
314                 [Test]
315                 [ExpectedException (typeof (NullReferenceException))]
316                 public void NullSource ()
317                 {
318                         PagedDataSource ds = new PagedDataSource ();
319                         ds.DataSource = null;
320                         IEnumerator data = ds.GetEnumerator ();
321                 }
322
323                 static void FillTable (DataTable table, int nelems)
324                 {
325                         table.Columns.Add (new DataColumn ("one", typeof (string)));
326                         table.Columns.Add (new DataColumn ("two", typeof (string)));
327
328                         for (int i = 0; i < nelems; i++) {
329                                 DataRow row = table.NewRow ();
330                                 row ["one"] = i % 2;
331                                 row ["two"] = i / 2;
332                                 table.Rows.Add (row);
333                         }
334                 }
335
336                 [Test]
337                 public void Paging1 ()
338                 {
339                         PagedDataSource paged = new PagedDataSource ();
340                         paged.AllowPaging = true;
341                         paged.PageSize = 5;
342                         DataTable table = new DataTable ();
343                         FillTable (table, 100);
344                         paged.DataSource = new DataView (table);
345
346                         Assert.IsTrue (paged.IsFirstPage, "first-1");
347                         Assert.IsFalse (paged.IsLastPage, "last-1");
348
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");
355                 }
356
357                 [Test]
358                 [ExpectedException (typeof (IndexOutOfRangeException))]
359                 public void Paging2 ()
360                 {
361                         PagedDataSource paged = new PagedDataSource ();
362                         paged.AllowPaging = true;
363                         paged.PageSize = 5;
364                         DataTable table = new DataTable ();
365                         FillTable (table, 100);
366                         paged.DataSource = new DataView (table);
367
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)
376                 }
377
378                 [Test]
379                 [ExpectedException (typeof (IndexOutOfRangeException))]
380                 public void Paging3 ()
381                 {
382                         PagedDataSource paged = new PagedDataSource ();
383                         paged.AllowPaging = true;
384                         paged.PageSize = 5;
385                         DataTable table = new DataTable ();
386                         FillTable (table, 100);
387                         paged.DataSource = new DataView (table);
388
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)
397                 }
398
399                 [Test]
400                 public void Paging4 ()
401                 {
402                         PagedDataSource paged = new PagedDataSource ();
403                         paged.AllowPaging = true;
404                         paged.PageSize = 5;
405                         DataTable table = new DataTable ();
406                         FillTable (table, 100);
407                         paged.DataSource = new DataView (table);
408
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");
417                 }
418
419                 [Test]
420                 public void Copy1 ()
421                 {
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 ());
430                 }
431
432                 [Test]
433                 [ExpectedException (typeof (NullReferenceException))]
434                 public void Copy2 ()
435                 {
436                         PagedDataSource paged = new PagedDataSource ();
437                         paged.DataSource = null;
438                         object [] data = new object [100];
439                         paged.CopyTo (data, 0);
440                 }
441
442                 [Test]
443                 public void Copy3 ()
444                 {
445                         PagedDataSource paged = new PagedDataSource ();
446                         paged.DataSource = new object [] {"1", "2"};
447                         object [] data = new object [100];
448                         paged.CopyTo (data, 0);
449                 }
450
451                 [Test]
452                 public void VirtualPager1 ()
453                 {
454                         PagedDataSource paged = new PagedDataSource ();
455                         paged.AllowPaging = true;
456                         paged.PageSize = 20;
457                         paged.VirtualCount = 100;
458                         DataTable table = new DataTable ();
459                         FillTable (table, 100);
460                         paged.DataSource = new DataView (table);
461
462                         int count = 0;
463                         IEnumerator rator = paged.GetEnumerator ();
464                         while (rator.MoveNext ())
465                                 count++;
466                         Assert.AreEqual (20, count, "count");
467                         Assert.AreEqual (true, paged.IsFirstPage, "first");
468                         Assert.AreEqual (false, paged.IsLastPage, "last");
469                 }
470
471                 [Test]
472                 public void VirtualPager2 ()
473                 {
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);
482
483                         int count = 0;
484                         IEnumerator rator = paged.GetEnumerator ();
485                         while (rator.MoveNext ())
486                                 count++;
487                         Assert.AreEqual (100, count, "count");
488                         Assert.AreEqual (true, paged.IsFirstPage, "first");
489                         Assert.AreEqual (true, paged.IsLastPage, "last");
490                 }
491         }
492 }
493