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      @Override
36      @SuppressWarnings("boxing") // OK in test code
37      public List<Integer> getComparableObjectsOrdered() {
38          final List<Integer> list = new LinkedList<>();
39          list.add(1);
40          list.add(2);
41          list.add(3);
42          list.add(4);
43          list.add(5);
44          return list;
45      }
46  
47      @Override
48      public String getCompatibilityVersion() {
49          return "4";
50      }
51  
52      @Override
53      public Comparator<Integer> makeObject() {
54          final Comparator<String> decorated = new ComparableComparator<>();
55          return ComparatorUtils.transformedComparator(decorated, TransformerUtils.<Integer>stringValueTransformer());
56      }
57  
58      @Test
59      public void testEquals() {
60          final Transformer<String, String> t1 = TransformerUtils.nopTransformer();
61          final TransformingComparator<String, String> comp1 = new TransformingComparator<>(t1);
62          final TransformingComparator<String, String> comp2 = new TransformingComparator<>(t1, comp1);
63  
64          // Checks the contract: equals-hashCode on comp1 and comp2
65          assertTrue(comp1.equals(comp2) ? comp1.hashCode() == comp2.hashCode() : true,
66                  "Contract failed: equals-hashCode");
67  
68          // Checks the contract: equals-hashCode on comp1 and comp2
69          assertTrue(comp2.equals(comp1) ? comp2.hashCode() == comp1.hashCode() : true,
70                  "Contract failed: equals-hashCode");
71      }
72  
73  //    public void testCreate() throws Exception {
74  //        writeExternalFormToDisk((java.io.Serializable) makeObject(), "src/test/resources/data/test/TransformingComparator.version4.obj");
75  //    }
76  
77  }