Index: test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java =================================================================== --- test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java (revision 598833) +++ test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java (working copy) @@ -23,140 +23,177 @@ import org.apache.harmony.pack200.SegmentConstantPool; import org.apache.harmony.pack200.bytecode.ByteCode; import org.apache.harmony.pack200.bytecode.CodeAttribute; +import org.apache.harmony.pack200.bytecode.LocalVariableTableAttribute; import org.apache.harmony.pack200.bytecode.OperandManager; /** * Tests for CodeAttribute */ public class CodeAttributeTest extends TestCase { - - Segment segment = new MockSegment(); - CpBands cpBands = new MockCpBands(segment); - public class MockSegment extends Segment { - public SegmentConstantPool getConstantPool() { - return new MockSegmentConstantPool(cpBands); - } - } + Segment segment = new MockSegment(); + CpBands cpBands = new MockCpBands(segment); - public class MockSegmentConstantPool extends SegmentConstantPool { - public MockSegmentConstantPool(CpBands bands) { - super(bands); - } - protected int matchSpecificPoolEntryIndex(String[] nameArray, String compareString, int desiredIndex) { - return 1; - } - } - - public class MockCpBands extends CpBands { - public MockCpBands(Segment segment) { - super(segment); - } - - public String[] getCpString() { - return new String[]{"Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf"}; - } - - public String[] getCpMethodClass() { - return new String[]{"Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf"}; - } - - public String[] getCpMethodDescriptor() { - return new String[]{"Un:un", "Deux:deux", "Trois:trois"}; - } + public class MockSegment extends Segment { + public SegmentConstantPool getConstantPool() { + return new MockSegmentConstantPool(cpBands); + } + } - public String[] getCpFieldClass() { - return new String[]{"Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf"}; - } - - public String[] getCpFieldDescriptor() { - return new String[]{"Un:un", "Deux:deux", "Trois:trois"}; - } -} - - public class MockOperandManager extends OperandManager { + public class MockSegmentConstantPool extends SegmentConstantPool { + public MockSegmentConstantPool(CpBands bands) { + super(bands); + } - public MockOperandManager() { - super(new int[]{}, // bcByte - new int[]{}, // bcShort - new int[]{}, // bcLocal - new int[]{}, // bcLabel - new int[]{}, // bcIntRef - new int[]{}, // bcFloatRef - new int[]{}, // bcLongRef - new int[]{}, // bcDoubleRef - new int[]{0, 1, 2, 3, 4}, // bcStringRef - new int[]{}, // bcClassRef - new int[]{}, // bcFieldRef - new int[]{}, // bcMethodRef - new int[]{}, // bcIMethodRef - new int[]{0, 0, 0, 0, 0, 0}, // bcThisField - new int[]{}, // bcSuperField - new int[]{0}, // bcThisMethod - new int[]{}, // bcSuperMethod - new int[]{} // bcInitRef - ); - } - } - public byte[] singleByteArray = { - 42, // aload_0 0 - 1, // aconst_null 1 - 18, // ldc 2 - -49, // return 4 - }; - - public byte[] mixedByteArray = { - -47, // aload_0_getstatic_this 0, 1 - -46, // aload_0_putstatic_this 4, 5 - 1, // aconst_null 8 - -45, // aload_0_getfield_this 9, 10 - // Should always end with a multibyte - // instruction - -44, // aload_0_putfield_this (int) 13, 14 - }; - + protected int matchSpecificPoolEntryIndex(String[] nameArray, + String compareString, int desiredIndex) { + return 1; + } + } + + public class MockCodeAttribute extends CodeAttribute { + + public MockCodeAttribute(int maxStack, int maxLocals, + byte[] codePacked, Segment segment, + OperandManager operandManager) { + super(maxStack, maxLocals, codePacked, segment, operandManager); + // TODO Auto-generated constructor stub + } + + public int getLength() { + return super.getLength(); + } + } + + public class MockCpBands extends CpBands { + public MockCpBands(Segment segment) { + super(segment); + } + + public String[] getCpString() { + return new String[] { "Un", "Deux", "Trois", "Quatre", "Cinq", + "Six", "Sept", "Huit", "Neuf" }; + } + + public String[] getCpMethodClass() { + return new String[] { "Un", "Deux", "Trois", "Quatre", "Cinq", + "Six", "Sept", "Huit", "Neuf" }; + } + + public String[] getCpMethodDescriptor() { + return new String[] { "Un:un", "Deux:deux", "Trois:trois" }; + } + + public String[] getCpFieldClass() { + return new String[] { "Un", "Deux", "Trois", "Quatre", "Cinq", + "Six", "Sept", "Huit", "Neuf" }; + } + + public String[] getCpFieldDescriptor() { + return new String[] { "Un:un", "Deux:deux", "Trois:trois" }; + } + } + + public class MockOperandManager extends OperandManager { + + public MockOperandManager() { + super(new int[] {}, // bcByte + new int[] {}, // bcShort + new int[] {}, // bcLocal + new int[] {}, // bcLabel + new int[] {}, // bcIntRef + new int[] {}, // bcFloatRef + new int[] {}, // bcLongRef + new int[] {}, // bcDoubleRef + new int[] { 0, 1, 2, 3, 4 }, // bcStringRef + new int[] {}, // bcClassRef + new int[] {}, // bcFieldRef + new int[] {}, // bcMethodRef + new int[] {}, // bcIMethodRef + new int[] { 0, 0, 0, 0, 0, 0 }, // bcThisField + new int[] {}, // bcSuperField + new int[] { 0 }, // bcThisMethod + new int[] {}, // bcSuperMethod + new int[] {} // bcInitRef + ); + } + } + + public byte[] singleByteArray = { 42, // aload_0 0 + 1, // aconst_null 1 + 18, // ldc 2 + -49, // return 4 + }; + + public byte[] mixedByteArray = { -47, // aload_0_getstatic_this 0, 1 + -46, // aload_0_putstatic_this 4, 5 + 1, // aconst_null 8 + -45, // aload_0_getfield_this 9, 10 + // Should always end with a multibyte + // instruction + -44, // aload_0_putfield_this (int) 13, 14 + }; + public void testSingleByteCodes() { - OperandManager operandManager = new MockOperandManager(); - operandManager.setSegment(segment); - operandManager.setCurrentClass("java/lang/Foo"); - - CodeAttribute attribute = new CodeAttribute( - 4, // maxStack - 3, // maxLocals - singleByteArray, // codePacked - segment, // segment - operandManager // operandManager - ); - assertEquals(3, attribute.maxLocals); - assertEquals(4, attribute.maxStack); - assertEquals("invokespecial_this", ((ByteCode)attribute.byteCodes.get(3)).toString()); - - int expectedLabels[] = new int[] {0, 1, 2, 4}; - for(int index=0; index < expectedLabels.length; index++) { - assertEquals(expectedLabels[index], ((Integer)attribute.byteCodeOffsets.get(index)).intValue()); - } + OperandManager operandManager = new MockOperandManager(); + operandManager.setSegment(segment); + operandManager.setCurrentClass("java/lang/Foo"); + + CodeAttribute attribute = new CodeAttribute(4, // maxStack + 3, // maxLocals + singleByteArray, // codePacked + segment, // segment + operandManager // operandManager + ); + assertEquals(3, attribute.maxLocals); + assertEquals(4, attribute.maxStack); + assertEquals("invokespecial_this", ((ByteCode) attribute.byteCodes + .get(3)).toString()); + + int expectedLabels[] = new int[] { 0, 1, 2, 4 }; + for (int index = 0; index < expectedLabels.length; index++) { + assertEquals(expectedLabels[index], + ((Integer) attribute.byteCodeOffsets.get(index)).intValue()); + } } - + public void testMixedByteCodes() { - OperandManager operandManager = new MockOperandManager(); - operandManager.setSegment(segment); - operandManager.setCurrentClass("java/lang/Foo"); - - CodeAttribute attribute = new CodeAttribute( - 3, // maxStack - 2, // maxLocals - mixedByteArray, // codePacked - segment, // segment - operandManager // operandManager - ); - assertEquals(2, attribute.maxLocals); - assertEquals(3, attribute.maxStack); - assertEquals("aload_0_putfield_this", ((ByteCode)attribute.byteCodes.get(4)).toString()); - - int expectedLabels[] = new int[] {0, 1, 4, 5, 8, 9, 10, 13, 14}; - for(int index=0; index < expectedLabels.length; index++) { - assertEquals(expectedLabels[index], ((Integer)attribute.byteCodeOffsets.get(index)).intValue()); - } + OperandManager operandManager = new MockOperandManager(); + operandManager.setSegment(segment); + operandManager.setCurrentClass("java/lang/Foo"); + + CodeAttribute attribute = new CodeAttribute(3, // maxStack + 2, // maxLocals + mixedByteArray, // codePacked + segment, // segment + operandManager // operandManager + ); + assertEquals(2, attribute.maxLocals); + assertEquals(3, attribute.maxStack); + assertEquals("aload_0_putfield_this", ((ByteCode) attribute.byteCodes + .get(4)).toString()); + + int expectedLabels[] = new int[] { 0, 1, 4, 5, 8, 9, 10, 13, 14 }; + for (int index = 0; index < expectedLabels.length; index++) { + assertEquals(expectedLabels[index], + ((Integer) attribute.byteCodeOffsets.get(index)).intValue()); + } } - + + public void testLength() { + OperandManager operandManager = new MockOperandManager(); + operandManager.setSegment(segment); + operandManager.setCurrentClass("java/lang/Foo"); + + MockCodeAttribute attribute = new MockCodeAttribute(3, // maxStack + 2, // maxLocals + mixedByteArray, // codePacked + segment, // segment + operandManager // operandManager + ); + assertEquals(29, attribute.getLength()); + + attribute.attributes.add(new LocalVariableTableAttribute(0, null, null, null, null, null)); + assertEquals(37, attribute.getLength()); + } }