Browse Source

SONAR-6318 Display component key in warning generated when symbols/highlighting are inconsistent

tags/5.2
Julien Lancelot 8 years ago
parent
commit
1fabcdaf19

+ 7
- 3
server/sonar-server/src/main/java/org/sonar/server/computation/source/HighlightingLineReader.java View File

@@ -30,9 +30,11 @@ import org.sonar.api.utils.log.Loggers;
import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.db.protobuf.DbFileSources;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.source.RangeOffsetConverter.RangeOffsetConverterException;

import static com.google.common.collect.Lists.newArrayList;
import static java.lang.String.format;
import static org.sonar.server.computation.source.RangeOffsetConverter.OFFSET_SEPARATOR;
import static org.sonar.server.computation.source.RangeOffsetConverter.SYMBOLS_SEPARATOR;

@@ -54,13 +56,15 @@ public class HighlightingLineReader implements LineReader {
.put(Constants.HighlightingType.PREPROCESS_DIRECTIVE, "p")
.build();

private final Component file;
private final Iterator<BatchReport.SyntaxHighlighting> lineHighlightingIterator;
private final RangeOffsetConverter rangeOffsetConverter;
private final List<BatchReport.SyntaxHighlighting> highlightingList;

private BatchReport.SyntaxHighlighting currentItem;

public HighlightingLineReader(Iterator<BatchReport.SyntaxHighlighting> lineHighlightingIterator, RangeOffsetConverter rangeOffsetConverter) {
public HighlightingLineReader(Component file, Iterator<BatchReport.SyntaxHighlighting> lineHighlightingIterator, RangeOffsetConverter rangeOffsetConverter) {
this.file = file;
this.lineHighlightingIterator = lineHighlightingIterator;
this.rangeOffsetConverter = rangeOffsetConverter;
this.highlightingList = newArrayList();
@@ -75,7 +79,7 @@ public class HighlightingLineReader implements LineReader {
processHighlightings(lineBuilder);
} catch (RangeOffsetConverterException e) {
isHighlightingValid = false;
LOG.warn("Inconsistency detected in Highlighting data. Highlighting will be ignored", e);
LOG.warn(format("Inconsistency detected in Highlighting data. Highlighting will be ignored for file '%s'", file.getKey()), e);
}
}

@@ -120,7 +124,7 @@ public class HighlightingLineReader implements LineReader {
if (cssClass != null) {
return cssClass;
} else {
throw new IllegalArgumentException(String.format("Unknown type %s ", type.toString()));
throw new IllegalArgumentException(format("Unknown type %s ", type.toString()));
}
}


+ 6
- 2
server/sonar-server/src/main/java/org/sonar/server/computation/source/SymbolsLineReader.java View File

@@ -35,7 +35,9 @@ import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.db.protobuf.DbFileSources;
import org.sonar.server.computation.component.Component;

import static java.lang.String.format;
import static org.sonar.server.computation.source.RangeOffsetConverter.OFFSET_SEPARATOR;
import static org.sonar.server.computation.source.RangeOffsetConverter.SYMBOLS_SEPARATOR;

@@ -43,13 +45,15 @@ public class SymbolsLineReader implements LineReader {

private static final Logger LOG = Loggers.get(HighlightingLineReader.class);

private final Component file;
private final RangeOffsetConverter rangeOffsetConverter;
private final List<BatchReport.Symbol> symbols;
private final Map<BatchReport.Symbol, Integer> idsBySymbol;

private boolean areSymbolsValid = true;

public SymbolsLineReader(Iterator<BatchReport.Symbol> symbols, RangeOffsetConverter rangeOffsetConverter) {
public SymbolsLineReader(Component file, Iterator<BatchReport.Symbol> symbols, RangeOffsetConverter rangeOffsetConverter) {
this.file = file;
this.rangeOffsetConverter = rangeOffsetConverter;
this.symbols = Lists.newArrayList(symbols);
// Sort symbols to have deterministic results and avoid false variation that would lead to an unnecessary update of the source files
@@ -68,7 +72,7 @@ public class SymbolsLineReader implements LineReader {
processSymbols(lineBuilder);
} catch (RangeOffsetConverter.RangeOffsetConverterException e) {
areSymbolsValid = false;
LOG.warn("Inconsistency detected in Symbols data. Symbols will be ignored", e);
LOG.warn(format("Inconsistency detected in Symbols data. Symbols will be ignored for file '%s'", file.getKey()), e);
}
}


+ 2
- 2
server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistFileSourcesStep.java View File

@@ -219,11 +219,11 @@ public class PersistFileSourcesStep implements ComputationStep {
RangeOffsetConverter rangeOffsetConverter = new RangeOffsetConverter();
CloseableIterator<BatchReport.SyntaxHighlighting> highlightingIt = reportReader.readComponentSyntaxHighlighting(componentRef);
closeables.add(highlightingIt);
readers.add(new HighlightingLineReader(highlightingIt, rangeOffsetConverter));
readers.add(new HighlightingLineReader(component, highlightingIt, rangeOffsetConverter));

CloseableIterator<BatchReport.Symbol> symbolsIt = reportReader.readComponentSymbols(componentRef);
closeables.add(symbolsIt);
readers.add(new SymbolsLineReader(symbolsIt, rangeOffsetConverter));
readers.add(new SymbolsLineReader(component, symbolsIt, rangeOffsetConverter));

CloseableIterator<BatchReport.Duplication> duplicationsIt = reportReader.readComponentDuplications(componentRef);
closeables.add(duplicationsIt);

+ 16
- 1
server/sonar-server/src/test/java/org/sonar/server/computation/source/HighlightingLineReaderTest.java View File

@@ -32,6 +32,7 @@ import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReport.TextRange;
import org.sonar.db.protobuf.DbFileSources;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.source.RangeOffsetConverter.RangeOffsetConverterException;

import static com.google.common.collect.ImmutableMap.of;
@@ -45,12 +46,15 @@ import static org.sonar.batch.protocol.Constants.HighlightingType.COMMENT;
import static org.sonar.batch.protocol.Constants.HighlightingType.CONSTANT;
import static org.sonar.batch.protocol.Constants.HighlightingType.HIGHLIGHTING_STRING;
import static org.sonar.db.protobuf.DbFileSources.Data.newBuilder;
import static org.sonar.server.computation.component.ReportComponent.builder;

public class HighlightingLineReaderTest {

@Rule
public LogTester logTester = new LogTester();

static final Component FILE = builder(Component.Type.FILE, 1).setUuid("FILE_UUID").setKey("FILE_KEY").build();

static final int DEFAULT_LINE_LENGTH = 5;

static final int LINE_1 = 1;
@@ -227,6 +231,17 @@ public class HighlightingLineReaderTest {
assertThat(logTester.logs(WARN)).isNotEmpty();
}

@Test
public void display_file_key_in_warning_when_range_offset_converter_throw_RangeOffsetConverterException() {
TextRange textRange1 = newTextRange(LINE_1, LINE_1);
doThrow(RangeOffsetConverterException.class).when(rangeOffsetConverter).offsetToString(textRange1, LINE_1, DEFAULT_LINE_LENGTH);
HighlightingLineReader highlightingLineReader = newReader(of(textRange1, ANNOTATION));

highlightingLineReader.read(line1);

assertThat(logTester.logs(WARN)).containsOnly("Inconsistency detected in Highlighting data. Highlighting will be ignored for file 'FILE_KEY'");
}

private HighlightingLineReader newReader(Map<TextRange, Constants.HighlightingType> textRangeByType) {
List<BatchReport.SyntaxHighlighting> syntaxHighlightingList = new ArrayList<>();
for (Map.Entry<TextRange, Constants.HighlightingType> entry : textRangeByType.entrySet()) {
@@ -235,7 +250,7 @@ public class HighlightingLineReaderTest {
.setType(entry.getValue())
.build());
}
return new HighlightingLineReader(syntaxHighlightingList.iterator(), rangeOffsetConverter);
return new HighlightingLineReader(FILE, syntaxHighlightingList.iterator(), rangeOffsetConverter);
}

private static TextRange newTextRange(int startLine, int enLine) {

+ 16
- 1
server/sonar-server/src/test/java/org/sonar/server/computation/source/SymbolsLineReaderTest.java View File

@@ -27,18 +27,22 @@ import org.sonar.api.utils.log.LogTester;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReport.TextRange;
import org.sonar.db.protobuf.DbFileSources;
import org.sonar.server.computation.component.Component;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.api.utils.log.LoggerLevel.WARN;
import static org.sonar.server.computation.component.ReportComponent.builder;

public class SymbolsLineReaderTest {

@Rule
public LogTester logTester = new LogTester();

static final Component FILE = builder(Component.Type.FILE, 1).setUuid("FILE_UUID").setKey("FILE_KEY").build();

static final int DEFAULT_LINE_LENGTH = 5;

static final int LINE_1 = 1;
@@ -285,6 +289,17 @@ public class SymbolsLineReaderTest {
assertThat(logTester.logs(WARN)).isNotEmpty();
}

@Test
public void display_file_key_in_warning_when_range_offset_converter_throw_RangeOffsetConverterException() {
TextRange declaration = newTextRange(LINE_1, LINE_1, OFFSET_1, OFFSET_3);
doThrow(RangeOffsetConverter.RangeOffsetConverterException.class).when(rangeOffsetConverter).offsetToString(declaration, LINE_1, DEFAULT_LINE_LENGTH);
SymbolsLineReader symbolsLineReader = newReader(newSymbol(declaration, newSingleLineTextRangeWithExpectedLabel(LINE_2, OFFSET_1, OFFSET_3, RANGE_LABEL_2)));

symbolsLineReader.read(line1);

assertThat(logTester.logs(WARN)).containsOnly("Inconsistency detected in Symbols data. Symbols will be ignored for file 'FILE_KEY'");
}

private BatchReport.Symbol newSymbol(TextRange declaration, TextRange... references) {
BatchReport.Symbol.Builder builder = BatchReport.Symbol.newBuilder()
.setDeclaration(declaration);
@@ -295,7 +310,7 @@ public class SymbolsLineReaderTest {
}

private SymbolsLineReader newReader(BatchReport.Symbol... symbols) {
return new SymbolsLineReader(Arrays.asList(symbols).iterator(), rangeOffsetConverter);
return new SymbolsLineReader(FILE, Arrays.asList(symbols).iterator(), rangeOffsetConverter);
}

private TextRange newSingleLineTextRangeWithExpectedLabel(int line, int startOffset, int endOffset, String rangeLabel) {

Loading…
Cancel
Save