Search in sources :

Example 31 with CloneGroup

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));
}
Also used : CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Example 32 with CloneGroup

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);
}
Also used : DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) List(java.util.List) CloneGroup(org.sonar.duplications.index.CloneGroup) TimeoutException(java.util.concurrent.TimeoutException) InputFile(org.sonar.api.batch.fs.InputFile) TimeoutException(java.util.concurrent.TimeoutException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 33 with CloneGroup

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.");
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) ArrayList(java.util.ArrayList) CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Aggregations

CloneGroup (org.sonar.duplications.index.CloneGroup)33 Test (org.junit.Test)28 Block (org.sonar.duplications.block.Block)16 CloneIndex (org.sonar.duplications.index.CloneIndex)16 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)15 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)10 ClonePart (org.sonar.duplications.index.ClonePart)9 ArrayList (java.util.ArrayList)3 File (java.io.File)2 ByteArray (org.sonar.duplications.block.ByteArray)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 List (java.util.List)1 TimeoutException (java.util.concurrent.TimeoutException)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 PackedMemoryCloneIndex (org.sonar.duplications.index.PackedMemoryCloneIndex)1 Duplication (org.sonar.scanner.protocol.output.ScannerReport.Duplication)1