001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.plugins.jacoco;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.jacoco.core.analysis.*;
024 import org.jacoco.core.data.ExecutionDataReader;
025 import org.jacoco.core.data.ExecutionDataStore;
026 import org.jacoco.core.data.SessionInfoStore;
027 import org.sonar.api.batch.SensorContext;
028 import org.sonar.api.measures.CoverageMeasuresBuilder;
029 import org.sonar.api.measures.Measure;
030 import org.sonar.api.resources.JavaFile;
031 import org.sonar.api.resources.Project;
032 import org.sonar.api.utils.SonarException;
033
034 import java.io.File;
035 import java.io.FileInputStream;
036 import java.io.IOException;
037 import java.util.Collection;
038
039 /**
040 * @author Evgeny Mandrikov
041 */
042 public abstract class AbstractAnalyzer {
043
044 public final void analyse(Project project, SensorContext context) {
045 final File buildOutputDir = project.getFileSystem().getBuildOutputDir();
046 if (!buildOutputDir.exists()) {
047 JaCoCoUtils.LOG.info("Project coverage is set to 0% as build output directory doesn't exists: {}", buildOutputDir);
048 return;
049 }
050 String path = getReportPath(project);
051 File jacocoExecutionData = project.getFileSystem().resolvePath(path);
052 try {
053 readExecutionData(jacocoExecutionData, buildOutputDir, context);
054 } catch (IOException e) {
055 throw new SonarException(e);
056 }
057 }
058
059 public final void readExecutionData(File jacocoExecutionData, File buildOutputDir, SensorContext context) throws IOException {
060 SessionInfoStore sessionInfoStore = new SessionInfoStore();
061 ExecutionDataStore executionDataStore = new ExecutionDataStore();
062
063 if (jacocoExecutionData == null || !jacocoExecutionData.exists() || !jacocoExecutionData.isFile()) {
064 JaCoCoUtils.LOG.info("Project coverage is set to 0% as no JaCoCo execution data has been dumped: {}", jacocoExecutionData);
065 } else {
066 JaCoCoUtils.LOG.info("Analysing {}", jacocoExecutionData);
067 ExecutionDataReader reader = new ExecutionDataReader(new FileInputStream(jacocoExecutionData));
068 reader.setSessionInfoVisitor(sessionInfoStore);
069 reader.setExecutionDataVisitor(executionDataStore);
070 reader.read();
071 }
072
073 CoverageBuilder coverageBuilder = new CoverageBuilder();
074 Analyzer analyzer = new Analyzer(executionDataStore, coverageBuilder);
075 analyzeAll(analyzer, buildOutputDir);
076
077 int analyzedResources = 0;
078 for (ISourceFileCoverage coverage : coverageBuilder.getSourceFiles()) {
079 JavaFile resource = getResource(coverage);
080 // Do not save measures on resource which doesn't exist in the context
081 if (context.getResource(resource) != null) {
082 analyzeFile(resource, coverage, context);
083 analyzedResources++;
084 }
085 }
086 if (analyzedResources == 0) {
087 JaCoCoUtils.LOG.warn("Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?");
088 }
089 }
090
091 static JavaFile getResource(ISourceFileCoverage coverage) {
092 String packageName = StringUtils.replaceChars(coverage.getPackageName(), '/', '.');
093 String fileName = StringUtils.substringBeforeLast(coverage.getName(), ".");
094 return new JavaFile(packageName, fileName);
095 }
096
097 /**
098 * Copied from {@link Analyzer#analyzeAll(File)} in order to add logging.
099 */
100 private void analyzeAll(Analyzer analyzer, File file) {
101 if (file.isDirectory()) {
102 for (File f : file.listFiles()) {
103 analyzeAll(analyzer, f);
104 }
105 } else {
106 try {
107 analyzer.analyzeAll(file);
108 } catch (Exception e) {
109 JaCoCoUtils.LOG.warn("Exception during analysis of file " + file.getAbsolutePath(), e);
110 }
111 }
112 }
113
114 private void analyzeFile(JavaFile resource, ISourceFileCoverage coverage, SensorContext context) {
115 CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
116 for (int lineId = coverage.getFirstLine(); lineId <= coverage.getLastLine(); lineId++) {
117 final int hits;
118 ILine line = coverage.getLine(lineId);
119 switch (line.getInstructionCounter().getStatus()) {
120 case ICounter.FULLY_COVERED:
121 case ICounter.PARTLY_COVERED:
122 hits = 1;
123 break;
124 case ICounter.NOT_COVERED:
125 hits = 0;
126 break;
127 case ICounter.EMPTY:
128 continue;
129 default:
130 JaCoCoUtils.LOG.warn("Unknown status for line {} in {}", lineId, resource);
131 continue;
132 }
133 builder.setHits(lineId, hits);
134
135 ICounter branchCounter = line.getBranchCounter();
136 int conditions = branchCounter.getTotalCount();
137 if (conditions > 0) {
138 int coveredConditions = branchCounter.getCoveredCount();
139 builder.setConditions(lineId, conditions, coveredConditions);
140 }
141 }
142
143 saveMeasures(context, resource, builder.createMeasures());
144 }
145
146 protected abstract void saveMeasures(SensorContext context, JavaFile resource, Collection<Measure> measures);
147
148 protected abstract String getReportPath(Project project);
149
150 }