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