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.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertSame;
25  
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.Map.Entry;
30  import java.util.TreeMap;
31  
32  import org.apache.commons.lang3.AbstractLangTest;
33  import org.apache.commons.lang3.SerializationUtils;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Test the Triple class.
38   */
39  class ImmutableTripleTest extends AbstractLangTest {
40  
41      @Test
42      void testBasic() {
43          final ImmutableTriple<Integer, String, Boolean> triple = new ImmutableTriple<>(0, "foo", Boolean.TRUE);
44          assertEquals(0, triple.left.intValue());
45          assertEquals(0, triple.getLeft().intValue());
46          assertEquals("foo", triple.middle);
47          assertEquals("foo", triple.getMiddle());
48          assertEquals(Boolean.TRUE, triple.right);
49          assertEquals(Boolean.TRUE, triple.getRight());
50          final ImmutableTriple<Object, String, Integer> triple2 = new ImmutableTriple<>(null, "bar", 42);
51          assertNull(triple2.left);
52          assertNull(triple2.getLeft());
53          assertEquals("bar", triple2.middle);
54          assertEquals("bar", triple2.getMiddle());
55          assertEquals(Integer.valueOf(42), triple2.right);
56          assertEquals(Integer.valueOf(42), triple2.getRight());
57      }
58  
59      @Test
60      void testEmptyArrayGenerics() {
61          final ImmutableTriple<Integer, String, Boolean>[] empty = ImmutableTriple.emptyArray();
62          assertEquals(0, empty.length);
63      }
64  
65      @Test
66      void testEmptyArrayLength() {
67          @SuppressWarnings("unchecked")
68          final ImmutableTriple<Integer, String, Boolean>[] empty = (ImmutableTriple<Integer, String, Boolean>[]) ImmutableTriple.EMPTY_ARRAY;
69          assertEquals(0, empty.length);
70      }
71  
72      @Test
73      void testEquals() {
74          assertEquals(ImmutableTriple.of(null, "foo", 42), ImmutableTriple.of(null, "foo", 42));
75          assertNotEquals(ImmutableTriple.of("foo", 0, Boolean.TRUE), ImmutableTriple.of("foo", null, null));
76          assertNotEquals(ImmutableTriple.of("foo", "bar", "baz"), ImmutableTriple.of("xyz", "bar", "blo"));
77  
78          final ImmutableTriple<String, String, String> p = ImmutableTriple.of("foo", "bar", "baz");
79          assertEquals(p, p);
80          assertNotEquals(p, new Object());
81      }
82  
83      @Test
84      void testHashCode() {
85          assertEquals(ImmutableTriple.of(null, "foo", Boolean.TRUE).hashCode(), ImmutableTriple.of(null, "foo", Boolean.TRUE).hashCode());
86      }
87  
88      @Test
89      void testNullTripleEquals() {
90          assertEquals(ImmutableTriple.nullTriple(), ImmutableTriple.nullTriple());
91      }
92  
93      @Test
94      void testNullTripleLeft() {
95          assertNull(ImmutableTriple.nullTriple().getLeft());
96      }
97  
98      @Test
99      void testNullTripleMiddle() {
100         assertNull(ImmutableTriple.nullTriple().getMiddle());
101     }
102 
103     @Test
104     void testNullTripleRight() {
105         assertNull(ImmutableTriple.nullTriple().getRight());
106     }
107 
108     @Test
109     void testNullTripleSame() {
110         assertSame(ImmutableTriple.nullTriple(), ImmutableTriple.nullTriple());
111     }
112 
113     @Test
114     void testNullTripleTyped() {
115         // No compiler warnings
116         // How do we assert that?
117         final ImmutableTriple<String, String, String> triple = ImmutableTriple.nullTriple();
118         assertNotNull(triple);
119     }
120 
121     @Test
122     void testOf() {
123         assertSame(ImmutableTriple.nullTriple(), ImmutableTriple.of(null, null, null));
124         assertEquals(0, ImmutableTriple.of(0, null, null).getLeft());
125         assertEquals(0, ImmutableTriple.of(null, 0, null).getMiddle());
126         assertEquals(0, ImmutableTriple.of(null, null, 0).getRight());
127         final ImmutableTriple<Integer, String, Boolean> triple = ImmutableTriple.of(0, "foo", Boolean.FALSE);
128         assertEquals(0, triple.left.intValue());
129         assertEquals(0, triple.getLeft().intValue());
130         assertEquals("foo", triple.middle);
131         assertEquals("foo", triple.getMiddle());
132         assertEquals(Boolean.FALSE, triple.right);
133         assertEquals(Boolean.FALSE, triple.getRight());
134         final ImmutableTriple<Object, String, Boolean> triple2 = ImmutableTriple.of(null, "bar", Boolean.TRUE);
135         assertNull(triple2.left);
136         assertNull(triple2.getLeft());
137         assertEquals("bar", triple2.middle);
138         assertEquals("bar", triple2.getMiddle());
139         assertEquals(Boolean.TRUE, triple2.right);
140         assertEquals(Boolean.TRUE, triple2.getRight());
141     }
142 
143     @Test
144     void testOfNonNull() {
145         assertNullPointerException(() -> ImmutableTriple.ofNonNull(null, null, null));
146         assertNullPointerException(() -> ImmutableTriple.ofNonNull(null, null, "z"));
147         assertNullPointerException(() -> ImmutableTriple.ofNonNull(null, "y", "z"));
148         assertNullPointerException(() -> ImmutableTriple.ofNonNull("x", null, null));
149         assertNullPointerException(() -> ImmutableTriple.ofNonNull("x", "y", null));
150         final ImmutableTriple<String, String, String> pair = ImmutableTriple.ofNonNull("x", "y", "z");
151         assertEquals("x", pair.left);
152         assertEquals("y", pair.middle);
153         assertEquals("z", pair.right);
154     }
155 
156     @Test
157     void testSerialization() throws Exception {
158         final ImmutableTriple<Integer, String, Boolean> origTriple = ImmutableTriple.of(0, "foo", Boolean.TRUE);
159         final ImmutableTriple<Integer, String, Boolean> deserializedTriple = SerializationUtils.roundtrip(origTriple);
160         assertEquals(origTriple, deserializedTriple);
161         assertEquals(origTriple.hashCode(), deserializedTriple.hashCode());
162     }
163 
164     @Test
165     void testToString() {
166         assertEquals("(null,null,null)", ImmutableTriple.of(null, null, null).toString());
167         assertEquals("(null,two,null)", ImmutableTriple.of(null, "two", null).toString());
168         assertEquals("(one,null,null)", ImmutableTriple.of("one", null, null).toString());
169         assertEquals("(one,two,null)", ImmutableTriple.of("one", "two", null).toString());
170         assertEquals("(null,two,three)", ImmutableTriple.of(null, "two", "three").toString());
171         assertEquals("(one,null,three)", ImmutableTriple.of("one", null, "three").toString());
172         assertEquals("(one,two,three)", MutableTriple.of("one", "two", "three").toString());
173     }
174 
175     @Test
176     void testUseAsKeyOfHashMap() {
177         final HashMap<ImmutableTriple<Object, Object, Object>, String> map = new HashMap<>();
178         final Object o1 = new Object();
179         final Object o2 = new Object();
180         final Object o3 = new Object();
181         final ImmutableTriple<Object, Object, Object> key1 = ImmutableTriple.of(o1, o2, o3);
182         final String value1 = "a1";
183         map.put(key1, value1);
184         assertEquals(value1, map.get(key1));
185         assertEquals(value1, map.get(ImmutableTriple.of(o1, o2, o3)));
186     }
187 
188     @Test
189     void testUseAsKeyOfTreeMap() {
190         final TreeMap<ImmutableTriple<Integer, Integer, Integer>, String> map = new TreeMap<>();
191         map.put(ImmutableTriple.of(0, 1, 2), "012");
192         map.put(ImmutableTriple.of(0, 1, 1), "011");
193         map.put(ImmutableTriple.of(0, 0, 1), "001");
194         final ArrayList<ImmutableTriple<Integer, Integer, Integer>> expected = new ArrayList<>();
195         expected.add(ImmutableTriple.of(0, 0, 1));
196         expected.add(ImmutableTriple.of(0, 1, 1));
197         expected.add(ImmutableTriple.of(0, 1, 2));
198         final Iterator<Entry<ImmutableTriple<Integer, Integer, Integer>, String>> it = map.entrySet().iterator();
199         for (final ImmutableTriple<Integer, Integer, Integer> item : expected) {
200             final Entry<ImmutableTriple<Integer, Integer, Integer>, String> entry = it.next();
201             assertEquals(item, entry.getKey());
202             assertEquals(item.getLeft() + "" + item.getMiddle() + "" + item.getRight(), entry.getValue());
203         }
204     }
205 }
206