1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }