• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Marc R. Hoffmann - initial API and implementation
11  *
12  *******************************************************************************/
13 package org.jacoco.report;
14 
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertNull;
19 import static org.junit.Assert.assertTrue;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31 
32 /**
33  * In-memory report output for test purposes.
34  */
35 public class MemoryMultiReportOutput implements IMultiReportOutput {
36 
37 	private final Map<String, ByteArrayOutputStream> files = new HashMap<String, ByteArrayOutputStream>();
38 
39 	private final Set<String> open = new HashSet<String>();
40 
41 	private boolean closed = false;
42 
createFile(final String path)43 	public OutputStream createFile(final String path) throws IOException {
44 		assertFalse("Duplicate output " + path, files.containsKey(path));
45 		open.add(path);
46 		final ByteArrayOutputStream out = new ByteArrayOutputStream() {
47 			@Override
48 			public void close() throws IOException {
49 				open.remove(path);
50 				super.close();
51 			}
52 		};
53 		files.put(path, out);
54 		return out;
55 	}
56 
close()57 	public void close() throws IOException {
58 		closed = true;
59 	}
60 
assertEmpty()61 	public void assertEmpty() {
62 		assertEquals(Collections.emptySet(), files.keySet());
63 	}
64 
assertFile(String path)65 	public void assertFile(String path) {
66 		assertNotNull(String.format("Missing file %s. Actual files are %s.",
67 				path, files.keySet()), files.get(path));
68 	}
69 
assertNoFile(String path)70 	public void assertNoFile(String path) {
71 		assertNull(String.format("Unexpected file %s.", path), files.get(path));
72 	}
73 
assertSingleFile(String path)74 	public void assertSingleFile(String path) {
75 		assertEquals(Collections.singleton(path), files.keySet());
76 	}
77 
getFile(String path)78 	public byte[] getFile(String path) {
79 		assertFile(path);
80 		return files.get(path).toByteArray();
81 	}
82 
getFileAsStream(String path)83 	public InputStream getFileAsStream(String path) {
84 		return new ByteArrayInputStream(getFile(path));
85 	}
86 
assertAllClosed()87 	public void assertAllClosed() {
88 		assertEquals(Collections.emptySet(), open);
89 		assertTrue(closed);
90 	}
91 
92 }
93