001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.util.*;
036:
037: import junit.framework.*;
038:
039: public class TestInstructionWithDifferentConstantPool extends TestCase {
040: ClassfileLoader oldLoader;
041: ClassfileLoader newLoader;
042:
043: protected void setUp() throws Exception {
044: super .setUp();
045:
046: oldLoader = new AggregatingClassfileLoader();
047: oldLoader
048: .load("tests/JarJarDiff/old/ModifiedPackage/DifferentConstantPool.class");
049:
050: newLoader = new AggregatingClassfileLoader();
051: newLoader
052: .load("tests/JarJarDiff/new/ModifiedPackage/DifferentConstantPool.class");
053: }
054:
055: public void testSamePositionInChangedConstantPool() {
056: Classfile oldClassfile = oldLoader
057: .getClassfile("ModifiedPackage.DifferentConstantPool");
058: Method_info oldMethod = oldClassfile
059: .getMethod("DifferentConstantPool()");
060: Code_attribute oldCode = oldMethod.getCode();
061: byte[] oldBytecode = oldCode.getCode();
062:
063: Classfile newClassfile = newLoader
064: .getClassfile("ModifiedPackage.DifferentConstantPool");
065: Method_info newMethod = newClassfile
066: .getMethod("DifferentConstantPool()");
067: Code_attribute newCode = newMethod.getCode();
068: byte[] newBytecode = newCode.getCode();
069:
070: assertEquals("Bytecode length", oldBytecode.length,
071: newBytecode.length);
072:
073: for (int i = 0; i < oldBytecode.length; i++) {
074: assertEquals("byte " + i, oldBytecode[i], newBytecode[i]);
075: }
076:
077: Iterator oldIterator = oldCode.iterator();
078: Iterator newIterator = newCode.iterator();
079:
080: Instruction oldInstruction;
081: Instruction newInstruction;
082:
083: // Instruction 0: aload_0
084: oldInstruction = (Instruction) oldIterator.next();
085: newInstruction = (Instruction) newIterator.next();
086: assertEquals("aload_0", oldInstruction.getOpcode(),
087: newInstruction.getOpcode());
088: assertEquals("aload_0", oldInstruction, newInstruction);
089: assertEquals("aload_0", oldInstruction.hashCode(),
090: newInstruction.hashCode());
091:
092: // Instruction 1: invokespecial
093: oldInstruction = (Instruction) oldIterator.next();
094: newInstruction = (Instruction) newIterator.next();
095: assertEquals("invokespecial", oldInstruction.getOpcode(),
096: newInstruction.getOpcode());
097: assertEquals("invokespecial", oldInstruction, newInstruction);
098: assertEquals("invokespecial", oldInstruction.hashCode(),
099: newInstruction.hashCode());
100:
101: // Instruction 2: return
102: oldInstruction = (Instruction) oldIterator.next();
103: newInstruction = (Instruction) newIterator.next();
104: assertEquals("return", oldInstruction.getOpcode(),
105: newInstruction.getOpcode());
106: assertEquals("return", oldInstruction, newInstruction);
107: assertEquals("return", oldInstruction.hashCode(),
108: newInstruction.hashCode());
109:
110: // The end
111: assertFalse("Extra instructions", oldIterator.hasNext());
112: assertFalse("Extra instructions", newIterator.hasNext());
113: }
114:
115: public void testIndependantOfConstantPool() {
116: Classfile oldClassfile = oldLoader
117: .getClassfile("ModifiedPackage.DifferentConstantPool");
118: Method_info oldMethod = oldClassfile
119: .getMethod("movedMethodRefInfo()");
120: Code_attribute oldCode = oldMethod.getCode();
121: byte[] oldBytecode = oldCode.getCode();
122:
123: Classfile newClassfile = newLoader
124: .getClassfile("ModifiedPackage.DifferentConstantPool");
125: Method_info newMethod = newClassfile
126: .getMethod("movedMethodRefInfo()");
127: Code_attribute newCode = newMethod.getCode();
128: byte[] newBytecode = newCode.getCode();
129:
130: assertEquals("Bytecode length", oldBytecode.length,
131: newBytecode.length);
132:
133: for (int i = 0; i < oldBytecode.length; i++) {
134: assertEquals("byte " + i, oldBytecode[i], newBytecode[i]);
135: }
136:
137: Iterator oldIterator = oldCode.iterator();
138: Iterator newIterator = newCode.iterator();
139:
140: Instruction oldInstruction;
141: Instruction newInstruction;
142:
143: // Instruction 0: return
144: oldInstruction = (Instruction) oldIterator.next();
145: newInstruction = (Instruction) newIterator.next();
146: assertEquals("return", oldInstruction.getOpcode(),
147: newInstruction.getOpcode());
148: assertEquals("return", oldInstruction, newInstruction);
149: assertEquals("return", oldInstruction.hashCode(),
150: newInstruction.hashCode());
151:
152: // The end
153: assertFalse("Extra instructions", oldIterator.hasNext());
154: assertFalse("Extra instructions", newIterator.hasNext());
155: }
156:
157: public void testShiftInChangedConstantPool() {
158: Classfile oldClassfile = oldLoader
159: .getClassfile("ModifiedPackage.DifferentConstantPool");
160: Method_info oldMethod = oldClassfile
161: .getMethod("callingMovedMethodRefInfo()");
162: Code_attribute oldCode = oldMethod.getCode();
163: byte[] oldBytecode = oldCode.getCode();
164:
165: Classfile newClassfile = newLoader
166: .getClassfile("ModifiedPackage.DifferentConstantPool");
167: Method_info newMethod = newClassfile
168: .getMethod("callingMovedMethodRefInfo()");
169: Code_attribute newCode = newMethod.getCode();
170: byte[] newBytecode = newCode.getCode();
171:
172: assertEquals("Bytecode length", oldBytecode.length,
173: newBytecode.length);
174:
175: boolean same = oldBytecode.length == newBytecode.length;
176: for (int i = 0; same && i < oldBytecode.length; i++) {
177: same = oldBytecode[i] == newBytecode[i];
178: }
179: assertFalse("Bytes are identical", same);
180:
181: Iterator oldIterator = oldCode.iterator();
182: Iterator newIterator = newCode.iterator();
183:
184: Instruction oldInstruction;
185: Instruction newInstruction;
186:
187: // Instruction 0: aload_0
188: oldInstruction = (Instruction) oldIterator.next();
189: newInstruction = (Instruction) newIterator.next();
190: assertEquals("aload_0", oldInstruction.getOpcode(),
191: newInstruction.getOpcode());
192: assertEquals("aload_0", oldInstruction, newInstruction);
193: assertEquals("aload_0", oldInstruction.hashCode(),
194: newInstruction.hashCode());
195:
196: // Instruction 1: invokevirtual
197: oldInstruction = (Instruction) oldIterator.next();
198: newInstruction = (Instruction) newIterator.next();
199: assertEquals("invokevirtual", oldInstruction.getOpcode(),
200: newInstruction.getOpcode());
201: assertEquals("invokevirtual", oldInstruction, newInstruction);
202: assertEquals("invokevirtual", oldInstruction.hashCode(),
203: newInstruction.hashCode());
204:
205: // Instruction 2: return
206: oldInstruction = (Instruction) oldIterator.next();
207: newInstruction = (Instruction) newIterator.next();
208: assertEquals("return", oldInstruction.getOpcode(),
209: newInstruction.getOpcode());
210: assertEquals("return", oldInstruction, newInstruction);
211: assertEquals("return", oldInstruction.hashCode(),
212: newInstruction.hashCode());
213:
214: // The end
215: assertFalse("Extra instructions", oldIterator.hasNext());
216: assertFalse("Extra instructions", newIterator.hasNext());
217: }
218: }
|