Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absence of Pack200 in JDK 14 should not cause failure of JaCoCo build #984

Merged
merged 8 commits into from Dec 18, 2019
Expand Up @@ -31,18 +31,18 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.jar.JarInputStream;
import java.util.jar.Pack200;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.internal.Pack200Streams;
import org.jacoco.core.internal.data.CRC64;
import org.jacoco.core.test.TargetLoader;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;
import org.junit.rules.TemporaryFolder;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
Expand Down Expand Up @@ -294,6 +294,13 @@ public void testAnalyzeAll_BrokenGZ() {

@Test
public void testAnalyzeAll_Pack200() throws IOException {
try {
Class.forName("java.util.jar.Pack200");
} catch (ClassNotFoundException e) {
throw new AssumptionViolatedException(
"this test requires JDK with Pack200");
}

final ByteArrayOutputStream zipbuffer = new ByteArrayOutputStream();
final ZipOutputStream zip = new ZipOutputStream(zipbuffer);
zip.putNextEntry(
Expand All @@ -303,10 +310,7 @@ public void testAnalyzeAll_Pack200() throws IOException {

final ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
GZIPOutputStream gzipOutput = new GZIPOutputStream(pack200buffer);
Pack200.newPacker()
.pack(new JarInputStream(
new ByteArrayInputStream(zipbuffer.toByteArray())),
gzipOutput);
Pack200Streams.pack(zipbuffer.toByteArray(), gzipOutput);
gzipOutput.finish();

final int count = analyzer.analyzeAll(
Expand Down
Expand Up @@ -26,22 +26,21 @@
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.jacoco.core.analysis.AnalyzerTest;
import org.jacoco.core.internal.Pack200Streams;
import org.jacoco.core.internal.data.CRC64;
import org.jacoco.core.internal.instr.InstrSupport;
import org.jacoco.core.runtime.IExecutionDataAccessorGenerator;
import org.jacoco.core.test.TargetLoader;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
Expand Down Expand Up @@ -398,6 +397,13 @@ public void testInstrumentAll_BrokenGZ() {

@Test
public void testInstrumentAll_Pack200() throws IOException {
try {
Class.forName("java.util.jar.Pack200");
} catch (ClassNotFoundException e) {
throw new AssumptionViolatedException(
"this test requires JDK with Pack200");
}

ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream();
ZipOutputStream zipout = new ZipOutputStream(jarbuffer);
zipout.putNextEntry(new ZipEntry("Test.class"));
Expand All @@ -406,26 +412,18 @@ public void testInstrumentAll_Pack200() throws IOException {

ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
GZIPOutputStream gzipOutput = new GZIPOutputStream(pack200buffer);
Pack200.newPacker()
.pack(new JarInputStream(
new ByteArrayInputStream(jarbuffer.toByteArray())),
gzipOutput);
Pack200Streams.pack(jarbuffer.toByteArray(), gzipOutput);
gzipOutput.finish();

ByteArrayOutputStream out = new ByteArrayOutputStream();
int count = instrumenter.instrumentAll(
new ByteArrayInputStream(pack200buffer.toByteArray()), out,
"Test");

jarbuffer.reset();
Pack200.newUnpacker()
.unpack(new GZIPInputStream(
new ByteArrayInputStream(out.toByteArray())),
new JarOutputStream(jarbuffer));

assertEquals(1, count);
ZipInputStream zipin = new ZipInputStream(
new ByteArrayInputStream(jarbuffer.toByteArray()));
Pack200Streams.unpack(new GZIPInputStream(
new ByteArrayInputStream(out.toByteArray()))));
assertEquals("Test.class", zipin.getNextEntry().getName());
assertNull(zipin.getNextEntry());
}
Expand Down
Expand Up @@ -19,14 +19,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.jar.JarInputStream;
import java.util.jar.Pack200;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.jacoco.core.test.TargetLoader;
import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;

/**
* Unit tests for {@link ContentTypeDetector}.
Expand Down Expand Up @@ -218,17 +217,21 @@ public void testZipFile() throws IOException {

@Test
public void testPack200File() throws IOException {
try {
Class.forName("java.util.jar.Pack200");
} catch (ClassNotFoundException e) {
throw new AssumptionViolatedException(
"this test requires JDK with Pack200");
}

final ByteArrayOutputStream zipbuffer = new ByteArrayOutputStream();
final ZipOutputStream zip = new ZipOutputStream(zipbuffer);
zip.putNextEntry(new ZipEntry("hello.txt"));
zip.write("Hello Zip!".getBytes());
zip.close();

final ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
Pack200.newPacker()
.pack(new JarInputStream(
new ByteArrayInputStream(zipbuffer.toByteArray())),
pack200buffer);
Pack200Streams.pack(zipbuffer.toByteArray(), pack200buffer);
initData(pack200buffer.toByteArray());
assertEquals(ContentTypeDetector.PACK200FILE, detector.getType());
assertContent();
Expand Down
Expand Up @@ -14,6 +14,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
Expand All @@ -25,21 +26,28 @@
import java.io.OutputStream;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.jacoco.core.test.TargetLoader;
import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;

/**
* Unit tests for {@link Pack200Streams}.
*/
public class Pack200StreamsTest {

@Test
public void testPack() throws IOException {
public void pack_should_pack() throws Exception {
try {
Class.forName("java.util.jar.Pack200");
} catch (ClassNotFoundException e) {
throw new AssumptionViolatedException(
"this test requires JDK with Pack200");
}

ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream();
ZipOutputStream zipout = new ZipOutputStream(jarbuffer);
zipout.putNextEntry(new ZipEntry("Test.class"));
Expand All @@ -51,9 +59,13 @@ public void testPack() throws IOException {
new NoCloseOutputStream(pack200buffer));

jarbuffer.reset();
Pack200.newUnpacker().unpack(
new ByteArrayInputStream(pack200buffer.toByteArray()),
new JarOutputStream(jarbuffer));
final Object unpacker = Class.forName("java.util.jar.Pack200")
.getMethod("newUnpacker").invoke(null);
Class.forName("java.util.jar.Pack200$Unpacker")
.getMethod("unpack", InputStream.class, JarOutputStream.class)
.invoke(unpacker,
new ByteArrayInputStream(pack200buffer.toByteArray()),
new JarOutputStream(jarbuffer));

ZipInputStream zipin = new ZipInputStream(
new ByteArrayInputStream(jarbuffer.toByteArray()));
Expand All @@ -62,16 +74,64 @@ public void testPack() throws IOException {
}

@Test
public void testUnpack() throws IOException {
public void pack_should_throw_IOException_when_can_not_write_to_OutputStream() {
try {
Class.forName("java.util.jar.Pack200");
} catch (ClassNotFoundException e) {
throw new AssumptionViolatedException(
"this test requires JDK with Pack200");
}

final OutputStream outputStream = new BrokenOutputStream();
try {
Pack200Streams.pack(new byte[0], outputStream);
fail("expected exception");
} catch (IOException e) {
assertTrue(e.getCause() instanceof IOException);
assertEquals("fake broken output stream",
e.getCause().getMessage());
}
}

@Test
public void pack_should_throw_IOException_when_Pack200_not_available_in_JDK() {
try {
Class.forName("java.util.jar.Pack200");
throw new AssumptionViolatedException(
"this test requires JDK without Pack200");
} catch (ClassNotFoundException ignore) {
}

try {
Pack200Streams.pack(new byte[0], new ByteArrayOutputStream());
fail("expected exception");
} catch (IOException e) {
assertNull(e.getMessage());
assertTrue(e.getCause() instanceof ClassNotFoundException);
}
}

@Test
public void unpack_should_unpack() throws Exception {
try {
Class.forName("java.util.jar.Pack200");
} catch (ClassNotFoundException e) {
throw new AssumptionViolatedException(
"this test requires JDK with Pack200");
}

ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream();
ZipOutputStream zipout = new ZipOutputStream(jarbuffer);
zipout.putNextEntry(new ZipEntry("Test.class"));
zipout.write(TargetLoader.getClassDataAsBytes(getClass()));
zipout.finish();

ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
Pack200.newPacker()
.pack(new JarInputStream(
final Object packer = Class.forName("java.util.jar.Pack200")
.getMethod("newPacker").invoke(null);
Class.forName("java.util.jar.Pack200$Packer")
.getMethod("pack", JarInputStream.class, OutputStream.class)
.invoke(packer, new JarInputStream(
new ByteArrayInputStream(jarbuffer.toByteArray())),
pack200buffer);

Expand All @@ -83,6 +143,43 @@ public void testUnpack() throws IOException {
assertNull(zipin.getNextEntry());
}

@Test
public void unpack_should_throw_IOException_when_can_not_read_from_InputStream() {
try {
Class.forName("java.util.jar.Pack200");
} catch (ClassNotFoundException e) {
throw new AssumptionViolatedException(
"this test requires JDK with Pack200");
}

final InputStream inputStream = new BrokenInputStream();
try {
Pack200Streams.unpack(inputStream);
fail("expected exception");
} catch (IOException e) {
assertTrue(e.getCause() instanceof IOException);
assertEquals("fake broken input stream", e.getCause().getMessage());
}
}

@Test
public void unpack_should_throw_IOException_when_Pack200_not_available_in_JDK() {
try {
Class.forName("java.util.jar.Pack200");
throw new AssumptionViolatedException(
"this test requires JDK without Pack200");
} catch (ClassNotFoundException ignore) {
}

try {
Pack200Streams.unpack(new ByteArrayInputStream(new byte[0]));
fail("expected exception");
} catch (IOException e) {
assertNull(e.getMessage());
assertTrue(e.getCause() instanceof ClassNotFoundException);
}
}

static class NoCloseInputStream extends FilterInputStream {
public NoCloseInputStream(InputStream in) {
super(in);
Expand All @@ -105,4 +202,18 @@ public void close() throws IOException {
}
}

private static class BrokenInputStream extends InputStream {
@Override
public int read() throws IOException {
throw new IOException("fake broken input stream");
}
}

private static class BrokenOutputStream extends OutputStream {
@Override
public void write(int b) throws IOException {
throw new IOException("fake broken output stream");
}
}

}