• 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.examples;
14 
15 import static org.hamcrest.CoreMatchers.containsString;
16 import static org.jacoco.examples.ConsoleOutput.containsLine;
17 
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 
22 import org.jacoco.core.data.ExecutionData;
23 import org.jacoco.core.data.ExecutionDataWriter;
24 import org.jacoco.core.data.SessionInfo;
25 import org.junit.Rule;
26 import org.junit.Test;
27 
28 /**
29  * Tests for {@link ExecDump}.
30  */
31 public class ExecDumpTest {
32 
33 	@Rule
34 	public ConsoleOutput console = new ConsoleOutput();
35 
36 	@Test
testRunExample()37 	public void testRunExample() throws Exception {
38 
39 		final String file = createExecFile();
40 		final String[] args = new String[] { file };
41 		new ExecDump(console.stream).execute(args);
42 
43 		console.expect(containsLine("exec file: " + file));
44 		console.expect(
45 				containsLine("CLASS ID         HITS/PROBES   CLASS NAME"));
46 		console.expect(containsString("Session \"testid\":"));
47 		console.expect(
48 				containsLine("0000000000001234    2 of   3   foo/MyClass"));
49 	}
50 
createExecFile()51 	private String createExecFile() throws IOException {
52 		File f = File.createTempFile("jacoco", ".exec");
53 		final FileOutputStream out = new FileOutputStream(f);
54 		final ExecutionDataWriter writer = new ExecutionDataWriter(out);
55 		writer.visitSessionInfo(new SessionInfo("testid", 1, 2));
56 		writer.visitClassExecution(new ExecutionData(0x1234, "foo/MyClass",
57 				new boolean[] { false, true, true }));
58 		writer.flush();
59 		out.close();
60 		return f.getPath();
61 	}
62 }
63