diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-01-27 15:41:03 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-01-29 09:59:35 +0100 |
commit | 55e42ef7efda721085d7be809689935f385676fd (patch) | |
tree | f3e32192b5c07ba763f529d25887914d5951c21c | |
parent | bea2847df13b73c7364193419873e3e8b47b5b73 (diff) | |
download | sonarqube-55e42ef7efda721085d7be809689935f385676fd.tar.gz sonarqube-55e42ef7efda721085d7be809689935f385676fd.zip |
SONAR-7013 fail in CE for bad line count in file source
2 files changed, 46 insertions, 15 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/source/SourceLinesRepositoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/source/SourceLinesRepositoryImpl.java index 5bda3e2f408..0453f2496f5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/source/SourceLinesRepositoryImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/source/SourceLinesRepositoryImpl.java @@ -38,34 +38,41 @@ public class SourceLinesRepositoryImpl implements SourceLinesRepository { } @Override - public CloseableIterator<String> readLines(Component component) { - requireNonNull(component, "Component should not be bull"); - checkArgument(component.getType() == FILE, "Component '%s' is not a file", component); + public CloseableIterator<String> readLines(Component file) { + requireNonNull(file, "Component should not be bull"); + checkArgument(file.getType() == FILE, "Component '%s' is not a file", file); - Optional<CloseableIterator<String>> linesIteratorOptional = reportReader.readFileSource(component.getReportAttributes().getRef()); + Optional<CloseableIterator<String>> linesIteratorOptional = reportReader.readFileSource(file.getReportAttributes().getRef()); - checkState(linesIteratorOptional.isPresent(), String.format("File '%s' has no source code", component)); - int numberOfLines = reportReader.readComponent(component.getReportAttributes().getRef()).getLines(); + checkState(linesIteratorOptional.isPresent(), String.format("File '%s' has no source code", file)); + int numberOfLines = reportReader.readComponent(file.getReportAttributes().getRef()).getLines(); CloseableIterator<String> lineIterator = linesIteratorOptional.get(); - return new ComponentLinesCloseableIterator(lineIterator, numberOfLines); + return new ComponentLinesCloseableIterator(file, lineIterator, numberOfLines); } private static class ComponentLinesCloseableIterator extends CloseableIterator<String> { private static final String EXTRA_END_LINE = ""; + + private final Component file; private final CloseableIterator<String> delegate; private final int numberOfLines; private int currentLine = 0; - private boolean addedExtraLine = false; - public ComponentLinesCloseableIterator(CloseableIterator<String> lineIterator, int numberOfLines) { + public ComponentLinesCloseableIterator(Component file, CloseableIterator<String> lineIterator, int numberOfLines) { + this.file = file; this.delegate = lineIterator; this.numberOfLines = numberOfLines; } @Override public boolean hasNext() { - return delegate.hasNext() || (currentLine < numberOfLines && !addedExtraLine); + if (delegate.hasNext()) { + checkState(currentLine < numberOfLines, "Source of file '%s' has at least one more line than the expected number (%s)", file, numberOfLines); + return true; + } + checkState((currentLine + 1) >= numberOfLines, "Source of file '%s' has less lines (%s) than the expected number (%s)", file, currentLine, numberOfLines); + return currentLine < numberOfLines; } @Override @@ -79,7 +86,6 @@ public class SourceLinesRepositoryImpl implements SourceLinesRepository { if (delegate.hasNext()) { return delegate.next(); } - addedExtraLine = true; return EXTRA_END_LINE; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/source/SourceLinesRepositoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/source/SourceLinesRepositoryImplTest.java index 5b5362113b0..5a49562ce7f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/source/SourceLinesRepositoryImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/source/SourceLinesRepositoryImplTest.java @@ -23,6 +23,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.batch.protocol.output.BatchReport; +import org.sonar.core.util.CloseableIterator; import org.sonar.server.computation.batch.BatchReportReaderRule; import org.sonar.server.computation.component.Component; @@ -57,19 +58,33 @@ public class SourceLinesRepositoryImplTest { } @Test - public void read_lines_add_at_most_one_extra_empty_line_when_sourceLine_has_less_elements_then_lineCount() throws Exception { - reportReader.putComponent(createFileBatchComponent(10)); + 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", ""); } @Test - public void read_lines_reads_all_lines_from_sourceLines_when_it_has_more_elements_then_lineCount() throws Exception { + 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)); + } + + @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"); - assertThat(underTest.readLines(FILE)).containsOnly("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 @@ -110,4 +125,14 @@ public class SourceLinesRepositoryImplTest { return BatchReport.Component.newBuilder().setRef(FILE_REF).setLines(lineCount).build(); } + private static void consume(CloseableIterator<String> stringCloseableIterator) { + try{ + while (stringCloseableIterator.hasNext()) { + stringCloseableIterator.next(); + } + } finally { + stringCloseableIterator.close(); + } + } + } |