From: Simon Brandhof Date: Thu, 12 Feb 2015 14:36:13 +0000 (+0100) Subject: Ignore source file information that is set on an unknown line X-Git-Tag: 5.1-RC1~297 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8ea9993161abfdfd439b26fd07d6fc5479a8fdb0;p=sonarqube.git Ignore source file information that is set on an unknown line --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java b/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java index 3475e9f6046..aa61c83c77e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java @@ -50,7 +50,7 @@ import java.util.List; import java.util.Map; /** - * Consolidate different caches for the export of report to server. + * Consolidate different caches for the export of file sources to server. * @see org.sonar.server.source.db.FileSourceDb */ public class SourceDataFactory implements BatchComponent { @@ -175,10 +175,13 @@ public class SourceDataFactory implements BatchComponent { for (Measure measure : measures) { Map lineMeasures = KeyValueFormat.parseIntString((String) measure.value()); for (Map.Entry lineMeasure : lineMeasures.entrySet()) { - String value = lineMeasure.getValue(); - if (StringUtils.isNotEmpty(value)) { - FileSourceDb.Line.Builder lineBuilder = to.getLinesBuilder(lineMeasure.getKey() - 1); - op.apply(value, lineBuilder); + int lineIdx = lineMeasure.getKey(); + if (lineIdx <= to.getLinesCount()) { + String value = lineMeasure.getValue(); + if (StringUtils.isNotEmpty(value)) { + FileSourceDb.Line.Builder lineBuilder = to.getLinesBuilder(lineIdx - 1); + op.apply(value, lineBuilder); + } } } } @@ -290,8 +293,8 @@ public class SourceDataFactory implements BatchComponent { } private void writeItem(G item, StringBuilder[] dataPerLine, int currentLineIdx, long startLineOffset, long endLineOffset, RangeItemWriter writer) { - if (startLineOffset == endLineOffset) { - // Do not store empty items + if (startLineOffset == endLineOffset || currentLineIdx > dataPerLine.length) { + // empty items or bad line index return; } if (dataPerLine[currentLineIdx - 1] == null) { @@ -363,8 +366,10 @@ public class SourceDataFactory implements BatchComponent { private void addBlock(int blockId, DuplicationGroup.Block block, FileSourceDb.Data.Builder to) { int currentLine = block.startLine(); for (int i = 0; i < block.length(); i++) { - to.getLinesBuilder(currentLine-1).addDuplications(blockId); - currentLine++; + if (currentLine <= to.getLinesCount()) { + to.getLinesBuilder(currentLine - 1).addDuplications(blockId); + currentLine++; + } } } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java index 2c510d62aac..3ced19c739b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java @@ -113,6 +113,24 @@ public class SourceDataFactoryTest { assertThat(data.getLines(2).getUtLineHits()).isEqualTo(4); } + @Test + public void applyLineMeasure_ignore_bad_line_numbers() throws Exception { + Metric metric = CoreMetrics.COVERAGE_LINE_HITS_DATA; + when(measureCache.byMetric("component_key", metric.key())).thenReturn( + // line 30 does not exist + Arrays.asList(new Measure().setData("30=42").setMetric(metric))); + + sut.applyLineMeasure("component_key", metric.key(), output, new SourceDataFactory.MeasureOperation() { + @Override + public void apply(String value, FileSourceDb.Line.Builder lineBuilder) { + lineBuilder.setUtLineHits(Integer.parseInt(value)); + } + }); + + FileSourceDb.Data data = output.build(); + assertThat(data.getLinesCount()).isEqualTo(3); + } + @Test public void applyLineMeasures() throws Exception { setupLineMeasure(CoreMetrics.SCM_AUTHORS_BY_LINE, "1=him;2=her"); @@ -169,6 +187,22 @@ public class SourceDataFactoryTest { assertThat(data.getLines(2).getDuplicationsList()).containsExactly(2); } + @Test + public void applyDuplications_ignore_bad_lines() throws Exception { + // duplication on 10 lines + DuplicationGroup group1 = new DuplicationGroup(new DuplicationGroup.Block(inputFile.key(), 1, 10)) + .addDuplicate(new DuplicationGroup.Block("anotherFile1", 12, 1)) + .addDuplicate(new DuplicationGroup.Block("anotherFile2", 13, 1)); + when(duplicationCache.byComponent(inputFile.key())).thenReturn(Lists.newArrayList(group1)); + + sut.applyDuplications(inputFile.key(), output); + + FileSourceDb.Data data = output.build(); + assertThat(data.getLines(0).getDuplicationsList()).containsExactly(1); + assertThat(data.getLines(1).getDuplicationsList()).containsExactly(1); + assertThat(data.getLines(2).getDuplicationsList()).containsExactly(1); + } + @Test public void applyHighlighting_missing() throws Exception { when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING)).thenReturn(null); @@ -199,6 +233,22 @@ public class SourceDataFactoryTest { assertThat(data.getLines(2).getHighlighting()).isEqualTo("0,9,c"); } + @Test + public void applyHighlighting_ignore_bad_line() throws Exception { + SyntaxHighlightingData highlighting = new SyntaxHighlightingDataBuilder() + .registerHighlightingRule(0, 4, TypeOfText.ANNOTATION) + .registerHighlightingRule(4, 5, TypeOfText.COMMENT) + .registerHighlightingRule(7, 25, TypeOfText.CONSTANT) + .build(); + when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING)).thenReturn(highlighting); + metadata.setOriginalLineOffsets(new int[] {0, 4, 7, 15}); + + sut.applyHighlighting(inputFile, metadata, output); + + FileSourceDb.Data data = output.build(); + assertThat(data.getLinesCount()).isEqualTo(3); + } + @Test public void applyHighlighting_multiple_lines() throws Exception { SyntaxHighlightingData highlighting = new SyntaxHighlightingDataBuilder()