1745N/A/*
3909N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
1745N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1745N/A *
1745N/A * This code is free software; you can redistribute it and/or modify it
1745N/A * under the terms of the GNU General Public License version 2 only, as
1745N/A * published by the Free Software Foundation.
1745N/A *
1745N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1745N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1745N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1745N/A * version 2 for more details (a copy is included in the LICENSE file that
1745N/A * accompanied this code).
1745N/A *
1745N/A * You should have received a copy of the GNU General Public License version
1745N/A * 2 along with this work; if not, write to the Free Software Foundation,
1745N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1745N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1745N/A */
1745N/A
1745N/A/*
1745N/A * @test
1799N/A * @bug 6797535 6889858 6891113
1745N/A * @summary Basic tests for methods in java.util.Objects
1745N/A * @author Joseph D. Darcy
1745N/A */
1745N/A
1745N/Aimport java.util.*;
1745N/A
1745N/Apublic class BasicObjectsTest {
1745N/A public static void main(String... args) {
1745N/A int errors = 0;
1745N/A errors += testEquals();
1799N/A errors += testDeepEquals();
1745N/A errors += testHashCode();
1799N/A errors += testHash();
1745N/A errors += testToString();
1799N/A errors += testToString2();
1745N/A errors += testCompare();
1768N/A errors += testNonNull();
1745N/A if (errors > 0 )
1745N/A throw new RuntimeException();
1745N/A }
1745N/A
1745N/A private static int testEquals() {
1745N/A int errors = 0;
1745N/A Object[] values = {null, "42", 42};
1745N/A for(int i = 0; i < values.length; i++)
1745N/A for(int j = 0; j < values.length; j++) {
1745N/A boolean expected = (i == j);
1745N/A Object a = values[i];
1745N/A Object b = values[j];
1745N/A boolean result = Objects.equals(a, b);
1745N/A if (result != expected) {
1745N/A errors++;
1745N/A System.err.printf("When equating %s to %s, got %b instead of %b%n.",
1745N/A a, b, result, expected);
1745N/A }
1745N/A }
1745N/A return errors;
1745N/A }
1745N/A
1799N/A private static int testDeepEquals() {
1799N/A int errors = 0;
1799N/A Object[] values = {null,
1799N/A null, // Change to values later
1799N/A new byte[] {(byte)1},
1799N/A new short[] {(short)1},
1799N/A new int[] {1},
1799N/A new long[] {1L},
1799N/A new char[] {(char)1},
1799N/A new float[] {1.0f},
1799N/A new double[]{1.0d},
1799N/A new String[]{"one"}};
1799N/A values[1] = values;
1799N/A
1799N/A for(int i = 0; i < values.length; i++)
1799N/A for(int j = 0; j < values.length; j++) {
1799N/A boolean expected = (i == j);
1799N/A Object a = values[i];
1799N/A Object b = values[j];
1799N/A boolean result = Objects.deepEquals(a, b);
1799N/A if (result != expected) {
1799N/A errors++;
1799N/A System.err.printf("When equating %s to %s, got %b instead of %b%n.",
1799N/A a, b, result, expected);
1799N/A }
1799N/A }
1799N/A
1799N/A return errors;
1799N/A }
1799N/A
1745N/A private static int testHashCode() {
1745N/A int errors = 0;
1745N/A errors += (Objects.hashCode(null) == 0 ) ? 0 : 1;
1745N/A String s = "42";
1745N/A errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1;
1745N/A return errors;
1745N/A }
1745N/A
1799N/A private static int testHash() {
1799N/A int errors = 0;
1799N/A
1799N/A Object[] data = new String[]{"perfect", "ham", "THC"};
1799N/A
1799N/A errors += ((Objects.hash((Object[])null) == 0) ? 0 : 1);
1799N/A
1799N/A errors += (Objects.hash("perfect", "ham", "THC") ==
1799N/A Arrays.hashCode(data)) ? 0 : 1;
1799N/A
1799N/A return errors;
1799N/A }
1799N/A
1745N/A private static int testToString() {
1745N/A int errors = 0;
1745N/A errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1;
1745N/A String s = "Some string";
1745N/A errors += (s.equals(Objects.toString(s)) ) ? 0 : 1;
1745N/A return errors;
1745N/A }
1745N/A
1799N/A private static int testToString2() {
1799N/A int errors = 0;
1799N/A String s = "not the default";
1799N/A errors += (s.equals(Objects.toString(null, s)) ) ? 0 : 1;
1799N/A errors += (s.equals(Objects.toString(s, "another string")) ) ? 0 : 1;
1799N/A return errors;
1799N/A }
1799N/A
1745N/A private static int testCompare() {
1745N/A int errors = 0;
1745N/A String[] values = {"e. e. cummings", "zzz"};
1745N/A String[] VALUES = {"E. E. Cummings", "ZZZ"};
1745N/A errors += compareTest(null, null, 0);
1745N/A for(int i = 0; i < values.length; i++) {
1745N/A String a = values[i];
1745N/A errors += compareTest(a, a, 0);
1745N/A for(int j = 0; j < VALUES.length; j++) {
1745N/A int expected = Integer.compare(i, j);
1745N/A String b = VALUES[j];
1745N/A errors += compareTest(a, b, expected);
1745N/A }
1745N/A }
1745N/A return errors;
1745N/A }
1745N/A
1745N/A private static int compareTest(String a, String b, int expected) {
1745N/A int errors = 0;
1745N/A int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);
1745N/A if (Integer.signum(result) != Integer.signum(expected)) {
1745N/A errors++;
1745N/A System.err.printf("When comparing %s to %s, got %d instead of %d%n.",
1745N/A a, b, result, expected);
1745N/A }
1745N/A return errors;
1745N/A }
1768N/A
1768N/A private static int testNonNull() {
1768N/A int errors = 0;
1768N/A String s;
1768N/A
1768N/A // Test 1-arg variant
1768N/A try {
3479N/A s = Objects.requireNonNull("pants");
1768N/A if (s != "pants") {
1768N/A System.err.printf("1-arg non-null failed to return its arg");
1768N/A errors++;
1768N/A }
1768N/A } catch (NullPointerException e) {
1768N/A System.err.printf("1-arg nonNull threw unexpected NPE");
1768N/A errors++;
1768N/A }
1768N/A
1768N/A try {
3479N/A s = Objects.requireNonNull(null);
1768N/A System.err.printf("1-arg nonNull failed to throw NPE");
1768N/A errors++;
1768N/A } catch (NullPointerException e) {
1768N/A // Expected
1768N/A }
1768N/A
1768N/A // Test 2-arg variant
1768N/A try {
3479N/A s = Objects.requireNonNull("pants", "trousers");
1768N/A if (s != "pants") {
1768N/A System.err.printf("2-arg nonNull failed to return its arg");
1768N/A errors++;
1768N/A }
1768N/A } catch (NullPointerException e) {
1768N/A System.err.printf("2-arg nonNull threw unexpected NPE");
1768N/A errors++;
1768N/A }
1768N/A
1768N/A try {
3479N/A s = Objects.requireNonNull(null, "pantaloons");
1768N/A System.err.printf("2-arg nonNull failed to throw NPE");
1768N/A errors++;
1768N/A } catch (NullPointerException e) {
1768N/A if (e.getMessage() != "pantaloons") {
1768N/A System.err.printf("2-arg nonNull threw NPE w/ bad detail msg");
1768N/A errors++;
1768N/A }
1768N/A }
1768N/A return errors;
1768N/A }
1745N/A}