aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/data/ExecutionDataReaderWriterTest.java
blob: 2cc2c97df90021c160e4732c22f7adb56d02b25a (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*******************************************************************************
 * Copyright (c) 2009, 2021 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.data;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Random;

import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for {@link ExecutionDataReader} and {@link ExecutionDataWriter}.
 * The tests don't care about the written binary format, they just verify
 * symmetry.
 */
public class ExecutionDataReaderWriterTest {

	protected ByteArrayOutputStream buffer;

	private ExecutionDataWriter writer;

	private ExecutionDataStore store;

	private SessionInfo sessionInfo;

	private Random random;

	@Before
	public void setup() throws IOException {
		buffer = new ByteArrayOutputStream();
		writer = createWriter(buffer);
		store = new ExecutionDataStore();
		random = new Random(5);
	}

	@Test
	public void testEmpty() throws IOException {
		final ExecutionDataReader reader = createReader();
		reader.setSessionInfoVisitor(new ISessionInfoVisitor() {
			public void visitSessionInfo(final SessionInfo info) {
				fail("No data expected.");
			}
		});
		reader.setExecutionDataVisitor(new IExecutionDataVisitor() {
			public void visitClassExecution(final ExecutionData data) {
				fail("No data expected.");
			}
		});
		assertFalse(reader.read());
	}

	@Test
	public void testFlush() throws IOException {
		final boolean[] flushCalled = new boolean[] { false };
		final OutputStream out = new OutputStream() {
			@Override
			public void write(int b) throws IOException {
			}

			@Override
			public void flush() throws IOException {
				flushCalled[0] = true;
			}
		};
		new ExecutionDataWriter(out).flush();
		assertTrue(flushCalled[0]);
	}

	@Test
	public void testCustomBlocks() throws IOException {
		buffer.write(0x55);
		buffer.write(0x66);
		final ExecutionDataReader reader = new ExecutionDataReader(
				new ByteArrayInputStream(buffer.toByteArray())) {

			@Override
			protected boolean readBlock(byte blocktype) throws IOException {
				switch (blocktype) {
				case 0x55:
					return true;
				case 0x66:
					return false;
				}
				return super.readBlock(blocktype);
			}
		};
		assertTrue(reader.read());
	}

	@Test
	public void testGetFileHeader() {
		byte[] header = ExecutionDataWriter.getFileHeader();
		assertEquals(5, header.length);
		assertEquals(0x01, 0xFF & header[0]);
		assertEquals(0xC0, 0xFF & header[1]);
		assertEquals(0xC0, 0xFF & header[2]);
		final char version = ExecutionDataWriter.FORMAT_VERSION;
		assertEquals(version >> 8, 0xFF & header[3]);
		assertEquals(version & 0xFF, 0xFF & header[4]);
	}

	@Test
	public void testMultipleHeaders() throws IOException {
		new ExecutionDataWriter(buffer);
		new ExecutionDataWriter(buffer);
		new ExecutionDataWriter(buffer);
		assertFalse(createReader().read());
	}

	@Test(expected = IOException.class)
	public void testInvalidMagicNumber() throws IOException {
		buffer = new ByteArrayOutputStream();
		buffer.write(ExecutionDataWriter.BLOCK_HEADER);
		buffer.write(0x12);
		buffer.write(0x34);
		createReader().read();
	}

	@Test(expected = IncompatibleExecDataVersionException.class)
	public void testInvalidVersion() throws IOException {
		buffer = new ByteArrayOutputStream();
		buffer.write(ExecutionDataWriter.BLOCK_HEADER);
		buffer.write(0xC0);
		buffer.write(0xC0);
		final char version = (char) (ExecutionDataWriter.FORMAT_VERSION - 1);
		buffer.write(version >> 8);
		buffer.write(version & 0xFF);
		createReader().read();
	}

	@Test(expected = IOException.class)
	public void testMissingHeader() throws IOException {
		buffer.reset();
		writer.visitClassExecution(
				new ExecutionData(Long.MIN_VALUE, "Sample", createData(8)));
		createReaderWithVisitors().read();
	}

	@Test(expected = IOException.class)
	public void testUnknownBlock() throws IOException {
		buffer.write(0xff);
		createReader().read();
	}

	@Test(expected = EOFException.class)
	public void testTruncatedFile() throws IOException {
		writer.visitClassExecution(
				new ExecutionData(Long.MIN_VALUE, "Sample", createData(8)));
		final byte[] content = buffer.toByteArray();
		buffer.reset();
		buffer.write(content, 0, content.length - 1);
		createReaderWithVisitors().read();
	}

	@Test
	public void testEmptyFile() throws IOException {
		buffer.reset();
		createReader().read();
	}

	// === Session Info ===

	@Test(expected = IOException.class)
	public void testNoSessionInfoVisitor() throws IOException {
		writer.visitSessionInfo(new SessionInfo("x", 0, 1));
		createReader().read();
	}

	@Test
	public void testSessionInfo() throws IOException {
		writer.visitSessionInfo(new SessionInfo("TestSession",
				2837123124567891234L, 3444234223498879234L));
		assertFalse(createReaderWithVisitors().read());
		assertNotNull(sessionInfo);
		assertEquals("TestSession", sessionInfo.getId());
		assertEquals(2837123124567891234L, sessionInfo.getStartTimeStamp());
		assertEquals(3444234223498879234L, sessionInfo.getDumpTimeStamp());
	}

	@Test(expected = RuntimeException.class)
	public void testSessionInfoIOException() throws IOException {
		final boolean[] broken = new boolean[1];
		final ExecutionDataWriter writer = createWriter(new OutputStream() {
			@Override
			public void write(int b) throws IOException {
				if (broken[0]) {
					throw new IOException();
				}
			}
		});
		broken[0] = true;
		writer.visitSessionInfo(new SessionInfo("X", 0, 0));
	}

	// === Execution Data ===

	@Test(expected = IOException.class)
	public void testNoExecutionDataVisitor() throws IOException {
		writer.visitClassExecution(
				new ExecutionData(Long.MIN_VALUE, "Sample", createData(8)));
		createReader().read();
	}

	@Test
	public void testMinClassId() throws IOException {
		final boolean[] data = createData(8);
		writer.visitClassExecution(
				new ExecutionData(Long.MIN_VALUE, "Sample", data));
		assertFalse(createReaderWithVisitors().read());
		assertArrayEquals(data, store.get(Long.MIN_VALUE).getProbes());
	}

	@Test
	public void testMaxClassId() throws IOException {
		final boolean[] data = createData(8);
		writer.visitClassExecution(
				new ExecutionData(Long.MAX_VALUE, "Sample", data));
		assertFalse(createReaderWithVisitors().read());
		assertArrayEquals(data, store.get(Long.MAX_VALUE).getProbes());
	}

	@Test
	public void testEmptyClass() throws IOException {
		final boolean[] data = createData(0);
		writer.visitClassExecution(new ExecutionData(3, "Sample", data));
		assertFalse(createReaderWithVisitors().read());
		assertTrue(store.getContents().isEmpty());
	}

	@Test
	public void testNoHitClass() throws IOException {
		final boolean[] data = new boolean[] { false, false, false };
		writer.visitClassExecution(new ExecutionData(3, "Sample", data));
		assertFalse(createReaderWithVisitors().read());
		assertTrue(store.getContents().isEmpty());
	}

	@Test
	public void testOneClass() throws IOException {
		final boolean[] data = createData(15);
		writer.visitClassExecution(new ExecutionData(3, "Sample", data));
		assertFalse(createReaderWithVisitors().read());
		assertArrayEquals(data, store.get(3).getProbes());
	}

	@Test
	public void testTwoClasses() throws IOException {
		final boolean[] data1 = createData(15);
		final boolean[] data2 = createData(185);
		writer.visitClassExecution(new ExecutionData(333, "Sample", data1));
		writer.visitClassExecution(new ExecutionData(-45, "Sample", data2));
		assertFalse(createReaderWithVisitors().read());
		assertArrayEquals(data1, store.get(333).getProbes());
		assertArrayEquals(data2, store.get(-45).getProbes());
	}

	@Test
	public void testBigClass() throws IOException {
		final boolean[] data = createData(3599);
		writer.visitClassExecution(new ExecutionData(123, "Sample", data));
		assertFalse(createReaderWithVisitors().read());
		assertArrayEquals(data, store.get(123).getProbes());
	}

	@Test(expected = RuntimeException.class)
	public void testExecutionDataIOException() throws IOException {
		final boolean[] broken = new boolean[1];
		final ExecutionDataWriter writer = createWriter(new OutputStream() {
			@Override
			public void write(int b) throws IOException {
				if (broken[0]) {
					throw new IOException();
				}
			}
		});
		broken[0] = true;
		writer.visitClassExecution(
				new ExecutionData(3, "Sample", createData(1)));
	}

	private ExecutionDataReader createReaderWithVisitors() throws IOException {
		final ExecutionDataReader reader = createReader();
		reader.setExecutionDataVisitor(store);
		reader.setSessionInfoVisitor(new ISessionInfoVisitor() {
			public void visitSessionInfo(SessionInfo info) {
				sessionInfo = info;
			}
		});
		return reader;
	}

	private boolean[] createData(final int probeCount) {
		final boolean[] data = new boolean[probeCount];
		for (int j = 0; j < data.length; j++) {
			data[j] = random.nextBoolean();
		}
		return data;
	}

	private void assertArrayEquals(final boolean[] expected,
			final boolean[] actual) {
		assertTrue(Arrays.equals(expected, actual));
	}

	protected ExecutionDataWriter createWriter(OutputStream out)
			throws IOException {
		return new ExecutionDataWriter(out);
	}

	protected ExecutionDataReader createReader() throws IOException {
		return new ExecutionDataReader(
				new ByteArrayInputStream(buffer.toByteArray()));
	}

}