1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| package com.jeantessier.classreader; |
34 |
| |
35 |
| import java.util.*; |
36 |
| |
37 |
| import junit.framework.*; |
38 |
| |
39 |
| public class TestInstructionWithDifferentConstantPool extends TestCase { |
40 |
| private ClassfileLoader oldLoader; |
41 |
| private ClassfileLoader newLoader; |
42 |
| |
43 |
3
| protected void setUp() throws Exception {
|
44 |
3
| super.setUp();
|
45 |
| |
46 |
3
| oldLoader = new AggregatingClassfileLoader();
|
47 |
3
| oldLoader.load("tests/JarJarDiff/old/ModifiedPackage/DifferentConstantPool.class");
|
48 |
| |
49 |
3
| newLoader = new AggregatingClassfileLoader();
|
50 |
3
| newLoader.load("tests/JarJarDiff/new/ModifiedPackage/DifferentConstantPool.class");
|
51 |
| } |
52 |
| |
53 |
1
| public void testSamePositionInChangedConstantPool() {
|
54 |
1
| Classfile oldClassfile = oldLoader.getClassfile("ModifiedPackage.DifferentConstantPool");
|
55 |
1
| Method_info oldMethod = oldClassfile.getMethod("DifferentConstantPool()");
|
56 |
1
| Code_attribute oldCode = oldMethod.getCode();
|
57 |
1
| byte[] oldBytecode = oldCode.getCode();
|
58 |
| |
59 |
1
| Classfile newClassfile = newLoader.getClassfile("ModifiedPackage.DifferentConstantPool");
|
60 |
1
| Method_info newMethod = newClassfile.getMethod("DifferentConstantPool()");
|
61 |
1
| Code_attribute newCode = newMethod.getCode();
|
62 |
1
| byte[] newBytecode = newCode.getCode();
|
63 |
| |
64 |
1
| assertEquals("Bytecode length", oldBytecode.length, newBytecode.length);
|
65 |
| |
66 |
1
| for (int i=0; i<oldBytecode.length; i++) {
|
67 |
5
| assertEquals("byte " + i, oldBytecode[i], newBytecode[i]);
|
68 |
| } |
69 |
| |
70 |
1
| Iterator<Instruction> oldIterator = oldCode.iterator();
|
71 |
1
| Iterator<Instruction> newIterator = newCode.iterator();
|
72 |
| |
73 |
1
| Instruction oldInstruction;
|
74 |
1
| Instruction newInstruction;
|
75 |
| |
76 |
| |
77 |
1
| oldInstruction = oldIterator.next();
|
78 |
1
| newInstruction = newIterator.next();
|
79 |
1
| assertEquals("aload_0", oldInstruction.getOpcode(), newInstruction.getOpcode());
|
80 |
1
| assertEquals("aload_0", oldInstruction, newInstruction);
|
81 |
1
| assertEquals("aload_0", oldInstruction.hashCode(), newInstruction.hashCode());
|
82 |
| |
83 |
| |
84 |
1
| oldInstruction = oldIterator.next();
|
85 |
1
| newInstruction = newIterator.next();
|
86 |
1
| assertEquals("invokespecial", oldInstruction.getOpcode(), newInstruction.getOpcode());
|
87 |
1
| assertEquals("invokespecial", oldInstruction, newInstruction);
|
88 |
1
| assertEquals("invokespecial", oldInstruction.hashCode(), newInstruction.hashCode());
|
89 |
| |
90 |
| |
91 |
1
| oldInstruction = oldIterator.next();
|
92 |
1
| newInstruction = newIterator.next();
|
93 |
1
| assertEquals("return", oldInstruction.getOpcode(), newInstruction.getOpcode());
|
94 |
1
| assertEquals("return", oldInstruction, newInstruction);
|
95 |
1
| assertEquals("return", oldInstruction.hashCode(), newInstruction.hashCode());
|
96 |
| |
97 |
| |
98 |
1
| assertFalse("Extra instructions", oldIterator.hasNext());
|
99 |
1
| assertFalse("Extra instructions", newIterator.hasNext());
|
100 |
| } |
101 |
| |
102 |
1
| public void testIndependantOfConstantPool() {
|
103 |
1
| Classfile oldClassfile = oldLoader.getClassfile("ModifiedPackage.DifferentConstantPool");
|
104 |
1
| Method_info oldMethod = oldClassfile.getMethod("movedMethodRefInfo()");
|
105 |
1
| Code_attribute oldCode = oldMethod.getCode();
|
106 |
1
| byte[] oldBytecode = oldCode.getCode();
|
107 |
| |
108 |
1
| Classfile newClassfile = newLoader.getClassfile("ModifiedPackage.DifferentConstantPool");
|
109 |
1
| Method_info newMethod = newClassfile.getMethod("movedMethodRefInfo()");
|
110 |
1
| Code_attribute newCode = newMethod.getCode();
|
111 |
1
| byte[] newBytecode = newCode.getCode();
|
112 |
| |
113 |
1
| assertEquals("Bytecode length", oldBytecode.length, newBytecode.length);
|
114 |
| |
115 |
1
| for (int i=0; i<oldBytecode.length; i++) {
|
116 |
1
| assertEquals("byte " + i, oldBytecode[i], newBytecode[i]);
|
117 |
| } |
118 |
| |
119 |
1
| Iterator<Instruction> oldIterator = oldCode.iterator();
|
120 |
1
| Iterator<Instruction> newIterator = newCode.iterator();
|
121 |
| |
122 |
1
| Instruction oldInstruction;
|
123 |
1
| Instruction newInstruction;
|
124 |
| |
125 |
| |
126 |
1
| oldInstruction = oldIterator.next();
|
127 |
1
| newInstruction = newIterator.next();
|
128 |
1
| assertEquals("return", oldInstruction.getOpcode(), newInstruction.getOpcode());
|
129 |
1
| assertEquals("return", oldInstruction, newInstruction);
|
130 |
1
| assertEquals("return", oldInstruction.hashCode(), newInstruction.hashCode());
|
131 |
| |
132 |
| |
133 |
1
| assertFalse("Extra instructions", oldIterator.hasNext());
|
134 |
1
| assertFalse("Extra instructions", newIterator.hasNext());
|
135 |
| } |
136 |
| |
137 |
1
| public void testShiftInChangedConstantPool() {
|
138 |
1
| Classfile oldClassfile = oldLoader.getClassfile("ModifiedPackage.DifferentConstantPool");
|
139 |
1
| Method_info oldMethod = oldClassfile.getMethod("callingMovedMethodRefInfo()");
|
140 |
1
| Code_attribute oldCode = oldMethod.getCode();
|
141 |
1
| byte[] oldBytecode = oldCode.getCode();
|
142 |
| |
143 |
1
| Classfile newClassfile = newLoader.getClassfile("ModifiedPackage.DifferentConstantPool");
|
144 |
1
| Method_info newMethod = newClassfile.getMethod("callingMovedMethodRefInfo()");
|
145 |
1
| Code_attribute newCode = newMethod.getCode();
|
146 |
1
| byte[] newBytecode = newCode.getCode();
|
147 |
| |
148 |
1
| assertEquals("Bytecode length", oldBytecode.length, newBytecode.length);
|
149 |
| |
150 |
1
| boolean same = oldBytecode.length == newBytecode.length;
|
151 |
1
| for (int i=0; same && i<oldBytecode.length; i++) {
|
152 |
4
| same = oldBytecode[i] == newBytecode[i];
|
153 |
| } |
154 |
1
| assertFalse("Bytes are identical", same);
|
155 |
| |
156 |
1
| Iterator<Instruction> oldIterator = oldCode.iterator();
|
157 |
1
| Iterator<Instruction> newIterator = newCode.iterator();
|
158 |
| |
159 |
1
| Instruction oldInstruction;
|
160 |
1
| Instruction newInstruction;
|
161 |
| |
162 |
| |
163 |
1
| oldInstruction = oldIterator.next();
|
164 |
1
| newInstruction = newIterator.next();
|
165 |
1
| assertEquals("aload_0", oldInstruction.getOpcode(), newInstruction.getOpcode());
|
166 |
1
| assertEquals("aload_0", oldInstruction, newInstruction);
|
167 |
1
| assertEquals("aload_0", oldInstruction.hashCode(), newInstruction.hashCode());
|
168 |
| |
169 |
| |
170 |
1
| oldInstruction = oldIterator.next();
|
171 |
1
| newInstruction = newIterator.next();
|
172 |
1
| assertEquals("invokevirtual", oldInstruction.getOpcode(), newInstruction.getOpcode());
|
173 |
1
| assertEquals("invokevirtual", oldInstruction, newInstruction);
|
174 |
1
| assertEquals("invokevirtual", oldInstruction.hashCode(), newInstruction.hashCode());
|
175 |
| |
176 |
| |
177 |
1
| oldInstruction = oldIterator.next();
|
178 |
1
| newInstruction = newIterator.next();
|
179 |
1
| assertEquals("return", oldInstruction.getOpcode(), newInstruction.getOpcode());
|
180 |
1
| assertEquals("return", oldInstruction, newInstruction);
|
181 |
1
| assertEquals("return", oldInstruction.hashCode(), newInstruction.hashCode());
|
182 |
| |
183 |
| |
184 |
1
| assertFalse("Extra instructions", oldIterator.hasNext());
|
185 |
1
| assertFalse("Extra instructions", newIterator.hasNext());
|
186 |
| } |
187 |
| } |