Search in sources :

Example 1 with ISourceFileCoverage

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.");
    }
}
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 2 with ISourceFileCoverage

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

Example 3 with ISourceFileCoverage

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)"));
}
Also used : ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) PackageCoverageImpl(org.jacoco.core.internal.analysis.PackageCoverageImpl) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) Document(org.w3c.dom.Document) SourceFileCoverageImpl(org.jacoco.core.internal.analysis.SourceFileCoverageImpl) Test(org.junit.Test)

Example 4 with ISourceFileCoverage

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));
}
Also used : IPackageCoverage(org.jacoco.core.analysis.IPackageCoverage) PackageCoverageImpl(org.jacoco.core.internal.analysis.PackageCoverageImpl) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) BundleCoverageImpl(org.jacoco.core.internal.analysis.BundleCoverageImpl) ClassCoverageImpl(org.jacoco.core.internal.analysis.ClassCoverageImpl) MethodCoverageImpl(org.jacoco.core.internal.analysis.MethodCoverageImpl) SourceFileCoverageImpl(org.jacoco.core.internal.analysis.SourceFileCoverageImpl)

Example 5 with ISourceFileCoverage

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);
}
Also used : IPackageCoverage(org.jacoco.core.analysis.IPackageCoverage) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage)

Aggregations

ISourceFileCoverage (org.jacoco.core.analysis.ISourceFileCoverage)18 IClassCoverage (org.jacoco.core.analysis.IClassCoverage)15 Test (org.junit.Test)7 IPackageCoverage (org.jacoco.core.analysis.IPackageCoverage)6 PackageCoverageImpl (org.jacoco.core.internal.analysis.PackageCoverageImpl)6 ClassCoverageImpl (org.jacoco.core.internal.analysis.ClassCoverageImpl)4 MethodCoverageImpl (org.jacoco.core.internal.analysis.MethodCoverageImpl)4 SourceFileCoverageImpl (org.jacoco.core.internal.analysis.SourceFileCoverageImpl)4 Document (org.w3c.dom.Document)4 Reader (java.io.Reader)2 Collection (java.util.Collection)2 CoverageBuilder (org.jacoco.core.analysis.CoverageBuilder)2 ISourceFileLocator (org.jacoco.report.ISourceFileLocator)2 InputFile (org.sonar.api.batch.fs.InputFile)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1