Pack200 - new tests and some accompanying bug fixes

git-svn-id: https://svn.apache.org/repos/asf/harmony/enhanced/java/trunk@986626 13f79535-47bb-0310-9956-ffa450edef68
master
Sian January 14 years ago
parent fcf361ebe7
commit d8216aad44
  1. 10
      src/main/java/org/apache/harmony/pack200/ClassBands.java
  2. 2
      src/main/java/org/apache/harmony/pack200/MetadataBandGroup.java
  3. 2
      src/main/java/org/apache/harmony/pack200/PackingUtils.java
  4. 9
      src/main/java/org/apache/harmony/pack200/PopulationCodec.java
  5. 6
      src/main/java/org/apache/harmony/pack200/RunCodec.java
  6. 21
      src/main/java/org/apache/harmony/pack200/Segment.java
  7. 14
      src/test/java/org/apache/harmony/pack200/tests/ArchiveTest.java
  8. 17
      src/test/java/org/apache/harmony/pack200/tests/CodecTest.java
  9. 16
      src/test/java/org/apache/harmony/pack200/tests/PopulationCodecTest.java
  10. 166
      src/test/java/org/apache/harmony/pack200/tests/RunCodecTest.java
  11. BIN
      src/test/resources/org/apache/harmony/pack200/tests/annotations.jar

@ -1328,22 +1328,22 @@ public class ClassBands extends BandSet {
boolean visible, List nameRU, List t, List values, List caseArrayN, List nestTypeRS, List nestNameRU, List nestPairN) { boolean visible, List nameRU, List t, List values, List caseArrayN, List nestTypeRS, List nestNameRU, List nestPairN) {
if(visible) { if(visible) {
method_RVPA_bands.addAnnotation(desc, nameRU, t, values, caseArrayN, nestTypeRS, nestNameRU, nestPairN); method_RVPA_bands.addAnnotation(desc, nameRU, t, values, caseArrayN, nestTypeRS, nestNameRU, nestPairN);
Integer flag = (Integer) tempMethodFlags.remove(tempMethodFlags.size() - 1); Long flag = (Long) tempMethodFlags.remove(tempMethodFlags.size() - 1);
if((flag.intValue() & (1<<23)) != 0) { if((flag.intValue() & (1<<23)) != 0) {
method_RVPA_bands.incrementAnnoN(); method_RVPA_bands.incrementAnnoN();
} else { } else {
method_RVPA_bands.newEntryInAnnoN(); method_RVPA_bands.newEntryInAnnoN();
} }
tempMethodFlags.add(new Integer(flag.intValue() | (1<<23))); tempMethodFlags.add(new Long(flag.longValue() | (1<<23)));
} else { } else {
method_RIPA_bands.addAnnotation(desc, nameRU, t, values, caseArrayN, nestTypeRS, nestNameRU, nestPairN); method_RIPA_bands.addAnnotation(desc, nameRU, t, values, caseArrayN, nestTypeRS, nestNameRU, nestPairN);
Integer flag = (Integer) tempMethodFlags.remove(tempMethodFlags.size() - 1); Long flag = (Long) tempMethodFlags.remove(tempMethodFlags.size() - 1);
if((flag.intValue() & (1<<24)) != 0) { if((flag.longValue() & (1<<24)) != 0) {
method_RIPA_bands.incrementAnnoN(); method_RIPA_bands.incrementAnnoN();
} else { } else {
method_RIPA_bands.newEntryInAnnoN(); method_RIPA_bands.newEntryInAnnoN();
} }
tempMethodFlags.add(new Integer(flag.intValue() | (1<<24))); tempMethodFlags.add(new Long(flag.longValue() | (1<<24)));
} }
} }

@ -268,7 +268,7 @@ public class MetadataBandGroup extends BandSet {
} else if (tag.equals("J")) { } else if (tag.equals("J")) {
Long value = (Long)valuesIterator.next(); Long value = (Long)valuesIterator.next();
caseJ_KJ.add(cpBands.getConstant(value)); caseJ_KJ.add(cpBands.getConstant(value));
} else if (tag.equals("C")) { } else if (tag.equals("c")) {
String value = (String)valuesIterator.next(); String value = (String)valuesIterator.next();
casec_RS.add(cpBands.getCPSignature(value)); casec_RS.add(cpBands.getCPSignature(value));
} else if (tag.equals("e")) { } else if (tag.equals("e")) {

@ -86,7 +86,7 @@ public class PackingUtils {
} }
/** /**
* When effort is 0, the packer copys through the original jar input stream * When effort is 0, the packer copies through the original jar input stream
* without compression * without compression
* *
* @param jarInputStream * @param jarInputStream

@ -151,11 +151,16 @@ public class PopulationCodec extends Codec {
} }
public byte[] encode(int[] favoured, int[] tokens, int[] unfavoured) throws Pack200Exception { public byte[] encode(int[] favoured, int[] tokens, int[] unfavoured) throws Pack200Exception {
byte[] favouredEncoded = favouredCodec.encode(favoured); int[] favoured2 = new int[favoured.length + 1];
System.arraycopy(favoured, 0, favoured2, 0, favoured.length);
favoured2[favoured2.length - 1] = favoured[favoured.length - 1]; // repeat last value;
byte[] favouredEncoded = favouredCodec.encode(favoured2);
byte[] tokensEncoded = tokenCodec.encode(tokens); byte[] tokensEncoded = tokenCodec.encode(tokens);
byte[] unfavouredEncoded = unfavouredCodec.encode(unfavoured); byte[] unfavouredEncoded = unfavouredCodec.encode(unfavoured);
byte[] band = new byte[favouredEncoded.length + tokensEncoded.length + unfavouredEncoded.length]; byte[] band = new byte[favouredEncoded.length + tokensEncoded.length + unfavouredEncoded.length];
System.arraycopy(favouredEncoded, 0, band, 0, favouredEncoded.length);
System.arraycopy(tokensEncoded, 0, band, favouredEncoded.length, tokensEncoded.length);
System.arraycopy(unfavouredEncoded, 0, band, favouredEncoded.length + tokensEncoded.length, unfavouredEncoded.length);
return band; return band;
} }

@ -135,13 +135,11 @@ public class RunCodec extends Codec {
} }
public byte[] encode(int value, int last) throws Pack200Exception { public byte[] encode(int value, int last) throws Pack200Exception {
// TODO Auto-generated method stub throw new Pack200Exception("Must encode entire band at once with a RunCodec");
return null;
} }
public byte[] encode(int value) throws Pack200Exception { public byte[] encode(int value) throws Pack200Exception {
// TODO Auto-generated method stub throw new Pack200Exception("Must encode entire band at once with a RunCodec");
return null;
} }
public int getK() { public int getK() {

@ -458,31 +458,40 @@ public class Segment implements ClassVisitor {
name = ""; name = "";
} }
nameRU.add(name); nameRU.add(name);
values.add(value); addValueAndTag(value);
addTag(value);
} }
private void addTag(Object value) { private void addValueAndTag(Object value) {
if(value instanceof Integer) { if(value instanceof Integer) {
T.add("I"); T.add("I");
values.add(value);
} else if (value instanceof Double) { } else if (value instanceof Double) {
T.add("D"); T.add("D");
values.add(value);
} else if (value instanceof Float) { } else if (value instanceof Float) {
T.add("F"); T.add("F");
values.add(value);
} else if (value instanceof Long) { } else if (value instanceof Long) {
T.add("J"); T.add("J");
values.add(value);
} else if (value instanceof Byte) { } else if (value instanceof Byte) {
T.add("B"); T.add("B");
values.add(new Integer(((Byte)value).intValue()));
} else if (value instanceof Character) { } else if (value instanceof Character) {
T.add("C"); T.add("C");
values.add(new Integer(((Character)value).charValue()));
} else if (value instanceof Short) { } else if (value instanceof Short) {
T.add("S"); T.add("S");
values.add(new Integer(((Short)value).intValue()));
} else if (value instanceof Boolean) { } else if (value instanceof Boolean) {
T.add("Z"); T.add("Z");
values.add(new Integer(((Boolean)value).booleanValue() ? 1 : 0));
} else if (value instanceof String) { } else if (value instanceof String) {
T.add("s"); T.add("s");
values.add(value);
} else if (value instanceof Type) { } else if (value instanceof Type) {
T.add("c"); T.add("c");
values.add(((Type)value).toString());
} }
} }
@ -499,8 +508,7 @@ public class Segment implements ClassVisitor {
Integer numPairs = (Integer) nestPairN.remove(nestPairN.size() - 1); Integer numPairs = (Integer) nestPairN.remove(nestPairN.size() - 1);
nestPairN.add(new Integer(numPairs.intValue() + 1)); nestPairN.add(new Integer(numPairs.intValue() + 1));
nestNameRU.add(name); nestNameRU.add(name);
values.add(value); addValueAndTag(value);
addTag(value);
} }
public AnnotationVisitor visitAnnotation(String arg0, public AnnotationVisitor visitAnnotation(String arg0,
@ -543,8 +551,7 @@ public class Segment implements ClassVisitor {
name = ""; name = "";
} }
nameRU.add(name); nameRU.add(name);
values.add(value); addValueAndTag(value);
addTag(value);
} }
public AnnotationVisitor visitAnnotation(String arg0, public AnnotationVisitor visitAnnotation(String arg0,

@ -344,6 +344,20 @@ public class ArchiveTest extends TestCase {
compareFiles(jarFile, jarFile2); compareFiles(jarFile, jarFile2);
} }
public void testAnnotations2() throws IOException, Pack200Exception,
URISyntaxException {
in = new JarFile(new File(Archive.class.getResource(
"/org/apache/harmony/pack200/tests/annotations.jar").toURI()));
file = File.createTempFile("annotations", ".pack");
file.deleteOnExit();
out = new FileOutputStream(file);
PackingOptions options = new PackingOptions();
options.setGzip(false);
new Archive(in, out, options).pack();
in.close();
out.close();
}
public void testE0() throws Pack200Exception, IOException, URISyntaxException { public void testE0() throws Pack200Exception, IOException, URISyntaxException {
File f1 = new File(Archive.class.getResource( File f1 = new File(Archive.class.getResource(

@ -165,23 +165,6 @@ public class CodecTest extends TestCase {
assertFalse(byte2s.encodes(256)); assertFalse(byte2s.encodes(256));
} }
public void testRunCodec() throws Exception {
RunCodec runCodec = new RunCodec(1, Codec.UNSIGNED5, Codec.BYTE1);
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[] {
(byte) 192, 0, (byte) 192, 0 });
assertEquals(192, runCodec.decode(bais));
assertEquals(192, runCodec.decode(bais));
assertEquals(0, runCodec.decode(bais));
assertEquals(0, bais.available());
runCodec = new RunCodec(1, Codec.BYTE1, Codec.UNSIGNED5);
bais = new ByteArrayInputStream(new byte[] { (byte) 192, 0, (byte) 192,
0 });
assertEquals(192, runCodec.decode(bais));
assertEquals(0, runCodec.decode(bais));
assertEquals(192, runCodec.decode(bais));
assertEquals(0, bais.available());
}
public void testUnsigned5() throws Exception { public void testUnsigned5() throws Exception {
decode(Codec.UNSIGNED5, new byte[] { 1 }, 1, 0); decode(Codec.UNSIGNED5, new byte[] { 1 }, 1, 0);
decode(Codec.UNSIGNED5, new byte[] { (byte) 191 }, 191, 0); decode(Codec.UNSIGNED5, new byte[] { (byte) 191 }, 191, 0);

@ -22,6 +22,7 @@ import java.io.InputStream;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.harmony.pack200.BHSDCodec;
import org.apache.harmony.pack200.Codec; import org.apache.harmony.pack200.Codec;
import org.apache.harmony.pack200.Pack200Exception; import org.apache.harmony.pack200.Pack200Exception;
import org.apache.harmony.pack200.PopulationCodec; import org.apache.harmony.pack200.PopulationCodec;
@ -63,5 +64,20 @@ public class PopulationCodecTest extends TestCase {
} }
assertEquals(0, in.available()); assertEquals(0, in.available());
} }
public void testEncodeSingleValue() {
try {
new PopulationCodec(BHSDCodec.SIGNED5, BHSDCodec.SIGNED5, BHSDCodec.UDELTA5).encode(5);
fail("Should not allow a single value to be encoded as we don't know which codec to use");
} catch (Pack200Exception e) {
// pass
}
try {
new PopulationCodec(BHSDCodec.SIGNED5, BHSDCodec.SIGNED5, BHSDCodec.UDELTA5).encode(5, 8);
fail("Should not allow a single value to be encoded as we don't know which codec to use");
} catch (Pack200Exception e) {
// pass
}
}
} }

@ -0,0 +1,166 @@
/*
* 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 java.io.ByteArrayInputStream;
import junit.framework.TestCase;
import org.apache.harmony.pack200.BHSDCodec;
import org.apache.harmony.pack200.Codec;
import org.apache.harmony.pack200.Pack200Exception;
import org.apache.harmony.pack200.PopulationCodec;
import org.apache.harmony.pack200.RunCodec;
/**
* Test for RunCodec
*/
public class RunCodecTest extends TestCase {
public void testRunCodec() {
try {
new RunCodec(0, BHSDCodec.SIGNED5, BHSDCodec.UDELTA5);
fail("Should not allow a k value of 0");
} catch (Pack200Exception e) {
// pass
}
try {
new RunCodec(10, null, BHSDCodec.UDELTA5);
fail("Should not allow a null codec");
} catch (Pack200Exception e) {
// pass
}
try {
new RunCodec(10, BHSDCodec.UDELTA5, null);
fail("Should not allow a null codec");
} catch (Pack200Exception e) {
// pass
}
try {
new RunCodec(10, null, null);
fail("Should not allow a null codec");
} catch (Pack200Exception e) {
// pass
}
}
public void testDecode() throws Exception {
RunCodec runCodec = new RunCodec(1, Codec.UNSIGNED5, Codec.BYTE1);
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[] {
(byte) 192, 0, (byte) 192, 0 });
assertEquals(192, runCodec.decode(bais));
assertEquals(192, runCodec.decode(bais));
assertEquals(0, runCodec.decode(bais));
assertEquals(0, bais.available());
runCodec = new RunCodec(1, Codec.BYTE1, Codec.UNSIGNED5);
bais = new ByteArrayInputStream(new byte[] { (byte) 192, 0, (byte) 192,
0 });
assertEquals(192, runCodec.decode(bais));
assertEquals(0, runCodec.decode(bais));
assertEquals(192, runCodec.decode(bais));
assertEquals(0, bais.available());
}
public void testDecodeInts() throws Exception {
int[] band = new int[] { 1, -2, -3, 1000, 55, 5, 10, 20 };
// first 5 of band to be encoded with DELTA5
byte[] bytes1 = Codec.DELTA5.encode(new int[] { 1, -2, -3, 1000, 55 });
// rest of band to be encoded with UNSIGNED5
byte[] bytes2 = Codec.UNSIGNED5.encode(new int[] { 5, 10, 20 });
byte[] bandEncoded = new byte[bytes1.length + bytes2.length];
System.arraycopy(bytes1, 0, bandEncoded, 0, bytes1.length);
System.arraycopy(bytes2, 0, bandEncoded, bytes1.length, bytes2.length);
RunCodec runCodec = new RunCodec(5, Codec.DELTA5, Codec.UNSIGNED5);
int[] bandDecoded = runCodec.decodeInts(8, new ByteArrayInputStream(
bandEncoded));
assertEquals(band.length, bandDecoded.length);
for (int i = 0; i < band.length; i++) {
assertEquals(band[i], bandDecoded[i]);
}
}
public void testNestedPopulationCodec() throws Exception {
int[] band = new int[] { 11, 12, 33, 4000, -555, 5, 10, 20, 10, 3, 20,
20, 20, 10, 10, 999, 20, 789, 10, 10, 355, 12345 };
// first 5 of band to be encoded with DELTA5
byte[] bytes1 = Codec.DELTA5
.encode(new int[] { 11, 12, 33, 4000, -555 });
// rest of band to be encoded with a PopulationCodec
PopulationCodec popCodec = new PopulationCodec(Codec.UNSIGNED5,
Codec.BYTE1, Codec.UNSIGNED5);
byte[] bytes2 = popCodec.encode(new int[] { 10, 20 }, new int[] { 0, 1,
2, 1, 0, 2, 2, 2, 1, 1, 0, 2, 0, 1, 1, 0, 0 }, new int[] { 5,
3, 999, 789, 355, 12345 });
byte[] bandEncoded = new byte[bytes1.length + bytes2.length];
System.arraycopy(bytes1, 0, bandEncoded, 0, bytes1.length);
System.arraycopy(bytes2, 0, bandEncoded, bytes1.length, bytes2.length);
RunCodec runCodec = new RunCodec(5, Codec.DELTA5, new PopulationCodec(
Codec.UNSIGNED5, Codec.BYTE1, Codec.UNSIGNED5));
int[] bandDecoded = runCodec.decodeInts(band.length,
new ByteArrayInputStream(bandEncoded));
assertEquals(band.length, bandDecoded.length);
for (int i = 0; i < band.length; i++) {
assertEquals(band[i], bandDecoded[i]);
}
}
public void testNestedRunCodec() throws Exception {
int[] band = new int[] { 1, 2, 3, 10, 20, 30, 100, 200, 300 };
// first 3 of band to be encoded with UDELTA5
byte[] bytes1 = Codec.UDELTA5.encode(new int[] { 1, 2, 3 });
// rest of band to be encoded with a RunCodec
byte[] bytes2 = Codec.BYTE1.encode(new int[] { 10, 20, 30 });
byte[] bytes3 = Codec.UNSIGNED5.encode(new int[] { 100, 200, 300 });
byte[] bandEncoded = new byte[bytes1.length + bytes2.length
+ bytes3.length];
System.arraycopy(bytes1, 0, bandEncoded, 0, bytes1.length);
System.arraycopy(bytes2, 0, bandEncoded, bytes1.length, bytes2.length);
System.arraycopy(bytes3, 0, bandEncoded, bytes1.length + bytes2.length,
bytes3.length);
RunCodec runCodec = new RunCodec(3, Codec.UDELTA5, new RunCodec(3,
Codec.BYTE1, Codec.UNSIGNED5));
int[] bandDecoded = runCodec.decodeInts(9, new ByteArrayInputStream(
bandEncoded));
assertEquals(band.length, bandDecoded.length);
for (int i = 0; i < band.length; i++) {
assertEquals(band[i], bandDecoded[i]);
}
}
public void testToString() throws Pack200Exception {
RunCodec runCodec = new RunCodec(3, Codec.UNSIGNED5, Codec.BYTE1);
assertEquals(
"RunCodec[k=" + 3 + ";aCodec=" + Codec.UNSIGNED5.toString()
+ "bCodec=" + Codec.BYTE1.toString() + "]",
runCodec.toString());
}
public void testEncodeSingleValue() {
try {
new RunCodec(10, BHSDCodec.SIGNED5, BHSDCodec.UDELTA5).encode(5);
fail("Should not allow a single value to be encoded as we don't know which codec to use");
} catch (Pack200Exception e) {
// pass
}
try {
new RunCodec(10, BHSDCodec.SIGNED5, BHSDCodec.UDELTA5).encode(5, 8);
fail("Should not allow a single value to be encoded as we don't know which codec to use");
} catch (Pack200Exception e) {
// pass
}
}
}
Loading…
Cancel
Save