test/jdk/java/util/Objects/BasicObjectsTest.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     1
/*
33277
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
     2
 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     4
 *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     8
 *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    13
 * accompanied this code).
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    14
 *
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4165
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4165
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4165
diff changeset
    21
 * questions.
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    22
 */
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    23
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    24
/*
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    25
 * @test
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
    26
 * @bug 6797535 6889858 6891113 8013712 8011800 8014365
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    27
 * @summary Basic tests for methods in java.util.Objects
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    28
 * @author  Joseph D. Darcy
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    29
 */
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    30
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    31
import java.util.*;
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
    32
import java.util.function.*;
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    33
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    34
public class BasicObjectsTest {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    35
    public static void main(String... args) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    36
        int errors = 0;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    37
        errors += testEquals();
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    38
        errors += testDeepEquals();
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    39
        errors += testHashCode();
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    40
        errors += testHash();
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    41
        errors += testToString();
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    42
        errors += testToString2();
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    43
        errors += testCompare();
17431
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
    44
        errors += testRequireNonNull();
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
    45
        errors += testIsNull();
16864
e3d9b9cf6d3c 8012343: Objects.requireNonNull(Object,Supplier) breaks genstubs build
chegar
parents: 16863
diff changeset
    46
        errors += testNonNull();
33277
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
    47
        errors += testNonNullOf();
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    48
        if (errors > 0 )
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    49
            throw new RuntimeException();
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    50
    }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    51
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    52
    private static int testEquals() {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    53
        int errors = 0;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    54
        Object[] values = {null, "42", 42};
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    55
        for(int i = 0; i < values.length; i++)
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    56
            for(int j = 0; j < values.length; j++) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    57
                boolean expected = (i == j);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    58
                Object a = values[i];
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    59
                Object b = values[j];
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    60
                boolean result = Objects.equals(a, b);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    61
                if (result != expected) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    62
                    errors++;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    63
                    System.err.printf("When equating %s to %s, got %b instead of %b%n.",
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    64
                                      a, b, result, expected);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    65
                }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    66
            }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    67
        return errors;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    68
    }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
    69
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    70
    private static int testDeepEquals() {
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    71
        int errors = 0;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    72
        Object[] values = {null,
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    73
                           null, // Change to values later
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    74
                           new byte[]  {(byte)1},
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    75
                           new short[] {(short)1},
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    76
                           new int[]   {1},
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    77
                           new long[]  {1L},
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    78
                           new char[]  {(char)1},
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    79
                           new float[] {1.0f},
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    80
                           new double[]{1.0d},
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    81
                           new String[]{"one"}};
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    82
        values[1] = values;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    83
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    84
        for(int i = 0; i < values.length; i++)
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    85
            for(int j = 0; j < values.length; j++) {
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    86
                boolean expected = (i == j);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    87
                Object a = values[i];
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    88
                Object b = values[j];
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    89
                boolean result = Objects.deepEquals(a, b);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    90
                if (result != expected) {
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    91
                    errors++;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    92
                    System.err.printf("When equating %s to %s, got %b instead of %b%n.",
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    93
                                      a, b, result, expected);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    94
                }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    95
            }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    96
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    97
        return errors;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    98
    }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
    99
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   100
    private static int testHashCode() {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   101
        int errors = 0;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   102
        errors += (Objects.hashCode(null) == 0 ) ? 0 : 1;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   103
        String s = "42";
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   104
        errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   105
        return errors;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   106
    }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   107
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   108
    private static int testHash() {
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   109
        int errors = 0;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   110
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   111
        Object[] data = new String[]{"perfect", "ham", "THC"};
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   112
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   113
        errors += ((Objects.hash((Object[])null) == 0) ? 0 : 1);
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   114
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   115
        errors += (Objects.hash("perfect", "ham", "THC") ==
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   116
                   Arrays.hashCode(data)) ? 0 : 1;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   117
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   118
        return errors;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   119
    }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   120
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   121
    private static int testToString() {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   122
        int errors = 0;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   123
        errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   124
        String s = "Some string";
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   125
        errors += (s.equals(Objects.toString(s)) ) ? 0 : 1;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   126
        return errors;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   127
    }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   128
4165
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   129
    private static int testToString2() {
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   130
        int errors = 0;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   131
        String s = "not the default";
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   132
        errors += (s.equals(Objects.toString(null, s)) ) ? 0 : 1;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   133
        errors += (s.equals(Objects.toString(s, "another string")) ) ? 0 : 1;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   134
        return errors;
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   135
    }
7cd799c224da 6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
darcy
parents: 4061
diff changeset
   136
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   137
    private static int testCompare() {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   138
        int errors = 0;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   139
        String[] values = {"e. e. cummings", "zzz"};
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   140
        String[] VALUES = {"E. E. Cummings", "ZZZ"};
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   141
        errors += compareTest(null, null, 0);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   142
        for(int i = 0; i < values.length; i++) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   143
            String a = values[i];
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   144
            errors += compareTest(a, a, 0);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   145
            for(int j = 0; j < VALUES.length; j++) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   146
                int expected = Integer.compare(i, j);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   147
                String b = VALUES[j];
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   148
                errors += compareTest(a, b, expected);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   149
            }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   150
        }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   151
        return errors;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   152
    }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   153
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   154
    private static int compareTest(String a, String b, int expected) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   155
        int errors = 0;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   156
        int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   157
        if (Integer.signum(result) != Integer.signum(expected)) {
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   158
            errors++;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   159
            System.err.printf("When comparing %s to %s, got %d instead of %d%n.",
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   160
                              a, b, result, expected);
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   161
        }
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   162
        return errors;
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   163
    }
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   164
17431
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   165
    private static int testRequireNonNull() {
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   166
        int errors = 0;
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   167
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   168
        final String RNN_1 = "1-arg requireNonNull";
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   169
        final String RNN_2 = "2-arg requireNonNull";
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   170
        final String RNN_3 = "Supplier requireNonNull";
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   171
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   172
        Function<String, String> rnn1 = s -> Objects.requireNonNull(s);
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   173
        Function<String, String> rnn2 = s -> Objects.requireNonNull(s, "trousers");
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   174
        Function<String, String> rnn3 = s -> Objects.requireNonNull(s, () -> "trousers");
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   175
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   176
        errors += testRNN_NonNull(rnn1, RNN_1);
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   177
        errors += testRNN_NonNull(rnn2, RNN_2);
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   178
        errors += testRNN_NonNull(rnn3, RNN_3);
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   179
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   180
        errors += testRNN_Null(rnn1, RNN_1, null);
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   181
        errors += testRNN_Null(rnn2, RNN_2, "trousers");
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   182
        errors += testRNN_Null(rnn3, RNN_3, "trousers");
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   183
        return errors;
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   184
    }
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   185
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   186
    private static int testRNN_NonNull(Function<String, String> testFunc,
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   187
                                       String testFuncName) {
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   188
        int errors = 0;
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   189
        try {
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   190
            String s = testFunc.apply("pants");
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   191
            if (s != "pants") {
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   192
                System.err.printf(testFuncName + " failed to return its arg");
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   193
                errors++;
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   194
            }
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   195
        } catch (NullPointerException e) {
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   196
            System.err.printf(testFuncName + " threw unexpected NPE");
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   197
            errors++;
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   198
        }
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   199
        return errors;
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   200
    }
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   201
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   202
    private static int testRNN_Null(Function<String, String> testFunc,
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   203
                                    String testFuncName,
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   204
                                    String expectedMessage) {
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   205
        int errors = 0;
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   206
        try {
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   207
            String s = testFunc.apply(null);
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   208
            System.err.printf(testFuncName + " failed to throw NPE");
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   209
            errors++;
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   210
        } catch (NullPointerException e) {
17465
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   211
            if (e.getMessage() != expectedMessage) {
6ea0e148a562 8014365: Restore Objects.requireNonNull(T, Supplier<String>)
darcy
parents: 17431
diff changeset
   212
                System.err.printf(testFuncName + " threw NPE w/ bad detail msg");
4061
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   213
                errors++;
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   214
            }
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   215
        }
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   216
        return errors;
35a627ad2443 6889858: Add nonNull methods to java.util.Objects
darcy
parents: 4038
diff changeset
   217
    }
17431
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   218
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   219
    private static int testIsNull() {
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   220
        int errors = 0;
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   221
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   222
        errors += Objects.isNull(null) ? 0 : 1;
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   223
        errors += Objects.isNull(Objects.class) ? 1 : 0;
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   224
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   225
        return errors;
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   226
    }
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   227
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   228
    private static int testNonNull() {
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   229
        int errors = 0;
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   230
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   231
        errors += Objects.nonNull(null) ? 1 : 0;
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   232
        errors += Objects.nonNull(Objects.class) ? 0 : 1;
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   233
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   234
        return errors;
335d87f0d817 8013712: Add Objects.nonNull and Objects.isNull
mduigou
parents: 16864
diff changeset
   235
    }
33277
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   236
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   237
    private static int testNonNullOf() {
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   238
        int errors = 0;
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   239
        String defString = new String("default");
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   240
        String nullString = null;
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   241
        String nonNullString = "non-null";
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   242
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   243
        // Confirm the compile time return type matches
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33277
diff changeset
   244
        String result = Objects.requireNonNullElse(nullString, defString);
33277
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   245
        errors += (result == defString) ? 0 : 1;
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33277
diff changeset
   246
        errors += (Objects.requireNonNullElse(nonNullString, defString) == nonNullString) ? 0 : 1;
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33277
diff changeset
   247
        errors += (Objects.requireNonNullElse(nonNullString, null) == nonNullString) ? 0 : 1;
33277
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   248
        try {
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33277
diff changeset
   249
            Objects.requireNonNullElse(null, null);
33277
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   250
            errors += 1;
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   251
        } catch (NullPointerException npe) {
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   252
            // expected
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   253
            errors += npe.getMessage().equals("defaultObj") ? 0 : 1;
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   254
        }
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   255
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   256
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33277
diff changeset
   257
        // Test requireNonNullElseGet with a supplier
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33277
diff changeset
   258
        errors += (Objects.requireNonNullElseGet(nullString, () -> defString) == defString) ? 0 : 1;
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33277
diff changeset
   259
        errors += (Objects.requireNonNullElseGet(nonNullString, () -> defString) == nonNullString) ? 0 : 1;
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33277
diff changeset
   260
        errors += (Objects.requireNonNullElseGet(nonNullString, () -> null) == nonNullString) ? 0 : 1;
33277
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   261
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   262
        try {
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33277
diff changeset
   263
            Objects.requireNonNullElseGet(null, () -> null);
33277
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   264
            errors += 1;
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   265
        } catch (NullPointerException npe) {
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   266
            // expected
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   267
            errors += npe.getMessage().equals("supplier.get()") ? 0 : 1;
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   268
        }
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   269
        try {       // supplier is null
33675
7d9d372a41df 8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
rriggs
parents: 33277
diff changeset
   270
            Objects.requireNonNullElseGet(null, null);
33277
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   271
            errors += 1;
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   272
        } catch (NullPointerException npe) {
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   273
            // expected
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   274
            errors += npe.getMessage().equals("supplier") ? 0 : 1;
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   275
        }
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   276
        return errors;
90a40b7f53ef 8138963: java.lang.Objects new method to default to non-null
rriggs
parents: 17465
diff changeset
   277
    }
4038
ea7aa63169db 6797535: Add shared two argument static equals method to the platform
darcy
parents:
diff changeset
   278
}