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.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_warning_when_no_exec_files_are_provided() 47 throws Exception { 48 execute("report", "--classfiles", getClassPath()); 49 50 assertOk(); 51 assertContains("[WARN] No execution data files provided.", out); 52 } 53 54 @Test 55 public void should_print_number_of_analyzed_classes() throws Exception { 56 execute("report", "--classfiles", getClassPath()); 57 58 assertOk(); 59 assertContains("[INFO] Analyzing 14 classes.", out); 60 } 61 62 @Test 63 public void should_print_warning_when_exec_data_does_not_match() 64 throws Exception { 65 File exec = new File(tmp.getRoot(), "jacoco.exec"); 66 final FileOutputStream execout = new FileOutputStream(exec); 67 ExecutionDataWriter writer = new ExecutionDataWriter(execout); 68 // Add probably invalid id for this test class: 69 writer.visitClassExecution( 70 new ExecutionData(0x123, getClass().getName().replace('.', '/'), 71 new boolean[] { true })); 72 execout.close(); 73 74 execute("report", exec.getAbsolutePath(), "--classfiles", 75 getClassPath()); 76 77 assertOk(); 78 assertContains("[WARN] Some classes do not match with execution data.", 79 out); 80 assertContains( 81 "[WARN] For report generation the same class files must be used as at runtime.", 82 out); 83 assertContains( 84 "[WARN] Execution data for class org/jacoco/cli/internal/commands/ReportTest does not match.", 85 out); 86 } 87 88 @Test 89 public void should_create_xml_report_when_xml_option_is_provided() 90 throws Exception { 91 File xml = new File(tmp.getRoot(), "coverage.xml"); 92 93 execute("report", "--classfiles", getClassPath(), "--xml", 94 xml.getAbsolutePath()); 95 96 assertOk(); 97 assertTrue(xml.isFile()); 98 } 99 100 @Test 101 public void should_create_csv_report_when_csv_option_is_provided() 102 throws Exception { 103 File csv = new File(tmp.getRoot(), "coverage.csv"); 104 105 execute("report", "--classfiles", getClassPath(), "--csv", 106 csv.getAbsolutePath()); 107 108 assertOk(); 109 assertTrue(csv.isFile()); 110 } 111 112 @Test 113 public void should_create_html_report_when_html_option_is_provided() 114 throws Exception { 115 File html = new File(tmp.getRoot(), "coverage"); 116 117 execute("report", "--classfiles", getClassPath(), "--sourcefiles", 118 "./src", "--html", html.getAbsolutePath()); 119 120 assertOk(); 121 assertTrue(html.isDirectory()); 122 assertTrue(new File(html, 123 "org.jacoco.cli.internal.commands/ReportTest.html").isFile()); 124 assertTrue(new File(html, 125 "org.jacoco.cli.internal.commands/ReportTest.java.html") 126 .isFile()); 127 } 128 129 @Test 130 public void should_use_all_values_when_multiple_classfiles_options_are_provided() 131 throws Exception { 132 File html = new File(tmp.getRoot(), "coverage"); 133 134 final String c1 = getClassPath() 135 + "/org/jacoco/cli/internal/commands/ReportTest.class"; 136 final String c2 = getClassPath() 137 + "/org/jacoco/cli/internal/commands/DumpTest.class"; 138 139 execute("report", "--classfiles", c1, "--classfiles", c2, "--html", 140 html.getAbsolutePath()); 141 142 assertOk(); 143 assertTrue(html.isDirectory()); 144 assertTrue(new File(html, 145 "org.jacoco.cli.internal.commands/ReportTest.html").isFile()); 146 assertTrue( 147 new File(html, "org.jacoco.cli.internal.commands/DumpTest.html") 148 .isFile()); 149 } 150 151} 152