1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.tuple;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNull;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.Calendar;
25 import java.util.HashSet;
26
27 import org.apache.commons.lang3.AbstractLangTest;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 public class TripleTest extends AbstractLangTest {
34
35 @Test
36 public void testComparable1() {
37 final Triple<String, String, String> triple1 = Triple.of("A", "D", "A");
38 final Triple<String, String, String> triple2 = Triple.of("B", "C", "A");
39 assertEquals(0, triple1.compareTo(triple1));
40 assertTrue(triple1.compareTo(triple2) < 0);
41 assertEquals(0, triple2.compareTo(triple2));
42 assertTrue(triple2.compareTo(triple1) > 0);
43 }
44
45 @Test
46 public void testComparable2() {
47 final Triple<String, String, String> triple1 = Triple.of("A", "C", "B");
48 final Triple<String, String, String> triple2 = Triple.of("A", "D", "B");
49 assertEquals(0, triple1.compareTo(triple1));
50 assertTrue(triple1.compareTo(triple2) < 0);
51 assertEquals(0, triple2.compareTo(triple2));
52 assertTrue(triple2.compareTo(triple1) > 0);
53 }
54
55 @Test
56 public void testComparable3() {
57 final Triple<String, String, String> triple1 = Triple.of("A", "A", "D");
58 final Triple<String, String, String> triple2 = Triple.of("A", "B", "C");
59 assertEquals(0, triple1.compareTo(triple1));
60 assertTrue(triple1.compareTo(triple2) < 0);
61 assertEquals(0, triple2.compareTo(triple2));
62 assertTrue(triple2.compareTo(triple1) > 0);
63 }
64
65 @Test
66 public void testComparable4() {
67 final Triple<String, String, String> triple1 = Triple.of("B", "A", "C");
68 final Triple<String, String, String> triple2 = Triple.of("B", "A", "D");
69 assertEquals(0, triple1.compareTo(triple1));
70 assertTrue(triple1.compareTo(triple2) < 0);
71 assertEquals(0, triple2.compareTo(triple2));
72 assertTrue(triple2.compareTo(triple1) > 0);
73 }
74
75 @Test
76 public void testCompatibilityBetweenTriples() {
77 final Triple<Integer, String, Boolean> triple = ImmutableTriple.of(0, "foo", Boolean.TRUE);
78 final Triple<Integer, String, Boolean> triple2 = MutableTriple.of(0, "foo", Boolean.TRUE);
79 assertEquals(triple, triple2);
80 assertEquals(triple.hashCode(), triple2.hashCode());
81 final HashSet<Triple<Integer, String, Boolean>> set = new HashSet<>();
82 set.add(triple);
83 assertTrue(set.contains(triple2));
84 }
85
86 @Test
87 public void testEmptyArrayGenerics() {
88 final Triple<Integer, String, Boolean>[] empty = Triple.emptyArray();
89 assertEquals(0, empty.length);
90 }
91
92 @Test
93 public void testEmptyArrayLength() {
94 @SuppressWarnings("unchecked")
95 final Triple<Integer, String, Boolean>[] empty = (Triple<Integer, String, Boolean>[]) Triple.EMPTY_ARRAY;
96 assertEquals(0, empty.length);
97 }
98
99 @Test
100 public void testFormattable_padded() {
101 final Triple<String, String, String> triple = Triple.of("Key", "Something", "Value");
102 assertEquals(" (Key,Something,Value)", String.format("%1$30s", triple));
103 }
104
105 @Test
106 public void testFormattable_simple() {
107 final Triple<String, String, String> triple = Triple.of("Key", "Something", "Value");
108 assertEquals("(Key,Something,Value)", String.format("%1$s", triple));
109 }
110
111 @Test
112 public void testOfNonNull() {
113 assertThrows(NullPointerException.class, () -> Triple.ofNonNull(null, null, null));
114 assertThrows(NullPointerException.class, () -> Triple.ofNonNull(null, null, "z"));
115 assertThrows(NullPointerException.class, () -> Triple.ofNonNull(null, "y", "z"));
116 assertThrows(NullPointerException.class, () -> Triple.ofNonNull("x", null, null));
117 assertThrows(NullPointerException.class, () -> Triple.ofNonNull("x", "y", null));
118 final Triple<String, String, String> pair = Triple.ofNonNull("x", "y", "z");
119 assertEquals("x", pair.getLeft());
120 assertEquals("y", pair.getMiddle());
121 assertEquals("z", pair.getRight());
122 }
123
124 @Test
125 public void testToString() {
126 final Triple<String, String, String> triple = Triple.of("Key", "Something", "Value");
127 assertEquals("(Key,Something,Value)", triple.toString());
128 }
129
130 @Test
131 public void testToStringCustom() {
132 final Calendar date = Calendar.getInstance();
133 date.set(2011, Calendar.APRIL, 25);
134 final Triple<String, String, Calendar> triple = Triple.of("DOB", "string", date);
135 assertEquals("Test created on " + "04-25-2011", triple.toString("Test created on %3$tm-%3$td-%3$tY"));
136 }
137
138 @Test
139 public void testTripleOf() {
140 final Triple<Integer, String, Boolean> triple = Triple.of(0, "foo", Boolean.TRUE);
141 assertTrue(triple instanceof ImmutableTriple<?, ?, ?>);
142 assertEquals(0, ((ImmutableTriple<Integer, String, Boolean>) triple).left.intValue());
143 assertEquals("foo", ((ImmutableTriple<Integer, String, Boolean>) triple).middle);
144 assertEquals(Boolean.TRUE, ((ImmutableTriple<Integer, String, Boolean>) triple).right);
145 final Triple<Object, String, Long> triple2 = Triple.of(null, "bar", Long.valueOf(200L));
146 assertTrue(triple2 instanceof ImmutableTriple<?, ?, ?>);
147 assertNull(((ImmutableTriple<Object, String, Long>) triple2).left);
148 assertEquals("bar", ((ImmutableTriple<Object, String, Long>) triple2).middle);
149 assertEquals(Long.valueOf(200L), ((ImmutableTriple<Object, String, Long>) triple2).right);
150 }
151
152 }
153