aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java
blob: 18e4ad6d66d4c05164b520548851a7e36a0e644a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.lang3.tuple;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.apache.commons.lang3.AbstractLangTest;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.jupiter.api.Test;

/**
 * Test the Pair class.
 */
public class ImmutablePairTest extends AbstractLangTest {

    @Test
    public void testBasic() {
        ImmutablePair<Integer, String> oldPair = new ImmutablePair<>(0, "foo");
        ImmutablePair<Integer, String> nowPair;
        for (int i=0; i<4; i++) {
            nowPair = ImmutablePair.of(oldPair);
            assertEquals(0, nowPair.left.intValue());
            assertEquals(0, nowPair.getLeft().intValue());
            assertEquals("foo", nowPair.right);
            assertEquals("foo", nowPair.getRight());
            assertEquals(oldPair, nowPair);
            oldPair = nowPair;
        }

        ImmutablePair<Object, String> oldPair2 = new ImmutablePair<>(null, "bar");
        ImmutablePair<Object, String> nowPair2;
        for (int i=0; i<4; i++) {
            nowPair2 = ImmutablePair.of(oldPair2);
            assertNull(nowPair2.left);
            assertNull(nowPair2.getLeft());
            assertEquals("bar", nowPair2.right);
            assertEquals("bar", nowPair2.getRight());
            oldPair2 = nowPair2;
        }
    }

    @Test
    public void testComparableLeftOnly() {
        final Pair<String, String> pair1 = ImmutablePair.left("A");
        final Pair<String, String> pair2 = ImmutablePair.left("B");
        assertEquals("A", pair1.getLeft());
        assertEquals("B", pair2.getLeft());
        assertEquals(0, pair1.compareTo(pair1));
        assertTrue(pair1.compareTo(pair2) < 0);
        assertEquals(0, pair2.compareTo(pair2));
        assertTrue(pair2.compareTo(pair1) > 0);
    }

    @Test
    public void testComparableRightOnly() {
        final Pair<String, String> pair1 = ImmutablePair.right("A");
        final Pair<String, String> pair2 = ImmutablePair.right("B");
        assertEquals("A", pair1.getRight());
        assertEquals("B", pair2.getRight());
        assertEquals(0, pair1.compareTo(pair1));
        assertTrue(pair1.compareTo(pair2) < 0);
        assertEquals(0, pair2.compareTo(pair2));
        assertTrue(pair2.compareTo(pair1) > 0);
    }

    @Test
    public void testEmptyArrayGenerics() {
        final ImmutablePair<Integer, String>[] empty = ImmutablePair.emptyArray();
        assertEquals(0, empty.length);
    }

    @Test
    public void testEmptyArrayLength() {
        @SuppressWarnings("unchecked")
        final ImmutablePair<Integer, String>[] empty = (ImmutablePair<Integer, String>[]) ImmutablePair.EMPTY_ARRAY;
        assertEquals(0, empty.length);
    }

    @Test
    public void testEquals() {
        assertEquals(ImmutablePair.of(null, "foo"), ImmutablePair.of(null, "foo"));
        assertNotEquals(ImmutablePair.of("foo", 0), ImmutablePair.of("foo", null));
        assertNotEquals(ImmutablePair.of("foo", "bar"), ImmutablePair.of("xyz", "bar"));

        final ImmutablePair<String, String> p = ImmutablePair.of("foo", "bar");
        assertEquals(p, p);
        assertNotEquals(p, new Object());
    }

    @Test
    public void testHashCode() {
        assertEquals(ImmutablePair.of(null, "foo").hashCode(), ImmutablePair.of(null, "foo").hashCode());
    }

    @Test
    public void testNullPairEquals() {
        assertEquals(ImmutablePair.nullPair(), ImmutablePair.nullPair());
    }

    @Test
    public void testNullPairKey() {
        assertNull(ImmutablePair.nullPair().getKey());
    }

    @Test
    public void testNullPairLeft() {
        assertNull(ImmutablePair.nullPair().getLeft());
    }

    @Test
    public void testNullPairRight() {
        assertNull(ImmutablePair.nullPair().getRight());
    }

    @Test
    public void testNullPairSame() {
        assertSame(ImmutablePair.nullPair(), ImmutablePair.nullPair());
    }

    @Test
    public void testNullPairTyped() {
        // No compiler warnings
        // How do we assert that?
        final ImmutablePair<String, String> pair = ImmutablePair.nullPair();
        assertNotNull(pair);
    }

    @Test
    public void testNullPairValue() {
        assertNull(ImmutablePair.nullPair().getValue());
    }

    @Test
    public void testOfNonNull() {
        assertThrows(NullPointerException.class, () -> ImmutablePair.ofNonNull(null, null));
        assertThrows(NullPointerException.class, () -> ImmutablePair.ofNonNull(null, "x"));
        assertThrows(NullPointerException.class, () -> ImmutablePair.ofNonNull("x", null));
        final ImmutablePair<String, String> pair = ImmutablePair.ofNonNull("x", "y");
        assertEquals("x", pair.left);
        assertEquals("y", pair.right);
    }

    @Test
    public void testPairOfMapEntry() {
        final HashMap<Integer, String> map = new HashMap<>();
        map.put(0, "foo");
        final Entry<Integer, String> entry = map.entrySet().iterator().next();
        final Pair<Integer, String> pair = ImmutablePair.of(entry);
        assertEquals(entry.getKey(), pair.getLeft());
        assertEquals(entry.getValue(), pair.getRight());
    }

    @Test
    public void testPairOfObjects() {
        final ImmutablePair<Integer, String> pair = ImmutablePair.of(0, "foo");
        assertEquals(0, pair.left.intValue());
        assertEquals(0, pair.getLeft().intValue());
        assertEquals("foo", pair.right);
        assertEquals("foo", pair.getRight());
        final ImmutablePair<Object, String> pair2 = ImmutablePair.of(null, "bar");
        assertNull(pair2.left);
        assertNull(pair2.getLeft());
        assertEquals("bar", pair2.right);
        assertEquals("bar", pair2.getRight());
        final ImmutablePair<?, ?> pair3 = ImmutablePair.of(null, null);
        assertNull(pair3.left);
        assertNull(pair3.right);
    }

    @Test
    public void testSerialization() throws Exception {
        final ImmutablePair<Integer, String> origPair = ImmutablePair.of(0, "foo");
        final ImmutablePair<Integer, String> deserializedPair = SerializationUtils.roundtrip(origPair);
        assertEquals(origPair, deserializedPair);
        assertEquals(origPair.hashCode(), deserializedPair.hashCode());
    }

    @Test
    public void testToString() {
        assertEquals("(null,null)", ImmutablePair.of(null, null).toString());
        assertEquals("(null,two)", ImmutablePair.of(null, "two").toString());
        assertEquals("(one,null)", ImmutablePair.of("one", null).toString());
        assertEquals("(one,two)", ImmutablePair.of("one", "two").toString());
    }

    @Test
    public void testToStringLeft() {
        final Pair<String, String> pair = ImmutablePair.left("Key");
        assertEquals("(Key,null)", pair.toString());
    }

    @Test
    public void testToStringRight() {
        final Pair<String, String> pair = ImmutablePair.right("Value");
        assertEquals("(null,Value)", pair.toString());
    }

    @Test
    public void testUseAsKeyOfHashMap() {
        final HashMap<ImmutablePair<Object, Object>, String> map = new HashMap<>();
        final Object o1 = new Object();
        final Object o2 = new Object();
        final ImmutablePair<Object, Object> key1 = ImmutablePair.of(o1, o2);
        final String value1 = "a1";
        map.put(key1, value1);
        assertEquals(value1, map.get(key1));
        assertEquals(value1, map.get(ImmutablePair.of(o1, o2)));
    }

    @Test
    public void testUseAsKeyOfTreeMap() {
        final TreeMap<ImmutablePair<Integer, Integer>, String> map = new TreeMap<>();
        map.put(ImmutablePair.of(1, 2), "12");
        map.put(ImmutablePair.of(1, 1), "11");
        map.put(ImmutablePair.of(0, 1), "01");
        final ArrayList<ImmutablePair<Integer, Integer>> expected = new ArrayList<>();
        expected.add(ImmutablePair.of(0, 1));
        expected.add(ImmutablePair.of(1, 1));
        expected.add(ImmutablePair.of(1, 2));
        final Iterator<Entry<ImmutablePair<Integer, Integer>, String>> it = map.entrySet().iterator();
        for (final ImmutablePair<Integer, Integer> item : expected) {
            final Entry<ImmutablePair<Integer, Integer>, String> entry = it.next();
            assertEquals(item, entry.getKey());
            assertEquals(item.getLeft() + "" + item.getRight(), entry.getValue());
        }
    }

    @Test
    public void testUnsupportedOperation() {
        final ImmutablePair<Integer, String> pair = new ImmutablePair<>(0, "foo");
        assertThrows(UnsupportedOperationException.class, () -> pair.setValue("any"));

    }
}