View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.component;
21  
22  import org.apache.myfaces.tobago.internal.config.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    @Override
40    @Before
41    public void setUp() throws Exception {
42      super.setUp();
43      nineRows = new ArrayList<String>();
44      Collections.addAll(nineRows, DATA);
45  
46      data = new UISheet();
47      data.setValue(new ListDataModel(nineRows));
48  
49      unknown = new UISheet();
50      unknown.setValue(new DataModelWithUnknownRows(nineRows));
51    }
52    
53    @Test
54    public void test5RowsPerPage() {
55      data.setRows(5);
56      // having rowCount 9 and 5 rows to display it looks like this (rotated, X X X X X means one page with 5 rows):
57      // X X X X X
58      // X X X X
59      Assert.assertEquals("number of pages", 2, data.getPages());
60    }
61  
62    @Test
63    public void test9RowsPerPage() {
64      data.setRows(9);
65      // X X X X X X X X X
66      Assert.assertEquals("number of pages", 1, data.getPages());
67    }
68  
69    @Test
70    public void test2RowsPerPage() {
71      data.setRows(2);
72      // X X
73      // X X
74      // X X
75      // X X
76      // X
77      Assert.assertEquals("number of pages", 5, data.getPages());
78    }
79  
80    @Test
81    public void test3RowsPerPage() {
82      data.setRows(3);
83      // X X X
84      // X X X
85      // X X X
86      Assert.assertEquals("number of pages", 3, data.getPages());
87    }
88  
89    @Test
90    public void test1RowPerPage() {
91      data.setRows(1);
92      // X
93      // X
94      // X
95      // X
96      // X
97      // X
98      // X
99      // X
100     // X
101     Assert.assertEquals("number of pages", 9, data.getPages());
102   }
103 
104   @Test
105   public void testAllRowsPerPage() {
106     data.setRows(0); // zero means all
107     // X X X X X X X X X
108     Assert.assertEquals("number of pages", 1, data.getPages());
109   }
110 
111   @Test
112   public void testCurrentPageRows5() {
113     data.setRows(5);
114     // initially first = 0
115     // * X X X X
116     // X X X X
117     Assert.assertEquals("current page", 0, data.getCurrentPage());
118     Assert.assertEquals("is at beginning", true, data.isAtBeginning());
119 
120     data.setFirst(5);
121     // now we set the first (show as an F)
122     // X X X X X
123     // * X X X
124     Assert.assertEquals("current page", 1, data.getCurrentPage());
125     Assert.assertEquals("is at beginning", false, data.isAtBeginning());
126 
127     data.setFirst(0);
128     // * X X X X
129     // X X X X
130     Assert.assertEquals("current page", 0, data.getCurrentPage());
131     Assert.assertEquals("is at beginning", true, data.isAtBeginning());
132 
133     data.setFirst(4);
134     // X X X X *
135     // X X X X
136     Assert.assertEquals("current page", 0, data.getCurrentPage());
137     Assert.assertEquals("is at beginning", false, data.isAtBeginning());
138 
139     data.setFirst(100);
140     // out of rage
141     Assert.assertEquals("current page", 1, data.getCurrentPage());
142     Assert.assertEquals("is at beginning", false, data.isAtBeginning());
143   }
144 
145   @Test
146   public void testCurrentPageRows20() {
147     data.setRows(20); // more rows than data entries
148     // initially first = 0
149     // * X X X X X X X X
150     Assert.assertEquals("current page", 0, data.getCurrentPage());
151     Assert.assertEquals("is at beginning", true, data.isAtBeginning());
152 
153     data.setFirst(8);
154     // now we set the first (show as an F)
155     // X X X X X X X X *
156     Assert.assertEquals("current page", 0, data.getCurrentPage());
157     Assert.assertEquals("is at beginning", false, data.isAtBeginning());
158 
159     data.setFirst(0);
160     // * X X X X X X X X
161     Assert.assertEquals("current page", 0, data.getCurrentPage());
162     Assert.assertEquals("is at beginning", true, data.isAtBeginning());
163 
164     data.setFirst(100);
165     // out of range
166     Assert.assertEquals("current page", 0, data.getCurrentPage());
167     Assert.assertEquals("is at beginning", false, data.isAtBeginning());
168   }
169 
170   @Test
171   public void testRowData() {
172     data.setRowIndex(0);
173     Assert.assertEquals("one", data.getRowData());
174     data.setRowIndex(8);
175     Assert.assertEquals("nine", data.getRowData());
176     data.setRowIndex(9);
177     try {
178       data.getRowData();
179       Assert.fail();
180     } catch (final IllegalArgumentException e) {
181       // okay: row is unavailable
182     }
183   }
184 
185   @Test
186   public void testHasRowCount() {
187     Assert.assertEquals("has row count", true, data.hasRowCount());
188     Assert.assertEquals("has row count", false, unknown.hasRowCount());
189   }
190 
191   @Test
192   public void testRowsUnlimited() {
193     data.setRows(5);
194     unknown.setRows(5);
195 
196     Assert.assertEquals("unlimited rows", false, data.isRowsUnlimited());
197     Assert.assertEquals("unlimited rows", false, unknown.isRowsUnlimited());
198 
199     data.setRows(0);
200     unknown.setRows(0);
201 
202     Assert.assertEquals("unlimited rows", true, data.isRowsUnlimited());
203     Assert.assertEquals("unlimited rows", true, unknown.isRowsUnlimited());
204   }
205 
206   @Test
207   public void testNeedMoreThanOnePage() {
208 
209     // known length:
210 
211     data.setRows(0);
212     // * X X X X X X X X
213     Assert.assertEquals("need more than one page", false, data.needMoreThanOnePage());
214 
215     data.setRows(5);
216     // * X X X X
217     // X X X X
218     Assert.assertEquals("need more than one page", true, data.needMoreThanOnePage());
219 
220     data.setRows(20);
221     // * X X X X X X X X
222     Assert.assertEquals("need more than one page", false, data.needMoreThanOnePage());
223 
224     // unknown length:
225 
226     unknown.setRows(0);
227     // * X X X X X X X X
228     Assert.assertEquals("need more than one page", false, unknown.needMoreThanOnePage());
229 
230     unknown.setRows(5);
231     // * X X X X
232     // X X X X
233     Assert.assertEquals("need more than one page", true, unknown.needMoreThanOnePage());
234 
235     unknown.setRows(20);
236     // * X X X X X X X X
237     Assert.assertEquals("need more than one page", true, unknown.needMoreThanOnePage());
238   }
239 
240   @Test
241   public void testFirstRowIndexOfLastPage() {
242 
243     // known length:
244 
245     data.setRows(0);
246     // * X X X X X X X X
247     Assert.assertEquals("first row index of last page", 0, data.getFirstRowIndexOfLastPage());
248 
249     data.setRows(5);
250     // * X X X X
251     // X X X X
252     Assert.assertEquals("first row index of last page", 5, data.getFirstRowIndexOfLastPage());
253 
254     data.setRows(20);
255     // * X X X X X X X X
256     Assert.assertEquals("first row index of last page", 0, data.getFirstRowIndexOfLastPage());
257 
258     // unknown length:
259 
260     unknown.setRows(0);
261     // * X X X X X X X X
262     Assert.assertEquals("first row index of last page", 0, unknown.getFirstRowIndexOfLastPage());
263 
264     unknown.setRows(5);
265     // * X X X X
266     // X X X X
267     try {
268       unknown.getFirstRowIndexOfLastPage();
269       Assert.fail("first row index of last page");
270     } catch (final IllegalArgumentException e) {
271       // okay: last page can't determined
272     }
273 
274     unknown.setRows(20);
275     // * X X X X X X X X
276     try {
277       unknown.getFirstRowIndexOfLastPage();
278       Assert.fail("first row index of last page");
279     } catch (final IllegalArgumentException e) {
280       // okay: last page can't determined
281     }
282   }
283 
284   @Test
285   public void testLastRowIndexOfCurrentPage() {
286 
287     // known length:
288 
289     data.setRows(0);
290     // * X X X X X X X X
291     Assert.assertEquals("last row index of current page", 9, data.getLastRowIndexOfCurrentPage());
292 
293     data.setRows(5);
294     // * X X X X
295     // X X X X
296     Assert.assertEquals("last row index of current page", 5, data.getLastRowIndexOfCurrentPage());
297 
298     data.setRows(20);
299     // * X X X X X X X X
300     Assert.assertEquals("last row index of current page", 9, data.getLastRowIndexOfCurrentPage());
301 
302     // unknown length:
303 
304     unknown.setRows(0);
305     // * X X X X X X X X
306     try {
307       unknown.getLastRowIndexOfCurrentPage();
308       Assert.fail("last row index of current page");
309     } catch (final IllegalArgumentException e) {
310       // okay: last row index of current page can't determined
311     }
312 
313     unknown.setRows(5);
314     // * X X X X
315     // X X X X
316     try {
317       unknown.getLastRowIndexOfCurrentPage();
318       Assert.fail("last row index of current page");
319     } catch (final IllegalArgumentException e) {
320       // okay: last row index of current page can't determined
321     }
322 
323     unknown.setRows(20);
324     // * X X X X X X X X
325     try {
326       unknown.getLastRowIndexOfCurrentPage();
327       Assert.fail("last row index of current page");
328     } catch (final IllegalArgumentException e) {
329       // okay: last row index of current page can't determined
330     }
331   }
332 
333   @Test
334   public void testGetCurrentPageOnUnknown() {
335 
336     // unknown length:
337 
338     unknown.setRows(0);
339     // * X X X X X X X X
340     Assert.assertEquals("current page", 0, unknown.getCurrentPage());
341 
342     unknown.setRows(5);
343     // * X X X X
344     // X X X X
345     Assert.assertEquals("current page", 0, unknown.getCurrentPage());
346 
347     unknown.setFirst(5);
348     // X X X X X
349     // * X X X
350     Assert.assertEquals("current page", 1, unknown.getCurrentPage());
351   }
352 
353   @Test
354   public void testGetPagesOnUnknown() {
355 
356     // unknown length:
357 
358     unknown.setRows(0);
359     // * X X X X X X X X
360     Assert.assertEquals("pages", 1, unknown.getPages());
361 
362     unknown.setRows(5);
363     // * X X X X
364     // X X X X
365     try {
366       unknown.getPages();
367       Assert.fail("pages");
368     } catch (final IllegalArgumentException e) {
369       // okay: pages can't determined
370     }
371   }
372 
373   @Test
374   public void testDynamicRemoval() {
375     nineRows.remove(0);
376     nineRows.remove(0);
377     nineRows.remove(0);
378     data.setRows(5);
379     Assert.assertEquals(2, data.getPages());
380     nineRows.remove(0);
381     nineRows.remove(0);
382     nineRows.remove(0);
383     Assert.assertEquals(1, data.getPages());
384   }
385 
386   @Test
387   public void testStripRowIndex() {
388     Assert.assertEquals("comp1:comp2", new UISheet().stripRowIndex("123:comp1:comp2"));
389     Assert.assertEquals("comp1:comp2", new UISheet().stripRowIndex("comp1:comp2"));
390   }
391 
392   private static class DataModelWithUnknownRows extends ListDataModel {
393 
394     public DataModelWithUnknownRows(final List list) {
395       super(list);
396     }
397 
398     @Override
399     public int getRowCount() {
400       return -1;
401     }
402   }
403   
404 }