Path: blob/master/test/jdk/java/util/Objects/BasicObjectsTest.java
41149 views
/*1* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6797535 6889858 6891113 8013712 8011800 801436526* @summary Basic tests for methods in java.util.Objects27* @author Joseph D. Darcy28*/2930import java.util.*;31import java.util.function.*;3233public class BasicObjectsTest {34public static void main(String... args) {35int errors = 0;36errors += testEquals();37errors += testDeepEquals();38errors += testHashCode();39errors += testHash();40errors += testToString();41errors += testToString2();42errors += testCompare();43errors += testRequireNonNull();44errors += testIsNull();45errors += testNonNull();46errors += testNonNullOf();47if (errors > 0 )48throw new RuntimeException();49}5051private static int testEquals() {52int errors = 0;53Object[] values = {null, "42", 42};54for(int i = 0; i < values.length; i++)55for(int j = 0; j < values.length; j++) {56boolean expected = (i == j);57Object a = values[i];58Object b = values[j];59boolean result = Objects.equals(a, b);60if (result != expected) {61errors++;62System.err.printf("When equating %s to %s, got %b instead of %b%n.",63a, b, result, expected);64}65}66return errors;67}6869private static int testDeepEquals() {70int errors = 0;71Object[] values = {null,72null, // Change to values later73new byte[] {(byte)1},74new short[] {(short)1},75new int[] {1},76new long[] {1L},77new char[] {(char)1},78new float[] {1.0f},79new double[]{1.0d},80new String[]{"one"}};81values[1] = values;8283for(int i = 0; i < values.length; i++)84for(int j = 0; j < values.length; j++) {85boolean expected = (i == j);86Object a = values[i];87Object b = values[j];88boolean result = Objects.deepEquals(a, b);89if (result != expected) {90errors++;91System.err.printf("When equating %s to %s, got %b instead of %b%n.",92a, b, result, expected);93}94}9596return errors;97}9899private static int testHashCode() {100int errors = 0;101errors += (Objects.hashCode(null) == 0 ) ? 0 : 1;102String s = "42";103errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1;104return errors;105}106107private static int testHash() {108int errors = 0;109110Object[] data = new String[]{"perfect", "ham", "THC"};111112errors += ((Objects.hash((Object[])null) == 0) ? 0 : 1);113114errors += (Objects.hash("perfect", "ham", "THC") ==115Arrays.hashCode(data)) ? 0 : 1;116117return errors;118}119120private static int testToString() {121int errors = 0;122errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1;123String s = "Some string";124errors += (s.equals(Objects.toString(s)) ) ? 0 : 1;125return errors;126}127128private static int testToString2() {129int errors = 0;130String s = "not the default";131errors += (s.equals(Objects.toString(null, s)) ) ? 0 : 1;132errors += (s.equals(Objects.toString(s, "another string")) ) ? 0 : 1;133return errors;134}135136private static int testCompare() {137int errors = 0;138String[] values = {"e. e. cummings", "zzz"};139String[] VALUES = {"E. E. Cummings", "ZZZ"};140errors += compareTest(null, null, 0);141for(int i = 0; i < values.length; i++) {142String a = values[i];143errors += compareTest(a, a, 0);144for(int j = 0; j < VALUES.length; j++) {145int expected = Integer.compare(i, j);146String b = VALUES[j];147errors += compareTest(a, b, expected);148}149}150return errors;151}152153private static int compareTest(String a, String b, int expected) {154int errors = 0;155int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);156if (Integer.signum(result) != Integer.signum(expected)) {157errors++;158System.err.printf("When comparing %s to %s, got %d instead of %d%n.",159a, b, result, expected);160}161return errors;162}163164private static int testRequireNonNull() {165int errors = 0;166167final String RNN_1 = "1-arg requireNonNull";168final String RNN_2 = "2-arg requireNonNull";169final String RNN_3 = "Supplier requireNonNull";170171Function<String, String> rnn1 = s -> Objects.requireNonNull(s);172Function<String, String> rnn2 = s -> Objects.requireNonNull(s, "trousers");173Function<String, String> rnn3 = s -> Objects.requireNonNull(s, () -> "trousers");174175errors += testRNN_NonNull(rnn1, RNN_1);176errors += testRNN_NonNull(rnn2, RNN_2);177errors += testRNN_NonNull(rnn3, RNN_3);178179errors += testRNN_Null(rnn1, RNN_1, null);180errors += testRNN_Null(rnn2, RNN_2, "trousers");181errors += testRNN_Null(rnn3, RNN_3, "trousers");182return errors;183}184185private static int testRNN_NonNull(Function<String, String> testFunc,186String testFuncName) {187int errors = 0;188try {189String s = testFunc.apply("pants");190if (s != "pants") {191System.err.printf(testFuncName + " failed to return its arg");192errors++;193}194} catch (NullPointerException e) {195System.err.printf(testFuncName + " threw unexpected NPE");196errors++;197}198return errors;199}200201private static int testRNN_Null(Function<String, String> testFunc,202String testFuncName,203String expectedMessage) {204int errors = 0;205try {206String s = testFunc.apply(null);207System.err.printf(testFuncName + " failed to throw NPE");208errors++;209} catch (NullPointerException e) {210if (e.getMessage() != expectedMessage) {211System.err.printf(testFuncName + " threw NPE w/ bad detail msg");212errors++;213}214}215return errors;216}217218private static int testIsNull() {219int errors = 0;220221errors += Objects.isNull(null) ? 0 : 1;222errors += Objects.isNull(Objects.class) ? 1 : 0;223224return errors;225}226227private static int testNonNull() {228int errors = 0;229230errors += Objects.nonNull(null) ? 1 : 0;231errors += Objects.nonNull(Objects.class) ? 0 : 1;232233return errors;234}235236private static int testNonNullOf() {237int errors = 0;238String defString = new String("default");239String nullString = null;240String nonNullString = "non-null";241242// Confirm the compile time return type matches243String result = Objects.requireNonNullElse(nullString, defString);244errors += (result == defString) ? 0 : 1;245errors += (Objects.requireNonNullElse(nonNullString, defString) == nonNullString) ? 0 : 1;246errors += (Objects.requireNonNullElse(nonNullString, null) == nonNullString) ? 0 : 1;247try {248Objects.requireNonNullElse(null, null);249errors += 1;250} catch (NullPointerException npe) {251// expected252errors += npe.getMessage().equals("defaultObj") ? 0 : 1;253}254255256// Test requireNonNullElseGet with a supplier257errors += (Objects.requireNonNullElseGet(nullString, () -> defString) == defString) ? 0 : 1;258errors += (Objects.requireNonNullElseGet(nonNullString, () -> defString) == nonNullString) ? 0 : 1;259errors += (Objects.requireNonNullElseGet(nonNullString, () -> null) == nonNullString) ? 0 : 1;260261try {262Objects.requireNonNullElseGet(null, () -> null);263errors += 1;264} catch (NullPointerException npe) {265// expected266errors += npe.getMessage().equals("supplier.get()") ? 0 : 1;267}268try { // supplier is null269Objects.requireNonNullElseGet(null, null);270errors += 1;271} catch (NullPointerException npe) {272// expected273errors += npe.getMessage().equals("supplier") ? 0 : 1;274}275return errors;276}277}278279280