Search in sources :

Example 1 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class CpdExecutorTest method reportTooManyDuplicates.

@Test
public void reportTooManyDuplicates() throws Exception {
    // 1 origin part + 101 duplicates = 102
    List<ClonePart> parts = new ArrayList<>(CpdExecutor.MAX_CLONE_PART_PER_GROUP + 2);
    for (int i = 0; i < CpdExecutor.MAX_CLONE_PART_PER_GROUP + 2; i++) {
        parts.add(new ClonePart(batchComponent1.key(), i, i, i + 1));
    }
    List<CloneGroup> groups = Arrays.asList(CloneGroup.builder().setLength(0).setOrigin(parts.get(0)).setParts(parts).build());
    executor.saveDuplications(batchComponent1, groups);
    Duplication[] dups = readDuplications(1);
    assertThat(dups[0].getDuplicateList()).hasSize(CpdExecutor.MAX_CLONE_PART_PER_GROUP);
    assertThat(logTester.logs(LoggerLevel.WARN)).contains("Too many duplication references on file " + batchComponent1 + " for block at line 0. Keep only the first " + CpdExecutor.MAX_CLONE_PART_PER_GROUP + " references.");
}
Also used : Duplication(org.sonar.scanner.protocol.output.ScannerReport.Duplication) ClonePart(org.sonar.duplications.index.ClonePart) ArrayList(java.util.ArrayList) CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Example 2 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class IntegrateCrossProjectDuplications method computeCpd.

public void computeCpd(Component component, Collection<Block> originBlocks, Collection<Block> duplicationBlocks) {
    CloneIndex duplicationIndex = new PackedMemoryCloneIndex();
    populateIndex(duplicationIndex, originBlocks);
    populateIndex(duplicationIndex, duplicationBlocks);
    List<CloneGroup> duplications = SuffixTreeCloneDetectionAlgorithm.detect(duplicationIndex, originBlocks);
    Iterable<CloneGroup> filtered = duplications.stream().filter(getNumberOfUnitsNotLessThan(component.getFileAttributes().getLanguageKey())).collect(Collectors.toList());
    addDuplications(component, filtered);
}
Also used : PackedMemoryCloneIndex(org.sonar.duplications.index.PackedMemoryCloneIndex) CloneIndex(org.sonar.duplications.index.CloneIndex) CloneGroup(org.sonar.duplications.index.CloneGroup) PackedMemoryCloneIndex(org.sonar.duplications.index.PackedMemoryCloneIndex)

Example 3 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class IntegrateCrossProjectDuplications method addDuplications.

private void addDuplications(Component file, Iterable<CloneGroup> duplications) {
    int cloneGroupCount = 0;
    for (CloneGroup duplication : duplications) {
        cloneGroupCount++;
        if (cloneGroupCount > MAX_CLONE_GROUP_PER_FILE) {
            LOGGER.warn("Too many duplication groups on file {}. Keeping only the first {} groups.", file.getDbKey(), MAX_CLONE_GROUP_PER_FILE);
            break;
        }
        addDuplication(file, duplication);
    }
}
Also used : CloneGroup(org.sonar.duplications.index.CloneGroup)

Example 4 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class DuplicationsCollector method endOfGroup.

/**
 * Constructs CloneGroup and saves it.
 */
@Override
public void endOfGroup() {
    ClonePart origin = null;
    CloneGroup.Builder builder = CloneGroup.builder().setLength(length);
    List<ClonePart> parts = new ArrayList<>(count);
    for (int[] b : blockNumbers) {
        Block firstBlock = text.getBlock(b[0]);
        Block lastBlock = text.getBlock(b[1]);
        ClonePart part = new ClonePart(firstBlock.getResourceId(), firstBlock.getIndexInFile(), firstBlock.getStartLine(), lastBlock.getEndLine());
        // TODO Godin: maybe use FastStringComparator here ?
        if (originResourceId.equals(part.getResourceId())) {
            // part from origin
            if (origin == null) {
                origin = part;
                // To calculate length important to use the origin, because otherwise block may come from DB without required data
                builder.setLengthInUnits(lastBlock.getEndUnit() - firstBlock.getStartUnit() + 1);
            } else if (part.getUnitStart() < origin.getUnitStart()) {
                origin = part;
            }
        }
        parts.add(part);
    }
    Collections.sort(parts, ContainsInComparator.CLONEPART_COMPARATOR);
    builder.setOrigin(origin).setParts(parts);
    filter(builder.build());
    reset();
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) ArrayList(java.util.ArrayList) Block(org.sonar.duplications.block.Block) CloneGroup(org.sonar.duplications.index.CloneGroup)

Example 5 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class DetectorTestCase method exampleFromPaper.

/**
 * Given:
 * <pre>
 * y:   2 3 4 5
 * z:     3 4
 * x: 1 2 3 4 5 6
 * </pre>
 * Expected:
 * <pre>
 * x-y (2 3 4 5)
 * x-y-z (3 4)
 * </pre>
 */
@Test
public void exampleFromPaper() {
    CloneIndex index = createIndex(newBlocks("y", "2 3 4 5"), newBlocks("z", "3 4"));
    Block[] fileBlocks = newBlocks("x", "1 2 3 4 5 6");
    List<CloneGroup> result = detect(index, fileBlocks);
    print(result);
    assertEquals(2, result.size());
    assertThat(result, hasCloneGroup(4, newClonePart("x", 1, 4), newClonePart("y", 0, 4)));
    assertThat(result, hasCloneGroup(2, newClonePart("x", 2, 2), newClonePart("y", 1, 2), newClonePart("z", 0, 2)));
}
Also used : Block(org.sonar.duplications.block.Block) MemoryCloneIndex(org.sonar.duplications.index.MemoryCloneIndex) CloneIndex(org.sonar.duplications.index.CloneIndex) CloneGroup(org.sonar.duplications.index.CloneGroup) CloneGroupMatcher.hasCloneGroup(org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup) Test(org.junit.Test)

Aggregations

CloneGroup (org.sonar.duplications.index.CloneGroup)37 Test (org.junit.Test)30 CloneIndex (org.sonar.duplications.index.CloneIndex)17 Block (org.sonar.duplications.block.Block)16 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)15 ClonePart (org.sonar.duplications.index.ClonePart)11 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)10 ArrayList (java.util.ArrayList)5 File (java.io.File)2 ByteArray (org.sonar.duplications.block.ByteArray)2 PackedMemoryCloneIndex (org.sonar.duplications.index.PackedMemoryCloneIndex)2 Duplication (org.sonar.scanner.protocol.output.ScannerReport.Duplication)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