Skip to content

Commit 60cf0dd

Browse files
Godinmarchof
authored andcommitted
Absence of Pack200 in JDK 14 should not cause failure of JaCoCo build (bazel-contrib#984)
1 parent 7f56550 commit 60cf0dd

File tree

6 files changed

+197
-37
lines changed

6 files changed

+197
-37
lines changed

org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@
3131
import java.util.HashMap;
3232
import java.util.HashSet;
3333
import java.util.Map;
34-
import java.util.jar.JarInputStream;
35-
import java.util.jar.Pack200;
3634
import java.util.zip.GZIPOutputStream;
3735
import java.util.zip.ZipEntry;
3836
import java.util.zip.ZipOutputStream;
3937

4038
import org.jacoco.core.data.ExecutionDataStore;
39+
import org.jacoco.core.internal.Pack200Streams;
4140
import org.jacoco.core.internal.data.CRC64;
4241
import org.jacoco.core.test.TargetLoader;
4342
import org.junit.Before;
4443
import org.junit.Rule;
4544
import org.junit.Test;
45+
import org.junit.internal.AssumptionViolatedException;
4646
import org.junit.rules.TemporaryFolder;
4747
import org.objectweb.asm.ClassWriter;
4848
import org.objectweb.asm.Opcodes;
@@ -294,6 +294,13 @@ public void testAnalyzeAll_BrokenGZ() {
294294

295295
@Test
296296
public void testAnalyzeAll_Pack200() throws IOException {
297+
try {
298+
Class.forName("java.util.jar.Pack200");
299+
} catch (ClassNotFoundException e) {
300+
throw new AssumptionViolatedException(
301+
"this test requires JDK with Pack200");
302+
}
303+
297304
final ByteArrayOutputStream zipbuffer = new ByteArrayOutputStream();
298305
final ZipOutputStream zip = new ZipOutputStream(zipbuffer);
299306
zip.putNextEntry(
@@ -303,10 +310,7 @@ public void testAnalyzeAll_Pack200() throws IOException {
303310

304311
final ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
305312
GZIPOutputStream gzipOutput = new GZIPOutputStream(pack200buffer);
306-
Pack200.newPacker()
307-
.pack(new JarInputStream(
308-
new ByteArrayInputStream(zipbuffer.toByteArray())),
309-
gzipOutput);
313+
Pack200Streams.pack(zipbuffer.toByteArray(), gzipOutput);
310314
gzipOutput.finish();
311315

312316
final int count = analyzer.analyzeAll(

org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,21 @@
2626
import java.io.OutputStream;
2727
import java.io.Serializable;
2828
import java.util.Arrays;
29-
import java.util.jar.JarInputStream;
30-
import java.util.jar.JarOutputStream;
31-
import java.util.jar.Pack200;
3229
import java.util.zip.GZIPInputStream;
3330
import java.util.zip.GZIPOutputStream;
3431
import java.util.zip.ZipEntry;
3532
import java.util.zip.ZipInputStream;
3633
import java.util.zip.ZipOutputStream;
3734

3835
import org.jacoco.core.analysis.AnalyzerTest;
36+
import org.jacoco.core.internal.Pack200Streams;
3937
import org.jacoco.core.internal.data.CRC64;
4038
import org.jacoco.core.internal.instr.InstrSupport;
4139
import org.jacoco.core.runtime.IExecutionDataAccessorGenerator;
4240
import org.jacoco.core.test.TargetLoader;
4341
import org.junit.Before;
4442
import org.junit.Test;
43+
import org.junit.internal.AssumptionViolatedException;
4544
import org.objectweb.asm.ClassWriter;
4645
import org.objectweb.asm.MethodVisitor;
4746
import org.objectweb.asm.Opcodes;
@@ -398,6 +397,13 @@ public void testInstrumentAll_BrokenGZ() {
398397

399398
@Test
400399
public void testInstrumentAll_Pack200() throws IOException {
400+
try {
401+
Class.forName("java.util.jar.Pack200");
402+
} catch (ClassNotFoundException e) {
403+
throw new AssumptionViolatedException(
404+
"this test requires JDK with Pack200");
405+
}
406+
401407
ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream();
402408
ZipOutputStream zipout = new ZipOutputStream(jarbuffer);
403409
zipout.putNextEntry(new ZipEntry("Test.class"));
@@ -406,26 +412,18 @@ public void testInstrumentAll_Pack200() throws IOException {
406412

407413
ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
408414
GZIPOutputStream gzipOutput = new GZIPOutputStream(pack200buffer);
409-
Pack200.newPacker()
410-
.pack(new JarInputStream(
411-
new ByteArrayInputStream(jarbuffer.toByteArray())),
412-
gzipOutput);
415+
Pack200Streams.pack(jarbuffer.toByteArray(), gzipOutput);
413416
gzipOutput.finish();
414417

415418
ByteArrayOutputStream out = new ByteArrayOutputStream();
416419
int count = instrumenter.instrumentAll(
417420
new ByteArrayInputStream(pack200buffer.toByteArray()), out,
418421
"Test");
419422

420-
jarbuffer.reset();
421-
Pack200.newUnpacker()
422-
.unpack(new GZIPInputStream(
423-
new ByteArrayInputStream(out.toByteArray())),
424-
new JarOutputStream(jarbuffer));
425-
426423
assertEquals(1, count);
427424
ZipInputStream zipin = new ZipInputStream(
428-
new ByteArrayInputStream(jarbuffer.toByteArray()));
425+
Pack200Streams.unpack(new GZIPInputStream(
426+
new ByteArrayInputStream(out.toByteArray()))));
429427
assertEquals("Test.class", zipin.getNextEntry().getName());
430428
assertNull(zipin.getNextEntry());
431429
}

org.jacoco.core.test/src/org/jacoco/core/internal/ContentTypeDetectorTest.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
22-
import java.util.jar.JarInputStream;
23-
import java.util.jar.Pack200;
2422
import java.util.zip.GZIPOutputStream;
2523
import java.util.zip.ZipEntry;
2624
import java.util.zip.ZipOutputStream;
2725

2826
import org.jacoco.core.test.TargetLoader;
2927
import org.junit.Test;
28+
import org.junit.internal.AssumptionViolatedException;
3029

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

219218
@Test
220219
public void testPack200File() throws IOException {
220+
try {
221+
Class.forName("java.util.jar.Pack200");
222+
} catch (ClassNotFoundException e) {
223+
throw new AssumptionViolatedException(
224+
"this test requires JDK with Pack200");
225+
}
226+
221227
final ByteArrayOutputStream zipbuffer = new ByteArrayOutputStream();
222228
final ZipOutputStream zip = new ZipOutputStream(zipbuffer);
223229
zip.putNextEntry(new ZipEntry("hello.txt"));
224230
zip.write("Hello Zip!".getBytes());
225231
zip.close();
226232

227233
final ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
228-
Pack200.newPacker()
229-
.pack(new JarInputStream(
230-
new ByteArrayInputStream(zipbuffer.toByteArray())),
231-
pack200buffer);
234+
Pack200Streams.pack(zipbuffer.toByteArray(), pack200buffer);
232235
initData(pack200buffer.toByteArray());
233236
assertEquals(ContentTypeDetector.PACK200FILE, detector.getType());
234237
assertContent();

org.jacoco.core.test/src/org/jacoco/core/internal/Pack200StreamsTest.java

+119-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import static org.junit.Assert.assertEquals;
1616
import static org.junit.Assert.assertNull;
17+
import static org.junit.Assert.assertTrue;
1718
import static org.junit.Assert.fail;
1819

1920
import java.io.ByteArrayInputStream;
@@ -25,21 +26,28 @@
2526
import java.io.OutputStream;
2627
import java.util.jar.JarInputStream;
2728
import java.util.jar.JarOutputStream;
28-
import java.util.jar.Pack200;
2929
import java.util.zip.ZipEntry;
3030
import java.util.zip.ZipInputStream;
3131
import java.util.zip.ZipOutputStream;
3232

3333
import org.jacoco.core.test.TargetLoader;
3434
import org.junit.Test;
35+
import org.junit.internal.AssumptionViolatedException;
3536

3637
/**
3738
* Unit tests for {@link Pack200Streams}.
3839
*/
3940
public class Pack200StreamsTest {
4041

4142
@Test
42-
public void testPack() throws IOException {
43+
public void pack_should_pack() throws Exception {
44+
try {
45+
Class.forName("java.util.jar.Pack200");
46+
} catch (ClassNotFoundException e) {
47+
throw new AssumptionViolatedException(
48+
"this test requires JDK with Pack200");
49+
}
50+
4351
ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream();
4452
ZipOutputStream zipout = new ZipOutputStream(jarbuffer);
4553
zipout.putNextEntry(new ZipEntry("Test.class"));
@@ -51,9 +59,13 @@ public void testPack() throws IOException {
5159
new NoCloseOutputStream(pack200buffer));
5260

5361
jarbuffer.reset();
54-
Pack200.newUnpacker().unpack(
55-
new ByteArrayInputStream(pack200buffer.toByteArray()),
56-
new JarOutputStream(jarbuffer));
62+
final Object unpacker = Class.forName("java.util.jar.Pack200")
63+
.getMethod("newUnpacker").invoke(null);
64+
Class.forName("java.util.jar.Pack200$Unpacker")
65+
.getMethod("unpack", InputStream.class, JarOutputStream.class)
66+
.invoke(unpacker,
67+
new ByteArrayInputStream(pack200buffer.toByteArray()),
68+
new JarOutputStream(jarbuffer));
5769

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

6476
@Test
65-
public void testUnpack() throws IOException {
77+
public void pack_should_throw_IOException_when_can_not_write_to_OutputStream() {
78+
try {
79+
Class.forName("java.util.jar.Pack200");
80+
} catch (ClassNotFoundException e) {
81+
throw new AssumptionViolatedException(
82+
"this test requires JDK with Pack200");
83+
}
84+
85+
final OutputStream outputStream = new BrokenOutputStream();
86+
try {
87+
Pack200Streams.pack(new byte[0], outputStream);
88+
fail("expected exception");
89+
} catch (IOException e) {
90+
assertTrue(e.getCause() instanceof IOException);
91+
assertEquals("fake broken output stream",
92+
e.getCause().getMessage());
93+
}
94+
}
95+
96+
@Test
97+
public void pack_should_throw_IOException_when_Pack200_not_available_in_JDK() {
98+
try {
99+
Class.forName("java.util.jar.Pack200");
100+
throw new AssumptionViolatedException(
101+
"this test requires JDK without Pack200");
102+
} catch (ClassNotFoundException ignore) {
103+
}
104+
105+
try {
106+
Pack200Streams.pack(new byte[0], new ByteArrayOutputStream());
107+
fail("expected exception");
108+
} catch (IOException e) {
109+
assertNull(e.getMessage());
110+
assertTrue(e.getCause() instanceof ClassNotFoundException);
111+
}
112+
}
113+
114+
@Test
115+
public void unpack_should_unpack() throws Exception {
116+
try {
117+
Class.forName("java.util.jar.Pack200");
118+
} catch (ClassNotFoundException e) {
119+
throw new AssumptionViolatedException(
120+
"this test requires JDK with Pack200");
121+
}
122+
66123
ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream();
67124
ZipOutputStream zipout = new ZipOutputStream(jarbuffer);
68125
zipout.putNextEntry(new ZipEntry("Test.class"));
69126
zipout.write(TargetLoader.getClassDataAsBytes(getClass()));
70127
zipout.finish();
71128

72129
ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
73-
Pack200.newPacker()
74-
.pack(new JarInputStream(
130+
final Object packer = Class.forName("java.util.jar.Pack200")
131+
.getMethod("newPacker").invoke(null);
132+
Class.forName("java.util.jar.Pack200$Packer")
133+
.getMethod("pack", JarInputStream.class, OutputStream.class)
134+
.invoke(packer, new JarInputStream(
75135
new ByteArrayInputStream(jarbuffer.toByteArray())),
76136
pack200buffer);
77137

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

146+
@Test
147+
public void unpack_should_throw_IOException_when_can_not_read_from_InputStream() {
148+
try {
149+
Class.forName("java.util.jar.Pack200");
150+
} catch (ClassNotFoundException e) {
151+
throw new AssumptionViolatedException(
152+
"this test requires JDK with Pack200");
153+
}
154+
155+
final InputStream inputStream = new BrokenInputStream();
156+
try {
157+
Pack200Streams.unpack(inputStream);
158+
fail("expected exception");
159+
} catch (IOException e) {
160+
assertTrue(e.getCause() instanceof IOException);
161+
assertEquals("fake broken input stream", e.getCause().getMessage());
162+
}
163+
}
164+
165+
@Test
166+
public void unpack_should_throw_IOException_when_Pack200_not_available_in_JDK() {
167+
try {
168+
Class.forName("java.util.jar.Pack200");
169+
throw new AssumptionViolatedException(
170+
"this test requires JDK without Pack200");
171+
} catch (ClassNotFoundException ignore) {
172+
}
173+
174+
try {
175+
Pack200Streams.unpack(new ByteArrayInputStream(new byte[0]));
176+
fail("expected exception");
177+
} catch (IOException e) {
178+
assertNull(e.getMessage());
179+
assertTrue(e.getCause() instanceof ClassNotFoundException);
180+
}
181+
}
182+
86183
static class NoCloseInputStream extends FilterInputStream {
87184
public NoCloseInputStream(InputStream in) {
88185
super(in);
@@ -105,4 +202,18 @@ public void close() throws IOException {
105202
}
106203
}
107204

205+
private static class BrokenInputStream extends InputStream {
206+
@Override
207+
public int read() throws IOException {
208+
throw new IOException("fake broken input stream");
209+
}
210+
}
211+
212+
private static class BrokenOutputStream extends OutputStream {
213+
@Override
214+
public void write(int b) throws IOException {
215+
throw new IOException("fake broken output stream");
216+
}
217+
}
218+
108219
}

0 commit comments

Comments
 (0)