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.util;
21  
22  import org.apache.myfaces.test.el.MockValueExpression;
23  import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
24  import org.apache.myfaces.tobago.internal.util.Fruit;
25  import org.junit.jupiter.api.Assertions;
26  import org.junit.jupiter.api.Test;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  public class ValueExpressionComparatorUnitTest extends AbstractTobagoTestBase {
32  
33    @Test
34    public void testComparingInstancesOfDifferentClasses() {
35  
36      final List<Fruit> original = Fruit.getFreshFruits();
37  
38      final ValueExpressionComparator ascendingComparator = new ValueExpressionComparator(
39          facesContext, "var", new MockValueExpression("#{var.name}", String.class), false, null);
40      final List<Fruit> ascending = new ArrayList<>(original);
41      ascending.sort(ascendingComparator);
42  
43      Assertions.assertEquals(original.get(0), ascending.get(0), "#0");
44      Assertions.assertEquals(original.get(3), ascending.get(1), "#1");
45      Assertions.assertEquals(original.get(1), ascending.get(2), "#2");
46      Assertions.assertEquals(original.get(2), ascending.get(3), "#3");
47  
48      final ValueExpressionComparator descendingComparator = new ValueExpressionComparator(
49          facesContext, "var", new MockValueExpression("#{var.name}", String.class), true, null);
50  
51      final List<Fruit> descending = new ArrayList<>(original);
52      descending.sort(descendingComparator);
53  
54      Assertions.assertEquals(original.get(2), descending.get(0), "#0");
55      Assertions.assertEquals(original.get(1), descending.get(1), "#1");
56      Assertions.assertEquals(original.get(3), descending.get(2), "#2");
57      Assertions.assertEquals(original.get(0), descending.get(3), "#3");
58    }
59  }