Package ch.hortis.sonar.core.service

Source Code of ch.hortis.sonar.core.service.CodeCoverageCalculator

/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.core.service;

import ch.hortis.sonar.model.File;
import ch.hortis.sonar.model.Metric;
import ch.hortis.sonar.model.Metrics;
import ch.hortis.sonar.service.MeasureKey;

import java.util.ArrayList;
import java.util.List;

public class CodeCoverageCalculator extends AbstractMetricCalculator {

  protected Metric coberturaBranchCoverage;
  protected Metric coberturaLineCoverage;
  protected Metric cloverCoverage;
  protected Metric codeCoverage;

  public Metrics getMetricKey() {
    return Metrics.CODE_COVERAGE;
  }
 
  protected void initCCMetrics() {
    coberturaBranchCoverage = getMetric(Metrics.COBERTURA_BRANCH_COVERAGE);
    coberturaLineCoverage = getMetric(Metrics.COBERTURA_LINE_COVERAGE);
    cloverCoverage = getMetric(Metrics.CLOVER_COVERAGE);
    codeCoverage = getMetric();
  }

  public void execute(Module module, List<Module> directSubmodules) {
    initCCMetrics();
    processAtProjectLevel(module, directSubmodules);
    processAtFileLevel(module);
  }

  private void processAtProjectLevel(Module module, List<Module> directSubmodules) {
    MeasureKey ccKey = new MeasureKey(codeCoverage);
    if (module.getMeasure(ccKey) == null) {
      MeasureKey linesKey = new MeasureKey(getMetric(Metrics.NCSS_NCSS));
      Double cc = getPreferredCodeCoverage(module, null);
      if (cc == null) {
        // average of submodules
        double sum = 0.0;
        long lines = 0;
        for (Module submodule : directSubmodules) {
          Double subLines = submodule.getMeasureValue(linesKey);
          Double subCC = submodule.getMeasureValue(ccKey);
          if (subLines != null && subCC != null) {
            lines += subLines;
            sum += subCC * subLines;
          }
        }
        if (lines > 0) {
          module.createMeasure(ccKey, sum / lines);
        }
      } else {
        module.createMeasure(ccKey, cc);
      }
    }
  }

  private void processAtFileLevel(Module module) {
    // select all cc files first
    List<File> ccFiles = new ArrayList<File>();
    for (MeasureKey key : module.getMeasureKeys()) {
      File ccFile = key.getFile();
      if (isFileCodeCoverageMeasureKey(key) && !ccFiles.contains(ccFile)) {
        ccFiles.add(ccFile);
      }
    }
    // now get the favorite measure for each file and create the measure
    for (File ccFile : ccFiles) {
      MeasureKey ccFileKey = new MeasureKey(codeCoverage, null, null, ccFile);
      Double ccValue = getPreferredCodeCoverage(module, ccFile);
      if ( ccValue != null ) {
        module.createMeasure(ccFileKey, ccValue);
      }
    }
  }

  protected Double getPreferredCodeCoverage(Module module, File file) {
    MeasureKey measureKey = new MeasureKey(cloverCoverage, null, null, file);
    Double cc = module.getMeasureValue(measureKey);
    if (cc == null) {
      measureKey = new MeasureKey(coberturaLineCoverage, null, null, file);
      cc = module.getMeasureValue(measureKey);
      if (cc == null) {
        measureKey = new MeasureKey(coberturaBranchCoverage, null, null, file);
        cc = module.getMeasureValue(measureKey);
      }
    }
    return cc;
  }

  private boolean isFileCodeCoverageMeasureKey(MeasureKey key) {
    return ( key.getMetric() != null && key.getRule() == null &&
             key.getRulesCategory() == null && key.getFile() != null ) &&
           ( key.getMetric().equals(cloverCoverage) ||
             key.getMetric().equals(coberturaLineCoverage) ||
             key.getMetric().equals(coberturaBranchCoverage) );
          
  }
}
TOP

Related Classes of ch.hortis.sonar.core.service.CodeCoverageCalculator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.