Java Code Examples for org.sonar.api.batch.sensor.coverage.CoverageType

The following examples show how to use org.sonar.api.batch.sensor.coverage.CoverageType. These examples are extracted from open source projects. 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 Project: SonarTsPlugin   Source File: TsCoverageSensorImpl.java    License: 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 Project: SonarTsPlugin   Source File: TsCoverageSensorImplTest.java    License: MIT License 6 votes vote down vote up
@Test
public void usesNonCommentLinesSetForLinesToCoverMetrics_IfSettingZeroCoverage() {
    Map<InputFile, Set<Integer>> nonCommentLineNumbersByFile = new HashMap<InputFile, Set<Integer>>();
    HashSet<Integer> nonCommentLineNumbers = new HashSet<Integer>();
    nonCommentLineNumbers.add(1);
    nonCommentLineNumbers.add(3);
    nonCommentLineNumbers.add(5);
    
    nonCommentLineNumbersByFile.put(this.file, nonCommentLineNumbers);
    
    this.sensor.execute(this.context, nonCommentLineNumbersByFile);

    // Expect lines 1, 3 and 5 to have zero coverage...
    assertEquals((Integer) 0, this.context.lineHits(this.file.key(), CoverageType.UNIT, 1));
    assertEquals((Integer) 0, this.context.lineHits(this.file.key(), CoverageType.UNIT, 3));
    assertEquals((Integer) 0, this.context.lineHits(this.file.key(), CoverageType.UNIT, 5));
 
    // and lines 2 and 5 to not have any coverage since they're not counted as code
    // according to our supplied map
    assertNull(this.context.lineHits(this.file.key(), CoverageType.UNIT, 2));
    assertNull(this.context.lineHits(this.file.key(), CoverageType.UNIT, 4));
}
 
Example 3
Source Project: sonar-ruby-plugin   Source File: SimpleCovParser.java    License: MIT License 5 votes vote down vote up
private NewCoverage buildCoverageForFile(SensorContext ctx, InputFile file, List<Integer> lineCounts) {
    NewCoverage coverage = ctx.newCoverage()
        .onFile(file)
        .ofType(CoverageType.UNIT);

    if (lineCounts == null || lineCounts.isEmpty()) {
        updateForZeroCoverage(file, coverage, gatherNonCommentLinesOfCodeForFile(file));
    } else {
        IntStream.range(0, lineCounts.size())
            .forEach(idx -> coverageForLine(coverage, idx, lineCounts.get(idx)));
    }

    return coverage;
}
 
Example 4
Source Project: sonar-ruby-plugin   Source File: SimpleCovSensorTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testExecute_EmptyReportPath() {
    SimpleCovSensor sensor = new SimpleCovSensor();
    sensor.execute(context);

    assertThat(context.lineHits(rubyFile.getAbsolutePath(), CoverageType.UNIT, 1)).isNull();
}
 
Example 5
Source Project: sonar-ruby-plugin   Source File: SimpleCovSensorTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testExecute() throws URISyntaxException {
    URL filePath = SimpleCovSensorTest.class.getClassLoader().getResource(".resultset.json");
    File resultFile = new File(filePath.toURI());
    context.settings().setProperty(RubyPlugin.SIMPLECOV_REPORT_PATH, resultFile.getAbsolutePath());

    SimpleCovSensor sensor = new SimpleCovSensor();
    sensor.execute(context);

    assertThat(context.lineHits("myProjectKey:test_controller.rb", CoverageType.UNIT, 1)).isEqualTo(1);
}
 
Example 6
Source Project: sonar-ruby-plugin   Source File: SimpleCovSensorTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testExecute_ZeroOutUnknownFiles() throws URISyntaxException {
    URL filePath = SimpleCovSensorTest.class.getClassLoader().getResource(".resultset.json");
    File resultFile = new File(filePath.toURI());
    context.settings().setProperty(RubyPlugin.SIMPLECOV_REPORT_PATH, resultFile.getAbsolutePath());

    SimpleCovSensor sensor = new SimpleCovSensor();
    sensor.execute(context);

    assertThat(context.lineHits("myProjectKey:unknown_file.rb", CoverageType.UNIT, 1)).isEqualTo(0);
}
 
Example 7
Source Project: SonarTsPlugin   Source File: TsCoverageSensorImplTest.java    License: MIT License 5 votes vote down vote up
@Test
public void savesZeroCoverage_IfNoReportAndSettingEnabled() {
    when(this.settings.getString(TypeScriptPlugin.SETTING_LCOV_REPORT_PATH)).thenReturn("");
    when(this.settings.getBoolean(TypeScriptPlugin.SETTING_FORCE_ZERO_COVERAGE)).thenReturn(true);

    this.sensor.execute(this.context, null);

    for (int i = 1; i <= this.file.lines(); i++) {
        assertEquals((Integer) 0, this.context.lineHits(this.file.key(), CoverageType.UNIT, i));
    }
}
 
Example 8
Source Project: SonarTsPlugin   Source File: TsCoverageSensorImplTest.java    License: MIT License 5 votes vote down vote up
@Test
public void savesNothing_IfNoReportAndSettingDisabled() {
    when(this.settings.getString(TypeScriptPlugin.SETTING_LCOV_REPORT_PATH)).thenReturn("");
    when(this.settings.getBoolean(TypeScriptPlugin.SETTING_FORCE_ZERO_COVERAGE)).thenReturn(false);
    
    this.sensor.execute(this.context, null);

    for (int i = 1; i <= this.file.lines(); i++) {
        assertNull(this.context.lineHits(this.file.key(), CoverageType.UNIT, i));
    }
}
 
Example 9
Source Project: SonarTsPlugin   Source File: TsCoverageSensorImplTest.java    License: MIT License 5 votes vote down vote up
@Test
public void savesNothing_IfNullReportPathAndSettingDisabled() {
    when(this.settings.getString(TypeScriptPlugin.SETTING_LCOV_REPORT_PATH)).thenReturn(null);
    when(this.settings.getBoolean(TypeScriptPlugin.SETTING_FORCE_ZERO_COVERAGE)).thenReturn(false);
    
    this.sensor.execute(this.context, null);

    for (int i = 1; i <= this.file.lines(); i++) {
        assertNull(this.context.lineHits(this.file.key(), CoverageType.UNIT, i));
    }
}
 
Example 10
Source Project: SonarTsPlugin   Source File: TsCoverageSensorImplTest.java    License: MIT License 5 votes vote down vote up
@Test
public void savesZeroCoverage_IfParserOutputsNothingForFile() {
    this.sensor.execute(this.context, null);

    for (int i = 1; i <= this.file.lines(); i++) {
        assertEquals((Integer) 0, this.context.lineHits(this.file.key(), CoverageType.UNIT, i));
    }
}
 
Example 11
Source Project: SonarTsPlugin   Source File: TsCoverageSensorImplTest.java    License: MIT License 5 votes vote down vote up
@Test
public void savesNoCoverage_IfNotFoundFilesAreIgnored() {   
    when(this.settings.getBoolean(TypeScriptPlugin.SETTING_IGNORE_NOT_FOUND)).thenReturn(true);
    this.sensor.execute(this.context, null);
    
    for (int i = 1; i <= this.file.lines(); i++) {
        assertNull(this.context.lineHits(this.file.key(), CoverageType.UNIT, i));
    }
}
 
Example 12
private static void collectFileMeasures(SensorContext context, SMInputCursor clazz) throws XMLStreamException {
  FileSystem fileSystem = context.fileSystem();
  FilePredicates predicates = fileSystem.predicates();
  Map<String, InputFile> inputFileByFilename = new HashMap<>();
  while (clazz.getNext() != null) {
    String fileName = clazz.getAttrValue("filename");

    InputFile inputFile;
    // mxml files are not supported by the plugin
    if (inputFileByFilename.containsKey(fileName)) {
      inputFile = inputFileByFilename.get(fileName);
    } else {
      String key = fileName.startsWith(File.separator) ? fileName : (File.separator + fileName);
      inputFile = fileSystem.inputFile(predicates.and(
        predicates.matchesPathPattern("file:**" + key.replace(File.separator, "/")),
        predicates.hasType(InputFile.Type.MAIN),
        predicates.hasLanguage(Lua.KEY)));
      inputFileByFilename.put(fileName, inputFile);
      if (inputFile == null && !fileName.endsWith(".mxml")) {
        LOG.warn("Cannot save coverage result for file: {}, because resource not found.", fileName);
      }
    }
    if (inputFile != null) {
      collectFileData(
        clazz,
        context.newCoverage()
          .onFile(inputFile)
          .ofType(CoverageType.UNIT)
      );
    } else {
      SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line");
      while (line.getNext() != null) {
        // advance
      }
    }
  }
}
 
Example 13
@Test
public void shouldParseReport() throws Exception {
  DefaultInputFile inputFile = new DefaultInputFile("key", "src/example/file.lua")
    .setLanguage(Lua.KEY)
    .setType(InputFile.Type.MAIN)
    .initMetadata(new FileMetadata().readMetadata(new FileReader(TEST_DIR + "src/example/file.lua")));

  tester.fileSystem().add(inputFile);

  tester.settings().setProperty(LuaPlugin.COBERTURA_REPORT_PATH, "coverage.xml");
  sensor.execute(tester);

  String componentKey = "key:src/example/file.lua";
  Integer[] expectedConditions = {2, null, null, null, null, null, null, null, null, null};
  Integer[] expectedCoveredConditions = {1, null, null, null, null, null, null, null, null, null};
  Integer[] expectedHits = {0, null, null, null, null, null, 0, null, null, null};
  for (int line = 1; line <= expectedConditions.length; line++) {
    assertThat(tester.coveredConditions(componentKey, CoverageType.UNIT, line)).as("line " + line).isEqualTo(expectedCoveredConditions[line - 1]);
    assertThat(tester.conditions(componentKey, CoverageType.UNIT, line)).as("line " + line).isEqualTo(expectedConditions[line - 1]);
    assertThat(tester.lineHits(componentKey, CoverageType.UNIT, line)).as("line " + line).isEqualTo(expectedHits[line - 1]);

    assertThat(tester.coveredConditions(componentKey, CoverageType.IT, line)).isNull();
    assertThat(tester.lineHits(componentKey, CoverageType.IT, line)).isNull();
    assertThat(tester.coveredConditions(componentKey, CoverageType.OVERALL, line)).isNull();
    assertThat(tester.lineHits(componentKey, CoverageType.OVERALL, line)).isNull();
  }
}