1/*******************************************************************************
2 * Copyright (c) 2009, 2017 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.assertTrue;
15
16import java.io.File;
17import java.io.FileOutputStream;
18
19import org.jacoco.cli.internal.CommandTestBase;
20import org.jacoco.core.data.ExecutionData;
21import org.jacoco.core.data.ExecutionDataWriter;
22import org.junit.Rule;
23import org.junit.Test;
24import org.junit.rules.TemporaryFolder;
25
26/**
27 * Unit tests for {@link Report}.
28 */
29public class ReportTest extends CommandTestBase {
30
31	@Rule
32	public TemporaryFolder tmp = new TemporaryFolder();
33
34	@Test
35	public void should_print_usage_when_no_options_are_given()
36			throws Exception {
37		execute("report");
38
39		assertFailure();
40		assertContains("Option \"-classfiles\" is required", err);
41		assertContains(
42				"Usage: java -jar jacococli.jar report [<execfiles> ...]", err);
43	}
44
45	@Test
46	public void should_print_number_of_analyzed_classes() throws Exception {
47		execute("report", "-classfiles", getClassPath());
48
49		assertOk();
50		assertContains("[INFO] Writing report with 14 classes.", out);
51	}
52
53	@Test
54	public void should_print_warning_when_exec_data_does_not_match()
55			throws Exception {
56		File exec = new File(tmp.getRoot(), "jacoco.exec");
57		final FileOutputStream execout = new FileOutputStream(exec);
58		ExecutionDataWriter writer = new ExecutionDataWriter(execout);
59		// Add probably invalid id for this test class:
60		writer.visitClassExecution(
61				new ExecutionData(0x123, getClass().getName().replace('.', '/'),
62						new boolean[] { true }));
63		execout.close();
64
65		execute("report", exec.getAbsolutePath(), "-classfiles",
66				getClassPath());
67
68		assertOk();
69		assertContains("[WARN] Some classes do not match with execution data.",
70				out);
71		assertContains(
72				"[WARN] For report generation the same class files must be used as at runtime.",
73				out);
74		assertContains(
75				"[WARN] Execution data for class org/jacoco/cli/internal/commands/ReportTest does not match.",
76				out);
77	}
78
79	@Test
80	public void should_create_xml_report_when_xml_option_is_provided()
81			throws Exception {
82		File xml = new File(tmp.getRoot(), "coverage.xml");
83
84		execute("report", "-classfiles", getClassPath(), "-xml",
85				xml.getAbsolutePath());
86
87		assertOk();
88		assertTrue(xml.isFile());
89	}
90
91	@Test
92	public void should_create_csv_report_when_csv_option_is_provided()
93			throws Exception {
94		File csv = new File(tmp.getRoot(), "coverage.csv");
95
96		execute("report", "-classfiles", getClassPath(), "-csv",
97				csv.getAbsolutePath());
98
99		assertOk();
100		assertTrue(csv.isFile());
101	}
102
103	@Test
104	public void should_create_html_report_when_html_option_is_provided()
105			throws Exception {
106		File html = new File(tmp.getRoot(), "coverage");
107
108		execute("report", "-classfiles", getClassPath(), "-sourcefiles",
109				"./src", "-html", html.getAbsolutePath());
110
111		assertOk();
112		assertTrue(html.isDirectory());
113		assertTrue(new File(html,
114				"org.jacoco.cli.internal.commands/ReportTest.html").isFile());
115		assertTrue(new File(html,
116				"org.jacoco.cli.internal.commands/ReportTest.java.html")
117						.isFile());
118	}
119
120	@Test
121	public void should_use_all_values_when_multiple_classfiles_options_are_provided()
122			throws Exception {
123		File html = new File(tmp.getRoot(), "coverage");
124
125		final String c1 = getClassPath()
126				+ "/org/jacoco/cli/internal/commands/ReportTest.class";
127		final String c2 = getClassPath()
128				+ "/org/jacoco/cli/internal/commands/DumpTest.class";
129
130		execute("report", "-classfiles", c1, "-classfiles", c2, "-html",
131				html.getAbsolutePath());
132
133		assertOk();
134		assertTrue(html.isDirectory());
135		assertTrue(new File(html,
136				"org.jacoco.cli.internal.commands/ReportTest.html").isFile());
137		assertTrue(
138				new File(html, "org.jacoco.cli.internal.commands/DumpTest.html")
139						.isFile());
140	}
141
142}
143