View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.comparators;
18  
19  import static org.junit.jupiter.api.Assertions.assertTrue;
20  
21  import java.util.Comparator;
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  import org.apache.commons.collections4.ComparatorUtils;
26  import org.apache.commons.collections4.Transformer;
27  import org.apache.commons.collections4.TransformerUtils;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test class for TransformingComparator.
32   */
33  public class TransformingComparatorTest extends AbstractComparatorTest<Integer> {
34  
35      //
36      // Initialization and busywork
37      //
38  
39      public TransformingComparatorTest() {
40          super(TransformingComparatorTest.class.getSimpleName());
41      }
42  
43      //
44      // Set up and tear down
45      //
46  
47      @Override
48      @SuppressWarnings("boxing") // OK in test code
49      public List<Integer> getComparableObjectsOrdered() {
50          final List<Integer> list = new LinkedList<>();
51          list.add(1);
52          list.add(2);
53          list.add(3);
54          list.add(4);
55          list.add(5);
56          return list;
57      }
58  
59      @Override
60      public String getCompatibilityVersion() {
61          return "4";
62      }
63  
64      @Override
65      public Comparator<Integer> makeObject() {
66          final Comparator<String> decorated = new ComparableComparator<>();
67          return ComparatorUtils.transformedComparator(decorated, TransformerUtils.<Integer>stringValueTransformer());
68      }
69  
70      @Test
71      public void testEquals() {
72          final Transformer<String, String> t1 = TransformerUtils.nopTransformer();
73          final TransformingComparator<String, String> comp1 = new TransformingComparator<>(t1);
74          final TransformingComparator<String, String> comp2 = new TransformingComparator<>(t1, comp1);
75  
76          // Checks the contract: equals-hashCode on comp1 and comp2
77          assertTrue(comp1.equals(comp2) ? comp1.hashCode() == comp2.hashCode() : true,
78                  "Contract failed: equals-hashCode");
79  
80          // Checks the contract: equals-hashCode on comp1 and comp2
81          assertTrue(comp2.equals(comp1) ? comp2.hashCode() == comp1.hashCode() : true,
82                  "Contract failed: equals-hashCode");
83      }
84  
85  //    public void testCreate() throws Exception {
86  //        writeExternalFormToDisk((java.io.Serializable) makeObject(), "src/test/resources/data/test/TransformingComparator.version4.obj");
87  //    }
88  
89      //
90      // The tests
91      //
92  
93  }