3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.step;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.mockito.ArgumentCaptor;
33 import org.sonar.api.utils.System2;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.DbTester;
37 import org.sonar.db.component.ComponentDto;
38 import org.sonar.db.component.ComponentTesting;
39 import org.sonar.db.component.SnapshotDto;
40 import org.sonar.db.component.SnapshotTesting;
41 import org.sonar.db.duplication.DuplicationUnitDto;
42 import org.sonar.duplications.block.Block;
43 import org.sonar.duplications.block.ByteArray;
44 import org.sonar.scanner.protocol.output.ScannerReport;
45 import org.sonar.server.computation.analysis.AnalysisMetadataHolderRule;
46 import org.sonar.server.computation.batch.BatchReportReaderRule;
47 import org.sonar.server.computation.batch.TreeRootHolderRule;
48 import org.sonar.server.computation.component.Component;
49 import org.sonar.server.computation.component.FileAttributes;
50 import org.sonar.server.computation.component.ReportComponent;
51 import org.sonar.server.computation.duplication.CrossProjectDuplicationStatusHolder;
52 import org.sonar.server.computation.duplication.IntegrateCrossProjectDuplications;
53 import org.sonar.server.computation.snapshot.Snapshot;
55 import static java.util.Arrays.asList;
56 import static org.assertj.core.api.Assertions.assertThat;
57 import static org.mockito.Mockito.eq;
58 import static org.mockito.Mockito.mock;
59 import static org.mockito.Mockito.verify;
60 import static org.mockito.Mockito.verifyZeroInteractions;
61 import static org.mockito.Mockito.when;
62 import static org.sonar.server.computation.component.Component.Type.FILE;
63 import static org.sonar.server.computation.component.Component.Type.PROJECT;
65 public class LoadCrossProjectDuplicationsRepositoryStepTest {
68 public ExpectedException thrown = ExpectedException.none();
70 static final String XOO_LANGUAGE = "xoo";
72 static final int PROJECT_REF = 1;
73 static final int FILE_REF = 2;
74 static final String CURRENT_FILE_KEY = "FILE_KEY";
76 static final Component CURRENT_FILE = ReportComponent.builder(FILE, FILE_REF)
77 .setKey(CURRENT_FILE_KEY)
78 .setFileAttributes(new FileAttributes(false, XOO_LANGUAGE))
82 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
83 ReportComponent.builder(PROJECT, PROJECT_REF)
84 .addChildren(CURRENT_FILE).build());
87 public BatchReportReaderRule batchReportReader = new BatchReportReaderRule();
90 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
92 CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder = mock(CrossProjectDuplicationStatusHolder.class);
95 public DbTester dbTester = DbTester.create(System2.INSTANCE);
97 DbClient dbClient = dbTester.getDbClient();
99 DbSession dbSession = dbTester.getSession();
101 IntegrateCrossProjectDuplications integrateCrossProjectDuplications = mock(IntegrateCrossProjectDuplications.class);
103 Snapshot baseProjectSnapshot;
105 ComputationStep underTest = new LoadCrossProjectDuplicationsRepositoryStep(treeRootHolder, batchReportReader, analysisMetadataHolder, crossProjectDuplicationStatusHolder,
106 integrateCrossProjectDuplications, dbClient);
109 public void setUp() throws Exception {
110 ComponentDto project = ComponentTesting.newProjectDto();
111 dbClient.componentDao().insert(dbSession, project);
112 SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(project);
113 dbClient.snapshotDao().insert(dbSession, projectSnapshot);
116 baseProjectSnapshot = new Snapshot.Builder()
117 .setId(projectSnapshot.getId())
118 .setCreatedAt(projectSnapshot.getCreatedAt())
123 public void call_compute_cpd_on_one_duplication() throws Exception {
124 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
125 analysisMetadataHolder.setBaseProjectSnapshot(baseProjectSnapshot);
127 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
128 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
130 ComponentDto otherFIle = createFile("OTHER_FILE_KEY", otherProject);
131 SnapshotDto otherFileSnapshot = createFileSnapshot(otherFIle, otherProjectSnapshot);
133 String hash = "a8998353e96320ec";
134 DuplicationUnitDto duplicate = new DuplicationUnitDto()
139 .setProjectSnapshotId(otherProjectSnapshot.getId())
140 .setSnapshotId(otherFileSnapshot.getId())
141 .setComponentUuid(otherFileSnapshot.getComponentUuid());
142 dbClient.duplicationDao().insert(dbSession, duplicate);
145 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
149 .setStartTokenIndex(0)
150 .setEndTokenIndex(10)
152 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock));
156 verify(integrateCrossProjectDuplications).computeCpd(CURRENT_FILE,
159 .setResourceId(CURRENT_FILE_KEY)
160 .setBlockHash(new ByteArray(hash))
162 .setLines(originBlock.getStartLine(), originBlock.getEndLine())
163 .setUnit(originBlock.getStartTokenIndex(), originBlock.getEndTokenIndex())
167 .setResourceId(otherFIle.getKey())
168 .setBlockHash(new ByteArray(hash))
169 .setIndexInFile(duplicate.getIndexInFile())
170 .setLines(duplicate.getStartLine(), duplicate.getEndLine())
175 public void call_compute_cpd_on_many_duplication() throws Exception {
176 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
177 analysisMetadataHolder.setBaseProjectSnapshot(baseProjectSnapshot);
179 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
180 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
182 ComponentDto otherFIle = createFile("OTHER_FILE_KEY", otherProject);
183 SnapshotDto otherFileSnapshot = createFileSnapshot(otherFIle, otherProjectSnapshot);
185 ScannerReport.CpdTextBlock originBlock1 = ScannerReport.CpdTextBlock.newBuilder()
186 .setHash("a8998353e96320ec")
189 .setStartTokenIndex(0)
190 .setEndTokenIndex(10)
192 ScannerReport.CpdTextBlock originBlock2 = ScannerReport.CpdTextBlock.newBuilder()
193 .setHash("b1234353e96320ff")
196 .setStartTokenIndex(5)
197 .setEndTokenIndex(15)
199 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock1, originBlock2));
201 DuplicationUnitDto duplicate1 = new DuplicationUnitDto()
202 .setHash(originBlock1.getHash())
206 .setProjectSnapshotId(otherProjectSnapshot.getId())
207 .setSnapshotId(otherFileSnapshot.getId())
208 .setComponentUuid(otherFileSnapshot.getComponentUuid());
210 DuplicationUnitDto duplicate2 = new DuplicationUnitDto()
211 .setHash(originBlock2.getHash())
215 .setProjectSnapshotId(otherProjectSnapshot.getId())
216 .setSnapshotId(otherFileSnapshot.getId())
217 .setComponentUuid(otherFileSnapshot.getComponentUuid());
218 dbClient.duplicationDao().insert(dbSession, duplicate1);
219 dbClient.duplicationDao().insert(dbSession, duplicate2);
224 Class<ArrayList<Block>> listClass = (Class<ArrayList<Block>>) (Class) ArrayList.class;
225 ArgumentCaptor<ArrayList<Block>> originBlocks = ArgumentCaptor.forClass(listClass);
226 ArgumentCaptor<ArrayList<Block>> duplicationBlocks = ArgumentCaptor.forClass(listClass);
228 verify(integrateCrossProjectDuplications).computeCpd(eq(CURRENT_FILE), originBlocks.capture(), duplicationBlocks.capture());
230 Map<Integer, Block> originBlocksByIndex = blocksByIndexInFile(originBlocks.getValue());
231 assertThat(originBlocksByIndex.get(0)).isEqualTo(
233 .setResourceId(CURRENT_FILE_KEY)
234 .setBlockHash(new ByteArray(originBlock1.getHash()))
236 .setLines(originBlock1.getStartLine(), originBlock1.getEndLine())
237 .setUnit(originBlock1.getStartTokenIndex(), originBlock1.getEndTokenIndex())
239 assertThat(originBlocksByIndex.get(1)).isEqualTo(
241 .setResourceId(CURRENT_FILE_KEY)
242 .setBlockHash(new ByteArray(originBlock2.getHash()))
244 .setLines(originBlock2.getStartLine(), originBlock2.getEndLine())
245 .setUnit(originBlock2.getStartTokenIndex(), originBlock2.getEndTokenIndex())
248 Map<Integer, Block> duplicationBlocksByIndex = blocksByIndexInFile(duplicationBlocks.getValue());
249 assertThat(duplicationBlocksByIndex.get(0)).isEqualTo(
251 .setResourceId(otherFIle.getKey())
252 .setBlockHash(new ByteArray(originBlock1.getHash()))
253 .setIndexInFile(duplicate1.getIndexInFile())
254 .setLines(duplicate1.getStartLine(), duplicate1.getEndLine())
256 assertThat(duplicationBlocksByIndex.get(1)).isEqualTo(
258 .setResourceId(otherFIle.getKey())
259 .setBlockHash(new ByteArray(originBlock2.getHash()))
260 .setIndexInFile(duplicate2.getIndexInFile())
261 .setLines(duplicate2.getStartLine(), duplicate2.getEndLine())
266 public void nothing_to_do_when_cross_project_duplication_is_disabled() throws Exception {
267 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(false);
268 analysisMetadataHolder.setBaseProjectSnapshot(baseProjectSnapshot);
270 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
271 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
273 ComponentDto otherFIle = createFile("OTHER_FILE_KEY", otherProject);
274 SnapshotDto otherFileSnapshot = createFileSnapshot(otherFIle, otherProjectSnapshot);
276 String hash = "a8998353e96320ec";
277 DuplicationUnitDto duplicate = new DuplicationUnitDto()
282 .setProjectSnapshotId(otherProjectSnapshot.getId())
283 .setSnapshotId(otherFileSnapshot.getId())
284 .setComponentUuid(otherFileSnapshot.getComponentUuid());
285 dbClient.duplicationDao().insert(dbSession, duplicate);
288 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
292 .setStartTokenIndex(0)
293 .setEndTokenIndex(10)
295 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock));
299 verifyZeroInteractions(integrateCrossProjectDuplications);
303 public void nothing_to_do_when_no_cpd_text_blocks_found() throws Exception {
304 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
305 analysisMetadataHolder.setBaseProjectSnapshot(baseProjectSnapshot);
307 batchReportReader.putDuplicationBlocks(FILE_REF, Collections.<ScannerReport.CpdTextBlock>emptyList());
311 verifyZeroInteractions(integrateCrossProjectDuplications);
315 public void nothing_to_do_when_cpd_text_blocks_exists_but_no_duplicated_found() throws Exception {
316 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
317 analysisMetadataHolder.setBaseProjectSnapshot(baseProjectSnapshot);
319 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
320 .setHash("a8998353e96320ec")
323 .setStartTokenIndex(0)
324 .setEndTokenIndex(10)
326 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock));
330 verifyZeroInteractions(integrateCrossProjectDuplications);
333 private ComponentDto createProject(String projectKey) {
334 ComponentDto project = ComponentTesting.newProjectDto().setKey(projectKey);
335 dbClient.componentDao().insert(dbSession, project);
340 private SnapshotDto createProjectSnapshot(ComponentDto project) {
341 SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(project);
342 dbClient.snapshotDao().insert(dbSession, projectSnapshot);
344 return projectSnapshot;
347 private ComponentDto createFile(String fileKey, ComponentDto project) {
348 ComponentDto file = ComponentTesting.newFileDto(project)
350 .setLanguage(XOO_LANGUAGE);
351 dbClient.componentDao().insert(dbSession, file);
356 private SnapshotDto createFileSnapshot(ComponentDto file, SnapshotDto projectSnapshot) {
357 SnapshotDto fileSnapshot = SnapshotTesting.createForComponent(file, projectSnapshot);
358 dbClient.snapshotDao().insert(dbSession, fileSnapshot);
363 private static Map<Integer, Block> blocksByIndexInFile(List<Block> blocks) {
364 Map<Integer, Block> blocksByIndexInFile = new HashMap<>();
365 for (Block block : blocks) {
366 blocksByIndexInFile.put(block.getIndexInFile(), block);
368 return blocksByIndexInFile;