rev 7044 : 8013712: Add Objects.nonNull and Objects.isNull
Reviewed-by: duke

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