org.jacoco.core.analysis.IBundleCoverage Java Examples

The following examples show how to use org.jacoco.core.analysis.IBundleCoverage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JacocoCoverageRunner.java    From bazel with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
IBundleCoverage analyzeStructure() throws IOException {
  final CoverageBuilder coverageBuilder = new CoverageBuilder();
  final Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(), coverageBuilder);
  Set<String> alreadyInstrumentedClasses = new HashSet<>();
  if (uninstrumentedClasses == null) {
    for (File classesJar : classesJars) {
      analyzeUninstrumentedClassesFromJar(analyzer, classesJar, alreadyInstrumentedClasses);
    }
  } else {
    for (Map.Entry<String, byte[]> entry : uninstrumentedClasses.entrySet()) {
      analyzer.analyzeClass(entry.getValue(), entry.getKey());
    }
  }

  // TODO(bazel-team): Find out where the name of the bundle can pop out in the report.
  return coverageBuilder.getBundle("isthisevenused");
}
 
Example #2
Source File: JacocoNBModuleReportGenerator.java    From tikione-jacocoverage with MIT License 6 votes vote down vote up
public void processNBModule(String projectName, List<String> classDirectories, List<String> sourceDirectories) throws IOException {
	CoverageBuilder coverageBuilder = new CoverageBuilder();
	Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(), coverageBuilder);

	for (String classDirectory : classDirectories) {
		analyzer.analyzeAll(new File(classDirectory));
	}

	IBundleCoverage bundleCoverage = coverageBuilder.getBundle(projectName);

	MultiSourceFileLocator sourceLocator = new MultiSourceFileLocator(4);
	for (String sourceDirectory : sourceDirectories) {
		sourceLocator.add(new DirectorySourceFileLocator(new File(sourceDirectory), DEF_ENCODING, 4));
	}

	groupVisitor.visitBundle(bundleCoverage, sourceLocator);
}
 
Example #3
Source File: ReportGenerator.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * 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
  IBundleCoverage bundleCoverage = analyzeStructure();

  createReport(bundleCoverage);
}
 
Example #4
Source File: ReportGenerator.java    From buck with Apache License 2.0 6 votes vote down vote up
private IBundleCoverage analyzeStructure() throws IOException {
  CoverageBuilder coverageBuilder = new CoverageBuilder();
  Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(), coverageBuilder);

  String[] classesDirs = classesPath.split(":");
  for (String classesDir : classesDirs) {
    File classesDirFile = new File(classesDir);
    if (classesDirFile.exists()) {
      for (File clazz : FileUtils.getFiles(classesDirFile, coverageIncludes, coverageExcludes)) {
        analyzer.analyzeAll(clazz);
      }
    }
  }

  return coverageBuilder.getBundle(title);
}
 
Example #5
Source File: JacocoCoverageRunner.java    From bazel with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #6
Source File: JacocoCoverageRunner.java    From bazel with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void createReport(
    final IBundleCoverage bundleCoverage, final Map<String, BranchCoverageDetail> branchDetails)
    throws IOException {
  JacocoLCOVFormatter formatter = new JacocoLCOVFormatter(createPathsSet());
  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();
}
 
Example #7
Source File: JaCoCoReportAnalyzer.java    From tikione-jacocoverage with MIT License 5 votes vote down vote up
/**
 * Load a JaCoCo binary report and convert it to HTML.
 * <br/>See <a href="http://www.eclemma.org/jacoco/trunk/doc/examples/java/ReportGenerator.java">report generator example code</a>.
 *
 * @param jacocoexec the JaCoCo binary report.
 * @param reportdir the folder to store HTML report.
 * @param prjClassesDir the directory containing project's compiled classes.
 * @param prjSourcesDir the directory containing project's Java source files.
 * @param projectName the project's name.
 * @return the absolute path of HTML report's {@code index.html} file.
 * @throws FileNotFoundException if the JaCoCo binary report, compiled classes or Java sources files directory can't be found.
 * @throws IOException if an I/O error occurs.
 */
public static String toHtmlReport(File jacocoexec, File reportdir, File prjClassesDir, File prjSourcesDir, String projectName)
        throws FileNotFoundException,
               IOException {
    // Load the JaCoCo binary report.
    FileInputStream fis = new FileInputStream(jacocoexec);
    ExecutionDataStore executionDataStore = new ExecutionDataStore();
    SessionInfoStore sessionInfoStore = new SessionInfoStore();
    try {
        ExecutionDataReader executionDataReader = new ExecutionDataReader(fis);
        executionDataReader.setExecutionDataVisitor(executionDataStore);
        executionDataReader.setSessionInfoVisitor(sessionInfoStore);
        while (executionDataReader.read()) {
        }
    } finally {
        fis.close();
    }

    // Convert the binary report to HTML.
    CoverageBuilder coverageBuilder = new CoverageBuilder();
    Analyzer analyzer = new Analyzer(executionDataStore, coverageBuilder);
    analyzer.analyzeAll(prjClassesDir);
    IBundleCoverage bundleCoverage = coverageBuilder.getBundle("JaCoCoverage analysis of project \"" + projectName
            + "\" (powered by JaCoCo from EclEmma)");
    HTMLFormatter htmlformatter = new HTMLFormatter();
    IReportVisitor visitor = htmlformatter.createVisitor(new FileMultiReportOutput(reportdir));
    visitor.visitInfo(sessionInfoStore.getInfos(), executionDataStore.getContents());
    visitor.visitBundle(bundleCoverage, new DirectorySourceFileLocator(prjSourcesDir, DEF_ENCODING, 4));
    visitor.visitEnd();
    return new File(reportdir, "index.html").getAbsolutePath();
}
 
Example #8
Source File: JaCoCoReportAnalyzer.java    From tikione-jacocoverage with MIT License 5 votes vote down vote up
/**
 * Load a JaCoCo binary report and convert it to XML.
 * <br/>See <a href="http://www.eclemma.org/jacoco/trunk/doc/examples/java/ReportGenerator.java">report generator example code</a>.
 *
 * @param jacocoexec the JaCoCo binary report.
 * @param xmlreport the XML file to generate.
 * @param prjClassesDir the directory containing project's compiled classes.
 * @param prjSourcesDir the directory containing project's Java source files.
 * @throws FileNotFoundException if the JaCoCo binary report, compiled classes or Java sources files directory can't be found.
 * @throws IOException if an I/O error occurs.
 */
public static void toXmlReport(File jacocoexec, File xmlreport, File prjClassesDir, File prjSourcesDir)
        throws FileNotFoundException,
               IOException {
    // Load the JaCoCo binary report.
    FileInputStream fis = new FileInputStream(jacocoexec);
    ExecutionDataStore executionDataStore = new ExecutionDataStore();
    SessionInfoStore sessionInfoStore = new SessionInfoStore();
    try {
        ExecutionDataReader executionDataReader = new ExecutionDataReader(fis);
        executionDataReader.setExecutionDataVisitor(executionDataStore);
        executionDataReader.setSessionInfoVisitor(sessionInfoStore);
        while (executionDataReader.read()) {
        }
    } finally {
        fis.close();
    }

    // Convert the binary report to XML.
    CoverageBuilder coverageBuilder = new CoverageBuilder();
    Analyzer analyzer = new Analyzer(executionDataStore, coverageBuilder);
    analyzer.analyzeAll(prjClassesDir);
    IBundleCoverage bundleCoverage = coverageBuilder.getBundle("JaCoCoverage analysis (powered by JaCoCo from EclEmma)");
    XMLFormatter xmlformatter = new XMLFormatter();
    xmlformatter.setOutputEncoding(DEF_ENCODING);
    IReportVisitor visitor = xmlformatter.createVisitor(new FileOutputStream(xmlreport));
    visitor.visitInfo(sessionInfoStore.getInfos(), executionDataStore.getContents());
    visitor.visitBundle(bundleCoverage, new DirectorySourceFileLocator(prjSourcesDir, DEF_ENCODING, 4));
    visitor.visitEnd();
}
 
Example #9
Source File: ReportGenerator.java    From buck with Apache License 2.0 4 votes vote down vote up
private void createReport(IBundleCoverage bundleCoverage) throws IOException {
  Set<String> unknownFormats = Sets.difference(reportFormats, KNOWN_REPORT_FORMATS);
  if (!unknownFormats.isEmpty()) {
    throw new RuntimeException("Unable to parse formats: " + String.join(",", reportFormats));
  }

  // Create a concrete report visitors based on some supplied
  // configuration. In this case we use the defaults
  List<IReportVisitor> visitors = new ArrayList<>();
  if (reportFormats.contains("csv")) {
    reportDirectory.mkdirs();
    CSVFormatter csvFormatter = new CSVFormatter();
    visitors.add(
        csvFormatter.createVisitor(
            new FileOutputStream(new File(reportDirectory, "coverage.csv"))));
  }

  if (reportFormats.contains("html")) {
    HTMLFormatter htmlFormatter = new HTMLFormatter();
    visitors.add(htmlFormatter.createVisitor(new FileMultiReportOutput(reportDirectory)));
  }

  if (reportFormats.contains("xml")) {
    reportDirectory.mkdirs();
    XMLFormatter xmlFormatter = new XMLFormatter();
    visitors.add(
        xmlFormatter.createVisitor(
            new FileOutputStream(new File(reportDirectory, "coverage.xml"))));
  }

  IReportVisitor visitor = new MultiReportVisitor(visitors);
  // 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.
  visitor.visitBundle(bundleCoverage, createSourceFileLocator());

  // Signal end of structure information to allow report to write all
  // information out
  visitor.visitEnd();
}