aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorJulien HENRY <henryju@yahoo.fr>2016-07-06 16:56:56 +0200
committerJulien HENRY <henryju@yahoo.fr>2016-07-07 09:44:53 +0200
commita4e0563f753bf04f6560f7014a974d2d4f248d84 (patch)
treeb488cf1ae113f32778c69009d8f925d875d90cda /sonar-scanner-engine
parent9cb0a45cbf5c1b2964cb7c095b9f87d94eb59c26 (diff)
downloadsonarqube-a4e0563f753bf04f6560f7014a974d2d4f248d84.tar.gz
sonarqube-a4e0563f753bf04f6560f7014a974d2d4f248d84.zip
SONAR-7783 Ensure Sensors don't save same data twice
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/index/SonarCpdBlockIndex.java6
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java13
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java34
3 files changed, 47 insertions, 6 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/index/SonarCpdBlockIndex.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/index/SonarCpdBlockIndex.java
index 93984a86168..b20e5f2c763 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/index/SonarCpdBlockIndex.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/index/SonarCpdBlockIndex.java
@@ -36,6 +36,7 @@ import org.sonar.duplications.index.CloneIndex;
import org.sonar.duplications.index.PackedMemoryCloneIndex;
import org.sonar.duplications.index.PackedMemoryCloneIndex.ResourceBlocks;
import org.sonar.scanner.index.BatchComponentCache;
+import org.sonar.scanner.protocol.output.FileStructure;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.report.ReportPublisher;
@@ -57,6 +58,9 @@ public class SonarCpdBlockIndex extends AbstractCloneIndex {
public void insert(InputFile inputFile, Collection<Block> blocks) {
if (isCrossProjectDuplicationEnabled(settings)) {
int id = batchComponentCache.get(inputFile).batchId();
+ if (publisher.getWriter().hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, id)) {
+ throw new UnsupportedOperationException("Trying to save CPD tokens twice for the same file is not supported: " + inputFile.absolutePath());
+ }
final ScannerReport.CpdTextBlock.Builder builder = ScannerReport.CpdTextBlock.newBuilder();
publisher.getWriter().writeCpdTextBlocks(id, Iterables.transform(blocks, new Function<Block, ScannerReport.CpdTextBlock>() {
@Override
@@ -110,7 +114,7 @@ public class SonarCpdBlockIndex extends AbstractCloneIndex {
public Iterator<ResourceBlocks> iterator() {
return mem.iterator();
}
-
+
@Override
public int noResources() {
return mem.noResources();
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
index 67fe7d9bc4b..1024e1747cd 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
@@ -58,6 +58,7 @@ import org.sonar.scanner.cpd.index.SonarCpdBlockIndex;
import org.sonar.scanner.index.BatchComponent;
import org.sonar.scanner.index.BatchComponentCache;
import org.sonar.scanner.issue.ModuleIssues;
+import org.sonar.scanner.protocol.output.FileStructure;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
import org.sonar.scanner.report.ReportPublisher;
@@ -191,14 +192,22 @@ public class DefaultSensorStorage implements SensorStorage {
public void store(DefaultHighlighting highlighting) {
ScannerReportWriter writer = reportPublisher.getWriter();
DefaultInputFile inputFile = (DefaultInputFile) highlighting.inputFile();
- writer.writeComponentSyntaxHighlighting(componentCache.get(inputFile).batchId(),
+ int componentRef = componentCache.get(inputFile).batchId();
+ if (writer.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef)) {
+ throw new UnsupportedOperationException("Trying to save highlighting twice for the same file is not supported: " + inputFile.absolutePath());
+ }
+ writer.writeComponentSyntaxHighlighting(componentRef,
Iterables.transform(highlighting.getSyntaxHighlightingRuleSet(), new BuildSyntaxHighlighting()));
}
@Override
public void store(DefaultSymbolTable symbolTable) {
ScannerReportWriter writer = reportPublisher.getWriter();
- writer.writeComponentSymbols(componentCache.get(symbolTable.inputFile()).batchId(),
+ int componentRef = componentCache.get(symbolTable.inputFile()).batchId();
+ if (writer.hasComponentData(FileStructure.Domain.SYMBOLS, componentRef)) {
+ throw new UnsupportedOperationException("Trying to save symbol table twice for the same file is not supported: " + symbolTable.inputFile().absolutePath());
+ }
+ writer.writeComponentSymbols(componentRef,
Iterables.transform(symbolTable.getReferencesBySymbol().entrySet(), new Function<Map.Entry<TextRange, Set<TextRange>>, ScannerReport.Symbol>() {
private ScannerReport.Symbol.Builder builder = ScannerReport.Symbol.newBuilder();
private ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder();
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
index 4f825e9743b..72bbfafac76 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
@@ -29,7 +29,9 @@ import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.measure.MetricFinder;
+import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
+import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
@@ -39,9 +41,9 @@ import org.sonar.api.resources.Resource;
import org.sonar.scanner.cpd.index.SonarCpdBlockIndex;
import org.sonar.scanner.index.BatchComponentCache;
import org.sonar.scanner.issue.ModuleIssues;
+import org.sonar.scanner.protocol.output.ScannerReportWriter;
import org.sonar.scanner.report.ReportPublisher;
import org.sonar.scanner.scan.measure.MeasureCache;
-import org.sonar.scanner.sensor.DefaultSensorStorage;
import org.sonar.scanner.sensor.coverage.CoverageExclusions;
import static org.assertj.core.api.Assertions.assertThat;
@@ -67,7 +69,7 @@ public class DefaultSensorStorageTest {
private BatchComponentCache resourceCache;
@Before
- public void prepare() {
+ public void prepare() throws Exception {
MetricFinder metricFinder = mock(MetricFinder.class);
when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
@@ -78,8 +80,10 @@ public class DefaultSensorStorageTest {
CoverageExclusions coverageExclusions = mock(CoverageExclusions.class);
when(coverageExclusions.accept(any(Resource.class), any(Measure.class))).thenReturn(true);
resourceCache = new BatchComponentCache();
+ ReportPublisher reportPublisher = mock(ReportPublisher.class);
+ when(reportPublisher.getWriter()).thenReturn(new ScannerReportWriter(temp.newFolder()));
underTest = new DefaultSensorStorage(metricFinder,
- moduleIssues, settings, coverageExclusions, resourceCache, mock(ReportPublisher.class), measureCache, mock(SonarCpdBlockIndex.class));
+ moduleIssues, settings, coverageExclusions, resourceCache, reportPublisher, measureCache, mock(SonarCpdBlockIndex.class));
}
@Test
@@ -131,4 +135,28 @@ public class DefaultSensorStorageTest {
assertThat(m.getMetric()).isEqualTo(CoreMetrics.NCLOC);
}
+ @Test(expected = UnsupportedOperationException.class)
+ public void duplicateHighlighting() throws Exception {
+ Resource sonarFile = File.create("src/Foo.java").setEffectiveKey("foo:src/Foo.java");
+ DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.java")
+ .setModuleBaseDir(temp.newFolder().toPath());
+ resourceCache.add(sonarFile, null).setInputComponent(inputFile);
+ DefaultHighlighting h = new DefaultHighlighting(null)
+ .onFile(inputFile);
+ underTest.store(h);
+ underTest.store(h);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void duplicateSymbolTable() throws Exception {
+ Resource sonarFile = File.create("src/Foo.java").setEffectiveKey("foo:src/Foo.java");
+ DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.java")
+ .setModuleBaseDir(temp.newFolder().toPath());
+ resourceCache.add(sonarFile, null).setInputComponent(inputFile);
+ DefaultSymbolTable st = new DefaultSymbolTable(null)
+ .onFile(inputFile);
+ underTest.store(st);
+ underTest.store(st);
+ }
+
}