1 /* 2 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6797535 6889858 6891113 8013712 8011800 8014365 27 * @summary Basic tests for methods in java.util.Objects 28 * @author Joseph D. Darcy 29 */ 30 package test.java.util.Objects; 31 32 import java.util.*; 33 import java.util.function.*; 34 35 import org.testng.annotations.Test; 36 import static org.testng.Assert.assertEquals; 37 import static org.testng.Assert.assertFalse; 38 import static org.testng.Assert.assertTrue; 39 import static org.testng.Assert.fail; 40 41 public class BasicObjectsTest { 42 43 @Test testEquals()44 public void testEquals() { 45 Object[] values = {null, "42", 42}; 46 for(int i = 0; i < values.length; i++) { 47 for (int j = 0; j < values.length; j++) { 48 boolean expected = (i == j); 49 Object a = values[i]; 50 Object b = values[j]; 51 boolean result = Objects.equals(a, b); 52 assertEquals(result, expected); 53 } 54 } 55 } 56 57 @Test testDeepEquals()58 public void testDeepEquals() { 59 Object[] values = {null, 60 null, // Change to values later 61 new byte[] {(byte)1}, 62 new short[] {(short)1}, 63 new int[] {1}, 64 new long[] {1L}, 65 new char[] {(char)1}, 66 new float[] {1.0f}, 67 new double[]{1.0d}, 68 new String[]{"one"}}; 69 values[1] = values; 70 71 for(int i = 0; i < values.length; i++) { 72 for (int j = 0; j < values.length; j++) { 73 boolean expected = (i == j); 74 Object a = values[i]; 75 Object b = values[j]; 76 boolean result = Objects.deepEquals(a, b); 77 assertEquals(result, expected); 78 } 79 } 80 } 81 82 @Test testHashCode()83 public void testHashCode() { 84 assertEquals(Objects.hashCode(null), 0); 85 String s = "42"; 86 assertEquals(Objects.hashCode(s), s.hashCode()); 87 } 88 89 @Test testHash()90 public void testHash() { 91 Object[] data = new String[]{"perfect", "ham", "THC"}; 92 assertEquals(Objects.hash((Object[])null), 0); 93 assertEquals(Objects.hash("perfect", "ham", "THC"), Arrays.hashCode(data)); 94 } 95 96 @Test testToString()97 public void testToString() { 98 assertEquals(Objects.toString(null), "null"); 99 String s = "Some string"; 100 assertEquals(Objects.toString(s), s); 101 } 102 103 @Test testToString2()104 public void testToString2() { 105 String s = "not the default"; 106 assertEquals(Objects.toString(null, s), s); 107 assertEquals(Objects.toString(s, "another string"), s); 108 } 109 110 @Test testCompare()111 public void testCompare() { 112 String[] values = {"e. e. cummings", "zzz"}; 113 String[] VALUES = {"E. E. Cummings", "ZZZ"}; 114 compareTest(null, null, 0); 115 for(int i = 0; i < values.length; i++) { 116 String a = values[i]; 117 compareTest(a, a, 0); 118 for(int j = 0; j < VALUES.length; j++) { 119 int expected = Integer.compare(i, j); 120 String b = VALUES[j]; 121 compareTest(a, b, expected); 122 } 123 } 124 } 125 compareTest(String a, String b, int expected)126 private static void compareTest(String a, String b, int expected) { 127 int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER); 128 assertEquals(Integer.signum(result), Integer.signum(expected)); 129 } 130 131 @Test testRequireNonNull()132 public void testRequireNonNull() { 133 final String RNN_1 = "1-arg requireNonNull"; 134 final String RNN_2 = "2-arg requireNonNull"; 135 final String RNN_3 = "Supplier requireNonNull"; 136 137 Function<String, String> rnn1 = s -> Objects.requireNonNull(s); 138 Function<String, String> rnn2 = s -> Objects.requireNonNull(s, "trousers"); 139 Function<String, String> rnn3 = s -> Objects.requireNonNull(s, () -> "trousers"); 140 141 testRNN_NonNull(rnn1, RNN_1); 142 testRNN_NonNull(rnn2, RNN_2); 143 testRNN_NonNull(rnn3, RNN_3); 144 145 testRNN_Null(rnn1, RNN_1, null); 146 testRNN_Null(rnn2, RNN_2, "trousers"); 147 testRNN_Null(rnn3, RNN_3, "trousers"); 148 } 149 testRNN_NonNull(Function<String, String> testFunc, String testFuncName)150 private static void testRNN_NonNull(Function<String, String> testFunc, 151 String testFuncName) { 152 try { 153 String s = testFunc.apply("pants"); 154 if (s != "pants") { 155 fail(testFuncName + " failed to return its arg"); 156 } 157 } catch (NullPointerException e) { 158 fail(testFuncName + " threw unexpected NPE"); 159 } 160 } 161 testRNN_Null(Function<String, String> testFunc, String testFuncName, String expectedMessage)162 private static void testRNN_Null(Function<String, String> testFunc, 163 String testFuncName, 164 String expectedMessage) { 165 try { 166 String s = testFunc.apply(null); 167 fail(testFuncName + " failed to throw NPE"); 168 } catch (NullPointerException e) { 169 assertEquals(e.getMessage(), expectedMessage); 170 } 171 } 172 173 @Test testIsNull()174 public void testIsNull() { 175 assertTrue(Objects.isNull(null)); 176 assertFalse(Objects.isNull(Objects.class)); 177 } 178 179 @Test testNonNull()180 public void testNonNull() { 181 assertFalse(Objects.nonNull(null)); 182 assertTrue(Objects.nonNull(Objects.class)); 183 } 184 185 @Test testNonNullOf()186 public void testNonNullOf() { 187 String defString = new String("default"); 188 String nullString = null; 189 String nonNullString = "non-null"; 190 191 // Confirm the compile time return type matches 192 String result = Objects.requireNonNullElse(nullString, defString); 193 194 if (result != defString) { 195 fail("comparison: references are not equal"); 196 } 197 if (Objects.requireNonNullElse(nonNullString, defString) != nonNullString) { 198 fail("comparison: Objects.requireNonNullElse(..., default)"); 199 } 200 if (Objects.requireNonNullElse(nonNullString, null) != nonNullString) { 201 fail("comparison: Objects.requireNonNullElse(..., null)"); 202 } 203 try { 204 Objects.requireNonNullElse(null, null); 205 fail("Unexpectedly didn't throw NPE"); 206 } catch (NullPointerException npe) { 207 // expected 208 assertEquals(npe.getMessage(), "defaultObj"); 209 } 210 211 212 // Test requireNonNullElseGet with a supplier 213 if (Objects.requireNonNullElseGet(nullString, () -> defString) != defString) { 214 fail("supplier: Objects.requireNonNullElseGet(nullString, () -> defString))"); 215 } 216 if (Objects.requireNonNullElseGet(nonNullString, () -> defString) != nonNullString) { 217 fail("supplier: Objects.requireNonNullElseGet(nonNullString, () -> defString))"); 218 } 219 if (Objects.requireNonNullElseGet(nonNullString, () -> null) != nonNullString) { 220 fail("Objects.requireNonNullElseGet(nonNullString, () -> null))"); 221 } 222 223 try { 224 Objects.requireNonNullElseGet(null, () -> null); 225 fail("Unexpectedly didn't throw NPE"); 226 } catch (NullPointerException npe) { 227 // expected 228 assertEquals(npe.getMessage(), "supplier.get()"); 229 } 230 try { // supplier is null 231 Objects.requireNonNullElseGet(null, null); 232 fail("Unexpectedly didn't throw NPE"); 233 } catch (NullPointerException npe) { 234 // expected 235 assertEquals(npe.getMessage(), "supplier"); 236 } 237 } 238 } 239