1 | /* |
2 | * Copyright (c) 2001-2009, Jean Tessier |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * |
12 | * * Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * * Neither the name of Jean Tessier nor the names of his contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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 | protected void setUp() throws Exception { |
44 | super.setUp(); |
45 | |
46 | oldLoader = new AggregatingClassfileLoader(); |
47 | oldLoader.load("tests/JarJarDiff/old/ModifiedPackage/DifferentConstantPool.class"); |
48 | |
49 | newLoader = new AggregatingClassfileLoader(); |
50 | newLoader.load("tests/JarJarDiff/new/ModifiedPackage/DifferentConstantPool.class"); |
51 | } |
52 | |
53 | public void testSamePositionInChangedConstantPool() { |
54 | Classfile oldClassfile = oldLoader.getClassfile("ModifiedPackage.DifferentConstantPool"); |
55 | Method_info oldMethod = oldClassfile.getMethod("DifferentConstantPool()"); |
56 | Code_attribute oldCode = oldMethod.getCode(); |
57 | byte[] oldBytecode = oldCode.getCode(); |
58 | |
59 | Classfile newClassfile = newLoader.getClassfile("ModifiedPackage.DifferentConstantPool"); |
60 | Method_info newMethod = newClassfile.getMethod("DifferentConstantPool()"); |
61 | Code_attribute newCode = newMethod.getCode(); |
62 | byte[] newBytecode = newCode.getCode(); |
63 | |
64 | assertEquals("Bytecode length", oldBytecode.length, newBytecode.length); |
65 | |
66 | for (int i=0; i<oldBytecode.length; i++) { |
67 | assertEquals("byte " + i, oldBytecode[i], newBytecode[i]); |
68 | } |
69 | |
70 | Iterator<Instruction> oldIterator = oldCode.iterator(); |
71 | Iterator<Instruction> newIterator = newCode.iterator(); |
72 | |
73 | Instruction oldInstruction; |
74 | Instruction newInstruction; |
75 | |
76 | // Instruction 0: aload_0 |
77 | oldInstruction = oldIterator.next(); |
78 | newInstruction = newIterator.next(); |
79 | assertEquals("aload_0", oldInstruction.getOpcode(), newInstruction.getOpcode()); |
80 | assertEquals("aload_0", oldInstruction, newInstruction); |
81 | assertEquals("aload_0", oldInstruction.hashCode(), newInstruction.hashCode()); |
82 | |
83 | // Instruction 1: invokespecial |
84 | oldInstruction = oldIterator.next(); |
85 | newInstruction = newIterator.next(); |
86 | assertEquals("invokespecial", oldInstruction.getOpcode(), newInstruction.getOpcode()); |
87 | assertEquals("invokespecial", oldInstruction, newInstruction); |
88 | assertEquals("invokespecial", oldInstruction.hashCode(), newInstruction.hashCode()); |
89 | |
90 | // Instruction 2: return |
91 | oldInstruction = oldIterator.next(); |
92 | newInstruction = newIterator.next(); |
93 | assertEquals("return", oldInstruction.getOpcode(), newInstruction.getOpcode()); |
94 | assertEquals("return", oldInstruction, newInstruction); |
95 | assertEquals("return", oldInstruction.hashCode(), newInstruction.hashCode()); |
96 | |
97 | // The end |
98 | assertFalse("Extra instructions", oldIterator.hasNext()); |
99 | assertFalse("Extra instructions", newIterator.hasNext()); |
100 | } |
101 | |
102 | public void testIndependantOfConstantPool() { |
103 | Classfile oldClassfile = oldLoader.getClassfile("ModifiedPackage.DifferentConstantPool"); |
104 | Method_info oldMethod = oldClassfile.getMethod("movedMethodRefInfo()"); |
105 | Code_attribute oldCode = oldMethod.getCode(); |
106 | byte[] oldBytecode = oldCode.getCode(); |
107 | |
108 | Classfile newClassfile = newLoader.getClassfile("ModifiedPackage.DifferentConstantPool"); |
109 | Method_info newMethod = newClassfile.getMethod("movedMethodRefInfo()"); |
110 | Code_attribute newCode = newMethod.getCode(); |
111 | byte[] newBytecode = newCode.getCode(); |
112 | |
113 | assertEquals("Bytecode length", oldBytecode.length, newBytecode.length); |
114 | |
115 | for (int i=0; i<oldBytecode.length; i++) { |
116 | assertEquals("byte " + i, oldBytecode[i], newBytecode[i]); |
117 | } |
118 | |
119 | Iterator<Instruction> oldIterator = oldCode.iterator(); |
120 | Iterator<Instruction> newIterator = newCode.iterator(); |
121 | |
122 | Instruction oldInstruction; |
123 | Instruction newInstruction; |
124 | |
125 | // Instruction 0: return |
126 | oldInstruction = oldIterator.next(); |
127 | newInstruction = newIterator.next(); |
128 | assertEquals("return", oldInstruction.getOpcode(), newInstruction.getOpcode()); |
129 | assertEquals("return", oldInstruction, newInstruction); |
130 | assertEquals("return", oldInstruction.hashCode(), newInstruction.hashCode()); |
131 | |
132 | // The end |
133 | assertFalse("Extra instructions", oldIterator.hasNext()); |
134 | assertFalse("Extra instructions", newIterator.hasNext()); |
135 | } |
136 | |
137 | public void testShiftInChangedConstantPool() { |
138 | Classfile oldClassfile = oldLoader.getClassfile("ModifiedPackage.DifferentConstantPool"); |
139 | Method_info oldMethod = oldClassfile.getMethod("callingMovedMethodRefInfo()"); |
140 | Code_attribute oldCode = oldMethod.getCode(); |
141 | byte[] oldBytecode = oldCode.getCode(); |
142 | |
143 | Classfile newClassfile = newLoader.getClassfile("ModifiedPackage.DifferentConstantPool"); |
144 | Method_info newMethod = newClassfile.getMethod("callingMovedMethodRefInfo()"); |
145 | Code_attribute newCode = newMethod.getCode(); |
146 | byte[] newBytecode = newCode.getCode(); |
147 | |
148 | assertEquals("Bytecode length", oldBytecode.length, newBytecode.length); |
149 | |
150 | boolean same = oldBytecode.length == newBytecode.length; |
151 | for (int i=0; same && i<oldBytecode.length; i++) { |
152 | same = oldBytecode[i] == newBytecode[i]; |
153 | } |
154 | assertFalse("Bytes are identical", same); |
155 | |
156 | Iterator<Instruction> oldIterator = oldCode.iterator(); |
157 | Iterator<Instruction> newIterator = newCode.iterator(); |
158 | |
159 | Instruction oldInstruction; |
160 | Instruction newInstruction; |
161 | |
162 | // Instruction 0: aload_0 |
163 | oldInstruction = oldIterator.next(); |
164 | newInstruction = newIterator.next(); |
165 | assertEquals("aload_0", oldInstruction.getOpcode(), newInstruction.getOpcode()); |
166 | assertEquals("aload_0", oldInstruction, newInstruction); |
167 | assertEquals("aload_0", oldInstruction.hashCode(), newInstruction.hashCode()); |
168 | |
169 | // Instruction 1: invokevirtual |
170 | oldInstruction = oldIterator.next(); |
171 | newInstruction = newIterator.next(); |
172 | assertEquals("invokevirtual", oldInstruction.getOpcode(), newInstruction.getOpcode()); |
173 | assertEquals("invokevirtual", oldInstruction, newInstruction); |
174 | assertEquals("invokevirtual", oldInstruction.hashCode(), newInstruction.hashCode()); |
175 | |
176 | // Instruction 2: return |
177 | oldInstruction = oldIterator.next(); |
178 | newInstruction = newIterator.next(); |
179 | assertEquals("return", oldInstruction.getOpcode(), newInstruction.getOpcode()); |
180 | assertEquals("return", oldInstruction, newInstruction); |
181 | assertEquals("return", oldInstruction.hashCode(), newInstruction.hashCode()); |
182 | |
183 | // The end |
184 | assertFalse("Extra instructions", oldIterator.hasNext()); |
185 | assertFalse("Extra instructions", newIterator.hasNext()); |
186 | } |
187 | } |