Search in sources :

Example 1 with IBundleCoverage

use of org.jacoco.core.analysis.IBundleCoverage in project buck by facebook.

the class ReportGenerator method create.

/**
         * Create the report.
         *
         * @throws IOException
         */
public void create() throws IOException {
    // Read the jacoco.exec file. Multiple data files could be merged
    // at this point
    loadExecutionData();
    // Run the structure analyzer on a single class folder to build up
    // the coverage model. The process would be similar if your classes
    // were in a jar file. Typically you would create a bundle for each
    // class folder and each jar you want in your report. If you have
    // more than one bundle you will need to add a grouping node to your
    // report
    final IBundleCoverage bundleCoverage = analyzeStructure();
    createReport(bundleCoverage);
}
Also used : IBundleCoverage(org.jacoco.core.analysis.IBundleCoverage)

Example 2 with IBundleCoverage

use of org.jacoco.core.analysis.IBundleCoverage in project jacoco by jacoco.

the class Report method execute.

@Override
public int execute(final PrintWriter out, final PrintWriter err) throws IOException {
    final ExecFileLoader loader = loadExecutionData(out);
    final IBundleCoverage bundle = analyze(loader.getExecutionDataStore(), out);
    writeReports(bundle, loader, out);
    return 0;
}
Also used : IBundleCoverage(org.jacoco.core.analysis.IBundleCoverage) ExecFileLoader(org.jacoco.core.tools.ExecFileLoader)

Example 3 with IBundleCoverage

use of org.jacoco.core.analysis.IBundleCoverage in project jacoco by jacoco.

the class ReportGenerator method create.

/**
 * Create the report.
 *
 * @throws IOException
 */
public void create() throws IOException {
    // Read the jacoco.exec file. Multiple data files could be merged
    // at this point
    loadExecutionData();
    // Run the structure analyzer on a single class folder to build up
    // the coverage model. The process would be similar if your classes
    // were in a jar file. Typically you would create a bundle for each
    // class folder and each jar you want in your report. If you have
    // more than one bundle you will need to add a grouping node to your
    // report
    final IBundleCoverage bundleCoverage = analyzeStructure();
    createReport(bundleCoverage);
}
Also used : IBundleCoverage(org.jacoco.core.analysis.IBundleCoverage)

Example 4 with IBundleCoverage

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

the class JacocoCoverageRunner method create.

public void create() throws IOException {
    // Read the jacoco.exec file. Multiple data files could be merged at this point
    execFileLoader = new ExecFileLoader();
    execFileLoader.load(executionData);
    // Run the structure analyzer on a single class folder or jar file to build up the coverage
    // model. Typically you would create a bundle for each class folder and each jar you want in
    // your report. If you have more than one bundle you may need to add a grouping node to the
    // report. The lcov formatter doesn't seem to care, and we're only using one bundle anyway.
    final IBundleCoverage bundleCoverage = analyzeStructure();
    final Map<String, BranchCoverageDetail> branchDetails = analyzeBranch();
    createReport(bundleCoverage, branchDetails);
}
Also used : IBundleCoverage(org.jacoco.core.analysis.IBundleCoverage) ExecFileLoader(org.jacoco.core.tools.ExecFileLoader)

Example 5 with IBundleCoverage

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

the class JacocoLCOVFormatter method createVisitor.

public IReportVisitor createVisitor(final File output, final Map<String, BranchCoverageDetail> branchCoverageDetail) {
    return new IReportVisitor() {

        private Map<String, Map<String, IClassCoverage>> sourceToClassCoverage = new TreeMap<>();

        private Map<String, ISourceFileCoverage> sourceToFileCoverage = new TreeMap<>();

        @Override
        public void visitInfo(List<SessionInfo> sessionInfos, Collection<ExecutionData> executionData) throws IOException {
        }

        @Override
        public void visitEnd() throws IOException {
            try (FileWriter fileWriter = new FileWriter(output, true);
                PrintWriter printWriter = new PrintWriter(fileWriter)) {
                for (String sourceFile : sourceToClassCoverage.keySet()) {
                    processSourceFile(printWriter, sourceFile);
                }
            }
        }

        @Override
        public void visitBundle(IBundleCoverage bundle, ISourceFileLocator locator) throws IOException {
            // information and process everything at the end.
            for (IPackageCoverage pkgCoverage : bundle.getPackages()) {
                for (IClassCoverage clsCoverage : pkgCoverage.getClasses()) {
                    String fileName = clsCoverage.getPackageName() + "/" + clsCoverage.getSourceFileName();
                    if (!sourceToClassCoverage.containsKey(fileName)) {
                        sourceToClassCoverage.put(fileName, new TreeMap<String, IClassCoverage>());
                    }
                    sourceToClassCoverage.get(fileName).put(clsCoverage.getName(), clsCoverage);
                }
                for (ISourceFileCoverage srcCoverage : pkgCoverage.getSourceFiles()) {
                    sourceToFileCoverage.put(srcCoverage.getPackageName() + "/" + srcCoverage.getName(), srcCoverage);
                }
            }
        }

        @Override
        public IReportGroupVisitor visitGroup(String name) throws IOException {
            return null;
        }

        private void processSourceFile(PrintWriter writer, String sourceFile) {
            writer.printf("SF:%s\n", sourceFile);
            ISourceFileCoverage srcCoverage = sourceToFileCoverage.get(sourceFile);
            if (srcCoverage != null) {
                // List methods, including methods from nested classes, in FN/FNDA pairs
                for (IClassCoverage clsCoverage : sourceToClassCoverage.get(sourceFile).values()) {
                    for (IMethodCoverage mthCoverage : clsCoverage.getMethods()) {
                        String name = constructFunctionName(mthCoverage, clsCoverage.getName());
                        writer.printf("FN:%d,%s\n", mthCoverage.getFirstLine(), name);
                        writer.printf("FNDA:%d,%s\n", mthCoverage.getMethodCounter().getCoveredCount(), name);
                    }
                }
                for (IClassCoverage clsCoverage : sourceToClassCoverage.get(sourceFile).values()) {
                    BranchCoverageDetail detail = branchCoverageDetail.get(clsCoverage.getName());
                    if (detail != null) {
                        for (int line : detail.linesWithBranches()) {
                            int numBranches = detail.getBranches(line);
                            boolean executed = detail.getExecutedBit(line);
                            if (executed) {
                                for (int branchIdx = 0; branchIdx < numBranches; branchIdx++) {
                                    if (detail.getTakenBit(line, branchIdx)) {
                                        // executed, taken
                                        writer.printf("BA:%d,%d\n", line, 2);
                                    } else {
                                        // executed, not taken
                                        writer.printf("BA:%d,%d\n", line, 1);
                                    }
                                }
                            } else {
                                for (int branchIdx = 0; branchIdx < numBranches; branchIdx++) {
                                    // not executed
                                    writer.printf("BA:%d,%d\n", line, 0);
                                }
                            }
                        }
                    }
                }
                // List of DA entries matching source lines
                int firstLine = srcCoverage.getFirstLine();
                int lastLine = srcCoverage.getLastLine();
                for (int line = firstLine; line <= lastLine; line++) {
                    ICounter instructionCounter = srcCoverage.getLine(line).getInstructionCounter();
                    if (instructionCounter.getTotalCount() != 0) {
                        writer.printf("DA:%d,%d\n", line, instructionCounter.getCoveredCount());
                    }
                }
            }
            writer.println("end_of_record");
        }

        private String constructFunctionName(IMethodCoverage mthCoverage, String clsName) {
            // lcov_merger doesn't seem to care about these entries.
            return clsName + "::" + mthCoverage.getName() + " " + mthCoverage.getDesc();
        }
    };
}
Also used : IMethodCoverage(org.jacoco.core.analysis.IMethodCoverage) FileWriter(java.io.FileWriter) ISourceFileLocator(org.jacoco.report.ISourceFileLocator) IReportVisitor(org.jacoco.report.IReportVisitor) IPackageCoverage(org.jacoco.core.analysis.IPackageCoverage) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) ICounter(org.jacoco.core.analysis.ICounter) Collection(java.util.Collection) IBundleCoverage(org.jacoco.core.analysis.IBundleCoverage) List(java.util.List) TreeMap(java.util.TreeMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Aggregations

IBundleCoverage (org.jacoco.core.analysis.IBundleCoverage)10 Collection (java.util.Collection)3 List (java.util.List)3 IReportVisitor (org.jacoco.report.IReportVisitor)3 ISourceFileLocator (org.jacoco.report.ISourceFileLocator)3 Analyzer (org.jacoco.core.analysis.Analyzer)2 CoverageBuilder (org.jacoco.core.analysis.CoverageBuilder)2 ExecFileLoader (org.jacoco.core.tools.ExecFileLoader)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 BuildException (org.apache.tools.ant.BuildException)1 Resource (org.apache.tools.ant.types.Resource)1 FileResource (org.apache.tools.ant.types.resources.FileResource)1 IClassCoverage (org.jacoco.core.analysis.IClassCoverage)1 ICounter (org.jacoco.core.analysis.ICounter)1 IMethodCoverage (org.jacoco.core.analysis.IMethodCoverage)1