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.internal.util;
21  
22  import org.apache.myfaces.test.el.MockValueExpression;
23  import org.apache.myfaces.tobago.component.Attributes;
24  import org.apache.myfaces.tobago.component.UIColumn;
25  import org.apache.myfaces.tobago.component.UILink;
26  import org.apache.myfaces.tobago.component.UIOut;
27  import org.apache.myfaces.tobago.component.UISheet;
28  import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
29  import org.junit.jupiter.api.Assertions;
30  import org.junit.jupiter.api.Test;
31  
32  import java.util.List;
33  
34  public class SortingUtilsUnitTest extends AbstractTobagoTestBase {
35  
36      @Test
37      public void testSheetValueNull() {
38          final UISheet sheet = new UISheet();
39          final UIColumn column = new UIColumn();
40          sheet.getChildren().add(column);
41          SortingUtils.sort(sheet, null);
42      }
43  
44      @Test
45      public void testNoChild() {
46          final List<Fruit> list = Fruit.getFreshFruits();
47  
48          final UISheet sheet = new UISheet();
49          sheet.getState().setSortedColumnId("id");
50          final UIColumn column = new UIColumn();
51          column.setId("id");
52          sheet.getChildren().add(column);
53          sheet.setValue(list);
54          SortingUtils.sort(sheet, null);
55  
56          // not sorted
57          Assertions.assertEquals(Apple.GOLDEN_DELICIOUS, list.get(0));
58          Assertions.assertEquals(Apple.SCHOENER_AUS_BOSKOOP, list.get(1));
59          Assertions.assertEquals(Pear.WILLIAMS_CHRIST, list.get(2));
60          Assertions.assertEquals(Pear.KOESTLICHE_AUS_CHARNEUX, list.get(3));
61      }
62  
63      @Test
64      public void testUIOut() {
65          final List<Fruit> list = Fruit.getFreshFruits();
66          final UISheet sheet = new UISheet();
67          sheet.getState().setSortedColumnId("id");
68          sheet.setVar("var");
69          final UIColumn column = new UIColumn();
70          column.setId("id");
71          sheet.getChildren().add(column);
72          sheet.setValue(list);
73          final UIOut out = new UIOut();
74          column.getChildren().add(out);
75          out.setValueExpression(Attributes.value.getName(),
76              new MockValueExpression("#{var.name}", String.class));
77          Assertions.assertNotNull(out.getValueExpression(Attributes.value.getName()));
78  
79          SortingUtils.sort(sheet, null);
80  
81          Assertions.assertEquals(Apple.GOLDEN_DELICIOUS, list.get(0));
82          Assertions.assertEquals(Pear.KOESTLICHE_AUS_CHARNEUX, list.get(1));
83          Assertions.assertEquals(Apple.SCHOENER_AUS_BOSKOOP, list.get(2));
84          Assertions.assertEquals(Pear.WILLIAMS_CHRIST, list.get(3));
85  
86          sheet.getState().setAscending(false);
87          SortingUtils.sort(sheet, null);
88  
89          Assertions.assertEquals(Pear.WILLIAMS_CHRIST, list.get(0));
90          Assertions.assertEquals(Apple.SCHOENER_AUS_BOSKOOP, list.get(1));
91          Assertions.assertEquals(Pear.KOESTLICHE_AUS_CHARNEUX, list.get(2));
92          Assertions.assertEquals(Apple.GOLDEN_DELICIOUS, list.get(3));
93      }
94  
95      @Test
96      public void testUILink() {
97          final List<Fruit> list = Fruit.getFreshFruits();
98          final UISheet sheet = new UISheet();
99          sheet.getState().setSortedColumnId("id");
100         final UIColumn column = new UIColumn();
101         column.setId("id");
102         sheet.getChildren().add(column);
103         sheet.setValue(list);
104         final UILink link = new UILink();
105         column.getChildren().add(link);
106         link.setValueExpression(Attributes.label.getName(),
107             new MockValueExpression("#{var.name}", String.class));
108 
109         Assertions.assertNotNull(link.getValueExpression(Attributes.label.getName()));
110 
111         SortingUtils.sort(sheet, null);
112 
113         Assertions.assertEquals(Apple.GOLDEN_DELICIOUS, list.get(0));
114         Assertions.assertEquals(Apple.SCHOENER_AUS_BOSKOOP, list.get(1));
115         Assertions.assertEquals(Pear.WILLIAMS_CHRIST, list.get(2));
116         Assertions.assertEquals(Pear.KOESTLICHE_AUS_CHARNEUX, list.get(3));
117     }
118 
119 }