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    *      https://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  package org.apache.commons.lang3.tuple;
18  
19  import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertSame;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.Map.Entry;
32  import java.util.TreeMap;
33  
34  import org.apache.commons.lang3.AbstractLangTest;
35  import org.apache.commons.lang3.SerializationUtils;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Test the Pair class.
40   */
41  class ImmutablePairTest extends AbstractLangTest {
42  
43      @Test
44      void testBasic() {
45          ImmutablePair<Integer, String> oldPair = new ImmutablePair<>(0, "foo");
46          ImmutablePair<Integer, String> nowPair;
47          for (int i = 0; i < 4; i++) {
48              nowPair = ImmutablePair.of(oldPair);
49              assertEquals(0, nowPair.left.intValue());
50              assertEquals(0, nowPair.getLeft().intValue());
51              assertEquals("foo", nowPair.right);
52              assertEquals("foo", nowPair.getRight());
53              assertEquals(oldPair, nowPair);
54              oldPair = nowPair;
55          }
56  
57          ImmutablePair<Object, String> oldPair2 = new ImmutablePair<>(null, "bar");
58          ImmutablePair<Object, String> nowPair2;
59          for (int i = 0; i < 4; i++) {
60              nowPair2 = ImmutablePair.of(oldPair2);
61              assertNull(nowPair2.left);
62              assertNull(nowPair2.getLeft());
63              assertEquals("bar", nowPair2.right);
64              assertEquals("bar", nowPair2.getRight());
65              oldPair2 = nowPair2;
66          }
67      }
68  
69      @Test
70      void testComparableLeftOnly() {
71          final Pair<String, String> pair1 = ImmutablePair.left("A");
72          final Pair<String, String> pair2 = ImmutablePair.left("B");
73          assertEquals("A", pair1.getLeft());
74          assertEquals("B", pair2.getLeft());
75          assertEquals(0, pair1.compareTo(pair1));
76          assertTrue(pair1.compareTo(pair2) < 0);
77          assertEquals(0, pair2.compareTo(pair2));
78          assertTrue(pair2.compareTo(pair1) > 0);
79      }
80  
81      @Test
82      void testComparableRightOnly() {
83          final Pair<String, String> pair1 = ImmutablePair.right("A");
84          final Pair<String, String> pair2 = ImmutablePair.right("B");
85          assertEquals("A", pair1.getRight());
86          assertEquals("B", pair2.getRight());
87          assertEquals(0, pair1.compareTo(pair1));
88          assertTrue(pair1.compareTo(pair2) < 0);
89          assertEquals(0, pair2.compareTo(pair2));
90          assertTrue(pair2.compareTo(pair1) > 0);
91      }
92  
93      @Test
94      void testEmptyArrayGenerics() {
95          final ImmutablePair<Integer, String>[] empty = ImmutablePair.emptyArray();
96          assertEquals(0, empty.length);
97      }
98  
99      @Test
100     void testEmptyArrayLength() {
101         @SuppressWarnings("unchecked")
102         final ImmutablePair<Integer, String>[] empty = (ImmutablePair<Integer, String>[]) ImmutablePair.EMPTY_ARRAY;
103         assertEquals(0, empty.length);
104     }
105 
106     @Test
107     void testEquals() {
108         assertEquals(ImmutablePair.of(null, "foo"), ImmutablePair.of(null, "foo"));
109         assertNotEquals(ImmutablePair.of("foo", 0), ImmutablePair.of("foo", null));
110         assertNotEquals(ImmutablePair.of("foo", "bar"), ImmutablePair.of("xyz", "bar"));
111 
112         final ImmutablePair<String, String> p = ImmutablePair.of("foo", "bar");
113         assertEquals(p, p);
114         assertNotEquals(p, new Object());
115     }
116 
117     @Test
118     void testHashCode() {
119         assertEquals(ImmutablePair.of(null, "foo").hashCode(), ImmutablePair.of(null, "foo").hashCode());
120     }
121 
122     @Test
123     void testNullPairEquals() {
124         assertEquals(ImmutablePair.nullPair(), ImmutablePair.nullPair());
125     }
126 
127     @Test
128     void testNullPairKey() {
129         assertNull(ImmutablePair.nullPair().getKey());
130     }
131 
132     @Test
133     void testNullPairLeft() {
134         assertNull(ImmutablePair.nullPair().getLeft());
135     }
136 
137     @Test
138     void testNullPairRight() {
139         assertNull(ImmutablePair.nullPair().getRight());
140     }
141 
142     @Test
143     void testNullPairSame() {
144         assertSame(ImmutablePair.nullPair(), ImmutablePair.nullPair());
145     }
146 
147     @Test
148     void testNullPairTyped() {
149         // No compiler warnings
150         // How do we assert that?
151         final ImmutablePair<String, String> pair = ImmutablePair.nullPair();
152         assertNotNull(pair);
153     }
154 
155     @Test
156     void testNullPairValue() {
157         assertNull(ImmutablePair.nullPair().getValue());
158     }
159 
160     @Test
161     void testOfNonNull() {
162         assertNullPointerException(() -> ImmutablePair.ofNonNull(null, null));
163         assertNullPointerException(() -> ImmutablePair.ofNonNull(null, "x"));
164         assertNullPointerException(() -> ImmutablePair.ofNonNull("x", null));
165         final ImmutablePair<String, String> pair = ImmutablePair.ofNonNull("x", "y");
166         assertEquals("x", pair.left);
167         assertEquals("y", pair.right);
168     }
169 
170     @Test
171     void testPairOfMapEntry() {
172         assertSame(ImmutablePair.nullPair(), ImmutablePair.of(null));
173         final HashMap<Integer, String> map = new HashMap<>();
174         map.put(0, "foo");
175         final Entry<Integer, String> entry = map.entrySet().iterator().next();
176         final Pair<Integer, String> pair = ImmutablePair.of(entry);
177         assertEquals(entry.getKey(), pair.getLeft());
178         assertEquals(entry.getValue(), pair.getRight());
179     }
180 
181     @Test
182     void testPairOfObjects() {
183         final ImmutablePair<Integer, String> pair = ImmutablePair.of(0, "foo");
184         assertEquals(0, pair.left.intValue());
185         assertEquals(0, pair.getLeft().intValue());
186         assertEquals("foo", pair.right);
187         assertEquals("foo", pair.getRight());
188         final ImmutablePair<Object, String> pair2 = ImmutablePair.of(null, "bar");
189         assertNull(pair2.left);
190         assertNull(pair2.getLeft());
191         assertEquals("bar", pair2.right);
192         assertEquals("bar", pair2.getRight());
193         final ImmutablePair<?, ?> pair3 = ImmutablePair.of(null, null);
194         assertNull(pair3.left);
195         assertNull(pair3.right);
196     }
197 
198     @Test
199     void testSerialization() throws Exception {
200         final ImmutablePair<Integer, String> origPair = ImmutablePair.of(0, "foo");
201         final ImmutablePair<Integer, String> deserializedPair = SerializationUtils.roundtrip(origPair);
202         assertEquals(origPair, deserializedPair);
203         assertEquals(origPair.hashCode(), deserializedPair.hashCode());
204     }
205 
206     @Test
207     void testToString() {
208         assertEquals("(null,null)", ImmutablePair.of(null, null).toString());
209         assertEquals("(null,two)", ImmutablePair.of(null, "two").toString());
210         assertEquals("(one,null)", ImmutablePair.of("one", null).toString());
211         assertEquals("(one,two)", ImmutablePair.of("one", "two").toString());
212     }
213 
214     @Test
215     void testToStringLeft() {
216         final Pair<String, String> pair = ImmutablePair.left("Key");
217         assertEquals("(Key,null)", pair.toString());
218     }
219 
220     @Test
221     void testToStringRight() {
222         final Pair<String, String> pair = ImmutablePair.right("Value");
223         assertEquals("(null,Value)", pair.toString());
224     }
225 
226     @Test
227     void testUnsupportedOperation() {
228         final ImmutablePair<Integer, String> pair = new ImmutablePair<>(0, "foo");
229         assertThrows(UnsupportedOperationException.class, () -> pair.setValue("any"));
230 
231     }
232 
233     @Test
234     void testUseAsKeyOfHashMap() {
235         final HashMap<ImmutablePair<Object, Object>, String> map = new HashMap<>();
236         final Object o1 = new Object();
237         final Object o2 = new Object();
238         final ImmutablePair<Object, Object> key1 = ImmutablePair.of(o1, o2);
239         final String value1 = "a1";
240         map.put(key1, value1);
241         assertEquals(value1, map.get(key1));
242         assertEquals(value1, map.get(ImmutablePair.of(o1, o2)));
243     }
244 
245     @Test
246     void testUseAsKeyOfTreeMap() {
247         final TreeMap<ImmutablePair<Integer, Integer>, String> map = new TreeMap<>();
248         map.put(ImmutablePair.of(1, 2), "12");
249         map.put(ImmutablePair.of(1, 1), "11");
250         map.put(ImmutablePair.of(0, 1), "01");
251         final ArrayList<ImmutablePair<Integer, Integer>> expected = new ArrayList<>();
252         expected.add(ImmutablePair.of(0, 1));
253         expected.add(ImmutablePair.of(1, 1));
254         expected.add(ImmutablePair.of(1, 2));
255         final Iterator<Entry<ImmutablePair<Integer, Integer>, String>> it = map.entrySet().iterator();
256         for (final ImmutablePair<Integer, Integer> item : expected) {
257             final Entry<ImmutablePair<Integer, Integer>, String> entry = it.next();
258             assertEquals(item, entry.getKey());
259             assertEquals(item.getLeft() + "" + item.getRight(), entry.getValue());
260         }
261     }
262 }