Skip to content

Commit

Permalink
SONAR-6258 Read/write Highlighting using Streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Apr 9, 2015
1 parent 9a934db commit 7155123
Show file tree
Hide file tree
Showing 18 changed files with 462 additions and 1,318 deletions.
Expand Up @@ -113,7 +113,7 @@ private static LineIterator sourceLinesIterator(File file) {

private List<LineReader> dataLineReaders(BatchReportReader reportReader, int componentRef) {
List<LineReader> lineReaders = newArrayList();
File coverageFile = reportReader.readFileCoverage(componentRef);
File coverageFile = reportReader.readComponentCoverage(componentRef);
if (coverageFile != null) {
lineReaders.add(new CoverageLineReader(new ReportIterator<>(coverageFile, BatchReport.Coverage.PARSER)));
}
Expand Down
Expand Up @@ -22,15 +22,10 @@

import com.google.common.annotations.VisibleForTesting;
import org.sonar.api.resources.Qualifiers;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReportReader;
import org.sonar.server.computation.ComputationContext;

import java.util.List;
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;

/**
* Nothing is persist for the moment. Only Syntax Highlighting are read and not persist for the moment
*/
Expand All @@ -50,46 +45,46 @@ public String[] supportedProjectQualifiers() {
@Override
public void execute(ComputationContext context) {
int rootComponentRef = context.getReportMetadata().getRootComponentRef();
recursivelyProcessComponent(context, rootComponentRef);
}

private void recursivelyProcessComponent(ComputationContext context, int componentRef) {
BatchReportReader reportReader = context.getReportReader();
BatchReport.Component component = reportReader.readComponent(componentRef);
List<BatchReport.SyntaxHighlighting.HighlightingRule> highlightingRules = reportReader.readComponentSyntaxHighlighting(componentRef);
processSyntaxHightlighting(component, highlightingRules);

for (Integer childRef : component.getChildRefList()) {
recursivelyProcessComponent(context, childRef);
}
}

private void processSyntaxHightlighting(BatchReport.Component component, List<BatchReport.SyntaxHighlighting.HighlightingRule> highlightingRules) {
syntaxHighlightingByLineForLastProcessedFile = newHashMap();
if (!highlightingRules.isEmpty()) {
for (BatchReport.SyntaxHighlighting.HighlightingRule highlightingRule : highlightingRules) {
processHighlightingRule(highlightingRule);
}
}
}

private void processHighlightingRule(BatchReport.SyntaxHighlighting.HighlightingRule highlightingRule) {
BatchReport.Range range = highlightingRule.getRange();
int startLine = range.getStartLine();
int endLine = range.getEndLine();
if (startLine != endLine) {
// TODO support syntax highlighting on multiple lines when source will be in compute, in order to be able to know the end line in this case
throw new IllegalStateException("To be implemented : Syntax Highlighting on multiple lines are not supported for the moment");
}
StringBuilder symbolLine = syntaxHighlightingByLineForLastProcessedFile.get(startLine);
if (symbolLine == null) {
symbolLine = new StringBuilder();
syntaxHighlightingByLineForLastProcessedFile.put(startLine, symbolLine);
}
symbolLine.append(range.getStartOffset()).append(OFFSET_SEPARATOR);
symbolLine.append(range.getEndOffset()).append(OFFSET_SEPARATOR);
symbolLine.append(highlightingRule.getType().toString());
// recursivelyProcessComponent(context, rootComponentRef);
}
//
// private void recursivelyProcessComponent(ComputationContext context, int componentRef) {
// BatchReportReader reportReader = context.getReportReader();
// BatchReport.Component component = reportReader.readComponent(componentRef);
// List<BatchReport.SyntaxHighlighting.HighlightingRule> highlightingRules = reportReader.readComponentSyntaxHighlighting(componentRef);
// processSyntaxHightlighting(component, highlightingRules);
//
// for (Integer childRef : component.getChildRefList()) {
// recursivelyProcessComponent(context, childRef);
// }
// }
//
// private void processSyntaxHightlighting(BatchReport.Component component, List<BatchReport.SyntaxHighlighting.HighlightingRule> highlightingRules) {
// syntaxHighlightingByLineForLastProcessedFile = newHashMap();
// if (!highlightingRules.isEmpty()) {
// for (BatchReport.SyntaxHighlighting.HighlightingRule highlightingRule : highlightingRules) {
// processHighlightingRule(highlightingRule);
// }
// }
// }
//
// private void processHighlightingRule(BatchReport.SyntaxHighlighting.HighlightingRule highlightingRule) {
// BatchReport.Range range = highlightingRule.getRange();
// int startLine = range.getStartLine();
// int endLine = range.getEndLine();
// if (startLine != endLine) {
// // TODO support syntax highlighting on multiple lines when source will be in compute, in order to be able to know the end line in this case
// throw new IllegalStateException("To be implemented : Syntax Highlighting on multiple lines are not supported for the moment");
// }
// StringBuilder symbolLine = syntaxHighlightingByLineForLastProcessedFile.get(startLine);
// if (symbolLine == null) {
// symbolLine = new StringBuilder();
// syntaxHighlightingByLineForLastProcessedFile.put(startLine, symbolLine);
// }
// symbolLine.append(range.getStartOffset()).append(OFFSET_SEPARATOR);
// symbolLine.append(range.getEndOffset()).append(OFFSET_SEPARATOR);
// symbolLine.append(highlightingRule.getType().toString());
// }

@VisibleForTesting
Map<Integer, StringBuilder> getSyntaxHighlightingByLine() {
Expand Down
Expand Up @@ -49,11 +49,11 @@ public void setUp() throws Exception {
File dir = temp.newFolder();
BatchReportWriter writer = new BatchReportWriter(dir);

writer.writeFileCoverage(1, newArrayList(
writer.writeComponentCoverage(1, newArrayList(
BatchReport.Coverage.newBuilder()
.setLine(1)
.build()
));
));

file = new FileStructure(dir).fileFor(FileStructure.Domain.COVERAGE, 1);
}
Expand Down
Expand Up @@ -186,7 +186,7 @@ public void persist_coverage() throws Exception {
.setLines(1)
.build());

writer.writeFileCoverage(FILE_REF, newArrayList(BatchReport.Coverage.newBuilder()
writer.writeComponentCoverage(FILE_REF, newArrayList(BatchReport.Coverage.newBuilder()
.setLine(1)
.setConditions(10)
.setUtHits(true)
Expand Down
Expand Up @@ -21,23 +21,14 @@

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReportReader;
import org.sonar.batch.protocol.output.BatchReportWriter;
import org.sonar.core.component.ComponentDto;
import org.sonar.server.component.ComponentTesting;
import org.sonar.server.computation.ComputationContext;

import java.io.File;
import java.io.IOException;

import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class PersistSyntaxHighLightingStepTest extends BaseStepTest {

private static final Integer FILE_REF = 3;
Expand All @@ -60,65 +51,65 @@ protected ComputationStep step() throws IOException {
return step;
}

@Test
public void compute_no_symbol() throws Exception {
initReport();

step.execute(new ComputationContext(new BatchReportReader(reportDir),
ComponentTesting.newProjectDto("PROJECT_A")));

assertThat(step.getSyntaxHighlightingByLine()).isEmpty();
}

@Test
public void compute_syntax_highlighting() throws Exception {
BatchReportWriter writer = initReport();

writer.writeComponentSyntaxHighlighting(FILE_REF, newArrayList(
BatchReport.SyntaxHighlighting.HighlightingRule.newBuilder()
.setRange(BatchReport.Range.newBuilder()
.setStartLine(1)
.setStartOffset(3)
.setEndLine(1)
.setEndOffset(5)
.build())
.setType(Constants.HighlightingType.ANNOTATION)
.build(),
BatchReport.SyntaxHighlighting.HighlightingRule.newBuilder()
.setRange(BatchReport.Range.newBuilder()
.setStartLine(3)
.setStartOffset(6)
.setEndLine(3)
.setEndOffset(7)
.build())
.setType(Constants.HighlightingType.COMMENT)
.build())
);

step.execute(new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class)));

assertThat(step.getSyntaxHighlightingByLine()).hasSize(2);
assertThat(step.getSyntaxHighlightingByLine().get(1).toString()).isEqualTo("3,5,ANNOTATION");
assertThat(step.getSyntaxHighlightingByLine().get(3).toString()).isEqualTo("6,7,COMMENT");
}

@Test(expected = IllegalStateException.class)
public void fail_when_range_is_defined_on_different_line() throws Exception {
BatchReportWriter writer = initReport();

writer.writeComponentSyntaxHighlighting(FILE_REF, newArrayList(
BatchReport.SyntaxHighlighting.HighlightingRule.newBuilder()
.setRange(BatchReport.Range.newBuilder()
.setStartLine(1)
.setStartOffset(3)
.setEndLine(2)
.setEndOffset(2)
.build())
.setType(Constants.HighlightingType.ANNOTATION)
.build()));

step.execute(new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class)));
}
// @Test
// public void compute_no_symbol() throws Exception {
// initReport();
//
// step.execute(new ComputationContext(new BatchReportReader(reportDir),
// ComponentTesting.newProjectDto("PROJECT_A")));
//
// assertThat(step.getSyntaxHighlightingByLine()).isEmpty();
// }
//
// @Test
// public void compute_syntax_highlighting() throws Exception {
// BatchReportWriter writer = initReport();
//
// writer.writeComponentSyntaxHighlighting(FILE_REF, newArrayList(
// BatchReport.SyntaxHighlighting.HighlightingRule.newBuilder()
// .setRange(BatchReport.Range.newBuilder()
// .setStartLine(1)
// .setStartOffset(3)
// .setEndLine(1)
// .setEndOffset(5)
// .build())
// .setType(Constants.HighlightingType.ANNOTATION)
// .build(),
// BatchReport.SyntaxHighlighting.HighlightingRule.newBuilder()
// .setRange(BatchReport.Range.newBuilder()
// .setStartLine(3)
// .setStartOffset(6)
// .setEndLine(3)
// .setEndOffset(7)
// .build())
// .setType(Constants.HighlightingType.COMMENT)
// .build())
// );
//
// step.execute(new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class)));
//
// assertThat(step.getSyntaxHighlightingByLine()).hasSize(2);
// assertThat(step.getSyntaxHighlightingByLine().get(1).toString()).isEqualTo("3,5,ANNOTATION");
// assertThat(step.getSyntaxHighlightingByLine().get(3).toString()).isEqualTo("6,7,COMMENT");
// }
//
// @Test(expected = IllegalStateException.class)
// public void fail_when_range_is_defined_on_different_line() throws Exception {
// BatchReportWriter writer = initReport();
//
// writer.writeComponentSyntaxHighlighting(FILE_REF, newArrayList(
// BatchReport.SyntaxHighlighting.HighlightingRule.newBuilder()
// .setRange(BatchReport.Range.newBuilder()
// .setStartLine(1)
// .setStartOffset(3)
// .setEndLine(2)
// .setEndOffset(2)
// .build())
// .setType(Constants.HighlightingType.ANNOTATION)
// .build()));
//
// step.execute(new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class)));
// }

private BatchReportWriter initReport() {
BatchReportWriter writer = new BatchReportWriter(reportDir);
Expand Down

0 comments on commit 7155123

Please sign in to comment.