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.juneau.commons.function;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  class Tuple1_Test extends TestBase {
28  
29  	//------------------------------------------------------------------------------------------------------------------
30  	// Basic tests.
31  	//------------------------------------------------------------------------------------------------------------------
32  	@Test void a01_basic() {
33  		var x = Tuple1.of("foo");
34  		assertEquals("foo", x.getA());
35  	}
36  
37  	@Test void a02_withNull() {
38  		var x = Tuple1.of((String)null);
39  		assertNull(x.getA());
40  	}
41  
42  	@Test void a03_withInteger() {
43  		var x = Tuple1.of(42);
44  		assertEquals(42, x.getA());
45  	}
46  
47  	//------------------------------------------------------------------------------------------------------------------
48  	// Equality tests.
49  	//------------------------------------------------------------------------------------------------------------------
50  	@Test void a04_equality_sameValues() {
51  		var x1 = Tuple1.of("foo");
52  		var x2 = Tuple1.of("foo");
53  		assertEquals(x1, x2);
54  		assertEquals(x1.hashCode(), x2.hashCode());
55  	}
56  
57  	@Test void a05_equality_differentValues() {
58  		var x1 = Tuple1.of("foo");
59  		var x2 = Tuple1.of("bar");
60  		assertNotEquals(x1, x2);
61  		// Hash codes may or may not be different, but values are different
62  	}
63  
64  	@Test void a06_equality_withNull() {
65  		var x1 = Tuple1.of((String)null);
66  		var x2 = Tuple1.of((String)null);
67  		var x3 = Tuple1.of("foo");
68  		assertEquals(x1, x2);
69  		assertEquals(x1.hashCode(), x2.hashCode());
70  		assertNotEquals(x1, x3);
71  		assertNotEquals(x3, x1);
72  	}
73  
74  	@Test void a07_equality_differentTypes() {
75  		var x1 = Tuple1.of("42");
76  		var x2 = Tuple1.of(42);
77  		assertNotEquals(x1, x2);
78  	}
79  
80  	@Test void a08_equality_notTuple1() {
81  		var x1 = Tuple1.of("foo");
82  		assertNotEquals(x1, "foo");
83  		assertNotEquals(x1, null);
84  		assertNotEquals(x1, new Object());
85  	}
86  
87  	//------------------------------------------------------------------------------------------------------------------
88  	// Array support tests (as mentioned in Javadoc).
89  	//------------------------------------------------------------------------------------------------------------------
90  	@Test void a09_arrayAsKey() {
91  		Map<Tuple1<String[]>,Integer> map = new HashMap<>();
92  		var key1 = Tuple1.of(new String[]{"a", "b"});
93  		var key2 = Tuple1.of(new String[]{"a", "b"});
94  		var key3 = Tuple1.of(new String[]{"c", "d"});
95  
96  		map.put(key1, 1);
97  		map.put(key2, 2);  // Should replace first entry
98  		map.put(key3, 3);
99  
100 		assertEquals(2, map.size());
101 		assertEquals(2, map.get(key1));  // Should get the replaced value
102 		assertEquals(2, map.get(key2));  // Same key
103 		assertEquals(3, map.get(key3));
104 	}
105 
106 	@Test void a10_arrayEquality() {
107 		var x1 = Tuple1.of(new String[]{"a", "b"});
108 		var x2 = Tuple1.of(new String[]{"a", "b"});
109 		var x3 = Tuple1.of(new String[]{"c", "d"});
110 
111 		assertEquals(x1, x2);
112 		assertEquals(x1.hashCode(), x2.hashCode());
113 		assertNotEquals(x1, x3);
114 	}
115 
116 	@Test void a11_arrayWithNullElements() {
117 		var x1 = Tuple1.of(new String[]{"a", null, "b"});
118 		var x2 = Tuple1.of(new String[]{"a", null, "b"});
119 		var x3 = Tuple1.of(new String[]{"a", "b"});
120 
121 		assertEquals(x1, x2);
122 		assertEquals(x1.hashCode(), x2.hashCode());
123 		assertNotEquals(x1, x3);
124 	}
125 
126 	@Test void a12_emptyArray() {
127 		var x1 = Tuple1.of(new String[0]);
128 		var x2 = Tuple1.of(new String[0]);
129 		var x3 = Tuple1.of(new String[]{"a"});
130 
131 		assertEquals(x1, x2);
132 		assertEquals(x1.hashCode(), x2.hashCode());
133 		assertNotEquals(x1, x3);
134 	}
135 
136 	//------------------------------------------------------------------------------------------------------------------
137 	// Constructor vs static factory.
138 	//------------------------------------------------------------------------------------------------------------------
139 	@Test void a13_constructor() {
140 		var x1 = new Tuple1<>("foo");
141 		var x2 = Tuple1.of("foo");
142 		assertEquals(x1, x2);
143 		assertEquals(x1.hashCode(), x2.hashCode());
144 	}
145 
146 	//------------------------------------------------------------------------------------------------------------------
147 	// Optional methods tests.
148 	//------------------------------------------------------------------------------------------------------------------
149 	@Test void a14_optA_withValue() {
150 		var x = Tuple1.of("foo");
151 		var opt = x.optA();
152 		assertTrue(opt.isPresent());
153 		assertEquals("foo", opt.get());
154 	}
155 
156 	@Test void a15_optA_withNull() {
157 		var x = Tuple1.of((String)null);
158 		var opt = x.optA();
159 		assertFalse(opt.isPresent());
160 		assertTrue(opt.isEmpty());
161 	}
162 }
163