1 /* 2 * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6797535 27 * @summary Basic tests for methods in java.util.Objects 28 * @author Joseph D. Darcy 29 */ 30 31 import java.util.*; 32 33 public class BasicObjectsTest { 34 public static void main(String... args) { 35 int errors = 0; 36 errors += testEquals(); 37 errors += testHashCode(); 38 errors += testToString(); 39 errors += testCompare(); 40 if (errors > 0 ) 41 throw new RuntimeException(); 42 } 43 44 private static int testEquals() { 45 int errors = 0; 46 Object[] values = {null, "42", 42}; 47 for(int i = 0; i < values.length; i++) 48 for(int j = 0; j < values.length; j++) { 49 boolean expected = (i == j); 50 Object a = values[i]; 51 Object b = values[j]; 52 boolean result = Objects.equals(a, b); 53 if (result != expected) { 54 errors++; 55 System.err.printf("When equating %s to %s, got %b intead of %b%n.", 56 a, b, result, expected); 57 } 58 } 59 return errors; 60 } 61 62 private static int testHashCode() { 63 int errors = 0; 64 errors += (Objects.hashCode(null) == 0 ) ? 0 : 1; 65 String s = "42"; 66 errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1; 67 return errors; 68 } 69 70 private static int testToString() { 71 int errors = 0; 72 errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1; 73 String s = "Some string"; 74 errors += (s.equals(Objects.toString(s)) ) ? 0 : 1; 75 return errors; 76 } 77 78 private static int testCompare() { 79 int errors = 0; 80 String[] values = {"e. e. cummings", "zzz"}; 81 String[] VALUES = {"E. E. Cummings", "ZZZ"}; 82 errors += compareTest(null, null, 0); 83 for(int i = 0; i < values.length; i++) { 84 String a = values[i]; 85 errors += compareTest(a, a, 0); 86 for(int j = 0; j < VALUES.length; j++) { 87 int expected = Integer.compare(i, j); 88 String b = VALUES[j]; 89 errors += compareTest(a, b, expected); 90 } 91 } 92 return errors; 93 } 94 95 private static int compareTest(String a, String b, int expected) { 96 int errors = 0; 97 int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER); 98 if (Integer.signum(result) != Integer.signum(expected)) { 99 errors++; 100 System.err.printf("When comaping %s to %s, got %d intead of %d%n.", 101 a, b, result, expected); 102 } 103 return errors; 104 } 105 }