aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/MemoryMultiReportOutput.java
blob: 1af76cc80a207b203ba340d97aa3853a16d8741d (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
/*******************************************************************************
 * 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.report;

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * In-memory report output for test purposes.
 */
public class MemoryMultiReportOutput implements IMultiReportOutput {

	private final Map<String, ByteArrayOutputStream> files = new HashMap<String, ByteArrayOutputStream>();

	private final Set<String> open = new HashSet<String>();

	private boolean closed = false;

	public OutputStream createFile(final String path) throws IOException {
		assertFalse("Duplicate output " + path, files.containsKey(path));
		open.add(path);
		final ByteArrayOutputStream out = new ByteArrayOutputStream() {
			@Override
			public void close() throws IOException {
				open.remove(path);
				super.close();
			}
		};
		files.put(path, out);
		return out;
	}

	public void close() throws IOException {
		closed = true;
	}

	public void assertEmpty() {
		assertEquals(Collections.emptySet(), files.keySet());
	}

	public void assertFile(String path) {
		assertNotNull(String.format("Missing file %s. Actual files are %s.",
				path, files.keySet()), files.get(path));
	}

	public void assertNoFile(String path) {
		assertNull(String.format("Unexpected file %s.", path), files.get(path));
	}

	public void assertSingleFile(String path) {
		assertEquals(Collections.singleton(path), files.keySet());
	}

	public byte[] getFile(String path) {
		assertFile(path);
		return files.get(path).toByteArray();
	}

	public InputStream getFileAsStream(String path) {
		return new ByteArrayInputStream(getFile(path));
	}

	public void assertAllClosed() {
		assertEquals(Collections.emptySet(), open);
		assertTrue(closed);
	}

}