2
0

PagedDataSourceTest.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // PagedDataSourceTest.cs
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. //
  6. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using NUnit.Framework;
  28. using System;
  29. using System.Data;
  30. using System.Collections;
  31. using System.Diagnostics;
  32. using System.Web.UI.WebControls;
  33. using System.ComponentModel;
  34. namespace MonoTests.System.Web.UI.WebControls {
  35. [TestFixture]
  36. public class PagedDataSourceTest {
  37. PagedDataSource ds;
  38. [SetUp]
  39. public void SetUp ()
  40. {
  41. ds = new PagedDataSource ();
  42. }
  43. public void SetUpTest ()
  44. {
  45. Assert.AreEqual (10, ds.PageSize);
  46. Assert.IsFalse (ds.AllowPaging);
  47. Assert.AreEqual (0, ds.CurrentPageIndex);
  48. Assert.IsFalse (ds.AllowCustomPaging);
  49. Assert.AreEqual (0, ds.VirtualCount);
  50. }
  51. public void Reset ()
  52. {
  53. ds.DataSource = null;
  54. ds.PageSize = 10;
  55. ds.AllowPaging = false;
  56. ds.CurrentPageIndex = 0;
  57. ds.AllowCustomPaging = false;
  58. ds.VirtualCount = 0;
  59. }
  60. void SetSource (IEnumerable source)
  61. {
  62. Reset ();
  63. ds.DataSource = source;
  64. }
  65. [Test]
  66. public void GetItemProperties ()
  67. {
  68. PagedDataSource ds = new PagedDataSource ();
  69. DataTable table = new DataTable ();
  70. table.Columns.Add (new DataColumn ("one", typeof (string)));
  71. table.Columns.Add (new DataColumn ("two", typeof (string)));
  72. table.Columns.Add (new DataColumn ("three", typeof (string)));
  73. ds.DataSource = new DataView (table);
  74. PropertyDescriptorCollection props = ds.GetItemProperties (null);
  75. Assert.AreEqual (props.Count, 3, "A1");
  76. Assert.AreEqual (props [0].Name, "one", "A2");
  77. Assert.AreEqual (props [1].Name, "two", "A3");
  78. Assert.AreEqual (props [2].Name, "three", "A4");
  79. ds.DataSource = new ArrayList ();
  80. props = ds.GetItemProperties (null);
  81. Assert.AreEqual (props, null, "A5");
  82. }
  83. [Test]
  84. public void GetEnumeratorTest ()
  85. {
  86. // Found out that there are 3 possibilities
  87. // for GetEnumerator () from this test.
  88. // One for ICollection, one for IList and otherwise, it uses the DataSource directly
  89. // Hashtable implements ICollection
  90. SetSource (new Hashtable ());
  91. //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
  92. // IList implementations
  93. SetSource (new int [] { 1, 2, 3, 4, 5 });
  94. //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
  95. SetSource (new ArrayList ().ToArray ());
  96. //Console.WriteLine (ds.GetEnumerator ().GetType ().Name);
  97. // Default case
  98. SetSource (new MyEnumerable ());
  99. }
  100. public class MyEnumerable : IEnumerable, IEnumerator
  101. {
  102. IEnumerator IEnumerable.GetEnumerator () { return this; }
  103. object IEnumerator.Current { get { return null; } }
  104. bool IEnumerator.MoveNext () { return false; }
  105. void IEnumerator.Reset () {}
  106. }
  107. [Test]
  108. public void FirstIndexInPageTest ()
  109. {
  110. SetSource (null);
  111. Assert.AreEqual (0, ds.FirstIndexInPage);
  112. SetSource (new int [] { 1, 2, 3, 4, 5 });
  113. ds.AllowPaging = false;
  114. Assert.AreEqual (0, ds.FirstIndexInPage);
  115. ds.AllowCustomPaging = false;
  116. Assert.AreEqual (0, ds.FirstIndexInPage);
  117. ds.AllowPaging = true;
  118. ds.CurrentPageIndex = 10;
  119. ds.PageSize = 5;
  120. Assert.AreEqual (ds.CurrentPageIndex * ds.PageSize, ds.FirstIndexInPage);
  121. }
  122. [Test]
  123. public void PageCountTest ()
  124. {
  125. SetSource (null);
  126. Assert.AreEqual (0, ds.PageCount, "A1");
  127. SetSource (new int [] {});
  128. ds.AllowPaging = false;
  129. Assert.AreEqual (1, ds.PageCount, "A2");
  130. ds.PageSize = 0;
  131. Assert.AreEqual (1, ds.PageCount, "A3");
  132. SetSource (new int [] { 1, 2, 3, 4, 5 });
  133. ds.AllowPaging = true;
  134. ds.PageSize = 10;
  135. Assert.AreEqual (1, ds.PageCount, "A4");
  136. SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
  137. ds.AllowPaging = true;
  138. ds.PageSize = 10;
  139. Assert.AreEqual (2, ds.PageCount, "A5");
  140. SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
  141. ds.PageSize = 5;
  142. ds.AllowPaging = true;
  143. Assert.AreEqual (3, ds.PageCount, "A6");
  144. SetSource (new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
  145. ds.AllowPaging = true;
  146. ds.PageSize = 3;
  147. Assert.AreEqual (4, ds.PageCount, "A7");
  148. }
  149. [Test]
  150. public void CountTest ()
  151. {
  152. SetSource (null);
  153. Assert.AreEqual (0, ds.Count);
  154. SetSource (new int [] { 1, 2, 3, 4, 5 });
  155. ds.AllowPaging = true;
  156. ds.AllowCustomPaging = true;
  157. Assert.AreEqual (ds.PageSize, ds.Count);
  158. // ds.AllowCustomPaging = false;
  159. // ds.CurrentPageIndex = ds.PageCount;
  160. // Assert.AreEqual (ds.FirstIndexInPage - ds.DataSourceCount, ds.Count);
  161. // ds.AllowPaging = false;
  162. // Assert.AreEqual (ds.DataSourceCount, ds.Count);
  163. }
  164. [Test]
  165. public void IsFirstPageTest ()
  166. {
  167. ds.AllowPaging = false;
  168. ds.CurrentPageIndex = 100;
  169. Assert.IsTrue (ds.IsFirstPage);
  170. ds.AllowPaging = true;
  171. ds.CurrentPageIndex = 0;
  172. Assert.IsTrue (ds.IsFirstPage);
  173. ds.CurrentPageIndex = 10;
  174. Assert.IsFalse (ds.IsFirstPage);
  175. }
  176. [Test]
  177. public void IsLastPageTest ()
  178. {
  179. Reset ();
  180. ds.AllowPaging = false;
  181. Assert.IsTrue (ds.IsLastPage);
  182. // When PageCount is 0, IsLastPage is false
  183. ds.AllowPaging = true;
  184. Assert.AreEqual (0, ds.PageCount);
  185. Assert.IsFalse (ds.IsLastPage);
  186. SetSource (new int [] { 1, 2, 3 });
  187. Assert.IsTrue (ds.IsLastPage);
  188. Assert.IsTrue (ds.IsLastPage == (ds.CurrentPageIndex == ds.PageCount - 1));
  189. ds.CurrentPageIndex = 3;
  190. // Assert.IsTrue (ds.IsLastPage);
  191. }
  192. // Need mucho grande more here
  193. public void EnumeratorTester (IEnumerator e, string name)
  194. {
  195. int index = 20;
  196. while (e.MoveNext ()) {
  197. Assert.AreEqual (e.Current, index, name + "-A1-" + index);
  198. index++;
  199. }
  200. Assert.AreEqual (30, index, name + "-A2");
  201. }
  202. public void EnumeratorTester_NoPaging (IEnumerator e, string name)
  203. {
  204. int index = 0;
  205. while (e.MoveNext ()) {
  206. Assert.AreEqual (e.Current, index, name + "-A1-" + index);
  207. index++;
  208. }
  209. Assert.AreEqual (50, index, name + "-A2");
  210. }
  211. [Test]
  212. public void TestEnumerators ()
  213. {
  214. PagedDataSource ds = new PagedDataSource ();
  215. ds.AllowPaging = true;
  216. ds.PageSize = 10;
  217. ds.CurrentPageIndex = 2;
  218. //
  219. // Collection Enumerator
  220. //
  221. Queue q = new Queue ();
  222. for (int i = 0; i < 50; i++)
  223. q.Enqueue (i);
  224. ds.DataSource = q;
  225. EnumeratorTester (ds.GetEnumerator (), "collection");
  226. //
  227. // List Enumerator
  228. //
  229. ArrayList l = new ArrayList ();
  230. for (int i = 0; i < 50; i++)
  231. l.Add (i);
  232. EnumeratorTester (ds.GetEnumerator (), "list");
  233. }
  234. public void TestEnumerators_NoPaging ()
  235. {
  236. PagedDataSource ds = new PagedDataSource ();
  237. ds.AllowPaging = false;
  238. //
  239. // Collection Enumerator
  240. //
  241. Queue q = new Queue ();
  242. for (int i = 0; i < 50; i++)
  243. q.Enqueue (i);
  244. ds.DataSource = q;
  245. EnumeratorTester_NoPaging (ds.GetEnumerator (), "collection");
  246. //
  247. // List Enumerator
  248. //
  249. ArrayList l = new ArrayList ();
  250. for (int i = 0; i < 50; i++)
  251. l.Add (i);
  252. EnumeratorTester_NoPaging (ds.GetEnumerator (), "list");
  253. }
  254. }
  255. }