]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8701 Read lines from Component instead of report
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 25 Jan 2017 14:42:58 +0000 (15:42 +0100)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 27 Jan 2017 15:26:30 +0000 (16:26 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/source/SourceLinesRepositoryImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/source/SourceLinesRepositoryImplTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStepTest.java

index 564b619f4d5f2147a80c4b800010adf817f9e96b..0034467f57f67a7c74195110a159b870f73dab00 100644 (file)
@@ -45,10 +45,9 @@ public class SourceLinesRepositoryImpl implements SourceLinesRepository {
     Optional<CloseableIterator<String>> linesIteratorOptional = reportReader.readFileSource(file.getReportAttributes().getRef());
 
     checkState(linesIteratorOptional.isPresent(), "File '%s' has no source code", file);
-    int numberOfLines = reportReader.readComponent(file.getReportAttributes().getRef()).getLines();
     CloseableIterator<String> lineIterator = linesIteratorOptional.get();
 
-    return new ComponentLinesCloseableIterator(file, lineIterator, numberOfLines);
+    return new ComponentLinesCloseableIterator(file, lineIterator, file.getFileAttributes().getLines());
   }
 
   private static class ComponentLinesCloseableIterator extends CloseableIterator<String> {
index 778c6c9292a6198745c479db8fd5dd0af642e4df..35b99f1036fd6979fb60c133c52139c4d8fdc238 100644 (file)
@@ -118,12 +118,10 @@ public class PersistFileSourcesStep implements ComputationStep {
 
     @Override
     public void visitFile(Component file) {
-      int fileRef = file.getReportAttributes().getRef();
-      ScannerReport.Component component = reportReader.readComponent(fileRef);
       CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
       LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file);
       try {
-        ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, lineReaders.readers(), component.getLines());
+        ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, lineReaders.readers(), file.getFileAttributes().getLines());
         ComputeFileSourceData.Data fileSourceData = computeFileSourceData.compute();
         persistSource(fileSourceData, file.getUuid(), lineReaders.getLatestChange());
       } catch (Exception e) {
index 42cee4c359183005d6d32bbea5506a53721180ee..d61335c30e0cb30b04be495b43eabee9221d5f76 100644 (file)
@@ -23,9 +23,9 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.core.util.CloseableIterator;
-import org.sonar.scanner.protocol.output.ScannerReport;
 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
 import org.sonar.server.computation.task.projectanalysis.component.Component;
+import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
@@ -36,11 +36,6 @@ public class SourceLinesRepositoryImplTest {
   static final String FILE_KEY = "FILE_KEY";
   static final int FILE_REF = 2;
 
-  static final Component FILE = builder(Component.Type.FILE, FILE_REF)
-    .setKey(FILE_KEY)
-    .setUuid(FILE_UUID)
-    .build();
-
   @Rule
   public ExpectedException thrown = ExpectedException.none();
 
@@ -51,50 +46,36 @@ public class SourceLinesRepositoryImplTest {
 
   @Test
   public void read_lines_from_report() throws Exception {
-    reportReader.putComponent(createFileBatchComponent(2));
     reportReader.putFileSourceLines(FILE_REF, "line1", "line2");
 
-    assertThat(underTest.readLines(FILE)).containsOnly("line1", "line2");
+    assertThat(underTest.readLines(createComponent(2))).containsOnly("line1", "line2");
   }
 
   @Test
   public void read_lines_adds_one_extra_empty_line_when_sourceLine_has_elements_count_equals_to_lineCount_minus_1() throws Exception {
-    reportReader.putComponent(createFileBatchComponent(3));
     reportReader.putFileSourceLines(FILE_REF, "line1", "line2");
 
-    assertThat(underTest.readLines(FILE)).containsOnly("line1", "line2", "");
+    assertThat(underTest.readLines(createComponent(3))).containsOnly("line1", "line2", "");
   }
 
   @Test
   public void read_lines_throws_ISE_when_sourceLine_has_less_elements_then_lineCount_minus_1() throws Exception {
-    reportReader.putComponent(createFileBatchComponent(10));
     reportReader.putFileSourceLines(FILE_REF, "line1", "line2");
 
     thrown.expect(IllegalStateException.class);
     thrown.expectMessage("Source of file 'ReportComponent{ref=2, key='FILE_KEY', type=FILE}' has less lines (2) than the expected number (10)");
 
-    consume(underTest.readLines(FILE));
+    consume(underTest.readLines(createComponent(10)));
   }
 
   @Test
   public void read_lines_throws_ISE_when_sourceLines_has_more_elements_then_lineCount() throws Exception {
-    reportReader.putComponent(createFileBatchComponent(2));
     reportReader.putFileSourceLines(FILE_REF, "line1", "line2", "line3");
 
     thrown.expect(IllegalStateException.class);
     thrown.expectMessage("Source of file 'ReportComponent{ref=2, key='FILE_KEY', type=FILE}' has at least one more line than the expected number (2)");
 
-    consume(underTest.readLines(FILE));
-  }
-
-  @Test
-  public void not_fail_to_read_lines_on_empty_file_from_report() throws Exception {
-    // File exist but there's no line
-    reportReader.putComponent(createFileBatchComponent(0));
-    reportReader.putFileSourceLines(FILE_REF);
-
-    // Should not try to read source file from the db
-    assertThat(underTest.readLines(FILE)).isEmpty();
+    consume(underTest.readLines(createComponent(2)));
   }
 
   @Test
@@ -102,7 +83,10 @@ public class SourceLinesRepositoryImplTest {
     thrown.expect(IllegalStateException.class);
     thrown.expectMessage("File 'ReportComponent{ref=2, key='FILE_KEY', type=FILE}' has no source code");
 
-    underTest.readLines(FILE);
+    underTest.readLines(builder(Component.Type.FILE, FILE_REF)
+      .setKey(FILE_KEY)
+      .setUuid(FILE_UUID)
+      .build());
   }
 
   @Test
@@ -121,12 +105,16 @@ public class SourceLinesRepositoryImplTest {
     underTest.readLines(builder(Component.Type.PROJECT, 123).setKey("NotFile").build());
   }
 
-  private static ScannerReport.Component createFileBatchComponent(int lineCount) {
-    return ScannerReport.Component.newBuilder().setRef(FILE_REF).setLines(lineCount).build();
+  private static Component createComponent(int lineCount) {
+    return builder(Component.Type.FILE, FILE_REF)
+      .setKey(FILE_KEY)
+      .setUuid(FILE_UUID)
+      .setFileAttributes(new FileAttributes(false, null, lineCount))
+      .build();
   }
 
   private static void consume(CloseableIterator<String> stringCloseableIterator) {
-    try{
+    try {
       while (stringCloseableIterator.hasNext()) {
         stringCloseableIterator.next();
       }
index b267afd4f0bdb0a8b189b5bd4c3d060636a81ac8..5b643e08c9067dcf1b0b5445e3c8dd677b7953a6 100644 (file)
@@ -36,6 +36,7 @@ import org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType;
 import org.sonar.scanner.protocol.output.ScannerReport.SyntaxHighlightingRule.HighlightingType;
 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
 import org.sonar.server.computation.task.projectanalysis.component.Component;
+import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
 import org.sonar.server.computation.task.projectanalysis.duplication.Duplicate;
@@ -458,7 +459,8 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
   private void initBasicReport(int numberOfLines) {
     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).addChildren(
       ReportComponent.builder(Component.Type.MODULE, 2).setUuid("MODULE").setKey("MODULE_KEY").addChildren(
-        ReportComponent.builder(Component.Type.FILE, FILE_REF).setUuid(FILE_UUID).setKey("MODULE_KEY:src/Foo.java").build())
+        ReportComponent.builder(Component.Type.FILE, FILE_REF).setUuid(FILE_UUID).setKey("MODULE_KEY:src/Foo.java")
+          .setFileAttributes(new FileAttributes(false, null, numberOfLines)).build())
         .build())
       .build());
 
@@ -475,6 +477,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
     reportReader.putComponent(ScannerReport.Component.newBuilder()
       .setRef(FILE_REF)
       .setType(ComponentType.FILE)
+      .setLines(numberOfLines)
       .build());
 
     for (int i = 1; i <= numberOfLines; i++) {