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    *      https://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  
18  package org.apache.commons.lang3.compare;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  
24  import java.util.Arrays;
25  import java.util.Comparator;
26  import java.util.List;
27  
28  import org.apache.commons.lang3.AbstractLangTest;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests {@link ObjectToStringComparator}.
33   */
34  class ObjectToStringComparatorTest extends AbstractLangTest {
35  
36      private static final class Thing {
37  
38          final String string;
39  
40          Thing(final String string) {
41              this.string = string;
42          }
43  
44          @Override
45          public String toString() {
46              return string;
47          }
48      }
49  
50      @Test
51      void testCompareNulls() {
52          final Comparator<Object> comp = ObjectToStringComparator.INSTANCE;
53          assertEquals(0, comp.compare(null, null));
54          final Object o1 = new Thing(null);
55          final Object o2 = new Thing(null);
56          assertEquals(0, comp.compare(o1, o1));
57          assertEquals(0, comp.compare(o1, o2));
58      }
59  
60      @Test
61      void testCompareNullWithNonNull() {
62          final Comparator<Object> comp = ObjectToStringComparator.INSTANCE;
63          final Object o1 = new Thing(null);
64          final Object o2 = new Thing("");
65          assertEquals(0, comp.compare(o1, o1));
66          assertEquals(0, comp.compare(o2, o2));
67          assertEquals(1, comp.compare(o1, o2));
68          assertEquals(-1, comp.compare(o2, o1));
69          assertEquals(1, comp.compare(null, o2));
70          assertEquals(-1, comp.compare(o2, null));
71      }
72  
73      @Test
74      void testNullLeft() {
75          final Thing thing = new Thing("y");
76          final List<Thing> things = Arrays.asList(null, thing);
77          things.sort(ObjectToStringComparator.INSTANCE);
78          assertEquals("y", things.get(0).string);
79          assertEquals(2, things.size());
80          assertSame(thing, things.get(0));
81          assertNull(things.get(1));
82      }
83  
84      @Test
85      void testNullRight() {
86          final Thing thing = new Thing("y");
87          final List<Thing> things = Arrays.asList(thing, null);
88          things.sort(ObjectToStringComparator.INSTANCE);
89          assertEquals(2, things.size());
90          assertSame(thing, things.get(0));
91          assertNull(things.get(1));
92      }
93  
94      @Test
95      void testNulls() {
96          final Thing thing = new Thing("y");
97          final List<Thing> things = Arrays.asList(null, thing, null);
98          things.sort(ObjectToStringComparator.INSTANCE);
99          assertEquals("y", things.get(0).string);
100         assertEquals(3, things.size());
101         assertSame(thing, things.get(0));
102         assertNull(things.get(1));
103         assertNull(things.get(2));
104     }
105 
106     @Test
107     void testNullToString() {
108         final List<Thing> things = Arrays.asList(new Thing(null), new Thing("y"), new Thing(null));
109         things.sort(ObjectToStringComparator.INSTANCE);
110         assertEquals("y", things.get(0).string);
111         assertNull(things.get(1).string);
112         assertNull(things.get(2).string);
113     }
114 
115     @Test
116     void testSortCollection() {
117         final List<Thing> things = Arrays.asList(new Thing("z"), new Thing("y"), new Thing("x"));
118         things.sort(ObjectToStringComparator.INSTANCE);
119         assertEquals("x", things.get(0).string);
120         assertEquals("y", things.get(1).string);
121         assertEquals("z", things.get(2).string);
122     }
123 }