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;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.mockito.ArgumentCaptor;
29 import org.sonar.api.utils.System2;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.component.ComponentDto;
34 import org.sonar.db.component.ComponentTesting;
35 import org.sonar.db.component.SnapshotDto;
36 import org.sonar.db.component.SnapshotTesting;
37 import org.sonar.db.duplication.DuplicationUnitDto;
38 import org.sonar.duplications.block.Block;
39 import org.sonar.duplications.block.ByteArray;
40 import org.sonar.scanner.protocol.output.ScannerReport;
41 import org.sonar.server.computation.analysis.AnalysisMetadataHolderRule;
42 import org.sonar.server.computation.batch.BatchReportReaderRule;
43 import org.sonar.server.computation.batch.TreeRootHolderRule;
44 import org.sonar.server.computation.component.Component;
45 import org.sonar.server.computation.component.FileAttributes;
46 import org.sonar.server.computation.component.ReportComponent;
47 import org.sonar.server.computation.duplication.CrossProjectDuplicationStatusHolder;
48 import org.sonar.server.computation.duplication.IntegrateCrossProjectDuplications;
49 import org.sonar.server.computation.snapshot.Snapshot;
51 import static java.util.Arrays.asList;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.mockito.Mockito.*;
54 import static org.sonar.server.computation.component.Component.Type.FILE;
55 import static org.sonar.server.computation.component.Component.Type.PROJECT;
57 public class LoadCrossProjectDuplicationsRepositoryStepTest {
60 public ExpectedException thrown = ExpectedException.none();
62 static final String XOO_LANGUAGE = "xoo";
64 static final int PROJECT_REF = 1;
65 static final int FILE_REF = 2;
66 static final String CURRENT_FILE_KEY = "FILE_KEY";
68 static final Component CURRENT_FILE = ReportComponent.builder(FILE, FILE_REF)
69 .setKey(CURRENT_FILE_KEY)
70 .setFileAttributes(new FileAttributes(false, XOO_LANGUAGE))
74 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(
75 ReportComponent.builder(PROJECT, PROJECT_REF)
76 .addChildren(CURRENT_FILE
80 public BatchReportReaderRule batchReportReader = new BatchReportReaderRule();
83 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
85 CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder = mock(CrossProjectDuplicationStatusHolder.class);
88 public DbTester dbTester = DbTester.create(System2.INSTANCE);
90 DbClient dbClient = dbTester.getDbClient();
92 DbSession dbSession = dbTester.getSession();
94 IntegrateCrossProjectDuplications integrateCrossProjectDuplications = mock(IntegrateCrossProjectDuplications.class);
96 Snapshot baseProjectSnapshot;
98 ComputationStep underTest = new LoadCrossProjectDuplicationsRepositoryStep(treeRootHolder, batchReportReader, analysisMetadataHolder, crossProjectDuplicationStatusHolder,
99 integrateCrossProjectDuplications, dbClient);
102 public void setUp() throws Exception {
103 ComponentDto project = ComponentTesting.newProjectDto();
104 dbClient.componentDao().insert(dbSession, project);
105 SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(project);
106 dbClient.snapshotDao().insert(dbSession, projectSnapshot);
109 baseProjectSnapshot = new Snapshot.Builder()
110 .setId(projectSnapshot.getId())
111 .setCreatedAt(projectSnapshot.getCreatedAt())
116 public void call_compute_cpd_on_one_duplication() throws Exception {
117 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
118 analysisMetadataHolder.setBaseProjectSnapshot(baseProjectSnapshot);
120 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
121 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
123 ComponentDto otherFIle = createFile("OTHER_FILE_KEY", otherProject);
124 SnapshotDto otherFileSnapshot = createFileSnapshot(otherFIle, otherProjectSnapshot);
126 String hash = "a8998353e96320ec";
127 DuplicationUnitDto duplicate = new DuplicationUnitDto()
132 .setProjectSnapshotId(otherProjectSnapshot.getId())
133 .setSnapshotId(otherFileSnapshot.getId());
134 dbClient.duplicationDao().insert(dbSession, duplicate);
137 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
141 .setStartTokenIndex(0)
142 .setEndTokenIndex(10)
144 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock));
148 verify(integrateCrossProjectDuplications).computeCpd(CURRENT_FILE,
151 .setResourceId(CURRENT_FILE_KEY)
152 .setBlockHash(new ByteArray(hash))
154 .setLines(originBlock.getStartLine(), originBlock.getEndLine())
155 .setUnit(originBlock.getStartTokenIndex(), originBlock.getEndTokenIndex())
160 .setResourceId(otherFIle.getKey())
161 .setBlockHash(new ByteArray(hash))
162 .setIndexInFile(duplicate.getIndexInFile())
163 .setLines(duplicate.getStartLine(), duplicate.getEndLine())
170 public void call_compute_cpd_on_many_duplication() throws Exception {
171 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
172 analysisMetadataHolder.setBaseProjectSnapshot(baseProjectSnapshot);
174 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
175 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
177 ComponentDto otherFIle = createFile("OTHER_FILE_KEY", otherProject);
178 SnapshotDto otherFileSnapshot = createFileSnapshot(otherFIle, otherProjectSnapshot);
180 ScannerReport.CpdTextBlock originBlock1 = ScannerReport.CpdTextBlock.newBuilder()
181 .setHash("a8998353e96320ec")
184 .setStartTokenIndex(0)
185 .setEndTokenIndex(10)
187 ScannerReport.CpdTextBlock originBlock2 = ScannerReport.CpdTextBlock.newBuilder()
188 .setHash("b1234353e96320ff")
191 .setStartTokenIndex(5)
192 .setEndTokenIndex(15)
194 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock1, originBlock2));
196 DuplicationUnitDto duplicate1 = new DuplicationUnitDto()
197 .setHash(originBlock1.getHash())
201 .setProjectSnapshotId(otherProjectSnapshot.getId())
202 .setSnapshotId(otherFileSnapshot.getId());
204 DuplicationUnitDto duplicate2 = new DuplicationUnitDto()
205 .setHash(originBlock2.getHash())
209 .setProjectSnapshotId(otherProjectSnapshot.getId())
210 .setSnapshotId(otherFileSnapshot.getId());
211 dbClient.duplicationDao().insert(dbSession, duplicate1);
212 dbClient.duplicationDao().insert(dbSession, duplicate2);
217 Class<ArrayList<Block>> listClass = (Class<ArrayList<Block>>) (Class) ArrayList.class;
218 ArgumentCaptor<ArrayList<Block>> originBlocks = ArgumentCaptor.forClass(listClass);
219 ArgumentCaptor<ArrayList<Block>> duplicationBlocks = ArgumentCaptor.forClass(listClass);
221 verify(integrateCrossProjectDuplications).computeCpd(eq(CURRENT_FILE), originBlocks.capture(), duplicationBlocks.capture());
223 Map<Integer, Block> originBlocksByIndex = blocksByIndexInFile(originBlocks.getValue());
224 assertThat(originBlocksByIndex.get(0)).isEqualTo(
226 .setResourceId(CURRENT_FILE_KEY)
227 .setBlockHash(new ByteArray(originBlock1.getHash()))
229 .setLines(originBlock1.getStartLine(), originBlock1.getEndLine())
230 .setUnit(originBlock1.getStartTokenIndex(), originBlock1.getEndTokenIndex())
233 assertThat(originBlocksByIndex.get(1)).isEqualTo(
235 .setResourceId(CURRENT_FILE_KEY)
236 .setBlockHash(new ByteArray(originBlock2.getHash()))
238 .setLines(originBlock2.getStartLine(), originBlock2.getEndLine())
239 .setUnit(originBlock2.getStartTokenIndex(), originBlock2.getEndTokenIndex())
243 Map<Integer, Block> duplicationBlocksByIndex = blocksByIndexInFile(duplicationBlocks.getValue());
244 assertThat(duplicationBlocksByIndex.get(0)).isEqualTo(
246 .setResourceId(otherFIle.getKey())
247 .setBlockHash(new ByteArray(originBlock1.getHash()))
248 .setIndexInFile(duplicate1.getIndexInFile())
249 .setLines(duplicate1.getStartLine(), duplicate1.getEndLine())
252 assertThat(duplicationBlocksByIndex.get(1)).isEqualTo(
254 .setResourceId(otherFIle.getKey())
255 .setBlockHash(new ByteArray(originBlock2.getHash()))
256 .setIndexInFile(duplicate2.getIndexInFile())
257 .setLines(duplicate2.getStartLine(), duplicate2.getEndLine())
263 public void nothing_to_do_when_cross_project_duplication_is_disabled() throws Exception {
264 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(false);
265 analysisMetadataHolder.setBaseProjectSnapshot(baseProjectSnapshot);
267 ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
268 SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
270 ComponentDto otherFIle = createFile("OTHER_FILE_KEY", otherProject);
271 SnapshotDto otherFileSnapshot = createFileSnapshot(otherFIle, otherProjectSnapshot);
273 String hash = "a8998353e96320ec";
274 DuplicationUnitDto duplicate = new DuplicationUnitDto()
279 .setProjectSnapshotId(otherProjectSnapshot.getId())
280 .setSnapshotId(otherFileSnapshot.getId());
281 dbClient.duplicationDao().insert(dbSession, duplicate);
284 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
288 .setStartTokenIndex(0)
289 .setEndTokenIndex(10)
291 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock));
295 verifyZeroInteractions(integrateCrossProjectDuplications);
299 public void nothing_to_do_when_no_cpd_text_blocks_found() throws Exception {
300 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
301 analysisMetadataHolder.setBaseProjectSnapshot(baseProjectSnapshot);
303 batchReportReader.putDuplicationBlocks(FILE_REF, Collections.<ScannerReport.CpdTextBlock>emptyList());
307 verifyZeroInteractions(integrateCrossProjectDuplications);
311 public void nothing_to_do_when_cpd_text_blocks_exists_but_no_duplicated_found() throws Exception {
312 when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
313 analysisMetadataHolder.setBaseProjectSnapshot(baseProjectSnapshot);
315 ScannerReport.CpdTextBlock originBlock = ScannerReport.CpdTextBlock.newBuilder()
316 .setHash("a8998353e96320ec")
319 .setStartTokenIndex(0)
320 .setEndTokenIndex(10)
322 batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock));
326 verifyZeroInteractions(integrateCrossProjectDuplications);
329 private ComponentDto createProject(String projectKey) {
330 ComponentDto project = ComponentTesting.newProjectDto().setKey(projectKey);
331 dbClient.componentDao().insert(dbSession, project);
336 private SnapshotDto createProjectSnapshot(ComponentDto project) {
337 SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(project);
338 dbClient.snapshotDao().insert(dbSession, projectSnapshot);
340 return projectSnapshot;
343 private ComponentDto createFile(String fileKey, ComponentDto project) {
344 ComponentDto file = ComponentTesting.newFileDto(project)
346 .setLanguage(XOO_LANGUAGE);
347 dbClient.componentDao().insert(dbSession, file);
352 private SnapshotDto createFileSnapshot(ComponentDto file, SnapshotDto projectSnapshot) {
353 SnapshotDto fileSnapshot = SnapshotTesting.createForComponent(file, projectSnapshot);
354 dbClient.snapshotDao().insert(dbSession, fileSnapshot);
359 private static Map<Integer, Block> blocksByIndexInFile(List<Block> blocks) {
360 Map<Integer, Block> blocksByIndexInFile = new HashMap<>();
361 for (Block block : blocks) {
362 blocksByIndexInFile.put(block.getIndexInFile(), block);
364 return blocksByIndexInFile;