aboutsummaryrefslogtreecommitdiff
path: root/jacoco-maven-plugin/src/org/jacoco/maven/ReportSupport.java
blob: 1cdf847c1de3a7780b79bdb57c525689ec25e5c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*******************************************************************************
 * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Evgeny Mandrikov - initial API and implementation
 *    Kyle Lieber - implementation of CheckMojo
 *
 *******************************************************************************/
package org.jacoco.maven;

import static java.lang.String.format;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.jacoco.core.analysis.Analyzer;
import org.jacoco.core.analysis.CoverageBuilder;
import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.tools.ExecFileLoader;
import org.jacoco.report.FileMultiReportOutput;
import org.jacoco.report.IReportGroupVisitor;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.ISourceFileLocator;
import org.jacoco.report.MultiReportVisitor;
import org.jacoco.report.check.IViolationsOutput;
import org.jacoco.report.check.Rule;
import org.jacoco.report.check.RulesChecker;
import org.jacoco.report.csv.CSVFormatter;
import org.jacoco.report.html.HTMLFormatter;
import org.jacoco.report.xml.XMLFormatter;

/**
 * Encapsulates the tasks to create reports for Maven projects. Instances are
 * supposed to be used in the following sequence:
 * 
 * <ol>
 * <li>Create an instance</li>
 * <li>Load one or multiple exec files with <code>loadExecutionData()</code></li>
 * <li>Add one or multiple formatters with <code>addXXX()</code> methods</li>
 * <li>Create the root visitor with <code>initRootVisitor()</code></li>
 * <li>Process one or multiple projects with <code>processProject()</code></li>
 * </ol>
 */
final class ReportSupport {

	private final Log log;
	private final ExecFileLoader loader;
	private final List<IReportVisitor> formatters;

	/**
	 * Construct a new instance with the given log output.
	 * 
	 * @param log
	 *            for log output
	 */
	public ReportSupport(final Log log) {
		this.log = log;
		this.loader = new ExecFileLoader();
		this.formatters = new ArrayList<IReportVisitor>();
	}

	/**
	 * Loads the given execution data file.
	 * 
	 * @param execFile
	 *            execution data file to load
	 * @throws IOException
	 *             if the file can't be loaded
	 */
	public void loadExecutionData(final File execFile) throws IOException {
		log.info("Loading execution data file " + execFile);
		loader.load(execFile);
	}

	public void addXmlFormatter(final File targetfile, final String encoding)
			throws IOException {
		final XMLFormatter xml = new XMLFormatter();
		xml.setOutputEncoding(encoding);
		formatters.add(xml.createVisitor(new FileOutputStream(targetfile)));
	}

	public void addCsvFormatter(final File targetfile, final String encoding)
			throws IOException {
		final CSVFormatter csv = new CSVFormatter();
		csv.setOutputEncoding(encoding);
		formatters.add(csv.createVisitor(new FileOutputStream(targetfile)));
	}

	public void addHtmlFormatter(final File targetdir, final String encoding,
			final String footer, final Locale locale) throws IOException {
		final HTMLFormatter htmlFormatter = new HTMLFormatter();
		htmlFormatter.setOutputEncoding(encoding);
		htmlFormatter.setLocale(locale);
		if (footer != null) {
			htmlFormatter.setFooterText(footer);
		}
		formatters.add(htmlFormatter.createVisitor(new FileMultiReportOutput(
				targetdir)));
	}

	public void addAllFormatters(final File targetdir, final String encoding,
			final String footer, final Locale locale) throws IOException {
		targetdir.mkdirs();
		addXmlFormatter(new File(targetdir, "jacoco.xml"), encoding);
		addCsvFormatter(new File(targetdir, "jacoco.csv"), encoding);
		addHtmlFormatter(targetdir, encoding, footer, locale);
	}

	public void addRulesChecker(final List<Rule> rules,
			final IViolationsOutput output) {
		final RulesChecker checker = new RulesChecker();
		checker.setRules(rules);
		formatters.add(checker.createVisitor(output));
	}

	public IReportVisitor initRootVisitor() throws IOException {
		final IReportVisitor visitor = new MultiReportVisitor(formatters);
		visitor.visitInfo(loader.getSessionInfoStore().getInfos(), loader
				.getExecutionDataStore().getContents());
		return visitor;
	}

	/**
	 * Calculates coverage for the given project and emits it to the report
	 * group without source references
	 * 
	 * @param visitor
	 *            group visitor to emit the project's coverage to
	 * @param project
	 *            the MavenProject
	 * @param includes
	 *            list of includes patterns
	 * @param excludes
	 *            list of excludes patterns
	 * @throws IOException
	 *             if class files can't be read
	 */
	public void processProject(final IReportGroupVisitor visitor,
			final MavenProject project, final List<String> includes,
			final List<String> excludes) throws IOException {
		processProject(visitor, project.getArtifactId(), project, includes,
				excludes, new NoSourceLocator());
	}

	/**
	 * Calculates coverage for the given project and emits it to the report
	 * group including source references
	 * 
	 * @param visitor
	 *            group visitor to emit the project's coverage to
	 * @param bundeName
	 *            name for this project in the report
	 * @param project
	 *            the MavenProject
	 * @param includes
	 *            list of includes patterns
	 * @param excludes
	 *            list of excludes patterns
	 * @param srcEncoding
	 *            encoding of the source files within this project
	 * @throws IOException
	 *             if class files can't be read
	 */
	public void processProject(final IReportGroupVisitor visitor,
			final String bundeName, final MavenProject project,
			final List<String> includes, final List<String> excludes,
			final String srcEncoding) throws IOException {
		processProject(visitor, bundeName, project, includes, excludes,
				new SourceFileCollection(project, srcEncoding));
	}

	private void processProject(final IReportGroupVisitor visitor,
			final String bundeName, final MavenProject project,
			final List<String> includes, final List<String> excludes,
			final ISourceFileLocator locator) throws IOException {
		final CoverageBuilder builder = new CoverageBuilder();
		final File classesDir = new File(project.getBuild()
				.getOutputDirectory());

		if (classesDir.isDirectory()) {
			final Analyzer analyzer = new Analyzer(
					loader.getExecutionDataStore(), builder);
			final FileFilter filter = new FileFilter(includes, excludes);
			for (final File file : filter.getFiles(classesDir)) {
				analyzer.analyzeAll(file);
			}
		}

		final IBundleCoverage bundle = builder.getBundle(bundeName);
		logBundleInfo(bundle, builder.getNoMatchClasses());

		visitor.visitBundle(bundle, locator);
	}

	private void logBundleInfo(final IBundleCoverage bundle,
			final Collection<IClassCoverage> nomatch) {
		log.info(format("Analyzed bundle '%s' with %s classes",
				bundle.getName(),
				Integer.valueOf(bundle.getClassCounter().getTotalCount())));
		if (!nomatch.isEmpty()) {
			log.warn(format(
					"Classes in bundle '%s' do no match with execution data. "
							+ "For report generation the same class files must be used as at runtime.",
					bundle.getName()));
			for (final IClassCoverage c : nomatch) {
				log.warn(format("Execution data for class %s does not match.",
						c.getName()));
			}
		}
		if (bundle.containsCode()
				&& bundle.getLineCounter().getTotalCount() == 0) {
			log.warn(
					"To enable source code annotation class files have to be compiled with debug information.");
		}
	}

	private class NoSourceLocator implements ISourceFileLocator {

		public Reader getSourceFile(final String packageName,
				final String fileName) {
			return null;
		}

		public int getTabWidth() {
			return 0;
		}
	}

	private class SourceFileCollection implements ISourceFileLocator {

		private final List<File> sourceRoots;
		private final String encoding;

		public SourceFileCollection(final MavenProject project,
				final String encoding) {
			this.sourceRoots = getCompileSourceRoots(project);
			this.encoding = encoding;
		}

		public Reader getSourceFile(final String packageName,
				final String fileName) throws IOException {
			final String r;
			if (packageName.length() > 0) {
				r = packageName + '/' + fileName;
			} else {
				r = fileName;
			}
			for (final File sourceRoot : sourceRoots) {
				final File file = new File(sourceRoot, r);
				if (file.exists() && file.isFile()) {
					return new InputStreamReader(new FileInputStream(file),
							encoding);
				}
			}
			return null;
		}

		public int getTabWidth() {
			return 4;
		}
	}

	private static List<File> getCompileSourceRoots(final MavenProject project) {
		final List<File> result = new ArrayList<File>();
		for (final Object path : project.getCompileSourceRoots()) {
			result.add(resolvePath(project, (String) path));
		}
		return result;
	}

	private static File resolvePath(final MavenProject project,
			final String path) {
		File file = new File(path);
		if (!file.isAbsolute()) {
			file = new File(project.getBasedir(), path);
		}
		return file;
	}

}