1/*******************************************************************************
2 * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *    Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.cli.internal.commands;
13
14import static org.junit.Assert.assertEquals;
15
16import java.io.File;
17import java.io.FileOutputStream;
18import java.io.IOException;
19import java.util.Arrays;
20import java.util.Collections;
21import java.util.HashSet;
22import java.util.Set;
23
24import org.jacoco.cli.internal.CommandTestBase;
25import org.jacoco.core.data.ExecutionData;
26import org.jacoco.core.data.ExecutionDataWriter;
27import org.jacoco.core.tools.ExecFileLoader;
28import org.junit.Rule;
29import org.junit.Test;
30import org.junit.rules.TemporaryFolder;
31
32/**
33 * Unit tests for {@link Merge}.
34 */
35public class MergeTest extends CommandTestBase {
36
37	@Rule
38	public TemporaryFolder tmp = new TemporaryFolder();
39
40	@Test
41	public void should_print_usage_when_no_options_are_given()
42			throws Exception {
43		execute("merge");
44
45		assertFailure();
46		assertContains("Option \"--destfile\" is required", err);
47		assertContains("java -jar jacococli.jar merge [<execfiles> ...]", err);
48	}
49
50	@Test
51	public void should_print_warning_when_no_exec_files_are_provided()
52			throws Exception {
53		File dest = new File(tmp.getRoot(), "merged.exec");
54		execute("merge", "--destfile", dest.getAbsolutePath());
55
56		assertOk();
57		assertContains("[WARN] No execution data files provided.", out);
58		Set<String> names = loadExecFile(dest);
59		assertEquals(Collections.emptySet(), names);
60	}
61
62	@Test
63	public void should_merge_exec_files() throws Exception {
64		File a = createExecFile("a");
65		File b = createExecFile("b");
66		File c = createExecFile("c");
67		File dest = new File(tmp.getRoot(), "merged.exec");
68
69		execute("merge", "--destfile", dest.getAbsolutePath(),
70				a.getAbsolutePath(), b.getAbsolutePath(), c.getAbsolutePath());
71
72		assertOk();
73		Set<String> names = loadExecFile(dest);
74		assertEquals(new HashSet<String>(Arrays.asList("a", "b", "c")), names);
75	}
76
77	private File createExecFile(String name) throws IOException {
78		File file = new File(tmp.getRoot(), name + ".exec");
79		final FileOutputStream execout = new FileOutputStream(file);
80		ExecutionDataWriter writer = new ExecutionDataWriter(execout);
81		writer.visitClassExecution(new ExecutionData(name.hashCode(), name,
82				new boolean[] { true }));
83		execout.close();
84		return file;
85	}
86
87	private Set<String> loadExecFile(File file) throws IOException {
88		ExecFileLoader loader = new ExecFileLoader();
89		loader.load(file);
90		Set<String> names = new HashSet<String>();
91		for (ExecutionData d : loader.getExecutionDataStore().getContents()) {
92			names.add(d.getName());
93		}
94		return names;
95	}
96
97}
98