1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Tests
.Helpers
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
20 using NUnit
.Framework
;
22 using Castle
.MonoRail
.Framework
.Helpers
;
25 public class PaginationHelperTestCase
27 private PaginationHelper helper
;
32 helper
= new PaginationHelper();
36 public void EmptyCollection()
38 IList items
= new ArrayList();
40 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 10, 1);
42 Assert
.IsNotNull(page
);
44 Assert
.AreEqual(0, page
.FirstItem
);
45 Assert
.AreEqual(0, page
.LastItem
);
46 Assert
.AreEqual(0, page
.TotalItems
);
48 Assert
.IsFalse(page
.HasFirst
);
49 Assert
.IsFalse(page
.HasLast
);
50 Assert
.IsFalse(page
.HasNext
);
51 Assert
.IsFalse(page
.HasPrevious
);
55 public void LessItemsThanPageSize()
57 IList items
= new ArrayList();
65 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 10, 1);
67 Assert
.IsNotNull(page
);
69 Assert
.AreEqual(1, page
.FirstItem
);
70 Assert
.AreEqual(5, page
.LastItem
);
71 Assert
.AreEqual(5, page
.TotalItems
);
73 Assert
.IsFalse(page
.HasFirst
);
74 Assert
.IsFalse(page
.HasLast
);
75 Assert
.IsFalse(page
.HasNext
);
76 Assert
.IsFalse(page
.HasPrevious
);
80 public void JustOneItem()
82 IList items
= new ArrayList();
86 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 10, 1);
88 Assert
.IsNotNull(page
);
90 Assert
.AreEqual(1, page
.FirstItem
);
91 Assert
.AreEqual(1, page
.LastItem
);
92 Assert
.AreEqual(1, page
.TotalItems
);
94 Assert
.IsFalse(page
.HasFirst
);
95 Assert
.IsFalse(page
.HasLast
);
96 Assert
.IsFalse(page
.HasNext
);
97 Assert
.IsFalse(page
.HasPrevious
);
101 public void ItemsEqualsToPageSize()
103 IList items
= new ArrayList();
109 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 3, 1);
111 Assert
.IsNotNull(page
);
113 Assert
.AreEqual(1, page
.FirstItem
);
114 Assert
.AreEqual(3, page
.LastItem
);
115 Assert
.AreEqual(3, page
.TotalItems
);
117 Assert
.IsFalse(page
.HasFirst
);
118 Assert
.IsFalse(page
.HasLast
);
119 Assert
.IsFalse(page
.HasNext
);
120 Assert
.IsFalse(page
.HasPrevious
);
124 public void ItemsBiggerThanPageSize()
126 IList items
= new ArrayList();
128 for(int i
=1; i
<= 15; i
++)
135 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 5, 1);
137 Assert
.IsNotNull(page
);
139 Assert
.AreEqual(1, page
.FirstItem
);
140 Assert
.AreEqual(5, page
.LastItem
);
141 Assert
.AreEqual(15, page
.TotalItems
);
143 Assert
.IsFalse(page
.HasFirst
);
144 Assert
.IsTrue(page
.HasLast
);
145 Assert
.IsTrue(page
.HasNext
);
146 Assert
.IsFalse(page
.HasPrevious
);
150 page
= PaginationHelper
.CreatePagination( items
, 5, 2 );
152 Assert
.IsNotNull(page
);
154 Assert
.AreEqual(6, page
.FirstItem
);
155 Assert
.AreEqual(10, page
.LastItem
);
156 Assert
.AreEqual(15, page
.TotalItems
);
158 Assert
.IsTrue(page
.HasFirst
);
159 Assert
.IsTrue(page
.HasLast
);
160 Assert
.IsTrue(page
.HasNext
);
161 Assert
.IsTrue(page
.HasPrevious
);
165 page
= PaginationHelper
.CreatePagination( items
, 5, 3 );
167 Assert
.IsNotNull(page
);
169 Assert
.AreEqual(11, page
.FirstItem
);
170 Assert
.AreEqual(15, page
.LastItem
);
171 Assert
.AreEqual(15, page
.TotalItems
);
173 Assert
.IsTrue(page
.HasFirst
);
174 Assert
.IsFalse(page
.HasLast
);
175 Assert
.IsFalse(page
.HasNext
);
176 Assert
.IsTrue(page
.HasPrevious
);
180 public void UsageWithGenerics()
182 IList
<int> items
= new List
<int>();
184 for (int i
= 1; i
<= 15; i
++)
191 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 5, 1);
193 Assert
.IsNotNull(page
);
195 Assert
.AreEqual(1, page
.FirstItem
);
196 Assert
.AreEqual(5, page
.LastItem
);
197 Assert
.AreEqual(15, page
.TotalItems
);
199 Assert
.IsFalse(page
.HasFirst
);
200 Assert
.IsTrue(page
.HasLast
);
201 Assert
.IsTrue(page
.HasNext
);
202 Assert
.IsFalse(page
.HasPrevious
);
206 page
= PaginationHelper
.CreatePagination(items
, 5, 2);
208 Assert
.IsNotNull(page
);
210 Assert
.AreEqual(6, page
.FirstItem
);
211 Assert
.AreEqual(10, page
.LastItem
);
212 Assert
.AreEqual(15, page
.TotalItems
);
214 Assert
.IsTrue(page
.HasFirst
);
215 Assert
.IsTrue(page
.HasLast
);
216 Assert
.IsTrue(page
.HasNext
);
217 Assert
.IsTrue(page
.HasPrevious
);
221 page
= PaginationHelper
.CreatePagination(items
, 5, 3);
223 Assert
.IsNotNull(page
);
225 Assert
.AreEqual(11, page
.FirstItem
);
226 Assert
.AreEqual(15, page
.LastItem
);
227 Assert
.AreEqual(15, page
.TotalItems
);
229 Assert
.IsTrue(page
.HasFirst
);
230 Assert
.IsFalse(page
.HasLast
);
231 Assert
.IsFalse(page
.HasNext
);
232 Assert
.IsTrue(page
.HasPrevious
);
237 public void CustomPaginationForObjectCollection()
239 IList items
= new ArrayList();
246 IPaginatedPage page
= PaginationHelper
.CreateCustomPage(items
, 3, 1, 8);
248 Assert
.IsNotNull(page
);
250 Assert
.AreEqual(1, page
.FirstItem
);
251 Assert
.AreEqual(3, page
.LastItem
);
252 Assert
.AreEqual(8, page
.TotalItems
);
254 Assert
.IsFalse(page
.HasFirst
);
255 Assert
.IsTrue(page
.HasLast
);
256 Assert
.IsTrue(page
.HasNext
);
257 Assert
.IsFalse(page
.HasPrevious
);
265 page
= PaginationHelper
.CreateCustomPage(items
, 3, 2, 8);
267 Assert
.IsNotNull(page
);
269 Assert
.AreEqual(4, page
.FirstItem
);
270 Assert
.AreEqual(6, page
.LastItem
);
271 Assert
.AreEqual(8, page
.TotalItems
);
273 Assert
.IsTrue(page
.HasFirst
);
274 Assert
.IsTrue(page
.HasLast
);
275 Assert
.IsTrue(page
.HasNext
);
276 Assert
.IsTrue(page
.HasPrevious
);
278 /* Third page, partial */
283 page
= PaginationHelper
.CreateCustomPage(items
, 3, 3, 8);
285 Assert
.IsNotNull(page
);
287 Assert
.AreEqual(7, page
.FirstItem
);
288 Assert
.AreEqual(8, page
.LastItem
);
289 Assert
.AreEqual(8, page
.TotalItems
);
291 Assert
.IsTrue(page
.HasFirst
);
292 Assert
.IsFalse(page
.HasLast
);
293 Assert
.IsFalse(page
.HasNext
);
294 Assert
.IsTrue(page
.HasPrevious
);
298 public void CustomPaginationForGenericCollection()
300 IList
<string> items
= new List
<string>();
307 IPaginatedPage page
= PaginationHelper
.CreateCustomPage(items
, 3, 1, 8);
309 Assert
.IsNotNull(page
);
311 Assert
.AreEqual(1, page
.FirstItem
);
312 Assert
.AreEqual(3, page
.LastItem
);
313 Assert
.AreEqual(8, page
.TotalItems
);
315 Assert
.IsFalse(page
.HasFirst
);
316 Assert
.IsTrue(page
.HasLast
);
317 Assert
.IsTrue(page
.HasNext
);
318 Assert
.IsFalse(page
.HasPrevious
);
326 page
= PaginationHelper
.CreateCustomPage(items
, 3, 2, 8);
328 Assert
.IsNotNull(page
);
330 Assert
.AreEqual(4, page
.FirstItem
);
331 Assert
.AreEqual(6, page
.LastItem
);
332 Assert
.AreEqual(8, page
.TotalItems
);
334 Assert
.IsTrue(page
.HasFirst
);
335 Assert
.IsTrue(page
.HasLast
);
336 Assert
.IsTrue(page
.HasNext
);
337 Assert
.IsTrue(page
.HasPrevious
);
339 /* Third page, partial */
344 page
= PaginationHelper
.CreateCustomPage(items
, 3, 3, 8);
346 Assert
.IsNotNull(page
);
348 Assert
.AreEqual(7, page
.FirstItem
);
349 Assert
.AreEqual(8, page
.LastItem
);
350 Assert
.AreEqual(8, page
.TotalItems
);
352 Assert
.IsTrue(page
.HasFirst
);
353 Assert
.IsFalse(page
.HasLast
);
354 Assert
.IsFalse(page
.HasNext
);
355 Assert
.IsTrue(page
.HasPrevious
);