1 /*******************************************************************************
2  * Copyright (c) 2009, 2015 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  *    Evgeny Mandrikov - initial API and implementation
10  *    Kyle Lieber - implementation of CheckMojo
11  *
12  *******************************************************************************/
13 package org.jacoco.maven;
14 
15 import static java.lang.String.format;
16 
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.Collection;
20 import java.util.List;
21 
22 import org.apache.maven.plugin.logging.Log;
23 import org.apache.maven.project.MavenProject;
24 import org.codehaus.plexus.util.FileUtils;
25 import org.jacoco.core.analysis.Analyzer;
26 import org.jacoco.core.analysis.CoverageBuilder;
27 import org.jacoco.core.analysis.IBundleCoverage;
28 import org.jacoco.core.analysis.IClassCoverage;
29 import org.jacoco.core.data.ExecutionDataStore;
30 
31 /**
32  * Creates an IBundleCoverage.
33  */
34 public final class BundleCreator {
35 
36 	private final MavenProject project;
37 	private final FileFilter fileFilter;
38 	private final Log log;
39 
40 	/**
41 	 * Construct a new BundleCreator given the MavenProject and FileFilter.
42 	 *
43 	 * @param project
44 	 *            the MavenProject
45 	 * @param fileFilter
46 	 *            the FileFilter
47 	 * @param log
48 	 *            for log output
49 	 */
BundleCreator(final MavenProject project, final FileFilter fileFilter, final Log log)50 	public BundleCreator(final MavenProject project,
51 			final FileFilter fileFilter, final Log log) {
52 		this.project = project;
53 		this.fileFilter = fileFilter;
54 		this.log = log;
55 	}
56 
57 	/**
58 	 * Create an IBundleCoverage for the given ExecutionDataStore.
59 	 *
60 	 * @param executionDataStore
61 	 *            the execution data.
62 	 * @return the coverage data.
63 	 * @throws IOException
64 	 *             if class files can't be read
65 	 */
createBundle( final ExecutionDataStore executionDataStore)66 	public IBundleCoverage createBundle(
67 			final ExecutionDataStore executionDataStore) throws IOException {
68 		final CoverageBuilder builder = new CoverageBuilder();
69 		final Analyzer analyzer = new Analyzer(executionDataStore, builder);
70 		final File classesDir = new File(this.project.getBuild()
71 				.getOutputDirectory());
72 
73 		@SuppressWarnings("unchecked")
74 		final List<File> filesToAnalyze = FileUtils.getFiles(classesDir,
75 				fileFilter.getIncludes(), fileFilter.getExcludes());
76 
77 		for (final File file : filesToAnalyze) {
78 			analyzer.analyzeAll(file);
79 		}
80 
81 		final IBundleCoverage bundle = builder
82 				.getBundle(this.project.getName());
83 		logBundleInfo(bundle, builder.getNoMatchClasses());
84 
85 		return bundle;
86 	}
87 
logBundleInfo(final IBundleCoverage bundle, final Collection<IClassCoverage> nomatch)88 	private void logBundleInfo(final IBundleCoverage bundle,
89 			final Collection<IClassCoverage> nomatch) {
90 		log.info(format("Analyzed bundle '%s' with %s classes",
91 				bundle.getName(),
92 				Integer.valueOf(bundle.getClassCounter().getTotalCount())));
93 		if (!nomatch.isEmpty()) {
94 			log.warn(format(
95 					"Classes in bundle '%s' do no match with execution data. "
96 							+ "For report generation the same class files must be used as at runtime.",
97 					bundle.getName()));
98 			for (final IClassCoverage c : nomatch) {
99 				log.warn(format("Execution data for class %s does not match.",
100 						c.getName()));
101 			}
102 		}
103 	}
104 
105 }
106