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.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.Calendar;
26  import java.util.HashMap;
27  import java.util.HashSet;
28  import java.util.Map;
29  import java.util.Map.Entry;
30  
31  import org.apache.commons.lang3.AbstractLangTest;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Test the Pair class.
36   */
37  public class PairTest extends AbstractLangTest {
38  
39      @Test
40      public void testAccept() {
41          final Pair<String, String> pair1 = Pair.of("A", "D");
42          final Pair<String, String> pair2 = Pair.of("B", "C");
43          final Map<String, String> map = new HashMap<>();
44          pair1.accept(map::put);
45          pair2.accept(map::put);
46          assertEquals("D", map.get("A"));
47          assertEquals("C", map.get("B"));
48          pair1.accept(map::put);
49          pair2.accept(map::put);
50          assertEquals("D", map.get("A"));
51          assertEquals("C", map.get("B"));
52      }
53  
54      @Test
55      public void testApply() {
56          final Pair<String, String> pair1 = Pair.of("A", "D");
57          final Pair<String, String> pair2 = Pair.of("B", "C");
58          final Map<String, String> map = new HashMap<>();
59          assertEquals(null, pair1.apply(map::put));
60          assertEquals(null, pair2.apply(map::put));
61          assertEquals("D", map.get("A"));
62          assertEquals("C", map.get("B"));
63          assertEquals("D", pair1.apply(map::put));
64          assertEquals("C", pair2.apply(map::put));
65          assertEquals("D", map.get("A"));
66          assertEquals("C", map.get("B"));
67      }
68  
69      @Test
70      public void testComparable1() {
71          final Pair<String, String> pair1 = Pair.of("A", "D");
72          final Pair<String, String> pair2 = Pair.of("B", "C");
73          assertEquals(0, pair1.compareTo(pair1));
74          assertTrue(pair1.compareTo(pair2) < 0);
75          assertEquals(0, pair2.compareTo(pair2));
76          assertTrue(pair2.compareTo(pair1) > 0);
77      }
78  
79      @Test
80      public void testComparable2() {
81          final Pair<String, String> pair1 = Pair.of("A", "C");
82          final Pair<String, String> pair2 = Pair.of("A", "D");
83          assertEquals(0, pair1.compareTo(pair1));
84          assertTrue(pair1.compareTo(pair2) < 0);
85          assertEquals(0, pair2.compareTo(pair2));
86          assertTrue(pair2.compareTo(pair1) > 0);
87      }
88  
89      @Test
90      public void testCompatibilityBetweenPairs() {
91          final Pair<Integer, String> pair = ImmutablePair.of(0, "foo");
92          final Pair<Integer, String> pair2 = MutablePair.of(0, "foo");
93          assertEquals(pair, pair2);
94          assertEquals(pair.hashCode(), pair2.hashCode());
95          final HashSet<Pair<Integer, String>> set = new HashSet<>();
96          set.add(pair);
97          assertTrue(set.contains(pair2));
98  
99          pair2.setValue("bar");
100         assertNotEquals(pair, pair2);
101         assertNotEquals(pair.hashCode(), pair2.hashCode());
102     }
103 
104     @Test
105     public void testEmptyArrayGenerics() {
106         final Pair<Integer, String>[] empty = Pair.emptyArray();
107         assertEquals(0, empty.length);
108     }
109 
110     @Test
111     public void testEmptyArrayLength() {
112         @SuppressWarnings("unchecked")
113         final Pair<Integer, String>[] empty = (Pair<Integer, String>[]) Pair.EMPTY_ARRAY;
114         assertEquals(0, empty.length);
115     }
116 
117     @Test
118     public void testFormattable_padded() {
119         final Pair<String, String> pair = Pair.of("Key", "Value");
120         assertEquals("         (Key,Value)", String.format("%1$20s", pair));
121     }
122 
123     @Test
124     public void testFormattable_simple() {
125         final Pair<String, String> pair = Pair.of("Key", "Value");
126         assertEquals("(Key,Value)", String.format("%1$s", pair));
127     }
128 
129     @Test
130     public void testMapEntry() {
131         final Pair<Integer, String> pair = ImmutablePair.of(0, "foo");
132         final HashMap<Integer, String> map = new HashMap<>();
133         map.put(0, "foo");
134         final Entry<Integer, String> entry = map.entrySet().iterator().next();
135         assertEquals(pair, entry);
136         assertEquals(pair.hashCode(), entry.hashCode());
137     }
138 
139     @Test
140     public void testOfNonNull() {
141         assertThrows(NullPointerException.class, () -> Pair.ofNonNull(null, null));
142         assertThrows(NullPointerException.class, () -> Pair.ofNonNull(null, "x"));
143         assertThrows(NullPointerException.class, () -> Pair.ofNonNull("x", null));
144         final Pair<String, String> pair = Pair.ofNonNull("x", "y");
145         assertEquals("x", pair.getLeft());
146         assertEquals("y", pair.getRight());
147     }
148 
149     @Test
150     public void testPairOfMapEntry() {
151         final HashMap<Integer, String> map = new HashMap<>();
152         map.put(0, "foo");
153         final Entry<Integer, String> entry = map.entrySet().iterator().next();
154         final Pair<Integer, String> pair = Pair.of(entry);
155         assertEquals(entry.getKey(), pair.getLeft());
156         assertEquals(entry.getValue(), pair.getRight());
157     }
158 
159     @Test
160     public void testPairOfObjects() {
161         final Pair<Integer, String> pair = Pair.of(0, "foo");
162         assertTrue(pair instanceof ImmutablePair<?, ?>);
163         assertEquals(0, ((ImmutablePair<Integer, String>) pair).left.intValue());
164         assertEquals("foo", ((ImmutablePair<Integer, String>) pair).right);
165         final Pair<Object, String> pair2 = Pair.of(null, "bar");
166         assertTrue(pair2 instanceof ImmutablePair<?, ?>);
167         assertNull(((ImmutablePair<Object, String>) pair2).left);
168         assertEquals("bar", ((ImmutablePair<Object, String>) pair2).right);
169         final Pair<?, ?> pair3 = Pair.of(null, null);
170         assertNull(pair3.getLeft());
171         assertNull(pair3.getRight());
172     }
173 
174     @Test
175     public void testToString() {
176         final Pair<String, String> pair = Pair.of("Key", "Value");
177         assertEquals("(Key,Value)", pair.toString());
178     }
179 
180     @Test
181     public void testToStringCustom() {
182         final Calendar date = Calendar.getInstance();
183         date.set(2011, Calendar.APRIL, 25);
184         final Pair<String, Calendar> pair = Pair.of("DOB", date);
185         assertEquals("Test created on " + "04-25-2011", pair.toString("Test created on %2$tm-%2$td-%2$tY"));
186     }
187 
188 }