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