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 junit.framework.TestCase;
020:
021: import org.apache.harmony.pack200.AttributeLayout;
022: import org.apache.harmony.pack200.Codec;
023: import org.apache.harmony.pack200.Pack200Exception;
024: import org.apache.harmony.pack200.Segment;
025: import org.apache.harmony.pack200.SegmentConstantPool;
026:
027: public class AttributeLayoutTest extends TestCase {
028:
029: public class TestSegment extends Segment {
030: public SegmentConstantPool getConstantPool() {
031: final Object[][] data = new Object[][] {
032: {}, // ALL
033: { "Zero", "One", "Two", "Three", "Four", "Five",
034: "Six", "Seven", "Eight", "Nine" }, // UTF-8
035: {},
036: {},
037: {},
038: {},
039: {},
040: {},
041: { "Eins", "Zwei", "Drei", "Vier", "Funf", "Sechs",
042: "Sieben", "Acht", "Neun" }, // Signature
043: };
044: return new SegmentConstantPool(null) {
045: public Object getValue(int cp, long index) {
046: if (index == -1)
047: return null;
048: return data[cp][(int) index];
049: }
050:
051: };
052: }
053: }
054:
055: public void testBadData() {
056: assertTrue(throwsException(null, AttributeLayout.CONTEXT_CLASS,
057: ""));
058: assertTrue(throwsException("", AttributeLayout.CONTEXT_CLASS,
059: ""));
060: assertTrue(!throwsException("name",
061: AttributeLayout.CONTEXT_CLASS, ""));
062: assertTrue(!throwsException("name",
063: AttributeLayout.CONTEXT_METHOD, ""));
064: assertTrue(!throwsException("name",
065: AttributeLayout.CONTEXT_FIELD, ""));
066: assertTrue(!throwsException("name",
067: AttributeLayout.CONTEXT_CODE, ""));
068: assertTrue(throwsException("name", -1, ""));
069: assertTrue(throwsException("name", 1234, ""));
070: }
071:
072: public void testLayoutRU() throws Pack200Exception {
073: AttributeLayout layout = new AttributeLayout("RU",
074: AttributeLayout.CONTEXT_CLASS, "RU", 1);
075: Segment segment = new TestSegment();
076: assertNull(layout.getValue(-1, segment.getConstantPool()));
077: assertEquals("Zero", layout.getValue(0, segment
078: .getConstantPool()));
079: assertEquals("One", layout.getValue(1, segment
080: .getConstantPool()));
081: }
082:
083: public void testLayoutRUN() throws Pack200Exception {
084: AttributeLayout layout = new AttributeLayout("RUN",
085: AttributeLayout.CONTEXT_CLASS, "RUN", 1);
086: Segment segment = new TestSegment();
087: assertNull(layout.getValue(0, segment.getConstantPool()));
088: assertEquals("Zero", layout.getValue(1, segment
089: .getConstantPool()));
090: assertEquals("One", layout.getValue(2, segment
091: .getConstantPool()));
092: }
093:
094: public void testLayoutRS() throws Pack200Exception {
095: AttributeLayout layout = new AttributeLayout("RS",
096: AttributeLayout.CONTEXT_CLASS, "RS", 1);
097: Segment segment = new TestSegment();
098: assertNull(layout.getValue(-1, segment.getConstantPool()));
099: assertEquals("Eins", layout.getValue(0, segment
100: .getConstantPool()));
101: assertEquals("Zwei", layout.getValue(1, segment
102: .getConstantPool()));
103: }
104:
105: public void testLayoutRSN() throws Pack200Exception {
106: AttributeLayout layout = new AttributeLayout("RSN",
107: AttributeLayout.CONTEXT_CLASS, "RSN", 1);
108: Segment segment = new TestSegment();
109: assertNull(layout.getValue(0, segment.getConstantPool()));
110: assertEquals("Eins", layout.getValue(1, segment
111: .getConstantPool()));
112: assertEquals("Zwei", layout.getValue(2, segment
113: .getConstantPool()));
114: }
115:
116: public void testGetCodec() throws Pack200Exception {
117: AttributeLayout layout = new AttributeLayout("O",
118: AttributeLayout.CONTEXT_CLASS, "HOBS", 1);
119: assertEquals(Codec.BRANCH5, layout.getCodec());
120: layout = new AttributeLayout("P",
121: AttributeLayout.CONTEXT_METHOD, "PIN", 1);
122: assertEquals(Codec.BCI5, layout.getCodec());
123: layout = new AttributeLayout("S",
124: AttributeLayout.CONTEXT_FIELD, "HS", 1);
125: assertEquals(Codec.SIGNED5, layout.getCodec());
126: layout = new AttributeLayout("RS",
127: AttributeLayout.CONTEXT_CODE, "RRRS", 1);
128: assertEquals(Codec.UNSIGNED5, layout.getCodec());
129: layout = new AttributeLayout("KS",
130: AttributeLayout.CONTEXT_CLASS, "RKS", 1);
131: assertEquals(Codec.UNSIGNED5, layout.getCodec());
132: layout = new AttributeLayout("B",
133: AttributeLayout.CONTEXT_CLASS, "TRKSB", 1);
134: assertEquals(Codec.BYTE1, layout.getCodec());
135: }
136:
137: public boolean throwsException(String name, int context,
138: String layout) {
139: try {
140: new AttributeLayout(name, context, layout, -1);
141: return false;
142: } catch (Pack200Exception e) {
143: return true;
144: }
145: }
146: }
|