1 /* 2 * Copyright (c) 2009, 2013, 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 8011800 27 * @summary Basic tests for methods in java.util.Objects 28 * @author Joseph D. Darcy 29 */ 30 31 import java.util.*; 32 import java.util.function.*; 33 34 public class BasicObjectsTest { 35 public static void main(String... args) { 36 int errors = 0; 37 errors += testEquals(); 38 errors += testDeepEquals(); 39 errors += testHashCode(); 40 errors += testHash(); 41 errors += testToString(); 42 errors += testToString2(); 43 errors += testCompare(); 44 errors += testRequireNonNull(); 45 if (errors > 0 ) 46 throw new RuntimeException(); 47 } 48 49 private static int testEquals() { 50 int errors = 0; 51 Object[] values = {null, "42", 42}; 52 for(int i = 0; i < values.length; i++) 53 for(int j = 0; j < values.length; j++) { 54 boolean expected = (i == j); 55 Object a = values[i]; 56 Object b = values[j]; 57 boolean result = Objects.equals(a, b); 58 if (result != expected) { 59 errors++; 60 System.err.printf("When equating %s to %s, got %b instead of %b%n.", 61 a, b, result, expected); 62 } 63 } 64 return errors; 65 } 66 67 private static int testDeepEquals() { 68 int errors = 0; 69 Object[] values = {null, 70 null, // Change to values later 71 new byte[] {(byte)1}, 72 new short[] {(short)1}, 73 new int[] {1}, 74 new long[] {1L}, 75 new char[] {(char)1}, 76 new float[] {1.0f}, 77 new double[]{1.0d}, 78 new String[]{"one"}}; 79 values[1] = values; 80 81 for(int i = 0; i < values.length; i++) 82 for(int j = 0; j < values.length; j++) { 83 boolean expected = (i == j); 84 Object a = values[i]; 85 Object b = values[j]; 86 boolean result = Objects.deepEquals(a, b); 87 if (result != expected) { 88 errors++; 89 System.err.printf("When equating %s to %s, got %b instead of %b%n.", 90 a, b, result, expected); 91 } 92 } 93 94 return errors; 95 } 96 97 private static int testHashCode() { 98 int errors = 0; 99 errors += (Objects.hashCode(null) == 0 ) ? 0 : 1; 100 String s = "42"; 101 errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1; 102 return errors; 103 } 104 105 private static int testHash() { 106 int errors = 0; 107 108 Object[] data = new String[]{"perfect", "ham", "THC"}; 109 110 errors += ((Objects.hash((Object[])null) == 0) ? 0 : 1); 111 112 errors += (Objects.hash("perfect", "ham", "THC") == 113 Arrays.hashCode(data)) ? 0 : 1; 114 115 return errors; 116 } 117 118 private static int testToString() { 119 int errors = 0; 120 errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1; 121 String s = "Some string"; 122 errors += (s.equals(Objects.toString(s)) ) ? 0 : 1; 123 return errors; 124 } 125 126 private static int testToString2() { 127 int errors = 0; 128 String s = "not the default"; 129 errors += (s.equals(Objects.toString(null, s)) ) ? 0 : 1; 130 errors += (s.equals(Objects.toString(s, "another string")) ) ? 0 : 1; 131 return errors; 132 } 133 134 private static int testCompare() { 135 int errors = 0; 136 String[] values = {"e. e. cummings", "zzz"}; 137 String[] VALUES = {"E. E. Cummings", "ZZZ"}; 138 errors += compareTest(null, null, 0); 139 for(int i = 0; i < values.length; i++) { 140 String a = values[i]; 141 errors += compareTest(a, a, 0); 142 for(int j = 0; j < VALUES.length; j++) { 143 int expected = Integer.compare(i, j); 144 String b = VALUES[j]; 145 errors += compareTest(a, b, expected); 146 } 147 } 148 return errors; 149 } 150 151 private static int compareTest(String a, String b, int expected) { 152 int errors = 0; 153 int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER); 154 if (Integer.signum(result) != Integer.signum(expected)) { 155 errors++; 156 System.err.printf("When comparing %s to %s, got %d instead of %d%n.", 157 a, b, result, expected); 158 } 159 return errors; 160 } 161 162 private static int testRequireNonNull() { 163 int errors = 0; 164 165 final String RNN_1 = "1-arg requireNonNull"; 166 final String RNN_2 = "2-arg requireNonNull"; 167 final String RNN_3 = "Supplier requireNonNull"; 168 169 Function<String, String> rnn1 = s -> Objects.requireNonNull(s); 170 Function<String, String> rnn2 = s -> Objects.requireNonNull(s, "trousers"); 171 Function<String, String> rnn3 = s -> Objects.requireNonNull(s, () -> "trousers"); 172 173 errors += testRNN_NonNull(rnn1, RNN_1); 174 errors += testRNN_NonNull(rnn2, RNN_2); 175 errors += testRNN_NonNull(rnn3, RNN_3); 176 177 errors += testRNN_Null(rnn1, RNN_1, null); 178 errors += testRNN_Null(rnn2, RNN_2, "trousers"); 179 errors += testRNN_Null(rnn3, RNN_3, "trousers"); 180 return errors; 181 } 182 183 private static int testRNN_NonNull(Function<String, String> testFunc, 184 String testFuncName) { 185 int errors = 0; 186 try { 187 String s = testFunc.apply("pants"); 188 if (s != "pants") { 189 System.err.printf(testFuncName + " failed to return its arg"); 190 errors++; 191 } 192 } catch (NullPointerException e) { 193 System.err.printf(testFuncName + " threw unexpected NPE"); 194 errors++; 195 } 196 return errors; 197 } 198 199 private static int testRNN_Null(Function<String, String> testFunc, 200 String testFuncName, 201 String expectedMessage) { 202 int errors = 0; 203 try { 204 String s = testFunc.apply(null); 205 System.err.printf(testFuncName + " failed to throw NPE"); 206 errors++; 207 } catch (NullPointerException e) { 208 if (e.getMessage() != expectedMessage) { 209 System.err.printf(testFuncName + " threw NPE w/ bad detail msg"); 210 errors++; 211 } 212 } 213 return errors; 214 } 215 }