1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.component;
21
22 import org.apache.myfaces.tobago.internal.mock.faces.AbstractTobagoTestBase;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26
27 import javax.faces.model.ListDataModel;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31
32 public class UISheetUnitTest extends AbstractTobagoTestBase {
33
34 private static final String[] DATA = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
35 private List<String> nineRows;
36 private UISheet data;
37 private UISheet unknown;
38
39 @Before
40 public void setUp() throws Exception {
41 super.setUp();
42 nineRows = new ArrayList<String>();
43 Collections.addAll(nineRows, DATA);
44
45 data = new UISheet();
46 data.setValue(new ListDataModel(nineRows));
47
48 unknown = new UISheet();
49 unknown.setValue(new DataModelWithUnknownRows(nineRows));
50 }
51
52 @Test
53 public void test5RowsPerPage() {
54 data.setRows(5);
55
56
57
58 Assert.assertEquals("number of pages", 2, data.getPages());
59 }
60
61 @Test
62 public void test9RowsPerPage() {
63 data.setRows(9);
64
65 Assert.assertEquals("number of pages", 1, data.getPages());
66 }
67
68 @Test
69 public void test2RowsPerPage() {
70 data.setRows(2);
71
72
73
74
75
76 Assert.assertEquals("number of pages", 5, data.getPages());
77 }
78
79 @Test
80 public void test3RowsPerPage() {
81 data.setRows(3);
82
83
84
85 Assert.assertEquals("number of pages", 3, data.getPages());
86 }
87
88 @Test
89 public void test1RowPerPage() {
90 data.setRows(1);
91
92
93
94
95
96
97
98
99
100 Assert.assertEquals("number of pages", 9, data.getPages());
101 }
102
103 @Test
104 public void testAllRowsPerPage() {
105 data.setRows(0);
106
107 Assert.assertEquals("number of pages", 1, data.getPages());
108 }
109
110 @Test
111 public void testCurrentPageRows5() {
112 data.setRows(5);
113
114
115
116 Assert.assertEquals("current page", 0, data.getCurrentPage());
117 Assert.assertEquals("is at beginning", true, data.isAtBeginning());
118
119 data.setFirst(5);
120
121
122
123 Assert.assertEquals("current page", 1, data.getCurrentPage());
124 Assert.assertEquals("is at beginning", false, data.isAtBeginning());
125
126 data.setFirst(0);
127
128
129 Assert.assertEquals("current page", 0, data.getCurrentPage());
130 Assert.assertEquals("is at beginning", true, data.isAtBeginning());
131
132 data.setFirst(4);
133
134
135 Assert.assertEquals("current page", 0, data.getCurrentPage());
136 Assert.assertEquals("is at beginning", false, data.isAtBeginning());
137
138 data.setFirst(100);
139
140 Assert.assertEquals("current page", 1, data.getCurrentPage());
141 Assert.assertEquals("is at beginning", false, data.isAtBeginning());
142 }
143
144 @Test
145 public void testCurrentPageRows20() {
146 data.setRows(20);
147
148
149 Assert.assertEquals("current page", 0, data.getCurrentPage());
150 Assert.assertEquals("is at beginning", true, data.isAtBeginning());
151
152 data.setFirst(8);
153
154
155 Assert.assertEquals("current page", 0, data.getCurrentPage());
156 Assert.assertEquals("is at beginning", false, data.isAtBeginning());
157
158 data.setFirst(0);
159
160 Assert.assertEquals("current page", 0, data.getCurrentPage());
161 Assert.assertEquals("is at beginning", true, data.isAtBeginning());
162
163 data.setFirst(100);
164
165 Assert.assertEquals("current page", 0, data.getCurrentPage());
166 Assert.assertEquals("is at beginning", false, data.isAtBeginning());
167 }
168
169 @Test
170 public void testRowData() {
171 data.setRowIndex(0);
172 Assert.assertEquals("one", data.getRowData());
173 data.setRowIndex(8);
174 Assert.assertEquals("nine", data.getRowData());
175 data.setRowIndex(9);
176 try {
177 data.getRowData();
178 Assert.fail();
179 } catch (IllegalArgumentException e) {
180
181 }
182 }
183
184 @Test
185 public void testHasRowCount() {
186 Assert.assertEquals("has row count", true, data.hasRowCount());
187 Assert.assertEquals("has row count", false, unknown.hasRowCount());
188 }
189
190 @Test
191 public void testRowsUnlimited() {
192 data.setRows(5);
193 unknown.setRows(5);
194
195 Assert.assertEquals("unlimited rows", false, data.isRowsUnlimited());
196 Assert.assertEquals("unlimited rows", false, unknown.isRowsUnlimited());
197
198 data.setRows(0);
199 unknown.setRows(0);
200
201 Assert.assertEquals("unlimited rows", true, data.isRowsUnlimited());
202 Assert.assertEquals("unlimited rows", true, unknown.isRowsUnlimited());
203 }
204
205 @Test
206 public void testNeedMoreThanOnePage() {
207
208
209
210 data.setRows(0);
211
212 Assert.assertEquals("need more than one page", false, data.needMoreThanOnePage());
213
214 data.setRows(5);
215
216
217 Assert.assertEquals("need more than one page", true, data.needMoreThanOnePage());
218
219 data.setRows(20);
220
221 Assert.assertEquals("need more than one page", false, data.needMoreThanOnePage());
222
223
224
225 unknown.setRows(0);
226
227 Assert.assertEquals("need more than one page", false, unknown.needMoreThanOnePage());
228
229 unknown.setRows(5);
230
231
232 Assert.assertEquals("need more than one page", true, unknown.needMoreThanOnePage());
233
234 unknown.setRows(20);
235
236 Assert.assertEquals("need more than one page", true, unknown.needMoreThanOnePage());
237 }
238
239 @Test
240 public void testFirstRowIndexOfLastPage() {
241
242
243
244 data.setRows(0);
245
246 Assert.assertEquals("first row index of last page", 0, data.getFirstRowIndexOfLastPage());
247
248 data.setRows(5);
249
250
251 Assert.assertEquals("first row index of last page", 5, data.getFirstRowIndexOfLastPage());
252
253 data.setRows(20);
254
255 Assert.assertEquals("first row index of last page", 0, data.getFirstRowIndexOfLastPage());
256
257
258
259 unknown.setRows(0);
260
261 Assert.assertEquals("first row index of last page", 0, unknown.getFirstRowIndexOfLastPage());
262
263 unknown.setRows(5);
264
265
266 try {
267 unknown.getFirstRowIndexOfLastPage();
268 Assert.fail("first row index of last page");
269 } catch (IllegalArgumentException e) {
270
271 }
272
273 unknown.setRows(20);
274
275 try {
276 unknown.getFirstRowIndexOfLastPage();
277 Assert.fail("first row index of last page");
278 } catch (IllegalArgumentException e) {
279
280 }
281 }
282
283 @Test
284 public void testLastRowIndexOfCurrentPage() {
285
286
287
288 data.setRows(0);
289
290 Assert.assertEquals("last row index of current page", 9, data.getLastRowIndexOfCurrentPage());
291
292 data.setRows(5);
293
294
295 Assert.assertEquals("last row index of current page", 5, data.getLastRowIndexOfCurrentPage());
296
297 data.setRows(20);
298
299 Assert.assertEquals("last row index of current page", 9, data.getLastRowIndexOfCurrentPage());
300
301
302
303 unknown.setRows(0);
304
305 try {
306 unknown.getLastRowIndexOfCurrentPage();
307 Assert.fail("last row index of current page");
308 } catch (IllegalArgumentException e) {
309
310 }
311
312 unknown.setRows(5);
313
314
315 try {
316 unknown.getLastRowIndexOfCurrentPage();
317 Assert.fail("last row index of current page");
318 } catch (IllegalArgumentException e) {
319
320 }
321
322 unknown.setRows(20);
323
324 try {
325 unknown.getLastRowIndexOfCurrentPage();
326 Assert.fail("last row index of current page");
327 } catch (IllegalArgumentException e) {
328
329 }
330 }
331
332 @Test
333 public void testGetCurrentPageOnUnknown() {
334
335
336
337 unknown.setRows(0);
338
339 Assert.assertEquals("current page", 0, unknown.getCurrentPage());
340
341 unknown.setRows(5);
342
343
344 Assert.assertEquals("current page", 0, unknown.getCurrentPage());
345
346 unknown.setFirst(5);
347
348
349 Assert.assertEquals("current page", 1, unknown.getCurrentPage());
350 }
351
352 @Test
353 public void testGetPagesOnUnknown() {
354
355
356
357 unknown.setRows(0);
358
359 Assert.assertEquals("pages", 1, unknown.getPages());
360
361 unknown.setRows(5);
362
363
364 try {
365 unknown.getPages();
366 Assert.fail("pages");
367 } catch (IllegalArgumentException e) {
368
369 }
370 }
371
372 @Test
373 public void testDynamicRemoval() {
374 nineRows.remove(0);
375 nineRows.remove(0);
376 nineRows.remove(0);
377 data.setRows(5);
378 Assert.assertEquals(2, data.getPages());
379 nineRows.remove(0);
380 nineRows.remove(0);
381 nineRows.remove(0);
382 Assert.assertEquals(1, data.getPages());
383 }
384
385 @Test
386 public void testStripRowIndex() {
387 Assert.assertEquals("comp1:comp2", new UISheet().stripRowIndex("123:comp1:comp2"));
388 Assert.assertEquals("comp1:comp2", new UISheet().stripRowIndex("comp1:comp2"));
389 }
390
391 private static class DataModelWithUnknownRows extends ListDataModel {
392
393 public DataModelWithUnknownRows(List list) {
394 super(list);
395 }
396
397 @Override
398 public int getRowCount() {
399 return -1;
400 }
401 }
402
403 }