Search in sources :

Example 6 with IReportVisitor

use of org.jacoco.report.IReportVisitor in project jacoco by jacoco.

the class XMLFormatterTest method testStructureWithGroup.

@Test
public void testStructureWithGroup() throws Exception {
    final IReportVisitor visitor = formatter.createVisitor(output);
    visitor.visitInfo(infos, data);
    driver.sendGroup(visitor);
    assertPathMatches("group", "/report/@name");
    assertPathMatches("bundle", "/report/group/@name");
    assertPathMatches("org/jacoco/example", "/report/group/package/@name");
    assertPathMatches("org/jacoco/example/FooClass", "/report/group/package/class/@name");
    assertPathMatches("fooMethod", "/report/group/package/class/method/@name");
    assertPathMatches("1", "count(/report/counter[@type='INSTRUCTION'])");
    assertPathMatches("10", "report/counter[@type='INSTRUCTION']/@missed");
    assertPathMatches("15", "report/counter[@type='INSTRUCTION']/@covered");
    assertPathMatches("1", "count(/report/counter[@type='BRANCH'])");
    assertPathMatches("1", "report/counter[@type='BRANCH']/@missed");
    assertPathMatches("2", "report/counter[@type='BRANCH']/@covered");
    assertPathMatches("1", "count(/report/counter[@type='COMPLEXITY'])");
    assertPathMatches("1", "report/counter[@type='COMPLEXITY']/@missed");
    assertPathMatches("2", "report/counter[@type='COMPLEXITY']/@covered");
    assertPathMatches("1", "count(/report/counter[@type='LINE'])");
    assertPathMatches("0", "report/counter[@type='LINE']/@missed");
    assertPathMatches("3", "report/counter[@type='LINE']/@covered");
    assertPathMatches("1", "count(/report/counter[@type='METHOD'])");
    assertPathMatches("0", "report/counter[@type='METHOD']/@missed");
    assertPathMatches("1", "report/counter[@type='METHOD']/@covered");
    assertPathMatches("1", "count(/report/counter[@type='CLASS'])");
    assertPathMatches("0", "report/counter[@type='CLASS']/@missed");
    assertPathMatches("1", "report/counter[@type='CLASS']/@covered");
}
Also used : IReportVisitor(org.jacoco.report.IReportVisitor) Test(org.junit.Test)

Example 7 with IReportVisitor

use of org.jacoco.report.IReportVisitor 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)

Example 8 with IReportVisitor

use of org.jacoco.report.IReportVisitor in project bazel by bazelbuild.

the class JacocoCoverageRunner method createReport.

private void createReport(final IBundleCoverage bundleCoverage, final Map<String, BranchCoverageDetail> branchDetails) throws IOException {
    JacocoLCOVFormatter formatter = new JacocoLCOVFormatter();
    final IReportVisitor visitor = formatter.createVisitor(reportFile, branchDetails);
    // Initialize the report with all of the execution and session information. At this point the
    // report doesn't know about the structure of the report being created.
    visitor.visitInfo(execFileLoader.getSessionInfoStore().getInfos(), execFileLoader.getExecutionDataStore().getContents());
    // Populate the report structure with the bundle coverage information.
    // Call visitGroup if you need groups in your report.
    // Note the API requires a sourceFileLocator because the HTML and XML formatters display a page
    // of code annotated with coverage information. Having the source files is not actually needed
    // for generating the lcov report...
    visitor.visitBundle(bundleCoverage, new ISourceFileLocator() {

        @Override
        public Reader getSourceFile(String packageName, String fileName) throws IOException {
            return null;
        }

        @Override
        public int getTabWidth() {
            return 0;
        }
    });
    // Signal end of structure information to allow report to write all information out
    visitor.visitEnd();
}
Also used : Reader(java.io.Reader) IOException(java.io.IOException) ISourceFileLocator(org.jacoco.report.ISourceFileLocator) IReportVisitor(org.jacoco.report.IReportVisitor)

Example 9 with IReportVisitor

use of org.jacoco.report.IReportVisitor in project jacoco by jacoco.

the class ReportTask method execute.

@Override
public void execute() throws BuildException {
    loadExecutionData();
    try {
        final IReportVisitor visitor = createVisitor();
        visitor.visitInfo(sessionInfoStore.getInfos(), executionDataStore.getContents());
        createReport(visitor, structure);
        visitor.visitEnd();
        for (final FormatterElement f : formatters) {
            f.finish();
        }
    } catch (final IOException e) {
        throw new BuildException("Error while creating report", e, getLocation());
    }
}
Also used : IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) IReportVisitor(org.jacoco.report.IReportVisitor)

Example 10 with IReportVisitor

use of org.jacoco.report.IReportVisitor in project jacoco by jacoco.

the class Report method writeReports.

private void writeReports(final IBundleCoverage bundle, final ExecFileLoader loader, final PrintWriter out) throws IOException {
    out.printf("[INFO] Analyzing %s classes.%n", Integer.valueOf(bundle.getClassCounter().getTotalCount()));
    final IReportVisitor visitor = createReportVisitor();
    visitor.visitInfo(loader.getSessionInfoStore().getInfos(), loader.getExecutionDataStore().getContents());
    visitor.visitBundle(bundle, getSourceLocator());
    visitor.visitEnd();
}
Also used : IReportVisitor(org.jacoco.report.IReportVisitor)

Aggregations

IReportVisitor (org.jacoco.report.IReportVisitor)19 Test (org.junit.Test)6 IOException (java.io.IOException)4 Collection (java.util.Collection)4 List (java.util.List)4 ISourceFileLocator (org.jacoco.report.ISourceFileLocator)4 IBundleCoverage (org.jacoco.core.analysis.IBundleCoverage)3 FileMultiReportOutput (org.jacoco.report.FileMultiReportOutput)3 HTMLFormatter (org.jacoco.report.html.HTMLFormatter)3 BufferedReader (java.io.BufferedReader)2 FileOutputStream (java.io.FileOutputStream)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 SessionInfo (org.jacoco.core.data.SessionInfo)2 MultiReportVisitor (org.jacoco.report.MultiReportVisitor)2 CSVFormatter (org.jacoco.report.csv.CSVFormatter)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1