1 // Copyright 2004-2008 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
17 using System
.Collections
;
18 using System
.Collections
.Generic
;
19 using NUnit
.Framework
;
21 using Castle
.MonoRail
.Framework
.Helpers
;
24 public class PaginationHelperTestCase
27 public void EmptyCollection()
29 IList items
= new ArrayList();
31 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 10, 1);
33 Assert
.IsNotNull(page
);
35 Assert
.AreEqual(0, page
.FirstItem
);
36 Assert
.AreEqual(0, page
.LastItem
);
37 Assert
.AreEqual(0, page
.TotalItems
);
39 Assert
.IsFalse(page
.HasFirst
);
40 Assert
.IsFalse(page
.HasLast
);
41 Assert
.IsFalse(page
.HasNext
);
42 Assert
.IsFalse(page
.HasPrevious
);
46 public void LessItemsThanPageSize()
48 IList items
= new ArrayList();
56 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 10, 1);
58 Assert
.IsNotNull(page
);
60 Assert
.AreEqual(1, page
.FirstItem
);
61 Assert
.AreEqual(5, page
.LastItem
);
62 Assert
.AreEqual(5, page
.TotalItems
);
64 Assert
.IsFalse(page
.HasFirst
);
65 Assert
.IsFalse(page
.HasLast
);
66 Assert
.IsFalse(page
.HasNext
);
67 Assert
.IsFalse(page
.HasPrevious
);
71 public void JustOneItem()
73 IList items
= new ArrayList();
77 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 10, 1);
79 Assert
.IsNotNull(page
);
81 Assert
.AreEqual(1, page
.FirstItem
);
82 Assert
.AreEqual(1, page
.LastItem
);
83 Assert
.AreEqual(1, page
.TotalItems
);
85 Assert
.IsFalse(page
.HasFirst
);
86 Assert
.IsFalse(page
.HasLast
);
87 Assert
.IsFalse(page
.HasNext
);
88 Assert
.IsFalse(page
.HasPrevious
);
92 public void ItemsEqualsToPageSize()
94 IList items
= new ArrayList();
100 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 3, 1);
102 Assert
.IsNotNull(page
);
104 Assert
.AreEqual(1, page
.FirstItem
);
105 Assert
.AreEqual(3, page
.LastItem
);
106 Assert
.AreEqual(3, page
.TotalItems
);
108 Assert
.IsFalse(page
.HasFirst
);
109 Assert
.IsFalse(page
.HasLast
);
110 Assert
.IsFalse(page
.HasNext
);
111 Assert
.IsFalse(page
.HasPrevious
);
115 public void ItemsBiggerThanPageSize()
117 IList items
= new ArrayList();
119 for(int i
=1; i
<= 15; i
++)
126 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 5, 1);
128 Assert
.IsNotNull(page
);
130 Assert
.AreEqual(1, page
.FirstItem
);
131 Assert
.AreEqual(5, page
.LastItem
);
132 Assert
.AreEqual(15, page
.TotalItems
);
134 Assert
.IsFalse(page
.HasFirst
);
135 Assert
.IsTrue(page
.HasLast
);
136 Assert
.IsTrue(page
.HasNext
);
137 Assert
.IsFalse(page
.HasPrevious
);
141 page
= PaginationHelper
.CreatePagination( items
, 5, 2 );
143 Assert
.IsNotNull(page
);
145 Assert
.AreEqual(6, page
.FirstItem
);
146 Assert
.AreEqual(10, page
.LastItem
);
147 Assert
.AreEqual(15, page
.TotalItems
);
149 Assert
.IsTrue(page
.HasFirst
);
150 Assert
.IsTrue(page
.HasLast
);
151 Assert
.IsTrue(page
.HasNext
);
152 Assert
.IsTrue(page
.HasPrevious
);
156 page
= PaginationHelper
.CreatePagination( items
, 5, 3 );
158 Assert
.IsNotNull(page
);
160 Assert
.AreEqual(11, page
.FirstItem
);
161 Assert
.AreEqual(15, page
.LastItem
);
162 Assert
.AreEqual(15, page
.TotalItems
);
164 Assert
.IsTrue(page
.HasFirst
);
165 Assert
.IsFalse(page
.HasLast
);
166 Assert
.IsFalse(page
.HasNext
);
167 Assert
.IsTrue(page
.HasPrevious
);
171 public void UsageWithGenerics()
173 IList
<int> items
= new List
<int>();
175 for (int i
= 1; i
<= 15; i
++)
182 IPaginatedPage page
= PaginationHelper
.CreatePagination(items
, 5, 1);
184 Assert
.IsNotNull(page
);
186 Assert
.AreEqual(1, page
.FirstItem
);
187 Assert
.AreEqual(5, page
.LastItem
);
188 Assert
.AreEqual(15, page
.TotalItems
);
190 Assert
.IsFalse(page
.HasFirst
);
191 Assert
.IsTrue(page
.HasLast
);
192 Assert
.IsTrue(page
.HasNext
);
193 Assert
.IsFalse(page
.HasPrevious
);
197 page
= PaginationHelper
.CreatePagination(items
, 5, 2);
199 Assert
.IsNotNull(page
);
201 Assert
.AreEqual(6, page
.FirstItem
);
202 Assert
.AreEqual(10, page
.LastItem
);
203 Assert
.AreEqual(15, page
.TotalItems
);
205 Assert
.IsTrue(page
.HasFirst
);
206 Assert
.IsTrue(page
.HasLast
);
207 Assert
.IsTrue(page
.HasNext
);
208 Assert
.IsTrue(page
.HasPrevious
);
212 page
= PaginationHelper
.CreatePagination(items
, 5, 3);
214 Assert
.IsNotNull(page
);
216 Assert
.AreEqual(11, page
.FirstItem
);
217 Assert
.AreEqual(15, page
.LastItem
);
218 Assert
.AreEqual(15, page
.TotalItems
);
220 Assert
.IsTrue(page
.HasFirst
);
221 Assert
.IsFalse(page
.HasLast
);
222 Assert
.IsFalse(page
.HasNext
);
223 Assert
.IsTrue(page
.HasPrevious
);
228 public void CustomPaginationForObjectCollection()
230 IList items
= new ArrayList();
237 IPaginatedPage page
= PaginationHelper
.CreateCustomPage(items
, 3, 1, 8);
239 Assert
.IsNotNull(page
);
241 Assert
.AreEqual(1, page
.FirstItem
);
242 Assert
.AreEqual(3, page
.LastItem
);
243 Assert
.AreEqual(8, page
.TotalItems
);
245 Assert
.IsFalse(page
.HasFirst
);
246 Assert
.IsTrue(page
.HasLast
);
247 Assert
.IsTrue(page
.HasNext
);
248 Assert
.IsFalse(page
.HasPrevious
);
256 page
= PaginationHelper
.CreateCustomPage(items
, 3, 2, 8);
258 Assert
.IsNotNull(page
);
260 Assert
.AreEqual(4, page
.FirstItem
);
261 Assert
.AreEqual(6, page
.LastItem
);
262 Assert
.AreEqual(8, page
.TotalItems
);
264 Assert
.IsTrue(page
.HasFirst
);
265 Assert
.IsTrue(page
.HasLast
);
266 Assert
.IsTrue(page
.HasNext
);
267 Assert
.IsTrue(page
.HasPrevious
);
269 /* Third page, partial */
274 page
= PaginationHelper
.CreateCustomPage(items
, 3, 3, 8);
276 Assert
.IsNotNull(page
);
278 Assert
.AreEqual(7, page
.FirstItem
);
279 Assert
.AreEqual(8, page
.LastItem
);
280 Assert
.AreEqual(8, page
.TotalItems
);
282 Assert
.IsTrue(page
.HasFirst
);
283 Assert
.IsFalse(page
.HasLast
);
284 Assert
.IsFalse(page
.HasNext
);
285 Assert
.IsTrue(page
.HasPrevious
);
289 public void CustomPaginationForGenericCollection()
291 IList
<string> items
= new List
<string>();
298 IPaginatedPage page
= PaginationHelper
.CreateCustomPage(items
, 3, 1, 8);
300 Assert
.IsNotNull(page
);
302 Assert
.AreEqual(1, page
.FirstItem
);
303 Assert
.AreEqual(3, page
.LastItem
);
304 Assert
.AreEqual(8, page
.TotalItems
);
306 Assert
.IsFalse(page
.HasFirst
);
307 Assert
.IsTrue(page
.HasLast
);
308 Assert
.IsTrue(page
.HasNext
);
309 Assert
.IsFalse(page
.HasPrevious
);
317 page
= PaginationHelper
.CreateCustomPage(items
, 3, 2, 8);
319 Assert
.IsNotNull(page
);
321 Assert
.AreEqual(4, page
.FirstItem
);
322 Assert
.AreEqual(6, page
.LastItem
);
323 Assert
.AreEqual(8, page
.TotalItems
);
325 Assert
.IsTrue(page
.HasFirst
);
326 Assert
.IsTrue(page
.HasLast
);
327 Assert
.IsTrue(page
.HasNext
);
328 Assert
.IsTrue(page
.HasPrevious
);
330 /* Third page, partial */
335 page
= PaginationHelper
.CreateCustomPage(items
, 3, 3, 8);
337 Assert
.IsNotNull(page
);
339 Assert
.AreEqual(7, page
.FirstItem
);
340 Assert
.AreEqual(8, page
.LastItem
);
341 Assert
.AreEqual(8, page
.TotalItems
);
343 Assert
.IsTrue(page
.HasFirst
);
344 Assert
.IsFalse(page
.HasLast
);
345 Assert
.IsFalse(page
.HasNext
);
346 Assert
.IsTrue(page
.HasPrevious
);