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.assertNotEquals;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertSame;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map.Entry;
30 import java.util.TreeMap;
31
32 import org.apache.commons.lang3.AbstractLangTest;
33 import org.apache.commons.lang3.SerializationUtils;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39 public class ImmutableTripleTest extends AbstractLangTest {
40
41 @Test
42 public void testBasic() {
43 final ImmutableTriple<Integer, String, Boolean> triple = new ImmutableTriple<>(0, "foo", Boolean.TRUE);
44 assertEquals(0, triple.left.intValue());
45 assertEquals(0, triple.getLeft().intValue());
46 assertEquals("foo", triple.middle);
47 assertEquals("foo", triple.getMiddle());
48 assertEquals(Boolean.TRUE, triple.right);
49 assertEquals(Boolean.TRUE, triple.getRight());
50 final ImmutableTriple<Object, String, Integer> triple2 = new ImmutableTriple<>(null, "bar", 42);
51 assertNull(triple2.left);
52 assertNull(triple2.getLeft());
53 assertEquals("bar", triple2.middle);
54 assertEquals("bar", triple2.getMiddle());
55 assertEquals(Integer.valueOf(42), triple2.right);
56 assertEquals(Integer.valueOf(42), triple2.getRight());
57 }
58
59 @Test
60 public void testEmptyArrayGenerics() {
61 final ImmutableTriple<Integer, String, Boolean>[] empty = ImmutableTriple.emptyArray();
62 assertEquals(0, empty.length);
63 }
64
65 @Test
66 public void testEmptyArrayLength() {
67 @SuppressWarnings("unchecked")
68 final ImmutableTriple<Integer, String, Boolean>[] empty = (ImmutableTriple<Integer, String, Boolean>[]) ImmutableTriple.EMPTY_ARRAY;
69 assertEquals(0, empty.length);
70 }
71
72 @Test
73 public void testEquals() {
74 assertEquals(ImmutableTriple.of(null, "foo", 42), ImmutableTriple.of(null, "foo", 42));
75 assertNotEquals(ImmutableTriple.of("foo", 0, Boolean.TRUE), ImmutableTriple.of("foo", null, null));
76 assertNotEquals(ImmutableTriple.of("foo", "bar", "baz"), ImmutableTriple.of("xyz", "bar", "blo"));
77
78 final ImmutableTriple<String, String, String> p = ImmutableTriple.of("foo", "bar", "baz");
79 assertEquals(p, p);
80 assertNotEquals(p, new Object());
81 }
82
83 @Test
84 public void testHashCode() {
85 assertEquals(ImmutableTriple.of(null, "foo", Boolean.TRUE).hashCode(), ImmutableTriple.of(null, "foo", Boolean.TRUE).hashCode());
86 }
87
88 @Test
89 public void testNullTripleEquals() {
90 assertEquals(ImmutableTriple.nullTriple(), ImmutableTriple.nullTriple());
91 }
92
93 @Test
94 public void testNullTripleLeft() {
95 assertNull(ImmutableTriple.nullTriple().getLeft());
96 }
97
98 @Test
99 public void testNullTripleMiddle() {
100 assertNull(ImmutableTriple.nullTriple().getMiddle());
101 }
102
103 @Test
104 public void testNullTripleRight() {
105 assertNull(ImmutableTriple.nullTriple().getRight());
106 }
107
108 @Test
109 public void testNullTripleSame() {
110 assertSame(ImmutableTriple.nullTriple(), ImmutableTriple.nullTriple());
111 }
112
113 @Test
114 public void testNullTripleTyped() {
115
116
117 final ImmutableTriple<String, String, String> triple = ImmutableTriple.nullTriple();
118 assertNotNull(triple);
119 }
120
121 @Test
122 public void testOfNonNull() {
123 assertThrows(NullPointerException.class, () -> ImmutableTriple.ofNonNull(null, null, null));
124 assertThrows(NullPointerException.class, () -> ImmutableTriple.ofNonNull(null, null, "z"));
125 assertThrows(NullPointerException.class, () -> ImmutableTriple.ofNonNull(null, "y", "z"));
126 assertThrows(NullPointerException.class, () -> ImmutableTriple.ofNonNull("x", null, null));
127 assertThrows(NullPointerException.class, () -> ImmutableTriple.ofNonNull("x", "y", null));
128 final ImmutableTriple<String, String, String> pair = ImmutableTriple.ofNonNull("x", "y", "z");
129 assertEquals("x", pair.left);
130 assertEquals("y", pair.middle);
131 assertEquals("z", pair.right);
132 }
133
134 @Test
135 public void testSerialization() throws Exception {
136 final ImmutableTriple<Integer, String, Boolean> origTriple = ImmutableTriple.of(0, "foo", Boolean.TRUE);
137 final ImmutableTriple<Integer, String, Boolean> deserializedTriple = SerializationUtils.roundtrip(origTriple);
138 assertEquals(origTriple, deserializedTriple);
139 assertEquals(origTriple.hashCode(), deserializedTriple.hashCode());
140 }
141
142 @Test
143 public void testToString() {
144 assertEquals("(null,null,null)", ImmutableTriple.of(null, null, null).toString());
145 assertEquals("(null,two,null)", ImmutableTriple.of(null, "two", null).toString());
146 assertEquals("(one,null,null)", ImmutableTriple.of("one", null, null).toString());
147 assertEquals("(one,two,null)", ImmutableTriple.of("one", "two", null).toString());
148 assertEquals("(null,two,three)", ImmutableTriple.of(null, "two", "three").toString());
149 assertEquals("(one,null,three)", ImmutableTriple.of("one", null, "three").toString());
150 assertEquals("(one,two,three)", MutableTriple.of("one", "two", "three").toString());
151 }
152
153 @Test
154 public void testTripleOf() {
155 final ImmutableTriple<Integer, String, Boolean> triple = ImmutableTriple.of(0, "foo", Boolean.FALSE);
156 assertEquals(0, triple.left.intValue());
157 assertEquals(0, triple.getLeft().intValue());
158 assertEquals("foo", triple.middle);
159 assertEquals("foo", triple.getMiddle());
160 assertEquals(Boolean.FALSE, triple.right);
161 assertEquals(Boolean.FALSE, triple.getRight());
162 final ImmutableTriple<Object, String, Boolean> triple2 = ImmutableTriple.of(null, "bar", Boolean.TRUE);
163 assertNull(triple2.left);
164 assertNull(triple2.getLeft());
165 assertEquals("bar", triple2.middle);
166 assertEquals("bar", triple2.getMiddle());
167 assertEquals(Boolean.TRUE, triple2.right);
168 assertEquals(Boolean.TRUE, triple2.getRight());
169 }
170
171 @Test
172 public void testUseAsKeyOfHashMap() {
173 final HashMap<ImmutableTriple<Object, Object, Object>, String> map = new HashMap<>();
174 final Object o1 = new Object();
175 final Object o2 = new Object();
176 final Object o3 = new Object();
177 final ImmutableTriple<Object, Object, Object> key1 = ImmutableTriple.of(o1, o2, o3);
178 final String value1 = "a1";
179 map.put(key1, value1);
180 assertEquals(value1, map.get(key1));
181 assertEquals(value1, map.get(ImmutableTriple.of(o1, o2, o3)));
182 }
183
184 @Test
185 public void testUseAsKeyOfTreeMap() {
186 final TreeMap<ImmutableTriple<Integer, Integer, Integer>, String> map = new TreeMap<>();
187 map.put(ImmutableTriple.of(0, 1, 2), "012");
188 map.put(ImmutableTriple.of(0, 1, 1), "011");
189 map.put(ImmutableTriple.of(0, 0, 1), "001");
190 final ArrayList<ImmutableTriple<Integer, Integer, Integer>> expected = new ArrayList<>();
191 expected.add(ImmutableTriple.of(0, 0, 1));
192 expected.add(ImmutableTriple.of(0, 1, 1));
193 expected.add(ImmutableTriple.of(0, 1, 2));
194 final Iterator<Entry<ImmutableTriple<Integer, Integer, Integer>, String>> it = map.entrySet().iterator();
195 for (final ImmutableTriple<Integer, Integer, Integer> item : expected) {
196 final Entry<ImmutableTriple<Integer, Integer, Integer>, String> entry = it.next();
197 assertEquals(item, entry.getKey());
198 assertEquals(item.getLeft() + "" + item.getMiddle() + "" + item.getRight(), entry.getValue());
199 }
200 }
201 }
202