blob: ee0018eb521eeeb1e8611d2ca342be7951dea695 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*******************************************************************************
* Copyright (c) 2009, 2015 Mountainminds GmbH & Co. KG and Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
package org.jacoco.core.internal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
/**
* Unit tests for {@link Pack200Streams}.
*/
public class Pack200StreamsTest {
@Test
public void testPack() throws IOException {
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();
Pack200Streams.pack(jarbuffer.toByteArray(), new NoCloseOutputStream(
pack200buffer));
jarbuffer.reset();
Pack200.newUnpacker().unpack(
new ByteArrayInputStream(pack200buffer.toByteArray()),
new JarOutputStream(jarbuffer));
ZipInputStream zipin = new ZipInputStream(new ByteArrayInputStream(
jarbuffer.toByteArray()));
assertEquals("Test.class", zipin.getNextEntry().getName());
assertNull(zipin.getNextEntry());
}
@Test
public void testUnpack() throws IOException {
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(new ByteArrayInputStream(
jarbuffer.toByteArray())), pack200buffer);
InputStream result = Pack200Streams.unpack(new NoCloseInputStream(
new ByteArrayInputStream(pack200buffer.toByteArray())));
ZipInputStream zipin = new ZipInputStream(result);
assertEquals("Test.class", zipin.getNextEntry().getName());
assertNull(zipin.getNextEntry());
}
static class NoCloseInputStream extends FilterInputStream {
public NoCloseInputStream(InputStream in) {
super(in);
}
@Override
public void close() throws IOException {
fail();
}
}
static class NoCloseOutputStream extends FilterOutputStream {
public NoCloseOutputStream(OutputStream out) {
super(out);
}
@Override
public void close() throws IOException {
fail();
}
}
}
|