aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/internal/Pack200StreamsTest.java
blob: 7905d3b9d3b1b368ac177555629554f27fa87e11 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*******************************************************************************
 * Copyright (c) 2009, 2023 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * 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.assertTrue;
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.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

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

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

	@Test
	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"));
		zipout.write(TargetLoader.getClassDataAsBytes(getClass()));
		zipout.finish();

		ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
		Pack200Streams.pack(jarbuffer.toByteArray(),
				new NoCloseOutputStream(pack200buffer));

		jarbuffer.reset();
		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()));
		assertEquals("Test.class", zipin.getNextEntry().getName());
		assertNull(zipin.getNextEntry());
	}

	@Test
	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();
		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);

		InputStream result = Pack200Streams.unpack(new NoCloseInputStream(
				new ByteArrayInputStream(pack200buffer.toByteArray())));

		ZipInputStream zipin = new ZipInputStream(result);
		assertEquals("Test.class", zipin.getNextEntry().getName());
		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);
		}

		@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();
		}
	}

	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");
		}
	}

}