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