Search in sources :

Example 1 with ISourceFileLocator

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

the class PackagePageTest method setup.

@Before
@Override
public void setup() throws Exception {
    super.setup();
    sourceLocator = new ISourceFileLocator() {

        public int getTabWidth() {
            return 4;
        }

        public Reader getSourceFile(String packageName, String fileName) throws IOException {
            return null;
        }
    };
}
Also used : Reader(java.io.Reader) IOException(java.io.IOException) ISourceFileLocator(org.jacoco.report.ISourceFileLocator) Before(org.junit.Before)

Example 2 with ISourceFileLocator

use of org.jacoco.report.ISourceFileLocator 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 3 with ISourceFileLocator

use of org.jacoco.report.ISourceFileLocator 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 4 with ISourceFileLocator

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

the class XMLFormatter method createVisitor.

/**
 * Creates a new visitor to write a report to the given stream.
 *
 * @param output
 *            output stream to write the report to
 * @return visitor to emit the report data to
 * @throws IOException
 *             in case of problems with the output stream
 */
public IReportVisitor createVisitor(final OutputStream output) throws IOException {
    class RootVisitor implements IReportVisitor {

        private ReportElement report;

        private List<SessionInfo> sessionInfos;

        private XMLGroupVisitor groupVisitor;

        public void visitInfo(final List<SessionInfo> sessionInfos, final Collection<ExecutionData> executionData) throws IOException {
            this.sessionInfos = sessionInfos;
        }

        public void visitBundle(final IBundleCoverage bundle, final ISourceFileLocator locator) throws IOException {
            createRootElement(bundle.getName());
            XMLCoverageWriter.writeBundle(bundle, report);
        }

        public IReportGroupVisitor visitGroup(final String name) throws IOException {
            createRootElement(name);
            groupVisitor = new XMLGroupVisitor(report, name);
            return groupVisitor;
        }

        private void createRootElement(final String name) throws IOException {
            report = new ReportElement(name, output, outputEncoding);
            for (final SessionInfo i : sessionInfos) {
                report.sessioninfo(i);
            }
        }

        public void visitEnd() throws IOException {
            if (groupVisitor != null) {
                groupVisitor.visitEnd();
            }
            report.close();
        }
    }
    return new RootVisitor();
}
Also used : XMLGroupVisitor(org.jacoco.report.internal.xml.XMLGroupVisitor) Collection(java.util.Collection) IBundleCoverage(org.jacoco.core.analysis.IBundleCoverage) SessionInfo(org.jacoco.core.data.SessionInfo) ReportElement(org.jacoco.report.internal.xml.ReportElement) List(java.util.List) ISourceFileLocator(org.jacoco.report.ISourceFileLocator) IReportVisitor(org.jacoco.report.IReportVisitor)

Example 5 with ISourceFileLocator

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

the class PackageSourcePageTest method setup.

@Before
@Override
public void setup() throws Exception {
    super.setup();
    SourceFileCoverageImpl src1 = new SourceFileCoverageImpl("Src1.java", "org/jacoco/example");
    src1.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 1);
    SourceFileCoverageImpl src2 = new SourceFileCoverageImpl("Src2.java", "org/jacoco/example");
    src2.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 1);
    node = new PackageCoverageImpl("org/jacoco/example", Collections.<IClassCoverage>emptyList(), Arrays.<ISourceFileCoverage>asList(src1, src2));
    sourceLocator = new ISourceFileLocator() {

        public int getTabWidth() {
            return 4;
        }

        public Reader getSourceFile(String packageName, String fileName) throws IOException {
            return fileName.equals("Src1.java") ? new StringReader("") : null;
        }
    };
    packagePageLink = new ILinkable() {

        public String getLinkStyle() {
            fail();
            return null;
        }

        public String getLinkLabel() {
            fail();
            return null;
        }

        public String getLink(ReportOutputFolder base) {
            return "index.html";
        }
    };
}
Also used : ReportOutputFolder(org.jacoco.report.internal.ReportOutputFolder) PackageCoverageImpl(org.jacoco.core.internal.analysis.PackageCoverageImpl) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) IOException(java.io.IOException) ILinkable(org.jacoco.report.internal.html.ILinkable) ISourceFileLocator(org.jacoco.report.ISourceFileLocator) SourceFileCoverageImpl(org.jacoco.core.internal.analysis.SourceFileCoverageImpl) Before(org.junit.Before)

Aggregations

ISourceFileLocator (org.jacoco.report.ISourceFileLocator)6 IReportVisitor (org.jacoco.report.IReportVisitor)4 IOException (java.io.IOException)3 Reader (java.io.Reader)3 Collection (java.util.Collection)3 List (java.util.List)3 IBundleCoverage (org.jacoco.core.analysis.IBundleCoverage)3 IClassCoverage (org.jacoco.core.analysis.IClassCoverage)2 ISourceFileCoverage (org.jacoco.core.analysis.ISourceFileCoverage)2 ReportOutputFolder (org.jacoco.report.internal.ReportOutputFolder)2 Before (org.junit.Before)2 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ICounter (org.jacoco.core.analysis.ICounter)1 IMethodCoverage (org.jacoco.core.analysis.IMethodCoverage)1 IPackageCoverage (org.jacoco.core.analysis.IPackageCoverage)1 SessionInfo (org.jacoco.core.data.SessionInfo)1