• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Guava Authors
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 com.google.common.base;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.annotations.J2ktIncompatible;
22 import com.google.common.testing.NullPointerTester;
23 import junit.framework.TestCase;
24 
25 /**
26  * Tests for {@link Objects}.
27  *
28  * @author Laurence Gonsalves
29  */
30 @GwtCompatible(emulated = true)
31 public class ObjectsTest extends TestCase {
32 
testEqual()33   public void testEqual() throws Exception {
34     assertTrue(Objects.equal(1, 1));
35     assertTrue(Objects.equal(null, null));
36 
37     // test distinct string objects
38     String s1 = "foobar";
39     String s2 = new String(s1);
40     assertTrue(Objects.equal(s1, s2));
41 
42     assertFalse(Objects.equal(s1, null));
43     assertFalse(Objects.equal(null, s1));
44     assertFalse(Objects.equal("foo", "bar"));
45     assertFalse(Objects.equal("1", 1));
46   }
47 
testHashCode()48   public void testHashCode() throws Exception {
49     int h1 = Objects.hashCode(1, "two", 3.0);
50     int h2 = Objects.hashCode(Integer.valueOf(1), new String("two"), Double.valueOf(3.0));
51     // repeatable
52     assertEquals(h1, h2);
53 
54     // These don't strictly need to be true, but they're nice properties.
55     assertTrue(Objects.hashCode(1, 2, null) != Objects.hashCode(1, 2));
56     assertTrue(Objects.hashCode(1, 2, null) != Objects.hashCode(1, null, 2));
57     assertTrue(Objects.hashCode(1, null, 2) != Objects.hashCode(1, 2));
58     assertTrue(Objects.hashCode(1, 2, 3) != Objects.hashCode(3, 2, 1));
59     assertTrue(Objects.hashCode(1, 2, 3) != Objects.hashCode(2, 3, 1));
60   }
61 
62   @J2ktIncompatible
63   @GwtIncompatible // NullPointerTester
testNullPointers()64   public void testNullPointers() {
65     NullPointerTester tester = new NullPointerTester();
66     tester.testAllPublicStaticMethods(Objects.class);
67   }
68 }
69