View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertFalse;
15  import static org.junit.jupiter.api.Assertions.assertNotEquals;
16  
17  import org.junit.jupiter.api.Test;
18  
19  /**
20   * Tests for {@link ObjectWrapper}.
21   */
22  class ObjectWrapperTest {
23  
24    @Test
25    void testEqualsWithSameObject() {
26      Object obj = new Object();
27      ObjectWrapper w1 = new ObjectWrapper(obj);
28      ObjectWrapper w2 = new ObjectWrapper(obj);
29      assertEquals(w1, w2);
30    }
31  
32    @Test
33    void testEqualsWithDifferentObject() {
34      ObjectWrapper w1 = new ObjectWrapper(new Object());
35      ObjectWrapper w2 = new ObjectWrapper(new Object());
36      assertNotEquals(w1, w2);
37    }
38  
39    @Test
40    void testEqualsWithNullWrapped() {
41      ObjectWrapper w1 = new ObjectWrapper(null);
42      // When wrapped object is null, equals(o1) returns o1 == null (not the wrapper)
43      // So two ObjectWrapper(null) instances are NOT equal to each other
44      assertNotEquals(w1, new ObjectWrapper(null));
45      // But null itself does equal the wrapper wrapping null
46      assertEquals(w1, null); // equals(null) when wrappedObject==null returns true
47    }
48  
49    @Test
50    void testEqualsNullWrappedVsNonNull() {
51      ObjectWrapper w1 = new ObjectWrapper(null);
52      ObjectWrapper w2 = new ObjectWrapper(new Object());
53      // null vs non-null: o1 != null, so returns false
54      assertNotEquals(w1, w2);
55    }
56  
57    @Test
58    void testEqualsDifferentClass() {
59      ObjectWrapper w = new ObjectWrapper(new Object());
60      assertFalse(w.equals("a string"));
61    }
62  
63    @Test
64    void testHashCodeWithSameInstance() {
65      Object obj = new Object();
66      ObjectWrapper w1 = new ObjectWrapper(obj);
67      ObjectWrapper w2 = new ObjectWrapper(obj);
68      assertEquals(w1.hashCode(), w2.hashCode());
69    }
70  
71    @Test
72    void testHashCodeWithNull() {
73      ObjectWrapper w = new ObjectWrapper(null);
74      assertEquals(System.identityHashCode(null), w.hashCode());
75    }
76  
77    @Test
78    void testHashCodeDifferentInstances() {
79      Object obj1 = new Object();
80      Object obj2 = new Object();
81      ObjectWrapper w1 = new ObjectWrapper(obj1);
82      ObjectWrapper w2 = new ObjectWrapper(obj2);
83      // Different object instances may have different hashCodes
84      // (not guaranteed but likely)
85      assertNotEquals(0, w1.hashCode()); // just verify it returns something
86      assertNotEquals(0, w2.hashCode());
87    }
88  }