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