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 org.apache.commons.lang3.AbstractLangTest;
25  import org.apache.commons.lang3.SerializationUtils;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Test the MutableTriple class.
30   */
31  public class MutableTripleTest extends AbstractLangTest {
32  
33      @Test
34      public void testBasic() {
35          final MutableTriple<Integer, String, Boolean> triple = new MutableTriple<>(0, "foo", Boolean.FALSE);
36          assertEquals(0, triple.getLeft().intValue());
37          assertEquals("foo", triple.getMiddle());
38          assertEquals(Boolean.FALSE, triple.getRight());
39          final MutableTriple<Object, String, String> triple2 = new MutableTriple<>(null, "bar", "hello");
40          assertNull(triple2.getLeft());
41          assertEquals("bar", triple2.getMiddle());
42          assertEquals("hello", triple2.getRight());
43      }
44  
45  
46      @Test
47      public void testDefault() {
48          final MutableTriple<Integer, String, Boolean> triple = new MutableTriple<>();
49          assertNull(triple.getLeft());
50          assertNull(triple.getMiddle());
51          assertNull(triple.getRight());
52      }
53  
54      @Test
55      public void testEmptyArrayGenerics() {
56          final MutableTriple<Integer, String, Boolean>[] empty = MutableTriple.emptyArray();
57          assertEquals(0, empty.length);
58      }
59  
60      @Test
61      public void testEmptyArrayLength() {
62          @SuppressWarnings("unchecked")
63          final MutableTriple<Integer, String, Boolean>[] empty = (MutableTriple<Integer, String, Boolean>[]) MutableTriple.EMPTY_ARRAY;
64          assertEquals(0, empty.length);
65      }
66  
67      @Test
68      public void testEquals() {
69          assertEquals(MutableTriple.of(null, "foo", "baz"), MutableTriple.of(null, "foo", "baz"));
70          assertNotEquals(MutableTriple.of("foo", 0, Boolean.TRUE), MutableTriple.of("foo", null, Boolean.TRUE));
71          assertNotEquals(MutableTriple.of("foo", "bar", "baz"), MutableTriple.of("xyz", "bar", "baz"));
72          assertNotEquals(MutableTriple.of("foo", "bar", "baz"), MutableTriple.of("foo", "bar", "blo"));
73  
74          final MutableTriple<String, String, String> p = MutableTriple.of("foo", "bar", "baz");
75          assertEquals(p, p);
76          assertNotEquals(p, new Object());
77      }
78  
79      @Test
80      public void testHashCode() {
81          assertEquals(MutableTriple.of(null, "foo", "baz").hashCode(), MutableTriple.of(null, "foo", "baz").hashCode());
82      }
83  
84      @Test
85      public void testMutate() {
86          final MutableTriple<Integer, String, Boolean> triple = new MutableTriple<>(0, "foo", Boolean.TRUE);
87          triple.setLeft(42);
88          triple.setMiddle("bar");
89          triple.setRight(Boolean.FALSE);
90          assertEquals(42, triple.getLeft().intValue());
91          assertEquals("bar", triple.getMiddle());
92          assertEquals(Boolean.FALSE, triple.getRight());
93      }
94  
95      @Test
96      public void testOfNonNull() {
97          assertThrows(NullPointerException.class, () -> MutableTriple.ofNonNull(null, null, null));
98          assertThrows(NullPointerException.class, () -> MutableTriple.ofNonNull(null, null, "z"));
99          assertThrows(NullPointerException.class, () -> MutableTriple.ofNonNull(null, "y", "z"));
100         assertThrows(NullPointerException.class, () -> MutableTriple.ofNonNull("x", null, null));
101         assertThrows(NullPointerException.class, () -> MutableTriple.ofNonNull("x", "y", null));
102         final MutableTriple<String, String, String> pair = MutableTriple.ofNonNull("x", "y", "z");
103         assertEquals("x", pair.left);
104         assertEquals("y", pair.middle);
105         assertEquals("z", pair.right);
106     }
107 
108     @Test
109     public void testSerialization() throws Exception {
110         final MutableTriple<Integer, String, Boolean> origTriple = MutableTriple.of(0, "foo", Boolean.TRUE);
111         final MutableTriple<Integer, String, Boolean> deserializedTriple = SerializationUtils.roundtrip(origTriple);
112         assertEquals(origTriple, deserializedTriple);
113         assertEquals(origTriple.hashCode(), deserializedTriple.hashCode());
114     }
115 
116     @Test
117     public void testToString() {
118         assertEquals("(null,null,null)", MutableTriple.of(null, null, null).toString());
119         assertEquals("(null,two,null)", MutableTriple.of(null, "two", null).toString());
120         assertEquals("(one,null,null)", MutableTriple.of("one", null, null).toString());
121         assertEquals("(one,two,null)", MutableTriple.of("one", "two", null).toString());
122         assertEquals("(null,two,three)", MutableTriple.of(null, "two", "three").toString());
123         assertEquals("(one,null,three)", MutableTriple.of("one", null, "three").toString());
124         assertEquals("(one,two,three)", MutableTriple.of("one", "two", "three").toString());
125     }
126 
127     @Test
128     public void testTripleOf() {
129         final MutableTriple<Integer, String, Boolean> triple = MutableTriple.of(0, "foo", Boolean.TRUE);
130         assertEquals(0, triple.getLeft().intValue());
131         assertEquals("foo", triple.getMiddle());
132         assertEquals(Boolean.TRUE, triple.getRight());
133         final MutableTriple<Object, String, String> triple2 = MutableTriple.of(null, "bar", "hello");
134         assertNull(triple2.getLeft());
135         assertEquals("bar", triple2.getMiddle());
136         assertEquals("hello", triple2.getRight());
137     }
138 }
139