View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package mockit.asm.types;
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.assertNotNull;
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 ReferenceTypeTest {
18  
19      @Test
20      void createFromTypeDescriptorForObjectType() {
21          ReferenceType type = ReferenceType.createFromTypeDescriptor("Ljava/lang/String;");
22          assertNotNull(type);
23          assertTrue(type instanceof ObjectType);
24          assertEquals("java/lang/String", type.getInternalName());
25      }
26  
27      @Test
28      void createFromTypeDescriptorForArrayType() {
29          ReferenceType type = ReferenceType.createFromTypeDescriptor("[Ljava/lang/String;");
30          assertNotNull(type);
31          assertTrue(type instanceof ArrayType);
32      }
33  
34      @Test
35      void createFromTypeDescriptorForMethodType() {
36          ReferenceType type = ReferenceType.createFromTypeDescriptor("(Ljava/lang/String;)V");
37          assertNotNull(type);
38          assertTrue(type instanceof MethodType);
39      }
40  
41      @Test
42      void createFromTypeDescriptorInvalidThrows() {
43          assertThrows(IllegalArgumentException.class, () -> ReferenceType.createFromTypeDescriptor("InvalidDescriptor"));
44      }
45  
46      @Test
47      void createFromInternalNameForObjectType() {
48          ReferenceType type = ReferenceType.createFromInternalName("java/lang/String");
49          assertNotNull(type);
50          assertTrue(type instanceof ObjectType);
51          assertEquals("java/lang/String", type.getInternalName());
52      }
53  
54      @Test
55      void createFromInternalNameForArrayType() {
56          ReferenceType type = ReferenceType.createFromInternalName("[Ljava/lang/String;");
57          assertNotNull(type);
58          assertTrue(type instanceof ArrayType);
59      }
60  
61      @Test
62      void equalsSameInstance() {
63          ObjectType type = ObjectType.create("java/lang/String");
64          assertEquals(type, type);
65      }
66  
67      @Test
68      void equalsNullReturnsFalse() {
69          ObjectType type = ObjectType.create("java/lang/String");
70          assertNotEquals(null, type);
71      }
72  
73      @Test
74      void equalsNonReferenceTypeReturnsFalse() {
75          ObjectType type = ObjectType.create("java/lang/String");
76          assertFalse(type.equals("not a ReferenceType"));
77      }
78  
79      @Test
80      void equalsSameTypeDescriptor() {
81          ObjectType type1 = ObjectType.create("java/lang/String");
82          ObjectType type2 = ObjectType.create("java/lang/String");
83          assertEquals(type1, type2);
84      }
85  
86      @Test
87      void equalsDifferentTypeDescriptor() {
88          ObjectType type1 = ObjectType.create("java/lang/String");
89          ObjectType type2 = ObjectType.create("java/lang/Integer");
90          assertNotEquals(type1, type2);
91      }
92  
93      @Test
94      void equalsDifferentClass() {
95          ReferenceType objType = ReferenceType.createFromInternalName("java/lang/String");
96          ReferenceType arrType = ReferenceType.createFromInternalName("[Ljava/lang/String;");
97          assertNotEquals(objType, arrType);
98      }
99  
100     @Test
101     void equalsDifferentLength() {
102         ObjectType type1 = ObjectType.create("java/lang/String");
103         ObjectType type2 = ObjectType.create("java/lang/Integer");
104         assertNotEquals(type1, type2);
105     }
106 
107     @Test
108     void hashCodeConsistentWithEquals() {
109         ObjectType type1 = ObjectType.create("java/lang/String");
110         ObjectType type2 = ObjectType.create("java/lang/String");
111         assertEquals(type1, type2);
112         assertEquals(type1.hashCode(), type2.hashCode());
113     }
114 
115     @Test
116     void hashCodeDifferentForDifferentTypes() {
117         ObjectType type1 = ObjectType.create("java/lang/String");
118         ObjectType type2 = ObjectType.create("java/lang/Integer");
119         assertNotEquals(type1.hashCode(), type2.hashCode());
120     }
121 
122     @Test
123     void getInternalName() {
124         String internalName = "java/util/List";
125         ReferenceType type = ReferenceType.createFromInternalName(internalName);
126         assertEquals(internalName, type.getInternalName());
127     }
128 
129     @Test
130     void getSize() {
131         ObjectType type = ObjectType.create("java/lang/String");
132         assertEquals(1, type.getSize());
133     }
134 }