CoberturaSensor implements Sensor { @Override public void execute(SensorContext context) { if (shouldExecuteOnProject()) { analyse(context); } } CoberturaSensor(Settings settings, FileSystem fileSystem); @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context); void analyse(SensorContext context); @Override String toString();  }
@Test public void should_parse_report() { FilePredicates fp = mock(FilePredicates.class); class CustomFilePredicate implements FilePredicate { final String fileName; CustomFilePredicate(String fileName) { this.fileName = fileName; } @Override public boolean apply(InputFile inputFile) { return true; } } when(fp.hasAbsolutePath(ArgumentMatchers.anyString())).thenAnswer(new Answer<FilePredicate>() { @Override public FilePredicate answer(InvocationOnMock invocation) throws Throwable { return new CustomFilePredicate(invocation.<String>getArgument(0)); } }); FileSystem mockfileSystem = mock(FileSystem.class); when(mockfileSystem.predicates()).thenReturn(fp); when(mockfileSystem.hasFiles(ArgumentMatchers.nullable(FilePredicate.class))).thenReturn(true); Map<String, DefaultInputFile> groovyFilesByName = new HashMap<>(); when(mockfileSystem.inputFile(any(FilePredicate.class))).thenAnswer(new Answer<InputFile>() { boolean firstCall = true; @Override public InputFile answer(InvocationOnMock invocation) throws Throwable { if (firstCall) { firstCall = false; return new DefaultInputFile("", "fake.java").setLanguage("java"); } String fileName = invocation.<CustomFilePredicate>getArgument(0).fileName; DefaultInputFile groovyFile; if (!groovyFilesByName.containsKey(fileName)) { groovyFile = new DefaultInputFile("", fileName).setLanguage(Groovy.KEY).setType(Type.MAIN).setLines(Integer.MAX_VALUE); groovyFilesByName.put(fileName, groovyFile); } return groovyFilesByName.get(fileName); } }); sensor = new CoberturaSensor(settings, mockfileSystem); SensorContextTester context = SensorContextTester.create(new File("")); sensor.execute(context); String filekey = ":/Users/cpicat/myproject/grails-app/domain/AboveEighteenFilters.groovy"; int[] lineHits = {2, 6, 7}; int[] lineNoHits = {9, 10, 11}; for (int line : lineHits) { assertThat(context.lineHits(filekey, CoverageType.UNIT, line)).isEqualTo(1); } for (int line : lineNoHits) { assertThat(context.lineHits(filekey, CoverageType.UNIT, line)).isEqualTo(0); } assertThat(context.lineHits(":/Users/cpicat/myproject/grails-app/domain/com/test/web/EmptyResultException.java", CoverageType.UNIT, 16)).isNull(); }
@Test public void should_not_save_any_measure_if_files_can_not_be_found() { FileSystem mockfileSystem = mock(FileSystem.class); when(mockfileSystem.predicates()).thenReturn(fileSystem.predicates()); when(mockfileSystem.inputFile(any(FilePredicate.class))).thenReturn(null); sensor = new CoberturaSensor(settings, mockfileSystem); SensorContext context = mock(SensorContext.class); sensor.execute(context); Mockito.verify(context, Mockito.never()).newCoverage(); }
@Test public void should_not_parse_report_if_settings_does_not_contain_report_path() { DefaultFileSystem fileSystem = new DefaultFileSystem(new File(".")); fileSystem.add(new DefaultInputFile("", "fake.groovy").setLanguage(Groovy.KEY)); sensor = new CoberturaSensor(new Settings(), fileSystem); SensorContext context = mock(SensorContext.class); sensor.execute(context); Mockito.verify(context, Mockito.never()).newCoverage(); }
@Test public void should_not_parse_report_if_report_does_not_exist() { Settings settings = new Settings(); settings.setProperty(GroovyPlugin.COBERTURA_REPORT_PATH, "org/sonar/plugins/groovy/cobertura/fake-coverage.xml"); DefaultFileSystem fileSystem = new DefaultFileSystem(new File(".")); fileSystem.add(new DefaultInputFile("", "fake.groovy").setLanguage(Groovy.KEY)); sensor = new CoberturaSensor(settings, fileSystem); SensorContext context = mock(SensorContext.class); sensor.execute(context); Mockito.verify(context, Mockito.never()).newCoverage(); }
@Test public void should_use_relative_path_to_get_report() { Settings settings = new Settings(); settings.setProperty(GroovyPlugin.COBERTURA_REPORT_PATH, " DefaultFileSystem fileSystem = new DefaultFileSystem(new File(".")); fileSystem.add(new DefaultInputFile("", "fake.groovy").setLanguage(Groovy.KEY)); sensor = new CoberturaSensor(settings, fileSystem); SensorContext context = mock(SensorContext.class); sensor.execute(context); Mockito.verify(context, Mockito.never()).newCoverage(); }