diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2018-09-12 11:27:27 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2018-10-10 09:23:03 +0200 |
commit | f31eb69eb58e1be3005a93d432c9136e1eee289d (patch) | |
tree | 360eda355a76e94ba59edfe6093fca55f0f9a84e | |
parent | 2419aab5bd60db808c9136b5dabda1d1d6d2adcb (diff) | |
download | sonarqube-f31eb69eb58e1be3005a93d432c9136e1eee289d.tar.gz sonarqube-f31eb69eb58e1be3005a93d432c9136e1eee289d.zip |
SONAR-11238 record warning when broken symbol data in report
2 files changed, 115 insertions, 14 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/FileSourceDataWarnings.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/FileSourceDataWarnings.java index f43770e9ff5..65b8cd6fa98 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/FileSourceDataWarnings.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/FileSourceDataWarnings.java @@ -32,6 +32,8 @@ import static com.google.common.base.Preconditions.checkState; import static java.lang.String.format; import static java.util.Objects.requireNonNull; import static java.util.stream.Collectors.joining; +import static org.sonar.ce.task.projectanalysis.source.linereader.LineReader.Data.HIGHLIGHTING; +import static org.sonar.ce.task.projectanalysis.source.linereader.LineReader.Data.SYMBOLS; public class FileSourceDataWarnings { private static final Comparator<Component> COMPONENT_COMPARATOR = Comparator.comparingInt(t -> t.getReportAttributes().getRef()); @@ -61,25 +63,31 @@ public class FileSourceDataWarnings { public void commitWarnings() { checkNotCommitted(); this.closed = true; - Set<Component> filesWithHighlightingErrors = fileErrorsPerData.get(LineReader.Data.HIGHLIGHTING); - if (filesWithHighlightingErrors == null) { + createWarning(HIGHLIGHTING, "highlighting"); + createWarning(SYMBOLS, "symbol"); + } + + private void createWarning(LineReader.Data data, String dataWording) { + Set<Component> filesWithErrors = fileErrorsPerData.get(data); + if (filesWithErrors == null) { return; } - taskMessages.add(new CeTaskMessages.Message(computeMessage(filesWithHighlightingErrors), system2.now())); + taskMessages.add(new CeTaskMessages.Message(computeMessage(dataWording, filesWithErrors), system2.now())); } - private static String computeMessage(Set<Component> filesWithHighlightingErrors) { - if (filesWithHighlightingErrors.size() == 1) { - Component file = filesWithHighlightingErrors.iterator().next(); - return format("Inconsistent highlighting data detected on file '%s'. " + + private static String computeMessage(String dataWording, Set<Component> filesWithErrors) { + if (filesWithErrors.size() == 1) { + Component file = filesWithErrors.iterator().next(); + return format("Inconsistent %s data detected on file '%s'. " + "File source may have been modified while analysis was running.", + dataWording, file.getReportAttributes().getPath()); } String lineHeader = "\n ° "; - return format("Inconsistent highlighting data detected on some files (%s in total). " + - "File source may have been modified while analysis was running.", filesWithHighlightingErrors.size()) - + filesWithHighlightingErrors.stream() + return format("Inconsistent %s data detected on some files (%s in total). " + + "File source may have been modified while analysis was running.", dataWording, filesWithErrors.size()) + + filesWithErrors.stream() .sorted(COMPONENT_COMPARATOR) .limit(5) .map(component -> component.getReportAttributes().getPath()) diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/FileSourceDataWarningsTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/FileSourceDataWarningsTest.java index 221cdf6013e..e16505214a1 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/FileSourceDataWarningsTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/FileSourceDataWarningsTest.java @@ -43,6 +43,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; import static org.sonar.ce.task.projectanalysis.source.linereader.LineReader.Data.HIGHLIGHTING; +import static org.sonar.ce.task.projectanalysis.source.linereader.LineReader.Data.SYMBOLS; @RunWith(DataProviderRunner.class) public class FileSourceDataWarningsTest { @@ -191,8 +192,100 @@ public class FileSourceDataWarningsTest { } @Test - @UseDataProvider("anyDataButHighlight") - public void creates_no_warning_when_read_error_for_anything_but_highlighting(LineReader.Data data) { + public void create_symbol_warning_when_one_file_HIGHLIGHT_read_error() { + ReportComponent file = ReportComponent.builder(Component.Type.FILE, 1) + .setUuid("uuid") + .setPath(path) + .build(); + LineReader.ReadError readError = new LineReader.ReadError(SYMBOLS, line); + when(system2.now()).thenReturn(timeStamp); + + underTest.addWarning(file, readError); + + verifyZeroInteractions(taskMessages); + + underTest.commitWarnings(); + + verify(taskMessages, times(1)) + .add(new CeTaskMessages.Message("Inconsistent symbol data detected on file '" + path + "'. " + + "File source may have been modified while analysis was running.", timeStamp)); + } + + @Test + public void create_symbol_warning_when_any_number_of_read_error_for_one_file() { + ReportComponent file = ReportComponent.builder(Component.Type.FILE, 1) + .setUuid("uuid") + .setPath(path) + .build(); + LineReader.ReadError[] readErrors = IntStream.range(0, 1 + random.nextInt(10)) + .mapToObj(i -> new LineReader.ReadError(SYMBOLS, line + i)) + .toArray(LineReader.ReadError[]::new); + when(system2.now()).thenReturn(timeStamp); + + Arrays.stream(readErrors).forEach(readError -> underTest.addWarning(file, readError)); + + verifyZeroInteractions(taskMessages); + + underTest.commitWarnings(); + + verify(taskMessages, times(1)) + .add(new CeTaskMessages.Message("Inconsistent symbol data detected on file '" + path + "'. " + + "File source may have been modified while analysis was running.", timeStamp)); + } + + @Test + public void create_symbol_warning_when_any_number_of_read_error_for_less_than_5_files() { + int fileCount = 2 + random.nextInt(3); + Component[] files = IntStream.range(0, fileCount) + .mapToObj(i -> ReportComponent.builder(Component.Type.FILE, i) + .setUuid("uuid_" + i) + .setPath(path + "_" + i) + .build()) + .toArray(Component[]::new); + when(system2.now()).thenReturn(timeStamp); + + Arrays.stream(files).forEach(file -> IntStream.range(0, 1 + random.nextInt(10)) + .forEach(i -> underTest.addWarning(file, new LineReader.ReadError(SYMBOLS, line + i)))); + + verifyZeroInteractions(taskMessages); + + underTest.commitWarnings(); + + String expectedMessage = "Inconsistent symbol data detected on some files (" + fileCount + " in total). " + + "File source may have been modified while analysis was running." + + Arrays.stream(files).map(f -> f.getReportAttributes().getPath()).collect(Collectors.joining("\n ° ", "\n ° ", "")); + verify(taskMessages, times(1)) + .add(new CeTaskMessages.Message(expectedMessage, timeStamp)); + } + + @Test + public void create_symbol_warning_when_any_number_of_read_error_for_more_than_5_files_only_the_5_first_by_ref() { + int fileCount = 6 + random.nextInt(4); + Component[] files = IntStream.range(0, fileCount) + .mapToObj(i -> ReportComponent.builder(Component.Type.FILE, i) + .setUuid("uuid_" + i) + .setPath(path + "_" + i) + .build()) + .toArray(Component[]::new); + when(system2.now()).thenReturn(timeStamp); + + Arrays.stream(files).forEach(file -> IntStream.range(0, 1 + random.nextInt(10)) + .forEach(i -> underTest.addWarning(file, new LineReader.ReadError(SYMBOLS, line + i)))); + + verifyZeroInteractions(taskMessages); + + underTest.commitWarnings(); + + String expectedMessage = "Inconsistent symbol data detected on some files (" + fileCount + " in total). " + + "File source may have been modified while analysis was running." + + Arrays.stream(files).limit(5).map(f -> f.getReportAttributes().getPath()).collect(Collectors.joining("\n ° ", "\n ° ", "")); + verify(taskMessages, times(1)) + .add(new CeTaskMessages.Message(expectedMessage, timeStamp)); + } + + @Test + @UseDataProvider("anyDataButHighlightAndSymbols") + public void creates_no_warning_when_read_error_for_anything_but_highlighting_and_symbols(LineReader.Data data) { ReportComponent file = ReportComponent.builder(Component.Type.FILE, 1) .setUuid("uuid") .setPath(path) @@ -210,9 +303,9 @@ public class FileSourceDataWarningsTest { } @DataProvider - public static Object[][] anyDataButHighlight() { + public static Object[][] anyDataButHighlightAndSymbols() { return Arrays.stream(LineReader.Data.values()) - .filter(t -> t != HIGHLIGHTING) + .filter(t -> t != HIGHLIGHTING && t != SYMBOLS) .map(t -> new Object[] {t}) .toArray(Object[][]::new); } |