aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2015-03-25 19:01:01 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2015-03-25 19:01:01 +0100
commit46dfa139944cb0ff94e5f05f18a55533b489ef10 (patch)
tree30916ff7677dc84ee67af5f8e4a4195852b55ddb
parentfe13138cef996cfc0ade3252d0452442635f6f92 (diff)
downloadsonarqube-46dfa139944cb0ff94e5f05f18a55533b489ef10.tar.gz
sonarqube-46dfa139944cb0ff94e5f05f18a55533b489ef10.zip
SONAR-6257 Fix duplication issue when there's folder in project structure and add component key in report
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistDuplicationMeasuresStep.java16
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistDuplicationMeasuresStepTest.java128
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java4
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java6
4 files changed, 130 insertions, 24 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistDuplicationMeasuresStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistDuplicationMeasuresStep.java
index 76bad984a1b..f86bc06ab71 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistDuplicationMeasuresStep.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistDuplicationMeasuresStep.java
@@ -23,6 +23,7 @@ package org.sonar.server.computation.step;
import org.apache.commons.lang.StringEscapeUtils;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.resources.Qualifiers;
+import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReportReader;
import org.sonar.core.component.ComponentKeys;
@@ -33,8 +34,6 @@ import org.sonar.core.persistence.MyBatis;
import org.sonar.server.computation.ComputationContext;
import org.sonar.server.db.DbClient;
-import javax.annotation.Nullable;
-
import java.util.List;
public class PersistDuplicationMeasuresStep implements ComputationStep {
@@ -57,23 +56,26 @@ public class PersistDuplicationMeasuresStep implements ComputationStep {
MetricDto duplicationMetric = dbClient.metricDao().selectByKey(session, CoreMetrics.DUPLICATIONS_DATA_KEY);
DuplicationContext duplicationContext = new DuplicationContext(context, duplicationMetric, session);
int rootComponentRef = context.getReportMetadata().getRootComponentRef();
- recursivelyProcessComponent(duplicationContext, null, rootComponentRef);
+ recursivelyProcessComponent(duplicationContext, rootComponentRef, rootComponentRef);
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}
- private void recursivelyProcessComponent(DuplicationContext duplicationContext, @Nullable Integer parentComponentRef, int componentRef) {
+ private void recursivelyProcessComponent(DuplicationContext duplicationContext, int parentModuleRef, int componentRef) {
BatchReportReader reportReader = duplicationContext.context().getReportReader();
BatchReport.Component component = reportReader.readComponent(componentRef);
List<BatchReport.Duplication> duplications = reportReader.readComponentDuplications(componentRef);
- if (!duplications.isEmpty() && parentComponentRef != null) {
- saveDuplications(duplicationContext, reportReader.readComponent(parentComponentRef), component, duplications);
+ if (!duplications.isEmpty()) {
+ saveDuplications(duplicationContext, reportReader.readComponent(parentModuleRef), component, duplications);
}
for (Integer childRef : component.getChildRefList()) {
- recursivelyProcessComponent(duplicationContext, componentRef, childRef);
+ // If current component is a folder, we need to keep the parent reference to module parent
+ int nextParent = !component.getType().equals(Constants.ComponentType.PROJECT) && !component.getType().equals(Constants.ComponentType.MODULE) ?
+ parentModuleRef : componentRef;
+ recursivelyProcessComponent(duplicationContext, nextParent, childRef);
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistDuplicationMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistDuplicationMeasuresStepTest.java
index c6772f5a912..b403045472d 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistDuplicationMeasuresStepTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistDuplicationMeasuresStepTest.java
@@ -131,22 +131,18 @@ public class PersistDuplicationMeasuresStepTest extends BaseStepTest {
BatchReportWriter writer = new BatchReportWriter(reportDir);
writer.writeMetadata(BatchReport.Metadata.newBuilder()
.setRootComponentRef(1)
- .setProjectKey("PROJECT_KEY")
- .setAnalysisDate(150000000L)
.build());
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(1)
.setType(Constants.ComponentType.PROJECT)
- .setUuid("UUID_A")
.setKey("PROJECT_KEY")
.setSnapshotId(10L)
.addChildRef(2)
.build());
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(2)
- .setType(Constants.ComponentType.PROJECT)
- .setUuid("UUID_B")
+ .setType(Constants.ComponentType.MODULE)
.setKey("MODULE_KEY")
.setSnapshotId(11L)
.addChildRef(3)
@@ -154,7 +150,6 @@ public class PersistDuplicationMeasuresStepTest extends BaseStepTest {
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(3)
.setType(Constants.ComponentType.FILE)
- .setUuid("UUID_C")
.setSnapshotId(12L)
.setPath("file")
.build());
@@ -183,6 +178,118 @@ public class PersistDuplicationMeasuresStepTest extends BaseStepTest {
}
@Test
+ public void persist_duplications_on_same_file_linked_on_a_folder() throws Exception {
+ saveDuplicationMetric();
+
+ BatchReportWriter writer = new BatchReportWriter(reportDir);
+ writer.writeMetadata(BatchReport.Metadata.newBuilder()
+ .setRootComponentRef(1)
+ .build());
+
+ writer.writeComponent(BatchReport.Component.newBuilder()
+ .setRef(1)
+ .setType(Constants.ComponentType.PROJECT)
+ .setKey("PROJECT_KEY")
+ .setSnapshotId(10L)
+ .addChildRef(2)
+ .build());
+ writer.writeComponent(BatchReport.Component.newBuilder()
+ .setRef(2)
+ .setType(Constants.ComponentType.DIRECTORY)
+ .setSnapshotId(11L)
+ .addChildRef(3)
+ .setPath("dir")
+ .build());
+ writer.writeComponent(BatchReport.Component.newBuilder()
+ .setRef(3)
+ .setType(Constants.ComponentType.FILE)
+ .setSnapshotId(12L)
+ .setPath("file")
+ .build());
+
+ BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
+ .setOriginBlock(BatchReport.DuplicationBlock.newBuilder()
+ .setOtherComponentRef(3)
+ .setStartLine(1)
+ .setEndLine(5)
+ .build())
+ .addDuplicatedBy(BatchReport.DuplicationBlock.newBuilder()
+ .setOtherComponentRef(3)
+ .setStartLine(6)
+ .setEndLine(10)
+ .build())
+ .build();
+ writer.writeComponentDuplications(3, newArrayList(duplication));
+
+ sut.execute(new ComputationContext(new BatchReportReader(reportDir), ComponentTesting.newProjectDto("PROJECT")));
+
+ assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
+
+ Map<String, Object> dto = dbTester.selectFirst("select snapshot_id as \"snapshotId\", text_value as \"textValue\" from project_measures");
+ assertThat(dto.get("snapshotId")).isEqualTo(12L);
+ assertThat(dto.get("textValue")).isEqualTo("<duplications><g><b s=\"1\" l=\"4\" r=\"PROJECT_KEY:file\"/><b s=\"6\" l=\"4\" r=\"PROJECT_KEY:file\"/></g></duplications>");
+ }
+
+ @Test
+ public void persist_duplications_on_same_file_linked_on_sub_folder() throws Exception {
+ saveDuplicationMetric();
+
+ BatchReportWriter writer = new BatchReportWriter(reportDir);
+ writer.writeMetadata(BatchReport.Metadata.newBuilder()
+ .setRootComponentRef(1)
+ .build());
+
+ writer.writeComponent(BatchReport.Component.newBuilder()
+ .setRef(1)
+ .setType(Constants.ComponentType.PROJECT)
+ .setKey("PROJECT_KEY")
+ .setSnapshotId(10L)
+ .addChildRef(2)
+ .build());
+ writer.writeComponent(BatchReport.Component.newBuilder()
+ .setRef(2)
+ .setType(Constants.ComponentType.DIRECTORY)
+ .setSnapshotId(11L)
+ .addChildRef(3)
+ .setPath("dir")
+ .build());
+ writer.writeComponent(BatchReport.Component.newBuilder()
+ .setRef(3)
+ .setType(Constants.ComponentType.DIRECTORY)
+ .setSnapshotId(12L)
+ .addChildRef(10)
+ .setPath("dir2")
+ .build());
+ writer.writeComponent(BatchReport.Component.newBuilder()
+ .setRef(10)
+ .setType(Constants.ComponentType.FILE)
+ .setSnapshotId(20L)
+ .setPath("file")
+ .build());
+
+ BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
+ .setOriginBlock(BatchReport.DuplicationBlock.newBuilder()
+ .setOtherComponentRef(10)
+ .setStartLine(1)
+ .setEndLine(5)
+ .build())
+ .addDuplicatedBy(BatchReport.DuplicationBlock.newBuilder()
+ .setOtherComponentRef(10)
+ .setStartLine(6)
+ .setEndLine(10)
+ .build())
+ .build();
+ writer.writeComponentDuplications(10, newArrayList(duplication));
+
+ sut.execute(new ComputationContext(new BatchReportReader(reportDir), ComponentTesting.newProjectDto("PROJECT")));
+
+ assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);
+
+ Map<String, Object> dto = dbTester.selectFirst("select snapshot_id as \"snapshotId\", text_value as \"textValue\" from project_measures");
+ assertThat(dto.get("textValue")).isEqualTo("<duplications><g><b s=\"1\" l=\"4\" r=\"PROJECT_KEY:file\"/><b s=\"6\" l=\"4\" r=\"PROJECT_KEY:file\"/></g></duplications>");
+ }
+
+ @Test
public void persist_duplications_on_same_file_when_a_branch_is_used() throws Exception {
saveDuplicationMetric();
@@ -190,15 +297,12 @@ public class PersistDuplicationMeasuresStepTest extends BaseStepTest {
BatchReportWriter writer = new BatchReportWriter(reportDir);
writer.writeMetadata(BatchReport.Metadata.newBuilder()
.setRootComponentRef(1)
- .setProjectKey("PROJECT_KEY")
.setBranch("origin/master")
- .setAnalysisDate(150000000L)
.build());
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(1)
.setType(Constants.ComponentType.PROJECT)
- .setUuid("UUID_A")
.setKey("PROJECT_KEY")
.setSnapshotId(10L)
.addChildRef(2)
@@ -206,7 +310,6 @@ public class PersistDuplicationMeasuresStepTest extends BaseStepTest {
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(2)
.setType(Constants.ComponentType.FILE)
- .setUuid("UUID_B")
.setSnapshotId(11L)
.setPath("file")
.build());
@@ -242,7 +345,6 @@ public class PersistDuplicationMeasuresStepTest extends BaseStepTest {
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(3)
.setType(Constants.ComponentType.FILE)
- .setUuid("UUID_C")
.setSnapshotId(12L)
.setPath("file2")
.build());
@@ -302,14 +404,11 @@ public class PersistDuplicationMeasuresStepTest extends BaseStepTest {
BatchReportWriter writer = new BatchReportWriter(reportDir);
writer.writeMetadata(BatchReport.Metadata.newBuilder()
.setRootComponentRef(1)
- .setProjectKey("PROJECT_KEY")
- .setAnalysisDate(150000000L)
.build());
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(1)
.setType(Constants.ComponentType.PROJECT)
- .setUuid("UUID_A")
.setKey("PROJECT_KEY")
.setSnapshotId(10L)
.addChildRef(2)
@@ -317,7 +416,6 @@ public class PersistDuplicationMeasuresStepTest extends BaseStepTest {
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(2)
.setType(Constants.ComponentType.FILE)
- .setUuid("UUID_B")
.setSnapshotId(11L)
.setPath("file")
.build());
diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java
index 506a7f23765..fa81851088e 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java
@@ -30,10 +30,11 @@ import org.sonar.batch.index.BatchResource;
import org.sonar.batch.index.ResourceCache;
import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.Constants.ComponentLinkType;
-import org.sonar.batch.protocol.output.*;
+import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReport.Component.Builder;
import org.sonar.batch.protocol.output.BatchReport.ComponentLink;
import org.sonar.batch.protocol.output.BatchReport.Event;
+import org.sonar.batch.protocol.output.BatchReportWriter;
import javax.annotation.CheckForNull;
@@ -65,6 +66,7 @@ public class ComponentsPublisher implements ReportPublisher {
// non-null fields
builder.setRef(batchResource.batchId());
builder.setType(getType(r));
+ builder.setKey(r.getKey());
// protocol buffers does not accept null values
diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java
index fb0c2aa4b5f..812d8110339 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java
@@ -36,7 +36,9 @@ import org.sonar.batch.protocol.Constants.ComponentLinkType;
import org.sonar.batch.protocol.Constants.EventCategory;
import org.sonar.batch.protocol.output.BatchReport.Component;
import org.sonar.batch.protocol.output.BatchReport.Event;
-import org.sonar.batch.protocol.output.*;
+import org.sonar.batch.protocol.output.BatchReportReader;
+import org.sonar.batch.protocol.output.BatchReportWriter;
+import org.sonar.batch.protocol.output.FileStructure;
import java.io.File;
import java.util.Arrays;
@@ -114,10 +116,12 @@ public class ComponentsPublisherTest {
BatchReportReader reader = new BatchReportReader(outputDir);
Component rootProtobuf = reader.readComponent(1);
+ assertThat(rootProtobuf.getKey()).isEqualTo("foo");
assertThat(rootProtobuf.getVersion()).isEqualTo("1.0");
assertThat(rootProtobuf.getLinkCount()).isEqualTo(0);
Component module1Protobuf = reader.readComponent(2);
+ assertThat(module1Protobuf.getKey()).isEqualTo("module1");
assertThat(module1Protobuf.getVersion()).isEqualTo("1.0");
}