diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-01-25 15:42:58 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-01-27 16:26:30 +0100 |
commit | 27c1900c7e4af804bbcfe2bec4e2152f1d4c87e1 (patch) | |
tree | f216dd96673b9a55cada607459376a8c7d8d841a | |
parent | f3741d7e1fd83961c038f71338f986f35a22720d (diff) | |
download | sonarqube-27c1900c7e4af804bbcfe2bec4e2152f1d4c87e1.tar.gz sonarqube-27c1900c7e4af804bbcfe2bec4e2152f1d4c87e1.zip |
SONAR-8701 Read lines from Component instead of report
4 files changed, 22 insertions, 34 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/source/SourceLinesRepositoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/source/SourceLinesRepositoryImpl.java index 564b619f4d5..0034467f57f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/source/SourceLinesRepositoryImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/source/SourceLinesRepositoryImpl.java @@ -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> { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStep.java index 778c6c9292a..35b99f1036f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStep.java @@ -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) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/source/SourceLinesRepositoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/source/SourceLinesRepositoryImplTest.java index 42cee4c3591..d61335c30e0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/source/SourceLinesRepositoryImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/source/SourceLinesRepositoryImplTest.java @@ -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(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStepTest.java index b267afd4f0b..5b643e08c90 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStepTest.java @@ -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++) { |