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.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.Settings;
29 import org.sonar.api.utils.log.LogTester;
30 import org.sonar.api.utils.log.LoggerLevel;
31 import org.sonar.duplications.block.Block;
32 import org.sonar.duplications.block.ByteArray;
33 import org.sonar.server.computation.task.projectanalysis.component.Component;
34 import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
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.server.computation.task.projectanalysis.component.Component.Type.FILE;
42 import static org.sonar.server.computation.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))
59 static final String OTHER_FILE_KEY = "OTHER_FILE_KEY";
61 Settings settings = new Settings();
63 IntegrateCrossProjectDuplications underTest = new IntegrateCrossProjectDuplications(settings, 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"))
86 Collection<Block> duplicatedBlocks = asList(
88 .setResourceId(OTHER_FILE_KEY)
89 .setBlockHash(new ByteArray("a8998353e96320ec"))
94 .setResourceId(OTHER_FILE_KEY)
95 .setBlockHash(new ByteArray("2b5747f0e4c59124"))
100 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
102 assertThat(duplicationRepository.getDuplications(ORIGIN_FILE))
104 crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55))
109 public void add_duplications_from_a_single_block() {
110 settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
112 Collection<Block> originBlocks = singletonList(
113 // This block contains 11 tokens -> a duplication will be created
115 .setResourceId(ORIGIN_FILE_KEY)
116 .setBlockHash(new ByteArray("a8998353e96320ec"))
123 Collection<Block> duplicatedBlocks = singletonList(
125 .setResourceId(OTHER_FILE_KEY)
126 .setBlockHash(new ByteArray("a8998353e96320ec"))
132 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
134 assertThat(duplicationRepository.getDuplications(ORIGIN_FILE))
136 crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55))
141 public void add_no_duplication_from_current_file() {
142 settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
144 Collection<Block> originBlocks = asList(
146 .setResourceId(ORIGIN_FILE_KEY)
147 .setBlockHash(new ByteArray("a8998353e96320ec"))
152 // Duplication is on the same file
154 .setResourceId(ORIGIN_FILE_KEY)
155 .setBlockHash(new ByteArray("a8998353e96320ec"))
162 Collection<Block> duplicatedBlocks = singletonList(
164 .setResourceId(OTHER_FILE_KEY)
165 .setBlockHash(new ByteArray("a8998353e96320ed"))
171 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
173 assertNoDuplicationAdded(ORIGIN_FILE);
177 public void add_no_duplication_when_not_enough_tokens() {
178 settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
180 Collection<Block> originBlocks = singletonList(
181 // This block contains 5 tokens -> not enough to consider it as a duplication
183 .setResourceId(ORIGIN_FILE_KEY)
184 .setBlockHash(new ByteArray("a8998353e96320ec"))
191 Collection<Block> duplicatedBlocks = singletonList(
193 .setResourceId(OTHER_FILE_KEY)
194 .setBlockHash(new ByteArray("a8998353e96320ec"))
200 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
202 assertNoDuplicationAdded(ORIGIN_FILE);
206 public void add_no_duplication_when_no_duplicated_blocks() {
207 settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
209 Collection<Block> originBlocks = singletonList(
211 .setResourceId(ORIGIN_FILE_KEY)
212 .setBlockHash(new ByteArray("a8998353e96320ec"))
219 underTest.computeCpd(ORIGIN_FILE, originBlocks, Collections.<Block>emptyList());
221 assertNoDuplicationAdded(ORIGIN_FILE);
225 public void add_duplication_for_java_even_when_no_token() {
226 Component javaFile = builder(FILE, 1)
227 .setKey(ORIGIN_FILE_KEY)
228 .setFileAttributes(new FileAttributes(false, "java"))
231 Collection<Block> originBlocks = singletonList(
232 // This block contains 0 token
234 .setResourceId(ORIGIN_FILE_KEY)
235 .setBlockHash(new ByteArray("a8998353e96320ec"))
242 Collection<Block> duplicatedBlocks = singletonList(
244 .setResourceId(OTHER_FILE_KEY)
245 .setBlockHash(new ByteArray("a8998353e96320ec"))
251 underTest.computeCpd(javaFile, originBlocks, duplicatedBlocks);
253 assertThat(duplicationRepository.getDuplications(ORIGIN_FILE))
255 crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55))
260 public void default_minimum_tokens_is_one_hundred() {
261 settings.setProperty("sonar.cpd.xoo.minimumTokens", (Integer) null);
263 Collection<Block> originBlocks = singletonList(
265 .setResourceId(ORIGIN_FILE_KEY)
266 .setBlockHash(new ByteArray("a8998353e96320ec"))
273 Collection<Block> duplicatedBlocks = singletonList(
275 .setResourceId(OTHER_FILE_KEY)
276 .setBlockHash(new ByteArray("a8998353e96320ec"))
282 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
284 assertThat(duplicationRepository.getDuplications(ORIGIN_FILE))
286 crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55))
291 public void do_not_compute_more_than_one_hundred_duplications_when_too_many_duplicated_references() throws Exception {
292 Collection<Block> originBlocks = new ArrayList<>();
293 Collection<Block> duplicatedBlocks = new ArrayList<>();
295 Block.Builder blockBuilder = new Block.Builder()
296 .setResourceId(ORIGIN_FILE_KEY)
297 .setBlockHash(new ByteArray("a8998353e96320ec"))
301 originBlocks.add(blockBuilder.build());
303 // Generate more than 100 duplications of the same block
304 for (int i = 0; i < 110; i++) {
305 duplicatedBlocks.add(
307 .setResourceId(randomAlphanumeric(16))
312 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
314 assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly(
315 "Too many duplication references on file " + ORIGIN_FILE_KEY + " for block at line 30. Keeping only the first 100 references.");
316 Iterable<Duplication> duplications = duplicationRepository.getDuplications(ORIGIN_FILE);
317 assertThat(duplications).hasSize(1);
318 assertThat(duplications.iterator().next().getDuplicates()).hasSize(100);
322 public void do_not_compute_more_than_one_hundred_duplications_when_too_many_duplications() throws Exception {
323 Collection<Block> originBlocks = new ArrayList<>();
324 Collection<Block> duplicatedBlocks = new ArrayList<>();
326 Block.Builder blockBuilder = new Block.Builder()
331 // Generate more than 100 duplication on different files
332 for (int i = 0; i < 110; i++) {
333 String hash = padStart("hash" + i, 16, 'a');
336 .setResourceId(ORIGIN_FILE_KEY)
337 .setBlockHash(new ByteArray(hash))
339 duplicatedBlocks.add(
341 .setResourceId("resource" + i)
342 .setBlockHash(new ByteArray(hash))
347 underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
349 assertThat(duplicationRepository.getDuplications(ORIGIN_FILE)).hasSize(100);
350 assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Too many duplication groups on file " + ORIGIN_FILE_KEY + ". Keeping only the first 100 groups.");
353 private static Duplication crossProjectDuplication(TextBlock original, String otherFileKey, TextBlock duplicate) {
354 return new Duplication(original, Arrays.<Duplicate>asList(new CrossProjectDuplicate(otherFileKey, duplicate)));
357 private void assertNoDuplicationAdded(Component file) {
358 assertThat(duplicationRepository.getDuplications(file)).isEmpty();