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  
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  
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.apache.commons.lang3.AbstractLangTest;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link ObjectToStringComparator}.
31   */
32  public class ObjectToStringComparatorTest extends AbstractLangTest {
33  
34      private static final class Thing {
35  
36          final String string;
37  
38          Thing(final String string) {
39              this.string = string;
40          }
41  
42          @Override
43          public String toString() {
44              return string;
45          }
46      }
47  
48      @Test
49      public void testNull() {
50          final List<Thing> things = Arrays.asList(null, new Thing("y"), null);
51          things.sort(ObjectToStringComparator.INSTANCE);
52          assertEquals("y", things.get(0).string);
53          assertNull(things.get(1));
54          assertNull(things.get(2));
55      }
56  
57      @Test
58      public void testNullToString() {
59          final List<Thing> things = Arrays.asList(new Thing(null), new Thing("y"), new Thing(null));
60          things.sort(ObjectToStringComparator.INSTANCE);
61          assertEquals("y", things.get(0).string);
62          assertNull(things.get(1).string);
63          assertNull(things.get(2).string);
64      }
65  
66      @Test
67      public void testSortCollection() {
68          final List<Thing> things = Arrays.asList(new Thing("z"), new Thing("y"), new Thing("x"));
69          things.sort(ObjectToStringComparator.INSTANCE);
70          assertEquals("x", things.get(0).string);
71          assertEquals("y", things.get(1).string);
72          assertEquals("z", things.get(2).string);
73      }
74  }