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  package org.apache.myfaces.trinidad.model;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  import javax.faces.model.DataModel;
25  import javax.faces.model.ListDataModel;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.myfaces.trinidad.model.CollectionModel;
31  import org.apache.myfaces.trinidad.model.SortCriterion;
32  import org.apache.myfaces.trinidad.model.SortableModel;
33  import org.apache.shale.test.base.AbstractJsfTestCase;
34  
35  /**
36   */
37  public class SortableModelTest extends AbstractJsfTestCase
38  {
39    public SortableModelTest(String name)
40    {
41      super(name);
42    }
43  
44    @Override
45    protected void setUp() throws Exception
46    {
47      super.setUp();
48    }
49  
50    @Override
51    protected void tearDown() throws Exception
52    {
53      super.tearDown();
54    }
55    
56    public static Test suite()
57    {
58      return new TestSuite(SortableModelTest.class);
59    }
60    
61    public void testInitialSort()
62    {
63      DataModel dModel = _createTestDataModel();
64      SortableModel sModel = new SortableModel(dModel);
65      _testInitialSort(sModel);
66    }
67  
68    private void _testInitialSort(SortableModel sModel)
69    {
70      sModel.setRowIndex(0);
71      assertTrue(sModel.getRowData() == _bean1);
72      sModel.setRowIndex(1);
73      assertTrue(sModel.getRowData() == _bean2);
74      sModel.setRowIndex(2);
75      assertTrue(sModel.getRowData() == _bean3);
76      sModel.setRowIndex(3);
77      assertTrue(sModel.getRowData() == _bean4);
78      sModel.setRowIndex(4);
79      assertTrue(sModel.getRowData() == _bean5);
80    }
81    
82    public void testRowCount()
83    {
84      DataModel dModel = _createTestDataModel();
85      SortableModel sModel = new SortableModel(dModel);
86      assertTrue(sModel.getRowCount() == dModel.getRowCount());    
87      _sort(sModel, "name", true);    
88      assertTrue(sModel.getRowCount() == dModel.getRowCount());    
89    }
90  
91    public void testSortableProperties()
92    {
93      DataModel dModel = _createTestDataModel();
94      SortableModel sModel = new SortableModel(dModel);
95      assertTrue(sModel.isSortable("age"));
96      assertTrue(sModel.isSortable("name"));
97      assertFalse(sModel.isSortable("object"));
98    }
99  
100   public void testSortAscendingDefault()
101   {
102     DataModel dModel = _createTestDataModel();
103     SortableModel sModel = new SortableModel(dModel);
104     _sort(sModel, "name", true);    
105 
106 
107 //    for (int i=0; i<sModel.getRowCount(); i++)
108 //    {
109 //      sModel.setRowIndex(i);
110 //      System.out.println("i:"+i+" data:"+sModel.getRowData());
111 //    }
112 
113     sModel.setRowIndex(0);
114     assertTrue(sModel.getRowData() == _bean2);
115     sModel.setRowIndex(1);
116     assertTrue(sModel.getRowData() == _bean5);
117     sModel.setRowIndex(2);
118     assertTrue(sModel.getRowData() == _bean4);
119     sModel.setRowIndex(3);
120     assertTrue(sModel.getRowData() == _bean1);
121     sModel.setRowIndex(4);
122     assertTrue(sModel.getRowData() == _bean3);
123   }
124   
125   public void testSortAscendingCaseInsensitive()
126   {
127     DataModel dModel = _createTestDataModel();
128     SortableModel sModel = new SortableModel(dModel);
129     _sort(sModel, "name", true, "Primary");    
130 
131     sModel.setRowIndex(0);
132     assertTrue(sModel.getRowData() == _bean2);
133     sModel.setRowIndex(1);
134     assertTrue(sModel.getRowData() == _bean5);
135     sModel.setRowIndex(2);
136     assertTrue(sModel.getRowData() == _bean4);
137     sModel.setRowIndex(3);
138     assertTrue(sModel.getRowData() == _bean1);
139     sModel.setRowIndex(4);
140     assertTrue(sModel.getRowData() == _bean3);
141   }
142 
143   public void testSortAscendingCaseSensitive()
144   {
145     DataModel dModel = _createTestDataModel();
146     SortableModel sModel = new SortableModel(dModel);
147     _sort(sModel, "name", true, "Identical");    
148 
149     sModel.setRowIndex(0);
150     assertTrue(sModel.getRowData() == _bean2);
151     sModel.setRowIndex(1);
152     assertTrue(sModel.getRowData() == _bean5);
153     sModel.setRowIndex(2);
154     assertTrue(sModel.getRowData() == _bean4);
155     sModel.setRowIndex(3);
156     assertTrue(sModel.getRowData() == _bean1);
157     sModel.setRowIndex(4);
158     assertTrue(sModel.getRowData() == _bean3);
159   }
160 
161   public void testUnsort()
162   {
163     DataModel dModel = _createTestDataModel();
164     SortableModel sModel = new SortableModel(dModel);
165     _sort(sModel, "name", true);    
166 
167     sModel.setSortCriteria(null);
168     _testInitialSort(sModel);
169   }
170   
171   public void testSortDescending()
172   {
173     DataModel dModel = _createTestDataModel();
174     SortableModel sModel = new SortableModel(dModel);
175     _sort(sModel, "age", false);    
176     sModel.setRowIndex(0);
177     assertTrue(sModel.getRowData() == _bean2);
178     sModel.setRowIndex(1);
179     assertTrue(sModel.getRowData() == _bean5);
180     sModel.setRowIndex(2);
181     assertTrue(sModel.getRowData() == _bean3);
182     sModel.setRowIndex(3);
183     assertTrue(sModel.getRowData() == _bean4);
184     sModel.setRowIndex(4);
185     assertTrue(sModel.getRowData() == _bean1);
186   }
187   
188 
189   // Test sorting an empty model - see bug 4258884
190   public void testEmptyTable()
191   {
192     DataModel emptyModel = new ListDataModel(new ArrayList<Bean>());
193     SortableModel sModel = new SortableModel(emptyModel);
194     _sort(sModel, "age", false);    
195   }
196 
197   public void testGetRowIndex()
198   {
199     DataModel dModel = _createTestDataModel();
200     SortableModel sModel = new SortableModel(dModel);
201     _sort(sModel, "age", true);    
202 
203     dModel.setRowIndex(2); //Zach
204     assertTrue(sModel.getRowIndex() == 2);
205     dModel.setRowIndex(0); //Tracy
206     assertTrue(sModel.getRowIndex() == 0);
207     dModel.setRowIndex(4); //ANNE
208     assertTrue(sModel.getRowIndex() == 3);
209     dModel.setRowIndex(3); //timmy
210     assertTrue(sModel.getRowIndex() == 1);
211     dModel.setRowIndex(1); //anne
212     assertTrue(sModel.getRowIndex() == 4);
213   }
214 
215   public void testSetRowIndex()
216   {
217     DataModel dModel = _createTestDataModel();
218     SortableModel sModel = new SortableModel(dModel);
219     _sort(sModel, "age", true);    
220 
221     sModel.setRowIndex(2); //Zach
222     assertTrue(dModel.getRowIndex() == 2);
223     sModel.setRowIndex(0); //Tracy
224     assertTrue(dModel.getRowIndex() == 0);
225     sModel.setRowIndex(3); //ANNE
226     assertTrue(dModel.getRowIndex() == 4);
227     sModel.setRowIndex(1); //timmy
228     assertTrue(dModel.getRowIndex() == 3);
229     sModel.setRowIndex(4); //anne
230     assertTrue(dModel.getRowIndex() == 1);
231   }
232   
233   private DataModel _createTestDataModel()
234   {
235     List<Bean> list = new ArrayList<Bean>(3);
236     list.add(_bean1);
237     list.add(_bean2);
238     list.add(_bean3);
239     list.add(_bean4);
240     list.add(_bean5);
241     return new ListDataModel(Collections.unmodifiableList(list));
242   }
243 
244   private void _sort(
245     CollectionModel model, 
246     String          property,
247     boolean         isAscending)
248   {
249     _sort(model, property, isAscending, null);
250   }
251 
252   private void _sort(
253     CollectionModel model, 
254     String          property,
255     boolean         isAscending,
256     String          strength)
257   {
258     SortStrength sortStrength = null;
259 
260     if (strength != null)
261     {
262       sortStrength = SortStrength.valueOf(strength.toUpperCase());
263     }
264 
265     SortCriterion criterion = sortStrength == null ?
266                               new SortCriterion(property, isAscending) :
267                               new SortCriterion(property, isAscending, sortStrength);
268     model.setSortCriteria(Collections.singletonList(criterion));
269   }
270   
271   private final Bean _bean1 = new Bean(10, "Tracy");
272   private final Bean _bean2 = new Bean(15, "anne");
273   private final Bean _bean3 = new Bean(12, "Zach");
274   private final Bean _bean4 = new Bean(11, "timmy");
275   private final Bean _bean5 = new Bean(14, "ANNE");
276   
277   public static final class Bean
278   {
279     public Bean(int age, String name)
280     {
281       _age = age;
282       _name = name;
283       _obj = new Object();
284     }
285     
286     public int getAge()
287     {
288       return _age;
289     }
290     
291     public String getName()
292     {
293       return _name;
294     }
295     
296     // this is not sortable
297     public Object getObject()
298     {
299       return _obj;
300     }
301     
302     @Override
303     public String toString()
304     {
305       return "name:"+_name+" age:"+_age;
306     }
307     
308     private final int _age;
309     private final String _name;
310     private final Object _obj;
311   }
312 }