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.assertInstanceOf;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.Calendar;
26  import java.util.HashSet;
27  
28  import org.apache.commons.lang3.AbstractLangTest;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Test the Triple class.
33   */
34  class TripleTest extends AbstractLangTest {
35  
36      @Test
37      void testComparable1() {
38          final Triple<String, String, String> triple1 = Triple.of("A", "D", "A");
39          final Triple<String, String, String> triple2 = Triple.of("B", "C", "A");
40          assertEquals(0, triple1.compareTo(triple1));
41          assertTrue(triple1.compareTo(triple2) < 0);
42          assertEquals(0, triple2.compareTo(triple2));
43          assertTrue(triple2.compareTo(triple1) > 0);
44      }
45  
46      @Test
47      void testComparable2() {
48          final Triple<String, String, String> triple1 = Triple.of("A", "C", "B");
49          final Triple<String, String, String> triple2 = Triple.of("A", "D", "B");
50          assertEquals(0, triple1.compareTo(triple1));
51          assertTrue(triple1.compareTo(triple2) < 0);
52          assertEquals(0, triple2.compareTo(triple2));
53          assertTrue(triple2.compareTo(triple1) > 0);
54      }
55  
56      @Test
57      void testComparable3() {
58          final Triple<String, String, String> triple1 = Triple.of("A", "A", "D");
59          final Triple<String, String, String> triple2 = Triple.of("A", "B", "C");
60          assertEquals(0, triple1.compareTo(triple1));
61          assertTrue(triple1.compareTo(triple2) < 0);
62          assertEquals(0, triple2.compareTo(triple2));
63          assertTrue(triple2.compareTo(triple1) > 0);
64      }
65  
66      @Test
67      void testComparable4() {
68          final Triple<String, String, String> triple1 = Triple.of("B", "A", "C");
69          final Triple<String, String, String> triple2 = Triple.of("B", "A", "D");
70          assertEquals(0, triple1.compareTo(triple1));
71          assertTrue(triple1.compareTo(triple2) < 0);
72          assertEquals(0, triple2.compareTo(triple2));
73          assertTrue(triple2.compareTo(triple1) > 0);
74      }
75  
76      @Test
77      void testCompatibilityBetweenTriples() {
78          final Triple<Integer, String, Boolean> triple = ImmutableTriple.of(0, "foo", Boolean.TRUE);
79          final Triple<Integer, String, Boolean> triple2 = MutableTriple.of(0, "foo", Boolean.TRUE);
80          assertEquals(triple, triple2);
81          assertEquals(triple.hashCode(), triple2.hashCode());
82          final HashSet<Triple<Integer, String, Boolean>> set = new HashSet<>();
83          set.add(triple);
84          assertTrue(set.contains(triple2));
85      }
86  
87      @Test
88      void testEmptyArrayGenerics() {
89          final Triple<Integer, String, Boolean>[] empty = Triple.emptyArray();
90          assertEquals(0, empty.length);
91      }
92  
93      @Test
94      void testEmptyArrayLength() {
95          @SuppressWarnings("unchecked")
96          final Triple<Integer, String, Boolean>[] empty = (Triple<Integer, String, Boolean>[]) Triple.EMPTY_ARRAY;
97          assertEquals(0, empty.length);
98      }
99  
100     @Test
101     void testFormattable_padded() {
102         final Triple<String, String, String> triple = Triple.of("Key", "Something", "Value");
103         assertEquals("         (Key,Something,Value)", String.format("%1$30s", triple));
104     }
105 
106     @Test
107     void testFormattable_simple() {
108         final Triple<String, String, String> triple = Triple.of("Key", "Something", "Value");
109         assertEquals("(Key,Something,Value)", String.format("%1$s", triple));
110     }
111 
112     @Test
113     void testOfNonNull() {
114         assertNullPointerException(() -> Triple.ofNonNull(null, null, null));
115         assertNullPointerException(() -> Triple.ofNonNull(null, null, "z"));
116         assertNullPointerException(() -> Triple.ofNonNull(null, "y", "z"));
117         assertNullPointerException(() -> Triple.ofNonNull("x", null, null));
118         assertNullPointerException(() -> Triple.ofNonNull("x", "y", null));
119         final Triple<String, String, String> pair = Triple.ofNonNull("x", "y", "z");
120         assertEquals("x", pair.getLeft());
121         assertEquals("y", pair.getMiddle());
122         assertEquals("z", pair.getRight());
123     }
124 
125     @Test
126     void testToString() {
127         final Triple<String, String, String> triple = Triple.of("Key", "Something", "Value");
128         assertEquals("(Key,Something,Value)", triple.toString());
129     }
130 
131     @Test
132     void testToStringCustom() {
133         final Calendar date = Calendar.getInstance();
134         date.set(2011, Calendar.APRIL, 25);
135         final Triple<String, String, Calendar> triple = Triple.of("DOB", "string", date);
136         assertEquals("Test created on 04-25-2011", triple.toString("Test created on %3$tm-%3$td-%3$tY"));
137     }
138 
139     @Test
140     void testTripleOf() {
141         final Triple<Integer, String, Boolean> triple = Triple.of(0, "foo", Boolean.TRUE);
142         assertInstanceOf(ImmutableTriple.class, triple);
143         assertEquals(0, ((ImmutableTriple<Integer, String, Boolean>) triple).left.intValue());
144         assertEquals("foo", ((ImmutableTriple<Integer, String, Boolean>) triple).middle);
145         assertEquals(Boolean.TRUE, ((ImmutableTriple<Integer, String, Boolean>) triple).right);
146         final Triple<Object, String, Long> triple2 = Triple.of(null, "bar", Long.valueOf(200L));
147         assertInstanceOf(ImmutableTriple.class, triple2);
148         assertNull(((ImmutableTriple<Object, String, Long>) triple2).left);
149         assertEquals("bar", ((ImmutableTriple<Object, String, Long>) triple2).middle);
150         assertEquals(Long.valueOf(200L), ((ImmutableTriple<Object, String, Long>) triple2).right);
151     }
152 
153 }
154