From 3bebc5fffefe706a322110a76d6098d04aad6e79 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 17 Jun 2016 11:36:49 +0200 Subject: [PATCH] SONAR-7778 support addition of DUPLICATIONS_INDEX.COMPONENT_UUID --- ...rsistCrossProjectDuplicationIndexStep.java | 4 +- ...ProjectDuplicationsRepositoryStepTest.java | 48 ++++++++-------- ...tCrossProjectDuplicationIndexStepTest.java | 7 ++- .../db/duplication/DuplicationUnitDto.java | 10 ++++ .../db/duplication/DuplicationMapper.xml | 7 ++- .../db/duplication/DuplicationDaoTest.java | 2 + .../DuplicationDaoTest/insert-result.xml | 8 ++- .../DuplicationDaoTest/select_candidates.xml | 56 ++++++++++++++++--- .../shouldDeleteSnapshot-result.xml | 8 ++- .../shouldDeleteSnapshot.xml | 16 +++++- .../shouldPurgeSnapshot-result.xml | 10 +++- .../PurgeCommandsTest/shouldPurgeSnapshot.xml | 20 +++++-- 12 files changed, 149 insertions(+), 47 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStep.java index 8a2410363fe..a59c2af1306 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStep.java @@ -100,8 +100,8 @@ public class PersistCrossProjectDuplicationIndexStep implements ComputationStep .setEndLine(block.getEndLine()) .setIndexInFile(indexInFile) .setSnapshotId(dbIdsRepository.getSnapshotId(component)) - .setProjectSnapshotId(projectSnapshotId) - ); + .setComponentUuid(component.getUuid()) + .setProjectSnapshotId(projectSnapshotId)); indexInFile++; } } finally { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadCrossProjectDuplicationsRepositoryStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadCrossProjectDuplicationsRepositoryStepTest.java index 356e33c0b5a..1b84b4591a9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadCrossProjectDuplicationsRepositoryStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadCrossProjectDuplicationsRepositoryStepTest.java @@ -19,8 +19,12 @@ */ package org.sonar.server.computation.step; -import java.util.*; - +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -50,7 +54,11 @@ import org.sonar.server.computation.snapshot.Snapshot; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; import static org.sonar.server.computation.component.Component.Type.FILE; import static org.sonar.server.computation.component.Component.Type.PROJECT; @@ -73,8 +81,7 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { @Rule public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot( ReportComponent.builder(PROJECT, PROJECT_REF) - .addChildren(CURRENT_FILE - ).build()); + .addChildren(CURRENT_FILE).build()); @Rule public BatchReportReaderRule batchReportReader = new BatchReportReaderRule(); @@ -130,7 +137,8 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setEndLine(55) .setIndexInFile(0) .setProjectSnapshotId(otherProjectSnapshot.getId()) - .setSnapshotId(otherFileSnapshot.getId()); + .setSnapshotId(otherFileSnapshot.getId()) + .setComponentUuid(otherFileSnapshot.getComponentUuid()); dbClient.duplicationDao().insert(dbSession, duplicate); dbSession.commit(); @@ -153,17 +161,14 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setIndexInFile(0) .setLines(originBlock.getStartLine(), originBlock.getEndLine()) .setUnit(originBlock.getStartTokenIndex(), originBlock.getEndTokenIndex()) - .build() - ), + .build()), Arrays.asList( new Block.Builder() .setResourceId(otherFIle.getKey()) .setBlockHash(new ByteArray(hash)) .setIndexInFile(duplicate.getIndexInFile()) .setLines(duplicate.getStartLine(), duplicate.getEndLine()) - .build() - ) - ); + .build())); } @Test @@ -199,7 +204,8 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setEndLine(55) .setIndexInFile(0) .setProjectSnapshotId(otherProjectSnapshot.getId()) - .setSnapshotId(otherFileSnapshot.getId()); + .setSnapshotId(otherFileSnapshot.getId()) + .setComponentUuid(otherFileSnapshot.getComponentUuid()); DuplicationUnitDto duplicate2 = new DuplicationUnitDto() .setHash(originBlock2.getHash()) @@ -207,7 +213,8 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setEndLine(35) .setIndexInFile(1) .setProjectSnapshotId(otherProjectSnapshot.getId()) - .setSnapshotId(otherFileSnapshot.getId()); + .setSnapshotId(otherFileSnapshot.getId()) + .setComponentUuid(otherFileSnapshot.getComponentUuid()); dbClient.duplicationDao().insert(dbSession, duplicate1); dbClient.duplicationDao().insert(dbSession, duplicate2); dbSession.commit(); @@ -228,8 +235,7 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setIndexInFile(0) .setLines(originBlock1.getStartLine(), originBlock1.getEndLine()) .setUnit(originBlock1.getStartTokenIndex(), originBlock1.getEndTokenIndex()) - .build() - ); + .build()); assertThat(originBlocksByIndex.get(1)).isEqualTo( new Block.Builder() .setResourceId(CURRENT_FILE_KEY) @@ -237,8 +243,7 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setIndexInFile(1) .setLines(originBlock2.getStartLine(), originBlock2.getEndLine()) .setUnit(originBlock2.getStartTokenIndex(), originBlock2.getEndTokenIndex()) - .build() - ); + .build()); Map duplicationBlocksByIndex = blocksByIndexInFile(duplicationBlocks.getValue()); assertThat(duplicationBlocksByIndex.get(0)).isEqualTo( @@ -247,16 +252,14 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setBlockHash(new ByteArray(originBlock1.getHash())) .setIndexInFile(duplicate1.getIndexInFile()) .setLines(duplicate1.getStartLine(), duplicate1.getEndLine()) - .build() - ); + .build()); assertThat(duplicationBlocksByIndex.get(1)).isEqualTo( new Block.Builder() .setResourceId(otherFIle.getKey()) .setBlockHash(new ByteArray(originBlock2.getHash())) .setIndexInFile(duplicate2.getIndexInFile()) .setLines(duplicate2.getStartLine(), duplicate2.getEndLine()) - .build() - ); + .build()); } @Test @@ -277,7 +280,8 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setEndLine(55) .setIndexInFile(0) .setProjectSnapshotId(otherProjectSnapshot.getId()) - .setSnapshotId(otherFileSnapshot.getId()); + .setSnapshotId(otherFileSnapshot.getId()) + .setComponentUuid(otherFileSnapshot.getComponentUuid()); dbClient.duplicationDao().insert(dbSession, duplicate); dbSession.commit(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStepTest.java index cfee41ef09a..6fcabab19e8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistCrossProjectDuplicationIndexStepTest.java @@ -23,7 +23,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; - import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -91,12 +90,13 @@ public class PersistCrossProjectDuplicationIndexStepTest { underTest.execute(); Map dto = dbTester.selectFirst("select hash as \"hash\", start_line as \"startLine\", end_line as \"endLine\", index_in_file as \"indexInFile\", " + - "snapshot_id as \"snapshotId\", project_snapshot_id as \"projectSnapshotId\" from duplications_index"); + "snapshot_id as \"snapshotId\", component_uuid as \"componentUuid\", project_snapshot_id as \"projectSnapshotId\" from duplications_index"); assertThat(dto.get("hash")).isEqualTo(CPD_TEXT_BLOCK.getHash()); assertThat(dto.get("startLine")).isEqualTo(30L); assertThat(dto.get("endLine")).isEqualTo(45L); assertThat(dto.get("indexInFile")).isEqualTo(0L); assertThat(dto.get("snapshotId")).isEqualTo(FILE_SNAPSHOT_ID); + assertThat(dto.get("componentUuid")).isEqualTo(FILE.getUuid()); assertThat(dto.get("projectSnapshotId")).isEqualTo(PROJECT_SNAPSHOT_ID); } @@ -114,12 +114,13 @@ public class PersistCrossProjectDuplicationIndexStepTest { underTest.execute(); List> dtos = dbTester.select("select hash as \"hash\", start_line as \"startLine\", end_line as \"endLine\", index_in_file as \"indexInFile\", " + - "snapshot_id as \"snapshotId\", project_snapshot_id as \"projectSnapshotId\" from duplications_index"); + "snapshot_id as \"snapshotId\", component_uuid as \"componentUuid\", project_snapshot_id as \"projectSnapshotId\" from duplications_index"); assertThat(dtos).extracting("hash").containsOnly(CPD_TEXT_BLOCK.getHash(), "b1234353e96320ff"); assertThat(dtos).extracting("startLine").containsOnly(30L, 20L); assertThat(dtos).extracting("endLine").containsOnly(45L, 15L); assertThat(dtos).extracting("indexInFile").containsOnly(0L, 1L); assertThat(dtos).extracting("snapshotId").containsOnly(FILE_SNAPSHOT_ID); + assertThat(dtos).extracting("componentUuid").containsOnly(FILE.getUuid()); assertThat(dtos).extracting("projectSnapshotId").containsOnly(PROJECT_SNAPSHOT_ID); } diff --git a/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationUnitDto.java b/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationUnitDto.java index 6cccfd94872..d915fb4466a 100644 --- a/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationUnitDto.java +++ b/sonar-db/src/main/java/org/sonar/db/duplication/DuplicationUnitDto.java @@ -23,6 +23,7 @@ public final class DuplicationUnitDto { private long id; private long snapshotId; + private String componentUuid; private long projectSnapshotId; private String hash; @@ -51,6 +52,15 @@ public final class DuplicationUnitDto { return this; } + public String getComponentUuid() { + return componentUuid; + } + + public DuplicationUnitDto setComponentUuid(String componentUuid) { + this.componentUuid = componentUuid; + return this; + } + public long getProjectSnapshotId() { return projectSnapshotId; } diff --git a/sonar-db/src/main/resources/org/sonar/db/duplication/DuplicationMapper.xml b/sonar-db/src/main/resources/org/sonar/db/duplication/DuplicationMapper.xml index b1ce10e4069..ec02d2e7ef8 100644 --- a/sonar-db/src/main/resources/org/sonar/db/duplication/DuplicationMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/duplication/DuplicationMapper.xml @@ -7,6 +7,7 @@ SELECT DISTINCT duplication_block.id as id, duplication_block.snapshot_id as snapshotId, + duplication_block.component_uuid as componentUuid, duplication_block.project_snapshot_id as projectSnapshotId, duplication_block.hash as hash, duplication_block.index_in_file as indexInFile, @@ -15,7 +16,7 @@ file_component.kee as componentKey FROM duplications_index duplication_block INNER JOIN snapshots snapshot ON duplication_block.snapshot_id=snapshot.id AND snapshot.islast=${_true} - INNER JOIN projects file_component ON file_component.uuid=snapshot.component_uuid AND file_component.language=#{language} + INNER JOIN projects file_component ON file_component.uuid=duplication_block.component_uuid AND file_component.language=#{language} AND file_component.enabled=${_true} AND duplication_block.hash in @@ -27,8 +28,8 @@ - INSERT INTO duplications_index (snapshot_id, project_snapshot_id, hash, index_in_file, start_line, end_line) - VALUES (#{snapshotId}, #{projectSnapshotId}, #{hash}, #{indexInFile}, #{startLine}, #{endLine}) + INSERT INTO duplications_index (snapshot_id, component_uuid, project_snapshot_id, hash, index_in_file, start_line, end_line) + VALUES (#{snapshotId}, #{componentUuid}, #{projectSnapshotId}, #{hash}, #{indexInFile}, #{startLine}, #{endLine}) diff --git a/sonar-db/src/test/java/org/sonar/db/duplication/DuplicationDaoTest.java b/sonar-db/src/test/java/org/sonar/db/duplication/DuplicationDaoTest.java index eeaf8e5e07a..4374de33307 100644 --- a/sonar-db/src/test/java/org/sonar/db/duplication/DuplicationDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/duplication/DuplicationDaoTest.java @@ -49,6 +49,7 @@ public class DuplicationDaoTest { DuplicationUnitDto block = blocks.get(0); assertThat(block.getComponentKey()).isEqualTo("bar-last"); + assertThat(block.getComponentUuid()).isEqualTo("uuid_2"); assertThat(block.getHash()).isEqualTo("aa"); assertThat(block.getIndexInFile()).isEqualTo(0); assertThat(block.getStartLine()).isEqualTo(1); @@ -67,6 +68,7 @@ public class DuplicationDaoTest { dao.insert(dbSession, new DuplicationUnitDto() .setProjectSnapshotId(1) .setSnapshotId(2) + .setComponentUuid("uuid_1") .setHash("bb") .setIndexInFile(0) .setStartLine(1) diff --git a/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/insert-result.xml b/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/insert-result.xml index 61362f40909..072ce7fd586 100644 --- a/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/insert-result.xml +++ b/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/insert-result.xml @@ -14,7 +14,13 @@ project_id="1"/> - diff --git a/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/select_candidates.xml b/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/select_candidates.xml index 32b73f94ea4..ec60a919101 100644 --- a/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/select_candidates.xml +++ b/sonar-db/src/test/resources/org/sonar/db/duplication/DuplicationDaoTest/select_candidates.xml @@ -47,37 +47,79 @@ - - - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteSnapshot-result.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteSnapshot-result.xml index d3ca84c438d..1c9ba52e8f7 100644 --- a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteSnapshot-result.xml +++ b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteSnapshot-result.xml @@ -25,6 +25,12 @@ alert_status="[null]" description="[null]" measure_data="[null]"/> - diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteSnapshot.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteSnapshot.xml index a4330d00591..9263dc9015e 100644 --- a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteSnapshot.xml +++ b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldDeleteSnapshot.xml @@ -25,7 +25,13 @@ alert_status="[null]" description="[null]" measure_data="[null]"/> - @@ -53,7 +59,13 @@ alert_status="[null]" description="[null]" measure_data="[null]"/> - diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldPurgeSnapshot-result.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldPurgeSnapshot-result.xml index 4ac3b5f5707..a6cd4cf7a52 100644 --- a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldPurgeSnapshot-result.xml +++ b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldPurgeSnapshot-result.xml @@ -73,7 +73,13 @@ Note that measures, events and reviews are not deleted. created_at="1228222680000" event_data="[null]"/> - + diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldPurgeSnapshot.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldPurgeSnapshot.xml index 54feba39511..af33f74ab37 100644 --- a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldPurgeSnapshot.xml +++ b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeCommandsTest/shouldPurgeSnapshot.xml @@ -26,8 +26,14 @@ created_at="1228222680000" event_data="[null]"/> - + @@ -59,7 +65,13 @@ created_at="1228222680000" event_data="[null]"/> - + -- 2.39.5