1
2
3
4
5
6 package mockit.internal;
7
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertNotEquals;
11 import static org.junit.jupiter.api.Assertions.assertSame;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
13 import static org.junit.jupiter.api.Assertions.assertTrue;
14
15 import org.junit.jupiter.api.Test;
16
17 final class ClassIdentificationTest {
18
19 @Test
20 void getLoadedClassReturnsCorrectClass() {
21 ClassIdentification id = new ClassIdentification(ClassIdentificationTest.class.getClassLoader(),
22 "java.lang.String");
23 Class<?> loaded = id.getLoadedClass();
24 assertSame(String.class, loaded);
25 }
26
27 @Test
28 void getLoadedClassWithNullLoaderUsesBootstrap() {
29 ClassIdentification id = new ClassIdentification(null, "java.lang.Integer");
30 Class<?> loaded = id.getLoadedClass();
31 assertSame(Integer.class, loaded);
32 }
33
34 @Test
35 void getLoadedClassThrowsForUnknownClass() {
36 ClassIdentification id = new ClassIdentification(ClassIdentificationTest.class.getClassLoader(),
37 "com.example.DoesNotExist");
38 assertThrows(RuntimeException.class, id::getLoadedClass);
39 }
40
41 @Test
42 void equalsSameInstance() {
43 ClassIdentification id = new ClassIdentification(null, "java.lang.String");
44 assertEquals(id, id);
45 }
46
47 @Test
48 void equalsNullReturnsFalse() {
49 ClassIdentification id = new ClassIdentification(null, "java.lang.String");
50 assertNotEquals(null, id);
51 }
52
53 @Test
54 void equalsDifferentClassReturnsFalse() {
55 ClassIdentification id = new ClassIdentification(null, "java.lang.String");
56 assertFalse(id.equals("some string"));
57 }
58
59 @Test
60 void equalsWithSameLoaderAndName() {
61 ClassLoader cl = ClassIdentificationTest.class.getClassLoader();
62 ClassIdentification id1 = new ClassIdentification(cl, "java.lang.String");
63 ClassIdentification id2 = new ClassIdentification(cl, "java.lang.String");
64 assertEquals(id1, id2);
65 }
66
67 @Test
68 void equalsWithDifferentName() {
69 ClassLoader cl = ClassIdentificationTest.class.getClassLoader();
70 ClassIdentification id1 = new ClassIdentification(cl, "java.lang.String");
71 ClassIdentification id2 = new ClassIdentification(cl, "java.lang.Integer");
72 assertNotEquals(id1, id2);
73 }
74
75 @Test
76 void equalsWithDifferentLoader() {
77
78 ClassLoader appCl = ClassIdentificationTest.class.getClassLoader();
79 ClassIdentification id1 = new ClassIdentification(null, "java.lang.String");
80 ClassIdentification id2 = new ClassIdentification(appCl, "java.lang.String");
81
82 assertNotEquals(id1, id2);
83 }
84
85 @Test
86 void equalsWithBothNullLoaders() {
87 ClassIdentification id1 = new ClassIdentification(null, "java.lang.String");
88 ClassIdentification id2 = new ClassIdentification(null, "java.lang.String");
89 assertEquals(id1, id2);
90 }
91
92 @Test
93 void hashCodeWithNullLoader() {
94 ClassIdentification id = new ClassIdentification(null, "java.lang.String");
95 assertEquals("java.lang.String".hashCode(), id.hashCode());
96 }
97
98 @Test
99 void hashCodeWithNonNullLoader() {
100 ClassLoader cl = ClassIdentificationTest.class.getClassLoader();
101 ClassIdentification id = new ClassIdentification(cl, "java.lang.String");
102 int expected = 31 * cl.hashCode() + "java.lang.String".hashCode();
103 assertEquals(expected, id.hashCode());
104 }
105
106 @Test
107 void equalInstancesHaveSameHashCode() {
108 ClassLoader cl = ClassIdentificationTest.class.getClassLoader();
109 ClassIdentification id1 = new ClassIdentification(cl, "java.lang.String");
110 ClassIdentification id2 = new ClassIdentification(cl, "java.lang.String");
111 assertTrue(id1.equals(id2));
112 assertEquals(id1.hashCode(), id2.hashCode());
113 }
114 }