Java Code Examples for org.sonar.api.batch.sensor.coverage.NewCoverage#lineHits()

The following examples show how to use org.sonar.api.batch.sensor.coverage.NewCoverage#lineHits() . 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: TsCoverageSensorImpl.java    From SonarTsPlugin with MIT License 6 votes vote down vote up
private void saveZeroValue(InputFile inputFile, SensorContext context, Set<Integer> nonCommentLineNumbers) {
      NewCoverage newCoverage = 
              context
              .newCoverage()
              .ofType(CoverageType.UNIT)
              .onFile(inputFile);

      if (nonCommentLineNumbers != null) {
          for (Integer nonCommentLineNumber : nonCommentLineNumbers) {
              newCoverage.lineHits(nonCommentLineNumber, 0);
          }
      }
      else {
          for (int i = 1; i <= inputFile.lines(); i++) {
              newCoverage.lineHits(i, 0);
          }
      }

      newCoverage.save();
}
 
Example 2
Source File: CoberturaReportParser.java    From sonar-lua with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void collectFileData(SMInputCursor clazz, NewCoverage newCoverage) throws XMLStreamException {
  SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line");
  while (line.getNext() != null) {
    int lineId = Integer.parseInt(line.getAttrValue("number"));
    try {
      newCoverage.lineHits(lineId, (int) ParsingUtils.parseNumber(line.getAttrValue("hits"), Locale.ENGLISH));
    } catch (ParseException e) {
      throw new IllegalStateException(e);
    }

    String isBranch = line.getAttrValue("branch");
    String text = line.getAttrValue("condition-coverage");
    if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) {
      String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/");
      newCoverage.conditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0]));
    }
  }
  newCoverage.save();
}
 
Example 3
Source File: CloverXmlReportParser.java    From sonar-clover with Apache License 2.0 5 votes vote down vote up
private void saveHitsData(InputFile resource, SMInputCursor lineCursor) throws ParseException, XMLStreamException {
    final NewCoverage coverage = context.newCoverage().onFile(resource);
    // cursor should be on the metrics element
    if (!canBeIncludedInFileMetrics(lineCursor)) {
        // cursor should now be on the line cursor; exclude this file if there are no elements to cover
        ((DefaultInputFile) resource).setExcludedForCoverage(true);
    }

    while (lineCursor.getNext() != null) {
        // skip class elements on format 2_3_2
        if (isClass(lineCursor)) {
            continue;
        }
        final int lineId = Integer.parseInt(lineCursor.getAttrValue("num"));
        String count = lineCursor.getAttrValue("count");
        if (StringUtils.isNotBlank(count)) {
            final int hits = Integer.parseInt(count);
            coverage.lineHits(lineId, hits);
        } else {
            int trueCount = (int) ParsingUtils.parseNumber(lineCursor.getAttrValue("truecount"));
            int falseCount = (int) ParsingUtils.parseNumber(lineCursor.getAttrValue("falsecount"));
            int coveredConditions = 0;
            if (trueCount > 0) {
                coveredConditions++;
            }
            if (falseCount > 0) {
                coveredConditions++;
            }

            coverage.conditions(lineId, 2, coveredConditions);
        }
    }

    coverage.save();
}
 
Example 4
Source File: SimpleCovParser.java    From sonar-ruby-plugin with MIT License 4 votes vote down vote up
private void coverageForLine(NewCoverage coverage, int lineNumber, Integer lineCount) {
    if (nonNull(lineCount)) {
        coverage.lineHits(lineNumber + 1, lineCount);
    }
}