001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.pack200.tests;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.harmony.pack200.CpBands;
025: import org.apache.harmony.pack200.Segment;
026: import org.apache.harmony.pack200.SegmentConstantPool;
027: import org.apache.harmony.pack200.bytecode.ByteCode;
028: import org.apache.harmony.pack200.bytecode.CodeAttribute;
029: import org.apache.harmony.pack200.bytecode.LocalVariableTableAttribute;
030: import org.apache.harmony.pack200.bytecode.OperandManager;
031:
032: /**
033: * Tests for CodeAttribute
034: */
035: public class CodeAttributeTest extends TestCase {
036:
037: public class MockCodeAttribute extends CodeAttribute {
038:
039: public MockCodeAttribute(int maxStack, int maxLocals,
040: byte[] codePacked, Segment segment,
041: OperandManager operandManager, List exceptionTable) {
042: super (maxStack, maxLocals, codePacked, segment,
043: operandManager, exceptionTable);
044: }
045:
046: public int getLength() {
047: return super .getLength();
048: }
049: }
050:
051: public class MockCpBands extends CpBands {
052: public MockCpBands(Segment segment) {
053: super (segment);
054: }
055:
056: public String[] getCpFieldClass() {
057: return new String[] { "Un", "Deux", "Trois", "Quatre",
058: "Cinq", "Six", "Sept", "Huit", "Neuf" };
059: }
060:
061: public String[] getCpFieldDescriptor() {
062: return new String[] { "Un:un", "Deux:deux", "Trois:trois" };
063: }
064:
065: public String[] getCpMethodClass() {
066: return new String[] { "Un", "Deux", "Trois", "Quatre",
067: "Cinq", "Six", "Sept", "Huit", "Neuf" };
068: }
069:
070: public String[] getCpMethodDescriptor() {
071: return new String[] { "Un:un", "Deux:deux", "Trois:trois" };
072: }
073:
074: public String[] getCpString() {
075: return new String[] { "Un", "Deux", "Trois", "Quatre",
076: "Cinq", "Six", "Sept", "Huit", "Neuf" };
077: }
078: }
079:
080: public class MockOperandManager extends OperandManager {
081:
082: public MockOperandManager() {
083: super (new int[] {}, // bcCaseCount
084: new int[] {}, // bcCaseValues
085: new int[] {}, // bcByte
086: new int[] {}, // bcShort
087: new int[] {}, // bcLocal
088: new int[] {}, // bcLabel
089: new int[] {}, // bcIntRef
090: new int[] {}, // bcFloatRef
091: new int[] {}, // bcLongRef
092: new int[] {}, // bcDoubleRef
093: new int[] { 0, 1, 2, 3, 4 }, // bcStringRef
094: new int[] {}, // bcClassRef
095: new int[] {}, // bcFieldRef
096: new int[] {}, // bcMethodRef
097: new int[] {}, // bcIMethodRef
098: new int[] { 0, 0, 0, 0, 0, 0 }, // bcThisField
099: new int[] {}, // bcSuperField
100: new int[] { 0 }, // bcThisMethod
101: new int[] {}, // bcSuperMethod
102: new int[] {} // bcInitRef
103: , null);
104: }
105: }
106:
107: public class MockSegment extends Segment {
108: public SegmentConstantPool getConstantPool() {
109: return new MockSegmentConstantPool(cpBands);
110: }
111: }
112:
113: public class MockSegmentConstantPool extends SegmentConstantPool {
114: public MockSegmentConstantPool(CpBands bands) {
115: super (bands);
116: }
117:
118: protected int matchSpecificPoolEntryIndex(String[] nameArray,
119: String compareString, int desiredIndex) {
120: return 1;
121: }
122: }
123:
124: Segment segment = new MockSegment();
125: CpBands cpBands = new MockCpBands(segment);
126:
127: public byte[] mixedByteArray = { -47, // aload_0_getstatic_this 0, 1
128: -46, // aload_0_putstatic_this 4, 5
129: 1, // aconst_null 8
130: -45, // aload_0_getfield_this 9, 10
131: // Should always end with a multibyte
132: // instruction
133: -44, // aload_0_putfield_this (int) 13, 14
134: };
135:
136: public byte[] singleByteArray = { 42, // aload_0 0
137: 1, // aconst_null 1
138: 18, // ldc 2
139: -49, // return 4
140: };
141:
142: public void testLength() {
143: OperandManager operandManager = new MockOperandManager();
144: operandManager.setSegment(segment);
145: operandManager.setCurrentClass("java/lang/Foo");
146:
147: MockCodeAttribute attribute = new MockCodeAttribute(3, // maxStack
148: 2, // maxLocals
149: mixedByteArray, // codePacked
150: segment, // segment
151: operandManager, // operandManager
152: new ArrayList());
153: assertEquals(29, attribute.getLength());
154:
155: attribute.attributes.add(new LocalVariableTableAttribute(0,
156: null, null, null, null, null));
157: assertEquals(37, attribute.getLength());
158: }
159:
160: public void testMixedByteCodes() {
161: OperandManager operandManager = new MockOperandManager();
162: operandManager.setSegment(segment);
163: operandManager.setCurrentClass("java/lang/Foo");
164:
165: CodeAttribute attribute = new CodeAttribute(3, // maxStack
166: 2, // maxLocals
167: mixedByteArray, // codePacked
168: segment, // segment
169: operandManager, // operandManager
170: new ArrayList());
171: assertEquals(2, attribute.maxLocals);
172: assertEquals(3, attribute.maxStack);
173: assertEquals("aload_0_putfield_this",
174: ((ByteCode) attribute.byteCodes.get(4)).toString());
175:
176: int expectedLabels[] = new int[] { 0, 1, 4, 5, 8, 9, 10, 13, 14 };
177: for (int index = 0; index < expectedLabels.length; index++) {
178: assertEquals(expectedLabels[index],
179: ((Integer) attribute.byteCodeOffsets.get(index))
180: .intValue());
181: }
182: }
183:
184: public void testSingleByteCodes() {
185: OperandManager operandManager = new MockOperandManager();
186: operandManager.setSegment(segment);
187: operandManager.setCurrentClass("java/lang/Foo");
188:
189: CodeAttribute attribute = new CodeAttribute(4, // maxStack
190: 3, // maxLocals
191: singleByteArray, // codePacked
192: segment, // segment
193: operandManager, // operandManager
194: new ArrayList());
195: assertEquals(3, attribute.maxLocals);
196: assertEquals(4, attribute.maxStack);
197: assertEquals("invokespecial_this",
198: ((ByteCode) attribute.byteCodes.get(3)).toString());
199:
200: int expectedLabels[] = new int[] { 0, 1, 2, 4 };
201: for (int index = 0; index < expectedLabels.length; index++) {
202: assertEquals(expectedLabels[index],
203: ((Integer) attribute.byteCodeOffsets.get(index))
204: .intValue());
205: }
206: }
207:
208: }
|