1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package libcore.java.util;
18 
19 import java.util.Arrays;
20 import java.util.Objects;
21 
22 public class ObjectsTest extends junit.framework.TestCase {
23   public static final class Hello {
toString()24     public String toString() { return "hello"; }
25   }
26 
test_compare()27   public void test_compare() throws Exception {
28     assertEquals(0, Objects.compare(null, null, String.CASE_INSENSITIVE_ORDER));
29     assertEquals(0, Objects.compare("a", "A", String.CASE_INSENSITIVE_ORDER));
30     assertEquals(-1, Objects.compare("a", "b", String.CASE_INSENSITIVE_ORDER));
31     assertEquals(1, Objects.compare("b", "a", String.CASE_INSENSITIVE_ORDER));
32   }
33 
test_deepEquals()34   public void test_deepEquals() throws Exception {
35     int[] xs = new int[3];
36     int[] ys = new int[4];
37     int[] zs = new int[3];
38     String[] o1 = new String[] { "hello" };
39     String[] o2 = new String[] { "world" };
40     String[] o3 = new String[] { "hello" };
41     assertTrue(Objects.deepEquals(null, null));
42     assertFalse(Objects.deepEquals(xs, null));
43     assertFalse(Objects.deepEquals(null, xs));
44     assertTrue(Objects.deepEquals(xs, xs));
45     assertTrue(Objects.deepEquals(xs, zs));
46     assertFalse(Objects.deepEquals(xs, ys));
47     assertTrue(Objects.deepEquals(o1, o1));
48     assertTrue(Objects.deepEquals(o1, o3));
49     assertFalse(Objects.deepEquals(o1, o2));
50     assertTrue(Objects.deepEquals("hello", "hello"));
51     assertFalse(Objects.deepEquals("hello", "world"));
52   }
53 
test_equals()54   public void test_equals() throws Exception {
55     Hello h1 = new Hello();
56     Hello h2 = new Hello();
57     assertTrue(Objects.equals(null, null));
58     assertFalse(Objects.equals(h1, null));
59     assertFalse(Objects.equals(null, h1));
60     assertFalse(Objects.equals(h1, h2));
61     assertTrue(Objects.equals(h1, h1));
62   }
63 
test_hash()64   public void test_hash() throws Exception {
65     assertEquals(Arrays.hashCode(new Object[0]), Objects.hash());
66     assertEquals(31, Objects.hash((Object) null));
67     assertEquals(0, Objects.hash((Object[]) null));
68     assertEquals(-1107615551, Objects.hash("hello", "world"));
69     assertEquals(23656287, Objects.hash("hello", "world", null));
70   }
71 
test_hashCode()72   public void test_hashCode() throws Exception {
73     Hello h = new Hello();
74     assertEquals(h.hashCode(), Objects.hashCode(h));
75     assertEquals(0, Objects.hashCode(null));
76   }
77 
test_requireNonNull_T()78   public void test_requireNonNull_T() throws Exception {
79     Hello h = new Hello();
80     assertEquals(h, Objects.requireNonNull(h));
81     try {
82       Objects.requireNonNull(null);
83       fail();
84     } catch (NullPointerException expected) {
85       assertEquals(null, expected.getMessage());
86     }
87   }
88 
test_requireNonNull_T_String()89   public void test_requireNonNull_T_String() throws Exception {
90     Hello h = new Hello();
91     assertEquals(h, Objects.requireNonNull(h, "test"));
92     try {
93       Objects.requireNonNull(null, "message");
94       fail();
95     } catch (NullPointerException expected) {
96       assertEquals("message", expected.getMessage());
97     }
98     try {
99       Objects.requireNonNull(null, null);
100       fail();
101     } catch (NullPointerException expected) {
102       assertEquals(null, expected.getMessage());
103     }
104   }
105 
test_toString_Object()106   public void test_toString_Object() throws Exception {
107     assertEquals("hello", Objects.toString(new Hello()));
108     assertEquals("null", Objects.toString(null));
109   }
110 
test_toString_Object_String()111   public void test_toString_Object_String() throws Exception {
112     assertEquals("hello", Objects.toString(new Hello(), "world"));
113     assertEquals("world", Objects.toString(null, "world"));
114     assertEquals(null, Objects.toString(null, null));
115   }
116 }
117