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.assertEquals;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.util.Collections;
26  import java.util.Comparator;
27  import java.util.LinkedList;
28  import java.util.List;
29  
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests for ReverseComparator.
34   */
35  public class ReverseComparatorTest extends AbstractComparatorTest<Integer> {
36  
37      public ReverseComparatorTest() {
38          super(ReverseComparatorTest.class.getSimpleName());
39      }
40  
41      @Override
42      public List<Integer> getComparableObjectsOrdered() {
43          final List<Integer> list = new LinkedList<>();
44          list.add(Integer.valueOf(1));
45          list.add(Integer.valueOf(2));
46          list.add(Integer.valueOf(3));
47          list.add(Integer.valueOf(4));
48          list.add(Integer.valueOf(5));
49          return list;
50      }
51  
52      @Override
53      public String getCompatibilityVersion() {
54          return "4";
55      }
56  
57  //    public void testCreate() throws Exception {
58  //        writeExternalFormToDisk((java.io.Serializable) makeObject(), "src/test/resources/data/test/ReverseComparator.version4.obj");
59  //    }
60  
61      /**
62       * For the purposes of this test, return a
63       * ReverseComparator that wraps the java.util.Collections.reverseOrder()
64       * Comparator.  The resulting comparator should
65       * sort according to natural Order.  (Note: we wrap
66       * a Comparator taken from the JDK so that we can
67       * save a "canonical" form in SCM).
68       *
69       * @return Comparator that returns "natural" order
70       */
71      @Override
72      public Comparator<Integer> makeObject() {
73          return new ReverseComparator<>(Collections.<Integer>reverseOrder());
74      }
75  
76      /**
77       * Override this inherited test since Collections.reverseOrder
78       * doesn't adhere to the "soft" Comparator contract, and we've
79       * already "canonized" the comparator returned by makeComparator.
80       */
81      @Test
82      @Override
83      public void testSerializeDeserializeThenCompare() throws Exception {
84          final Comparator<?> comp = new ReverseComparator<>(new ComparableComparator<>());
85  
86          final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
87          final ObjectOutputStream out = new ObjectOutputStream(buffer);
88          out.writeObject(comp);
89          out.close();
90  
91          final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
92          final Object dest = in.readObject();
93          in.close();
94          assertEquals(comp, dest, "obj != deserialize(serialize(obj))");
95      }
96  
97  }