1 /*
   2  * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6797535
  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 += testHashCode();
  38         errors += testToString();
  39         errors += testCompare();
  40         errors += testNonNull();
  41         if (errors > 0 )
  42             throw new RuntimeException();
  43     }
  44 
  45     private static int testEquals() {
  46         int errors = 0;
  47         Object[] values = {null, "42", 42};
  48         for(int i = 0; i < values.length; i++)
  49             for(int j = 0; j < values.length; j++) {
  50                 boolean expected = (i == j);
  51                 Object a = values[i];
  52                 Object b = values[j];
  53                 boolean result = Objects.equals(a, b);
  54                 if (result != expected) {
  55                     errors++;
  56                     System.err.printf("When equating %s to %s, got %b instead of %b%n.",
  57                                       a, b, result, expected);
  58                 }
  59             }
  60         return errors;
  61     }
  62 
  63     private static int testHashCode() {
  64         int errors = 0;
  65         errors += (Objects.hashCode(null) == 0 ) ? 0 : 1;
  66         String s = "42";
  67         errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1;
  68         return errors;
  69     }
  70 
  71     private static int testToString() {
  72         int errors = 0;
  73         errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1;
  74         String s = "Some string";
  75         errors += (s.equals(Objects.toString(s)) ) ? 0 : 1;
  76         return errors;
  77     }
  78 
  79     private static int testCompare() {
  80         int errors = 0;
  81         String[] values = {"e. e. cummings", "zzz"};
  82         String[] VALUES = {"E. E. Cummings", "ZZZ"};
  83         errors += compareTest(null, null, 0);
  84         for(int i = 0; i < values.length; i++) {
  85             String a = values[i];
  86             errors += compareTest(a, a, 0);
  87             for(int j = 0; j < VALUES.length; j++) {
  88                 int expected = Integer.compare(i, j);
  89                 String b = VALUES[j];
  90                 errors += compareTest(a, b, expected);
  91             }
  92         }
  93         return errors;
  94     }
  95 
  96     private static int compareTest(String a, String b, int expected) {
  97         int errors = 0;
  98         int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);
  99         if (Integer.signum(result) != Integer.signum(expected)) {
 100             errors++;
 101             System.err.printf("When comparing %s to %s, got %d instead of %d%n.",
 102                               a, b, result, expected);
 103         }
 104         return errors;
 105     }
 106 
 107     private static int testNonNull() {
 108         int errors = 0;
 109         String s;
 110 
 111         // Test 1-arg variant
 112         try {
 113             s = Objects.nonNull("pants");
 114             if (s != "pants") {
 115                 System.err.printf("1-arg non-null failed to return its arg");
 116                 errors++;
 117             }
 118         } catch (NullPointerException e) {
 119             System.err.printf("1-arg nonNull threw unexpected NPE");
 120             errors++;
 121         }
 122 
 123         try {
 124             s = Objects.nonNull(null);
 125             System.err.printf("1-arg nonNull failed to throw NPE");
 126             errors++;
 127         } catch (NullPointerException e) {
 128             // Expected
 129         }
 130 
 131         // Test 2-arg variant
 132         try {
 133             s = Objects.nonNull("pants", "trousers");
 134             if (s != "pants") {
 135                 System.err.printf("2-arg nonNull failed to return its arg");
 136                 errors++;
 137             }
 138         } catch (NullPointerException e) {
 139             System.err.printf("2-arg nonNull threw unexpected NPE");
 140             errors++;
 141         }
 142 
 143         try {
 144             s = Objects.nonNull(null, "pantaloons");
 145             System.err.printf("2-arg nonNull failed to throw NPE");
 146             errors++;
 147         } catch (NullPointerException e) {
 148             if (e.getMessage() != "pantaloons") {
 149                 System.err.printf("2-arg nonNull threw NPE w/ bad detail msg");
 150                 errors++;
 151             }
 152         }
 153         return errors;
 154     }
 155 }