]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11238 record warning when broken symbol data in report
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 12 Sep 2018 09:27:27 +0000 (11:27 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 10 Oct 2018 07:23:03 +0000 (09:23 +0200)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/FileSourceDataWarnings.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/FileSourceDataWarningsTest.java

index f43770e9ff54d5acf9df0b5489ca66a4f90c6c77..65b8cd6fa98aba58cd5ab237bbc717f54acb91e4 100644 (file)
@@ -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())
index 221cdf6013ee7c9b6f4642ffebe3475ff04adb49..e16505214a166cbd09e33ace4580206ceac6a909 100644 (file)
@@ -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);
   }