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.List;
26  
27  import org.apache.commons.lang3.AbstractLangTest;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link ObjectToStringComparator}.
32   */
33  class ObjectToStringComparatorTest extends AbstractLangTest {
34  
35      private static final class Thing {
36  
37          final String string;
38  
39          Thing(final String string) {
40              this.string = string;
41          }
42  
43          @Override
44          public String toString() {
45              return string;
46          }
47      }
48  
49      @Test
50      void testNullLeft() {
51          final Thing thing = new Thing("y");
52          final List<Thing> things = Arrays.asList(null, thing);
53          things.sort(ObjectToStringComparator.INSTANCE);
54          assertEquals("y", things.get(0).string);
55          assertEquals(2, things.size());
56          assertSame(thing, things.get(0));
57          assertNull(things.get(1));
58      }
59  
60      @Test
61      void testNullRight() {
62          final Thing thing = new Thing("y");
63          final List<Thing> things = Arrays.asList(thing, null);
64          things.sort(ObjectToStringComparator.INSTANCE);
65          assertEquals(2, things.size());
66          assertSame(thing, things.get(0));
67          assertNull(things.get(1));
68      }
69  
70      @Test
71      void testNulls() {
72          final Thing thing = new Thing("y");
73          final List<Thing> things = Arrays.asList(null, thing, null);
74          things.sort(ObjectToStringComparator.INSTANCE);
75          assertEquals("y", things.get(0).string);
76          assertEquals(3, things.size());
77          assertSame(thing, things.get(0));
78          assertNull(things.get(1));
79          assertNull(things.get(2));
80      }
81  
82      @Test
83      void testNullToString() {
84          final List<Thing> things = Arrays.asList(new Thing(null), new Thing("y"), new Thing(null));
85          things.sort(ObjectToStringComparator.INSTANCE);
86          assertEquals("y", things.get(0).string);
87          assertNull(things.get(1).string);
88          assertNull(things.get(2).string);
89      }
90  
91      @Test
92      void testSortCollection() {
93          final List<Thing> things = Arrays.asList(new Thing("z"), new Thing("y"), new Thing("x"));
94          things.sort(ObjectToStringComparator.INSTANCE);
95          assertEquals("x", things.get(0).string);
96          assertEquals("y", things.get(1).string);
97          assertEquals("z", things.get(2).string);
98      }
99  }