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    @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      // having rowCount 9 and 5 rows to display it looks like this (rotated, X X X X X means one page with 5 rows):
56      // X X X X X
57      // X X X X
58      Assert.assertEquals("number of pages", 2, data.getPages());
59    }
60  
61    @Test
62    public void test9RowsPerPage() {
63      data.setRows(9);
64      // X X X X X X X X X
65      Assert.assertEquals("number of pages", 1, data.getPages());
66    }
67  
68    @Test
69    public void test2RowsPerPage() {
70      data.setRows(2);
71      // X X
72      // X X
73      // X X
74      // X X
75      // X
76      Assert.assertEquals("number of pages", 5, data.getPages());
77    }
78  
79    @Test
80    public void test3RowsPerPage() {
81      data.setRows(3);
82      // X X X
83      // X X X
84      // X X X
85      Assert.assertEquals("number of pages", 3, data.getPages());
86    }
87  
88    @Test
89    public void test1RowPerPage() {
90      data.setRows(1);
91      // X
92      // X
93      // X
94      // X
95      // X
96      // X
97      // X
98      // X
99      // X
100     Assert.assertEquals("number of pages", 9, data.getPages());
101   }
102 
103   @Test
104   public void testAllRowsPerPage() {
105     data.setRows(0); // zero means all
106     // X X X X X X X X X
107     Assert.assertEquals("number of pages", 1, data.getPages());
108   }
109 
110   @Test
111   public void testCurrentPageRows5() {
112     data.setRows(5);
113     // initially first = 0
114     // * X X X X
115     // X X X X
116     Assert.assertEquals("current page", 0, data.getCurrentPage());
117     Assert.assertEquals("is at beginning", true, data.isAtBeginning());
118 
119     data.setFirst(5);
120     // now we set the first (show as an F)
121     // X X X X X
122     // * X X X
123     Assert.assertEquals("current page", 1, data.getCurrentPage());
124     Assert.assertEquals("is at beginning", false, data.isAtBeginning());
125 
126     data.setFirst(0);
127     // * X X X X
128     // X X X X
129     Assert.assertEquals("current page", 0, data.getCurrentPage());
130     Assert.assertEquals("is at beginning", true, data.isAtBeginning());
131 
132     data.setFirst(4);
133     // X X X X *
134     // X X X X
135     Assert.assertEquals("current page", 0, data.getCurrentPage());
136     Assert.assertEquals("is at beginning", false, data.isAtBeginning());
137 
138     data.setFirst(100);
139     // out of rage
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); // more rows than data entries
147     // initially first = 0
148     // * X X X X X X X X
149     Assert.assertEquals("current page", 0, data.getCurrentPage());
150     Assert.assertEquals("is at beginning", true, data.isAtBeginning());
151 
152     data.setFirst(8);
153     // now we set the first (show as an F)
154     // X X X X X X X X *
155     Assert.assertEquals("current page", 0, data.getCurrentPage());
156     Assert.assertEquals("is at beginning", false, data.isAtBeginning());
157 
158     data.setFirst(0);
159     // * X X X X X X X X
160     Assert.assertEquals("current page", 0, data.getCurrentPage());
161     Assert.assertEquals("is at beginning", true, data.isAtBeginning());
162 
163     data.setFirst(100);
164     // out of range
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 (final IllegalArgumentException e) {
180       // okay: row is unavailable
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     // known length:
209 
210     data.setRows(0);
211     // * X X X X X X X X
212     Assert.assertEquals("need more than one page", false, data.needMoreThanOnePage());
213 
214     data.setRows(5);
215     // * X X X X
216     // X X X X
217     Assert.assertEquals("need more than one page", true, data.needMoreThanOnePage());
218 
219     data.setRows(20);
220     // * X X X X X X X X
221     Assert.assertEquals("need more than one page", false, data.needMoreThanOnePage());
222 
223     // unknown length:
224 
225     unknown.setRows(0);
226     // * X X X X X X X X
227     Assert.assertEquals("need more than one page", false, unknown.needMoreThanOnePage());
228 
229     unknown.setRows(5);
230     // * X X X X
231     // X X X X
232     Assert.assertEquals("need more than one page", true, unknown.needMoreThanOnePage());
233 
234     unknown.setRows(20);
235     // * X X X X X X X X
236     Assert.assertEquals("need more than one page", true, unknown.needMoreThanOnePage());
237   }
238 
239   @Test
240   public void testFirstRowIndexOfLastPage() {
241 
242     // known length:
243 
244     data.setRows(0);
245     // * X X X X X X X X
246     Assert.assertEquals("first row index of last page", 0, data.getFirstRowIndexOfLastPage());
247 
248     data.setRows(5);
249     // * X X X X
250     // X X X X
251     Assert.assertEquals("first row index of last page", 5, data.getFirstRowIndexOfLastPage());
252 
253     data.setRows(20);
254     // * X X X X X X X X
255     Assert.assertEquals("first row index of last page", 0, data.getFirstRowIndexOfLastPage());
256 
257     // unknown length:
258 
259     unknown.setRows(0);
260     // * X X X X X X X X
261     Assert.assertEquals("first row index of last page", 0, unknown.getFirstRowIndexOfLastPage());
262 
263     unknown.setRows(5);
264     // * X X X X
265     // X X X X
266     try {
267       unknown.getFirstRowIndexOfLastPage();
268       Assert.fail("first row index of last page");
269     } catch (final IllegalArgumentException e) {
270       // okay: last page can't determined
271     }
272 
273     unknown.setRows(20);
274     // * X X X X X X X X
275     try {
276       unknown.getFirstRowIndexOfLastPage();
277       Assert.fail("first row index of last page");
278     } catch (final IllegalArgumentException e) {
279       // okay: last page can't determined
280     }
281   }
282 
283   @Test
284   public void testLastRowIndexOfCurrentPage() {
285 
286     // known length:
287 
288     data.setRows(0);
289     // * X X X X X X X X
290     Assert.assertEquals("last row index of current page", 9, data.getLastRowIndexOfCurrentPage());
291 
292     data.setRows(5);
293     // * X X X X
294     // X X X X
295     Assert.assertEquals("last row index of current page", 5, data.getLastRowIndexOfCurrentPage());
296 
297     data.setRows(20);
298     // * X X X X X X X X
299     Assert.assertEquals("last row index of current page", 9, data.getLastRowIndexOfCurrentPage());
300 
301     // unknown length:
302 
303     unknown.setRows(0);
304     // * X X X X X X X X
305     try {
306       unknown.getLastRowIndexOfCurrentPage();
307       Assert.fail("last row index of current page");
308     } catch (final IllegalArgumentException e) {
309       // okay: last row index of current page can't determined
310     }
311 
312     unknown.setRows(5);
313     // * X X X X
314     // X X X X
315     try {
316       unknown.getLastRowIndexOfCurrentPage();
317       Assert.fail("last row index of current page");
318     } catch (final IllegalArgumentException e) {
319       // okay: last row index of current page can't determined
320     }
321 
322     unknown.setRows(20);
323     // * X X X X X X X X
324     try {
325       unknown.getLastRowIndexOfCurrentPage();
326       Assert.fail("last row index of current page");
327     } catch (final IllegalArgumentException e) {
328       // okay: last row index of current page can't determined
329     }
330   }
331 
332   @Test
333   public void testGetCurrentPageOnUnknown() {
334 
335     // unknown length:
336 
337     unknown.setRows(0);
338     // * X X X X X X X X
339     Assert.assertEquals("current page", 0, unknown.getCurrentPage());
340 
341     unknown.setRows(5);
342     // * X X X X
343     // X X X X
344     Assert.assertEquals("current page", 0, unknown.getCurrentPage());
345 
346     unknown.setFirst(5);
347     // X X X X X
348     // * X X X
349     Assert.assertEquals("current page", 1, unknown.getCurrentPage());
350   }
351 
352   @Test
353   public void testGetPagesOnUnknown() {
354 
355     // unknown length:
356 
357     unknown.setRows(0);
358     // * X X X X X X X X
359     Assert.assertEquals("pages", 1, unknown.getPages());
360 
361     unknown.setRows(5);
362     // * X X X X
363     // X X X X
364     try {
365       unknown.getPages();
366       Assert.fail("pages");
367     } catch (final IllegalArgumentException e) {
368       // okay: pages can't determined
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(final List list) {
394       super(list);
395     }
396 
397     @Override
398     public int getRowCount() {
399       return -1;
400     }
401   }
402   
403 }