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;
}
};
}
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();
}
};
}
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();
}
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();
}
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";
}
};
}
Aggregations