use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.
the class FilterTest method one_part_of_C2_covers_two_parts_of_C1.
/**
* Given:
* <pre>
* c1: a[0-0], a[2-2], b[0-0], b[2-2]
* c2: a[0-2], b[0-2]
* </pre>
* Expected:
* <pre>
* c1 in c2 (all parts of c1 covered by parts of c2 and all resources the same)
* c2 not in c1 (not all parts of c2 covered by parts of c1 and all resources the same)
* </pre>
*/
@Test
public void one_part_of_C2_covers_two_parts_of_C1() {
// Note that line numbers don't matter for method which we test.
CloneGroup c1 = newCloneGroup(1, newClonePart("a", 0), newClonePart("a", 2), newClonePart("b", 0), newClonePart("b", 2));
CloneGroup c2 = newCloneGroup(3, newClonePart("a", 0), newClonePart("b", 0));
assertThat(Filter.containsIn(c1, c2), is(true));
assertThat(Filter.containsIn(c2, c1), is(false));
}
use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.
the class CpdExecutor method runCpdAnalysis.
@VisibleForTesting
void runCpdAnalysis(ExecutorService executorService, String componentKey, final Collection<Block> fileBlocks, long timeout) {
DefaultInputComponent component = (DefaultInputComponent) componentStore.getByKey(componentKey);
if (component == null) {
LOG.error("Resource not found in component store: {}. Skipping CPD computation for it", componentKey);
return;
}
InputFile inputFile = (InputFile) component;
LOG.debug("Detection of duplications for {}", inputFile.absolutePath());
progressReport.message(String.format("%d/%d - current file: %s", count, total, inputFile.absolutePath()));
List<CloneGroup> duplications;
Future<List<CloneGroup>> futureResult = executorService.submit(() -> SuffixTreeCloneDetectionAlgorithm.detect(index, fileBlocks));
try {
duplications = futureResult.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
LOG.warn("Timeout during detection of duplications for " + inputFile.absolutePath());
futureResult.cancel(true);
return;
} catch (Exception e) {
throw new IllegalStateException("Fail during detection of duplication for " + inputFile.absolutePath(), e);
}
List<CloneGroup> filtered;
if (!"java".equalsIgnoreCase(inputFile.language())) {
Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(getMinimumTokens(inputFile.language()));
filtered = from(duplications).filter(minimumTokensPredicate).toList();
} else {
filtered = duplications;
}
saveDuplications(component, filtered);
}
use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.
the class CpdExecutorTest method reportTooManyDuplications.
@Test
public void reportTooManyDuplications() throws Exception {
// 1 origin part + 101 duplicates = 102
List<CloneGroup> dups = new ArrayList<>(CpdExecutor.MAX_CLONE_GROUP_PER_FILE + 1);
for (int i = 0; i < CpdExecutor.MAX_CLONE_GROUP_PER_FILE + 1; i++) {
ClonePart clonePart = new ClonePart(batchComponent1.key(), i, i, i + 1);
ClonePart dupPart = new ClonePart(batchComponent1.key(), i + 1, i + 1, i + 2);
dups.add(newCloneGroup(clonePart, dupPart));
}
executor.saveDuplications(batchComponent1, dups);
assertThat(reader.readComponentDuplications(batchComponent1.batchId())).hasSize(CpdExecutor.MAX_CLONE_GROUP_PER_FILE);
assertThat(logTester.logs(LoggerLevel.WARN)).contains("Too many duplication groups on file " + batchComponent1 + ". Keep only the first " + CpdExecutor.MAX_CLONE_GROUP_PER_FILE + " groups.");
}
Aggregations