1 /* 2 * Copyright (c) 2009, 2022, 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 package test.java.util.Objects; 25 26 /* 27 * @test 28 * @bug 6797535 6889858 6891113 8013712 8011800 8014365 8280168 29 * @summary Basic tests for methods in java.util.Objects 30 */ 31 32 import java.util.*; 33 import java.util.function.*; 34 35 public class BasicObjectsTest { main(String... args)36 public static void main(String... args) { 37 int errors = 0; 38 errors += testEquals(); 39 errors += testDeepEquals(); 40 errors += testHashCode(); 41 errors += testHash(); 42 errors += testToString(); 43 errors += testToString2(); 44 errors += testToIdentityString(); 45 errors += testCompare(); 46 errors += testRequireNonNull(); 47 errors += testIsNull(); 48 errors += testNonNull(); 49 errors += testNonNullOf(); 50 if (errors > 0 ) 51 throw new RuntimeException(); 52 } 53 testEquals()54 private static int testEquals() { 55 int errors = 0; 56 Object[] values = {null, "42", 42}; 57 for(int i = 0; i < values.length; i++) 58 for(int j = 0; j < values.length; j++) { 59 boolean expected = (i == j); 60 Object a = values[i]; 61 Object b = values[j]; 62 boolean result = Objects.equals(a, b); 63 if (result != expected) { 64 errors++; 65 System.err.printf("When equating %s to %s, got %b instead of %b%n.", 66 a, b, result, expected); 67 } 68 } 69 return errors; 70 } 71 testDeepEquals()72 private static int testDeepEquals() { 73 int errors = 0; 74 Object[] values = {null, 75 null, // Change to values later 76 new byte[] {(byte)1}, 77 new short[] {(short)1}, 78 new int[] {1}, 79 new long[] {1L}, 80 new char[] {(char)1}, 81 new float[] {1.0f}, 82 new double[]{1.0d}, 83 new String[]{"one"}}; 84 values[1] = values; 85 86 for(int i = 0; i < values.length; i++) 87 for(int j = 0; j < values.length; j++) { 88 boolean expected = (i == j); 89 Object a = values[i]; 90 Object b = values[j]; 91 boolean result = Objects.deepEquals(a, b); 92 if (result != expected) { 93 errors++; 94 System.err.printf("When equating %s to %s, got %b instead of %b%n.", 95 a, b, result, expected); 96 } 97 } 98 99 return errors; 100 } 101 testHashCode()102 private static int testHashCode() { 103 int errors = 0; 104 errors += (Objects.hashCode(null) == 0 ) ? 0 : 1; 105 String s = "42"; 106 errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1; 107 return errors; 108 } 109 testHash()110 private static int testHash() { 111 int errors = 0; 112 113 Object[] data = new String[]{"perfect", "ham", "THC"}; 114 115 errors += ((Objects.hash((Object[])null) == 0) ? 0 : 1); 116 117 errors += (Objects.hash("perfect", "ham", "THC") == 118 Arrays.hashCode(data)) ? 0 : 1; 119 120 return errors; 121 } 122 testToString()123 private static int testToString() { 124 int errors = 0; 125 errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1; 126 String s = "Some string"; 127 errors += (s.equals(Objects.toString(s)) ) ? 0 : 1; 128 return errors; 129 } 130 testToString2()131 private static int testToString2() { 132 int errors = 0; 133 String s = "not the default"; 134 errors += (s.equals(Objects.toString(null, s)) ) ? 0 : 1; 135 errors += (s.equals(Objects.toString(s, "another string")) ) ? 0 : 1; 136 return errors; 137 } 138 testToIdentityString()139 private static int testToIdentityString() { 140 int errors = 0; 141 // Test null behavior 142 try { 143 Objects.toIdentityString(null); 144 errors++; 145 } catch (NullPointerException npe) { 146 ; // Expected 147 } 148 // Behavior on typical objects 149 Object o = new Object(){}; 150 errors += (Objects.toIdentityString(o).equals(o.toString()))? 0 : 1; 151 // Verify object's toString *not* called 152 Object badToString = new Object() { 153 @Override 154 public String toString() { 155 throw new RuntimeException(); 156 } 157 }; 158 Objects.toIdentityString(badToString); 159 // Verify object's hashCode *not* called 160 Object badHashCode = new Object() { 161 @Override 162 public int hashCode() { 163 throw new RuntimeException("0xDEADBEFF"); 164 } 165 }; 166 Objects.toIdentityString(badHashCode); 167 return errors; 168 } 169 testCompare()170 private static int testCompare() { 171 int errors = 0; 172 String[] values = {"e. e. cummings", "zzz"}; 173 String[] VALUES = {"E. E. Cummings", "ZZZ"}; 174 errors += compareTest(null, null, 0); 175 for(int i = 0; i < values.length; i++) { 176 String a = values[i]; 177 errors += compareTest(a, a, 0); 178 for(int j = 0; j < VALUES.length; j++) { 179 int expected = Integer.compare(i, j); 180 String b = VALUES[j]; 181 errors += compareTest(a, b, expected); 182 } 183 } 184 return errors; 185 } 186 compareTest(String a, String b, int expected)187 private static int compareTest(String a, String b, int expected) { 188 int errors = 0; 189 int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER); 190 if (Integer.signum(result) != Integer.signum(expected)) { 191 errors++; 192 System.err.printf("When comparing %s to %s, got %d instead of %d%n.", 193 a, b, result, expected); 194 } 195 return errors; 196 } 197 testRequireNonNull()198 private static int testRequireNonNull() { 199 int errors = 0; 200 201 final String RNN_1 = "1-arg requireNonNull"; 202 final String RNN_2 = "2-arg requireNonNull"; 203 final String RNN_3 = "Supplier requireNonNull"; 204 205 Function<String, String> rnn1 = s -> Objects.requireNonNull(s); 206 Function<String, String> rnn2 = s -> Objects.requireNonNull(s, "trousers"); 207 Function<String, String> rnn3 = s -> Objects.requireNonNull(s, () -> "trousers"); 208 209 errors += testRNN_NonNull(rnn1, RNN_1); 210 errors += testRNN_NonNull(rnn2, RNN_2); 211 errors += testRNN_NonNull(rnn3, RNN_3); 212 213 errors += testRNN_Null(rnn1, RNN_1, null); 214 errors += testRNN_Null(rnn2, RNN_2, "trousers"); 215 errors += testRNN_Null(rnn3, RNN_3, "trousers"); 216 return errors; 217 } 218 testRNN_NonNull(Function<String, String> testFunc, String testFuncName)219 private static int testRNN_NonNull(Function<String, String> testFunc, 220 String testFuncName) { 221 int errors = 0; 222 try { 223 String s = testFunc.apply("pants"); 224 if (s != "pants") { 225 System.err.printf(testFuncName + " failed to return its arg"); 226 errors++; 227 } 228 } catch (NullPointerException e) { 229 System.err.printf(testFuncName + " threw unexpected NPE"); 230 errors++; 231 } 232 return errors; 233 } 234 testRNN_Null(Function<String, String> testFunc, String testFuncName, String expectedMessage)235 private static int testRNN_Null(Function<String, String> testFunc, 236 String testFuncName, 237 String expectedMessage) { 238 int errors = 0; 239 try { 240 String s = testFunc.apply(null); 241 System.err.printf(testFuncName + " failed to throw NPE"); 242 errors++; 243 } catch (NullPointerException e) { 244 if (e.getMessage() != expectedMessage) { 245 System.err.printf(testFuncName + " threw NPE w/ bad detail msg"); 246 errors++; 247 } 248 } 249 return errors; 250 } 251 testIsNull()252 private static int testIsNull() { 253 int errors = 0; 254 255 errors += Objects.isNull(null) ? 0 : 1; 256 errors += Objects.isNull(Objects.class) ? 1 : 0; 257 258 return errors; 259 } 260 testNonNull()261 private static int testNonNull() { 262 int errors = 0; 263 264 errors += Objects.nonNull(null) ? 1 : 0; 265 errors += Objects.nonNull(Objects.class) ? 0 : 1; 266 267 return errors; 268 } 269 testNonNullOf()270 private static int testNonNullOf() { 271 int errors = 0; 272 String defString = new String("default"); 273 String nullString = null; 274 String nonNullString = "non-null"; 275 276 // Confirm the compile time return type matches 277 String result = Objects.requireNonNullElse(nullString, defString); 278 errors += (result == defString) ? 0 : 1; 279 errors += (Objects.requireNonNullElse(nonNullString, defString) == nonNullString) ? 0 : 1; 280 errors += (Objects.requireNonNullElse(nonNullString, null) == nonNullString) ? 0 : 1; 281 try { 282 Objects.requireNonNullElse(null, null); 283 errors += 1; 284 } catch (NullPointerException npe) { 285 // expected 286 errors += npe.getMessage().equals("defaultObj") ? 0 : 1; 287 } 288 289 290 // Test requireNonNullElseGet with a supplier 291 errors += (Objects.requireNonNullElseGet(nullString, () -> defString) == defString) ? 0 : 1; 292 errors += (Objects.requireNonNullElseGet(nonNullString, () -> defString) == nonNullString) ? 0 : 1; 293 errors += (Objects.requireNonNullElseGet(nonNullString, () -> null) == nonNullString) ? 0 : 1; 294 295 try { 296 Objects.requireNonNullElseGet(null, () -> null); 297 errors += 1; 298 } catch (NullPointerException npe) { 299 // expected 300 errors += npe.getMessage().equals("supplier.get()") ? 0 : 1; 301 } 302 try { // supplier is null 303 Objects.requireNonNullElseGet(null, null); 304 errors += 1; 305 } catch (NullPointerException npe) { 306 // expected 307 errors += npe.getMessage().equals("supplier") ? 0 : 1; 308 } 309 return errors; 310 } 311 } 312