3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info 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.ce.task.projectanalysis.duplication;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.config.internal.MapSettings;
29 import org.sonar.api.utils.log.LogTester;
30 import org.sonar.api.utils.log.LoggerLevel;
31 import org.sonar.ce.task.projectanalysis.component.Component;
32 import org.sonar.ce.task.projectanalysis.component.FileAttributes;
33 import org.sonar.duplications.block.Block;
34 import org.sonar.duplications.block.ByteArray;
36 import static com.google.common.base.Strings.padStart;
37 import static java.util.Arrays.asList;
38 import static java.util.Collections.singletonList;
39 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
42 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
44 public class IntegrateCrossProjectDuplicationsTest {
47 public LogTester logTester = new LogTester();
49 public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create();
51 static final String XOO_LANGUAGE = "xoo";
53 static final String ORIGIN_FILE_KEY = "ORIGIN_FILE_KEY";
54 static final Component ORIGIN_FILE = builder(FILE, 1)
55 .setKey(ORIGIN_FILE_KEY)
56 .setFileAttributes(new FileAttributes(false, XOO_LANGUAGE, 1))
59 static final String OTHER_FILE_KEY = "OTHER_FILE_KEY";
61 MapSettings settings = new MapSettings();
63 IntegrateCrossProjectDuplications underTest = new IntegrateCrossProjectDuplications(settings.asConfig(), duplicationRepository);
66 public void add_duplications_from_two_blocks() {
67 settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
69 Collection<Block> originBlocks = asList(
71 .setResourceId(ORIGIN_FILE_KEY)
72 .setBlockHash(new ByteArray("a8998353e96320ec"))
78 .setResourceId(ORIGIN_FILE_KEY)
79 .setBlockHash(new ByteArray("2b5747f0e4c59124"))
85 Collection<Block> duplicatedBlocks = asList(
87 .setResourceId(OTHER_FILE_KEY)
88 .setBlockHash(new ByteArray("a8998353e96320ec"))
93 .setResourceId(OTHER_FILE_KEY)
94 .setBlockHash(new ByteArray("2b5747f0e4c59124"))
99 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
101 assertThat(duplicationRepository.getDuplications(ORIGIN_FILE))
103 crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55)));
107 public void add_duplications_from_a_single_block() {
108 settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
110 Collection<Block> originBlocks = singletonList(
111 // This block contains 11 tokens -> a duplication will be created
113 .setResourceId(ORIGIN_FILE_KEY)
114 .setBlockHash(new ByteArray("a8998353e96320ec"))
120 Collection<Block> duplicatedBlocks = singletonList(
122 .setResourceId(OTHER_FILE_KEY)
123 .setBlockHash(new ByteArray("a8998353e96320ec"))
128 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
130 assertThat(duplicationRepository.getDuplications(ORIGIN_FILE))
132 crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55)));
136 public void add_no_duplication_from_current_file() {
137 settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
139 Collection<Block> originBlocks = asList(
141 .setResourceId(ORIGIN_FILE_KEY)
142 .setBlockHash(new ByteArray("a8998353e96320ec"))
147 // Duplication is on the same file
149 .setResourceId(ORIGIN_FILE_KEY)
150 .setBlockHash(new ByteArray("a8998353e96320ec"))
156 Collection<Block> duplicatedBlocks = singletonList(
158 .setResourceId(OTHER_FILE_KEY)
159 .setBlockHash(new ByteArray("a8998353e96320ed"))
164 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
166 assertNoDuplicationAdded(ORIGIN_FILE);
170 public void add_no_duplication_when_not_enough_tokens() {
171 settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
173 Collection<Block> originBlocks = singletonList(
174 // This block contains 5 tokens -> not enough to consider it as a duplication
176 .setResourceId(ORIGIN_FILE_KEY)
177 .setBlockHash(new ByteArray("a8998353e96320ec"))
183 Collection<Block> duplicatedBlocks = singletonList(
185 .setResourceId(OTHER_FILE_KEY)
186 .setBlockHash(new ByteArray("a8998353e96320ec"))
191 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
193 assertNoDuplicationAdded(ORIGIN_FILE);
197 public void add_no_duplication_when_no_duplicated_blocks() {
198 settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
200 Collection<Block> originBlocks = singletonList(
202 .setResourceId(ORIGIN_FILE_KEY)
203 .setBlockHash(new ByteArray("a8998353e96320ec"))
209 underTest.computeCpd(ORIGIN_FILE, originBlocks, Collections.emptyList());
211 assertNoDuplicationAdded(ORIGIN_FILE);
215 public void add_duplication_for_java_even_when_no_token() {
216 Component javaFile = builder(FILE, 1)
217 .setKey(ORIGIN_FILE_KEY)
218 .setFileAttributes(new FileAttributes(false, "java", 10))
221 Collection<Block> originBlocks = singletonList(
222 // This block contains 0 token
224 .setResourceId(ORIGIN_FILE_KEY)
225 .setBlockHash(new ByteArray("a8998353e96320ec"))
231 Collection<Block> duplicatedBlocks = singletonList(
233 .setResourceId(OTHER_FILE_KEY)
234 .setBlockHash(new ByteArray("a8998353e96320ec"))
239 underTest.computeCpd(javaFile, originBlocks, duplicatedBlocks);
241 assertThat(duplicationRepository.getDuplications(ORIGIN_FILE))
243 crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55)));
247 public void default_minimum_tokens_is_one_hundred() {
248 settings.setProperty("sonar.cpd.xoo.minimumTokens", (Integer) null);
250 Collection<Block> originBlocks = singletonList(
252 .setResourceId(ORIGIN_FILE_KEY)
253 .setBlockHash(new ByteArray("a8998353e96320ec"))
259 Collection<Block> duplicatedBlocks = singletonList(
261 .setResourceId(OTHER_FILE_KEY)
262 .setBlockHash(new ByteArray("a8998353e96320ec"))
267 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
269 assertThat(duplicationRepository.getDuplications(ORIGIN_FILE))
271 crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55)));
275 public void do_not_compute_more_than_one_hundred_duplications_when_too_many_duplicated_references() {
276 Collection<Block> originBlocks = new ArrayList<>();
277 Collection<Block> duplicatedBlocks = new ArrayList<>();
279 Block.Builder blockBuilder = new Block.Builder()
280 .setResourceId(ORIGIN_FILE_KEY)
281 .setBlockHash(new ByteArray("a8998353e96320ec"))
285 originBlocks.add(blockBuilder.build());
287 // Generate more than 100 duplications of the same block
288 for (int i = 0; i < 110; i++) {
289 duplicatedBlocks.add(
291 .setResourceId(randomAlphanumeric(16))
295 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
297 assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly(
298 "Too many duplication references on file " + ORIGIN_FILE_KEY + " for block at line 30. Keeping only the first 100 references.");
299 Iterable<Duplication> duplications = duplicationRepository.getDuplications(ORIGIN_FILE);
300 assertThat(duplications).hasSize(1);
301 assertThat(duplications.iterator().next().getDuplicates()).hasSize(100);
305 public void do_not_compute_more_than_one_hundred_duplications_when_too_many_duplications() {
306 Collection<Block> originBlocks = new ArrayList<>();
307 Collection<Block> duplicatedBlocks = new ArrayList<>();
309 Block.Builder blockBuilder = new Block.Builder()
314 // Generate more than 100 duplication on different files
315 for (int i = 0; i < 110; i++) {
316 String hash = padStart("hash" + i, 16, 'a');
319 .setResourceId(ORIGIN_FILE_KEY)
320 .setBlockHash(new ByteArray(hash))
322 duplicatedBlocks.add(
324 .setResourceId("resource" + i)
325 .setBlockHash(new ByteArray(hash))
329 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
331 assertThat(duplicationRepository.getDuplications(ORIGIN_FILE)).hasSize(100);
332 assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Too many duplication groups on file " + ORIGIN_FILE_KEY + ". Keeping only the first 100 groups.");
336 public void log_warning_if_this_deprecated_feature_is_enabled() {
337 settings.setProperty("sonar.cpd.cross_project", "true");
339 new IntegrateCrossProjectDuplications(settings.asConfig(), duplicationRepository);
341 assertThat(logTester.logs()).containsExactly("This analysis uses the deprecated cross-project duplication feature.");
344 private static Duplication crossProjectDuplication(TextBlock original, String otherFileKey, TextBlock duplicate) {
345 return new Duplication(original, Arrays.asList(new CrossProjectDuplicate(otherFileKey, duplicate)));
348 private void assertNoDuplicationAdded(Component file) {
349 assertThat(duplicationRepository.getDuplications(file)).isEmpty();