Index: test/java/org/apache/harmony/pack200/tests/BcBandsTest.java =================================================================== --- test/java/org/apache/harmony/pack200/tests/BcBandsTest.java (revision 598351) +++ test/java/org/apache/harmony/pack200/tests/BcBandsTest.java (working copy) @@ -31,6 +31,8 @@ import org.apache.harmony.pack200.Segment; import org.apache.harmony.pack200.SegmentConstantPool; import org.apache.harmony.pack200.SegmentHeader; +import org.apache.harmony.pack200.bytecode.Attribute; +import org.apache.harmony.pack200.bytecode.LineNumberTableAttribute; import junit.framework.TestCase; @@ -179,6 +181,32 @@ } return attributes; } + + public ArrayList getLineNumberAttributes() { + int totalMethods = 0; + for(int classIndex = 0; classIndex < numMethods.length; classIndex++) { + totalMethods = totalMethods + numMethods[classIndex]; + } + ArrayList[] codeAttributes = new ArrayList[totalMethods]; + for(int index = 0; index < codeAttributes.length; index++) { + codeAttributes[index] = new ArrayList(); + } + ArrayList lineNumberList = new ArrayList(); + for(int classIndex=0; classIndex < codeAttributes.length; classIndex++) { + boolean foundLineNumberTable = false; + for(int attributeIndex = 0; attributeIndex < codeAttributes[classIndex].size(); attributeIndex++) { + Attribute attribute = (Attribute)codeAttributes[classIndex].get(attributeIndex); + if(attribute.getClass() == LineNumberTableAttribute.class) { + foundLineNumberTable = true; + lineNumberList.add(attribute); + } + } + if(!foundLineNumberTable) { + lineNumberList.add(null); + } + } + return lineNumberList; + } } public class MockSegment extends AbstractBandsTestCase.MockSegment { Index: test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java =================================================================== --- test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java (revision 0) +++ test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java (revision 0) @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.harmony.pack200.tests; + +import junit.framework.TestCase; + +import org.apache.harmony.pack200.CpBands; +import org.apache.harmony.pack200.Segment; +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.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); + } + } + + 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 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()); + } + } + + 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()); + } + } + +} \ No newline at end of file