Search in sources :

Example 6 with CoverageBuilder

use of org.jacoco.core.analysis.CoverageBuilder in project dspot by STAMP-project.

the class JacocoListener method coverageResults.

private CoverageResults coverageResults(ExecutionDataStore executionData) {
    final CoverageBuilder coverageBuilder = new CoverageBuilder();
    final Analyzer analyzer = new Analyzer(executionData, coverageBuilder);
    try {
        analyzer.analyzeAll(new File(classesDirectory));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return new CoverageResults(coverageBuilder);
}
Also used : CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) IOException(java.io.IOException) Analyzer(org.jacoco.core.analysis.Analyzer) File(java.io.File)

Example 7 with CoverageBuilder

use of org.jacoco.core.analysis.CoverageBuilder 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.");
    }
}
Also used : NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) InputFile(org.sonar.api.batch.fs.InputFile)

Example 8 with CoverageBuilder

use of org.jacoco.core.analysis.CoverageBuilder 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;
}
Also used : ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) InputFile(org.sonar.api.batch.fs.InputFile)

Example 9 with CoverageBuilder

use of org.jacoco.core.analysis.CoverageBuilder in project sonar-java by SonarSource.

the class JacocoReportReader method analyzeFiles.

/**
 * Caller must guarantee that {@code classFiles} are actually class file.
 */
public CoverageBuilder analyzeFiles(ExecutionDataStore executionDataStore, Collection<File> classFiles) {
    CoverageBuilder coverageBuilder = new CoverageBuilder();
    if (useCurrentBinaryFormat) {
        Analyzer analyzer = new Analyzer(executionDataStore, coverageBuilder);
        for (File classFile : classFiles) {
            analyzeClassFile(analyzer, classFile);
        }
    } else {
        org.jacoco.previous.core.analysis.Analyzer analyzer = new org.jacoco.previous.core.analysis.Analyzer(executionDataStore, coverageBuilder);
        for (File classFile : classFiles) {
            analyzeClassFile(analyzer, classFile);
        }
    }
    logNoMatchClasses(coverageBuilder.getNoMatchClasses());
    return coverageBuilder;
}
Also used : CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) Analyzer(org.jacoco.core.analysis.Analyzer) File(java.io.File)

Example 10 with CoverageBuilder

use of org.jacoco.core.analysis.CoverageBuilder in project bazel by bazelbuild.

the class JacocoCoverageRunner method analyzeStructure.

private IBundleCoverage analyzeStructure() throws IOException {
    final CoverageBuilder coverageBuilder = new CoverageBuilder();
    final Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(), coverageBuilder);
    for (File classesJar : classesJars) {
        analyzer.analyzeAll(classesJar);
    }
    // TODO(bazel-team): Find out where the name of the bundle can pop out in the report.
    return coverageBuilder.getBundle("isthisevenused");
}
Also used : CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) Analyzer(org.jacoco.core.analysis.Analyzer) File(java.io.File)

Aggregations

CoverageBuilder (org.jacoco.core.analysis.CoverageBuilder)16 Analyzer (org.jacoco.core.analysis.Analyzer)13 File (java.io.File)8 ISourceFileCoverage (org.jacoco.core.analysis.ISourceFileCoverage)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 IBundleCoverage (org.jacoco.core.analysis.IBundleCoverage)2 IClassCoverage (org.jacoco.core.analysis.IClassCoverage)2 ExecutionDataStore (org.jacoco.core.data.ExecutionDataStore)2 SessionInfoStore (org.jacoco.core.data.SessionInfoStore)2 RuntimeData (org.jacoco.core.runtime.RuntimeData)2 InputFile (org.sonar.api.batch.fs.InputFile)2 Resource (org.apache.tools.ant.types.Resource)1 FileResource (org.apache.tools.ant.types.resources.FileResource)1 IMethodCoverage (org.jacoco.core.analysis.IMethodCoverage)1 ExecutionData (org.jacoco.core.data.ExecutionData)1 Instrumenter (org.jacoco.core.instr.Instrumenter)1 IRuntime (org.jacoco.core.runtime.IRuntime)1 LoggerRuntime (org.jacoco.core.runtime.LoggerRuntime)1 Test (org.junit.Test)1