use of org.jacoco.core.analysis.ISourceFileCoverage in project sonar-java by SonarSource.
the class UnitTestAnalyzer method readExecutionData.
private void readExecutionData(@Nullable File jacocoExecutionData, SensorContext context) {
File newJacocoExecutionData = jacocoExecutionData;
if (newJacocoExecutionData == null || !newJacocoExecutionData.isFile()) {
JaCoCoExtensions.LOG.info("Project coverage is set to 0% as no JaCoCo execution data has been dumped: {}", newJacocoExecutionData);
newJacocoExecutionData = null;
}
ExecutionDataVisitor executionDataVisitor = new ExecutionDataVisitor();
jacocoReportReader = new JacocoReportReader(newJacocoExecutionData).readJacocoReport(executionDataVisitor, executionDataVisitor);
boolean collectedCoveragePerTest = readCoveragePerTests(executionDataVisitor);
CoverageBuilder coverageBuilder = jacocoReportReader.analyzeFiles(executionDataVisitor.getMerged(), classFilesCache.values());
int analyzedResources = 0;
for (ISourceFileCoverage coverage : coverageBuilder.getSourceFiles()) {
InputFile inputFile = getResource(coverage);
if (inputFile != null) {
NewCoverage newCoverage = context.newCoverage().onFile(inputFile);
analyzeFile(newCoverage, inputFile, coverage);
newCoverage.save();
analyzedResources++;
}
}
if (analyzedResources == 0) {
JaCoCoExtensions.LOG.warn("Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?");
} else if (collectedCoveragePerTest) {
JaCoCoExtensions.LOG.info("Information about coverage per test has been collected.");
} else if (newJacocoExecutionData != null) {
JaCoCoExtensions.LOG.info("No information about coverage per test.");
}
}
use of org.jacoco.core.analysis.ISourceFileCoverage in project sonar-java by SonarSource.
the class UnitTestAnalyzer method analyzeLinesCoveredByTests.
private boolean analyzeLinesCoveredByTests(String sessionId, ExecutionDataStore executionDataStore) {
int i = sessionId.indexOf(' ');
if (i < 0) {
return false;
}
String testClassName = sessionId.substring(0, i);
String testName = sessionId.substring(i + 1);
InputFile testResource = javaResourceLocator.findResourceByClassName(testClassName);
if (testResource == null) {
// No such test class
return false;
}
boolean result = false;
CoverageBuilder coverageBuilder = jacocoReportReader.analyzeFiles(executionDataStore, classFilesOfStore(executionDataStore));
for (ISourceFileCoverage coverage : coverageBuilder.getSourceFiles()) {
InputFile resource = getResource(coverage);
if (resource != null) {
List<Integer> coveredLines = coveredLines(coverage);
if (!coveredLines.isEmpty() && addCoverage(resource, testResource, testName, coveredLines)) {
result = true;
}
}
}
return result;
}
use of org.jacoco.core.analysis.ISourceFileCoverage in project jacoco by jacoco.
the class PackageSourcePageTest method should_render_non_empty_sources.
@Test
public void should_render_non_empty_sources() throws Exception {
final ISourceFileCoverage emptySource = new SourceFileCoverageImpl("Empty.java", "example");
final SourceFileCoverageImpl nonEmptySource = new SourceFileCoverageImpl("NonEmpty.java", "example");
nonEmptySource.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 1);
node = new PackageCoverageImpl("example", Collections.<IClassCoverage>emptyList(), Arrays.asList(emptySource, nonEmptySource));
page = new PackageSourcePage(node, null, sourceLocator, rootFolder, context, packagePageLink);
page.render();
final Document doc = support.parse(output.getFile("index.source.html"));
assertEquals("NonEmpty.java", support.findStr(doc, "/html/body/table[1]/tbody/tr[1]/td[1]/span"));
assertEquals("1", support.findStr(doc, "count(/html/body/table[1]/tbody/tr)"));
}
use of org.jacoco.core.analysis.ISourceFileCoverage in project jacoco by jacoco.
the class BundleCheckerTest method createBundle.
private IBundleCoverage createBundle() {
final MethodCoverageImpl m = new MethodCoverageImpl("fooMethod", "()V", null);
m.increment(CounterImpl.getInstance(5, 5), CounterImpl.COUNTER_0_0, 1);
m.incrementMethodCounter();
final ClassCoverageImpl c = new ClassCoverageImpl("org/jacoco/example/FooClass", 1001, false);
c.setSourceFileName("FooClass.java");
c.addMethod(m);
final SourceFileCoverageImpl s = new SourceFileCoverageImpl("FooClass.java", "org/jacoco/example");
s.increment(c);
IPackageCoverage p = new PackageCoverageImpl("org/jacoco/example", Collections.singleton((IClassCoverage) c), Collections.singleton((ISourceFileCoverage) s));
return new BundleCoverageImpl("Test", Collections.singleton(p));
}
use of org.jacoco.core.analysis.ISourceFileCoverage in project jacoco by jacoco.
the class Source method load.
/**
* Loads the source file which holds the given target class.
*
* @param target
* the target class we want the source for
* @param bundle
* the bundle containing the analyzed class and its source file
* @return a {@link Source} instance
*/
public static Source load(Class<?> target, IBundleCoverage bundle) throws IOException {
final IPackageCoverage pkgCov = findByName(bundle.getPackages(), vm(target.getPackage().getName()));
final IClassCoverage clsCov = findByName(pkgCov.getClasses(), vm(target.getName()));
final ISourceFileCoverage srcCov = findByName(pkgCov.getSourceFiles(), clsCov.getSourceFileName());
return new Source(open(SRC_LOCATION + pkgCov.getName() + "/" + clsCov.getSourceFileName()), srcCov);
}
Aggregations