Search in sources :

Example 11 with ISourceFileCoverage

use of org.jacoco.core.analysis.ISourceFileCoverage 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 12 with ISourceFileCoverage

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

the class PackagePageTest method testContentsWithSource.

@Test
public void testContentsWithSource() throws Exception {
    IClassCoverage class1 = new ClassCoverageImpl("org/jacoco/example/Foo1", 0x1000, false);
    IClassCoverage class2 = new ClassCoverageImpl("org/jacoco/example/Foo2", 0x2000, false);
    ISourceFileCoverage src1 = new SourceFileCoverageImpl("Src1.java", "org/jacoco/example");
    node = new PackageCoverageImpl("org/jacoco/example", Arrays.asList(class1, class2), Arrays.asList(src1));
    page = new PackagePage(node, null, sourceLocator, rootFolder, context);
    page.render();
    final Document doc = support.parse(output.getFile("index.html"));
    // Expect "Source Files" links
    assertEquals("index.source.html", support.findStr(doc, "/html/body/div[1]/span[1]/a/@href"));
    assertEquals("el_source", support.findStr(doc, "/html/body/div[1]/span[1]/a/@class"));
    assertEquals("Source Files", support.findStr(doc, "/html/body/div[1]/span[1]/a"));
    assertEquals("el_class", support.findStr(doc, "/html/body/table[1]/tbody/tr[1]/td[1]/a/@class"));
    assertEquals("Foo1", support.findStr(doc, "/html/body/table[1]/tbody/tr[1]/td[1]/a"));
    assertEquals("el_class", support.findStr(doc, "/html/body/table[1]/tbody/tr[2]/td[1]/a/@class"));
    assertEquals("Foo2", support.findStr(doc, "/html/body/table[1]/tbody/tr[2]/td[1]/a"));
    output.assertFile("index.source.html");
}
Also used : IClassCoverage(org.jacoco.core.analysis.IClassCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) PackageCoverageImpl(org.jacoco.core.internal.analysis.PackageCoverageImpl) ClassCoverageImpl(org.jacoco.core.internal.analysis.ClassCoverageImpl) Document(org.w3c.dom.Document) SourceFileCoverageImpl(org.jacoco.core.internal.analysis.SourceFileCoverageImpl) Test(org.junit.Test)

Example 13 with ISourceFileCoverage

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

the class PackagePageTest method testContentsNoSource.

@Test
public void testContentsNoSource() throws Exception {
    IClassCoverage class1 = new ClassCoverageImpl("org/jacoco/example/Foo1", 0x1000, false);
    IClassCoverage class2 = new ClassCoverageImpl("org/jacoco/example/Foo2", 0x2000, false);
    node = new PackageCoverageImpl("org/jacoco/example", Arrays.asList(class1, class2), Collections.<ISourceFileCoverage>emptyList());
    page = new PackagePage(node, null, sourceLocator, rootFolder, context);
    page.render();
    // Expect no "Source Files" link
    final Document doc = support.parse(output.getFile("index.html"));
    assertEquals("Sessions", support.findStr(doc, "/html/body/div[1]/span[1]/a"));
    // Expect no source files page:
    output.assertNoFile("index.source.html");
}
Also used : IClassCoverage(org.jacoco.core.analysis.IClassCoverage) PackageCoverageImpl(org.jacoco.core.internal.analysis.PackageCoverageImpl) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) ClassCoverageImpl(org.jacoco.core.internal.analysis.ClassCoverageImpl) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 14 with ISourceFileCoverage

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

the class PackageSourcePageTest method setup.

@Before
@Override
public void setup() throws Exception {
    super.setup();
    ISourceFileCoverage src1 = new SourceFileCoverageImpl("Src1.java", "org/jacoco/example");
    ISourceFileCoverage src2 = new SourceFileCoverageImpl("Src2.java", "org/jacoco/example");
    node = new PackageCoverageImpl("org/jacoco/example", Collections.<IClassCoverage>emptyList(), Arrays.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) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) PackageCoverageImpl(org.jacoco.core.internal.analysis.PackageCoverageImpl) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) 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)

Example 15 with ISourceFileCoverage

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

the class XMLCoverageWriter method writePackage.

private static void writePackage(final IPackageCoverage p, final XMLElement parent) throws IOException {
    final XMLElement element = createChild(parent, "package", p.getName());
    for (final IClassCoverage c : p.getClasses()) {
        writeClass(c, element);
    }
    for (final ISourceFileCoverage s : p.getSourceFiles()) {
        writeSourceFile(s, element);
    }
    writeCounters(p, element);
}
Also used : IClassCoverage(org.jacoco.core.analysis.IClassCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage)

Aggregations

ISourceFileCoverage (org.jacoco.core.analysis.ISourceFileCoverage)15 IClassCoverage (org.jacoco.core.analysis.IClassCoverage)11 IPackageCoverage (org.jacoco.core.analysis.IPackageCoverage)5 Test (org.junit.Test)5 PackageCoverageImpl (org.jacoco.core.internal.analysis.PackageCoverageImpl)4 CoverageBuilder (org.jacoco.core.analysis.CoverageBuilder)3 ClassCoverageImpl (org.jacoco.core.internal.analysis.ClassCoverageImpl)3 SourceFileCoverageImpl (org.jacoco.core.internal.analysis.SourceFileCoverageImpl)3 Reader (java.io.Reader)2 Collection (java.util.Collection)2 ISourceFileLocator (org.jacoco.report.ISourceFileLocator)2 InputFile (org.sonar.api.batch.fs.InputFile)2 Document (org.w3c.dom.Document)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 HashMap (java.util.HashMap)1