PagedDataSourceTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. //
  2. // PagedDataSourceTest.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  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. using NUnit.Framework;
  30. using System;
  31. using System.Data;
  32. using System.Collections;
  33. using System.Diagnostics;
  34. using System.Web.UI.WebControls;
  35. using System.ComponentModel;
  36. namespace MonoTests.System.Web.UI.WebControls {
  37. [TestFixture]
  38. public class PagedDataSourceTest {
  39. PagedDataSource ds;
  40. [SetUp]
  41. public void SetUp ()
  42. {
  43. ds = new PagedDataSource ();
  44. }
  45. public void SetUpTest ()
  46. {
  47. Assert.AreEqual (10, ds.PageSize);
  48. Assert.IsFalse (ds.AllowPaging);
  49. Assert.AreEqual (0, ds.CurrentPageIndex);
  50. Assert.IsFalse (ds.AllowCustomPaging);
  51. Assert.AreEqual (0, ds.VirtualCount);
  52. }
  53. public void Reset ()
  54. {
  55. ds.DataSource = null;
  56. ds.PageSize = 10;
  57. ds.AllowPaging = false;
  58. ds.CurrentPageIndex = 0;
  59. ds.AllowCustomPaging = false;
  60. ds.VirtualCount = 0;
  61. }
  62. void SetSource (IEnumerable source)
  63. {
  64. Reset ();
  65. ds.DataSource = source;
  66. }
  67. [Test]
  68. public void GetItemProperties ()
  69. {
  70. PagedDataSource ds = new PagedDataSource ();
  71. DataTable table = new DataTable ();
  72. table.Columns.Add (new DataColumn ("one", typeof (string)));
  73. table.Columns.Add (new DataColumn ("two", typeof (string)));
  74. table.Columns.Add (new DataColumn ("three", typeof (string)));
  75. ds.DataSource = new DataView (table);
  76. PropertyDescriptorCollection props = ds.GetItemProperties (null);
  77. Assert.AreEqual (props.Count, 3, "A1");
  78. Assert.AreEqual (props [0].Name, "one", "A2");
  79. Assert.AreEqual (props [1].Name, "two", "A3");
  80. Assert.AreEqual (props [2].Name, "three", "A4");
  81. ds.DataSource = new ArrayList ();
  82. props = ds.GetItemProperties (null);
  83. Assert.AreEqual (props, null, "A5");
  84. }
  85. [Test]
  86. public void GetEnumeratorTest ()
  87. {
  88. // Found out that there are 3 possibilities
  89. // for GetEnumerator () from this test.
  90. // One for ICollection, one for IList and otherwise, it uses the DataSource directly
  91. // Hashtable implements ICollection
  92. SetSource (new Hashtable ());
  93. //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
  94. // IList implementations
  95. SetSource (new int [] { 1, 2, 3, 4, 5 });
  96. //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
  97. SetSource (new ArrayList ().ToArray ());
  98. //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
  99. // Default case
  100. SetSource (new MyEnumerable ());
  101. }
  102. public class MyEnumerable : IEnumerable, IEnumerator
  103. {
  104. IEnumerator IEnumerable.GetEnumerator () { return this; }
  105. object IEnumerator.Current { get { return null; } }
  106. bool IEnumerator.MoveNext () { return false; }
  107. void IEnumerator.Reset () {}
  108. }
  109. [Test]
  110. public void FirstIndexInPageTest ()
  111. {
  112. SetSource (null);
  113. Assert.AreEqual (0, ds.FirstIndexInPage);
  114. SetSource (new int [] { 1, 2, 3, 4, 5 });
  115. ds.AllowPaging = false;
  116. Assert.AreEqual (0, ds.FirstIndexInPage);
  117. ds.AllowCustomPaging = false;
  118. Assert.AreEqual (0, ds.FirstIndexInPage);
  119. ds.AllowPaging = true;
  120. ds.CurrentPageIndex = 10;
  121. ds.PageSize = 5;
  122. Assert.AreEqual (ds.CurrentPageIndex * ds.PageSize, ds.FirstIndexInPage);
  123. }
  124. [Test]
  125. public void PageCountTest ()
  126. {
  127. SetSource (null);
  128. Assert.AreEqual (0, ds.PageCount, "A1");
  129. SetSource (new int [] {});
  130. ds.AllowPaging = false;
  131. Assert.AreEqual (1, ds.PageCount, "A2");
  132. ds.PageSize = 0;
  133. Assert.AreEqual (1, ds.PageCount, "A3");
  134. SetSource (new int [] { 1, 2, 3, 4, 5 });
  135. ds.AllowPaging = true;
  136. ds.PageSize = 10;
  137. Assert.AreEqual (1, ds.PageCount, "A4");
  138. SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
  139. ds.AllowPaging = true;
  140. ds.PageSize = 10;
  141. Assert.AreEqual (2, ds.PageCount, "A5");
  142. SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
  143. ds.PageSize = 5;
  144. ds.AllowPaging = true;
  145. Assert.AreEqual (3, ds.PageCount, "A6");
  146. SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
  147. ds.AllowPaging = true;
  148. ds.PageSize = 3;
  149. Assert.AreEqual (4, ds.PageCount, "A7");
  150. }
  151. [Test]
  152. public void CountTest ()
  153. {
  154. SetSource (null);
  155. Assert.AreEqual (0, ds.Count);
  156. SetSource (new int [] { 1, 2, 3, 4, 5 });
  157. ds.AllowPaging = true;
  158. ds.AllowCustomPaging = true;
  159. Assert.AreEqual (ds.PageSize, ds.Count);
  160. // ds.AllowCustomPaging = false;
  161. // ds.CurrentPageIndex = ds.PageCount;
  162. // Assert.AreEqual (ds.FirstIndexInPage - ds.DataSourceCount, ds.Count);
  163. // ds.AllowPaging = false;
  164. // Assert.AreEqual (ds.DataSourceCount, ds.Count);
  165. }
  166. [Test]
  167. public void IsFirstPageTest ()
  168. {
  169. ds.AllowPaging = false;
  170. ds.CurrentPageIndex = 100;
  171. Assert.IsTrue (ds.IsFirstPage);
  172. ds.AllowPaging = true;
  173. ds.CurrentPageIndex = 0;
  174. Assert.IsTrue (ds.IsFirstPage);
  175. ds.CurrentPageIndex = 10;
  176. Assert.IsFalse (ds.IsFirstPage);
  177. }
  178. [Test]
  179. public void IsLastPageTest ()
  180. {
  181. Reset ();
  182. ds.AllowPaging = false;
  183. Assert.IsTrue (ds.IsLastPage);
  184. // When PageCount is 0, IsLastPage is false
  185. ds.AllowPaging = true;
  186. Assert.AreEqual (0, ds.PageCount);
  187. Assert.IsFalse (ds.IsLastPage);
  188. SetSource (new int [] { 1, 2, 3 });
  189. Assert.IsTrue (ds.IsLastPage);
  190. Assert.IsTrue (ds.IsLastPage == (ds.CurrentPageIndex == ds.PageCount - 1));
  191. ds.CurrentPageIndex = 3;
  192. // Assert.IsTrue (ds.IsLastPage);
  193. }
  194. // Need mucho grande more here
  195. public void EnumeratorTester (IEnumerator e, string name)
  196. {
  197. int index = 20;
  198. while (e.MoveNext ()) {
  199. Assert.AreEqual (e.Current, index, name + "-A1-" + index);
  200. index++;
  201. }
  202. Assert.AreEqual (30, index, name + "-A2");
  203. }
  204. public void EnumeratorTester_NoPaging (IEnumerator e, string name)
  205. {
  206. int index = 0;
  207. while (e.MoveNext ()) {
  208. Assert.AreEqual (e.Current, index, name + "-A1-" + index);
  209. index++;
  210. }
  211. Assert.AreEqual (50, index, name + "-A2");
  212. }
  213. [Test]
  214. public void TestEnumerators ()
  215. {
  216. PagedDataSource ds = new PagedDataSource ();
  217. ds.AllowPaging = true;
  218. ds.PageSize = 10;
  219. ds.CurrentPageIndex = 2;
  220. //
  221. // Collection Enumerator
  222. //
  223. Queue q = new Queue ();
  224. for (int i = 0; i < 50; i++)
  225. q.Enqueue (i);
  226. ds.DataSource = q;
  227. EnumeratorTester (ds.GetEnumerator (), "collection");
  228. //
  229. // List Enumerator
  230. //
  231. ArrayList l = new ArrayList ();
  232. for (int i = 0; i < 50; i++)
  233. l.Add (i);
  234. EnumeratorTester (ds.GetEnumerator (), "list");
  235. }
  236. [Test]
  237. public void TestEnumerators_NoPaging ()
  238. {
  239. PagedDataSource ds = new PagedDataSource ();
  240. ds.AllowPaging = false;
  241. //
  242. // Collection Enumerator
  243. //
  244. Queue q = new Queue ();
  245. for (int i = 0; i < 50; i++)
  246. q.Enqueue (i);
  247. ds.DataSource = q;
  248. EnumeratorTester_NoPaging (ds.GetEnumerator (), "collection");
  249. //
  250. // List Enumerator
  251. //
  252. ArrayList l = new ArrayList ();
  253. for (int i = 0; i < 50; i++)
  254. l.Add (i);
  255. EnumeratorTester_NoPaging (ds.GetEnumerator (), "list");
  256. }
  257. [Test]
  258. [ExpectedException (typeof (NullReferenceException))]
  259. public void NullSource ()
  260. {
  261. PagedDataSource ds = new PagedDataSource ();
  262. ds.DataSource = null;
  263. IEnumerator data = ds.GetEnumerator ();
  264. }
  265. static void FillTable (DataTable table, int nelems)
  266. {
  267. table.Columns.Add (new DataColumn ("one", typeof (string)));
  268. table.Columns.Add (new DataColumn ("two", typeof (string)));
  269. for (int i = 0; i < nelems; i++) {
  270. DataRow row = table.NewRow ();
  271. row ["one"] = i % 2;
  272. row ["two"] = i / 2;
  273. table.Rows.Add (row);
  274. }
  275. }
  276. [Test]
  277. public void Paging1 ()
  278. {
  279. PagedDataSource paged = new PagedDataSource ();
  280. paged.AllowPaging = true;
  281. paged.PageSize = 5;
  282. DataTable table = new DataTable ();
  283. FillTable (table, 100);
  284. paged.DataSource = new DataView (table);
  285. Assert.IsTrue (paged.IsFirstPage, "first-1");
  286. Assert.IsFalse (paged.IsLastPage, "last-1");
  287. paged.CurrentPageIndex = 100; // no problem setting this.
  288. Assert.AreEqual (100, paged.CurrentPageIndex, "current-1");
  289. Assert.IsFalse (paged.IsFirstPage, "first-2");
  290. Assert.IsFalse (paged.IsLastPage, "last-2");
  291. IEnumerator rator = paged.GetEnumerator ();
  292. Assert.IsFalse (rator.MoveNext (), "beyondtheend-1");
  293. }
  294. [Test]
  295. [ExpectedException (typeof (IndexOutOfRangeException))]
  296. public void Paging2 ()
  297. {
  298. PagedDataSource paged = new PagedDataSource ();
  299. paged.AllowPaging = true;
  300. paged.PageSize = 5;
  301. DataTable table = new DataTable ();
  302. FillTable (table, 100);
  303. paged.DataSource = new DataView (table);
  304. paged.CurrentPageIndex = -1;
  305. Assert.AreEqual (-1, paged.CurrentPageIndex, "current");
  306. Assert.IsFalse (paged.IsFirstPage, "first");
  307. Assert.IsFalse (paged.IsLastPage, "last");
  308. IEnumerator rator = paged.GetEnumerator ();
  309. Assert.AreEqual (-1, paged.CurrentPageIndex, "current-2");
  310. Assert.IsTrue (rator.MoveNext (), "beyondtheend");
  311. DataRowView drv = (DataRowView) rator.Current; // Throws (out of range)
  312. }
  313. [Test]
  314. [ExpectedException (typeof (IndexOutOfRangeException))]
  315. public void Paging3 ()
  316. {
  317. PagedDataSource paged = new PagedDataSource ();
  318. paged.AllowPaging = true;
  319. paged.PageSize = 5;
  320. DataTable table = new DataTable ();
  321. FillTable (table, 100);
  322. paged.DataSource = new DataView (table);
  323. paged.CurrentPageIndex = -7;
  324. Assert.AreEqual (-7, paged.CurrentPageIndex, "current");
  325. Assert.IsFalse (paged.IsFirstPage, "first");
  326. Assert.IsFalse (paged.IsLastPage, "last");
  327. IEnumerator rator = paged.GetEnumerator ();
  328. Assert.AreEqual (-7, paged.CurrentPageIndex, "current-2");
  329. Assert.IsTrue (rator.MoveNext (), "beyondtheend");
  330. DataRowView drv = (DataRowView) rator.Current; // Throws (out of range)
  331. }
  332. [Test]
  333. public void Paging4 ()
  334. {
  335. PagedDataSource paged = new PagedDataSource ();
  336. paged.AllowPaging = true;
  337. paged.PageSize = 5;
  338. DataTable table = new DataTable ();
  339. FillTable (table, 100);
  340. paged.DataSource = new DataView (table);
  341. paged.CurrentPageIndex = 1;
  342. IEnumerator rator = paged.GetEnumerator ();
  343. Assert.IsTrue (rator.MoveNext (), "beginning-1");
  344. DataRowView drv = (DataRowView) rator.Current;
  345. int one = Int32.Parse ((string) drv ["one"]);
  346. Assert.IsTrue (one == 0 || one == 1, "one-1");
  347. int res = one + 2 * Int32.Parse ((string) drv ["two"]);
  348. Assert.AreEqual (5, res, "five");
  349. }
  350. [Test]
  351. public void Copy1 ()
  352. {
  353. PagedDataSource paged = new PagedDataSource ();
  354. DataTable table = new DataTable ();
  355. FillTable (table, 100);
  356. paged.DataSource = new DataView (table);
  357. object [] data = new object [100];
  358. paged.CopyTo (data, 0);
  359. Type t = typeof (DataRowView);
  360. Assert.AreEqual (t, data [0].GetType ());
  361. }
  362. [Test]
  363. [ExpectedException (typeof (NullReferenceException))]
  364. public void Copy2 ()
  365. {
  366. PagedDataSource paged = new PagedDataSource ();
  367. paged.DataSource = null;
  368. object [] data = new object [100];
  369. paged.CopyTo (data, 0);
  370. }
  371. [Test]
  372. public void Copy3 ()
  373. {
  374. PagedDataSource paged = new PagedDataSource ();
  375. paged.DataSource = new object [] {"1", "2"};
  376. object [] data = new object [100];
  377. paged.CopyTo (data, 0);
  378. }
  379. [Test]
  380. public void VirtualPager1 ()
  381. {
  382. PagedDataSource paged = new PagedDataSource ();
  383. paged.AllowPaging = true;
  384. paged.PageSize = 20;
  385. paged.VirtualCount = 100;
  386. DataTable table = new DataTable ();
  387. FillTable (table, 100);
  388. paged.DataSource = new DataView (table);
  389. int count = 0;
  390. IEnumerator rator = paged.GetEnumerator ();
  391. while (rator.MoveNext ())
  392. count++;
  393. Assert.AreEqual (20, count, "count");
  394. Assert.AreEqual (true, paged.IsFirstPage, "first");
  395. Assert.AreEqual (false, paged.IsLastPage, "last");
  396. }
  397. [Test]
  398. public void VirtualPager2 ()
  399. {
  400. PagedDataSource paged = new PagedDataSource ();
  401. paged.AllowPaging = true;
  402. paged.PageSize = 100;
  403. paged.VirtualCount = 50;
  404. paged.AllowCustomPaging = true;
  405. DataTable table = new DataTable ();
  406. FillTable (table, 100);
  407. paged.DataSource = new DataView (table);
  408. int count = 0;
  409. IEnumerator rator = paged.GetEnumerator ();
  410. while (rator.MoveNext ())
  411. count++;
  412. Assert.AreEqual (100, count, "count");
  413. Assert.AreEqual (true, paged.IsFirstPage, "first");
  414. Assert.AreEqual (true, paged.IsLastPage, "last");
  415. }
  416. }
  417. }