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.constantPool;
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.assertTrue;
11  
12  import mockit.asm.jvmConstants.ConstantPoolTypes;
13  
14  import org.junit.jupiter.api.Test;
15  
16  final class DynamicItemTest {
17  
18      @Test
19      void constructorWithIndex() {
20          DynamicItem item = new DynamicItem(1);
21          assertEquals(1, item.index);
22      }
23  
24      @Test
25      void copyConstructor() {
26          DynamicItem original = new DynamicItem(1);
27          original.set(ConstantPoolTypes.INVOKE_DYNAMIC, "myMethod", "(I)V", 3);
28  
29          DynamicItem copy = new DynamicItem(2, original);
30          assertEquals(2, copy.index);
31          assertEquals(3, copy.bsmIndex);
32          assertEquals("myMethod", copy.name);
33          assertEquals("(I)V", copy.desc);
34      }
35  
36      @Test
37      void setUpdatesAllFields() {
38          DynamicItem item = new DynamicItem(1);
39          item.set(ConstantPoolTypes.INVOKE_DYNAMIC, "method", "(Ljava/lang/String;)I", 5);
40  
41          assertEquals("method", item.name);
42          assertEquals("(Ljava/lang/String;)I", item.desc);
43          assertEquals(5, item.bsmIndex);
44      }
45  
46      @Test
47      void isEqualToWithSameValues() {
48          DynamicItem item1 = new DynamicItem(1);
49          item1.set(ConstantPoolTypes.INVOKE_DYNAMIC, "method", "(I)V", 2);
50  
51          DynamicItem item2 = new DynamicItem(3);
52          item2.set(ConstantPoolTypes.INVOKE_DYNAMIC, "method", "(I)V", 2);
53  
54          // Cast to Item to invoke DynamicItem.isEqualTo(Item)
55          assertTrue(item1.isEqualTo((Item) item2));
56      }
57  
58      @Test
59      void isEqualToWithDifferentBsmIndex() {
60          DynamicItem item1 = new DynamicItem(1);
61          item1.set(ConstantPoolTypes.INVOKE_DYNAMIC, "method", "(I)V", 2);
62  
63          DynamicItem item2 = new DynamicItem(3);
64          item2.set(ConstantPoolTypes.INVOKE_DYNAMIC, "method", "(I)V", 5);
65  
66          // Cast to Item to invoke DynamicItem.isEqualTo(Item) which checks bsmIndex
67          assertFalse(item1.isEqualTo((Item) item2));
68      }
69  
70      @Test
71      void isEqualToWithDifferentName() {
72          DynamicItem item1 = new DynamicItem(1);
73          item1.set(ConstantPoolTypes.INVOKE_DYNAMIC, "method1", "(I)V", 2);
74  
75          DynamicItem item2 = new DynamicItem(3);
76          item2.set(ConstantPoolTypes.INVOKE_DYNAMIC, "method2", "(I)V", 2);
77  
78          assertFalse(item1.isEqualTo((Item) item2));
79      }
80  }