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  
24  import java.util.HashMap;
25  import java.util.Map.Entry;
26  
27  import org.apache.commons.lang3.AbstractLangTest;
28  import org.apache.commons.lang3.SerializationUtils;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Test the MutablePair class.
33   */
34  public class MutablePairTest extends AbstractLangTest {
35  
36      @Test
37      public void testBasic() {
38          MutablePair<Integer, String> oldPair = new MutablePair<>(0, "foo");
39          MutablePair<Integer, String> nowPair;
40          for (int i=0; i<4; i++) {
41              nowPair = MutablePair.of(oldPair);
42              assertEquals(0, nowPair.left.intValue());
43              assertEquals(0, nowPair.getLeft().intValue());
44              assertEquals("foo", nowPair.right);
45              assertEquals("foo", nowPair.getRight());
46              assertEquals(oldPair, nowPair);
47              oldPair = nowPair;
48          }
49  
50          MutablePair<Object, String> oldPair2 = new MutablePair<>(null, "bar");
51          MutablePair<Object, String> nowPair2;
52          for (int i=0; i<4; i++) {
53              nowPair2 = MutablePair.of(oldPair2);
54              assertNull(nowPair2.left);
55              assertNull(nowPair2.getLeft());
56              assertEquals("bar", nowPair2.right);
57              assertEquals("bar", nowPair2.getRight());
58              oldPair2 = nowPair2;
59          }
60      }
61  
62      @Test
63      public void testDefault() {
64          final MutablePair<Integer, String> pair = new MutablePair<>();
65          assertNull(pair.getLeft());
66          assertNull(pair.getRight());
67      }
68  
69      @Test
70      public void testEmptyArrayGenerics() {
71          final MutablePair<Integer, String>[] empty = MutablePair.emptyArray();
72          assertEquals(0, empty.length);
73      }
74  
75      @Test
76      public void testEmptyArrayLength() {
77          @SuppressWarnings("unchecked")
78          final MutablePair<Integer, String>[] empty = (MutablePair<Integer, String>[]) MutablePair.EMPTY_ARRAY;
79          assertEquals(0, empty.length);
80      }
81  
82      @Test
83      public void testEquals() {
84          assertEquals(MutablePair.of(null, "foo"), MutablePair.of(null, "foo"));
85          assertNotEquals(MutablePair.of("foo", 0), MutablePair.of("foo", null));
86          assertNotEquals(MutablePair.of("foo", "bar"), MutablePair.of("xyz", "bar"));
87  
88          final MutablePair<String, String> p = MutablePair.of("foo", "bar");
89          assertEquals(p, p);
90          assertNotEquals(p, new Object());
91      }
92  
93      @Test
94      public void testHashCode() {
95          assertEquals(MutablePair.of(null, "foo").hashCode(), MutablePair.of(null, "foo").hashCode());
96      }
97  
98      @Test
99      public void testMutate() {
100         final MutablePair<Integer, String> pair = new MutablePair<>(0, "foo");
101         pair.setLeft(42);
102         pair.setRight("bar");
103         assertEquals(42, pair.getLeft().intValue());
104         assertEquals("bar", pair.getRight());
105     }
106 
107     @Test
108     public void testOfNonNull() {
109         assertThrows(NullPointerException.class, () -> MutablePair.ofNonNull(null, null));
110         assertThrows(NullPointerException.class, () -> MutablePair.ofNonNull(null, "x"));
111         assertThrows(NullPointerException.class, () -> MutablePair.ofNonNull("x", null));
112         final MutablePair<String, String> pair = MutablePair.ofNonNull("x", "y");
113         assertEquals("x", pair.left);
114         assertEquals("y", pair.right);
115     }
116 
117     @Test
118     public void testPairOfMapEntry() {
119         final HashMap<Integer, String> map = new HashMap<>();
120         map.put(0, "foo");
121         final Entry<Integer, String> entry = map.entrySet().iterator().next();
122         final Pair<Integer, String> pair = MutablePair.of(entry);
123         assertEquals(entry.getKey(), pair.getLeft());
124         assertEquals(entry.getValue(), pair.getRight());
125     }
126 
127     @Test
128     public void testPairOfObjects() {
129         final MutablePair<Integer, String> pair = MutablePair.of(0, "foo");
130         assertEquals(0, pair.getLeft().intValue());
131         assertEquals("foo", pair.getRight());
132         final MutablePair<Object, String> pair2 = MutablePair.of(null, "bar");
133         assertNull(pair2.getLeft());
134         assertEquals("bar", pair2.getRight());
135         final MutablePair<?, ?> pair3 = MutablePair.of(null, null);
136         assertNull(pair3.left);
137         assertNull(pair3.right);
138     }
139 
140     @Test
141     public void testSerialization() throws Exception {
142         final MutablePair<Integer, String> origPair = MutablePair.of(0, "foo");
143         final MutablePair<Integer, String> deserializedPair = SerializationUtils.roundtrip(origPair);
144         assertEquals(origPair, deserializedPair);
145         assertEquals(origPair.hashCode(), deserializedPair.hashCode());
146     }
147 
148     @Test
149     public void testToString() {
150         assertEquals("(null,null)", MutablePair.of(null, null).toString());
151         assertEquals("(null,two)", MutablePair.of(null, "two").toString());
152         assertEquals("(one,null)", MutablePair.of("one", null).toString());
153         assertEquals("(one,two)", MutablePair.of("one", "two").toString());
154     }
155 }