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.io.ByteArrayInputStream;
020: import java.io.IOException;
021:
022: import org.apache.harmony.pack200.ClassBands;
023: import org.apache.harmony.pack200.Codec;
024: import org.apache.harmony.pack200.CpBands;
025: import org.apache.harmony.pack200.Pack200Exception;
026: import org.apache.harmony.pack200.Segment;
027:
028: /**
029: *
030: */
031: public class ClassBandsTest extends AbstractBandsTestCase {
032:
033: private String[] cpClasses;
034: private String[] cpDescriptor;
035: private String[] cpUTF8;
036:
037: public class MockCpBands extends CpBands {
038:
039: public MockCpBands(Segment segment) {
040: super (segment);
041: }
042:
043: public String[] getCpClass() {
044: return cpClasses;
045: }
046:
047: public String[] getCpDescriptor() {
048: return cpDescriptor;
049: }
050:
051: public String[] getCpUTF8() {
052: return cpUTF8;
053: }
054:
055: public int[] getCpInt() {
056: return new int[0];
057: }
058:
059: public double[] getCpDouble() {
060: return new double[0];
061: }
062:
063: public long[] getCpLong() {
064: return new long[0];
065: }
066:
067: public float[] getCpFloat() {
068: return new float[0];
069: }
070:
071: public String[] getCpSignature() {
072: return new String[0];
073: }
074: }
075:
076: public class MockSegment extends AbstractBandsTestCase.MockSegment {
077: protected CpBands getCpBands() {
078: return new MockCpBands(this );
079: }
080: }
081:
082: ClassBands classBands = new ClassBands(new MockSegment());
083:
084: public void testSimple() throws IOException, Pack200Exception {
085: cpClasses = new String[] { "Class1", "Class2", "Class3",
086: "Interface1", "Interface2" };
087: cpDescriptor = new String[0];
088: cpUTF8 = new String[0];
089: byte[] classThis = Codec.DELTA5.encode(1, 0);
090: byte[] classSuper = Codec.DELTA5.encode(2, 0);
091: byte[] classInterfaceCount = Codec.DELTA5.encode(2, 0);
092: byte[] classInterfaceRef1 = classBands.encodeBandLong(
093: new long[] { 3, 4 }, Codec.DELTA5);
094: byte[] classFieldCount = Codec.DELTA5.encode(0, 0);
095: byte[] classMethodCount = Codec.DELTA5.encode(0, 0);
096: byte[] classFlags = Codec.UNSIGNED5.encode(0, 0);
097: byte[][] allArrays = new byte[][] { classThis, classSuper,
098: classInterfaceCount, classInterfaceRef1,
099: classFieldCount, classMethodCount, classFlags };
100: int total = classThis.length + classSuper.length
101: + classInterfaceCount.length
102: + classInterfaceRef1.length + classFieldCount.length
103: + classMethodCount.length + classFlags.length;
104: byte[] bytes = new byte[total];
105: int index = 0;
106: for (int i = 0; i < allArrays.length; i++) {
107: for (int j = 0; j < allArrays[i].length; j++) {
108: bytes[index] = allArrays[i][j];
109: index++;
110: }
111: }
112: ByteArrayInputStream in = new ByteArrayInputStream(bytes);
113: classBands.unpack(in);
114: assertEquals(cpClasses[1], classBands.getClassThis()[0]);
115: assertEquals(cpClasses[2], classBands.getClassSuper()[0]);
116: assertEquals(1, classBands.getClassInterfaces().length);
117: assertEquals(2, classBands.getClassInterfaces()[0].length);
118: assertEquals(cpClasses[3],
119: classBands.getClassInterfaces()[0][0]);
120: assertEquals(cpClasses[4],
121: classBands.getClassInterfaces()[0][1]);
122: cpClasses = null;
123: }
124:
125: public void testWithMethods() throws Pack200Exception, IOException {
126: cpClasses = new String[] { "Class1", "Class2", "Class3" };
127: cpDescriptor = new String[] { "method1", "method2", "method3" };
128: cpUTF8 = new String[0];
129: byte[] classThis = Codec.DELTA5.encode(1, 0);
130: byte[] classSuper = Codec.DELTA5.encode(2, 0);
131: byte[] classInterfaceCount = Codec.DELTA5.encode(0, 0);
132: byte[] classFieldCount = Codec.DELTA5.encode(0, 0);
133: byte[] classMethodCount = Codec.DELTA5.encode(3, 0);
134: byte[] methodDescr = classBands.encodeBandLong(new long[] { 0,
135: 1, 2 }, Codec.MDELTA5);
136: byte[] methodFlagsLo = classBands.encodeBandLong(new long[] {
137: 0, 0, 0 }, Codec.UNSIGNED5);
138: byte[] classFlags = Codec.UNSIGNED5.encode(0, 0);
139: byte[][] allArrays = new byte[][] { classThis, classSuper,
140: classInterfaceCount, classFieldCount, classMethodCount,
141: methodDescr, methodFlagsLo, classFlags, };
142: int total = 0;
143: for (int i = 0; i < allArrays.length; i++) {
144: total += allArrays[i].length;
145: }
146: byte[] bytes = new byte[total];
147: int index = 0;
148: for (int i = 0; i < allArrays.length; i++) {
149: for (int j = 0; j < allArrays[i].length; j++) {
150: bytes[index] = allArrays[i][j];
151: index++;
152: }
153: }
154: ByteArrayInputStream in = new ByteArrayInputStream(bytes);
155: classBands.unpack(in);
156: assertEquals(cpClasses[1], classBands.getClassThis()[0]);
157: assertEquals(cpClasses[2], classBands.getClassSuper()[0]);
158: assertEquals(1, classBands.getMethodDescr().length);
159: assertEquals(3, classBands.getMethodDescr()[0].length);
160: assertEquals(cpDescriptor[0], classBands.getMethodDescr()[0][0]);
161: assertEquals(cpDescriptor[1], classBands.getMethodDescr()[0][1]);
162: assertEquals(cpDescriptor[2], classBands.getMethodDescr()[0][2]);
163:
164: cpClasses = null;
165: cpDescriptor = null;
166: }
167:
168: public void testWithFields() {
169:
170: }
171:
172: }
|