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